Skip to content

GODRIVER-2388 Improved Bulk Write API. #1884

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

Merged
merged 15 commits into from
Jan 24, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions internal/driverutil/operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,5 @@ const (
ListIndexesOp = "listIndexes" // ListIndexesOp is the name for listing indexes
ListDatabasesOp = "listDatabases" // ListDatabasesOp is the name for listing databases
UpdateOp = "update" // UpdateOp is the name for updating
BulkWriteOp = "bulkWrite" // BulkWriteOp is the name for client-level bulk write
)
4 changes: 2 additions & 2 deletions internal/integration/client_side_encryption_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -394,8 +394,8 @@ func TestClientSideEncryptionCustomCrypt(t *testing.T) {
"expected 0 calls to DecryptExplicit, got %v", cc.numDecryptExplicitCalls)
assert.Equal(mt, cc.numCloseCalls, 0,
"expected 0 calls to Close, got %v", cc.numCloseCalls)
assert.Equal(mt, cc.numBypassAutoEncryptionCalls, 2,
"expected 2 calls to BypassAutoEncryption, got %v", cc.numBypassAutoEncryptionCalls)
assert.Equal(mt, cc.numBypassAutoEncryptionCalls, 1,
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

We only call it once after the operation.go refactoring.

"expected 1 call to BypassAutoEncryption, got %v", cc.numBypassAutoEncryptionCalls)
})
}

Expand Down
123 changes: 123 additions & 0 deletions internal/integration/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"go.mongodb.org/mongo-driver/v2/mongo"
"go.mongodb.org/mongo-driver/v2/mongo/options"
"go.mongodb.org/mongo-driver/v2/mongo/readpref"
"go.mongodb.org/mongo-driver/v2/mongo/writeconcern"
"go.mongodb.org/mongo-driver/v2/x/bsonx/bsoncore"
"go.mongodb.org/mongo-driver/v2/x/mongo/driver"
"go.mongodb.org/mongo-driver/v2/x/mongo/driver/wiremessage"
Expand Down Expand Up @@ -720,6 +721,128 @@ func TestClient(t *testing.T) {
})
}

func TestClient_BulkWrite(t *testing.T) {
mt := mtest.New(t, noClientOpts)

mtBulkWriteOpts := mtest.NewOptions().MinServerVersion("8.0").AtlasDataLake(false).ClientType(mtest.Pinned)
mt.RunOpts("bulk write with nil filter", mtBulkWriteOpts, func(mt *mtest.T) {
mt.Parallel()

testCases := []struct {
name string
writes []mongo.ClientBulkWrite
errorString string
}{
{
name: "DeleteOne",
writes: []mongo.ClientBulkWrite{{
Database: "foo",
Collection: "bar",
Model: mongo.NewClientDeleteOneModel(),
}},
errorString: "delete filter cannot be nil",
},
{
name: "DeleteMany",
writes: []mongo.ClientBulkWrite{{
Database: "foo",
Collection: "bar",
Model: mongo.NewClientDeleteManyModel(),
}},
errorString: "delete filter cannot be nil",
},
{
name: "UpdateOne",
writes: []mongo.ClientBulkWrite{{
Database: "foo",
Collection: "bar",
Model: mongo.NewClientUpdateOneModel(),
}},
errorString: "update filter cannot be nil",
},
{
name: "UpdateMany",
writes: []mongo.ClientBulkWrite{{
Database: "foo",
Collection: "bar",
Model: mongo.NewClientUpdateManyModel(),
}},
errorString: "update filter cannot be nil",
},
}
for _, tc := range testCases {
tc := tc

mt.Run(tc.name, func(mt *mtest.T) {
mt.Parallel()

_, err := mt.Client.BulkWrite(context.Background(), tc.writes)
require.EqualError(mt, err, tc.errorString)
})
}
})
mt.RunOpts("bulk write with write concern", mtBulkWriteOpts, func(mt *mtest.T) {
mt.Parallel()

testCases := []struct {
name string
opts *options.ClientBulkWriteOptionsBuilder
want bool
}{
{
name: "unacknowledged",
opts: options.ClientBulkWrite().SetWriteConcern(writeconcern.Unacknowledged()).SetOrdered(false),
want: false,
},
{
name: "acknowledged",
want: true,
},
}
for _, tc := range testCases {
tc := tc

mt.Run(tc.name, func(mt *mtest.T) {
mt.Parallel()

insertOneModel := mongo.NewClientInsertOneModel().SetDocument(bson.D{{"x", 1}})
writes := []mongo.ClientBulkWrite{{
Database: "foo",
Collection: "bar",
Model: insertOneModel,
}}
res, err := mt.Client.BulkWrite(context.Background(), writes, tc.opts)
require.NoError(mt, err, "BulkWrite error: %v", err)
require.NotNil(mt, res, "expected a ClientBulkWriteResult")
assert.Equal(mt, res.Acknowledged, tc.want, "expected Acknowledged: %v, got: %v", tc.want, res.Acknowledged)
})
}
})
var bulkWrites int
cmdMonitor := &event.CommandMonitor{
Started: func(_ context.Context, evt *event.CommandStartedEvent) {
if evt.CommandName == "bulkWrite" {
bulkWrites++
}
},
}
clientOpts := options.Client().SetMonitor(cmdMonitor)
mt.RunOpts("bulk write with large messages", mtBulkWriteOpts.ClientOptions(clientOpts), func(mt *mtest.T) {
mt.Parallel()

document := bson.D{{"largeField", strings.Repeat("a", 16777216-100)}} // Adjust size to account for BSON overhead
writes := []mongo.ClientBulkWrite{
{"db", "x", mongo.NewClientInsertOneModel().SetDocument(document)},
{"db", "x", mongo.NewClientInsertOneModel().SetDocument(document)},
{"db", "x", mongo.NewClientInsertOneModel().SetDocument(document)},
}

_, err := mt.Client.BulkWrite(context.Background(), writes)
require.NoError(t, err)
assert.Equal(t, 2, bulkWrites, "expected %d bulkWrites, got %d", 2, bulkWrites)
})
}

func TestClient_BSONOptions(t *testing.T) {
t.Parallel()

Expand Down
42 changes: 41 additions & 1 deletion internal/integration/collection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ func TestCollection(t *testing.T) {
mt.Run("large document batches", func(mt *mtest.T) {
mt.Parallel()

docs := []interface{}{create16MBDocument(mt), create16MBDocument(mt)}
docs := []interface{}{create16MBDocument(mt), create16MBDocument(mt), create16MBDocument(mt)}
_, err := mt.Coll.InsertMany(context.Background(), docs)
assert.Nil(mt, err, "InsertMany error: %v", err)
evt := mt.GetStartedEvent()
Expand Down Expand Up @@ -1715,6 +1715,46 @@ func TestCollection(t *testing.T) {
})
}
})
mt.Run("error on nil filter", func(mt *mtest.T) {
mt.Parallel()

testCases := []struct {
name string
model mongo.WriteModel
errorString string
}{
{
name: "DeleteOne",
model: mongo.NewDeleteOneModel(),
errorString: "delete filter cannot be nil",
},
{
name: "DeleteMany",
model: mongo.NewDeleteManyModel(),
errorString: "delete filter cannot be nil",
},
{
name: "UpdateOne",
model: mongo.NewUpdateOneModel().SetUpdate(bson.D{{"$set", bson.D{{"x", 1}}}}),
errorString: "update filter cannot be nil",
},
{
name: "UpdateMany",
model: mongo.NewUpdateManyModel().SetUpdate(bson.D{{"$set", bson.D{{"x", 1}}}}),
errorString: "update filter cannot be nil",
},
}
for _, tc := range testCases {
tc := tc

mt.Run(tc.name, func(mt *mtest.T) {
mt.Parallel()

_, err := mt.Coll.BulkWrite(context.Background(), []mongo.WriteModel{tc.model})
assert.EqualError(mt, err, tc.errorString)
})
}
})
mt.Run("correct model in errors", func(mt *mtest.T) {
models := []mongo.WriteModel{
mongo.NewUpdateOneModel().SetFilter(bson.M{}).SetUpdate(bson.M{
Expand Down
Loading
Loading