Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement collStats for SQLite #3295

Merged
merged 22 commits into from
Sep 4, 2023

Conversation

chilagrow
Copy link
Member

@chilagrow chilagrow commented Aug 31, 2023

Description

Closes #3259.

Readiness checklist

  • I added/updated unit tests (and they pass).
  • I added/updated integration/compatibility tests (and they pass).
  • I added/updated comments and checked rendering.
  • I made spot refactorings.
  • I updated user documentation.
  • I ran task all, and it passed.
  • I ensured that PR title is good enough for the changelog.
  • (for maintainers only) I set Reviewers (@FerretDB/core), Milestone (Next), Labels, Project and project's Sprint fields.
  • I marked all done items in this checklist.

@chilagrow chilagrow added the code/feature Some user-visible feature is not implemented yet label Aug 31, 2023
@chilagrow chilagrow added this to the Next milestone Aug 31, 2023
@chilagrow chilagrow self-assigned this Aug 31, 2023
@codecov
Copy link

codecov bot commented Aug 31, 2023

Codecov Report

Merging #3295 (d69ffef) into main (76c3b68) will increase coverage by 0.12%.
The diff coverage is 80.18%.

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #3295      +/-   ##
==========================================
+ Coverage   75.09%   75.22%   +0.12%     
==========================================
  Files         400      400              
  Lines       23196    23374     +178     
==========================================
+ Hits        17420    17582     +162     
- Misses       4756     4765       +9     
- Partials     1020     1027       +7     
Files Changed Coverage
internal/handlers/sqlite/msg_collstats.go 66.19%
internal/backends/sqlite/database.go 82.14%
internal/handlers/sqlite/msg_aggregate.go 86.81%
internal/backends/sqlite/collection.go 88.00%
internal/backends/collection.go 100.00%
internal/backends/database.go 100.00%
internal/handlers/pg/msg_collstats.go 100.00%
internal/handlers/sqlite/msg_dbstats.go 100.00%
Flag Coverage Δ
hana ?
integration 71.80% <80.18%> (+0.15%) ⬆️
mongodb 5.06% <0.00%> (-0.04%) ⬇️
pg 61.96% <0.45%> (-0.49%) ⬇️
shard-1 56.60% <70.27%> (+0.88%) ⬆️
shard-2 55.61% <55.40%> (+1.13%) ⬆️
shard-3 54.35% <39.63%> (-1.60%) ⬇️
sqlite 55.45% <79.72%> (+0.41%) ⬆️
unit 24.82% <15.31%> (-0.06%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

@mergify
Copy link
Contributor

mergify bot commented Aug 31, 2023

@chilagrow this pull request has merge conflicts.

@mergify mergify bot added the conflict PRs that have merge conflicts label Aug 31, 2023
@mergify mergify bot removed the conflict PRs that have merge conflicts label Sep 1, 2023
@chilagrow
Copy link
Member Author

The numbers look fine

test> db.runCommand({collStats:"collection"})                                                                                                                                                                      
{                                                                                                                                                                                                                  
  ns: 'test.collection',                                                                                                                                                                                           
  size: Long("0"),                                                                                                                                                                                                 
  count: Long("0"),                                                                                                                                                                                                
  storageSize: Long("0"),                                                                                                                                                                                          
  nindexes: Long("0"),                                                                                                                                                                                             
  totalIndexSize: Long("0"),                                                                                                                                                                                       
  totalSize: Long("0"),                                                                                                                                                                                            
  scaleFactor: 1,                                                                                                                                                                                                  
  ok: 1                                                                                                                                                                                                            
}                                                                                                                                                                                                                  

test> db.collection.insertMany([{_id: 1}, {_id: 2}])                                                                                                                                                               
{ acknowledged: true, insertedIds: { '0': 1, '1': 2 } }                                                                                                                                                            
test> db.runCommand({collStats:"collection"})                                                                                                                                                                      
{                                                                                                                                                                                                                  
  ns: 'test.collection',                                                                                                                                                                                           
  size: Long("4096"),                                                                                                                                                                                              
  count: Long("2"),                                                                                                                                                                                                
  avgObjSize: Long("2048"),                                                                                                                                                                                        
  storageSize: Long("4096"),                                                                                                                                                                                       
  nindexes: Long("0"),                                                                                                                                                                                             
  totalIndexSize: Long("0"),                                                                                                                                                                                       
  totalSize: Long("4096"),                                                                                                                                                                                         
  scaleFactor: 1,                                                                                                                                                                                                  
  ok: 1                                                                                                                                                                                                            
}                                                                                                                                                                                                                  

test> db.collection.aggregate( [ { $collStats: {count: {}, storageStats: {}} } ] )                                                                                                                                 
[                                                                                                                                                                                                                  
  {                                                                                                                                                                                                                
    ns: 'test.collection',                                                                                                                                                                                         
    host: 'alma',                                                                                                                                                                                                  
    localTime: '2023-09-01T07:16:13Z',                                                                                                                                                                             
    storageStats: {                                                                                                                                                                                                
      size: Long("4096"),                                                                                                                                                                                          
      count: Long("2"),                                                                                                                                                                                    [0/1938]
      avgObjSize: Long("2048"),                                      
      storageSize: Long("4096"),                                     
      freeStorageSize: Long("0"),                                    
      capped: false,                                                 
      wiredTiger: {},                                                
      nindexes: Long("0"),                                           
      indexDetails: {},                                              
      indexBuilds: {},                                               
      totalIndexSize: Long("0"),                                     
      totalSize: Long("4096"),                                       
      indexSizes: {},                                                
      scaleFactor: Long("0")                                         
    },                                                               
    count: Long("2")                                                 
  }                                                                  
]                                                                    

@chilagrow chilagrow marked this pull request as ready for review September 1, 2023 07:40
@chilagrow chilagrow requested review from AlekSi and a team as code owners September 1, 2023 07:40
@chilagrow chilagrow requested review from rumyantseva, a team and noisersup September 1, 2023 07:40
@chilagrow chilagrow enabled auto-merge (squash) September 1, 2023 07:40
@mergify
Copy link
Contributor

mergify bot commented Sep 1, 2023

@chilagrow this pull request has merge conflicts.

@mergify mergify bot added the conflict PRs that have merge conflicts label Sep 1, 2023
defer observability.FuncCall(ctx)()

res, err := cc.c.Stats(ctx, params)
checkError(err, ErrorCodeCollectionDoesNotExist)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's do the same thing as with Database's Stats: return zero-filled struct without error. That's how we handled that "error" in the handler anyway, so why return an error at all?

Copy link
Member Author

@chilagrow chilagrow Sep 4, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For collStats command, indeed it returns zero-filled struct.
However, aggregation $collStats stage returns command error on non-existing name space, and existing compat test would fail if error isn't returned.

https://github.com/FerretDB/FerretDB/pull/3295/files#diff-b26835ea59cb934e35c55f22543373b2954d6a2f81ed7901d94d2b5e9ee0ade1R388

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then, could we do the opposite and return ErrorCodeDatabaseDoesNotExist in Database.Stats? That would make both Stats method consistent

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That would work, let me do that 👍

Copy link
Contributor

@rumyantseva rumyantseva left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No questions about the implementation (apart from what Alexey already asked).

I have a couple of questions about how we check errors, but it's about the new backend in general, not about this particular PR, so I'll ask on Slack.

@mergify mergify bot removed the conflict PRs that have merge conflicts label Sep 4, 2023
@@ -93,7 +93,7 @@ func (h *Handler) MsgCollStats(ctx context.Context, msg *wire.OpMsg) (*wire.OpMs
}

pairs = append(pairs,
"storageSize", stats.SizeTotal/scale,
"storageSize", stats.SizeCollection/scale,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix

storageSize does not include index size. See totalIndexSize for index sizing.

https://www.mongodb.com/docs/manual/reference/command/collStats/#mongodb-data-collStats.storageSize

defer observability.FuncCall(ctx)()

res, err := cc.c.Stats(ctx, params)
checkError(err, ErrorCodeCollectionDoesNotExist)
Copy link
Member Author

@chilagrow chilagrow Sep 4, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For collStats command, indeed it returns zero-filled struct.
However, aggregation $collStats stage returns command error on non-existing name space, and existing compat test would fail if error isn't returned.

https://github.com/FerretDB/FerretDB/pull/3295/files#diff-b26835ea59cb934e35c55f22543373b2954d6a2f81ed7901d94d2b5e9ee0ade1R388

@mergify
Copy link
Contributor

mergify bot commented Sep 4, 2023

@chilagrow this pull request has merge conflicts.

@mergify mergify bot added the conflict PRs that have merge conflicts label Sep 4, 2023
Copy link
Member

@AlekSi AlekSi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#3295 (comment) + plus there is a conflict now

@mergify mergify bot removed the conflict PRs that have merge conflicts label Sep 4, 2023
@chilagrow
Copy link
Member Author

I have a couple of questions about how we check errors, but it's about the new backend in general, not about this particular PR, so I'll ask on Slack.

@rumyantseva what are your questions?

@chilagrow chilagrow requested a review from AlekSi September 4, 2023 05:49
@AlekSi AlekSi disabled auto-merge September 4, 2023 10:52
@AlekSi AlekSi merged commit 4e8aba9 into FerretDB:main Sep 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
code/feature Some user-visible feature is not implemented yet
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

Implement dbStats and collStats for SQLite
3 participants