Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
qingyang-hu committed Sep 20, 2023
1 parent 4b0c121 commit 1b99cfd
Show file tree
Hide file tree
Showing 14 changed files with 53 additions and 203 deletions.
27 changes: 0 additions & 27 deletions benchmark/canary.go

This file was deleted.

21 changes: 3 additions & 18 deletions benchmark/operation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,10 @@ func BenchmarkClientWrite(b *testing.B) {
}
for _, bm := range benchmarks {
b.Run(bm.name, func(b *testing.B) {
client, err := mongo.NewClient(bm.opt)
client, err := mongo.Connect(context.Background(), bm.opt)
if err != nil {
b.Fatalf("error creating client: %v", err)
}
ctx := context.Background()
err = client.Connect(ctx)
if err != nil {
b.Fatalf("error connecting: %v", err)
}
defer client.Disconnect(context.Background())
coll := client.Database("test").Collection("test")
_, err = coll.DeleteMany(context.Background(), bson.D{})
Expand Down Expand Up @@ -76,15 +71,10 @@ func BenchmarkClientBulkWrite(b *testing.B) {
}
for _, bm := range benchmarks {
b.Run(bm.name, func(b *testing.B) {
client, err := mongo.NewClient(bm.opt)
client, err := mongo.Connect(context.Background(), bm.opt)
if err != nil {
b.Fatalf("error creating client: %v", err)
}
ctx := context.Background()
err = client.Connect(ctx)
if err != nil {
b.Fatalf("error connecting: %v", err)
}
defer client.Disconnect(context.Background())
coll := client.Database("test").Collection("test")
_, err = coll.DeleteMany(context.Background(), bson.D{})
Expand Down Expand Up @@ -125,15 +115,10 @@ func BenchmarkClientRead(b *testing.B) {
}
for _, bm := range benchmarks {
b.Run(bm.name, func(b *testing.B) {
client, err := mongo.NewClient(bm.opt)
client, err := mongo.Connect(context.Background(), bm.opt)
if err != nil {
b.Fatalf("error creating client: %v", err)
}
ctx := context.Background()
err = client.Connect(ctx)
if err != nil {
b.Fatalf("error connecting: %v", err)
}
defer client.Disconnect(context.Background())
coll := client.Database("test").Collection("test")
_, err = coll.DeleteMany(context.Background(), bson.D{})
Expand Down
5 changes: 1 addition & 4 deletions benchmark/single.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,10 @@ func getClientDB(ctx context.Context) (*mongo.Database, error) {
if err != nil {
return nil, err
}
client, err := mongo.NewClient(options.Client().ApplyURI(cs.String()))
client, err := mongo.Connect(ctx, options.Client().ApplyURI(cs.String()))
if err != nil {
return nil, err
}
if err = client.Connect(ctx); err != nil {
return nil, err
}

db := client.Database(integtest.GetDBName(cs))
return db, nil
Expand Down
44 changes: 13 additions & 31 deletions event/monitoring.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,6 @@ type CommandStartedEvent struct {
CommandName string
RequestID int64
ConnectionID string
// ServerConnectionID contains the connection ID from the server of the operation. If the server does not return
// this value (e.g. on MDB < 4.2), it is unset. If the server connection ID would cause an int32 overflow, then
// then this field will be nil.
//
// Deprecated: Use ServerConnectionID64.
ServerConnectionID *int32
// ServerConnectionID64 contains the connection ID from the server of the operation. If the server does not
// return this value (e.g. on MDB < 4.2), it is unset.
ServerConnectionID64 *int64
Expand All @@ -39,19 +33,11 @@ type CommandStartedEvent struct {

// CommandFinishedEvent represents a generic command finishing.
type CommandFinishedEvent struct {
// Deprecated: Use Duration instead.
DurationNanos int64
Duration time.Duration
CommandName string
DatabaseName string
RequestID int64
ConnectionID string
// ServerConnectionID contains the connection ID from the server of the operation. If the server does not return
// this value (e.g. on MDB < 4.2), it is unset.If the server connection ID would cause an int32 overflow, then
// this field will be nil.
//
// Deprecated: Use ServerConnectionID64.
ServerConnectionID *int32
Duration time.Duration
CommandName string
DatabaseName string
RequestID int64
ConnectionID string
// ServerConnectionID64 contains the connection ID from the server of the operation. If the server does not
// return this value (e.g. on MDB < 4.2), it is unset.
ServerConnectionID64 *int64
Expand Down Expand Up @@ -174,22 +160,18 @@ type ServerHeartbeatStartedEvent struct {

// ServerHeartbeatSucceededEvent is an event generated when the heartbeat succeeds.
type ServerHeartbeatSucceededEvent struct {
// Deprecated: Use Duration instead.
DurationNanos int64
Duration time.Duration
Reply description.Server
ConnectionID string // The address this heartbeat was sent to with a unique identifier
Awaited bool // If this heartbeat was awaitable
Duration time.Duration
Reply description.Server
ConnectionID string // The address this heartbeat was sent to with a unique identifier
Awaited bool // If this heartbeat was awaitable
}

// ServerHeartbeatFailedEvent is an event generated when the heartbeat fails.
type ServerHeartbeatFailedEvent struct {
// Deprecated: Use Duration instead.
DurationNanos int64
Duration time.Duration
Failure error
ConnectionID string // The address this heartbeat was sent to with a unique identifier
Awaited bool // If this heartbeat was awaitable
Duration time.Duration
Failure error
ConnectionID string // The address this heartbeat was sent to with a unique identifier
Awaited bool // If this heartbeat was awaitable
}

// ServerMonitor represents a monitor that is triggered for different server events. The client
Expand Down
29 changes: 12 additions & 17 deletions mongo/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,7 @@ type Client struct {
encryptedFieldsMap map[string]interface{}
}

// Connect creates a new Client and then initializes it using the Connect method. This is equivalent to calling
// NewClient followed by Client.Connect.
// Connect creates a new Client and then initializes it using the Connect method.
//
// When creating an options.ClientOptions, the order the methods are called matters. Later Set*
// methods will overwrite the values from previous Set* method invocations. This includes the
Expand All @@ -104,18 +103,18 @@ type Client struct {
// The Client.Ping method can be used to verify that the deployment is successfully connected and the
// Client was correctly configured.
func Connect(ctx context.Context, opts ...*options.ClientOptions) (*Client, error) {
c, err := NewClient(opts...)
c, err := newClient(opts...)
if err != nil {
return nil, err
}
err = c.Connect(ctx)
err = c.connect(ctx)
if err != nil {
return nil, err
}
return c, nil
}

// NewClient creates a new client to connect to a deployment specified by the uri.
// newClient creates a new client to connect to a deployment specified by the uri.
//
// When creating an options.ClientOptions, the order the methods are called matters. Later Set*
// methods will overwrite the values from previous Set* method invocations. This includes the
Expand All @@ -128,9 +127,7 @@ func Connect(ctx context.Context, opts ...*options.ClientOptions) (*Client, erro
// option fields of previous options, there is no partial overwriting. For example, if Username is
// set in the Auth field for the first option, and Password is set for the second but with no
// Username, after the merge the Username field will be empty.
//
// Deprecated: Use [Connect] instead.
func NewClient(opts ...*options.ClientOptions) (*Client, error) {
func newClient(opts ...*options.ClientOptions) (*Client, error) {
clientOpt := options.MergeClientOptions(opts...)

id, err := uuid.New()
Expand Down Expand Up @@ -235,14 +232,12 @@ func NewClient(opts ...*options.ClientOptions) (*Client, error) {
return client, nil
}

// Connect initializes the Client by starting background monitoring goroutines.
// connect initializes the Client by starting background monitoring goroutines.
// If the Client was created using the NewClient function, this method must be called before a Client can be used.
//
// Connect starts background goroutines to monitor the state of the deployment and does not do any I/O in the main
// goroutine. The Client.Ping method can be used to verify that the connection was created successfully.
//
// Deprecated: Use [mongo.Connect] instead.
func (c *Client) Connect(ctx context.Context) error {
func (c *Client) connect(ctx context.Context) error {
if connector, ok := c.deployment.(driver.Connector); ok {
err := connector.Connect()
if err != nil {
Expand All @@ -257,19 +252,19 @@ func (c *Client) Connect(ctx context.Context) error {
}

if c.internalClientFLE != nil {
if err := c.internalClientFLE.Connect(ctx); err != nil {
if err := c.internalClientFLE.connect(ctx); err != nil {
return err
}
}

if c.keyVaultClientFLE != nil && c.keyVaultClientFLE != c.internalClientFLE && c.keyVaultClientFLE != c {
if err := c.keyVaultClientFLE.Connect(ctx); err != nil {
if err := c.keyVaultClientFLE.connect(ctx); err != nil {
return err
}
}

if c.metadataClientFLE != nil && c.metadataClientFLE != c.internalClientFLE && c.metadataClientFLE != c {
if err := c.metadataClientFLE.Connect(ctx); err != nil {
if err := c.metadataClientFLE.connect(ctx); err != nil {
return err
}
}
Expand Down Expand Up @@ -489,7 +484,7 @@ func (c *Client) getOrCreateInternalClient(clientOpts *options.ClientOptions) (*
internalClientOpts.AutoEncryptionOptions = nil
internalClientOpts.SetMinPoolSize(0)
var err error
c.internalClientFLE, err = NewClient(internalClientOpts)
c.internalClientFLE, err = newClient(internalClientOpts)
return c.internalClientFLE, err
}

Expand All @@ -499,7 +494,7 @@ func (c *Client) configureKeyVaultClientFLE(clientOpts *options.ClientOptions) e
aeOpts := clientOpts.AutoEncryptionOptions
switch {
case aeOpts.KeyVaultClientOptions != nil:
c.keyVaultClientFLE, err = NewClient(aeOpts.KeyVaultClientOptions)
c.keyVaultClientFLE, err = newClient(aeOpts.KeyVaultClientOptions)
case clientOpts.MaxPoolSize != nil && *clientOpts.MaxPoolSize == 0:
c.keyVaultClientFLE = c
default:
Expand Down
18 changes: 9 additions & 9 deletions mongo/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func setupClient(opts ...*options.ClientOptions) *Client {
integtest.AddTestServerAPIVersion(clientOpts)
opts = append(opts, clientOpts)
}
client, _ := NewClient(opts...)
client, _ := Connect(context.Background(), opts...)
return client
}

Expand Down Expand Up @@ -183,7 +183,7 @@ func TestClient(t *testing.T) {
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
_, err := NewClient(tc.opts)
_, err := Connect(context.Background(), tc.opts)
assert.Equal(t, tc.err, err, "expected error %v, got %v", tc.err, err)
})
}
Expand Down Expand Up @@ -227,7 +227,7 @@ func TestClient(t *testing.T) {
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
_, err := NewClient(tc.opts)
_, err := Connect(context.Background(), tc.opts)
assert.Equal(t, tc.err, err, "expected error %v, got %v", tc.err, err)
})
}
Expand All @@ -249,7 +249,7 @@ func TestClient(t *testing.T) {
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
client, err := NewClient(tc.opts)
client, err := Connect(context.Background(), tc.opts)
if tc.expectErr {
assert.NotNil(t, err, "expected error, got nil")
return
Expand Down Expand Up @@ -277,7 +277,7 @@ func TestClient(t *testing.T) {
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
client, err := NewClient(tc.opts)
client, err := Connect(context.Background(), tc.opts)
if tc.expectErr {
assert.NotNil(t, err, "expected error, got nil")
return
Expand Down Expand Up @@ -412,22 +412,22 @@ func TestClient(t *testing.T) {

t.Run("success with all options", func(t *testing.T) {
serverAPIOptions := getServerAPIOptions()
client, err := NewClient(options.Client().SetServerAPIOptions(serverAPIOptions))
client, err := Connect(context.Background(), options.Client().SetServerAPIOptions(serverAPIOptions))
assert.Nil(t, err, "unexpected error from NewClient: %v", err)
convertedAPIOptions := topology.ConvertToDriverAPIOptions(serverAPIOptions)
assert.Equal(t, convertedAPIOptions, client.serverAPI,
"mismatch in serverAPI; expected %v, got %v", convertedAPIOptions, client.serverAPI)
})
t.Run("failure with unsupported version", func(t *testing.T) {
serverAPIOptions := options.ServerAPI("badVersion")
_, err := NewClient(options.Client().SetServerAPIOptions(serverAPIOptions))
_, err := Connect(context.Background(), options.Client().SetServerAPIOptions(serverAPIOptions))
assert.NotNil(t, err, "expected error from NewClient, got nil")
errmsg := `api version "badVersion" not supported; this driver version only supports API version "1"`
assert.Equal(t, errmsg, err.Error(), "expected error %v, got %v", errmsg, err.Error())
})
t.Run("cannot modify options after client creation", func(t *testing.T) {
serverAPIOptions := getServerAPIOptions()
client, err := NewClient(options.Client().SetServerAPIOptions(serverAPIOptions))
client, err := Connect(context.Background(), options.Client().SetServerAPIOptions(serverAPIOptions))
assert.Nil(t, err, "unexpected error from NewClient: %v", err)

expectedServerAPIOptions := getServerAPIOptions()
Expand Down Expand Up @@ -476,7 +476,7 @@ func TestClient(t *testing.T) {
extraOptions["__cryptSharedLibDisabledForTestOnly"] = true
}

_, err := NewClient(options.Client().
_, err := Connect(context.Background(), options.Client().
SetAutoEncryptionOptions(options.AutoEncryption().
SetKmsProviders(map[string]map[string]interface{}{
"local": {"key": make([]byte, 96)},
Expand Down
2 changes: 0 additions & 2 deletions mongo/database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,7 @@ func TestDatabase(t *testing.T) {
})
t.Run("TransientTransactionError label", func(t *testing.T) {
client := setupClient(options.Client().ApplyURI("mongodb://nonexistent").SetServerSelectionTimeout(3 * time.Second))
err := client.Connect(bgCtx)
defer client.Disconnect(bgCtx)
assert.Nil(t, err, "expected nil, got %v", err)

t.Run("negative case of non-transaction", func(t *testing.T) {
var sse topology.ServerSelectionError
Expand Down
4 changes: 1 addition & 3 deletions mongo/integration/client_options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@ func TestClientOptions_CustomDialer(t *testing.T) {
cs := integtest.ConnString(t)
opts := options.Client().ApplyURI(cs.String()).SetDialer(td)
integtest.AddTestServerAPIVersion(opts)
client, err := mongo.NewClient(opts)
require.NoError(t, err)
err = client.Connect(context.Background())
client, err := mongo.Connect(context.Background(), opts)
require.NoError(t, err)
_, err = client.ListDatabases(context.Background(), bson.D{})
require.NoError(t, err)
Expand Down
2 changes: 1 addition & 1 deletion mongo/integration/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ func TestClient(t *testing.T) {
})
mt.RunOpts("watch", noClientOpts, func(mt *mtest.T) {
mt.Run("disconnected", func(mt *mtest.T) {
c, err := mongo.NewClient(options.Client().ApplyURI(mtest.ClusterURI()))
c, err := mongo.Connect(context.Background(), options.Client().ApplyURI(mtest.ClusterURI()))
assert.Nil(mt, err, "NewClient error: %v", err)
_, err = c.Watch(context.Background(), mongo.Pipeline{})
assert.Equal(mt, mongo.ErrClientDisconnected, err, "expected error %v, got %v", mongo.ErrClientDisconnected, err)
Expand Down
Loading

0 comments on commit 1b99cfd

Please sign in to comment.