Skip to content

Commit

Permalink
Do not return stats in Backend.ListDatabases (#3588)
Browse files Browse the repository at this point in the history
  • Loading branch information
AlekSi committed Oct 17, 2023
1 parent c3888f1 commit 6131bf4
Show file tree
Hide file tree
Showing 10 changed files with 85 additions and 72 deletions.
11 changes: 9 additions & 2 deletions internal/backends/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@
package backends

import (
"cmp"
"context"

"github.com/prometheus/client_golang/prometheus"
"golang.org/x/exp/slices"

"github.com/FerretDB/FerretDB/internal/clientconn/conninfo"
"github.com/FerretDB/FerretDB/internal/util/must"
Expand Down Expand Up @@ -131,16 +133,21 @@ type ListDatabasesResult struct {
// DatabaseInfo represents information about a single database.
type DatabaseInfo struct {
Name string
Size int64
}

// ListDatabases returns a list of databases.
// ListDatabases returns a list of databases sorted by name.
func (bc *backendContract) ListDatabases(ctx context.Context, params *ListDatabasesParams) (*ListDatabasesResult, error) {
defer observability.FuncCall(ctx)()

res, err := bc.b.ListDatabases(ctx, params)
checkError(err)

if res != nil && len(res.Databases) > 0 {
must.BeTrue(slices.IsSortedFunc(res.Databases, func(a, b DatabaseInfo) int {
return cmp.Compare(a.Name, b.Name)
}))
}

return res, err
}

Expand Down
12 changes: 10 additions & 2 deletions internal/backends/collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,8 @@ type IndexSize struct {
Size int64
}

// Stats returns statistics about the collection.
// Stats returns statistic estimations about the collection.
// All returned values are not exact, but might be more accurate when Stats is called with `Refresh: true`.
//
// The errors for non-existing database and non-existing collection are the same.
func (cc *collectionContract) Stats(ctx context.Context, params *CollectionStatsParams) (*CollectionStatsResult, error) {
Expand Down Expand Up @@ -296,7 +297,7 @@ type IndexKeyPair struct {
Descending bool
}

// ListIndexes returns information about indexes in the database.
// ListIndexes returns a list of collection indexes.
//
// The errors for non-existing database and non-existing collection are the same.
func (cc *collectionContract) ListIndexes(ctx context.Context, params *ListIndexesParams) (*ListIndexesResult, error) {
Expand All @@ -305,6 +306,13 @@ func (cc *collectionContract) ListIndexes(ctx context.Context, params *ListIndex
res, err := cc.c.ListIndexes(ctx, params)
checkError(err, ErrorCodeCollectionDoesNotExist)

// TODO https://github.com/FerretDB/FerretDB/issues/3589
// if res != nil && len(res.Indexes) > 0 {
// must.BeTrue(slices.IsSortedFunc(res.Indexes, func(a, b IndexInfo) int {
// return cmp.Compare(a.Name, b.Name)
// }))
// }

return res, err
}

Expand Down
16 changes: 14 additions & 2 deletions internal/backends/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@
package backends

import (
"cmp"
"context"

"golang.org/x/exp/slices"

"github.com/FerretDB/FerretDB/internal/util/must"
"github.com/FerretDB/FerretDB/internal/util/observability"
)
Expand Down Expand Up @@ -94,15 +97,23 @@ func (ci *CollectionInfo) Capped() bool {
return ci.CappedSize > 0 || ci.CappedDocuments > 0
}

// ListCollections returns information about collections in the database.
// ListCollections returns a list collections in the database sorted by name.
//
// Database may not exist; that's not an error.
//
// Contract ensures that returned list is sorted by name.
func (dbc *databaseContract) ListCollections(ctx context.Context, params *ListCollectionsParams) (*ListCollectionsResult, error) {
defer observability.FuncCall(ctx)()

res, err := dbc.db.ListCollections(ctx, params)
checkError(err)

if res != nil && len(res.Collections) > 0 {
must.BeTrue(slices.IsSortedFunc(res.Collections, func(a, b CollectionInfo) int {
return cmp.Compare(a.Name, b.Name)
}))
}

return res, err
}

Expand Down Expand Up @@ -199,7 +210,8 @@ type DatabaseStatsResult struct {
SizeCollections int64
}

// Stats returns statistics about the database.
// Stats returns statistic estimations about the database.
// All returned values are not exact, but might be more accurate when Stats is called with `Refresh: true`.
func (dbc *databaseContract) Stats(ctx context.Context, params *DatabaseStatsParams) (*DatabaseStatsResult, error) {
defer observability.FuncCall(ctx)()

Expand Down
16 changes: 0 additions & 16 deletions internal/backends/postgresql/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,24 +130,8 @@ func (b *backend) ListDatabases(ctx context.Context, params *backends.ListDataba
}

for i, dbName := range list {
db, err := b.Database(dbName)
if err != nil {
return nil, lazyerrors.Error(err)
}

stats, err := db.Stats(ctx, new(backends.DatabaseStatsParams))
if backends.ErrorCodeIs(err, backends.ErrorCodeDatabaseDoesNotExist) {
stats = new(backends.DatabaseStatsResult)
err = nil
}

if err != nil {
return nil, lazyerrors.Error(err)
}

res.Databases[i] = backends.DatabaseInfo{
Name: dbName,
Size: stats.SizeTotal,
}
}

Expand Down
5 changes: 5 additions & 0 deletions internal/backends/postgresql/collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,11 @@ func (c *collection) ListIndexes(ctx context.Context, params *backends.ListIndex
}
}

// TODO https://github.com/FerretDB/FerretDB/issues/3589
// slices.SortFunc(res.Indexes, func(a, b backends.IndexInfo) int {
// return cmp.Compare(a.Name, b.Name)
// })

return &res, nil
}

Expand Down
16 changes: 0 additions & 16 deletions internal/backends/sqlite/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,24 +106,8 @@ func (b *backend) ListDatabases(ctx context.Context, params *backends.ListDataba
}

for i, dbName := range list {
db, err := b.Database(dbName)
if err != nil {
return nil, lazyerrors.Error(err)
}

stats, err := db.Stats(ctx, new(backends.DatabaseStatsParams))
if backends.ErrorCodeIs(err, backends.ErrorCodeDatabaseDoesNotExist) {
stats = new(backends.DatabaseStatsResult)
err = nil
}

if err != nil {
return nil, lazyerrors.Error(err)
}

res.Databases[i] = backends.DatabaseInfo{
Name: dbName,
Size: stats.SizeTotal,
}
}

Expand Down
5 changes: 5 additions & 0 deletions internal/backends/sqlite/collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,11 @@ func (c *collection) ListIndexes(ctx context.Context, params *backends.ListIndex
}
}

// TODO https://github.com/FerretDB/FerretDB/issues/3589
// slices.SortFunc(res.Indexes, func(a, b backends.IndexInfo) int {
// return cmp.Compare(a.Name, b.Name)
// })

return &res, nil
}

Expand Down
18 changes: 7 additions & 11 deletions internal/handlers/sqlite/msg_aggregate.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,16 @@
package sqlite

import (
"cmp"
"context"
"errors"
"fmt"
"math"
"os"
"time"

"golang.org/x/exp/slices"

"github.com/FerretDB/FerretDB/internal/backends"
"github.com/FerretDB/FerretDB/internal/clientconn/conninfo"
"github.com/FerretDB/FerretDB/internal/clientconn/cursor"
Expand Down Expand Up @@ -402,17 +405,10 @@ func processStagesStats(ctx context.Context, closer *iterator.MultiCloser, p *st
return nil, lazyerrors.Error(err)
}

var found bool

for _, cInfo := range cList.Collections {
if cInfo.Name == p.cName {
found = true
break
}
}

if !found {
cInfo = backends.CollectionInfo{}
if i, found := slices.BinarySearchFunc(cList.Collections, p.cName, func(e backends.CollectionInfo, t string) int {
return cmp.Compare(e.Name, t)
}); found {
cInfo = cList.Collections[i]
}

var iList *backends.ListIndexesResult
Expand Down
20 changes: 8 additions & 12 deletions internal/handlers/sqlite/msg_collstats.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@
package sqlite

import (
"cmp"
"context"
"fmt"

"golang.org/x/exp/slices"

"github.com/FerretDB/FerretDB/internal/backends"
"github.com/FerretDB/FerretDB/internal/handlers/common"
"github.com/FerretDB/FerretDB/internal/handlers/commonerrors"
Expand Down Expand Up @@ -76,23 +79,16 @@ func (h *Handler) MsgCollStats(ctx context.Context, msg *wire.OpMsg) (*wire.OpMs
return nil, lazyerrors.Error(err)
}

list, err := db.ListCollections(ctx, new(backends.ListCollectionsParams))
collections, err := db.ListCollections(ctx, new(backends.ListCollectionsParams))
if err != nil {
return nil, lazyerrors.Error(err)
}

var found bool
var cInfo backends.CollectionInfo

for _, cInfo := range list.Collections {
if cInfo.Name == collection {
found = true
break
}
}

if !found {
cInfo = backends.CollectionInfo{}
if i, found := slices.BinarySearchFunc(collections.Collections, collection, func(e backends.CollectionInfo, t string) int {
return cmp.Compare(e.Name, t)
}); found {
cInfo = collections.Collections[i]
}

indexes, err := c.ListIndexes(ctx, new(backends.ListIndexesParams))
Expand Down
38 changes: 27 additions & 11 deletions internal/handlers/sqlite/msg_listdatabases.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ package sqlite
import (
"context"

"go.uber.org/zap"

"github.com/FerretDB/FerretDB/internal/handlers/common"
"github.com/FerretDB/FerretDB/internal/handlers/commonparams"
"github.com/FerretDB/FerretDB/internal/types"
Expand Down Expand Up @@ -56,14 +58,34 @@ func (h *Handler) MsgListDatabases(ctx context.Context, msg *wire.OpMsg) (*wire.

databases := types.MakeArray(len(res.Databases))

for _, db := range res.Databases {
for _, dbInfo := range res.Databases {
if nameOnly {
databases.Append(must.NotFail(types.NewDocument(
"name", dbInfo.Name,
)))

continue
}

db, err := h.b.Database(dbInfo.Name)
if err != nil {
h.L.Warn("Failed to get database", zap.Error(err))
continue
}

stats, err := db.Stats(ctx, nil)
if err != nil {
h.L.Warn("Failed to get database stats", zap.Error(err))
continue
}

d := must.NotFail(types.NewDocument(
"name", db.Name,
"sizeOnDisk", db.Size,
"empty", db.Size == 0,
"name", dbInfo.Name,
"sizeOnDisk", stats.SizeTotal,
"empty", stats.SizeTotal == 0,
))

totalSize += db.Size
totalSize += stats.SizeTotal

matches, err := common.FilterDocument(d, filter)
if err != nil {
Expand All @@ -74,12 +96,6 @@ func (h *Handler) MsgListDatabases(ctx context.Context, msg *wire.OpMsg) (*wire.
continue
}

if nameOnly {
d = must.NotFail(types.NewDocument(
"name", db.Name,
))
}

databases.Append(d)
}

Expand Down

0 comments on commit 6131bf4

Please sign in to comment.