Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
qingyang-hu committed Sep 27, 2023
1 parent f7daae8 commit f050dc0
Show file tree
Hide file tree
Showing 30 changed files with 113 additions and 366 deletions.
23 changes: 17 additions & 6 deletions examples/documentation_examples/examples.go
Original file line number Diff line number Diff line change
Expand Up @@ -1763,7 +1763,7 @@ func UpdateEmployeeInfo(ctx context.Context, client *mongo.Client) error {
return client.UseSession(ctx, func(sctx mongo.SessionContext) error {
err := sctx.StartTransaction(options.Transaction().
SetReadConcern(readconcern.Snapshot()).
SetWriteConcern(writeconcern.New(writeconcern.WMajority())),
SetWriteConcern(writeconcern.Majority()),
)
if err != nil {
return err
Expand Down Expand Up @@ -1921,7 +1921,7 @@ func TransactionsExamples(ctx context.Context, client *mongo.Client) error {

err := sctx.StartTransaction(options.Transaction().
SetReadConcern(readconcern.Snapshot()).
SetWriteConcern(writeconcern.New(writeconcern.WMajority())),
SetWriteConcern(writeconcern.Majority()),
)
if err != nil {
return err
Expand Down Expand Up @@ -1971,7 +1971,10 @@ func WithTransactionExample(ctx context.Context) error {
defer func() { _ = client.Disconnect(ctx) }()

// Prereq: Create collections.
wcMajority := writeconcern.New(writeconcern.WMajority(), writeconcern.WTimeout(1*time.Second))
wcMajority := &writeconcern.WriteConcern{
W: "majority",
WTimeout: 1 * time.Second,
}
wcMajorityCollectionOpts := options.Collection().SetWriteConcern(wcMajority)
fooColl := client.Database("mydb1").Collection("foo", wcMajorityCollectionOpts)
barColl := client.Database("mydb1").Collection("bar", wcMajorityCollectionOpts)
Expand Down Expand Up @@ -2552,7 +2555,11 @@ func CausalConsistencyExamples(client *mongo.Client) error {

// Use a causally-consistent session to run some operations
opts := options.Session().SetDefaultReadConcern(readconcern.Majority()).SetDefaultWriteConcern(
writeconcern.New(writeconcern.WMajority(), writeconcern.WTimeout(1000)))
&writeconcern.WriteConcern{
W: "majority",
WTimeout: 1000,
},
)
session1, err := client.StartSession(opts)
if err != nil {
return err
Expand Down Expand Up @@ -2585,8 +2592,12 @@ func CausalConsistencyExamples(client *mongo.Client) error {

// Make a new session that is causally consistent with session1 so session2 reads what session1 writes
opts = options.Session().SetDefaultReadPreference(readpref.Secondary()).SetDefaultReadConcern(
readconcern.Majority()).SetDefaultWriteConcern(writeconcern.New(writeconcern.WMajority(),
writeconcern.WTimeout(1000)))
readconcern.Majority()).SetDefaultWriteConcern(
&writeconcern.WriteConcern{
W: "majority",
WTimeout: 1000,
},
)
session2, err := client.StartSession(opts)
if err != nil {
return err
Expand Down
2 changes: 1 addition & 1 deletion mongo/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ const (
var (
// keyVaultCollOpts specifies options used to communicate with the key vault collection
keyVaultCollOpts = options.Collection().SetReadConcern(readconcern.Majority()).
SetWriteConcern(writeconcern.New(writeconcern.WMajority()))
SetWriteConcern(writeconcern.Majority())

endSessionsBatchSize = 10000
)
Expand Down
4 changes: 2 additions & 2 deletions mongo/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ func TestClient(t *testing.T) {
}
})
t.Run("write concern", func(t *testing.T) {
wc := writeconcern.New(writeconcern.WMajority())
wc := writeconcern.Majority()
client := setupClient(options.Client().SetWriteConcern(wc))
assert.Equal(t, wc, client.writeConcern, "mismatch; expected write concern %v, got %v", wc, client.writeConcern)
})
Expand Down Expand Up @@ -354,7 +354,7 @@ func TestClient(t *testing.T) {
},
}
clientOpts := options.Client().ApplyURI(cs.Original).SetReadPreference(readpref.Primary()).
SetWriteConcern(writeconcern.New(writeconcern.WMajority())).SetMonitor(cmdMonitor)
SetWriteConcern(writeconcern.Majority()).SetMonitor(cmdMonitor)
integtest.AddTestServerAPIVersion(clientOpts)
client, err := Connect(bgCtx, clientOpts)
assert.Nil(t, err, "Connect error: %v", err)
Expand Down
14 changes: 7 additions & 7 deletions mongo/collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ func (coll *Collection) BulkWrite(ctx context.Context, models []WriteModel,
if sess.TransactionRunning() {
wc = nil
}
if !writeconcern.AckWrite(wc) {
if !wc.Acknowledged() {
sess = nil
}

Expand Down Expand Up @@ -323,7 +323,7 @@ func (coll *Collection) insert(ctx context.Context, documents []interface{},
if sess.TransactionRunning() {
wc = nil
}
if !writeconcern.AckWrite(wc) {
if !wc.Acknowledged() {
sess = nil
}

Expand Down Expand Up @@ -504,7 +504,7 @@ func (coll *Collection) delete(ctx context.Context, filter interface{}, deleteOn
if sess.TransactionRunning() {
wc = nil
}
if !writeconcern.AckWrite(wc) {
if !wc.Acknowledged() {
sess = nil
}

Expand Down Expand Up @@ -689,7 +689,7 @@ func (coll *Collection) updateOrReplace(ctx context.Context, filter bsoncore.Doc
if sess.TransactionRunning() {
wc = nil
}
if !writeconcern.AckWrite(wc) {
if !wc.Acknowledged() {
sess = nil
}

Expand Down Expand Up @@ -986,7 +986,7 @@ func aggregate(a aggregateParams) (cur *Cursor, err error) {
wc = nil
rc = nil
}
if !writeconcern.AckWrite(wc) {
if !wc.Acknowledged() {
closeImplicitSession(sess)
sess = nil
}
Expand Down Expand Up @@ -1699,7 +1699,7 @@ func (coll *Collection) findAndModify(ctx context.Context, op *operation.FindAnd
if sess.TransactionRunning() {
wc = nil
}
if !writeconcern.AckWrite(wc) {
if !wc.Acknowledged() {
sess = nil
}

Expand Down Expand Up @@ -2229,7 +2229,7 @@ func (coll *Collection) drop(ctx context.Context) error {
if sess.TransactionRunning() {
wc = nil
}
if !writeconcern.AckWrite(wc) {
if !wc.Acknowledged() {
sess = nil
}

Expand Down
6 changes: 3 additions & 3 deletions mongo/collection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ func TestCollection(t *testing.T) {
t.Run("specified options", func(t *testing.T) {
rpPrimary := readpref.Primary()
rpSecondary := readpref.Secondary()
wc1 := writeconcern.New(writeconcern.W(5))
wc2 := writeconcern.New(writeconcern.W(10))
wc1 := &writeconcern.WriteConcern{W: 5}
wc2 := &writeconcern.WriteConcern{W: 10}
rcLocal := readconcern.Local()
rcMajority := readconcern.Majority()

Expand All @@ -64,7 +64,7 @@ func TestCollection(t *testing.T) {
t.Run("inherit options", func(t *testing.T) {
rpPrimary := readpref.Primary()
rcLocal := readconcern.Local()
wc1 := writeconcern.New(writeconcern.W(10))
wc1 := &writeconcern.WriteConcern{W: 10}

db := setupDb("foo", options.Database().SetReadPreference(rpPrimary).SetReadConcern(rcLocal))
coll := db.Collection("bar", options.Collection().SetWriteConcern(wc1))
Expand Down
4 changes: 2 additions & 2 deletions mongo/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ func (db *Database) Drop(ctx context.Context) error {
if sess.TransactionRunning() {
wc = nil
}
if !writeconcern.AckWrite(wc) {
if !wc.Acknowledged() {
sess = nil
}

Expand Down Expand Up @@ -941,7 +941,7 @@ func (db *Database) executeCreateOperation(ctx context.Context, op *operation.Cr
if sess.TransactionRunning() {
wc = nil
}
if !writeconcern.AckWrite(wc) {
if !wc.Acknowledged() {
sess = nil
}

Expand Down
6 changes: 3 additions & 3 deletions mongo/database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ func TestDatabase(t *testing.T) {
t.Run("custom", func(t *testing.T) {
rpPrimary := readpref.Primary()
rpSecondary := readpref.Secondary()
wc1 := writeconcern.New(writeconcern.W(5))
wc2 := writeconcern.New(writeconcern.W(10))
wc1 := &writeconcern.WriteConcern{W: 5}
wc2 := &writeconcern.WriteConcern{W: 10}
rcLocal := readconcern.Local()
rcMajority := readconcern.Majority()
reg := bsoncodec.NewRegistryBuilder().Build()
Expand All @@ -70,7 +70,7 @@ func TestDatabase(t *testing.T) {
t.Run("inherit", func(t *testing.T) {
rpPrimary := readpref.Primary()
rcLocal := readconcern.Local()
wc1 := writeconcern.New(writeconcern.W(10))
wc1 := &writeconcern.WriteConcern{W: 10}
reg := bsoncodec.NewRegistryBuilder().Build()

client := setupClient(options.Client().SetReadPreference(rpPrimary).SetReadConcern(rcLocal).SetRegistry(reg))
Expand Down
2 changes: 1 addition & 1 deletion mongo/gridfs/gridfs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func TestGridFS(t *testing.T) {
clientOpts := options.Client().
ApplyURI(cs.Original).
SetReadPreference(readpref.Primary()).
SetWriteConcern(writeconcern.New(writeconcern.WMajority())).
SetWriteConcern(writeconcern.Majority()).
SetPoolMonitor(poolMonitor).
// Connect to a single host. For sharded clusters, this will pin to a single mongos, which avoids
// non-deterministic versioning errors in the server. This has no effect for replica sets because the driver
Expand Down
5 changes: 2 additions & 3 deletions mongo/index_view.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"go.mongodb.org/mongo-driver/mongo/description"
"go.mongodb.org/mongo-driver/mongo/options"
"go.mongodb.org/mongo-driver/mongo/readpref"
"go.mongodb.org/mongo-driver/mongo/writeconcern"
"go.mongodb.org/mongo-driver/x/bsonx/bsoncore"
"go.mongodb.org/mongo-driver/x/mongo/driver"
"go.mongodb.org/mongo-driver/x/mongo/driver/operation"
Expand Down Expand Up @@ -254,7 +253,7 @@ func (iv IndexView) CreateMany(ctx context.Context, models []IndexModel, opts ..
if sess.TransactionRunning() {
wc = nil
}
if !writeconcern.AckWrite(wc) {
if !wc.Acknowledged() {
sess = nil
}

Expand Down Expand Up @@ -399,7 +398,7 @@ func (iv IndexView) drop(ctx context.Context, name string, opts ...*options.Drop
if sess.TransactionRunning() {
wc = nil
}
if !writeconcern.AckWrite(wc) {
if !wc.Acknowledged() {
sess = nil
}

Expand Down
10 changes: 8 additions & 2 deletions mongo/integration/client_side_encryption_prose_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,10 @@ func TestClientSideEncryptionProse(t *testing.T) {
alteredKeydoc, _ = bsoncore.AppendDocumentEnd(alteredKeydoc, cidx)

// Insert the copied key document into keyvault.datakeys with majority write concern.
wcMajority := writeconcern.New(writeconcern.WMajority(), writeconcern.WTimeout(1*time.Second))
wcMajority := &writeconcern.WriteConcern{
W: "majority",
WTimeout: 1 * time.Second,
}
wcMajorityCollectionOpts := options.Collection().SetWriteConcern(wcMajority)
wcmColl := cse.kvClient.Database(kvDatabase).Collection(dkCollection, wcMajorityCollectionOpts)
_, err = wcmColl.InsertOne(context.Background(), alteredKeydoc)
Expand Down Expand Up @@ -1883,7 +1886,10 @@ func TestClientSideEncryptionProse(t *testing.T) {
}),
}

wcMajority := writeconcern.New(writeconcern.WMajority(), writeconcern.WTimeout(1*time.Second))
wcMajority := &writeconcern.WriteConcern{
W: "majority",
WTimeout: 1 * time.Second,
}
wcMajorityCollectionOpts := options.Collection().SetWriteConcern(wcMajority)
wcmColl := cse.kvClient.Database(kvDatabase).Collection(dkCollection, wcMajorityCollectionOpts)
_, err = wcmColl.Indexes().CreateOne(context.Background(), keyVaultIndex)
Expand Down
7 changes: 5 additions & 2 deletions mongo/integration/collection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ var (
// impossibleWc is a write concern that can't be satisfied and is used to test write concern errors
// for various operations. It includes a timeout because legacy servers will wait for all W nodes to respond,
// causing tests to hang.
impossibleWc = writeconcern.New(writeconcern.W(30), writeconcern.WTimeout(time.Second))
impossibleWc = &writeconcern.WriteConcern{
W: 10,
WTimeout: time.Second,
}
)

func TestCollection(t *testing.T) {
Expand Down Expand Up @@ -1682,7 +1685,7 @@ func TestCollection(t *testing.T) {
assert.Equal(mt, res.UpsertedIDs[3].(string), id3, "expected UpsertedIDs[3] to be %v, got %v", id3, res.UpsertedIDs[3])
})
unackClientOpts := options.Client().
SetWriteConcern(writeconcern.New(writeconcern.W(0)))
SetWriteConcern(&writeconcern.WriteConcern{W: 0})
unackMtOpts := mtest.NewOptions().
ClientOptions(unackClientOpts).
MinServerVersion("3.6")
Expand Down
4 changes: 2 additions & 2 deletions mongo/integration/index_view_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ func TestIndexView(t *testing.T) {
}
})
unackClientOpts := options.Client().
SetWriteConcern(writeconcern.New(writeconcern.W(0)))
SetWriteConcern(&writeconcern.WriteConcern{W: 0})
unackMtOpts := mtest.NewOptions().
ClientOptions(unackClientOpts).
MinServerVersion("3.6")
Expand Down Expand Up @@ -344,7 +344,7 @@ func TestIndexView(t *testing.T) {
Name: indexNames[1],
})
})
wc := writeconcern.New(writeconcern.W(1))
wc := &writeconcern.WriteConcern{W: 1}
wcMtOpts := mtest.NewOptions().CollectionOptions(options.Collection().SetWriteConcern(wc))
mt.RunOpts("uses writeconcern", wcMtOpts, func(mt *mtest.T) {
iv := mt.Coll.Indexes()
Expand Down
19 changes: 10 additions & 9 deletions mongo/integration/json_helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,12 @@ func createClientOptions(t testing.TB, opts bson.Raw) *options.ClientOptions {
switch opt.Type {
case bson.TypeInt32:
w := int(opt.Int32())
clientOpts.SetWriteConcern(writeconcern.New(writeconcern.W(w)))
clientOpts.SetWriteConcern(&writeconcern.WriteConcern{W: w})
case bson.TypeDouble:
w := int(opt.Double())
clientOpts.SetWriteConcern(writeconcern.New(writeconcern.W(w)))
clientOpts.SetWriteConcern(&writeconcern.WriteConcern{W: w})
case bson.TypeString:
clientOpts.SetWriteConcern(writeconcern.New(writeconcern.WMajority()))
clientOpts.SetWriteConcern(writeconcern.Majority())
default:
t.Fatalf("unrecognized type for w client option: %v", opt.Type)
}
Expand Down Expand Up @@ -398,7 +398,7 @@ func createWriteConcern(t testing.TB, opt bson.RawValue) *writeconcern.WriteConc
return nil
}

var opts []writeconcern.Option
wc := &writeconcern.WriteConcern{}
elems, _ := wcDoc.Elements()
for _, elem := range elems {
key := elem.Key()
Expand All @@ -407,27 +407,28 @@ func createWriteConcern(t testing.TB, opt bson.RawValue) *writeconcern.WriteConc
switch key {
case "wtimeout":
wtimeout := convertValueToMilliseconds(t, val)
opts = append(opts, writeconcern.WTimeout(wtimeout))
wc.WTimeout = wtimeout
case "j":
opts = append(opts, writeconcern.J(val.Boolean()))
j := val.Boolean()
wc.Journal = &j
case "w":
switch val.Type {
case bson.TypeString:
if val.StringValue() != "majority" {
break
}
opts = append(opts, writeconcern.WMajority())
wc.W = "majority"
case bson.TypeInt32:
w := int(val.Int32())
opts = append(opts, writeconcern.W(w))
wc.W = w
default:
t.Fatalf("unrecognized type for w: %v", val.Type)
}
default:
t.Fatalf("unrecognized write concern option: %v", key)
}
}
return writeconcern.New(opts...)
return wc
}

// create a read preference from a string.
Expand Down
7 changes: 5 additions & 2 deletions mongo/integration/mtest/mongotest.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import (

var (
// MajorityWc is the majority write concern.
MajorityWc = writeconcern.New(writeconcern.WMajority())
MajorityWc = writeconcern.Majority()
// PrimaryRp is the primary read preference.
PrimaryRp = readpref.Primary()
// SecondaryRp is the secondary read preference.
Expand Down Expand Up @@ -526,7 +526,10 @@ func (t *T) ClearCollections() {
// could prevent it from being dropped for sharded clusters. We can resolve this by
// re-instantiating the collection with a majority write concern before dropping.
collname := coll.created.Name()
wcm := writeconcern.New(writeconcern.WMajority(), writeconcern.WTimeout(1*time.Second))
wcm := &writeconcern.WriteConcern{
W: "majority",
WTimeout: 1 * time.Second,
}
wccoll := t.DB.Collection(collname, options.Collection().SetWriteConcern(wcm))
_ = wccoll.Drop(context.Background())

Expand Down
2 changes: 1 addition & 1 deletion mongo/integration/mtest/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ var testContext struct {
}

func setupClient(opts *options.ClientOptions) (*mongo.Client, error) {
wcMajority := writeconcern.New(writeconcern.WMajority())
wcMajority := writeconcern.Majority()
// set ServerAPIOptions to latest version if required
if opts.ServerAPIOptions == nil && testContext.requireAPIVersion {
opts.SetServerAPIOptions(options.ServerAPI(driver.TestServerAPIVersion))
Expand Down
Loading

0 comments on commit f050dc0

Please sign in to comment.