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 MsgCount for Tigris #928

Merged
merged 22 commits into from
Jul 26, 2022
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -129,19 +129,25 @@ tasks:
desc: "Run integration tests for PostgreSQL handler"
dir: integration
cmds:
- go test -count=1 {{if ne OS "windows"}}-race{{end}} -shuffle=on -coverprofile=integration-pg.txt -coverpkg=../... -handler=pg
- >
AlekSi marked this conversation as resolved.
Show resolved Hide resolved
go test -count=1 {{if ne OS "windows"}}-race{{end}} -shuffle=on -coverpkg=../...
-coverprofile=integration-pg.txt . -handler=pg

test-integration-tigris:
desc: "Run integration tests for Tigris handler"
dir: integration/tigris
dir: integration
cmds:
- go test -count=1 {{if ne OS "windows"}}-race{{end}} -tags=ferretdb_tigris -shuffle=on -coverprofile=../integration-tigris.txt -coverpkg=../... -handler=tigris
- >
go test -count=1 {{if ne OS "windows"}}-race{{end}} -shuffle=on -coverpkg=../...
-coverprofile=integration-tigris.txt -tags=ferretdb_tigris ./tigris/... -handler=tigris

test-integration-mongodb:
desc: "Run integration tests for MongoDB"
dir: integration
cmds:
- go test -count=1 {{if ne OS "windows"}}-race{{end}} -shuffle=on -coverprofile=integration-mongodb.txt -coverpkg=../... -target-port=37017 -compat-port=0
- >
go test -count=1 {{if ne OS "windows"}}-race{{end}} -shuffle=on -coverpkg=../...
-coverprofile=integration-mongodb.txt . -target-port=37017 -compat-port=0

bench-short:
desc: "Benchmark for about 20 seconds (with default BENCHTIME)"
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ require (
golang.org/x/exp v0.0.0-20220713135740-79cabaa25d75
golang.org/x/net v0.0.0-20220708220712-1185a9018129
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8
google.golang.org/grpc v1.48.0
google.golang.org/grpc v1.48.0 // indirect
AlekSi marked this conversation as resolved.
Show resolved Hide resolved
)

require (
Expand Down
53 changes: 53 additions & 0 deletions integration/tigris/smoke_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@
package tigris

import (
"math"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.mongodb.org/mongo-driver/bson"

Expand All @@ -34,3 +36,54 @@ func TestSmoke(t *testing.T) {
require.NoError(t, err)
integration.AssertEqualDocuments(t, bson.D{{"_id", "fixed_double"}, {"double_value", 42.13}}, doc)
}

// TestSmokeMsgCount implements simple smoke tests for MsgCount.
// TODO Implement proper testing: https://github.com/FerretDB/FerretDB/issues/931.
func TestSmokeMsgCount(t *testing.T) {
t.Parallel()

// As Tigris require different fields for different types,
// for this smoke test we only use the fixed scalars.
ctx, collection := setup.Setup(t, shareddata.FixedScalars)

for name, tc := range map[string]struct {
command any
response int32
}{
"CountAllFixedScalars": {
command: bson.D{{"count", collection.Name()}},
response: 6,
},
"CountExactlyOneDocument": {
command: bson.D{
{"count", collection.Name()},
{"query", bson.D{{"double_value", math.MaxFloat64}}},
},
response: 1,
},
"CountNonExistingCollection": {
command: bson.D{
{"count", "doesnotexist"},
{"query", bson.D{{"v", true}}},
},
response: 0,
},
} {
name, tc := name, tc
t.Run(name, func(t *testing.T) {
t.Parallel()

var actual bson.D
err := collection.Database().RunCommand(ctx, tc.command).Decode(&actual)
require.NoError(t, err)

m := actual.Map()

assert.Equal(t, float64(1), m["ok"])

keys := integration.CollectKeys(t, actual)
assert.Contains(t, keys, "n")
assert.Equal(t, tc.response, m["n"])
})
}
}
17 changes: 16 additions & 1 deletion internal/handlers/tigris/fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ package tigris
import (
"context"

api "github.com/tigrisdata/tigris-client-go/api/server/v1"
"github.com/tigrisdata/tigris-client-go/driver"
"go.uber.org/zap"

"github.com/FerretDB/FerretDB/internal/tjson"
"github.com/FerretDB/FerretDB/internal/types"
Expand All @@ -37,7 +39,20 @@ func (h *Handler) fetch(ctx context.Context, param fetchParam) ([]*types.Documen
db := h.driver.UseDatabase(param.db)

collection, err := db.DescribeCollection(ctx, param.collection)
if err != nil {
switch err := err.(type) {
case nil:
// do nothing
case *driver.Error:
//nolint:nosnakecase // Tigris named their const that way
if err.Code == api.Code_NOT_FOUND {
h.L.Debug(
"Collection doesn't exist, handling a case to deal with a non-existing collection (return empty list)",
zap.String("db", param.db), zap.String("collection", param.collection),
)
return []*types.Document{}, nil
}
return nil, lazyerrors.Error(err)
default:
return nil, lazyerrors.Error(err)
}

Expand Down
101 changes: 99 additions & 2 deletions internal/handlers/tigris/msg_count.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,110 @@ package tigris

import (
"context"
"fmt"

"github.com/FerretDB/FerretDB/internal/handlers/common"
"github.com/FerretDB/FerretDB/internal/types"
"github.com/FerretDB/FerretDB/internal/util/lazyerrors"
"github.com/FerretDB/FerretDB/internal/util/must"
"github.com/FerretDB/FerretDB/internal/wire"
)

// MsgCount implements HandlerInterface.
func (h *Handler) MsgCount(ctx context.Context, msg *wire.OpMsg) (*wire.OpMsg, error) {
// TODO https://github.com/FerretDB/FerretDB/issues/771
return nil, notImplemented(must.NotFail(msg.Document()).Command())
document, err := msg.Document()
if err != nil {
return nil, lazyerrors.Error(err)
}

unimplementedFields := []string{
"skip",
"returnKey",
"showRecordId",
"tailable",
"oplogReplay",
"noCursorTimeout",
"awaitData",
"allowPartialResults",
"collation",
"allowDiskUse",
"let",
AlekSi marked this conversation as resolved.
Show resolved Hide resolved
}
if err := common.Unimplemented(document, unimplementedFields...); err != nil {
return nil, err
}
ignoredFields := []string{
"hint",
"batchSize",
"singleBatch",
"maxTimeMS",
"readConcern",
"max",
"min",
rumyantseva marked this conversation as resolved.
Show resolved Hide resolved
}
common.Ignored(document, h.L, ignoredFields...)

var filter *types.Document
if filter, err = common.GetOptionalParam(document, "query", filter); err != nil {
return nil, err
}

var limit int64
if l, _ := document.Get("limit"); l != nil {
if limit, err = common.GetWholeNumberParam(l); err != nil {
return nil, err
}
}

var fp fetchParam
if fp.db, err = common.GetRequiredParam[string](document, "$db"); err != nil {
return nil, err
}
collectionParam, err := document.Get(document.Command())
if err != nil {
return nil, err
}
var ok bool
if fp.collection, ok = collectionParam.(string); !ok {
return nil, common.NewErrorMsg(
common.ErrBadValue,
fmt.Sprintf("collection name has invalid type %s", common.AliasFromType(collectionParam)),
)
}

fetchedDocs, err := h.fetch(ctx, fp)
if err != nil {
return nil, err
}

resDocs := make([]*types.Document, 0, 16)
for _, doc := range fetchedDocs {
matches, err := common.FilterDocument(doc, filter)
if err != nil {
return nil, err
}

if !matches {
continue
}

resDocs = append(resDocs, doc)
}

if resDocs, err = common.LimitDocuments(resDocs, limit); err != nil {
return nil, err
}

var reply wire.OpMsg
err = reply.SetSections(wire.OpMsgSection{
Documents: []*types.Document{must.NotFail(types.NewDocument(
"n", int32(len(resDocs)),
"ok", float64(1),
))},
})
if err != nil {
return nil, lazyerrors.Error(err)
}

return &reply, nil
}
15 changes: 11 additions & 4 deletions internal/handlers/tigris/msg_drop.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ package tigris
import (
"context"

"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
api "github.com/tigrisdata/tigris-client-go/api/server/v1"
"github.com/tigrisdata/tigris-client-go/driver"

"github.com/FerretDB/FerretDB/internal/handlers/common"
"github.com/FerretDB/FerretDB/internal/types"
Expand Down Expand Up @@ -46,11 +46,18 @@ func (h *Handler) MsgDrop(ctx context.Context, msg *wire.OpMsg) (*wire.OpMsg, er
return nil, err
}

if err = h.driver.UseDatabase(db).DropCollection(ctx, collection); err != nil {
if status.Code(err) == codes.NotFound {
err = h.driver.UseDatabase(db).DropCollection(ctx, collection)
switch err := err.(type) {
case nil:
// do nothing
case *driver.Error:
//nolint:nosnakecase // Tigris named their const that way
if err.Code == api.Code_NOT_FOUND {
AlekSi marked this conversation as resolved.
Show resolved Hide resolved
return nil, common.NewErrorMsg(common.ErrNamespaceNotFound, "ns not found")
}
return nil, lazyerrors.Error(err)
default:
return nil, lazyerrors.Error(err)
}

var reply wire.OpMsg
Expand Down