Skip to content

Commit

Permalink
Merge branch 'master' into godriver2949
Browse files Browse the repository at this point in the history
  • Loading branch information
qingyang-hu authored Dec 8, 2023
2 parents ec1d1b0 + 7211924 commit 5ff85c3
Show file tree
Hide file tree
Showing 137 changed files with 1,155 additions and 1,214 deletions.
158 changes: 89 additions & 69 deletions .evergreen/config.yml

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion .evergreen/run-mongodb-aws-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ set -x

# For Go 1.16+, Go builds requires a go.mod file in the current working directory or a parent
# directory. Spawn a new subshell, "cd" to the project directory, then run "go run".
(cd ${PROJECT_DIRECTORY} && go run "./cmd/testaws/main.go")
(cd ${PROJECT_DIRECTORY} && go run "./cmd/testaws/main.go" | tee test.suite)
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ evg-test-load-balancers:
go test $(BUILD_TAGS) ./mongo/integration -run TestChangeStreamSpec -v -timeout $(TEST_TIMEOUT)s >> test.suite
go test $(BUILD_TAGS) ./mongo/integration -run TestInitialDNSSeedlistDiscoverySpec/load_balanced -v -timeout $(TEST_TIMEOUT)s >> test.suite
go test $(BUILD_TAGS) ./mongo/integration -run TestLoadBalancerSupport -v -timeout $(TEST_TIMEOUT)s >> test.suite
go test $(BUILD_TAGS) ./mongo/integration -run TestLoadBalancedConnectionHandshake -v -timeout $(TEST_TIMEOUT)s >> test.suite
go test $(BUILD_TAGS) ./mongo/integration/unified -run TestUnifiedSpec -v -timeout $(TEST_TIMEOUT)s >> test.suite

.PHONY: evg-test-search-index
Expand Down Expand Up @@ -196,7 +197,7 @@ build-kms-test:
### Benchmark specific targets and support. ###
.PHONY: benchmark
benchmark:perf
go test $(BUILD_TAGS) -benchmem -bench=. ./benchmark
go test $(BUILD_TAGS) -benchmem -bench=. ./benchmark | test benchmark.suite

.PHONY: driver-benchmark
driver-benchmark:perf
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ import (

ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
client, err := mongo.Connect(ctx, options.Client().ApplyURI("mongodb://localhost:27017"))
client, err := mongo.Connect(options.Client().ApplyURI("mongodb://localhost:27017"))
```

Make sure to defer a call to `Disconnect` after instantiating your client:
Expand Down Expand Up @@ -166,12 +166,12 @@ Compression can be enabled using the `compressors` parameter on the connection s

```go
opts := options.Client().ApplyURI("mongodb://localhost:27017/?compressors=snappy,zlib,zstd")
client, _ := mongo.Connect(context.TODO(), opts)
client, _ := mongo.Connect(opts)
```

```go
opts := options.Client().SetCompressors([]string{"snappy", "zlib", "zstd"})
client, _ := mongo.Connect(context.TODO(), opts)
client, _ := mongo.Connect(opts)
```

If compressors are set, the Go Driver negotiates with the server to select the first common compressor. For server configuration and defaults, refer to [`networkMessageCompressors`](https://www.mongodb.com/docs/manual/reference/program/mongod/#std-option-mongod.--networkMessageCompressors).
Expand Down
9 changes: 5 additions & 4 deletions benchmark/harness_case.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package benchmark

import (
"context"
"errors"
"fmt"
"path/filepath"
"reflect"
Expand Down Expand Up @@ -95,12 +96,12 @@ benchRepeat:
res.Duration = c.elapsed
c.cumulativeRuntime += res.Duration

switch res.Error {
case context.DeadlineExceeded:
switch {
case errors.Is(res.Error, context.DeadlineExceeded):
break benchRepeat
case context.Canceled:
case errors.Is(res.Error, context.Canceled):
break benchRepeat
case nil:
case res.Error == nil:
out.Trials++
c.elapsed = 0
out.Raw = append(out.Raw, res)
Expand Down
4 changes: 2 additions & 2 deletions benchmark/multi.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func MultiFindMany(ctx context.Context, tm TimerManager, iters int) error {
ctx, cancel := context.WithCancel(ctx)
defer cancel()

db, err := getClientDB(ctx)
db, err := getClientDB()
if err != nil {
return err
}
Expand Down Expand Up @@ -83,7 +83,7 @@ func multiInsertCase(ctx context.Context, tm TimerManager, iters int, data strin
ctx, cancel := context.WithCancel(ctx)
defer cancel()

db, err := getClientDB(ctx)
db, err := getClientDB()
if err != nil {
return err
}
Expand Down
6 changes: 3 additions & 3 deletions benchmark/operation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func BenchmarkClientWrite(b *testing.B) {
}
for _, bm := range benchmarks {
b.Run(bm.name, func(b *testing.B) {
client, err := mongo.Connect(context.Background(), bm.opt)
client, err := mongo.Connect(bm.opt)
if err != nil {
b.Fatalf("error creating client: %v", err)
}
Expand Down Expand Up @@ -71,7 +71,7 @@ func BenchmarkClientBulkWrite(b *testing.B) {
}
for _, bm := range benchmarks {
b.Run(bm.name, func(b *testing.B) {
client, err := mongo.Connect(context.Background(), bm.opt)
client, err := mongo.Connect(bm.opt)
if err != nil {
b.Fatalf("error creating client: %v", err)
}
Expand Down Expand Up @@ -115,7 +115,7 @@ func BenchmarkClientRead(b *testing.B) {
}
for _, bm := range benchmarks {
b.Run(bm.name, func(b *testing.B) {
client, err := mongo.Connect(context.Background(), bm.opt)
client, err := mongo.Connect(bm.opt)
if err != nil {
b.Fatalf("error creating client: %v", err)
}
Expand Down
10 changes: 5 additions & 5 deletions benchmark/single.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ const (
largeData = "large_doc.json"
)

func getClientDB(ctx context.Context) (*mongo.Database, error) {
func getClientDB() (*mongo.Database, error) {
cs, err := integtest.GetConnString()
if err != nil {
return nil, err
}
client, err := mongo.Connect(ctx, options.Client().ApplyURI(cs.String()))
client, err := mongo.Connect(options.Client().ApplyURI(cs.String()))
if err != nil {
return nil, err
}
Expand All @@ -42,7 +42,7 @@ func SingleRunCommand(ctx context.Context, tm TimerManager, iters int) error {
ctx, cancel := context.WithCancel(ctx)
defer cancel()

db, err := getClientDB(ctx)
db, err := getClientDB()
if err != nil {
return err
}
Expand Down Expand Up @@ -75,7 +75,7 @@ func SingleFindOneByID(ctx context.Context, tm TimerManager, iters int) error {
ctx, cancel := context.WithCancel(ctx)
defer cancel()

db, err := getClientDB(ctx)
db, err := getClientDB()
if err != nil {
return err
}
Expand Down Expand Up @@ -123,7 +123,7 @@ func singleInsertCase(ctx context.Context, tm TimerManager, iters int, data stri
ctx, cancel := context.WithCancel(ctx)
defer cancel()

db, err := getClientDB(ctx)
db, err := getClientDB()
if err != nil {
return err
}
Expand Down
10 changes: 5 additions & 5 deletions bson/bsoncodec/default_value_decoders.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func newDefaultStructCodec() *StructCodec {
if err != nil {
// This function is called from the codec registration path, so errors can't be propagated. If there's an error
// constructing the StructCodec, we panic to avoid losing it.
panic(fmt.Errorf("error creating default StructCodec: %v", err))
panic(fmt.Errorf("error creating default StructCodec: %w", err))
}
return codec
}
Expand Down Expand Up @@ -178,7 +178,7 @@ func (dvd DefaultValueDecoders) DDecodeValue(dc DecodeContext, vr bsonrw.ValueRe

for {
key, elemVr, err := dr.ReadElement()
if err == bsonrw.ErrEOD {
if errors.Is(err, bsonrw.ErrEOD) {
break
} else if err != nil {
return err
Expand Down Expand Up @@ -1379,7 +1379,7 @@ func (dvd DefaultValueDecoders) MapDecodeValue(dc DecodeContext, vr bsonrw.Value
keyType := val.Type().Key()
for {
key, vr, err := dr.ReadElement()
if err == bsonrw.ErrEOD {
if errors.Is(err, bsonrw.ErrEOD) {
break
}
if err != nil {
Expand Down Expand Up @@ -1675,7 +1675,7 @@ func (dvd DefaultValueDecoders) decodeDefault(dc DecodeContext, vr bsonrw.ValueR
idx := 0
for {
vr, err := ar.ReadValue()
if err == bsonrw.ErrEOA {
if errors.Is(err, bsonrw.ErrEOA) {
break
}
if err != nil {
Expand Down Expand Up @@ -1787,7 +1787,7 @@ func (DefaultValueDecoders) decodeElemsFromDocumentReader(dc DecodeContext, dr b
elems := make([]reflect.Value, 0)
for {
key, vr, err := dr.ReadElement()
if err == bsonrw.ErrEOD {
if errors.Is(err, bsonrw.ErrEOD) {
break
}
if err != nil {
Expand Down
8 changes: 4 additions & 4 deletions bson/bsoncodec/default_value_decoders_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2370,8 +2370,8 @@ func TestDefaultValueDecoders(t *testing.T) {
return
}
if rc.val == cansettest { // We're doing an IsValid and CanSet test
wanterr, ok := rc.err.(ValueDecoderError)
if !ok {
var wanterr ValueDecoderError
if !errors.As(rc.err, &wanterr) {
t.Fatalf("Error must be a DecodeValueError, but got a %T", rc.err)
}

Expand Down Expand Up @@ -3685,8 +3685,8 @@ func TestDefaultValueDecoders(t *testing.T) {
val := reflect.New(reflect.TypeOf(outer{})).Elem()
err := defaultTestStructCodec.DecodeValue(dc, vr, val)

decodeErr, ok := err.(*DecodeError)
assert.True(t, ok, "expected DecodeError, got %v of type %T", err, err)
var decodeErr *DecodeError
assert.True(t, errors.As(err, &decodeErr), "expected DecodeError, got %v of type %T", err, err)
expectedKeys := []string{"foo", "bar"}
assert.Equal(t, expectedKeys, decodeErr.Keys(), "expected keys slice %v, got %v", expectedKeys,
decodeErr.Keys())
Expand Down
12 changes: 6 additions & 6 deletions bson/bsoncodec/default_value_encoders.go
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ func (dve DefaultValueEncoders) mapEncodeValue(ec EncodeContext, dw bsonrw.Docum
}

currEncoder, currVal, lookupErr := dve.lookupElementEncoder(ec, encoder, val.MapIndex(key))
if lookupErr != nil && lookupErr != errInvalidValue {
if lookupErr != nil && !errors.Is(lookupErr, errInvalidValue) {
return lookupErr
}

Expand All @@ -352,7 +352,7 @@ func (dve DefaultValueEncoders) mapEncodeValue(ec EncodeContext, dw bsonrw.Docum
return err
}

if lookupErr == errInvalidValue {
if errors.Is(lookupErr, errInvalidValue) {
err = vw.WriteNull()
if err != nil {
return err
Expand Down Expand Up @@ -418,7 +418,7 @@ func (dve DefaultValueEncoders) ArrayEncodeValue(ec EncodeContext, vw bsonrw.Val

for idx := 0; idx < val.Len(); idx++ {
currEncoder, currVal, lookupErr := dve.lookupElementEncoder(ec, encoder, val.Index(idx))
if lookupErr != nil && lookupErr != errInvalidValue {
if lookupErr != nil && !errors.Is(lookupErr, errInvalidValue) {
return lookupErr
}

Expand All @@ -427,7 +427,7 @@ func (dve DefaultValueEncoders) ArrayEncodeValue(ec EncodeContext, vw bsonrw.Val
return err
}

if lookupErr == errInvalidValue {
if errors.Is(lookupErr, errInvalidValue) {
err = vw.WriteNull()
if err != nil {
return err
Expand Down Expand Up @@ -487,7 +487,7 @@ func (dve DefaultValueEncoders) SliceEncodeValue(ec EncodeContext, vw bsonrw.Val

for idx := 0; idx < val.Len(); idx++ {
currEncoder, currVal, lookupErr := dve.lookupElementEncoder(ec, encoder, val.Index(idx))
if lookupErr != nil && lookupErr != errInvalidValue {
if lookupErr != nil && !errors.Is(lookupErr, errInvalidValue) {
return lookupErr
}

Expand All @@ -496,7 +496,7 @@ func (dve DefaultValueEncoders) SliceEncodeValue(ec EncodeContext, vw bsonrw.Val
return err
}

if lookupErr == errInvalidValue {
if errors.Is(lookupErr, errInvalidValue) {
err = vw.WriteNull()
if err != nil {
return err
Expand Down
7 changes: 4 additions & 3 deletions bson/bsoncodec/map_codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package bsoncodec

import (
"encoding"
"errors"
"fmt"
"reflect"
"strconv"
Expand Down Expand Up @@ -137,7 +138,7 @@ func (mc *MapCodec) mapEncodeValue(ec EncodeContext, dw bsonrw.DocumentWriter, v
return err
}

if lookupErr == errInvalidValue {
if errors.Is(lookupErr, errInvalidValue) {
err = vw.WriteNull()
if err != nil {
return err
Expand Down Expand Up @@ -200,7 +201,7 @@ func (mc *MapCodec) DecodeValue(dc DecodeContext, vr bsonrw.ValueReader, val ref

for {
key, vr, err := dr.ReadElement()
if err == bsonrw.ErrEOD {
if errors.Is(err, bsonrw.ErrEOD) {
break
}
if err != nil {
Expand Down Expand Up @@ -313,7 +314,7 @@ func (mc *MapCodec) decodeKey(key string, keyType reflect.Type) (reflect.Value,
if mc.EncodeKeysWithStringer {
parsed, err := strconv.ParseFloat(key, 64)
if err != nil {
return keyVal, fmt.Errorf("Map key is defined to be a decimal type (%v) but got error %v", keyType.Kind(), err)
return keyVal, fmt.Errorf("Map key is defined to be a decimal type (%v) but got error %w", keyType.Kind(), err)
}
keyVal = reflect.ValueOf(parsed)
break
Expand Down
9 changes: 3 additions & 6 deletions bson/bsoncodec/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,9 @@ func (r *Registry) RegisterTypeMapEntry(bt bsontype.Type, rt reflect.Type) {
// If no encoder is found, an error of type ErrNoEncoder is returned. LookupEncoder is safe for
// concurrent use by multiple goroutines after all codecs and encoders are registered.
func (r *Registry) LookupEncoder(valueType reflect.Type) (ValueEncoder, error) {
if valueType == nil {
return nil, ErrNoEncoder{Type: valueType}
}
enc, found := r.lookupTypeEncoder(valueType)
if found {
if enc == nil {
Expand All @@ -400,15 +403,10 @@ func (r *Registry) LookupEncoder(valueType reflect.Type) (ValueEncoder, error) {
if found {
return r.typeEncoders.LoadOrStore(valueType, enc), nil
}
if valueType == nil {
r.storeTypeEncoder(valueType, nil)
return nil, ErrNoEncoder{Type: valueType}
}

if v, ok := r.kindEncoders.Load(valueType.Kind()); ok {
return r.storeTypeEncoder(valueType, v), nil
}
r.storeTypeEncoder(valueType, nil)
return nil, ErrNoEncoder{Type: valueType}
}

Expand Down Expand Up @@ -474,7 +472,6 @@ func (r *Registry) LookupDecoder(valueType reflect.Type) (ValueDecoder, error) {
if v, ok := r.kindDecoders.Load(valueType.Kind()); ok {
return r.storeTypeDecoder(valueType, v), nil
}
r.storeTypeDecoder(valueType, nil)
return nil, ErrNoDecoder{Type: valueType}
}

Expand Down
Loading

0 comments on commit 5ff85c3

Please sign in to comment.