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

Fix MySQL collection stats #4145

Merged
merged 3 commits into from Mar 6, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
88 changes: 87 additions & 1 deletion internal/backends/mysql/collection.go
Expand Up @@ -366,7 +366,93 @@

// Stats implements backends.Collection interface.
func (c *collection) Stats(ctx context.Context, params *backends.CollectionStatsParams) (*backends.CollectionStatsResult, error) {
return nil, lazyerrors.New("not yet implemented")
p, err := c.r.DatabaseGetExisting(ctx, c.dbName)
if err != nil {
return nil, lazyerrors.Error(err)

Check warning on line 371 in internal/backends/mysql/collection.go

View check run for this annotation

Codecov / codecov/patch

internal/backends/mysql/collection.go#L369-L371

Added lines #L369 - L371 were not covered by tests
}

if p == nil {
return nil, backends.NewError(
backends.ErrorCodeCollectionDoesNotExist,
lazyerrors.Errorf("no ns %s.%s", c.dbName, c.name),
)

Check warning on line 378 in internal/backends/mysql/collection.go

View check run for this annotation

Codecov / codecov/patch

internal/backends/mysql/collection.go#L374-L378

Added lines #L374 - L378 were not covered by tests
}

coll, err := c.r.CollectionGet(ctx, c.dbName, c.name)
if err != nil {
return nil, lazyerrors.Error(err)

Check warning on line 383 in internal/backends/mysql/collection.go

View check run for this annotation

Codecov / codecov/patch

internal/backends/mysql/collection.go#L381-L383

Added lines #L381 - L383 were not covered by tests
}

if coll == nil {
return nil, backends.NewError(
backends.ErrorCodeCollectionDoesNotExist,
lazyerrors.Errorf("no ns %s.%s", c.dbName, c.name),
)

Check warning on line 390 in internal/backends/mysql/collection.go

View check run for this annotation

Codecov / codecov/patch

internal/backends/mysql/collection.go#L386-L390

Added lines #L386 - L390 were not covered by tests
}

stats, err := collectionsStats(ctx, p, c.dbName, []*metadata.Collection{coll}, params.Refresh)
if err != nil {
return nil, lazyerrors.Error(err)

Check warning on line 395 in internal/backends/mysql/collection.go

View check run for this annotation

Codecov / codecov/patch

internal/backends/mysql/collection.go#L393-L395

Added lines #L393 - L395 were not covered by tests
}

indexMap := map[string]string{}
for _, index := range coll.Indexes {
indexMap[index.Index] = index.Name

Check warning on line 400 in internal/backends/mysql/collection.go

View check run for this annotation

Codecov / codecov/patch

internal/backends/mysql/collection.go#L398-L400

Added lines #L398 - L400 were not covered by tests
}

q := `
SELECT
s.index_name,
t.index_length,
FROM information_schema.tables t
JOIN information_schema.statistics s
ON t.table_schema = s.table_schema AND t.table_name = s.table_name
WHERE t.table_schema = ? AND t.table_name IN ?
`

Check warning on line 411 in internal/backends/mysql/collection.go

View check run for this annotation

Codecov / codecov/patch

internal/backends/mysql/collection.go#L403-L411

Added lines #L403 - L411 were not covered by tests

rows, err := p.QueryContext(ctx, q, c.dbName, coll.TableName)
if err != nil {
return nil, lazyerrors.Error(err)

Check warning on line 415 in internal/backends/mysql/collection.go

View check run for this annotation

Codecov / codecov/patch

internal/backends/mysql/collection.go#L413-L415

Added lines #L413 - L415 were not covered by tests
}

defer rows.Close()

Check warning on line 418 in internal/backends/mysql/collection.go

View check run for this annotation

Codecov / codecov/patch

internal/backends/mysql/collection.go#L418

Added line #L418 was not covered by tests

indexSizes := make([]backends.IndexSize, len(indexMap))
var i int

Check warning on line 421 in internal/backends/mysql/collection.go

View check run for this annotation

Codecov / codecov/patch

internal/backends/mysql/collection.go#L420-L421

Added lines #L420 - L421 were not covered by tests

for rows.Next() {
var name string
var size int64

Check warning on line 425 in internal/backends/mysql/collection.go

View check run for this annotation

Codecov / codecov/patch

internal/backends/mysql/collection.go#L423-L425

Added lines #L423 - L425 were not covered by tests

if err := rows.Scan(&name, &size); err != nil {
return nil, lazyerrors.Error(err)

Check warning on line 428 in internal/backends/mysql/collection.go

View check run for this annotation

Codecov / codecov/patch

internal/backends/mysql/collection.go#L427-L428

Added lines #L427 - L428 were not covered by tests
}

indexName, ok := indexMap[name]
if !ok {

Check warning on line 432 in internal/backends/mysql/collection.go

View check run for this annotation

Codecov / codecov/patch

internal/backends/mysql/collection.go#L431-L432

Added lines #L431 - L432 were not covered by tests
// new indexes have been created since metadata was last fetched
continue

Check warning on line 434 in internal/backends/mysql/collection.go

View check run for this annotation

Codecov / codecov/patch

internal/backends/mysql/collection.go#L434

Added line #L434 was not covered by tests
}

indexSizes[i] = backends.IndexSize{
Name: indexName,
Size: size,

Check warning on line 439 in internal/backends/mysql/collection.go

View check run for this annotation

Codecov / codecov/patch

internal/backends/mysql/collection.go#L437-L439

Added lines #L437 - L439 were not covered by tests
}
i++

Check warning on line 441 in internal/backends/mysql/collection.go

View check run for this annotation

Codecov / codecov/patch

internal/backends/mysql/collection.go#L441

Added line #L441 was not covered by tests
}

if rows.Err() != nil {
return nil, lazyerrors.Error(rows.Err())

Check warning on line 445 in internal/backends/mysql/collection.go

View check run for this annotation

Codecov / codecov/patch

internal/backends/mysql/collection.go#L444-L445

Added lines #L444 - L445 were not covered by tests
}

return &backends.CollectionStatsResult{
CountDocuments: stats.countDocuments,
SizeTotal: stats.sizeTables + stats.sizeIndexes,
SizeIndexes: stats.sizeIndexes,
SizeCollection: stats.sizeTables,
SizeFreeStorage: stats.sizeFreeStorage,
IndexSizes: indexSizes,
}, nil

Check warning on line 455 in internal/backends/mysql/collection.go

View check run for this annotation

Codecov / codecov/patch

internal/backends/mysql/collection.go#L448-L455

Added lines #L448 - L455 were not covered by tests
}

// Compact implements backends.Collection interface.
Expand Down