Skip to content

Commit 625787f

Browse files
authored
PBM-799: Make WriteConcern option configurable (#925)
* Add WriteConcern option * Add ConnectTimeout & ServerSelectionTimeout option * Reuse MongoConnect within physical restore * Use single node write concern for standalone mode
1 parent d36f1bf commit 625787f

File tree

2 files changed

+41
-24
lines changed

2 files changed

+41
-24
lines changed

pbm/connect/connect.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"net/url"
66
"strings"
7+
"time"
78

89
"go.mongodb.org/mongo-driver/mongo"
910
"go.mongodb.org/mongo-driver/mongo/options"
@@ -34,6 +35,9 @@ func Direct(direct bool) MongoOption {
3435
}
3536
}
3637

38+
// ReadConcern option sets availability guarantees for read operation.
39+
// For PBM typically use: [readconcern.Local] or [readconcern.Majority].
40+
// If the option is not specified the default is: [readconcern.Majority].
3741
func ReadConcern(readConcern *readconcern.ReadConcern) MongoOption {
3842
return func(opts *options.ClientOptions) error {
3943
if readConcern == nil {
@@ -44,13 +48,41 @@ func ReadConcern(readConcern *readconcern.ReadConcern) MongoOption {
4448
}
4549
}
4650

51+
// WriteConcern option sets level of acknowledgment for write operation.
52+
// For PBM typically use: [writeconcern.W1] or [writeconcern.Majority].
53+
// If the option is not specified the default is: [writeconcern.Majority].
54+
func WriteConcern(writeConcern *writeconcern.WriteConcern) MongoOption {
55+
return func(opts *options.ClientOptions) error {
56+
if writeConcern == nil {
57+
return errors.New("WriteConcern not specified")
58+
}
59+
opts.SetWriteConcern(writeConcern)
60+
return nil
61+
}
62+
}
63+
64+
// NoRS option removes replica set name setting
4765
func NoRS() MongoOption {
4866
return func(opts *options.ClientOptions) error {
4967
opts.SetReplicaSet("")
5068
return nil
5169
}
5270
}
5371

72+
func ConnectTimeout(d time.Duration) MongoOption {
73+
return func(opts *options.ClientOptions) error {
74+
opts.SetConnectTimeout(d)
75+
return nil
76+
}
77+
}
78+
79+
func ServerSelectionTimeout(d time.Duration) MongoOption {
80+
return func(opts *options.ClientOptions) error {
81+
opts.SetServerSelectionTimeout(d)
82+
return nil
83+
}
84+
}
85+
5486
func MongoConnect(ctx context.Context, uri string, mongoOptions ...MongoOption) (*mongo.Client, error) {
5587
if !strings.HasPrefix(uri, "mongodb://") {
5688
uri = "mongodb://" + uri

pbm/restore/physical.go

Lines changed: 9 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"go.mongodb.org/mongo-driver/bson/primitive"
2424
"go.mongodb.org/mongo-driver/mongo"
2525
"go.mongodb.org/mongo-driver/mongo/options"
26+
"go.mongodb.org/mongo-driver/mongo/writeconcern"
2627
"golang.org/x/mod/semver"
2728
"gopkg.in/yaml.v2"
2829

@@ -1669,8 +1670,15 @@ func tryConn(port int, logpath string) (*mongo.Client, error) {
16691670

16701671
var cn *mongo.Client
16711672
var err error
1673+
host := fmt.Sprintf("mongodb://localhost:%d", port)
16721674
for i := 0; i < tryConnCount; i++ {
1673-
cn, err = conn(port, tryConnTimeout)
1675+
cn, err = connect.MongoConnect(context.Background(), host,
1676+
connect.AppName("pbm-physical-restore"),
1677+
connect.Direct(true),
1678+
connect.WriteConcern(writeconcern.W1()),
1679+
connect.ConnectTimeout(time.Second*120),
1680+
connect.ServerSelectionTimeout(tryConnTimeout),
1681+
)
16741682
if err == nil {
16751683
return cn, nil
16761684
}
@@ -1698,29 +1706,6 @@ func tryConn(port int, logpath string) (*mongo.Client, error) {
16981706
return nil, errors.Errorf("failed to connect after %d tries: %v", tryConnCount, err)
16991707
}
17001708

1701-
func conn(port int, tout time.Duration) (*mongo.Client, error) {
1702-
ctx := context.Background()
1703-
1704-
opts := options.Client().
1705-
SetHosts([]string{"localhost:" + strconv.Itoa(port)}).
1706-
SetAppName("pbm-physical-restore").
1707-
SetDirect(true).
1708-
SetConnectTimeout(time.Second * 120).
1709-
SetServerSelectionTimeout(tout)
1710-
1711-
conn, err := mongo.Connect(ctx, opts)
1712-
if err != nil {
1713-
return nil, errors.Wrap(err, "connect")
1714-
}
1715-
1716-
err = conn.Ping(ctx, nil)
1717-
if err != nil {
1718-
return nil, errors.Wrap(err, "ping")
1719-
}
1720-
1721-
return conn, nil
1722-
}
1723-
17241709
const internalMongodLog = "pbm.restore.log"
17251710

17261711
func (r *PhysRestore) startMongo(opts ...string) error {

0 commit comments

Comments
 (0)