From 497ac3ee42663b85a3307977f3d48826827e23c5 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Thu, 9 Nov 2023 15:14:21 +0400 Subject: [PATCH] chore(deps): bump go.mongodb.org/mongo-driver from 1.12.1 to 1.13.0 (#299) Bumps [go.mongodb.org/mongo-driver](https://github.com/mongodb/mongo-go-driver) from 1.12.1 to 1.13.0. - [Release notes](https://github.com/mongodb/mongo-go-driver/releases) - [Commits](https://github.com/mongodb/mongo-go-driver/compare/v1.12.1...v1.13.0) --- updated-dependencies: - dependency-name: go.mongodb.org/mongo-driver dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> --- go.mod | 2 +- go.sum | 4 +- .../bson/bsoncodec/codec_cache.go | 166 +++++++++++ .../bson/bsoncodec/default_value_decoders.go | 2 +- .../bson/bsoncodec/pointer_codec.go | 59 ++-- .../mongo-driver/bson/bsoncodec/registry.go | 160 ++++------ .../bson/bsoncodec/slice_codec.go | 2 +- .../bson/bsoncodec/struct_codec.go | 105 +++---- .../mongo-driver/bson/bsoncodec/types.go | 1 + .../mongo-driver/bson/bsonrw/copier.go | 8 +- .../mongo-driver/bson/bsonrw/value_reader.go | 12 +- .../mongo-driver/bson/bsonrw/value_writer.go | 74 +++-- .../mongo-driver/bson/bsontype/bsontype.go | 1 + .../mongo-driver/bson/marshal.go | 32 +- .../go.mongodb.org/mongo-driver/bson/raw.go | 11 +- .../go.mongodb.org/mongo-driver/bson/types.go | 1 + .../mongo-driver/event/monitoring.go | 1 + .../{string_util.go => bsonutil/bsonutil.go} | 33 ++- .../internal/cancellation_listener.go | 47 --- .../internal/codecutil/encoding.go | 65 ++++ .../internal/credproviders/imds_provider.go | 11 +- .../{csfle_util.go => csfle/csfle.go} | 2 +- .../internal/{csot_util.go => csot/csot.go} | 2 +- .../mongo-driver/internal/driverutil/hello.go | 128 ++++++++ .../internal/driverutil/operation.go | 31 ++ .../mongo-driver/internal/error.go | 123 -------- .../{const.go => handshake/handshake.go} | 8 +- .../{http.go => httputil/httputil.go} | 10 +- .../mongo-driver/internal/logger/component.go | 195 +++++++++--- .../mongo-driver/internal/logger/context.go | 48 +++ .../mongo-driver/internal/logger/logger.go | 11 +- .../mongo-driver/internal/ptrutil/int64.go | 39 +++ .../internal/{randutil => }/rand/bits.go | 0 .../internal/{randutil => }/rand/exp.go | 0 .../internal/{randutil => }/rand/normal.go | 0 .../internal/{randutil => }/rand/rand.go | 2 +- .../internal/{randutil => }/rand/rng.go | 0 .../internal/randutil/randutil.go | 2 +- .../internal/uri_validation_errors.go | 22 -- .../{internal => mongo}/background_context.go | 6 +- .../mongo-driver/mongo/batch_cursor.go | 17 +- .../mongo-driver/mongo/bulk_write.go | 2 +- .../mongo-driver/mongo/change_stream.go | 14 +- .../mongo-driver/mongo/client.go | 6 +- .../mongo-driver/mongo/collection.go | 77 +++-- .../mongo-driver/mongo/cursor.go | 17 ++ .../mongo-driver/mongo/database.go | 16 +- .../mongo-driver/mongo/description/server.go | 86 +++--- .../mongo/description/server_selector.go | 168 +++++++---- .../mongo/description/topology.go | 12 +- .../mongo-driver/mongo/errors.go | 86 +++--- .../mongo-driver/mongo/index_view.go | 3 + .../mongo-driver/mongo/mongo.go | 30 +- .../mongo-driver/mongo/mongocryptd.go | 4 +- .../mongo/options/autoencryptionoptions.go | 4 +- .../mongo/options/clientencryptionoptions.go | 4 +- .../mongo/options/clientoptions.go | 72 +++-- .../mongo/options/runcmdoptions.go | 4 +- .../mongo/options/searchindexoptions.go | 41 +++ .../mongo-driver/mongo/results.go | 2 +- .../mongo-driver/mongo/search_index_view.go | 279 ++++++++++++++++++ .../mongo-driver/mongo/session.go | 7 +- .../mongo-driver/mongo/single_result.go | 18 +- .../mongo-driver/version/version.go | 2 +- .../mongo-driver/x/bsonx/bsoncore/bsoncore.go | 2 +- .../mongo-driver/x/mongo/driver/DESIGN.md | 4 + .../x/mongo/driver/auth/creds/gcpcreds.go | 17 +- .../x/mongo/driver/auth/default.go | 18 +- .../driver/auth/internal/gssapi/gss_wrapper.c | 12 +- .../driver/auth/internal/gssapi/gss_wrapper.h | 8 +- .../mongo/driver/auth/internal/gssapi/sspi.go | 2 +- .../auth/internal/gssapi/sspi_wrapper.c | 6 +- .../auth/internal/gssapi/sspi_wrapper.h | 4 +- .../mongo-driver/x/mongo/driver/auth/x509.go | 13 +- .../x/mongo/driver/batch_cursor.go | 95 ++++-- .../mongo-driver/x/mongo/driver/batches.go | 2 +- .../x/mongo/driver/compression.go | 114 ++++--- .../x/mongo/driver/connstring/connstring.go | 97 ++++-- .../mongo-driver/x/mongo/driver/driver.go | 18 +- .../mongo-driver/x/mongo/driver/errors.go | 35 ++- .../mongo-driver/x/mongo/driver/legacy.go | 1 + .../driver/list_collections_batch_cursor.go | 134 --------- .../x/mongo/driver/mongocrypt/binary.go | 17 +- .../x/mongo/driver/mongocrypt/mongocrypt.go | 10 +- .../x/mongo/driver/ocsp/config.go | 4 +- .../mongo-driver/x/mongo/driver/ocsp/ocsp.go | 2 +- .../mongo-driver/x/mongo/driver/operation.go | 267 +++++++++++++---- .../driver/operation/abort_transaction.go | 2 + .../x/mongo/driver/operation/aggregate.go | 2 + .../driver/operation/commit_transaction.go | 2 + .../x/mongo/driver/operation/count.go | 2 + .../x/mongo/driver/operation/create.go | 1 - .../{createIndexes.go => create_indexes.go} | 2 + .../driver/operation/create_search_indexes.go | 245 +++++++++++++++ .../x/mongo/driver/operation/delete.go | 2 + .../x/mongo/driver/operation/distinct.go | 2 + .../mongo/driver/operation/drop_collection.go | 2 + .../x/mongo/driver/operation/drop_database.go | 2 + .../x/mongo/driver/operation/drop_indexes.go | 2 + .../driver/operation/drop_search_index.go | 227 ++++++++++++++ .../x/mongo/driver/operation/end_sessions.go | 2 + .../x/mongo/driver/operation/find.go | 2 + .../mongo/driver/operation/find_and_modify.go | 2 + .../x/mongo/driver/operation/hello.go | 211 ++++++++----- .../x/mongo/driver/operation/insert.go | 2 + .../x/mongo/driver/operation/listDatabases.go | 2 + .../driver/operation/list_collections.go | 15 +- .../x/mongo/driver/operation/list_indexes.go | 2 + .../x/mongo/driver/operation/update.go | 2 + .../driver/operation/update_search_index.go | 240 +++++++++++++++ .../x/mongo/driver/session/server_session.go | 4 +- .../x/mongo/driver/session/session_pool.go | 4 +- .../x/mongo/driver/topology/DESIGN.md | 7 +- .../x/mongo/driver/topology/connection.go | 47 ++- .../driver/topology/connection_options.go | 6 +- .../x/mongo/driver/topology/errors.go | 38 ++- .../x/mongo/driver/topology/fsm.go | 87 ++++-- .../x/mongo/driver/topology/pool.go | 30 +- .../x/mongo/driver/topology/rtt_monitor.go | 31 +- .../x/mongo/driver/topology/server.go | 128 ++++++-- .../x/mongo/driver/topology/server_options.go | 38 ++- .../x/mongo/driver/topology/topology.go | 239 ++++++++++++++- .../mongo/driver/topology/topology_options.go | 46 ++- .../x/mongo/driver/wiremessage/wiremessage.go | 35 ++- vendor/modules.txt | 13 +- 125 files changed, 3713 insertions(+), 1292 deletions(-) create mode 100644 vendor/go.mongodb.org/mongo-driver/bson/bsoncodec/codec_cache.go rename vendor/go.mongodb.org/mongo-driver/internal/{string_util.go => bsonutil/bsonutil.go} (64%) delete mode 100644 vendor/go.mongodb.org/mongo-driver/internal/cancellation_listener.go create mode 100644 vendor/go.mongodb.org/mongo-driver/internal/codecutil/encoding.go rename vendor/go.mongodb.org/mongo-driver/internal/{csfle_util.go => csfle/csfle.go} (98%) rename vendor/go.mongodb.org/mongo-driver/internal/{csot_util.go => csot/csot.go} (99%) create mode 100644 vendor/go.mongodb.org/mongo-driver/internal/driverutil/hello.go create mode 100644 vendor/go.mongodb.org/mongo-driver/internal/driverutil/operation.go delete mode 100644 vendor/go.mongodb.org/mongo-driver/internal/error.go rename vendor/go.mongodb.org/mongo-driver/internal/{const.go => handshake/handshake.go} (64%) rename vendor/go.mongodb.org/mongo-driver/internal/{http.go => httputil/httputil.go} (74%) create mode 100644 vendor/go.mongodb.org/mongo-driver/internal/logger/context.go create mode 100644 vendor/go.mongodb.org/mongo-driver/internal/ptrutil/int64.go rename vendor/go.mongodb.org/mongo-driver/internal/{randutil => }/rand/bits.go (100%) rename vendor/go.mongodb.org/mongo-driver/internal/{randutil => }/rand/exp.go (100%) rename vendor/go.mongodb.org/mongo-driver/internal/{randutil => }/rand/normal.go (100%) rename vendor/go.mongodb.org/mongo-driver/internal/{randutil => }/rand/rand.go (99%) rename vendor/go.mongodb.org/mongo-driver/internal/{randutil => }/rand/rng.go (100%) delete mode 100644 vendor/go.mongodb.org/mongo-driver/internal/uri_validation_errors.go rename vendor/go.mongodb.org/mongo-driver/{internal => mongo}/background_context.go (87%) create mode 100644 vendor/go.mongodb.org/mongo-driver/mongo/options/searchindexoptions.go create mode 100644 vendor/go.mongodb.org/mongo-driver/mongo/search_index_view.go delete mode 100644 vendor/go.mongodb.org/mongo-driver/x/mongo/driver/list_collections_batch_cursor.go rename vendor/go.mongodb.org/mongo-driver/x/mongo/driver/operation/{createIndexes.go => create_indexes.go} (98%) create mode 100644 vendor/go.mongodb.org/mongo-driver/x/mongo/driver/operation/create_search_indexes.go create mode 100644 vendor/go.mongodb.org/mongo-driver/x/mongo/driver/operation/drop_search_index.go create mode 100644 vendor/go.mongodb.org/mongo-driver/x/mongo/driver/operation/update_search_index.go diff --git a/go.mod b/go.mod index e2319e387..173f9ca1d 100644 --- a/go.mod +++ b/go.mod @@ -17,7 +17,7 @@ require ( github.com/stretchr/testify v1.8.4 github.com/tcnksm/go-input v0.0.0-20180404061846-548a7d7a8ee8 github.com/urfave/cli/v2 v2.25.7 - go.mongodb.org/mongo-driver v1.12.1 + go.mongodb.org/mongo-driver v1.13.0 ) require ( diff --git a/go.sum b/go.sum index 8a86a6ff9..e05726bd1 100644 --- a/go.sum +++ b/go.sum @@ -334,8 +334,8 @@ github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9de github.com/yuin/goldmark v1.1.32/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= -go.mongodb.org/mongo-driver v1.12.1 h1:nLkghSU8fQNaK7oUmDhQFsnrtcoNy7Z6LVFKsEecqgE= -go.mongodb.org/mongo-driver v1.12.1/go.mod h1:/rGBTebI3XYboVmgz+Wv3Bcbl3aD0QF9zl6kDDw18rQ= +go.mongodb.org/mongo-driver v1.13.0 h1:67DgFFjYOCMWdtTEmKFpV3ffWlFnh+CYZ8ZS/tXWUfY= +go.mongodb.org/mongo-driver v1.13.0/go.mod h1:/rGBTebI3XYboVmgz+Wv3Bcbl3aD0QF9zl6kDDw18rQ= go.opencensus.io v0.21.0/go.mod h1:mSImk1erAIZhrmZN+AvHh14ztQfjbGwt4TtuofqLduU= go.opencensus.io v0.22.0/go.mod h1:+kGneAE2xo2IficOXnaByMWTGM9T73dGwxeWcUqIpI8= go.opencensus.io v0.22.2/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw= diff --git a/vendor/go.mongodb.org/mongo-driver/bson/bsoncodec/codec_cache.go b/vendor/go.mongodb.org/mongo-driver/bson/bsoncodec/codec_cache.go new file mode 100644 index 000000000..844b50299 --- /dev/null +++ b/vendor/go.mongodb.org/mongo-driver/bson/bsoncodec/codec_cache.go @@ -0,0 +1,166 @@ +// Copyright (C) MongoDB, Inc. 2017-present. +// +// Licensed under the Apache License, Version 2.0 (the "License"); you may +// not use this file except in compliance with the License. You may obtain +// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 + +package bsoncodec + +import ( + "reflect" + "sync" + "sync/atomic" +) + +// Runtime check that the kind encoder and decoder caches can store any valid +// reflect.Kind constant. +func init() { + if s := reflect.Kind(len(kindEncoderCache{}.entries)).String(); s != "kind27" { + panic("The capacity of kindEncoderCache is too small.\n" + + "This is due to a new type being added to reflect.Kind.") + } +} + +// statically assert array size +var _ = (kindEncoderCache{}).entries[reflect.UnsafePointer] +var _ = (kindDecoderCache{}).entries[reflect.UnsafePointer] + +type typeEncoderCache struct { + cache sync.Map // map[reflect.Type]ValueEncoder +} + +func (c *typeEncoderCache) Store(rt reflect.Type, enc ValueEncoder) { + c.cache.Store(rt, enc) +} + +func (c *typeEncoderCache) Load(rt reflect.Type) (ValueEncoder, bool) { + if v, _ := c.cache.Load(rt); v != nil { + return v.(ValueEncoder), true + } + return nil, false +} + +func (c *typeEncoderCache) LoadOrStore(rt reflect.Type, enc ValueEncoder) ValueEncoder { + if v, loaded := c.cache.LoadOrStore(rt, enc); loaded { + enc = v.(ValueEncoder) + } + return enc +} + +func (c *typeEncoderCache) Clone() *typeEncoderCache { + cc := new(typeEncoderCache) + c.cache.Range(func(k, v interface{}) bool { + if k != nil && v != nil { + cc.cache.Store(k, v) + } + return true + }) + return cc +} + +type typeDecoderCache struct { + cache sync.Map // map[reflect.Type]ValueDecoder +} + +func (c *typeDecoderCache) Store(rt reflect.Type, dec ValueDecoder) { + c.cache.Store(rt, dec) +} + +func (c *typeDecoderCache) Load(rt reflect.Type) (ValueDecoder, bool) { + if v, _ := c.cache.Load(rt); v != nil { + return v.(ValueDecoder), true + } + return nil, false +} + +func (c *typeDecoderCache) LoadOrStore(rt reflect.Type, dec ValueDecoder) ValueDecoder { + if v, loaded := c.cache.LoadOrStore(rt, dec); loaded { + dec = v.(ValueDecoder) + } + return dec +} + +func (c *typeDecoderCache) Clone() *typeDecoderCache { + cc := new(typeDecoderCache) + c.cache.Range(func(k, v interface{}) bool { + if k != nil && v != nil { + cc.cache.Store(k, v) + } + return true + }) + return cc +} + +// atomic.Value requires that all calls to Store() have the same concrete type +// so we wrap the ValueEncoder with a kindEncoderCacheEntry to ensure the type +// is always the same (since different concrete types may implement the +// ValueEncoder interface). +type kindEncoderCacheEntry struct { + enc ValueEncoder +} + +type kindEncoderCache struct { + entries [reflect.UnsafePointer + 1]atomic.Value // *kindEncoderCacheEntry +} + +func (c *kindEncoderCache) Store(rt reflect.Kind, enc ValueEncoder) { + if enc != nil && rt < reflect.Kind(len(c.entries)) { + c.entries[rt].Store(&kindEncoderCacheEntry{enc: enc}) + } +} + +func (c *kindEncoderCache) Load(rt reflect.Kind) (ValueEncoder, bool) { + if rt < reflect.Kind(len(c.entries)) { + if ent, ok := c.entries[rt].Load().(*kindEncoderCacheEntry); ok { + return ent.enc, ent.enc != nil + } + } + return nil, false +} + +func (c *kindEncoderCache) Clone() *kindEncoderCache { + cc := new(kindEncoderCache) + for i, v := range c.entries { + if val := v.Load(); val != nil { + cc.entries[i].Store(val) + } + } + return cc +} + +// atomic.Value requires that all calls to Store() have the same concrete type +// so we wrap the ValueDecoder with a kindDecoderCacheEntry to ensure the type +// is always the same (since different concrete types may implement the +// ValueDecoder interface). +type kindDecoderCacheEntry struct { + dec ValueDecoder +} + +type kindDecoderCache struct { + entries [reflect.UnsafePointer + 1]atomic.Value // *kindDecoderCacheEntry +} + +func (c *kindDecoderCache) Store(rt reflect.Kind, dec ValueDecoder) { + if rt < reflect.Kind(len(c.entries)) { + c.entries[rt].Store(&kindDecoderCacheEntry{dec: dec}) + } +} + +func (c *kindDecoderCache) Load(rt reflect.Kind) (ValueDecoder, bool) { + if rt < reflect.Kind(len(c.entries)) { + if ent, ok := c.entries[rt].Load().(*kindDecoderCacheEntry); ok { + return ent.dec, ent.dec != nil + } + } + return nil, false +} + +func (c *kindDecoderCache) Clone() *kindDecoderCache { + cc := new(kindDecoderCache) + for i, v := range c.entries { + if val := v.Load(); val != nil { + cc.entries[i].Store(val) + } + } + return cc +} diff --git a/vendor/go.mongodb.org/mongo-driver/bson/bsoncodec/default_value_decoders.go b/vendor/go.mongodb.org/mongo-driver/bson/bsoncodec/default_value_decoders.go index e479c3585..2ce119731 100644 --- a/vendor/go.mongodb.org/mongo-driver/bson/bsoncodec/default_value_decoders.go +++ b/vendor/go.mongodb.org/mongo-driver/bson/bsoncodec/default_value_decoders.go @@ -24,7 +24,7 @@ import ( var ( defaultValueDecoders DefaultValueDecoders - errCannotTruncate = errors.New("float64 can only be truncated to an integer type when truncation is enabled") + errCannotTruncate = errors.New("float64 can only be truncated to a lower precision type when truncation is enabled") ) type decodeBinaryError struct { diff --git a/vendor/go.mongodb.org/mongo-driver/bson/bsoncodec/pointer_codec.go b/vendor/go.mongodb.org/mongo-driver/bson/bsoncodec/pointer_codec.go index a1bf9c3e2..e5923230b 100644 --- a/vendor/go.mongodb.org/mongo-driver/bson/bsoncodec/pointer_codec.go +++ b/vendor/go.mongodb.org/mongo-driver/bson/bsoncodec/pointer_codec.go @@ -8,7 +8,6 @@ package bsoncodec import ( "reflect" - "sync" "go.mongodb.org/mongo-driver/bson/bsonrw" "go.mongodb.org/mongo-driver/bson/bsontype" @@ -22,9 +21,8 @@ var _ ValueDecoder = &PointerCodec{} // Deprecated: Use [go.mongodb.org/mongo-driver/bson.NewRegistry] to get a registry with the // PointerCodec registered. type PointerCodec struct { - ecache map[reflect.Type]ValueEncoder - dcache map[reflect.Type]ValueDecoder - l sync.RWMutex + ecache typeEncoderCache + dcache typeDecoderCache } // NewPointerCodec returns a PointerCodec that has been initialized. @@ -32,10 +30,7 @@ type PointerCodec struct { // Deprecated: Use [go.mongodb.org/mongo-driver/bson.NewRegistry] to get a registry with the // PointerCodec registered. func NewPointerCodec() *PointerCodec { - return &PointerCodec{ - ecache: make(map[reflect.Type]ValueEncoder), - dcache: make(map[reflect.Type]ValueDecoder), - } + return &PointerCodec{} } // EncodeValue handles encoding a pointer by either encoding it to BSON Null if the pointer is nil @@ -52,24 +47,19 @@ func (pc *PointerCodec) EncodeValue(ec EncodeContext, vw bsonrw.ValueWriter, val return vw.WriteNull() } - pc.l.RLock() - enc, ok := pc.ecache[val.Type()] - pc.l.RUnlock() - if ok { - if enc == nil { - return ErrNoEncoder{Type: val.Type()} + typ := val.Type() + if v, ok := pc.ecache.Load(typ); ok { + if v == nil { + return ErrNoEncoder{Type: typ} } - return enc.EncodeValue(ec, vw, val.Elem()) + return v.EncodeValue(ec, vw, val.Elem()) } - - enc, err := ec.LookupEncoder(val.Type().Elem()) - pc.l.Lock() - pc.ecache[val.Type()] = enc - pc.l.Unlock() + // TODO(charlie): handle concurrent requests for the same type + enc, err := ec.LookupEncoder(typ.Elem()) + enc = pc.ecache.LoadOrStore(typ, enc) if err != nil { return err } - return enc.EncodeValue(ec, vw, val.Elem()) } @@ -80,36 +70,31 @@ func (pc *PointerCodec) DecodeValue(dc DecodeContext, vr bsonrw.ValueReader, val return ValueDecoderError{Name: "PointerCodec.DecodeValue", Kinds: []reflect.Kind{reflect.Ptr}, Received: val} } + typ := val.Type() if vr.Type() == bsontype.Null { - val.Set(reflect.Zero(val.Type())) + val.Set(reflect.Zero(typ)) return vr.ReadNull() } if vr.Type() == bsontype.Undefined { - val.Set(reflect.Zero(val.Type())) + val.Set(reflect.Zero(typ)) return vr.ReadUndefined() } if val.IsNil() { - val.Set(reflect.New(val.Type().Elem())) + val.Set(reflect.New(typ.Elem())) } - pc.l.RLock() - dec, ok := pc.dcache[val.Type()] - pc.l.RUnlock() - if ok { - if dec == nil { - return ErrNoDecoder{Type: val.Type()} + if v, ok := pc.dcache.Load(typ); ok { + if v == nil { + return ErrNoDecoder{Type: typ} } - return dec.DecodeValue(dc, vr, val.Elem()) + return v.DecodeValue(dc, vr, val.Elem()) } - - dec, err := dc.LookupDecoder(val.Type().Elem()) - pc.l.Lock() - pc.dcache[val.Type()] = dec - pc.l.Unlock() + // TODO(charlie): handle concurrent requests for the same type + dec, err := dc.LookupDecoder(typ.Elem()) + dec = pc.dcache.LoadOrStore(typ, dec) if err != nil { return err } - return dec.DecodeValue(dc, vr, val.Elem()) } diff --git a/vendor/go.mongodb.org/mongo-driver/bson/bsoncodec/registry.go b/vendor/go.mongodb.org/mongo-driver/bson/bsoncodec/registry.go index 930de2849..f309ee2b3 100644 --- a/vendor/go.mongodb.org/mongo-driver/bson/bsoncodec/registry.go +++ b/vendor/go.mongodb.org/mongo-driver/bson/bsoncodec/registry.go @@ -216,72 +216,42 @@ func (rb *RegistryBuilder) RegisterTypeMapEntry(bt bsontype.Type, rt reflect.Typ // // Deprecated: Use NewRegistry instead. func (rb *RegistryBuilder) Build() *Registry { - registry := new(Registry) - - registry.typeEncoders = make(map[reflect.Type]ValueEncoder, len(rb.registry.typeEncoders)) - for t, enc := range rb.registry.typeEncoders { - registry.typeEncoders[t] = enc - } - - registry.typeDecoders = make(map[reflect.Type]ValueDecoder, len(rb.registry.typeDecoders)) - for t, dec := range rb.registry.typeDecoders { - registry.typeDecoders[t] = dec - } - - registry.interfaceEncoders = make([]interfaceValueEncoder, len(rb.registry.interfaceEncoders)) - copy(registry.interfaceEncoders, rb.registry.interfaceEncoders) - - registry.interfaceDecoders = make([]interfaceValueDecoder, len(rb.registry.interfaceDecoders)) - copy(registry.interfaceDecoders, rb.registry.interfaceDecoders) - - registry.kindEncoders = make(map[reflect.Kind]ValueEncoder) - for kind, enc := range rb.registry.kindEncoders { - registry.kindEncoders[kind] = enc + r := &Registry{ + interfaceEncoders: append([]interfaceValueEncoder(nil), rb.registry.interfaceEncoders...), + interfaceDecoders: append([]interfaceValueDecoder(nil), rb.registry.interfaceDecoders...), + typeEncoders: rb.registry.typeEncoders.Clone(), + typeDecoders: rb.registry.typeDecoders.Clone(), + kindEncoders: rb.registry.kindEncoders.Clone(), + kindDecoders: rb.registry.kindDecoders.Clone(), } - - registry.kindDecoders = make(map[reflect.Kind]ValueDecoder) - for kind, dec := range rb.registry.kindDecoders { - registry.kindDecoders[kind] = dec - } - - registry.typeMap = make(map[bsontype.Type]reflect.Type) - for bt, rt := range rb.registry.typeMap { - registry.typeMap[bt] = rt - } - - return registry + rb.registry.typeMap.Range(func(k, v interface{}) bool { + if k != nil && v != nil { + r.typeMap.Store(k, v) + } + return true + }) + return r } // A Registry is used to store and retrieve codecs for types and interfaces. This type is the main // typed passed around and Encoders and Decoders are constructed from it. type Registry struct { - typeEncoders map[reflect.Type]ValueEncoder - typeDecoders map[reflect.Type]ValueDecoder - interfaceEncoders []interfaceValueEncoder interfaceDecoders []interfaceValueDecoder - - kindEncoders map[reflect.Kind]ValueEncoder - kindDecoders map[reflect.Kind]ValueDecoder - - typeMap map[bsontype.Type]reflect.Type - - mu sync.RWMutex + typeEncoders *typeEncoderCache + typeDecoders *typeDecoderCache + kindEncoders *kindEncoderCache + kindDecoders *kindDecoderCache + typeMap sync.Map // map[bsontype.Type]reflect.Type } // NewRegistry creates a new empty Registry. func NewRegistry() *Registry { return &Registry{ - typeEncoders: make(map[reflect.Type]ValueEncoder), - typeDecoders: make(map[reflect.Type]ValueDecoder), - - interfaceEncoders: make([]interfaceValueEncoder, 0), - interfaceDecoders: make([]interfaceValueDecoder, 0), - - kindEncoders: make(map[reflect.Kind]ValueEncoder), - kindDecoders: make(map[reflect.Kind]ValueDecoder), - - typeMap: make(map[bsontype.Type]reflect.Type), + typeEncoders: new(typeEncoderCache), + typeDecoders: new(typeDecoderCache), + kindEncoders: new(kindEncoderCache), + kindDecoders: new(kindDecoderCache), } } @@ -296,7 +266,7 @@ func NewRegistry() *Registry { // // RegisterTypeEncoder should not be called concurrently with any other Registry method. func (r *Registry) RegisterTypeEncoder(valueType reflect.Type, enc ValueEncoder) { - r.typeEncoders[valueType] = enc + r.typeEncoders.Store(valueType, enc) } // RegisterTypeDecoder registers the provided ValueDecoder for the provided type. @@ -310,7 +280,7 @@ func (r *Registry) RegisterTypeEncoder(valueType reflect.Type, enc ValueEncoder) // // RegisterTypeDecoder should not be called concurrently with any other Registry method. func (r *Registry) RegisterTypeDecoder(valueType reflect.Type, dec ValueDecoder) { - r.typeDecoders[valueType] = dec + r.typeDecoders.Store(valueType, dec) } // RegisterKindEncoder registers the provided ValueEncoder for the provided kind. @@ -326,7 +296,7 @@ func (r *Registry) RegisterTypeDecoder(valueType reflect.Type, dec ValueDecoder) // // RegisterKindEncoder should not be called concurrently with any other Registry method. func (r *Registry) RegisterKindEncoder(kind reflect.Kind, enc ValueEncoder) { - r.kindEncoders[kind] = enc + r.kindEncoders.Store(kind, enc) } // RegisterKindDecoder registers the provided ValueDecoder for the provided kind. @@ -342,7 +312,7 @@ func (r *Registry) RegisterKindEncoder(kind reflect.Kind, enc ValueEncoder) { // // RegisterKindDecoder should not be called concurrently with any other Registry method. func (r *Registry) RegisterKindDecoder(kind reflect.Kind, dec ValueDecoder) { - r.kindDecoders[kind] = dec + r.kindDecoders.Store(kind, dec) } // RegisterInterfaceEncoder registers an encoder for the provided interface type iface. This encoder will @@ -401,7 +371,7 @@ func (r *Registry) RegisterInterfaceDecoder(iface reflect.Type, dec ValueDecoder // // reg.RegisterTypeMapEntry(bsontype.EmbeddedDocument, reflect.TypeOf(bson.Raw{})) func (r *Registry) RegisterTypeMapEntry(bt bsontype.Type, rt reflect.Type) { - r.typeMap[bt] = rt + r.typeMap.Store(bt, rt) } // LookupEncoder returns the first matching encoder in the Registry. It uses the following lookup @@ -418,9 +388,7 @@ 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) { - r.mu.RLock() enc, found := r.lookupTypeEncoder(valueType) - r.mu.RUnlock() if found { if enc == nil { return nil, ErrNoEncoder{Type: valueType} @@ -430,36 +398,26 @@ func (r *Registry) LookupEncoder(valueType reflect.Type) (ValueEncoder, error) { enc, found = r.lookupInterfaceEncoder(valueType, true) if found { - r.mu.Lock() - r.typeEncoders[valueType] = enc - r.mu.Unlock() - return enc, nil + return r.typeEncoders.LoadOrStore(valueType, enc), nil } - if valueType == nil { - r.mu.Lock() - r.typeEncoders[valueType] = nil - r.mu.Unlock() + r.storeTypeEncoder(valueType, nil) return nil, ErrNoEncoder{Type: valueType} } - enc, found = r.kindEncoders[valueType.Kind()] - if !found { - r.mu.Lock() - r.typeEncoders[valueType] = nil - r.mu.Unlock() - 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} +} - r.mu.Lock() - r.typeEncoders[valueType] = enc - r.mu.Unlock() - return enc, nil +func (r *Registry) storeTypeEncoder(rt reflect.Type, enc ValueEncoder) ValueEncoder { + return r.typeEncoders.LoadOrStore(rt, enc) } -func (r *Registry) lookupTypeEncoder(valueType reflect.Type) (ValueEncoder, bool) { - enc, found := r.typeEncoders[valueType] - return enc, found +func (r *Registry) lookupTypeEncoder(rt reflect.Type) (ValueEncoder, bool) { + return r.typeEncoders.Load(rt) } func (r *Registry) lookupInterfaceEncoder(valueType reflect.Type, allowAddr bool) (ValueEncoder, bool) { @@ -475,7 +433,7 @@ func (r *Registry) lookupInterfaceEncoder(valueType reflect.Type, allowAddr bool // ahead in interfaceEncoders defaultEnc, found := r.lookupInterfaceEncoder(valueType, false) if !found { - defaultEnc = r.kindEncoders[valueType.Kind()] + defaultEnc, _ = r.kindEncoders.Load(valueType.Kind()) } return newCondAddrEncoder(ienc.ve, defaultEnc), true } @@ -500,10 +458,7 @@ func (r *Registry) LookupDecoder(valueType reflect.Type) (ValueDecoder, error) { if valueType == nil { return nil, ErrNilType } - decodererr := ErrNoDecoder{Type: valueType} - r.mu.RLock() dec, found := r.lookupTypeDecoder(valueType) - r.mu.RUnlock() if found { if dec == nil { return nil, ErrNoDecoder{Type: valueType} @@ -513,29 +468,22 @@ func (r *Registry) LookupDecoder(valueType reflect.Type) (ValueDecoder, error) { dec, found = r.lookupInterfaceDecoder(valueType, true) if found { - r.mu.Lock() - r.typeDecoders[valueType] = dec - r.mu.Unlock() - return dec, nil + return r.storeTypeDecoder(valueType, dec), nil } - dec, found = r.kindDecoders[valueType.Kind()] - if !found { - r.mu.Lock() - r.typeDecoders[valueType] = nil - r.mu.Unlock() - return nil, decodererr + if v, ok := r.kindDecoders.Load(valueType.Kind()); ok { + return r.storeTypeDecoder(valueType, v), nil } - - r.mu.Lock() - r.typeDecoders[valueType] = dec - r.mu.Unlock() - return dec, nil + r.storeTypeDecoder(valueType, nil) + return nil, ErrNoDecoder{Type: valueType} } func (r *Registry) lookupTypeDecoder(valueType reflect.Type) (ValueDecoder, bool) { - dec, found := r.typeDecoders[valueType] - return dec, found + return r.typeDecoders.Load(valueType) +} + +func (r *Registry) storeTypeDecoder(typ reflect.Type, dec ValueDecoder) ValueDecoder { + return r.typeDecoders.LoadOrStore(typ, dec) } func (r *Registry) lookupInterfaceDecoder(valueType reflect.Type, allowAddr bool) (ValueDecoder, bool) { @@ -548,7 +496,7 @@ func (r *Registry) lookupInterfaceDecoder(valueType reflect.Type, allowAddr bool // ahead in interfaceDecoders defaultDec, found := r.lookupInterfaceDecoder(valueType, false) if !found { - defaultDec = r.kindDecoders[valueType.Kind()] + defaultDec, _ = r.kindDecoders.Load(valueType.Kind()) } return newCondAddrDecoder(idec.vd, defaultDec), true } @@ -561,11 +509,11 @@ func (r *Registry) lookupInterfaceDecoder(valueType reflect.Type, allowAddr bool // // LookupTypeMapEntry should not be called concurrently with any other Registry method. func (r *Registry) LookupTypeMapEntry(bt bsontype.Type) (reflect.Type, error) { - t, ok := r.typeMap[bt] - if !ok || t == nil { + v, ok := r.typeMap.Load(bt) + if v == nil || !ok { return nil, ErrNoTypeMapEntry{Type: bt} } - return t, nil + return v.(reflect.Type), nil } type interfaceValueEncoder struct { diff --git a/vendor/go.mongodb.org/mongo-driver/bson/bsoncodec/slice_codec.go b/vendor/go.mongodb.org/mongo-driver/bson/bsoncodec/slice_codec.go index 20c3e7549..a43daf005 100644 --- a/vendor/go.mongodb.org/mongo-driver/bson/bsoncodec/slice_codec.go +++ b/vendor/go.mongodb.org/mongo-driver/bson/bsoncodec/slice_codec.go @@ -62,7 +62,7 @@ func (sc SliceCodec) EncodeValue(ec EncodeContext, vw bsonrw.ValueWriter, val re } // If we have a []primitive.E we want to treat it as a document instead of as an array. - if val.Type().ConvertibleTo(tD) { + if val.Type() == tD || val.Type().ConvertibleTo(tD) { d := val.Convert(tD).Interface().(primitive.D) dw, err := vw.WriteDocument() diff --git a/vendor/go.mongodb.org/mongo-driver/bson/bsoncodec/struct_codec.go b/vendor/go.mongodb.org/mongo-driver/bson/bsoncodec/struct_codec.go index 1dfdd9886..4cde0a4d6 100644 --- a/vendor/go.mongodb.org/mongo-driver/bson/bsoncodec/struct_codec.go +++ b/vendor/go.mongodb.org/mongo-driver/bson/bsoncodec/struct_codec.go @@ -63,8 +63,7 @@ type Zeroer interface { // Deprecated: Use [go.mongodb.org/mongo-driver/bson.NewRegistry] to get a registry with the // StructCodec registered. type StructCodec struct { - cache map[reflect.Type]*structDescription - l sync.RWMutex + cache sync.Map // map[reflect.Type]*structDescription parser StructTagParser // DecodeZeroStruct causes DecodeValue to delete any existing values from Go structs in the @@ -115,7 +114,6 @@ func NewStructCodec(p StructTagParser, opts ...*bsonoptions.StructCodecOptions) structOpt := bsonoptions.MergeStructCodecOptions(opts...) codec := &StructCodec{ - cache: make(map[reflect.Type]*structDescription), parser: p, } @@ -192,15 +190,14 @@ func (sc *StructCodec) EncodeValue(ec EncodeContext, vw bsonrw.ValueWriter, val encoder := desc.encoder var zero bool - rvInterface := rv.Interface() if cz, ok := encoder.(CodecZeroer); ok { - zero = cz.IsTypeZero(rvInterface) + zero = cz.IsTypeZero(rv.Interface()) } else if rv.Kind() == reflect.Interface { // isZero will not treat an interface rv as an interface, so we need to check for the // zero interface separately. zero = rv.IsNil() } else { - zero = isZero(rvInterface, sc.EncodeOmitDefaultStruct || ec.omitZeroStruct) + zero = isZero(rv, sc.EncodeOmitDefaultStruct || ec.omitZeroStruct) } if desc.omitEmpty && zero { continue @@ -394,56 +391,32 @@ func (sc *StructCodec) DecodeValue(dc DecodeContext, vr bsonrw.ValueReader, val return nil } -func isZero(i interface{}, omitZeroStruct bool) bool { - v := reflect.ValueOf(i) - - // check the value validity - if !v.IsValid() { - return true +func isZero(v reflect.Value, omitZeroStruct bool) bool { + kind := v.Kind() + if (kind != reflect.Ptr || !v.IsNil()) && v.Type().Implements(tZeroer) { + return v.Interface().(Zeroer).IsZero() } - - if z, ok := v.Interface().(Zeroer); ok && (v.Kind() != reflect.Ptr || !v.IsNil()) { - return z.IsZero() - } - - switch v.Kind() { - case reflect.Array, reflect.Map, reflect.Slice, reflect.String: - return v.Len() == 0 - case reflect.Bool: - return !v.Bool() - case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: - return v.Int() == 0 - case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr: - return v.Uint() == 0 - case reflect.Float32, reflect.Float64: - return v.Float() == 0 - case reflect.Interface, reflect.Ptr: - return v.IsNil() - case reflect.Struct: + if kind == reflect.Struct { if !omitZeroStruct { return false } - - // TODO(GODRIVER-2820): Update the logic to be able to handle private struct fields. - // TODO Use condition "reflect.Zero(v.Type()).Equal(v)" instead. - vt := v.Type() if vt == tTime { return v.Interface().(time.Time).IsZero() } - for i := 0; i < v.NumField(); i++ { - if vt.Field(i).PkgPath != "" && !vt.Field(i).Anonymous { + numField := vt.NumField() + for i := 0; i < numField; i++ { + ff := vt.Field(i) + if ff.PkgPath != "" && !ff.Anonymous { continue // Private field } - fld := v.Field(i) - if !isZero(fld.Interface(), omitZeroStruct) { + if !isZero(v.Field(i), omitZeroStruct) { return false } } return true } - - return false + return !v.IsValid() || v.IsZero() } type structDescription struct { @@ -502,13 +475,27 @@ func (sc *StructCodec) describeStruct( ) (*structDescription, error) { // We need to analyze the struct, including getting the tags, collecting // information about inlining, and create a map of the field name to the field. - sc.l.RLock() - ds, exists := sc.cache[t] - sc.l.RUnlock() - if exists { - return ds, nil + if v, ok := sc.cache.Load(t); ok { + return v.(*structDescription), nil + } + // TODO(charlie): Only describe the struct once when called + // concurrently with the same type. + ds, err := sc.describeStructSlow(r, t, useJSONStructTags, errorOnDuplicates) + if err != nil { + return nil, err } + if v, loaded := sc.cache.LoadOrStore(t, ds); loaded { + ds = v.(*structDescription) + } + return ds, nil +} +func (sc *StructCodec) describeStructSlow( + r *Registry, + t reflect.Type, + useJSONStructTags bool, + errorOnDuplicates bool, +) (*structDescription, error) { numFields := t.NumField() sd := &structDescription{ fm: make(map[string]fieldDescription, numFields), @@ -639,10 +626,6 @@ func (sc *StructCodec) describeStruct( sort.Sort(byIndex(sd.fl)) - sc.l.Lock() - sc.cache[t] = sd - sc.l.Unlock() - return sd, nil } @@ -700,21 +683,21 @@ func getInlineField(val reflect.Value, index []int) (reflect.Value, error) { // DeepZero returns recursive zero object func deepZero(st reflect.Type) (result reflect.Value) { - result = reflect.Indirect(reflect.New(st)) - - if result.Kind() == reflect.Struct { - for i := 0; i < result.NumField(); i++ { - if f := result.Field(i); f.Kind() == reflect.Ptr { - if f.CanInterface() { - if ft := reflect.TypeOf(f.Interface()); ft.Elem().Kind() == reflect.Struct { - result.Field(i).Set(recursivePointerTo(deepZero(ft.Elem()))) - } + if st.Kind() == reflect.Struct { + numField := st.NumField() + for i := 0; i < numField; i++ { + if result == emptyValue { + result = reflect.Indirect(reflect.New(st)) + } + f := result.Field(i) + if f.CanInterface() { + if f.Type().Kind() == reflect.Struct { + result.Field(i).Set(recursivePointerTo(deepZero(f.Type().Elem()))) } } } } - - return + return result } // recursivePointerTo calls reflect.New(v.Type) but recursively for its fields inside diff --git a/vendor/go.mongodb.org/mongo-driver/bson/bsoncodec/types.go b/vendor/go.mongodb.org/mongo-driver/bson/bsoncodec/types.go index 07f4b70e6..6ade17b7d 100644 --- a/vendor/go.mongodb.org/mongo-driver/bson/bsoncodec/types.go +++ b/vendor/go.mongodb.org/mongo-driver/bson/bsoncodec/types.go @@ -34,6 +34,7 @@ var tValueUnmarshaler = reflect.TypeOf((*ValueUnmarshaler)(nil)).Elem() var tMarshaler = reflect.TypeOf((*Marshaler)(nil)).Elem() var tUnmarshaler = reflect.TypeOf((*Unmarshaler)(nil)).Elem() var tProxy = reflect.TypeOf((*Proxy)(nil)).Elem() +var tZeroer = reflect.TypeOf((*Zeroer)(nil)).Elem() var tBinary = reflect.TypeOf(primitive.Binary{}) var tUndefined = reflect.TypeOf(primitive.Undefined{}) diff --git a/vendor/go.mongodb.org/mongo-driver/bson/bsonrw/copier.go b/vendor/go.mongodb.org/mongo-driver/bson/bsonrw/copier.go index 33d59bd25..4d279b7fe 100644 --- a/vendor/go.mongodb.org/mongo-driver/bson/bsonrw/copier.go +++ b/vendor/go.mongodb.org/mongo-driver/bson/bsonrw/copier.go @@ -124,7 +124,7 @@ func (c Copier) CopyBytesToDocumentWriter(dst DocumentWriter, src []byte) error } func (c Copier) copyBytesToValueWriter(src []byte, wef writeElementFn) error { - // TODO(skriptble): Create errors types here. Anything thats a tag should be a property. + // TODO(skriptble): Create errors types here. Anything that is a tag should be a property. length, rem, ok := bsoncore.ReadLength(src) if !ok { return fmt.Errorf("couldn't read length from src, not enough bytes. length=%d", len(src)) @@ -193,7 +193,7 @@ func (c Copier) AppendDocumentBytes(dst []byte, src ValueReader) ([]byte, error) } vw := vwPool.Get().(*valueWriter) - defer vwPool.Put(vw) + defer putValueWriter(vw) vw.reset(dst) @@ -213,7 +213,7 @@ func (c Copier) AppendArrayBytes(dst []byte, src ValueReader) ([]byte, error) { } vw := vwPool.Get().(*valueWriter) - defer vwPool.Put(vw) + defer putValueWriter(vw) vw.reset(dst) @@ -258,7 +258,7 @@ func (c Copier) AppendValueBytes(dst []byte, src ValueReader) (bsontype.Type, [] } vw := vwPool.Get().(*valueWriter) - defer vwPool.Put(vw) + defer putValueWriter(vw) start := len(dst) diff --git a/vendor/go.mongodb.org/mongo-driver/bson/bsonrw/value_reader.go b/vendor/go.mongodb.org/mongo-driver/bson/bsonrw/value_reader.go index 9bf24fae0..a242bb57c 100644 --- a/vendor/go.mongodb.org/mongo-driver/bson/bsonrw/value_reader.go +++ b/vendor/go.mongodb.org/mongo-driver/bson/bsonrw/value_reader.go @@ -739,8 +739,7 @@ func (vr *valueReader) ReadValue() (ValueReader, error) { return nil, ErrEOA } - _, err = vr.readCString() - if err != nil { + if err := vr.skipCString(); err != nil { return nil, err } @@ -794,6 +793,15 @@ func (vr *valueReader) readByte() (byte, error) { return vr.d[vr.offset-1], nil } +func (vr *valueReader) skipCString() error { + idx := bytes.IndexByte(vr.d[vr.offset:], 0x00) + if idx < 0 { + return io.EOF + } + vr.offset += int64(idx) + 1 + return nil +} + func (vr *valueReader) readCString() (string, error) { idx := bytes.IndexByte(vr.d[vr.offset:], 0x00) if idx < 0 { diff --git a/vendor/go.mongodb.org/mongo-driver/bson/bsonrw/value_writer.go b/vendor/go.mongodb.org/mongo-driver/bson/bsonrw/value_writer.go index a6dd8d34f..311518a80 100644 --- a/vendor/go.mongodb.org/mongo-driver/bson/bsonrw/value_writer.go +++ b/vendor/go.mongodb.org/mongo-driver/bson/bsonrw/value_writer.go @@ -28,6 +28,13 @@ var vwPool = sync.Pool{ }, } +func putValueWriter(vw *valueWriter) { + if vw != nil { + vw.w = nil // don't leak the writer + vwPool.Put(vw) + } +} + // BSONValueWriterPool is a pool for BSON ValueWriters. // // Deprecated: BSONValueWriterPool will not be supported in Go Driver 2.0. @@ -149,32 +156,21 @@ type valueWriter struct { } func (vw *valueWriter) advanceFrame() { - if vw.frame+1 >= int64(len(vw.stack)) { // We need to grow the stack - length := len(vw.stack) - if length+1 >= cap(vw.stack) { - // double it - buf := make([]vwState, 2*cap(vw.stack)+1) - copy(buf, vw.stack) - vw.stack = buf - } - vw.stack = vw.stack[:length+1] - } vw.frame++ + if vw.frame >= int64(len(vw.stack)) { + vw.stack = append(vw.stack, vwState{}) + } } func (vw *valueWriter) push(m mode) { vw.advanceFrame() // Clean the stack - vw.stack[vw.frame].mode = m - vw.stack[vw.frame].key = "" - vw.stack[vw.frame].arrkey = 0 - vw.stack[vw.frame].start = 0 + vw.stack[vw.frame] = vwState{mode: m} - vw.stack[vw.frame].mode = m switch m { case mDocument, mArray, mCodeWithScope: - vw.reserveLength() + vw.reserveLength() // WARN: this is not needed } } @@ -213,6 +209,7 @@ func newValueWriter(w io.Writer) *valueWriter { return vw } +// TODO: only used in tests func newValueWriterFromSlice(buf []byte) *valueWriter { vw := new(valueWriter) stack := make([]vwState, 1, 5) @@ -249,17 +246,16 @@ func (vw *valueWriter) invalidTransitionError(destination mode, name string, mod } func (vw *valueWriter) writeElementHeader(t bsontype.Type, destination mode, callerName string, addmodes ...mode) error { - switch vw.stack[vw.frame].mode { + frame := &vw.stack[vw.frame] + switch frame.mode { case mElement: - key := vw.stack[vw.frame].key + key := frame.key if !isValidCString(key) { return errors.New("BSON element key cannot contain null bytes") } - - vw.buf = bsoncore.AppendHeader(vw.buf, t, key) + vw.appendHeader(t, key) case mValue: - // TODO: Do this with a cache of the first 1000 or so array keys. - vw.buf = bsoncore.AppendHeader(vw.buf, t, strconv.Itoa(vw.stack[vw.frame].arrkey)) + vw.appendIntHeader(t, frame.arrkey) default: modes := []mode{mElement, mValue} if addmodes != nil { @@ -601,9 +597,11 @@ func (vw *valueWriter) writeLength() error { if length > maxSize { return errMaxDocumentSizeExceeded{size: int64(len(vw.buf))} } - length = length - int(vw.stack[vw.frame].start) - start := vw.stack[vw.frame].start + frame := &vw.stack[vw.frame] + length = length - int(frame.start) + start := frame.start + _ = vw.buf[start+3] // BCE vw.buf[start+0] = byte(length) vw.buf[start+1] = byte(length >> 8) vw.buf[start+2] = byte(length >> 16) @@ -612,5 +610,31 @@ func (vw *valueWriter) writeLength() error { } func isValidCString(cs string) bool { - return !strings.ContainsRune(cs, '\x00') + // Disallow the zero byte in a cstring because the zero byte is used as the + // terminating character. + // + // It's safe to check bytes instead of runes because all multibyte UTF-8 + // code points start with (binary) 11xxxxxx or 10xxxxxx, so 00000000 (i.e. + // 0) will never be part of a multibyte UTF-8 code point. This logic is the + // same as the "r < utf8.RuneSelf" case in strings.IndexRune but can be + // inlined. + // + // https://cs.opensource.google/go/go/+/refs/tags/go1.21.1:src/strings/strings.go;l=127 + return strings.IndexByte(cs, 0) == -1 +} + +// appendHeader is the same as bsoncore.AppendHeader but does not check if the +// key is a valid C string since the caller has already checked for that. +// +// The caller of this function must check if key is a valid C string. +func (vw *valueWriter) appendHeader(t bsontype.Type, key string) { + vw.buf = bsoncore.AppendType(vw.buf, t) + vw.buf = append(vw.buf, key...) + vw.buf = append(vw.buf, 0x00) +} + +func (vw *valueWriter) appendIntHeader(t bsontype.Type, key int) { + vw.buf = bsoncore.AppendType(vw.buf, t) + vw.buf = strconv.AppendInt(vw.buf, int64(key), 10) + vw.buf = append(vw.buf, 0x00) } diff --git a/vendor/go.mongodb.org/mongo-driver/bson/bsontype/bsontype.go b/vendor/go.mongodb.org/mongo-driver/bson/bsontype/bsontype.go index 8cff5492d..255d9909e 100644 --- a/vendor/go.mongodb.org/mongo-driver/bson/bsontype/bsontype.go +++ b/vendor/go.mongodb.org/mongo-driver/bson/bsontype/bsontype.go @@ -47,6 +47,7 @@ const ( BinaryMD5 byte = 0x05 BinaryEncrypted byte = 0x06 BinaryColumn byte = 0x07 + BinarySensitive byte = 0x08 BinaryUserDefined byte = 0x80 ) diff --git a/vendor/go.mongodb.org/mongo-driver/bson/marshal.go b/vendor/go.mongodb.org/mongo-driver/bson/marshal.go index f2c48d049..17ce6697e 100644 --- a/vendor/go.mongodb.org/mongo-driver/bson/marshal.go +++ b/vendor/go.mongodb.org/mongo-driver/bson/marshal.go @@ -9,6 +9,7 @@ package bson import ( "bytes" "encoding/json" + "sync" "go.mongodb.org/mongo-driver/bson/bsoncodec" "go.mongodb.org/mongo-driver/bson/bsonrw" @@ -141,6 +142,13 @@ func MarshalAppendWithRegistry(r *bsoncodec.Registry, dst []byte, val interface{ return MarshalAppendWithContext(bsoncodec.EncodeContext{Registry: r}, dst, val) } +// Pool of buffers for marshalling BSON. +var bufPool = sync.Pool{ + New: func() interface{} { + return new(bytes.Buffer) + }, +} + // MarshalAppendWithContext will encode val as a BSON document using Registry r and EncodeContext ec and append the // bytes to dst. If dst is not large enough to hold the bytes, it will be grown. If val is not a type that can be // transformed into a document, MarshalValueAppendWithContext should be used instead. @@ -162,8 +170,26 @@ func MarshalAppendWithRegistry(r *bsoncodec.Registry, dst []byte, val interface{ // // See [Encoder] for more examples. func MarshalAppendWithContext(ec bsoncodec.EncodeContext, dst []byte, val interface{}) ([]byte, error) { - sw := new(bsonrw.SliceWriter) - *sw = dst + sw := bufPool.Get().(*bytes.Buffer) + defer func() { + // Proper usage of a sync.Pool requires each entry to have approximately + // the same memory cost. To obtain this property when the stored type + // contains a variably-sized buffer, we add a hard limit on the maximum + // buffer to place back in the pool. We limit the size to 16MiB because + // that's the maximum wire message size supported by any current MongoDB + // server. + // + // Comment based on + // https://cs.opensource.google/go/go/+/refs/tags/go1.19:src/fmt/print.go;l=147 + // + // Recycle byte slices that are smaller than 16MiB and at least half + // occupied. + if sw.Cap() < 16*1024*1024 && sw.Cap()/2 < sw.Len() { + bufPool.Put(sw) + } + }() + + sw.Reset() vw := bvwPool.Get(sw) defer bvwPool.Put(vw) @@ -184,7 +210,7 @@ func MarshalAppendWithContext(ec bsoncodec.EncodeContext, dst []byte, val interf return nil, err } - return *sw, nil + return append(dst, sw.Bytes()...), nil } // MarshalValue returns the BSON encoding of val. diff --git a/vendor/go.mongodb.org/mongo-driver/bson/raw.go b/vendor/go.mongodb.org/mongo-driver/bson/raw.go index fe990a177..130da61ba 100644 --- a/vendor/go.mongodb.org/mongo-driver/bson/raw.go +++ b/vendor/go.mongodb.org/mongo-driver/bson/raw.go @@ -60,12 +60,19 @@ func (r Raw) LookupErr(key ...string) (RawValue, error) { // elements. If the document is not valid, the elements up to the invalid point will be returned // along with an error. func (r Raw) Elements() ([]RawElement, error) { - elems, err := bsoncore.Document(r).Elements() + doc := bsoncore.Document(r) + if len(doc) == 0 { + return nil, nil + } + elems, err := doc.Elements() + if err != nil { + return nil, err + } relems := make([]RawElement, 0, len(elems)) for _, elem := range elems { relems = append(relems, RawElement(elem)) } - return relems, err + return relems, nil } // Values returns this document as a slice of values. The returned slice will contain valid values. diff --git a/vendor/go.mongodb.org/mongo-driver/bson/types.go b/vendor/go.mongodb.org/mongo-driver/bson/types.go index e201ac37e..ef3981246 100644 --- a/vendor/go.mongodb.org/mongo-driver/bson/types.go +++ b/vendor/go.mongodb.org/mongo-driver/bson/types.go @@ -45,5 +45,6 @@ const ( TypeBinaryMD5 = bsontype.BinaryMD5 TypeBinaryEncrypted = bsontype.BinaryEncrypted TypeBinaryColumn = bsontype.BinaryColumn + TypeBinarySensitive = bsontype.BinarySensitive TypeBinaryUserDefined = bsontype.BinaryUserDefined ) diff --git a/vendor/go.mongodb.org/mongo-driver/event/monitoring.go b/vendor/go.mongodb.org/mongo-driver/event/monitoring.go index 195fb6b46..53d1caf2e 100644 --- a/vendor/go.mongodb.org/mongo-driver/event/monitoring.go +++ b/vendor/go.mongodb.org/mongo-driver/event/monitoring.go @@ -43,6 +43,7 @@ type CommandFinishedEvent struct { 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 diff --git a/vendor/go.mongodb.org/mongo-driver/internal/string_util.go b/vendor/go.mongodb.org/mongo-driver/internal/bsonutil/bsonutil.go similarity index 64% rename from vendor/go.mongodb.org/mongo-driver/internal/string_util.go rename to vendor/go.mongodb.org/mongo-driver/internal/bsonutil/bsonutil.go index 6cafa791d..eebb32890 100644 --- a/vendor/go.mongodb.org/mongo-driver/internal/string_util.go +++ b/vendor/go.mongodb.org/mongo-driver/internal/bsonutil/bsonutil.go @@ -4,7 +4,7 @@ // not use this file except in compliance with the License. You may obtain // a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 -package internal +package bsonutil import ( "fmt" @@ -12,13 +12,6 @@ import ( "go.mongodb.org/mongo-driver/bson" ) -// StringSliceFromRawElement decodes the provided BSON element into a []string. This internally calls -// StringSliceFromRawValue on the element's value. The error conditions outlined in that function's documentation -// apply for this function as well. -func StringSliceFromRawElement(element bson.RawElement) ([]string, error) { - return StringSliceFromRawValue(element.Key(), element.Value()) -} - // StringSliceFromRawValue decodes the provided BSON value into a []string. This function returns an error if the value // is not an array or any of the elements in the array are not strings. The name parameter is used to add context to // error messages. @@ -43,3 +36,27 @@ func StringSliceFromRawValue(name string, val bson.RawValue) ([]string, error) { } return strs, nil } + +// RawToDocuments converts a bson.Raw that is internally an array of documents to []bson.Raw. +func RawToDocuments(doc bson.Raw) []bson.Raw { + values, err := doc.Values() + if err != nil { + panic(fmt.Sprintf("error converting BSON document to values: %v", err)) + } + + out := make([]bson.Raw, len(values)) + for i := range values { + out[i] = values[i].Document() + } + + return out +} + +// RawToInterfaces takes one or many bson.Raw documents and returns them as a []interface{}. +func RawToInterfaces(docs ...bson.Raw) []interface{} { + out := make([]interface{}, len(docs)) + for i := range docs { + out[i] = docs[i] + } + return out +} diff --git a/vendor/go.mongodb.org/mongo-driver/internal/cancellation_listener.go b/vendor/go.mongodb.org/mongo-driver/internal/cancellation_listener.go deleted file mode 100644 index a7fa163bb..000000000 --- a/vendor/go.mongodb.org/mongo-driver/internal/cancellation_listener.go +++ /dev/null @@ -1,47 +0,0 @@ -// Copyright (C) MongoDB, Inc. 2017-present. -// -// Licensed under the Apache License, Version 2.0 (the "License"); you may -// not use this file except in compliance with the License. You may obtain -// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 - -package internal - -import "context" - -// CancellationListener listens for context cancellation in a loop until the context expires or the listener is aborted. -type CancellationListener struct { - aborted bool - done chan struct{} -} - -// NewCancellationListener constructs a CancellationListener. -func NewCancellationListener() *CancellationListener { - return &CancellationListener{ - done: make(chan struct{}), - } -} - -// Listen blocks until the provided context is cancelled or listening is aborted via the StopListening function. If this -// detects that the context has been cancelled (i.e. ctx.Err() == context.Canceled), the provided callback is called to -// abort in-progress work. Even if the context expires, this function will block until StopListening is called. -func (c *CancellationListener) Listen(ctx context.Context, abortFn func()) { - c.aborted = false - - select { - case <-ctx.Done(): - if ctx.Err() == context.Canceled { - c.aborted = true - abortFn() - } - - <-c.done - case <-c.done: - } -} - -// StopListening stops the in-progress Listen call. This blocks if there is no in-progress Listen call. This function -// will return true if the provided abort callback was called when listening for cancellation on the previous context. -func (c *CancellationListener) StopListening() bool { - c.done <- struct{}{} - return c.aborted -} diff --git a/vendor/go.mongodb.org/mongo-driver/internal/codecutil/encoding.go b/vendor/go.mongodb.org/mongo-driver/internal/codecutil/encoding.go new file mode 100644 index 000000000..2aaf8f271 --- /dev/null +++ b/vendor/go.mongodb.org/mongo-driver/internal/codecutil/encoding.go @@ -0,0 +1,65 @@ +// Copyright (C) MongoDB, Inc. 2023-present. +// +// Licensed under the Apache License, Version 2.0 (the "License"); you may +// not use this file except in compliance with the License. You may obtain +// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 + +package codecutil + +import ( + "bytes" + "errors" + "fmt" + "io" + "reflect" + + "go.mongodb.org/mongo-driver/bson" + "go.mongodb.org/mongo-driver/x/bsonx/bsoncore" +) + +var ErrNilValue = errors.New("value is nil") + +// MarshalError is returned when attempting to transform a value into a document +// results in an error. +type MarshalError struct { + Value interface{} + Err error +} + +// Error implements the error interface. +func (e MarshalError) Error() string { + return fmt.Sprintf("cannot transform type %s to a BSON Document: %v", + reflect.TypeOf(e.Value), e.Err) +} + +// EncoderFn is used to functionally construct an encoder for marshaling values. +type EncoderFn func(io.Writer) (*bson.Encoder, error) + +// MarshalValue will attempt to encode the value with the encoder returned by +// the encoder function. +func MarshalValue(val interface{}, encFn EncoderFn) (bsoncore.Value, error) { + // If the val is already a bsoncore.Value, then do nothing. + if bval, ok := val.(bsoncore.Value); ok { + return bval, nil + } + + if val == nil { + return bsoncore.Value{}, ErrNilValue + } + + buf := new(bytes.Buffer) + + enc, err := encFn(buf) + if err != nil { + return bsoncore.Value{}, err + } + + // Encode the value in a single-element document with an empty key. Use + // bsoncore to extract the first element and return the BSON value. + err = enc.Encode(bson.D{{Key: "", Value: val}}) + if err != nil { + return bsoncore.Value{}, MarshalError{Value: val, Err: err} + } + + return bsoncore.Document(buf.Bytes()).Index(0).Value(), nil +} diff --git a/vendor/go.mongodb.org/mongo-driver/internal/credproviders/imds_provider.go b/vendor/go.mongodb.org/mongo-driver/internal/credproviders/imds_provider.go index 4d2a95b2e..96dad1a82 100644 --- a/vendor/go.mongodb.org/mongo-driver/internal/credproviders/imds_provider.go +++ b/vendor/go.mongodb.org/mongo-driver/internal/credproviders/imds_provider.go @@ -15,7 +15,6 @@ import ( "net/url" "time" - "go.mongodb.org/mongo-driver/internal" "go.mongodb.org/mongo-driver/internal/aws/credentials" ) @@ -47,7 +46,7 @@ func (a *AzureProvider) RetrieveWithContext(ctx context.Context) (credentials.Va v := credentials.Value{ProviderName: AzureProviderName} req, err := http.NewRequest(http.MethodGet, azureURI, nil) if err != nil { - return v, internal.WrapErrorf(err, "unable to retrieve Azure credentials") + return v, fmt.Errorf("unable to retrieve Azure credentials: %w", err) } q := make(url.Values) q.Set("api-version", "2018-02-01") @@ -58,15 +57,15 @@ func (a *AzureProvider) RetrieveWithContext(ctx context.Context) (credentials.Va resp, err := a.httpClient.Do(req.WithContext(ctx)) if err != nil { - return v, internal.WrapErrorf(err, "unable to retrieve Azure credentials") + return v, fmt.Errorf("unable to retrieve Azure credentials: %w", err) } defer resp.Body.Close() body, err := ioutil.ReadAll(resp.Body) if err != nil { - return v, internal.WrapErrorf(err, "unable to retrieve Azure credentials: error reading response body") + return v, fmt.Errorf("unable to retrieve Azure credentials: error reading response body: %w", err) } if resp.StatusCode != http.StatusOK { - return v, internal.WrapErrorf(err, "unable to retrieve Azure credentials: expected StatusCode 200, got StatusCode: %v. Response body: %s", resp.StatusCode, body) + return v, fmt.Errorf("unable to retrieve Azure credentials: expected StatusCode 200, got StatusCode: %v. Response body: %s", resp.StatusCode, body) } var tokenResponse struct { AccessToken string `json:"access_token"` @@ -75,7 +74,7 @@ func (a *AzureProvider) RetrieveWithContext(ctx context.Context) (credentials.Va // Attempt to read body as JSON err = json.Unmarshal(body, &tokenResponse) if err != nil { - return v, internal.WrapErrorf(err, "unable to retrieve Azure credentials: error reading body JSON. Response body: %s", body) + return v, fmt.Errorf("unable to retrieve Azure credentials: error reading body JSON: %w (response body: %s)", err, body) } if tokenResponse.AccessToken == "" { return v, fmt.Errorf("unable to retrieve Azure credentials: got unexpected empty accessToken from Azure Metadata Server. Response body: %s", body) diff --git a/vendor/go.mongodb.org/mongo-driver/internal/csfle_util.go b/vendor/go.mongodb.org/mongo-driver/internal/csfle/csfle.go similarity index 98% rename from vendor/go.mongodb.org/mongo-driver/internal/csfle_util.go rename to vendor/go.mongodb.org/mongo-driver/internal/csfle/csfle.go index 635d8e353..71e71b468 100644 --- a/vendor/go.mongodb.org/mongo-driver/internal/csfle_util.go +++ b/vendor/go.mongodb.org/mongo-driver/internal/csfle/csfle.go @@ -4,7 +4,7 @@ // not use this file except in compliance with the License. You may obtain // a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 -package internal +package csfle import ( "fmt" diff --git a/vendor/go.mongodb.org/mongo-driver/internal/csot_util.go b/vendor/go.mongodb.org/mongo-driver/internal/csot/csot.go similarity index 99% rename from vendor/go.mongodb.org/mongo-driver/internal/csot_util.go rename to vendor/go.mongodb.org/mongo-driver/internal/csot/csot.go index 1e63257b3..678252c51 100644 --- a/vendor/go.mongodb.org/mongo-driver/internal/csot_util.go +++ b/vendor/go.mongodb.org/mongo-driver/internal/csot/csot.go @@ -4,7 +4,7 @@ // not use this file except in compliance with the License. You may obtain // a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 -package internal +package csot import ( "context" diff --git a/vendor/go.mongodb.org/mongo-driver/internal/driverutil/hello.go b/vendor/go.mongodb.org/mongo-driver/internal/driverutil/hello.go new file mode 100644 index 000000000..18a70f0ca --- /dev/null +++ b/vendor/go.mongodb.org/mongo-driver/internal/driverutil/hello.go @@ -0,0 +1,128 @@ +// Copyright (C) MongoDB, Inc. 2023-present. +// +// Licensed under the Apache License, Version 2.0 (the "License"); you may +// not use this file except in compliance with the License. You may obtain +// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 + +package driverutil + +import ( + "os" + "strings" +) + +const AwsLambdaPrefix = "AWS_Lambda_" + +const ( + // FaaS environment variable names + + // EnvVarAWSExecutionEnv is the AWS Execution environment variable. + EnvVarAWSExecutionEnv = "AWS_EXECUTION_ENV" + // EnvVarAWSLambdaRuntimeAPI is the AWS Lambda runtime API variable. + EnvVarAWSLambdaRuntimeAPI = "AWS_LAMBDA_RUNTIME_API" + // EnvVarFunctionsWorkerRuntime is the functions worker runtime variable. + EnvVarFunctionsWorkerRuntime = "FUNCTIONS_WORKER_RUNTIME" + // EnvVarKService is the K Service variable. + EnvVarKService = "K_SERVICE" + // EnvVarFunctionName is the function name variable. + EnvVarFunctionName = "FUNCTION_NAME" + // EnvVarVercel is the Vercel variable. + EnvVarVercel = "VERCEL" + // EnvVarK8s is the K8s variable. + EnvVarK8s = "KUBERNETES_SERVICE_HOST" +) + +const ( + // FaaS environment variable names + + // EnvVarAWSRegion is the AWS region variable. + EnvVarAWSRegion = "AWS_REGION" + // EnvVarAWSLambdaFunctionMemorySize is the AWS Lambda function memory size variable. + EnvVarAWSLambdaFunctionMemorySize = "AWS_LAMBDA_FUNCTION_MEMORY_SIZE" + // EnvVarFunctionMemoryMB is the function memory in megabytes variable. + EnvVarFunctionMemoryMB = "FUNCTION_MEMORY_MB" + // EnvVarFunctionTimeoutSec is the function timeout in seconds variable. + EnvVarFunctionTimeoutSec = "FUNCTION_TIMEOUT_SEC" + // EnvVarFunctionRegion is the function region variable. + EnvVarFunctionRegion = "FUNCTION_REGION" + // EnvVarVercelRegion is the Vercel region variable. + EnvVarVercelRegion = "VERCEL_REGION" +) + +const ( + // FaaS environment names used by the client + + // EnvNameAWSLambda is the AWS Lambda environment name. + EnvNameAWSLambda = "aws.lambda" + // EnvNameAzureFunc is the Azure Function environment name. + EnvNameAzureFunc = "azure.func" + // EnvNameGCPFunc is the Google Cloud Function environment name. + EnvNameGCPFunc = "gcp.func" + // EnvNameVercel is the Vercel environment name. + EnvNameVercel = "vercel" +) + +// GetFaasEnvName parses the FaaS environment variable name and returns the +// corresponding name used by the client. If none of the variables or variables +// for multiple names are populated the client.env value MUST be entirely +// omitted. When variables for multiple "client.env.name" values are present, +// "vercel" takes precedence over "aws.lambda"; any other combination MUST cause +// "client.env" to be entirely omitted. +func GetFaasEnvName() string { + envVars := []string{ + EnvVarAWSExecutionEnv, + EnvVarAWSLambdaRuntimeAPI, + EnvVarFunctionsWorkerRuntime, + EnvVarKService, + EnvVarFunctionName, + EnvVarVercel, + } + + // If none of the variables are populated the client.env value MUST be + // entirely omitted. + names := make(map[string]struct{}) + + for _, envVar := range envVars { + val := os.Getenv(envVar) + if val == "" { + continue + } + + var name string + + switch envVar { + case EnvVarAWSExecutionEnv: + if !strings.HasPrefix(val, AwsLambdaPrefix) { + continue + } + + name = EnvNameAWSLambda + case EnvVarAWSLambdaRuntimeAPI: + name = EnvNameAWSLambda + case EnvVarFunctionsWorkerRuntime: + name = EnvNameAzureFunc + case EnvVarKService, EnvVarFunctionName: + name = EnvNameGCPFunc + case EnvVarVercel: + // "vercel" takes precedence over "aws.lambda". + delete(names, EnvNameAWSLambda) + + name = EnvNameVercel + } + + names[name] = struct{}{} + if len(names) > 1 { + // If multiple names are populated the client.env value + // MUST be entirely omitted. + names = nil + + break + } + } + + for name := range names { + return name + } + + return "" +} diff --git a/vendor/go.mongodb.org/mongo-driver/internal/driverutil/operation.go b/vendor/go.mongodb.org/mongo-driver/internal/driverutil/operation.go new file mode 100644 index 000000000..32704312f --- /dev/null +++ b/vendor/go.mongodb.org/mongo-driver/internal/driverutil/operation.go @@ -0,0 +1,31 @@ +// Copyright (C) MongoDB, Inc. 2023-present. +// +// Licensed under the Apache License, Version 2.0 (the "License"); you may +// not use this file except in compliance with the License. You may obtain +// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 + +package driverutil + +// Operation Names should be sourced from the command reference documentation: +// https://www.mongodb.com/docs/manual/reference/command/ +const ( + AbortTransactionOp = "abortTransaction" // AbortTransactionOp is the name for aborting a transaction + AggregateOp = "aggregate" // AggregateOp is the name for aggregating + CommitTransactionOp = "commitTransaction" // CommitTransactionOp is the name for committing a transaction + CountOp = "count" // CountOp is the name for counting + CreateOp = "create" // CreateOp is the name for creating + CreateIndexesOp = "createIndexes" // CreateIndexesOp is the name for creating indexes + DeleteOp = "delete" // DeleteOp is the name for deleting + DistinctOp = "distinct" // DistinctOp is the name for distinct + DropOp = "drop" // DropOp is the name for dropping + DropDatabaseOp = "dropDatabase" // DropDatabaseOp is the name for dropping a database + DropIndexesOp = "dropIndexes" // DropIndexesOp is the name for dropping indexes + EndSessionsOp = "endSessions" // EndSessionsOp is the name for ending sessions + FindAndModifyOp = "findAndModify" // FindAndModifyOp is the name for finding and modifying + FindOp = "find" // FindOp is the name for finding + InsertOp = "insert" // InsertOp is the name for inserting + ListCollectionsOp = "listCollections" // ListCollectionsOp is the name for listing collections + 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 +) diff --git a/vendor/go.mongodb.org/mongo-driver/internal/error.go b/vendor/go.mongodb.org/mongo-driver/internal/error.go deleted file mode 100644 index 348bcdfb1..000000000 --- a/vendor/go.mongodb.org/mongo-driver/internal/error.go +++ /dev/null @@ -1,123 +0,0 @@ -// Copyright (C) MongoDB, Inc. 2017-present. -// -// Licensed under the Apache License, Version 2.0 (the "License"); you may -// not use this file except in compliance with the License. You may obtain -// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 - -package internal - -import ( - "fmt" -) - -// WrappedError represents an error that contains another error. -type WrappedError interface { - // Message gets the basic message of the error. - Message() string - // Inner gets the inner error if one exists. - Inner() error -} - -// RolledUpErrorMessage gets a flattened error message. -func RolledUpErrorMessage(err error) string { - if wrappedErr, ok := err.(WrappedError); ok { - inner := wrappedErr.Inner() - if inner != nil { - return fmt.Sprintf("%s: %s", wrappedErr.Message(), RolledUpErrorMessage(inner)) - } - - return wrappedErr.Message() - } - - return err.Error() -} - -// UnwrapError attempts to unwrap the error down to its root cause. -func UnwrapError(err error) error { - - switch tErr := err.(type) { - case WrappedError: - return UnwrapError(tErr.Inner()) - case *multiError: - return UnwrapError(tErr.errors[0]) - } - - return err -} - -// WrapError wraps an error with a message. -func WrapError(inner error, message string) error { - return &wrappedError{message, inner} -} - -// WrapErrorf wraps an error with a message. -func WrapErrorf(inner error, format string, args ...interface{}) error { - return &wrappedError{fmt.Sprintf(format, args...), inner} -} - -// MultiError combines multiple errors into a single error. If there are no errors, -// nil is returned. If there is 1 error, it is returned. Otherwise, they are combined. -func MultiError(errors ...error) error { - - // remove nils from the error list - var nonNils []error - for _, e := range errors { - if e != nil { - nonNils = append(nonNils, e) - } - } - - switch len(nonNils) { - case 0: - return nil - case 1: - return nonNils[0] - default: - return &multiError{ - message: "multiple errors encountered", - errors: nonNils, - } - } -} - -type multiError struct { - message string - errors []error -} - -func (e *multiError) Message() string { - return e.message -} - -func (e *multiError) Error() string { - result := e.message - for _, e := range e.errors { - result += fmt.Sprintf("\n %s", e) - } - return result -} - -func (e *multiError) Errors() []error { - return e.errors -} - -type wrappedError struct { - message string - inner error -} - -func (e *wrappedError) Message() string { - return e.message -} - -func (e *wrappedError) Error() string { - return RolledUpErrorMessage(e) -} - -func (e *wrappedError) Inner() error { - return e.inner -} - -func (e *wrappedError) Unwrap() error { - return e.inner -} diff --git a/vendor/go.mongodb.org/mongo-driver/internal/const.go b/vendor/go.mongodb.org/mongo-driver/internal/handshake/handshake.go similarity index 64% rename from vendor/go.mongodb.org/mongo-driver/internal/const.go rename to vendor/go.mongodb.org/mongo-driver/internal/handshake/handshake.go index a7ef69d13..c9537d3ef 100644 --- a/vendor/go.mongodb.org/mongo-driver/internal/const.go +++ b/vendor/go.mongodb.org/mongo-driver/internal/handshake/handshake.go @@ -4,16 +4,10 @@ // not use this file except in compliance with the License. You may obtain // a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 -package internal // import "go.mongodb.org/mongo-driver/internal" - -// Version is the current version of the driver. -var Version = "local build" +package handshake // LegacyHello is the legacy version of the hello command. var LegacyHello = "isMaster" // LegacyHelloLowercase is the lowercase, legacy version of the hello command. var LegacyHelloLowercase = "ismaster" - -// LegacyNotPrimary is the legacy version of the "not primary" server error message. -var LegacyNotPrimary = "not master" diff --git a/vendor/go.mongodb.org/mongo-driver/internal/http.go b/vendor/go.mongodb.org/mongo-driver/internal/httputil/httputil.go similarity index 74% rename from vendor/go.mongodb.org/mongo-driver/internal/http.go rename to vendor/go.mongodb.org/mongo-driver/internal/httputil/httputil.go index 1391ac4ca..db0dd5f12 100644 --- a/vendor/go.mongodb.org/mongo-driver/internal/http.go +++ b/vendor/go.mongodb.org/mongo-driver/internal/httputil/httputil.go @@ -4,7 +4,7 @@ // not use this file except in compliance with the License. You may obtain // a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 -package internal // import "go.mongodb.org/mongo-driver/internal" +package httputil import ( "net/http" @@ -16,10 +16,10 @@ var DefaultHTTPClient = &http.Client{ } // CloseIdleHTTPConnections closes any connections which were previously -// connected from previous requests but are now sitting idle in -// a "keep-alive" state. It does not interrupt any connections currently -// in use. -// Borrowed from go standard library. +// connected from previous requests but are now sitting idle in a "keep-alive" +// state. It does not interrupt any connections currently in use. +// +// Borrowed from the Go standard library. func CloseIdleHTTPConnections(client *http.Client) { type closeIdler interface { CloseIdleConnections() diff --git a/vendor/go.mongodb.org/mongo-driver/internal/logger/component.go b/vendor/go.mongodb.org/mongo-driver/internal/logger/component.go index da9c43de4..0a3d55320 100644 --- a/vendor/go.mongodb.org/mongo-driver/internal/logger/component.go +++ b/vendor/go.mongodb.org/mongo-driver/internal/logger/component.go @@ -14,48 +14,70 @@ import ( ) const ( - CommandFailed = "Command failed" - CommandStarted = "Command started" - CommandSucceeded = "Command succeeded" - ConnectionPoolCreated = "Connection pool created" - ConnectionPoolReady = "Connection pool ready" - ConnectionPoolCleared = "Connection pool cleared" - ConnectionPoolClosed = "Connection pool closed" - ConnectionCreated = "Connection created" - ConnectionReady = "Connection ready" - ConnectionClosed = "Connection closed" - ConnectionCheckoutStarted = "Connection checkout started" - ConnectionCheckoutFailed = "Connection checkout failed" - ConnectionCheckedOut = "Connection checked out" - ConnectionCheckedIn = "Connection checked in" + CommandFailed = "Command failed" + CommandStarted = "Command started" + CommandSucceeded = "Command succeeded" + ConnectionPoolCreated = "Connection pool created" + ConnectionPoolReady = "Connection pool ready" + ConnectionPoolCleared = "Connection pool cleared" + ConnectionPoolClosed = "Connection pool closed" + ConnectionCreated = "Connection created" + ConnectionReady = "Connection ready" + ConnectionClosed = "Connection closed" + ConnectionCheckoutStarted = "Connection checkout started" + ConnectionCheckoutFailed = "Connection checkout failed" + ConnectionCheckedOut = "Connection checked out" + ConnectionCheckedIn = "Connection checked in" + ServerSelectionFailed = "Server selection failed" + ServerSelectionStarted = "Server selection started" + ServerSelectionSucceeded = "Server selection succeeded" + ServerSelectionWaiting = "Waiting for suitable server to become available" + TopologyClosed = "Stopped topology monitoring" + TopologyDescriptionChanged = "Topology description changed" + TopologyOpening = "Starting topology monitoring" + TopologyServerClosed = "Stopped server monitoring" + TopologyServerHeartbeatFailed = "Server heartbeat failed" + TopologyServerHeartbeatStarted = "Server heartbeat started" + TopologyServerHeartbeatSucceeded = "Server heartbeat succeeded" + TopologyServerOpening = "Starting server monitoring" ) const ( - KeyCommand = "command" - KeyCommandName = "commandName" - KeyDatabaseName = "databaseName" - KeyDriverConnectionID = "driverConnectionId" - KeyDurationMS = "durationMS" - KeyError = "error" - KeyFailure = "failure" - KeyMaxConnecting = "maxConnecting" - KeyMaxIdleTimeMS = "maxIdleTimeMS" - KeyMaxPoolSize = "maxPoolSize" - KeyMessage = "message" - KeyMinPoolSize = "minPoolSize" - KeyOperationID = "operationId" - KeyReason = "reason" - KeyReply = "reply" - KeyRequestID = "requestId" - KeyServerConnectionID = "serverConnectionId" - KeyServerHost = "serverHost" - KeyServerPort = "serverPort" - KeyServiceID = "serviceId" - KeyTimestamp = "timestamp" + KeyAwaited = "awaited" + KeyCommand = "command" + KeyCommandName = "commandName" + KeyDatabaseName = "databaseName" + KeyDriverConnectionID = "driverConnectionId" + KeyDurationMS = "durationMS" + KeyError = "error" + KeyFailure = "failure" + KeyMaxConnecting = "maxConnecting" + KeyMaxIdleTimeMS = "maxIdleTimeMS" + KeyMaxPoolSize = "maxPoolSize" + KeyMessage = "message" + KeyMinPoolSize = "minPoolSize" + KeyNewDescription = "newDescription" + KeyOperation = "operation" + KeyOperationID = "operationId" + KeyPreviousDescription = "previousDescription" + KeyRemainingTimeMS = "remainingTimeMS" + KeyReason = "reason" + KeyReply = "reply" + KeyRequestID = "requestId" + KeySelector = "selector" + KeyServerConnectionID = "serverConnectionId" + KeyServerHost = "serverHost" + KeyServerPort = "serverPort" + KeyServiceID = "serviceId" + KeyTimestamp = "timestamp" + KeyTopologyDescription = "topologyDescription" + KeyTopologyID = "topologyId" ) +// KeyValues is a list of key-value pairs. type KeyValues []interface{} +// Add adds a key-value pair to an instance of a KeyValues list. func (kvs *KeyValues) Add(key string, value interface{}) { *kvs = append(*kvs, key, value) } @@ -125,6 +147,7 @@ type Command struct { // TODO(GODRIVER-2824): change the DriverConnectionID type to int64. DriverConnectionID uint64 // Driver's ID for the connection Name string // Command name + DatabaseName string // Database name Message string // Message associated with the command OperationID int32 // Driver-generated operation ID RequestID int64 // Driver-generated request ID @@ -137,10 +160,11 @@ type Command struct { // SerializeCommand takes a command and a variable number of key-value pairs and // returns a slice of interface{} that can be passed to the logger for // structured logging. -func SerializeCommand(cmd Command, extraKeysAndValues ...interface{}) []interface{} { +func SerializeCommand(cmd Command, extraKeysAndValues ...interface{}) KeyValues { // Initialize the boilerplate keys and values. keysAndValues := KeyValues{ KeyCommandName, cmd.Name, + KeyDatabaseName, cmd.DatabaseName, KeyDriverConnectionID, cmd.DriverConnectionID, KeyMessage, cmd.Message, KeyOperationID, cmd.OperationID, @@ -153,7 +177,7 @@ func SerializeCommand(cmd Command, extraKeysAndValues ...interface{}) []interfac keysAndValues.Add(extraKeysAndValues[i].(string), extraKeysAndValues[i+1]) } - port, err := strconv.ParseInt(cmd.ServerPort, 0, 32) + port, err := strconv.ParseInt(cmd.ServerPort, 10, 32) if err == nil { keysAndValues.Add(KeyServerPort, port) } @@ -178,9 +202,9 @@ type Connection struct { ServerPort string // Port for the server } -// SerializeConnection serializes a ConnectionMessage into a slice of keys -// and values that can be passed to a logger. -func SerializeConnection(conn Connection, extraKeysAndValues ...interface{}) []interface{} { +// SerializeConnection serializes a Connection message into a slice of keys and +// values that can be passed to a logger. +func SerializeConnection(conn Connection, extraKeysAndValues ...interface{}) KeyValues { // Initialize the boilerplate keys and values. keysAndValues := KeyValues{ KeyMessage, conn.Message, @@ -192,10 +216,99 @@ func SerializeConnection(conn Connection, extraKeysAndValues ...interface{}) []i keysAndValues.Add(extraKeysAndValues[i].(string), extraKeysAndValues[i+1]) } - port, err := strconv.ParseInt(conn.ServerPort, 0, 32) + port, err := strconv.ParseInt(conn.ServerPort, 10, 32) + if err == nil { + keysAndValues.Add(KeyServerPort, port) + } + + return keysAndValues +} + +// Server contains data that all server messages MAY contain. +type Server struct { + DriverConnectionID uint64 // Driver's ID for the connection + TopologyID primitive.ObjectID // Driver's unique ID for this topology + Message string // Message associated with the topology + ServerConnectionID *int64 // Server's ID for the connection + ServerHost string // Hostname or IP address for the server + ServerPort string // Port for the server +} + +// SerializeServer serializes a Server message into a slice of keys and +// values that can be passed to a logger. +func SerializeServer(srv Server, extraKV ...interface{}) KeyValues { + // Initialize the boilerplate keys and values. + keysAndValues := KeyValues{ + KeyDriverConnectionID, srv.DriverConnectionID, + KeyMessage, srv.Message, + KeyServerHost, srv.ServerHost, + KeyTopologyID, srv.TopologyID.Hex(), + } + + if connID := srv.ServerConnectionID; connID != nil { + keysAndValues.Add(KeyServerConnectionID, *connID) + } + + port, err := strconv.ParseInt(srv.ServerPort, 10, 32) if err == nil { keysAndValues.Add(KeyServerPort, port) } + // Add the optional keys and values. + for i := 0; i < len(extraKV); i += 2 { + keysAndValues.Add(extraKV[i].(string), extraKV[i+1]) + } + + return keysAndValues +} + +// ServerSelection contains data that all server selection messages MUST +// contain. +type ServerSelection struct { + Selector string + OperationID *int32 + Operation string + TopologyDescription string +} + +// SerializeServerSelection serializes a Topology message into a slice of keys +// and values that can be passed to a logger. +func SerializeServerSelection(srvSelection ServerSelection, extraKV ...interface{}) KeyValues { + keysAndValues := KeyValues{ + KeySelector, srvSelection.Selector, + KeyOperation, srvSelection.Operation, + KeyTopologyDescription, srvSelection.TopologyDescription, + } + + if srvSelection.OperationID != nil { + keysAndValues.Add(KeyOperationID, *srvSelection.OperationID) + } + + // Add the optional keys and values. + for i := 0; i < len(extraKV); i += 2 { + keysAndValues.Add(extraKV[i].(string), extraKV[i+1]) + } + + return keysAndValues +} + +// Topology contains data that all topology messages MAY contain. +type Topology struct { + ID primitive.ObjectID // Driver's unique ID for this topology + Message string // Message associated with the topology +} + +// SerializeTopology serializes a Topology message into a slice of keys and +// values that can be passed to a logger. +func SerializeTopology(topo Topology, extraKV ...interface{}) KeyValues { + keysAndValues := KeyValues{ + KeyTopologyID, topo.ID.Hex(), + } + + // Add the optional keys and values. + for i := 0; i < len(extraKV); i += 2 { + keysAndValues.Add(extraKV[i].(string), extraKV[i+1]) + } + return keysAndValues } diff --git a/vendor/go.mongodb.org/mongo-driver/internal/logger/context.go b/vendor/go.mongodb.org/mongo-driver/internal/logger/context.go new file mode 100644 index 000000000..785f141c4 --- /dev/null +++ b/vendor/go.mongodb.org/mongo-driver/internal/logger/context.go @@ -0,0 +1,48 @@ +// Copyright (C) MongoDB, Inc. 2023-present. +// +// Licensed under the Apache License, Version 2.0 (the "License"); you may +// not use this file except in compliance with the License. You may obtain +// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 + +package logger + +import "context" + +// contextKey is a custom type used to prevent key collisions when using the +// context package. +type contextKey string + +const ( + contextKeyOperation contextKey = "operation" + contextKeyOperationID contextKey = "operationID" +) + +// WithOperationName adds the operation name to the context. +func WithOperationName(ctx context.Context, operation string) context.Context { + return context.WithValue(ctx, contextKeyOperation, operation) +} + +// WithOperationID adds the operation ID to the context. +func WithOperationID(ctx context.Context, operationID int32) context.Context { + return context.WithValue(ctx, contextKeyOperationID, operationID) +} + +// OperationName returns the operation name from the context. +func OperationName(ctx context.Context) (string, bool) { + operationName := ctx.Value(contextKeyOperation) + if operationName == nil { + return "", false + } + + return operationName.(string), true +} + +// OperationID returns the operation ID from the context. +func OperationID(ctx context.Context) (int32, bool) { + operationID := ctx.Value(contextKeyOperationID) + if operationID == nil { + return 0, false + } + + return operationID.(int32), true +} diff --git a/vendor/go.mongodb.org/mongo-driver/internal/logger/logger.go b/vendor/go.mongodb.org/mongo-driver/internal/logger/logger.go index 07dcffe66..03d42814f 100644 --- a/vendor/go.mongodb.org/mongo-driver/internal/logger/logger.go +++ b/vendor/go.mongodb.org/mongo-driver/internal/logger/logger.go @@ -4,6 +4,8 @@ // not use this file except in compliance with the License. You may obtain // a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 +// Package logger provides the internal logging solution for the MongoDB Go +// Driver. package logger import ( @@ -17,7 +19,7 @@ import ( // logged for a stringified BSON document. const DefaultMaxDocumentLength = 1000 -// TruncationSuffix are trailling ellipsis "..." appended to a message to +// TruncationSuffix are trailing ellipsis "..." appended to a message to // indicate to the user that truncation occurred. This constant does not count // toward the max document length. const TruncationSuffix = "..." @@ -99,6 +101,13 @@ func (logger *Logger) LevelComponentEnabled(level Level, component Component) bo // Print will synchronously print the given message to the configured LogSink. // If the LogSink is nil, then this method will do nothing. Future work could be done to make // this method asynchronous, see buffer management in libraries such as log4j. +// +// It's worth noting that many structured logs defined by DBX-wide +// specifications include a "message" field, which is often shared with the +// message arguments passed to this print function. The "Info" method used by +// this function is implemented based on the go-logr/logr LogSink interface, +// which is why "Print" has a message parameter. Any duplication in code is +// intentional to adhere to the logr pattern. func (logger *Logger) Print(level Level, component Component, msg string, keysAndValues ...interface{}) { // If the level is not enabled for the component, then // skip the message. diff --git a/vendor/go.mongodb.org/mongo-driver/internal/ptrutil/int64.go b/vendor/go.mongodb.org/mongo-driver/internal/ptrutil/int64.go new file mode 100644 index 000000000..1c3ab57ef --- /dev/null +++ b/vendor/go.mongodb.org/mongo-driver/internal/ptrutil/int64.go @@ -0,0 +1,39 @@ +// Copyright (C) MongoDB, Inc. 2023-present. +// +// Licensed under the Apache License, Version 2.0 (the "License"); you may +// not use this file except in compliance with the License. You may obtain +// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 + +package ptrutil + +// CompareInt64 is a piecewise function with the following return conditions: +// +// (1) 2, ptr1 != nil AND ptr2 == nil +// (2) 1, *ptr1 > *ptr2 +// (3) 0, ptr1 == ptr2 or *ptr1 == *ptr2 +// (4) -1, *ptr1 < *ptr2 +// (5) -2, ptr1 == nil AND ptr2 != nil +func CompareInt64(ptr1, ptr2 *int64) int { + if ptr1 == ptr2 { + // This will catch the double nil or same-pointer cases. + return 0 + } + + if ptr1 == nil && ptr2 != nil { + return -2 + } + + if ptr1 != nil && ptr2 == nil { + return 2 + } + + if *ptr1 > *ptr2 { + return 1 + } + + if *ptr1 < *ptr2 { + return -1 + } + + return 0 +} diff --git a/vendor/go.mongodb.org/mongo-driver/internal/randutil/rand/bits.go b/vendor/go.mongodb.org/mongo-driver/internal/rand/bits.go similarity index 100% rename from vendor/go.mongodb.org/mongo-driver/internal/randutil/rand/bits.go rename to vendor/go.mongodb.org/mongo-driver/internal/rand/bits.go diff --git a/vendor/go.mongodb.org/mongo-driver/internal/randutil/rand/exp.go b/vendor/go.mongodb.org/mongo-driver/internal/rand/exp.go similarity index 100% rename from vendor/go.mongodb.org/mongo-driver/internal/randutil/rand/exp.go rename to vendor/go.mongodb.org/mongo-driver/internal/rand/exp.go diff --git a/vendor/go.mongodb.org/mongo-driver/internal/randutil/rand/normal.go b/vendor/go.mongodb.org/mongo-driver/internal/rand/normal.go similarity index 100% rename from vendor/go.mongodb.org/mongo-driver/internal/randutil/rand/normal.go rename to vendor/go.mongodb.org/mongo-driver/internal/rand/normal.go diff --git a/vendor/go.mongodb.org/mongo-driver/internal/randutil/rand/rand.go b/vendor/go.mongodb.org/mongo-driver/internal/rand/rand.go similarity index 99% rename from vendor/go.mongodb.org/mongo-driver/internal/randutil/rand/rand.go rename to vendor/go.mongodb.org/mongo-driver/internal/rand/rand.go index ffd0509bd..4c3d3e6ee 100644 --- a/vendor/go.mongodb.org/mongo-driver/internal/randutil/rand/rand.go +++ b/vendor/go.mongodb.org/mongo-driver/internal/rand/rand.go @@ -357,7 +357,7 @@ func (s *LockedSource) Seed(seed uint64) { s.lk.Unlock() } -// seedPos implements Seed for a LockedSource without a race condiiton. +// seedPos implements Seed for a LockedSource without a race condition. func (s *LockedSource) seedPos(seed uint64, readPos *int8) { s.lk.Lock() s.src.Seed(seed) diff --git a/vendor/go.mongodb.org/mongo-driver/internal/randutil/rand/rng.go b/vendor/go.mongodb.org/mongo-driver/internal/rand/rng.go similarity index 100% rename from vendor/go.mongodb.org/mongo-driver/internal/randutil/rand/rng.go rename to vendor/go.mongodb.org/mongo-driver/internal/rand/rng.go diff --git a/vendor/go.mongodb.org/mongo-driver/internal/randutil/randutil.go b/vendor/go.mongodb.org/mongo-driver/internal/randutil/randutil.go index 961607432..dd8c6d6f4 100644 --- a/vendor/go.mongodb.org/mongo-driver/internal/randutil/randutil.go +++ b/vendor/go.mongodb.org/mongo-driver/internal/randutil/randutil.go @@ -12,7 +12,7 @@ import ( "fmt" "io" - xrand "go.mongodb.org/mongo-driver/internal/randutil/rand" + xrand "go.mongodb.org/mongo-driver/internal/rand" ) // NewLockedRand returns a new "x/exp/rand" pseudo-random number generator seeded with a diff --git a/vendor/go.mongodb.org/mongo-driver/internal/uri_validation_errors.go b/vendor/go.mongodb.org/mongo-driver/internal/uri_validation_errors.go deleted file mode 100644 index 21e73002a..000000000 --- a/vendor/go.mongodb.org/mongo-driver/internal/uri_validation_errors.go +++ /dev/null @@ -1,22 +0,0 @@ -// Copyright (C) MongoDB, Inc. 2017-present. -// -// Licensed under the Apache License, Version 2.0 (the "License"); you may -// not use this file except in compliance with the License. You may obtain -// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 - -package internal - -import "errors" - -var ( - // ErrLoadBalancedWithMultipleHosts is returned when loadBalanced=true is specified in a URI with multiple hosts. - ErrLoadBalancedWithMultipleHosts = errors.New("loadBalanced cannot be set to true if multiple hosts are specified") - // ErrLoadBalancedWithReplicaSet is returned when loadBalanced=true is specified in a URI with the replicaSet option. - ErrLoadBalancedWithReplicaSet = errors.New("loadBalanced cannot be set to true if a replica set name is specified") - // ErrLoadBalancedWithDirectConnection is returned when loadBalanced=true is specified in a URI with the directConnection option. - ErrLoadBalancedWithDirectConnection = errors.New("loadBalanced cannot be set to true if the direct connection option is specified") - // ErrSRVMaxHostsWithReplicaSet is returned when srvMaxHosts > 0 is specified in a URI with the replicaSet option. - ErrSRVMaxHostsWithReplicaSet = errors.New("srvMaxHosts cannot be a positive value if a replica set name is specified") - // ErrSRVMaxHostsWithLoadBalanced is returned when srvMaxHosts > 0 is specified in a URI with loadBalanced=true. - ErrSRVMaxHostsWithLoadBalanced = errors.New("srvMaxHosts cannot be a positive value if loadBalanced is set to true") -) diff --git a/vendor/go.mongodb.org/mongo-driver/internal/background_context.go b/vendor/go.mongodb.org/mongo-driver/mongo/background_context.go similarity index 87% rename from vendor/go.mongodb.org/mongo-driver/internal/background_context.go rename to vendor/go.mongodb.org/mongo-driver/mongo/background_context.go index 6f190edb3..e4146e8b7 100644 --- a/vendor/go.mongodb.org/mongo-driver/internal/background_context.go +++ b/vendor/go.mongodb.org/mongo-driver/mongo/background_context.go @@ -4,7 +4,7 @@ // not use this file except in compliance with the License. You may obtain // a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 -package internal +package mongo import "context" @@ -16,9 +16,9 @@ type backgroundContext struct { childValuesCtx context.Context } -// NewBackgroundContext creates a new Context whose behavior matches that of context.Background(), but Value calls are +// newBackgroundContext creates a new Context whose behavior matches that of context.Background(), but Value calls are // forwarded to the provided ctx parameter. If ctx is nil, context.Background() is returned. -func NewBackgroundContext(ctx context.Context) context.Context { +func newBackgroundContext(ctx context.Context) context.Context { if ctx == nil { return context.Background() } diff --git a/vendor/go.mongodb.org/mongo-driver/mongo/batch_cursor.go b/vendor/go.mongodb.org/mongo-driver/mongo/batch_cursor.go index da2e27bc6..51d59d0ff 100644 --- a/vendor/go.mongodb.org/mongo-driver/mongo/batch_cursor.go +++ b/vendor/go.mongodb.org/mongo-driver/mongo/batch_cursor.go @@ -8,6 +8,7 @@ package mongo import ( "context" + "time" "go.mongodb.org/mongo-driver/x/bsonx/bsoncore" "go.mongodb.org/mongo-driver/x/mongo/driver" @@ -35,9 +36,21 @@ type batchCursor interface { // Close closes the cursor. Close(context.Context) error - // The SetBatchSize method is a modifier function used to adjust the - // batch size of the cursor that implements it. + // SetBatchSize is a modifier function used to adjust the batch size of + // the cursor that implements it. SetBatchSize(int32) + + // SetMaxTime will set the maximum amount of time the server will allow + // the operations to execute. The server will error if this field is set + // but the cursor is not configured with awaitData=true. + // + // The time.Duration value passed by this setter will be converted and + // rounded down to the nearest millisecond. + SetMaxTime(time.Duration) + + // SetComment will set a user-configurable comment that can be used to + // identify the operation in server logs. + SetComment(interface{}) } // changeStreamCursor is the interface implemented by batch cursors that also provide the functionality for retrieving diff --git a/vendor/go.mongodb.org/mongo-driver/mongo/bulk_write.go b/vendor/go.mongodb.org/mongo-driver/mongo/bulk_write.go index 58e64f1d9..42d286ea7 100644 --- a/vendor/go.mongodb.org/mongo-driver/mongo/bulk_write.go +++ b/vendor/go.mongodb.org/mongo-driver/mongo/bulk_write.go @@ -26,7 +26,7 @@ type bulkWriteBatch struct { indexes []int } -// bulkWrite perfoms a bulkwrite operation +// bulkWrite performs a bulkwrite operation type bulkWrite struct { comment interface{} ordered *bool diff --git a/vendor/go.mongodb.org/mongo-driver/mongo/change_stream.go b/vendor/go.mongodb.org/mongo-driver/mongo/change_stream.go index 166dfa79f..773cbb0e5 100644 --- a/vendor/go.mongodb.org/mongo-driver/mongo/change_stream.go +++ b/vendor/go.mongodb.org/mongo-driver/mongo/change_stream.go @@ -17,7 +17,7 @@ import ( "go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/bson/bsoncodec" "go.mongodb.org/mongo-driver/bson/primitive" - "go.mongodb.org/mongo-driver/internal" + "go.mongodb.org/mongo-driver/internal/csot" "go.mongodb.org/mongo-driver/mongo/description" "go.mongodb.org/mongo-driver/mongo/options" "go.mongodb.org/mongo-driver/mongo/readconcern" @@ -107,6 +107,10 @@ func newChangeStream(ctx context.Context, config changeStreamConfig, pipeline in ctx = context.Background() } + cursorOpts := config.client.createBaseCursorOptions() + + cursorOpts.MarshalValueEncoderFn = newEncoderFn(config.bsonOpts, config.registry) + cs := &ChangeStream{ client: config.client, bsonOpts: config.bsonOpts, @@ -117,7 +121,7 @@ func newChangeStream(ctx context.Context, config changeStreamConfig, pipeline in description.ReadPrefSelector(config.readPreference), description.LatencySelector(config.client.localThreshold), }), - cursorOptions: config.client.createBaseCursorOptions(), + cursorOptions: cursorOpts, } cs.sess = sessionFromContext(ctx) @@ -276,8 +280,8 @@ func (cs *ChangeStream) executeOperation(ctx context.Context, resuming bool) err // If no deadline is set on the passed-in context, cs.client.timeout is set, and context is not already // a Timeout context, honor cs.client.timeout in new Timeout context for change stream operation execution // and potential retry. - if _, deadlineSet := ctx.Deadline(); !deadlineSet && cs.client.timeout != nil && !internal.IsTimeoutContext(ctx) { - newCtx, cancelFunc := internal.MakeTimeoutContext(ctx, *cs.client.timeout) + if _, deadlineSet := ctx.Deadline(); !deadlineSet && cs.client.timeout != nil && !csot.IsTimeoutContext(ctx) { + newCtx, cancelFunc := csot.MakeTimeoutContext(ctx, *cs.client.timeout) // Redefine ctx to be the new timeout-derived context. ctx = newCtx // Cancel the timeout-derived context at the end of executeOperation to avoid a context leak. @@ -290,7 +294,7 @@ func (cs *ChangeStream) executeOperation(ctx context.Context, resuming bool) err if cs.client.retryReads { retries = 1 } - if internal.IsTimeoutContext(ctx) { + if csot.IsTimeoutContext(ctx) { retries = -1 } diff --git a/vendor/go.mongodb.org/mongo-driver/mongo/client.go b/vendor/go.mongodb.org/mongo-driver/mongo/client.go index 588d741fa..592927483 100644 --- a/vendor/go.mongodb.org/mongo-driver/mongo/client.go +++ b/vendor/go.mongodb.org/mongo-driver/mongo/client.go @@ -16,7 +16,7 @@ import ( "go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/bson/bsoncodec" "go.mongodb.org/mongo-driver/event" - "go.mongodb.org/mongo-driver/internal" + "go.mongodb.org/mongo-driver/internal/httputil" "go.mongodb.org/mongo-driver/internal/logger" "go.mongodb.org/mongo-driver/internal/uuid" "go.mongodb.org/mongo-driver/mongo/description" @@ -303,8 +303,8 @@ func (c *Client) Disconnect(ctx context.Context) error { ctx = context.Background() } - if c.httpClient == internal.DefaultHTTPClient { - defer internal.CloseIdleHTTPConnections(c.httpClient) + if c.httpClient == httputil.DefaultHTTPClient { + defer httputil.CloseIdleHTTPConnections(c.httpClient) } c.endSessions(ctx) diff --git a/vendor/go.mongodb.org/mongo-driver/mongo/collection.go b/vendor/go.mongodb.org/mongo-driver/mongo/collection.go index 1e696ded9..fcbfcc77a 100644 --- a/vendor/go.mongodb.org/mongo-driver/mongo/collection.go +++ b/vendor/go.mongodb.org/mongo-driver/mongo/collection.go @@ -18,7 +18,7 @@ import ( "go.mongodb.org/mongo-driver/bson/bsoncodec" "go.mongodb.org/mongo-driver/bson/bsontype" "go.mongodb.org/mongo-driver/bson/primitive" - "go.mongodb.org/mongo-driver/internal" + "go.mongodb.org/mongo-driver/internal/csfle" "go.mongodb.org/mongo-driver/mongo/description" "go.mongodb.org/mongo-driver/mongo/options" "go.mongodb.org/mongo-driver/mongo/readconcern" @@ -841,8 +841,11 @@ func aggregate(a aggregateParams) (cur *Cursor, err error) { } ao := options.MergeAggregateOptions(a.opts...) + cursorOpts := a.client.createBaseCursorOptions() + cursorOpts.MarshalValueEncoderFn = newEncoderFn(a.bsonOpts, a.registry) + op := operation.NewAggregate(pipelineArr). Session(sess). WriteConcern(wc). @@ -1230,6 +1233,9 @@ func (coll *Collection) Find(ctx context.Context, filter interface{}, Timeout(coll.client.timeout).MaxTime(fo.MaxTime).Logger(coll.client.logger) cursorOpts := coll.client.createBaseCursorOptions() + + cursorOpts.MarshalValueEncoderFn = newEncoderFn(coll.bsonOpts, coll.registry) + if fo.AllowDiskUse != nil { op.AllowDiskUse(*fo.AllowDiskUse) } @@ -1767,6 +1773,13 @@ func (coll *Collection) Indexes() IndexView { return IndexView{coll: coll} } +// SearchIndexes returns a SearchIndexView instance that can be used to perform operations on the search indexes for the collection. +func (coll *Collection) SearchIndexes() SearchIndexView { + return SearchIndexView{ + coll: coll, + } +} + // Drop drops the collection on the server. This method ignores "namespace not found" errors so it is safe to drop // a collection that does not exist on the server. func (coll *Collection) Drop(ctx context.Context) error { @@ -1798,7 +1811,7 @@ func (coll *Collection) dropEncryptedCollection(ctx context.Context, ef interfac // Drop the two encryption-related, associated collections: `escCollection` and `ecocCollection`. // Drop ESCCollection. - escCollection, err := internal.GetEncryptedStateCollectionName(efBSON, coll.name, internal.EncryptedStateCollection) + escCollection, err := csfle.GetEncryptedStateCollectionName(efBSON, coll.name, csfle.EncryptedStateCollection) if err != nil { return err } @@ -1807,7 +1820,7 @@ func (coll *Collection) dropEncryptedCollection(ctx context.Context, ef interfac } // Drop ECOCCollection. - ecocCollection, err := internal.GetEncryptedStateCollectionName(efBSON, coll.name, internal.EncryptedCompactionCollection) + ecocCollection, err := csfle.GetEncryptedStateCollectionName(efBSON, coll.name, csfle.EncryptedCompactionCollection) if err != nil { return err } @@ -1854,7 +1867,7 @@ func (coll *Collection) drop(ctx context.Context) error { ServerAPI(coll.client.serverAPI).Timeout(coll.client.timeout) err = op.Execute(ctx) - // ignore namespace not found erorrs + // ignore namespace not found errors driverErr, ok := err.(driver.Error) if !ok || (ok && !driverErr.NamespaceNotFound()) { return replaceErrors(err) @@ -1862,26 +1875,52 @@ func (coll *Collection) drop(ctx context.Context) error { return nil } -// makePinnedSelector makes a selector for a pinned session with a pinned server. Will attempt to do server selection on -// the pinned server but if that fails it will go through a list of default selectors -func makePinnedSelector(sess *session.Client, defaultSelector description.ServerSelector) description.ServerSelectorFunc { - return func(t description.Topology, svrs []description.Server) ([]description.Server, error) { - if sess != nil && sess.PinnedServer != nil { - // If there is a pinned server, try to find it in the list of candidates. - for _, candidate := range svrs { - if candidate.Addr == sess.PinnedServer.Addr { - return []description.Server{candidate}, nil - } - } +type pinnedServerSelector struct { + stringer fmt.Stringer + fallback description.ServerSelector + session *session.Client +} + +func (pss pinnedServerSelector) String() string { + if pss.stringer == nil { + return "" + } - return nil, nil + return pss.stringer.String() +} + +func (pss pinnedServerSelector) SelectServer( + t description.Topology, + svrs []description.Server, +) ([]description.Server, error) { + if pss.session != nil && pss.session.PinnedServer != nil { + // If there is a pinned server, try to find it in the list of candidates. + for _, candidate := range svrs { + if candidate.Addr == pss.session.PinnedServer.Addr { + return []description.Server{candidate}, nil + } } - return defaultSelector.SelectServer(t, svrs) + return nil, nil + } + + return pss.fallback.SelectServer(t, svrs) +} + +func makePinnedSelector(sess *session.Client, fallback description.ServerSelector) description.ServerSelector { + pss := pinnedServerSelector{ + session: sess, + fallback: fallback, } + + if srvSelectorStringer, ok := fallback.(fmt.Stringer); ok { + pss.stringer = srvSelectorStringer + } + + return pss } -func makeReadPrefSelector(sess *session.Client, selector description.ServerSelector, localThreshold time.Duration) description.ServerSelectorFunc { +func makeReadPrefSelector(sess *session.Client, selector description.ServerSelector, localThreshold time.Duration) description.ServerSelector { if sess != nil && sess.TransactionRunning() { selector = description.CompositeSelector([]description.ServerSelector{ description.ReadPrefSelector(sess.CurrentRp), @@ -1892,7 +1931,7 @@ func makeReadPrefSelector(sess *session.Client, selector description.ServerSelec return makePinnedSelector(sess, selector) } -func makeOutputAggregateSelector(sess *session.Client, rp *readpref.ReadPref, localThreshold time.Duration) description.ServerSelectorFunc { +func makeOutputAggregateSelector(sess *session.Client, rp *readpref.ReadPref, localThreshold time.Duration) description.ServerSelector { if sess != nil && sess.TransactionRunning() { // Use current transaction's read preference if available rp = sess.CurrentRp diff --git a/vendor/go.mongodb.org/mongo-driver/mongo/cursor.go b/vendor/go.mongodb.org/mongo-driver/mongo/cursor.go index 9b348cb46..d2228ed9c 100644 --- a/vendor/go.mongodb.org/mongo-driver/mongo/cursor.go +++ b/vendor/go.mongodb.org/mongo-driver/mongo/cursor.go @@ -12,6 +12,7 @@ import ( "fmt" "io" "reflect" + "time" "go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/bson/bsoncodec" @@ -389,6 +390,22 @@ func (c *Cursor) SetBatchSize(batchSize int32) { c.bc.SetBatchSize(batchSize) } +// SetMaxTime will set the maximum amount of time the server will allow the +// operations to execute. The server will error if this field is set but the +// cursor is not configured with awaitData=true. +// +// The time.Duration value passed by this setter will be converted and rounded +// down to the nearest millisecond. +func (c *Cursor) SetMaxTime(dur time.Duration) { + c.bc.SetMaxTime(dur) +} + +// SetComment will set a user-configurable comment that can be used to identify +// the operation in server logs. +func (c *Cursor) SetComment(comment interface{}) { + c.bc.SetComment(comment) +} + // BatchCursorFromCursor returns a driver.BatchCursor for the given Cursor. If there is no underlying // driver.BatchCursor, nil is returned. // diff --git a/vendor/go.mongodb.org/mongo-driver/mongo/database.go b/vendor/go.mongodb.org/mongo-driver/mongo/database.go index 8dd0352ae..f5d5ad379 100644 --- a/vendor/go.mongodb.org/mongo-driver/mongo/database.go +++ b/vendor/go.mongodb.org/mongo-driver/mongo/database.go @@ -14,7 +14,7 @@ import ( "go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/bson/bsoncodec" - "go.mongodb.org/mongo-driver/internal" + "go.mongodb.org/mongo-driver/internal/csfle" "go.mongodb.org/mongo-driver/mongo/description" "go.mongodb.org/mongo-driver/mongo/options" "go.mongodb.org/mongo-driver/mongo/readconcern" @@ -177,6 +177,9 @@ func (db *Database) processRunCommand(ctx context.Context, cmd interface{}, switch cursorCommand { case true: cursorOpts := db.client.createBaseCursorOptions() + + cursorOpts.MarshalValueEncoderFn = newEncoderFn(db.bsonOpts, db.registry) + op = operation.NewCursorCommand(runCmdDoc, cursorOpts) default: op = operation.NewCommand(runCmdDoc) @@ -254,6 +257,10 @@ func (db *Database) RunCommandCursor(ctx context.Context, runCommand interface{} if err = op.Execute(ctx); err != nil { closeImplicitSession(sess) + if errors.Is(err, driver.ErrNoCursor) { + return nil, errors.New( + "database response does not contain a cursor; try using RunCommand instead") + } return nil, replaceErrors(err) } @@ -395,6 +402,9 @@ func (db *Database) ListCollections(ctx context.Context, filter interface{}, opt ServerAPI(db.client.serverAPI).Timeout(db.client.timeout) cursorOpts := db.client.createBaseCursorOptions() + + cursorOpts.MarshalValueEncoderFn = newEncoderFn(db.bsonOpts, db.registry) + if lco.NameOnly != nil { op = op.NameOnly(*lco.NameOnly) } @@ -617,7 +627,7 @@ func (db *Database) createCollectionWithEncryptedFields(ctx context.Context, nam stateCollectionOpts := options.CreateCollection(). SetClusteredIndex(bson.D{{"key", bson.D{{"_id", 1}}}, {"unique", true}}) // Create ESCCollection. - escCollection, err := internal.GetEncryptedStateCollectionName(efBSON, name, internal.EncryptedStateCollection) + escCollection, err := csfle.GetEncryptedStateCollectionName(efBSON, name, csfle.EncryptedStateCollection) if err != nil { return err } @@ -627,7 +637,7 @@ func (db *Database) createCollectionWithEncryptedFields(ctx context.Context, nam } // Create ECOCCollection. - ecocCollection, err := internal.GetEncryptedStateCollectionName(efBSON, name, internal.EncryptedCompactionCollection) + ecocCollection, err := csfle.GetEncryptedStateCollectionName(efBSON, name, csfle.EncryptedCompactionCollection) if err != nil { return err } diff --git a/vendor/go.mongodb.org/mongo-driver/mongo/description/server.go b/vendor/go.mongodb.org/mongo-driver/mongo/description/server.go index a20c86ac9..cf3942383 100644 --- a/vendor/go.mongodb.org/mongo-driver/mongo/description/server.go +++ b/vendor/go.mongodb.org/mongo-driver/mongo/description/server.go @@ -13,7 +13,9 @@ import ( "go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/bson/primitive" - "go.mongodb.org/mongo-driver/internal" + "go.mongodb.org/mongo-driver/internal/bsonutil" + "go.mongodb.org/mongo-driver/internal/handshake" + "go.mongodb.org/mongo-driver/internal/ptrutil" "go.mongodb.org/mongo-driver/mongo/address" "go.mongodb.org/mongo-driver/tag" ) @@ -31,35 +33,37 @@ type SelectedServer struct { type Server struct { Addr address.Address - Arbiters []string - AverageRTT time.Duration - AverageRTTSet bool - Compression []string // compression methods returned by server - CanonicalAddr address.Address - ElectionID primitive.ObjectID - HeartbeatInterval time.Duration - HelloOK bool - Hosts []string - IsCryptd bool - LastError error - LastUpdateTime time.Time - LastWriteTime time.Time - MaxBatchCount uint32 - MaxDocumentSize uint32 - MaxMessageSize uint32 - Members []address.Address - Passives []string - Passive bool - Primary address.Address - ReadOnly bool - ServiceID *primitive.ObjectID // Only set for servers that are deployed behind a load balancer. - SessionTimeoutMinutes uint32 - SetName string - SetVersion uint32 - Tags tag.Set - TopologyVersion *TopologyVersion - Kind ServerKind - WireVersion *VersionRange + Arbiters []string + AverageRTT time.Duration + AverageRTTSet bool + Compression []string // compression methods returned by server + CanonicalAddr address.Address + ElectionID primitive.ObjectID + HeartbeatInterval time.Duration + HelloOK bool + Hosts []string + IsCryptd bool + LastError error + LastUpdateTime time.Time + LastWriteTime time.Time + MaxBatchCount uint32 + MaxDocumentSize uint32 + MaxMessageSize uint32 + Members []address.Address + Passives []string + Passive bool + Primary address.Address + ReadOnly bool + ServiceID *primitive.ObjectID // Only set for servers that are deployed behind a load balancer. + // Deprecated: Use SessionTimeoutMinutesPtr instead. + SessionTimeoutMinutes uint32 + SessionTimeoutMinutesPtr *int64 + SetName string + SetVersion uint32 + Tags tag.Set + TopologyVersion *TopologyVersion + Kind ServerKind + WireVersion *VersionRange } // NewServer creates a new server description from the given hello command response. @@ -78,7 +82,7 @@ func NewServer(addr address.Address, response bson.Raw) Server { switch element.Key() { case "arbiters": var err error - desc.Arbiters, err = internal.StringSliceFromRawElement(element) + desc.Arbiters, err = stringSliceFromRawElement(element) if err != nil { desc.LastError = err return desc @@ -91,7 +95,7 @@ func NewServer(addr address.Address, response bson.Raw) Server { } case "compression": var err error - desc.Compression, err = internal.StringSliceFromRawElement(element) + desc.Compression, err = stringSliceFromRawElement(element) if err != nil { desc.LastError = err return desc @@ -122,7 +126,7 @@ func NewServer(addr address.Address, response bson.Raw) Server { } case "hosts": var err error - desc.Hosts, err = internal.StringSliceFromRawElement(element) + desc.Hosts, err = stringSliceFromRawElement(element) if err != nil { desc.LastError = err return desc @@ -133,7 +137,7 @@ func NewServer(addr address.Address, response bson.Raw) Server { desc.LastError = fmt.Errorf("expected 'isWritablePrimary' to be a boolean but it's a BSON %s", element.Value().Type) return desc } - case internal.LegacyHelloLowercase: + case handshake.LegacyHelloLowercase: isWritablePrimary, ok = element.Value().BooleanOK() if !ok { desc.LastError = fmt.Errorf("expected legacy hello to be a boolean but it's a BSON %s", element.Value().Type) @@ -166,7 +170,9 @@ func NewServer(addr address.Address, response bson.Raw) Server { desc.LastError = fmt.Errorf("expected 'logicalSessionTimeoutMinutes' to be an integer but it's a BSON %s", element.Value().Type) return desc } + desc.SessionTimeoutMinutes = uint32(i64) + desc.SessionTimeoutMinutesPtr = &i64 case "maxBsonObjectSize": i64, ok := element.Value().AsInt64OK() if !ok { @@ -225,7 +231,7 @@ func NewServer(addr address.Address, response bson.Raw) Server { } case "passives": var err error - desc.Passives, err = internal.StringSliceFromRawElement(element) + desc.Passives, err = stringSliceFromRawElement(element) if err != nil { desc.LastError = err return desc @@ -462,7 +468,7 @@ func (s Server) Equal(other Server) bool { return false } - if s.SessionTimeoutMinutes != other.SessionTimeoutMinutes { + if ptrutil.CompareInt64(s.SessionTimeoutMinutesPtr, other.SessionTimeoutMinutesPtr) != 0 { return false } @@ -486,3 +492,11 @@ func sliceStringEqual(a []string, b []string) bool { } return true } + +// stringSliceFromRawElement decodes the provided BSON element into a []string. +// This internally calls StringSliceFromRawValue on the element's value. The +// error conditions outlined in that function's documentation apply for this +// function as well. +func stringSliceFromRawElement(element bson.RawElement) ([]string, error) { + return bsonutil.StringSliceFromRawValue(element.Key(), element.Value()) +} diff --git a/vendor/go.mongodb.org/mongo-driver/mongo/description/server_selector.go b/vendor/go.mongodb.org/mongo-driver/mongo/description/server_selector.go index 2b21a4bd6..aee1f050c 100644 --- a/vendor/go.mongodb.org/mongo-driver/mongo/description/server_selector.go +++ b/vendor/go.mongodb.org/mongo-driver/mongo/description/server_selector.go @@ -7,6 +7,7 @@ package description import ( + "encoding/json" "fmt" "math" "time" @@ -30,10 +31,48 @@ func (ssf ServerSelectorFunc) SelectServer(t Topology, s []Server) ([]Server, er return ssf(t, s) } +// serverSelectorInfo contains metadata concerning the server selector for the +// purpose of publication. +type serverSelectorInfo struct { + Type string + Data string `json:",omitempty"` + Selectors []serverSelectorInfo `json:",omitempty"` +} + +// String returns the JSON string representation of the serverSelectorInfo. +func (sss serverSelectorInfo) String() string { + bytes, _ := json.Marshal(sss) + + return string(bytes) +} + +// serverSelectorInfoGetter is an interface that defines an info() method to +// get the serverSelectorInfo. +type serverSelectorInfoGetter interface { + info() serverSelectorInfo +} + type compositeSelector struct { selectors []ServerSelector } +func (cs *compositeSelector) info() serverSelectorInfo { + csInfo := serverSelectorInfo{Type: "compositeSelector"} + + for _, sel := range cs.selectors { + if getter, ok := sel.(serverSelectorInfoGetter); ok { + csInfo.Selectors = append(csInfo.Selectors, getter.info()) + } + } + + return csInfo +} + +// String returns the JSON string representation of the compositeSelector. +func (cs *compositeSelector) String() string { + return cs.info().String() +} + // CompositeSelector combines multiple selectors into a single selector by applying them in order to the candidates // list. // @@ -68,8 +107,16 @@ func LatencySelector(latency time.Duration) ServerSelector { return &latencySelector{latency: latency} } -func (ls *latencySelector) SelectServer(t Topology, candidates []Server) ([]Server, error) { - if ls.latency < 0 { +func (latencySelector) info() serverSelectorInfo { + return serverSelectorInfo{Type: "latencySelector"} +} + +func (selector latencySelector) String() string { + return selector.info().String() +} + +func (selector *latencySelector) SelectServer(t Topology, candidates []Server) ([]Server, error) { + if selector.latency < 0 { return candidates, nil } if t.Kind == LoadBalanced { @@ -94,7 +141,7 @@ func (ls *latencySelector) SelectServer(t Topology, candidates []Server) ([]Serv return candidates, nil } - max := min + ls.latency + max := min + selector.latency viableIndexes := make([]int, 0, len(candidates)) for i, candidate := range candidates { @@ -115,75 +162,88 @@ func (ls *latencySelector) SelectServer(t Topology, candidates []Server) ([]Serv } } +type writeServerSelector struct{} + // WriteSelector selects all the writable servers. func WriteSelector() ServerSelector { - return ServerSelectorFunc(func(t Topology, candidates []Server) ([]Server, error) { - switch t.Kind { - case Single, LoadBalanced: - return candidates, nil - default: - result := []Server{} - for _, candidate := range candidates { - switch candidate.Kind { - case Mongos, RSPrimary, Standalone: - result = append(result, candidate) - } + return writeServerSelector{} +} + +func (writeServerSelector) info() serverSelectorInfo { + return serverSelectorInfo{Type: "writeSelector"} +} + +func (selector writeServerSelector) String() string { + return selector.info().String() +} + +func (writeServerSelector) SelectServer(t Topology, candidates []Server) ([]Server, error) { + switch t.Kind { + case Single, LoadBalanced: + return candidates, nil + default: + result := []Server{} + for _, candidate := range candidates { + switch candidate.Kind { + case Mongos, RSPrimary, Standalone: + result = append(result, candidate) } - return result, nil } - }) + return result, nil + } +} + +type readPrefServerSelector struct { + rp *readpref.ReadPref + isOutputAggregate bool } // ReadPrefSelector selects servers based on the provided read preference. func ReadPrefSelector(rp *readpref.ReadPref) ServerSelector { - return readPrefSelector(rp, false) + return readPrefServerSelector{ + rp: rp, + isOutputAggregate: false, + } } -// OutputAggregateSelector selects servers based on the provided read preference given that the underlying operation is -// aggregate with an output stage. -func OutputAggregateSelector(rp *readpref.ReadPref) ServerSelector { - return readPrefSelector(rp, true) +func (selector readPrefServerSelector) info() serverSelectorInfo { + return serverSelectorInfo{ + Type: "readPrefSelector", + Data: selector.rp.String(), + } } -func readPrefSelector(rp *readpref.ReadPref, isOutputAggregate bool) ServerSelector { - return ServerSelectorFunc(func(t Topology, candidates []Server) ([]Server, error) { - if t.Kind == LoadBalanced { - // In LoadBalanced mode, there should only be one server in the topology and it must be selected. We check - // this before checking MaxStaleness support because there's no monitoring in this mode, so the candidate - // server wouldn't have a wire version set, which would result in an error. - return candidates, nil - } +func (selector readPrefServerSelector) String() string { + return selector.info().String() +} - if _, set := rp.MaxStaleness(); set { - for _, s := range candidates { - if s.Kind != Unknown { - if err := maxStalenessSupported(s.WireVersion); err != nil { - return nil, err - } - } - } - } +func (selector readPrefServerSelector) SelectServer(t Topology, candidates []Server) ([]Server, error) { + if t.Kind == LoadBalanced { + // In LoadBalanced mode, there should only be one server in the topology and it must be selected. We check + // this before checking MaxStaleness support because there's no monitoring in this mode, so the candidate + // server wouldn't have a wire version set, which would result in an error. + return candidates, nil + } - switch t.Kind { - case Single: - return candidates, nil - case ReplicaSetNoPrimary, ReplicaSetWithPrimary: - return selectForReplicaSet(rp, isOutputAggregate, t, candidates) - case Sharded: - return selectByKind(candidates, Mongos), nil - } + switch t.Kind { + case Single: + return candidates, nil + case ReplicaSetNoPrimary, ReplicaSetWithPrimary: + return selectForReplicaSet(selector.rp, selector.isOutputAggregate, t, candidates) + case Sharded: + return selectByKind(candidates, Mongos), nil + } - return nil, nil - }) + return nil, nil } -// maxStalenessSupported returns an error if the given server version does not support max staleness. -func maxStalenessSupported(wireVersion *VersionRange) error { - if wireVersion != nil && wireVersion.Max < 5 { - return fmt.Errorf("max staleness is only supported for servers 3.4 or newer") +// OutputAggregateSelector selects servers based on the provided read preference +// given that the underlying operation is aggregate with an output stage. +func OutputAggregateSelector(rp *readpref.ReadPref) ServerSelector { + return readPrefServerSelector{ + rp: rp, + isOutputAggregate: true, } - - return nil } func selectForReplicaSet(rp *readpref.ReadPref, isOutputAggregate bool, t Topology, candidates []Server) ([]Server, error) { diff --git a/vendor/go.mongodb.org/mongo-driver/mongo/description/topology.go b/vendor/go.mongodb.org/mongo-driver/mongo/description/topology.go index 8544548c9..b082515e5 100644 --- a/vendor/go.mongodb.org/mongo-driver/mongo/description/topology.go +++ b/vendor/go.mongodb.org/mongo-driver/mongo/description/topology.go @@ -14,11 +14,13 @@ import ( // Topology contains information about a MongoDB cluster. type Topology struct { - Servers []Server - SetName string - Kind TopologyKind - SessionTimeoutMinutes uint32 - CompatibilityErr error + Servers []Server + SetName string + Kind TopologyKind + // Deprecated: Use SessionTimeoutMinutesPtr instead. + SessionTimeoutMinutes uint32 + SessionTimeoutMinutesPtr *int64 + CompatibilityErr error } // String implements the Stringer interface. diff --git a/vendor/go.mongodb.org/mongo-driver/mongo/errors.go b/vendor/go.mongodb.org/mongo-driver/mongo/errors.go index aff99378d..72c3bcc24 100644 --- a/vendor/go.mongodb.org/mongo-driver/mongo/errors.go +++ b/vendor/go.mongodb.org/mongo-driver/mongo/errors.go @@ -15,6 +15,7 @@ import ( "strings" "go.mongodb.org/mongo-driver/bson" + "go.mongodb.org/mongo-driver/internal/codecutil" "go.mongodb.org/mongo-driver/x/mongo/driver" "go.mongodb.org/mongo-driver/x/mongo/driver/mongocrypt" "go.mongodb.org/mongo-driver/x/mongo/driver/topology" @@ -87,53 +88,70 @@ func replaceErrors(err error) error { return MongocryptError{Code: me.Code, Message: me.Message} } + if errors.Is(err, codecutil.ErrNilValue) { + return ErrNilValue + } + + if marshalErr, ok := err.(codecutil.MarshalError); ok { + return MarshalError{ + Value: marshalErr.Value, + Err: marshalErr.Err, + } + } + return err } -// IsDuplicateKeyError returns true if err is a duplicate key error +// IsDuplicateKeyError returns true if err is a duplicate key error. func IsDuplicateKeyError(err error) bool { - // handles SERVER-7164 and SERVER-11493 - for ; err != nil; err = unwrap(err) { - if e, ok := err.(ServerError); ok { - return e.HasErrorCode(11000) || e.HasErrorCode(11001) || e.HasErrorCode(12582) || - e.HasErrorCodeWithMessage(16460, " E11000 ") - } + if se := ServerError(nil); errors.As(err, &se) { + return se.HasErrorCode(11000) || // Duplicate key error. + se.HasErrorCode(11001) || // Duplicate key error on update. + // Duplicate key error in a capped collection. See SERVER-7164. + se.HasErrorCode(12582) || + // Mongos insert error caused by a duplicate key error. See + // SERVER-11493. + se.HasErrorCodeWithMessage(16460, " E11000 ") } return false } -// IsTimeout returns true if err is from a timeout +// timeoutErrs is a list of error values that indicate a timeout happened. +var timeoutErrs = [...]error{ + context.DeadlineExceeded, + driver.ErrDeadlineWouldBeExceeded, + topology.ErrServerSelectionTimeout, +} + +// IsTimeout returns true if err was caused by a timeout. For error chains, +// IsTimeout returns true if any error in the chain was caused by a timeout. func IsTimeout(err error) bool { - for ; err != nil; err = unwrap(err) { - // check unwrappable errors together - if err == context.DeadlineExceeded { - return true - } - if err == driver.ErrDeadlineWouldBeExceeded { - return true - } - if err == topology.ErrServerSelectionTimeout { - return true - } - if _, ok := err.(topology.WaitQueueTimeoutError); ok { + // Check if the error chain contains any of the timeout error values. + for _, target := range timeoutErrs { + if errors.Is(err, target) { return true } - if ce, ok := err.(CommandError); ok && ce.IsMaxTimeMSExpiredError() { - return true - } - if we, ok := err.(WriteException); ok && we.WriteConcernError != nil && - we.WriteConcernError.IsMaxTimeMSExpiredError() { + } + + // Check if the error chain contains any error types that can indicate + // timeout. + if errors.As(err, &topology.WaitQueueTimeoutError{}) { + return true + } + if ce := (CommandError{}); errors.As(err, &ce) && ce.IsMaxTimeMSExpiredError() { + return true + } + if we := (WriteException{}); errors.As(err, &we) && we.WriteConcernError != nil && we.WriteConcernError.IsMaxTimeMSExpiredError() { + return true + } + if ne := net.Error(nil); errors.As(err, &ne) { + return ne.Timeout() + } + // Check timeout error labels. + if le := LabeledError(nil); errors.As(err, &le) { + if le.HasErrorLabel("NetworkTimeoutError") || le.HasErrorLabel("ExceededTimeLimitError") { return true } - if ne, ok := err.(net.Error); ok { - return ne.Timeout() - } - //timeout error labels - if le, ok := err.(LabeledError); ok { - if le.HasErrorLabel("NetworkTimeoutError") || le.HasErrorLabel("ExceededTimeLimitError") { - return true - } - } } return false diff --git a/vendor/go.mongodb.org/mongo-driver/mongo/index_view.go b/vendor/go.mongodb.org/mongo-driver/mongo/index_view.go index 502de2f2f..41a93a214 100644 --- a/vendor/go.mongodb.org/mongo-driver/mongo/index_view.go +++ b/vendor/go.mongodb.org/mongo-driver/mongo/index_view.go @@ -94,6 +94,9 @@ func (iv IndexView) List(ctx context.Context, opts ...*options.ListIndexesOption Timeout(iv.coll.client.timeout) cursorOpts := iv.coll.client.createBaseCursorOptions() + + cursorOpts.MarshalValueEncoderFn = newEncoderFn(iv.coll.bsonOpts, iv.coll.registry) + lio := options.MergeListIndexesOptions(opts...) if lio.BatchSize != nil { op = op.BatchSize(*lio.BatchSize) diff --git a/vendor/go.mongodb.org/mongo-driver/mongo/mongo.go b/vendor/go.mongodb.org/mongo-driver/mongo/mongo.go index ded99e4e2..393c5b771 100644 --- a/vendor/go.mongodb.org/mongo-driver/mongo/mongo.go +++ b/vendor/go.mongodb.org/mongo-driver/mongo/mongo.go @@ -17,6 +17,7 @@ import ( "strconv" "strings" + "go.mongodb.org/mongo-driver/internal/codecutil" "go.mongodb.org/mongo-driver/mongo/options" "go.mongodb.org/mongo-driver/x/bsonx/bsoncore" @@ -132,6 +133,14 @@ func getEncoder( return enc, nil } +// newEncoderFn will return a function for constructing an encoder based on the +// provided codec options. +func newEncoderFn(opts *options.BSONOptions, registry *bsoncodec.Registry) codecutil.EncoderFn { + return func(w io.Writer) (*bson.Encoder, error) { + return getEncoder(w, opts, registry) + } +} + // marshal marshals the given value as a BSON document. Byte slices are always converted to a // bson.Raw before marshaling. // @@ -421,26 +430,7 @@ func marshalValue( bsonOpts *options.BSONOptions, registry *bsoncodec.Registry, ) (bsoncore.Value, error) { - if registry == nil { - registry = bson.DefaultRegistry - } - if val == nil { - return bsoncore.Value{}, ErrNilValue - } - - buf := new(bytes.Buffer) - enc, err := getEncoder(buf, bsonOpts, registry) - if err != nil { - return bsoncore.Value{}, fmt.Errorf("error configuring BSON encoder: %w", err) - } - - // Encode the value in a single-element document with an empty key. Use bsoncore to extract the - // first element and return the BSON value. - err = enc.Encode(bson.D{{Key: "", Value: val}}) - if err != nil { - return bsoncore.Value{}, MarshalError{Value: val, Err: err} - } - return bsoncore.Document(buf.Bytes()).Index(0).Value(), nil + return codecutil.MarshalValue(val, newEncoderFn(bsonOpts, registry)) } // Build the aggregation pipeline for the CountDocument command. diff --git a/vendor/go.mongodb.org/mongo-driver/mongo/mongocryptd.go b/vendor/go.mongodb.org/mongo-driver/mongo/mongocryptd.go index 41aebc76c..2603a3918 100644 --- a/vendor/go.mongodb.org/mongo-driver/mongo/mongocryptd.go +++ b/vendor/go.mongodb.org/mongo-driver/mongo/mongocryptd.go @@ -91,7 +91,7 @@ func (mc *mongocryptdClient) markCommand(ctx context.Context, dbName string, cmd ctx = NewSessionContext(ctx, nil) db := mc.client.Database(dbName, databaseOpts) - res, err := db.RunCommand(ctx, cmd).DecodeBytes() + res, err := db.RunCommand(ctx, cmd).Raw() // propagate original result if err == nil { return bsoncore.Document(res), nil @@ -105,7 +105,7 @@ func (mc *mongocryptdClient) markCommand(ctx context.Context, dbName string, cmd if err = mc.spawnProcess(); err != nil { return nil, err } - res, err = db.RunCommand(ctx, cmd).DecodeBytes() + res, err = db.RunCommand(ctx, cmd).Raw() if err != nil { return nil, MongocryptdError{Wrapped: err} } diff --git a/vendor/go.mongodb.org/mongo-driver/mongo/options/autoencryptionoptions.go b/vendor/go.mongodb.org/mongo-driver/mongo/options/autoencryptionoptions.go index d561d5ef1..15d513862 100644 --- a/vendor/go.mongodb.org/mongo-driver/mongo/options/autoencryptionoptions.go +++ b/vendor/go.mongodb.org/mongo-driver/mongo/options/autoencryptionoptions.go @@ -10,7 +10,7 @@ import ( "crypto/tls" "net/http" - "go.mongodb.org/mongo-driver/internal" + "go.mongodb.org/mongo-driver/internal/httputil" ) // AutoEncryptionOptions represents options used to configure auto encryption/decryption behavior for a mongo.Client @@ -43,7 +43,7 @@ type AutoEncryptionOptions struct { // AutoEncryption creates a new AutoEncryptionOptions configured with default values. func AutoEncryption() *AutoEncryptionOptions { return &AutoEncryptionOptions{ - HTTPClient: internal.DefaultHTTPClient, + HTTPClient: httputil.DefaultHTTPClient, } } diff --git a/vendor/go.mongodb.org/mongo-driver/mongo/options/clientencryptionoptions.go b/vendor/go.mongodb.org/mongo-driver/mongo/options/clientencryptionoptions.go index 7eef3fe6a..2457f682b 100644 --- a/vendor/go.mongodb.org/mongo-driver/mongo/options/clientencryptionoptions.go +++ b/vendor/go.mongodb.org/mongo-driver/mongo/options/clientencryptionoptions.go @@ -11,7 +11,7 @@ import ( "fmt" "net/http" - "go.mongodb.org/mongo-driver/internal" + "go.mongodb.org/mongo-driver/internal/httputil" ) // ClientEncryptionOptions represents all possible options used to configure a ClientEncryption instance. @@ -25,7 +25,7 @@ type ClientEncryptionOptions struct { // ClientEncryption creates a new ClientEncryptionOptions instance. func ClientEncryption() *ClientEncryptionOptions { return &ClientEncryptionOptions{ - HTTPClient: internal.DefaultHTTPClient, + HTTPClient: httputil.DefaultHTTPClient, } } diff --git a/vendor/go.mongodb.org/mongo-driver/mongo/options/clientoptions.go b/vendor/go.mongodb.org/mongo-driver/mongo/options/clientoptions.go index 1c2e5bed5..42664be03 100644 --- a/vendor/go.mongodb.org/mongo-driver/mongo/options/clientoptions.go +++ b/vendor/go.mongodb.org/mongo-driver/mongo/options/clientoptions.go @@ -23,7 +23,7 @@ import ( "github.com/youmark/pkcs8" "go.mongodb.org/mongo-driver/bson/bsoncodec" "go.mongodb.org/mongo-driver/event" - "go.mongodb.org/mongo-driver/internal" + "go.mongodb.org/mongo-driver/internal/httputil" "go.mongodb.org/mongo-driver/mongo/readconcern" "go.mongodb.org/mongo-driver/mongo/readpref" "go.mongodb.org/mongo-driver/mongo/writeconcern" @@ -33,6 +33,26 @@ import ( "go.mongodb.org/mongo-driver/x/mongo/driver/wiremessage" ) +const ( + // ServerMonitoringModeAuto indicates that the client will behave like "poll" + // mode when running on a FaaS (Function as a Service) platform, or like + // "stream" mode otherwise. The client detects its execution environment by + // following the rules for generating the "client.env" handshake metadata field + // as specified in the MongoDB Handshake specification. This is the default + // mode. + ServerMonitoringModeAuto = connstring.ServerMonitoringModeAuto + + // ServerMonitoringModePoll indicates that the client will periodically check + // the server using a hello or legacy hello command and then sleep for + // heartbeatFrequencyMS milliseconds before running another check. + ServerMonitoringModePoll = connstring.ServerMonitoringModePoll + + // ServerMonitoringModeStream indicates that the client will use a streaming + // protocol when the server supports it. The streaming protocol optimally + // reduces the time it takes for a client to discover server state changes. + ServerMonitoringModeStream = connstring.ServerMonitoringModeStream +) + // ContextDialer is an interface that can be implemented by types that can create connections. It should be used to // provide a custom dialer when configuring a Client. // @@ -206,6 +226,7 @@ type ClientOptions struct { RetryReads *bool RetryWrites *bool ServerAPIOptions *ServerAPIOptions + ServerMonitoringMode *string ServerSelectionTimeout *time.Duration SRVMaxHosts *int SRVServiceName *string @@ -249,7 +270,7 @@ type ClientOptions struct { // Client creates a new ClientOptions instance. func Client() *ClientOptions { return &ClientOptions{ - HTTPClient: internal.DefaultHTTPClient, + HTTPClient: httputil.DefaultHTTPClient, } } @@ -287,25 +308,30 @@ func (c *ClientOptions) validate() error { // Validation for load-balanced mode. if c.LoadBalanced != nil && *c.LoadBalanced { if len(c.Hosts) > 1 { - return internal.ErrLoadBalancedWithMultipleHosts + return connstring.ErrLoadBalancedWithMultipleHosts } if c.ReplicaSet != nil { - return internal.ErrLoadBalancedWithReplicaSet + return connstring.ErrLoadBalancedWithReplicaSet } if c.Direct != nil && *c.Direct { - return internal.ErrLoadBalancedWithDirectConnection + return connstring.ErrLoadBalancedWithDirectConnection } } // Validation for srvMaxHosts. if c.SRVMaxHosts != nil && *c.SRVMaxHosts > 0 { if c.ReplicaSet != nil { - return internal.ErrSRVMaxHostsWithReplicaSet + return connstring.ErrSRVMaxHostsWithReplicaSet } if c.LoadBalanced != nil && *c.LoadBalanced { - return internal.ErrSRVMaxHostsWithLoadBalanced + return connstring.ErrSRVMaxHostsWithLoadBalanced } } + + if mode := c.ServerMonitoringMode; mode != nil && !connstring.IsValidServerMonitoringMode(*mode) { + return fmt.Errorf("invalid server monitoring mode: %q", *mode) + } + return nil } @@ -317,7 +343,7 @@ func (c *ClientOptions) GetURI() string { // ApplyURI parses the given URI and sets options accordingly. The URI can contain host names, IPv4/IPv6 literals, or // an SRV record that will be resolved when the Client is created. When using an SRV record, TLS support is -// implictly enabled. Specify the "tls=false" URI option to override this. +// implicitly enabled. Specify the "tls=false" URI option to override this. // // If the connection string contains any options that have previously been set, it will overwrite them. Options that // correspond to multiple URI parameters, such as WriteConcern, will be completely overwritten if any of the query @@ -573,7 +599,7 @@ func (c *ClientOptions) SetAuth(auth Credential) *ClientOptions { // 3. "zstd" - requires server version >= 4.2, and driver version >= 1.2.0 with cgo support enabled or driver // version >= 1.3.0 without cgo. // -// If this option is specified, the driver will perform a negotiation with the server to determine a common list of of +// If this option is specified, the driver will perform a negotiation with the server to determine a common list of // compressors and will use the first one in that list when performing operations. See // https://www.mongodb.com/docs/manual/reference/program/mongod/#cmdoption-mongod-networkmessagecompressors for more // information about configuring compression on the server and the server-side defaults. @@ -586,18 +612,17 @@ func (c *ClientOptions) SetCompressors(comps []string) *ClientOptions { return c } -// SetConnectTimeout specifies a timeout that is used for creating connections to the server. If a custom Dialer is -// specified through SetDialer, this option must not be used. This can be set through ApplyURI with the -// "connectTimeoutMS" (e.g "connectTimeoutMS=30") option. If set to 0, no timeout will be used. The default is 30 -// seconds. +// SetConnectTimeout specifies a timeout that is used for creating connections to the server. This can be set through +// ApplyURI with the "connectTimeoutMS" (e.g "connectTimeoutMS=30") option. If set to 0, no timeout will be used. The +// default is 30 seconds. func (c *ClientOptions) SetConnectTimeout(d time.Duration) *ClientOptions { c.ConnectTimeout = &d return c } -// SetDialer specifies a custom ContextDialer to be used to create new connections to the server. The default is a -// net.Dialer with the Timeout field set to ConnectTimeout. See https://golang.org/pkg/net/#Dialer for more information -// about the net.Dialer type. +// SetDialer specifies a custom ContextDialer to be used to create new connections to the server. This method overrides +// the default net.Dialer, so dialer options such as Timeout, KeepAlive, Resolver, etc can be set. +// See https://golang.org/pkg/net/#Dialer for more information about the net.Dialer type. func (c *ClientOptions) SetDialer(d ContextDialer) *ClientOptions { c.Dialer = d return c @@ -872,7 +897,7 @@ func (c *ClientOptions) SetTLSConfig(cfg *tls.Config) *ClientOptions { // SetHTTPClient specifies the http.Client to be used for any HTTP requests. // -// This should only be used to set custom HTTP client configurations. By default, the connection will use an internal.DefaultHTTPClient. +// This should only be used to set custom HTTP client configurations. By default, the connection will use an httputil.DefaultHTTPClient. func (c *ClientOptions) SetHTTPClient(client *http.Client) *ClientOptions { c.HTTPClient = client return c @@ -946,6 +971,16 @@ func (c *ClientOptions) SetServerAPIOptions(opts *ServerAPIOptions) *ClientOptio return c } +// SetServerMonitoringMode specifies the server monitoring protocol to use. See +// the helper constants ServerMonitoringModeAuto, ServerMonitoringModePoll, and +// ServerMonitoringModeStream for more information about valid server +// monitoring modes. +func (c *ClientOptions) SetServerMonitoringMode(mode string) *ClientOptions { + c.ServerMonitoringMode = &mode + + return c +} + // SetSRVMaxHosts specifies the maximum number of SRV results to randomly select during polling. To limit the number // of hosts selected in SRV discovery, this function must be called before ApplyURI. This can also be set through // the "srvMaxHosts" URI option. @@ -1108,6 +1143,9 @@ func MergeClientOptions(opts ...*ClientOptions) *ClientOptions { if opt.LoggerOptions != nil { c.LoggerOptions = opt.LoggerOptions } + if opt.ServerMonitoringMode != nil { + c.ServerMonitoringMode = opt.ServerMonitoringMode + } } return c diff --git a/vendor/go.mongodb.org/mongo-driver/mongo/options/runcmdoptions.go b/vendor/go.mongodb.org/mongo-driver/mongo/options/runcmdoptions.go index 8c3899e78..b0cdec32c 100644 --- a/vendor/go.mongodb.org/mongo-driver/mongo/options/runcmdoptions.go +++ b/vendor/go.mongodb.org/mongo-driver/mongo/options/runcmdoptions.go @@ -6,7 +6,9 @@ package options -import "go.mongodb.org/mongo-driver/mongo/readpref" +import ( + "go.mongodb.org/mongo-driver/mongo/readpref" +) // RunCmdOptions represents options that can be used to configure a RunCommand operation. type RunCmdOptions struct { diff --git a/vendor/go.mongodb.org/mongo-driver/mongo/options/searchindexoptions.go b/vendor/go.mongodb.org/mongo-driver/mongo/options/searchindexoptions.go new file mode 100644 index 000000000..9774d615b --- /dev/null +++ b/vendor/go.mongodb.org/mongo-driver/mongo/options/searchindexoptions.go @@ -0,0 +1,41 @@ +// Copyright (C) MongoDB, Inc. 2023-present. +// +// Licensed under the Apache License, Version 2.0 (the "License"); you may +// not use this file except in compliance with the License. You may obtain +// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 + +package options + +// SearchIndexesOptions represents options that can be used to configure a SearchIndexView. +type SearchIndexesOptions struct { + Name *string +} + +// SearchIndexes creates a new SearchIndexesOptions instance. +func SearchIndexes() *SearchIndexesOptions { + return &SearchIndexesOptions{} +} + +// SetName sets the value for the Name field. +func (sio *SearchIndexesOptions) SetName(name string) *SearchIndexesOptions { + sio.Name = &name + return sio +} + +// CreateSearchIndexesOptions represents options that can be used to configure a SearchIndexView.CreateOne or +// SearchIndexView.CreateMany operation. +type CreateSearchIndexesOptions struct { +} + +// ListSearchIndexesOptions represents options that can be used to configure a SearchIndexView.List operation. +type ListSearchIndexesOptions struct { + AggregateOpts *AggregateOptions +} + +// DropSearchIndexOptions represents options that can be used to configure a SearchIndexView.DropOne operation. +type DropSearchIndexOptions struct { +} + +// UpdateSearchIndexOptions represents options that can be used to configure a SearchIndexView.UpdateOne operation. +type UpdateSearchIndexOptions struct { +} diff --git a/vendor/go.mongodb.org/mongo-driver/mongo/results.go b/vendor/go.mongodb.org/mongo-driver/mongo/results.go index 8408e8ba1..2dbaf2af6 100644 --- a/vendor/go.mongodb.org/mongo-driver/mongo/results.go +++ b/vendor/go.mongodb.org/mongo-driver/mongo/results.go @@ -87,7 +87,7 @@ func newListDatabasesResultFromOperation(res operation.ListDatabasesResult) List type DatabaseSpecification struct { Name string // The name of the database. SizeOnDisk int64 // The total size of the database files on disk in bytes. - Empty bool // Specfies whether or not the database is empty. + Empty bool // Specifies whether or not the database is empty. } // UpdateResult is the result type returned from UpdateOne, UpdateMany, and ReplaceOne operations. diff --git a/vendor/go.mongodb.org/mongo-driver/mongo/search_index_view.go b/vendor/go.mongodb.org/mongo-driver/mongo/search_index_view.go new file mode 100644 index 000000000..6a7871531 --- /dev/null +++ b/vendor/go.mongodb.org/mongo-driver/mongo/search_index_view.go @@ -0,0 +1,279 @@ +// Copyright (C) MongoDB, Inc. 2023-present. +// +// Licensed under the Apache License, Version 2.0 (the "License"); you may +// not use this file except in compliance with the License. You may obtain +// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 + +package mongo + +import ( + "context" + "fmt" + "strconv" + + "go.mongodb.org/mongo-driver/bson" + "go.mongodb.org/mongo-driver/mongo/options" + "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" + "go.mongodb.org/mongo-driver/x/mongo/driver/session" +) + +// SearchIndexView is a type that can be used to create, drop, list and update search indexes on a collection. A SearchIndexView for +// a collection can be created by a call to Collection.SearchIndexes(). +type SearchIndexView struct { + coll *Collection +} + +// SearchIndexModel represents a new search index to be created. +type SearchIndexModel struct { + // A document describing the definition for the search index. It cannot be nil. + // See https://www.mongodb.com/docs/atlas/atlas-search/create-index/ for reference. + Definition interface{} + + // The search index options. + Options *options.SearchIndexesOptions +} + +// List executes a listSearchIndexes command and returns a cursor over the search indexes in the collection. +// +// The name parameter specifies the index name. A nil pointer matches all indexes. +// +// The opts parameter can be used to specify options for this operation (see the options.ListSearchIndexesOptions +// documentation). +func (siv SearchIndexView) List( + ctx context.Context, + searchIdxOpts *options.SearchIndexesOptions, + opts ...*options.ListSearchIndexesOptions, +) (*Cursor, error) { + if ctx == nil { + ctx = context.Background() + } + + index := bson.D{} + if searchIdxOpts != nil && searchIdxOpts.Name != nil { + index = bson.D{{"name", *searchIdxOpts.Name}} + } + + aggregateOpts := make([]*options.AggregateOptions, len(opts)) + for i, opt := range opts { + aggregateOpts[i] = opt.AggregateOpts + } + + return siv.coll.Aggregate(ctx, Pipeline{{{"$listSearchIndexes", index}}}, aggregateOpts...) +} + +// CreateOne executes a createSearchIndexes command to create a search index on the collection and returns the name of the new +// search index. See the SearchIndexView.CreateMany documentation for more information and an example. +func (siv SearchIndexView) CreateOne( + ctx context.Context, + model SearchIndexModel, + opts ...*options.CreateSearchIndexesOptions, +) (string, error) { + names, err := siv.CreateMany(ctx, []SearchIndexModel{model}, opts...) + if err != nil { + return "", err + } + + return names[0], nil +} + +// CreateMany executes a createSearchIndexes command to create multiple search indexes on the collection and returns +// the names of the new search indexes. +// +// For each SearchIndexModel in the models parameter, the index name can be specified. +// +// The opts parameter can be used to specify options for this operation (see the options.CreateSearchIndexesOptions +// documentation). +func (siv SearchIndexView) CreateMany( + ctx context.Context, + models []SearchIndexModel, + _ ...*options.CreateSearchIndexesOptions, +) ([]string, error) { + var indexes bsoncore.Document + aidx, indexes := bsoncore.AppendArrayStart(indexes) + + for i, model := range models { + if model.Definition == nil { + return nil, fmt.Errorf("search index model definition cannot be nil") + } + + definition, err := marshal(model.Definition, siv.coll.bsonOpts, siv.coll.registry) + if err != nil { + return nil, err + } + + var iidx int32 + iidx, indexes = bsoncore.AppendDocumentElementStart(indexes, strconv.Itoa(i)) + if model.Options != nil && model.Options.Name != nil { + indexes = bsoncore.AppendStringElement(indexes, "name", *model.Options.Name) + } + indexes = bsoncore.AppendDocumentElement(indexes, "definition", definition) + + indexes, err = bsoncore.AppendDocumentEnd(indexes, iidx) + if err != nil { + return nil, err + } + } + + indexes, err := bsoncore.AppendArrayEnd(indexes, aidx) + if err != nil { + return nil, err + } + + sess := sessionFromContext(ctx) + + if sess == nil && siv.coll.client.sessionPool != nil { + sess = session.NewImplicitClientSession(siv.coll.client.sessionPool, siv.coll.client.id) + defer sess.EndSession() + } + + err = siv.coll.client.validSession(sess) + if err != nil { + return nil, err + } + + wc := siv.coll.writeConcern + if sess.TransactionRunning() { + wc = nil + } + if !writeconcern.AckWrite(wc) { + sess = nil + } + + selector := makePinnedSelector(sess, siv.coll.writeSelector) + + op := operation.NewCreateSearchIndexes(indexes). + Session(sess).WriteConcern(wc).ClusterClock(siv.coll.client.clock). + Database(siv.coll.db.name).Collection(siv.coll.name).CommandMonitor(siv.coll.client.monitor). + Deployment(siv.coll.client.deployment).ServerSelector(selector).ServerAPI(siv.coll.client.serverAPI). + Timeout(siv.coll.client.timeout) + + err = op.Execute(ctx) + if err != nil { + _, err = processWriteError(err) + return nil, err + } + + indexesCreated := op.Result().IndexesCreated + names := make([]string, 0, len(indexesCreated)) + for _, index := range indexesCreated { + names = append(names, index.Name) + } + + return names, nil +} + +// DropOne executes a dropSearchIndexes operation to drop a search index on the collection. +// +// The name parameter should be the name of the search index to drop. If the name is "*", ErrMultipleIndexDrop will be returned +// without running the command because doing so would drop all search indexes. +// +// The opts parameter can be used to specify options for this operation (see the options.DropSearchIndexOptions +// documentation). +func (siv SearchIndexView) DropOne( + ctx context.Context, + name string, + _ ...*options.DropSearchIndexOptions, +) error { + if name == "*" { + return ErrMultipleIndexDrop + } + + if ctx == nil { + ctx = context.Background() + } + + sess := sessionFromContext(ctx) + if sess == nil && siv.coll.client.sessionPool != nil { + sess = session.NewImplicitClientSession(siv.coll.client.sessionPool, siv.coll.client.id) + defer sess.EndSession() + } + + err := siv.coll.client.validSession(sess) + if err != nil { + return err + } + + wc := siv.coll.writeConcern + if sess.TransactionRunning() { + wc = nil + } + if !writeconcern.AckWrite(wc) { + sess = nil + } + + selector := makePinnedSelector(sess, siv.coll.writeSelector) + + op := operation.NewDropSearchIndex(name). + Session(sess).WriteConcern(wc).CommandMonitor(siv.coll.client.monitor). + ServerSelector(selector).ClusterClock(siv.coll.client.clock). + Database(siv.coll.db.name).Collection(siv.coll.name). + Deployment(siv.coll.client.deployment).ServerAPI(siv.coll.client.serverAPI). + Timeout(siv.coll.client.timeout) + + err = op.Execute(ctx) + if de, ok := err.(driver.Error); ok && de.NamespaceNotFound() { + return nil + } + return err +} + +// UpdateOne executes a updateSearchIndex operation to update a search index on the collection. +// +// The name parameter should be the name of the search index to update. +// +// The definition parameter is a document describing the definition for the search index. It cannot be nil. +// +// The opts parameter can be used to specify options for this operation (see the options.UpdateSearchIndexOptions +// documentation). +func (siv SearchIndexView) UpdateOne( + ctx context.Context, + name string, + definition interface{}, + _ ...*options.UpdateSearchIndexOptions, +) error { + if definition == nil { + return fmt.Errorf("search index definition cannot be nil") + } + + indexDefinition, err := marshal(definition, siv.coll.bsonOpts, siv.coll.registry) + if err != nil { + return err + } + + if ctx == nil { + ctx = context.Background() + } + + sess := sessionFromContext(ctx) + if sess == nil && siv.coll.client.sessionPool != nil { + sess = session.NewImplicitClientSession(siv.coll.client.sessionPool, siv.coll.client.id) + defer sess.EndSession() + } + + err = siv.coll.client.validSession(sess) + if err != nil { + return err + } + + wc := siv.coll.writeConcern + if sess.TransactionRunning() { + wc = nil + } + if !writeconcern.AckWrite(wc) { + sess = nil + } + + selector := makePinnedSelector(sess, siv.coll.writeSelector) + + op := operation.NewUpdateSearchIndex(name, indexDefinition). + Session(sess).WriteConcern(wc).CommandMonitor(siv.coll.client.monitor). + ServerSelector(selector).ClusterClock(siv.coll.client.clock). + Database(siv.coll.db.name).Collection(siv.coll.name). + Deployment(siv.coll.client.deployment).ServerAPI(siv.coll.client.serverAPI). + Timeout(siv.coll.client.timeout) + + return op.Execute(ctx) +} diff --git a/vendor/go.mongodb.org/mongo-driver/mongo/session.go b/vendor/go.mongodb.org/mongo-driver/mongo/session.go index 766e07f39..8f1e029b9 100644 --- a/vendor/go.mongodb.org/mongo-driver/mongo/session.go +++ b/vendor/go.mongodb.org/mongo-driver/mongo/session.go @@ -13,7 +13,6 @@ import ( "go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/bson/primitive" - "go.mongodb.org/mongo-driver/internal" "go.mongodb.org/mongo-driver/mongo/description" "go.mongodb.org/mongo-driver/mongo/options" "go.mongodb.org/mongo-driver/x/bsonx/bsoncore" @@ -196,7 +195,7 @@ func (s *sessionImpl) WithTransaction(ctx context.Context, fn func(ctx SessionCo if s.clientSession.TransactionRunning() { // Wrap the user-provided Context in a new one that behaves like context.Background() for deadlines and // cancellations, but forwards Value requests to the original one. - _ = s.AbortTransaction(internal.NewBackgroundContext(ctx)) + _ = s.AbortTransaction(newBackgroundContext(ctx)) } select { @@ -228,13 +227,13 @@ func (s *sessionImpl) WithTransaction(ctx context.Context, fn func(ctx SessionCo if ctx.Err() != nil { // Wrap the user-provided Context in a new one that behaves like context.Background() for deadlines and // cancellations, but forwards Value requests to the original one. - _ = s.AbortTransaction(internal.NewBackgroundContext(ctx)) + _ = s.AbortTransaction(newBackgroundContext(ctx)) return nil, ctx.Err() } CommitLoop: for { - err = s.CommitTransaction(ctx) + err = s.CommitTransaction(newBackgroundContext(ctx)) // End when error is nil, as transaction has been committed. if err == nil { return res, nil diff --git a/vendor/go.mongodb.org/mongo-driver/mongo/single_result.go b/vendor/go.mongodb.org/mongo-driver/mongo/single_result.go index 9c9b4f4fc..f6ed4dc88 100644 --- a/vendor/go.mongodb.org/mongo-driver/mongo/single_result.go +++ b/vendor/go.mongodb.org/mongo-driver/mongo/single_result.go @@ -83,10 +83,11 @@ func (sr *SingleResult) Decode(v interface{}) error { return dec.Decode(v) } -// DecodeBytes will return the document represented by this SingleResult as a bson.Raw. If there was an error from the -// operation that created this SingleResult, both the result and that error will be returned. If the operation returned -// no documents, this will return (nil, ErrNoDocuments). -func (sr *SingleResult) DecodeBytes() (bson.Raw, error) { +// Raw returns the document represented by this SingleResult as a bson.Raw. If +// there was an error from the operation that created this SingleResult, both +// the result and that error will be returned. If the operation returned no +// documents, this will return (nil, ErrNoDocuments). +func (sr *SingleResult) Raw() (bson.Raw, error) { if sr.err != nil { return sr.rdr, sr.err } @@ -97,6 +98,15 @@ func (sr *SingleResult) DecodeBytes() (bson.Raw, error) { return sr.rdr, nil } +// DecodeBytes will return the document represented by this SingleResult as a bson.Raw. If there was an error from the +// operation that created this SingleResult, both the result and that error will be returned. If the operation returned +// no documents, this will return (nil, ErrNoDocuments). +// +// Deprecated: Use [SingleResult.Raw] instead. +func (sr *SingleResult) DecodeBytes() (bson.Raw, error) { + return sr.Raw() +} + // setRdrContents will set the contents of rdr by iterating the underlying cursor if necessary. func (sr *SingleResult) setRdrContents() error { switch { diff --git a/vendor/go.mongodb.org/mongo-driver/version/version.go b/vendor/go.mongodb.org/mongo-driver/version/version.go index 80d4fb9d5..4bd1f9b5e 100644 --- a/vendor/go.mongodb.org/mongo-driver/version/version.go +++ b/vendor/go.mongodb.org/mongo-driver/version/version.go @@ -8,4 +8,4 @@ package version // import "go.mongodb.org/mongo-driver/version" // Driver is the current version of the driver. -var Driver = "v1.12.1" +var Driver = "v1.13.0" diff --git a/vendor/go.mongodb.org/mongo-driver/x/bsonx/bsoncore/bsoncore.go b/vendor/go.mongodb.org/mongo-driver/x/bsonx/bsoncore/bsoncore.go index e52674aac..88133293e 100644 --- a/vendor/go.mongodb.org/mongo-driver/x/bsonx/bsoncore/bsoncore.go +++ b/vendor/go.mongodb.org/mongo-driver/x/bsonx/bsoncore/bsoncore.go @@ -235,7 +235,7 @@ func BuildDocumentValue(elems ...[]byte) Value { return Value{Type: bsontype.EmbeddedDocument, Data: BuildDocument(nil, elems...)} } -// BuildDocumentElement will append a BSON embedded document elemnt using key and the provided +// BuildDocumentElement will append a BSON embedded document element using key and the provided // elements and return the extended buffer. func BuildDocumentElement(dst []byte, key string, elems ...[]byte) []byte { return BuildDocument(AppendHeader(dst, bsontype.EmbeddedDocument, key), elems...) diff --git a/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/DESIGN.md b/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/DESIGN.md index 2fde89f81..3c3e6c56c 100644 --- a/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/DESIGN.md +++ b/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/DESIGN.md @@ -1,12 +1,15 @@ # Driver Library Design + This document outlines the design for this package. ## Deployment, Server, and Connection + Acquiring a `Connection` from a `Server` selected from a `Deployment` enables sending and receiving wire messages. A `Deployment` represents an set of MongoDB servers and a `Server` represents a member of that set. These three types form the operation execution stack. ### Compression + Compression is handled by Connection type while uncompression is handled automatically by the Operation type. This is done because the compressor to use for compressing a wire message is chosen by the connection during handshake, while uncompression can be performed without this @@ -14,6 +17,7 @@ information. This does make the design of compression non-symmetric, but it make to implement and more consistent. ## Operation + The `Operation` type handles executing a series of commands using a `Deployment`. For most uses `Operation` will only execute a single command, but the main use case for a series of commands is batch split write commands, such as insert. The type itself is heavily documented, so reading the diff --git a/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/auth/creds/gcpcreds.go b/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/auth/creds/gcpcreds.go index f5b06ff97..74f352e36 100644 --- a/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/auth/creds/gcpcreds.go +++ b/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/auth/creds/gcpcreds.go @@ -14,7 +14,6 @@ import ( "net/http" "os" - "go.mongodb.org/mongo-driver/internal" "go.mongodb.org/mongo-driver/x/bsonx/bsoncore" ) @@ -37,20 +36,23 @@ func (p GCPCredentialProvider) GetCredentialsDoc(ctx context.Context) (bsoncore. url := fmt.Sprintf("http://%s/computeMetadata/v1/instance/service-accounts/default/token", metadataHost) req, err := http.NewRequest(http.MethodGet, url, nil) if err != nil { - return nil, internal.WrapErrorf(err, "unable to retrieve GCP credentials") + return nil, fmt.Errorf("unable to retrieve GCP credentials: %w", err) } req.Header.Set("Metadata-Flavor", "Google") resp, err := p.httpClient.Do(req.WithContext(ctx)) if err != nil { - return nil, internal.WrapErrorf(err, "unable to retrieve GCP credentials") + return nil, fmt.Errorf("unable to retrieve GCP credentials: %w", err) } defer resp.Body.Close() body, err := ioutil.ReadAll(resp.Body) if err != nil { - return nil, internal.WrapErrorf(err, "unable to retrieve GCP credentials: error reading response body") + return nil, fmt.Errorf("unable to retrieve GCP credentials: error reading response body: %w", err) } if resp.StatusCode != http.StatusOK { - return nil, internal.WrapErrorf(err, "unable to retrieve GCP credentials: expected StatusCode 200, got StatusCode: %v. Response body: %s", resp.StatusCode, body) + return nil, fmt.Errorf( + "unable to retrieve GCP credentials: expected StatusCode 200, got StatusCode: %v. Response body: %s", + resp.StatusCode, + body) } var tokenResponse struct { AccessToken string `json:"access_token"` @@ -58,7 +60,10 @@ func (p GCPCredentialProvider) GetCredentialsDoc(ctx context.Context) (bsoncore. // Attempt to read body as JSON err = json.Unmarshal(body, &tokenResponse) if err != nil { - return nil, internal.WrapErrorf(err, "unable to retrieve GCP credentials: error reading body JSON. Response body: %s", body) + return nil, fmt.Errorf( + "unable to retrieve GCP credentials: error reading body JSON: %w (response body: %s)", + err, + body) } if tokenResponse.AccessToken == "" { return nil, fmt.Errorf("unable to retrieve GCP credentials: got unexpected empty accessToken from GCP Metadata Server. Response body: %s", body) diff --git a/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/auth/default.go b/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/auth/default.go index e266ad542..6f2ca5224 100644 --- a/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/auth/default.go +++ b/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/auth/default.go @@ -9,8 +9,6 @@ package auth import ( "context" "fmt" - - "go.mongodb.org/mongo-driver/mongo/description" ) func newDefaultAuthenticator(cred *Cred) (Authenticator, error) { @@ -78,21 +76,7 @@ func chooseAuthMechanism(cfg *Config) string { return v } } - return SCRAMSHA1 - } - - if err := scramSHA1Supported(cfg.HandshakeInfo.Description.WireVersion); err == nil { - return SCRAMSHA1 - } - - return MONGODBCR -} - -// scramSHA1Supported returns an error if the given server version does not support scram-sha-1. -func scramSHA1Supported(wireVersion *description.VersionRange) error { - if wireVersion != nil && wireVersion.Max < 3 { - return fmt.Errorf("SCRAM-SHA-1 is only supported for servers 3.0 or newer") } - return nil + return SCRAMSHA1 } diff --git a/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/auth/internal/gssapi/gss_wrapper.c b/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/auth/internal/gssapi/gss_wrapper.c index ec49d9612..68b725414 100644 --- a/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/auth/internal/gssapi/gss_wrapper.c +++ b/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/auth/internal/gssapi/gss_wrapper.c @@ -12,9 +12,9 @@ #include "gss_wrapper.h" OM_uint32 gssapi_canonicalize_name( - OM_uint32* minor_status, - char *input_name, - gss_OID input_name_type, + OM_uint32* minor_status, + char *input_name, + gss_OID input_name_type, gss_name_t *output_name ) { @@ -39,8 +39,8 @@ OM_uint32 gssapi_canonicalize_name( } int gssapi_error_desc( - OM_uint32 maj_stat, - OM_uint32 min_stat, + OM_uint32 maj_stat, + OM_uint32 min_stat, char **desc ) { @@ -207,7 +207,7 @@ int gssapi_client_wrap_msg( void* input, size_t input_length, void** output, - size_t* output_length + size_t* output_length ) { gss_buffer_desc input_buffer = GSS_C_EMPTY_BUFFER; diff --git a/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/auth/internal/gssapi/gss_wrapper.h b/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/auth/internal/gssapi/gss_wrapper.h index 1cb9cd3c1..a105ba58b 100644 --- a/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/auth/internal/gssapi/gss_wrapper.h +++ b/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/auth/internal/gssapi/gss_wrapper.h @@ -32,8 +32,8 @@ typedef struct { } gssapi_client_state; int gssapi_error_desc( - OM_uint32 maj_stat, - OM_uint32 min_stat, + OM_uint32 maj_stat, + OM_uint32 min_stat, char **desc ); @@ -62,11 +62,11 @@ int gssapi_client_wrap_msg( void* input, size_t input_length, void** output, - size_t* output_length + size_t* output_length ); int gssapi_client_destroy( gssapi_client_state *client ); -#endif \ No newline at end of file +#endif diff --git a/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/auth/internal/gssapi/sspi.go b/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/auth/internal/gssapi/sspi.go index 36e9633f8..6e7d3ed8a 100644 --- a/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/auth/internal/gssapi/sspi.go +++ b/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/auth/internal/gssapi/sspi.go @@ -112,7 +112,7 @@ func (sc *SaslClient) Start() (string, []byte, error) { status := C.sspi_client_init(&sc.state, cusername, cpassword) if status != C.SSPI_OK { - return mechName, nil, sc.getError("unable to intitialize client") + return mechName, nil, sc.getError("unable to initialize client") } payload, err := sc.Next(nil) diff --git a/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/auth/internal/gssapi/sspi_wrapper.c b/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/auth/internal/gssapi/sspi_wrapper.c index f65565471..bc73723e8 100644 --- a/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/auth/internal/gssapi/sspi_wrapper.c +++ b/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/auth/internal/gssapi/sspi_wrapper.c @@ -69,7 +69,7 @@ int sspi_client_init( if (username) { if (password) { SEC_WINNT_AUTH_IDENTITY auth_identity; - + #ifdef _UNICODE auth_identity.Flags = SEC_WINNT_AUTH_IDENTITY_UNICODE; #else @@ -186,7 +186,7 @@ int sspi_client_wrap_msg( PVOID input, ULONG input_length, PVOID* output, - ULONG* output_length + ULONG* output_length ) { SecPkgContext_Sizes sizes; @@ -246,4 +246,4 @@ int sspi_client_destroy( sspi_functions->FreeCredentialsHandle(&client->cred); return SSPI_OK; -} \ No newline at end of file +} diff --git a/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/auth/internal/gssapi/sspi_wrapper.h b/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/auth/internal/gssapi/sspi_wrapper.h index 2d08e939e..e59e55c69 100644 --- a/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/auth/internal/gssapi/sspi_wrapper.h +++ b/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/auth/internal/gssapi/sspi_wrapper.h @@ -54,11 +54,11 @@ int sspi_client_wrap_msg( PVOID input, ULONG input_length, PVOID* output, - ULONG* output_length + ULONG* output_length ); int sspi_client_destroy( sspi_client_state *client ); -#endif \ No newline at end of file +#endif diff --git a/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/auth/x509.go b/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/auth/x509.go index e0a61eda8..03a9d750e 100644 --- a/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/auth/x509.go +++ b/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/auth/x509.go @@ -9,7 +9,6 @@ package auth import ( "context" - "go.mongodb.org/mongo-driver/mongo/description" "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" @@ -37,22 +36,16 @@ var _ SpeculativeConversation = (*x509Conversation)(nil) // FirstMessage returns the first message to be sent to the server. func (c *x509Conversation) FirstMessage() (bsoncore.Document, error) { - return createFirstX509Message(description.Server{}, ""), nil + return createFirstX509Message(), nil } // createFirstX509Message creates the first message for the X509 conversation. -func createFirstX509Message(desc description.Server, user string) bsoncore.Document { +func createFirstX509Message() bsoncore.Document { elements := [][]byte{ bsoncore.AppendInt32Element(nil, "authenticate", 1), bsoncore.AppendStringElement(nil, "mechanism", MongoDBX509), } - // Server versions < 3.4 require the username to be included in the message. Versions >= 3.4 will extract the - // username from the certificate. - if desc.WireVersion != nil && desc.WireVersion.Max < 5 { - elements = append(elements, bsoncore.AppendStringElement(nil, "user", user)) - } - return bsoncore.BuildDocument(nil, elements...) } @@ -69,7 +62,7 @@ func (a *MongoDBX509Authenticator) CreateSpeculativeConversation() (SpeculativeC // Auth authenticates the provided connection by conducting an X509 authentication conversation. func (a *MongoDBX509Authenticator) Auth(ctx context.Context, cfg *Config) error { - requestDoc := createFirstX509Message(cfg.Description, a.User) + requestDoc := createFirstX509Message() authCmd := operation. NewCommand(requestDoc). Database("$external"). diff --git a/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/batch_cursor.go b/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/batch_cursor.go index a3f21f96c..fefcfdb47 100644 --- a/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/batch_cursor.go +++ b/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/batch_cursor.go @@ -10,22 +10,31 @@ import ( "context" "errors" "fmt" + "io" "strings" + "time" + "go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/bson/bsontype" "go.mongodb.org/mongo-driver/event" - "go.mongodb.org/mongo-driver/internal" + "go.mongodb.org/mongo-driver/internal/codecutil" + "go.mongodb.org/mongo-driver/internal/csot" "go.mongodb.org/mongo-driver/mongo/description" "go.mongodb.org/mongo-driver/x/bsonx/bsoncore" "go.mongodb.org/mongo-driver/x/mongo/driver/session" ) +// ErrNoCursor is returned by NewCursorResponse when the database response does +// not contain a cursor. +var ErrNoCursor = errors.New("database response does not contain a cursor") + // BatchCursor is a batch implementation of a cursor. It returns documents in entire batches instead // of one at a time. An individual document cursor can be built on top of this batch cursor. type BatchCursor struct { clientSession *session.Client clock *session.ClusterClock - comment bsoncore.Value + comment interface{} + encoderFn codecutil.EncoderFn database string collection string id int64 @@ -62,17 +71,27 @@ type CursorResponse struct { postBatchResumeToken bsoncore.Document } -// NewCursorResponse constructs a cursor response from the given response and server. This method -// can be used within the ProcessResponse method for an operation. +// NewCursorResponse constructs a cursor response from the given response and +// server. If the provided database response does not contain a cursor, it +// returns ErrNoCursor. +// +// NewCursorResponse can be used within the ProcessResponse method for an operation. func NewCursorResponse(info ResponseInfo) (CursorResponse, error) { response := info.ServerResponse - cur, ok := response.Lookup("cursor").DocumentOK() + cur, err := response.LookupErr("cursor") + if err == bsoncore.ErrElementNotFound { + return CursorResponse{}, ErrNoCursor + } + if err != nil { + return CursorResponse{}, fmt.Errorf("error getting cursor from database response: %w", err) + } + curDoc, ok := cur.DocumentOK() if !ok { - return CursorResponse{}, fmt.Errorf("cursor should be an embedded document but is of BSON type %s", response.Lookup("cursor").Type) + return CursorResponse{}, fmt.Errorf("cursor should be an embedded document but is BSON type %s", cur.Type) } - elems, err := cur.Elements() + elems, err := curDoc.Elements() if err != nil { - return CursorResponse{}, err + return CursorResponse{}, fmt.Errorf("error getting elements from cursor: %w", err) } curresp := CursorResponse{Server: info.Server, Desc: info.ConnectionDescription} @@ -133,13 +152,14 @@ func NewCursorResponse(info ResponseInfo) (CursorResponse, error) { // CursorOptions are extra options that are required to construct a BatchCursor. type CursorOptions struct { - BatchSize int32 - Comment bsoncore.Value - MaxTimeMS int64 - Limit int32 - CommandMonitor *event.CommandMonitor - Crypt Crypt - ServerAPI *ServerAPIOptions + BatchSize int32 + Comment bsoncore.Value + MaxTimeMS int64 + Limit int32 + CommandMonitor *event.CommandMonitor + Crypt Crypt + ServerAPI *ServerAPIOptions + MarshalValueEncoderFn func(io.Writer) (*bson.Encoder, error) } // NewBatchCursor creates a new BatchCursor from the provided parameters. @@ -163,12 +183,13 @@ func NewBatchCursor(cr CursorResponse, clientSession *session.Client, clock *ses crypt: opts.Crypt, serverAPI: opts.ServerAPI, serverDescription: cr.Desc, + encoderFn: opts.MarshalValueEncoderFn, } if ds != nil { bc.numReturned = int32(ds.DocumentCount()) } - if cr.Desc.WireVersion == nil || cr.Desc.WireVersion.Max < 4 { + if cr.Desc.WireVersion == nil { bc.limit = opts.Limit // Take as many documents from the batch as needed. @@ -305,6 +326,12 @@ func (bc *BatchCursor) KillCursor(ctx context.Context) error { Legacy: LegacyKillCursors, CommandMonitor: bc.cmdMonitor, ServerAPI: bc.serverAPI, + + // No read preference is passed to the killCursor command, + // resulting in the default read preference: "primaryPreferred". + // Since this could be confusing, and there is no requirement + // to use a read preference here, we omit it. + omitReadPreference: true, }.Execute(ctx) } @@ -351,10 +378,17 @@ func (bc *BatchCursor) getMore(ctx context.Context) { if bc.maxTimeMS > 0 { dst = bsoncore.AppendInt64Element(dst, "maxTimeMS", bc.maxTimeMS) } + + comment, err := codecutil.MarshalValue(bc.comment, bc.encoderFn) + if err != nil { + return nil, fmt.Errorf("error marshaling comment as a BSON value: %w", err) + } + // The getMore command does not support commenting pre-4.4. - if bc.comment.Type != bsontype.Type(0) && bc.serverDescription.WireVersion.Max >= 9 { - dst = bsoncore.AppendValueElement(dst, "comment", bc.comment) + if comment.Type != bsontype.Type(0) && bc.serverDescription.WireVersion.Max >= 9 { + dst = bsoncore.AppendValueElement(dst, "comment", comment) } + return dst, nil }, Database: bc.database, @@ -398,6 +432,12 @@ func (bc *BatchCursor) getMore(ctx context.Context) { CommandMonitor: bc.cmdMonitor, Crypt: bc.crypt, ServerAPI: bc.serverAPI, + + // No read preference is passed to the getMore command, + // resulting in the default read preference: "primaryPreferred". + // Since this could be confusing, and there is no requirement + // to use a read preference here, we omit it. + omitReadPreference: true, }.Execute(ctx) // Once the cursor has been drained, we can unpin the connection if one is currently pinned. @@ -430,11 +470,26 @@ func (bc *BatchCursor) PostBatchResumeToken() bsoncore.Document { return bc.postBatchResumeToken } -// SetBatchSize sets the batchSize for future getMores. +// SetBatchSize sets the batchSize for future getMore operations. func (bc *BatchCursor) SetBatchSize(size int32) { bc.batchSize = size } +// SetMaxTime will set the maximum amount of time the server will allow the +// operations to execute. The server will error if this field is set but the +// cursor is not configured with awaitData=true. +// +// The time.Duration value passed by this setter will be converted and rounded +// down to the nearest millisecond. +func (bc *BatchCursor) SetMaxTime(dur time.Duration) { + bc.maxTimeMS = int64(dur / time.Millisecond) +} + +// SetComment sets the comment for future getMore operations. +func (bc *BatchCursor) SetComment(comment interface{}) { + bc.comment = comment +} + func (bc *BatchCursor) getOperationDeployment() Deployment { if bc.connection != nil { return &loadBalancedCursorDeployment{ @@ -471,7 +526,7 @@ func (lbcd *loadBalancedCursorDeployment) Connection(_ context.Context) (Connect // RTTMonitor implements the driver.Server interface. func (lbcd *loadBalancedCursorDeployment) RTTMonitor() RTTMonitor { - return &internal.ZeroRTTMonitor{} + return &csot.ZeroRTTMonitor{} } func (lbcd *loadBalancedCursorDeployment) ProcessError(err error, conn Connection) ProcessErrorResult { diff --git a/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/batches.go b/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/batches.go index 3e7dca9ac..be430afa1 100644 --- a/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/batches.go +++ b/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/batches.go @@ -17,7 +17,7 @@ import ( var ErrDocumentTooLarge = errors.New("an inserted document is too large") // Batches contains the necessary information to batch split an operation. This is only used for write -// oeprations. +// operations. type Batches struct { Identifier string Documents []bsoncore.Document diff --git a/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/compression.go b/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/compression.go index 7f355f61a..d79b024b7 100644 --- a/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/compression.go +++ b/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/compression.go @@ -26,48 +26,72 @@ type CompressionOpts struct { UncompressedSize int32 } -var zstdEncoders sync.Map // map[zstd.EncoderLevel]*zstd.Encoder +// mustZstdNewWriter creates a zstd.Encoder with the given level and a nil +// destination writer. It panics on any errors and should only be used at +// package initialization time. +func mustZstdNewWriter(lvl zstd.EncoderLevel) *zstd.Encoder { + enc, err := zstd.NewWriter(nil, zstd.WithEncoderLevel(lvl)) + if err != nil { + panic(err) + } + return enc +} + +var zstdEncoders = [zstd.SpeedBestCompression + 1]*zstd.Encoder{ + 0: nil, // zstd.speedNotSet + zstd.SpeedFastest: mustZstdNewWriter(zstd.SpeedFastest), + zstd.SpeedDefault: mustZstdNewWriter(zstd.SpeedDefault), + zstd.SpeedBetterCompression: mustZstdNewWriter(zstd.SpeedBetterCompression), + zstd.SpeedBestCompression: mustZstdNewWriter(zstd.SpeedBestCompression), +} func getZstdEncoder(level zstd.EncoderLevel) (*zstd.Encoder, error) { - if v, ok := zstdEncoders.Load(level); ok { - return v.(*zstd.Encoder), nil - } - encoder, err := zstd.NewWriter(nil, zstd.WithEncoderLevel(level)) - if err != nil { - return nil, err + if zstd.SpeedFastest <= level && level <= zstd.SpeedBestCompression { + return zstdEncoders[level], nil } - zstdEncoders.Store(level, encoder) - return encoder, nil + // The level is outside the expected range, return an error. + return nil, fmt.Errorf("invalid zstd compression level: %d", level) } -var zlibEncoders sync.Map // map[int /*level*/]*zlibEncoder +// zlibEncodersOffset is the offset into the zlibEncoders array for a given +// compression level. +const zlibEncodersOffset = -zlib.HuffmanOnly // HuffmanOnly == -2 + +var zlibEncoders [zlib.BestCompression + zlibEncodersOffset + 1]sync.Pool func getZlibEncoder(level int) (*zlibEncoder, error) { - if v, ok := zlibEncoders.Load(level); ok { - return v.(*zlibEncoder), nil - } - writer, err := zlib.NewWriterLevel(nil, level) - if err != nil { - return nil, err + if zlib.HuffmanOnly <= level && level <= zlib.BestCompression { + if enc, _ := zlibEncoders[level+zlibEncodersOffset].Get().(*zlibEncoder); enc != nil { + return enc, nil + } + writer, err := zlib.NewWriterLevel(nil, level) + if err != nil { + return nil, err + } + enc := &zlibEncoder{writer: writer, level: level} + return enc, nil } - encoder := &zlibEncoder{writer: writer, buf: new(bytes.Buffer)} - zlibEncoders.Store(level, encoder) + // The level is outside the expected range, return an error. + return nil, fmt.Errorf("invalid zlib compression level: %d", level) +} - return encoder, nil +func putZlibEncoder(enc *zlibEncoder) { + if enc != nil { + zlibEncoders[enc.level+zlibEncodersOffset].Put(enc) + } } type zlibEncoder struct { - mu sync.Mutex writer *zlib.Writer - buf *bytes.Buffer + buf bytes.Buffer + level int } func (e *zlibEncoder) Encode(dst, src []byte) ([]byte, error) { - e.mu.Lock() - defer e.mu.Unlock() + defer putZlibEncoder(e) e.buf.Reset() - e.writer.Reset(e.buf) + e.writer.Reset(&e.buf) _, err := e.writer.Write(src) if err != nil { @@ -105,8 +129,15 @@ func CompressPayload(in []byte, opts CompressionOpts) ([]byte, error) { } } +var zstdReaderPool = sync.Pool{ + New: func() interface{} { + r, _ := zstd.NewReader(nil) + return r + }, +} + // DecompressPayload takes a byte slice that has been compressed and undoes it according to the options passed -func DecompressPayload(in []byte, opts CompressionOpts) (uncompressed []byte, err error) { +func DecompressPayload(in []byte, opts CompressionOpts) ([]byte, error) { switch opts.Compressor { case wiremessage.CompressorNoOp: return in, nil @@ -117,34 +148,29 @@ func DecompressPayload(in []byte, opts CompressionOpts) (uncompressed []byte, er } else if int32(l) != opts.UncompressedSize { return nil, fmt.Errorf("unexpected decompression size, expected %v but got %v", opts.UncompressedSize, l) } - uncompressed = make([]byte, opts.UncompressedSize) - return snappy.Decode(uncompressed, in) + out := make([]byte, opts.UncompressedSize) + return snappy.Decode(out, in) case wiremessage.CompressorZLib: r, err := zlib.NewReader(bytes.NewReader(in)) if err != nil { return nil, err } - defer func() { - err = r.Close() - }() - uncompressed = make([]byte, opts.UncompressedSize) - _, err = io.ReadFull(r, uncompressed) - if err != nil { + out := make([]byte, opts.UncompressedSize) + if _, err := io.ReadFull(r, out); err != nil { return nil, err } - return uncompressed, nil - case wiremessage.CompressorZstd: - r, err := zstd.NewReader(bytes.NewBuffer(in)) - if err != nil { - return nil, err - } - defer r.Close() - uncompressed = make([]byte, opts.UncompressedSize) - _, err = io.ReadFull(r, uncompressed) - if err != nil { + if err := r.Close(); err != nil { return nil, err } - return uncompressed, nil + return out, nil + case wiremessage.CompressorZstd: + buf := make([]byte, 0, opts.UncompressedSize) + // Using a pool here is about ~20% faster + // than using a single global zstd.Reader + r := zstdReaderPool.Get().(*zstd.Decoder) + out, err := r.DecodeAll(in, buf) + zstdReaderPool.Put(r) + return out, err default: return nil, fmt.Errorf("unknown compressor ID %v", opts.Compressor) } diff --git a/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/connstring/connstring.go b/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/connstring/connstring.go index 6f03a5857..cd4313647 100644 --- a/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/connstring/connstring.go +++ b/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/connstring/connstring.go @@ -15,13 +15,59 @@ import ( "strings" "time" - "go.mongodb.org/mongo-driver/internal" "go.mongodb.org/mongo-driver/internal/randutil" "go.mongodb.org/mongo-driver/mongo/writeconcern" "go.mongodb.org/mongo-driver/x/mongo/driver/dns" "go.mongodb.org/mongo-driver/x/mongo/driver/wiremessage" ) +const ( + // ServerMonitoringModeAuto indicates that the client will behave like "poll" + // mode when running on a FaaS (Function as a Service) platform, or like + // "stream" mode otherwise. The client detects its execution environment by + // following the rules for generating the "client.env" handshake metadata field + // as specified in the MongoDB Handshake specification. This is the default + // mode. + ServerMonitoringModeAuto = "auto" + + // ServerMonitoringModePoll indicates that the client will periodically check + // the server using a hello or legacy hello command and then sleep for + // heartbeatFrequencyMS milliseconds before running another check. + ServerMonitoringModePoll = "poll" + + // ServerMonitoringModeStream indicates that the client will use a streaming + // protocol when the server supports it. The streaming protocol optimally + // reduces the time it takes for a client to discover server state changes. + ServerMonitoringModeStream = "stream" +) + +var ( + // ErrLoadBalancedWithMultipleHosts is returned when loadBalanced=true is + // specified in a URI with multiple hosts. + ErrLoadBalancedWithMultipleHosts = errors.New( + "loadBalanced cannot be set to true if multiple hosts are specified") + + // ErrLoadBalancedWithReplicaSet is returned when loadBalanced=true is + // specified in a URI with the replicaSet option. + ErrLoadBalancedWithReplicaSet = errors.New( + "loadBalanced cannot be set to true if a replica set name is specified") + + // ErrLoadBalancedWithDirectConnection is returned when loadBalanced=true is + // specified in a URI with the directConnection option. + ErrLoadBalancedWithDirectConnection = errors.New( + "loadBalanced cannot be set to true if the direct connection option is specified") + + // ErrSRVMaxHostsWithReplicaSet is returned when srvMaxHosts > 0 is + // specified in a URI with the replicaSet option. + ErrSRVMaxHostsWithReplicaSet = errors.New( + "srvMaxHosts cannot be a positive value if a replica set name is specified") + + // ErrSRVMaxHostsWithLoadBalanced is returned when srvMaxHosts > 0 is + // specified in a URI with loadBalanced=true. + ErrSRVMaxHostsWithLoadBalanced = errors.New( + "srvMaxHosts cannot be a positive value if loadBalanced is set to true") +) + // random is a package-global pseudo-random number generator. var random = randutil.NewLockedRand() @@ -31,11 +77,11 @@ func ParseAndValidate(s string) (ConnString, error) { p := parser{dnsResolver: dns.DefaultResolver} err := p.parse(s) if err != nil { - return p.ConnString, internal.WrapErrorf(err, "error parsing uri") + return p.ConnString, fmt.Errorf("error parsing uri: %w", err) } err = p.ConnString.Validate() if err != nil { - return p.ConnString, internal.WrapErrorf(err, "error validating uri") + return p.ConnString, fmt.Errorf("error validating uri: %w", err) } return p.ConnString, nil } @@ -47,7 +93,7 @@ func Parse(s string) (ConnString, error) { p := parser{dnsResolver: dns.DefaultResolver} err := p.parse(s) if err != nil { - err = internal.WrapErrorf(err, "error parsing uri") + err = fmt.Errorf("error parsing uri: %w", err) } return p.ConnString, err } @@ -99,6 +145,7 @@ type ConnString struct { MaxStalenessSet bool ReplicaSet string Scheme string + ServerMonitoringMode string ServerSelectionTimeout time.Duration ServerSelectionTimeoutSet bool SocketTimeout time.Duration @@ -213,7 +260,7 @@ func (p *parser) parse(original string) error { // remove the scheme uri = uri[len(SchemeMongoDB)+3:] } else { - return fmt.Errorf("scheme must be \"mongodb\" or \"mongodb+srv\"") + return errors.New(`scheme must be "mongodb" or "mongodb+srv"`) } if idx := strings.Index(uri, "@"); idx != -1 { @@ -235,7 +282,7 @@ func (p *parser) parse(original string) error { } p.Username, err = url.PathUnescape(username) if err != nil { - return internal.WrapErrorf(err, "invalid username") + return fmt.Errorf("invalid username: %w", err) } p.UsernameSet = true @@ -248,7 +295,7 @@ func (p *parser) parse(original string) error { } p.Password, err = url.PathUnescape(password) if err != nil { - return internal.WrapErrorf(err, "invalid password") + return fmt.Errorf("invalid password: %w", err) } } @@ -325,7 +372,7 @@ func (p *parser) parse(original string) error { for _, host := range parsedHosts { err = p.addHost(host) if err != nil { - return internal.WrapErrorf(err, "invalid host %q", host) + return fmt.Errorf("invalid host %q: %w", host, err) } } if len(p.Hosts) == 0 { @@ -371,27 +418,27 @@ func (p *parser) validate() error { return errors.New("a direct connection cannot be made if an SRV URI is used") } if p.LoadBalancedSet && p.LoadBalanced { - return internal.ErrLoadBalancedWithDirectConnection + return ErrLoadBalancedWithDirectConnection } } // Validation for load-balanced mode. if p.LoadBalancedSet && p.LoadBalanced { if len(p.Hosts) > 1 { - return internal.ErrLoadBalancedWithMultipleHosts + return ErrLoadBalancedWithMultipleHosts } if p.ReplicaSet != "" { - return internal.ErrLoadBalancedWithReplicaSet + return ErrLoadBalancedWithReplicaSet } } // Check for invalid use of SRVMaxHosts. if p.SRVMaxHosts > 0 { if p.ReplicaSet != "" { - return internal.ErrSRVMaxHostsWithReplicaSet + return ErrSRVMaxHostsWithReplicaSet } if p.LoadBalanced { - return internal.ErrSRVMaxHostsWithLoadBalanced + return ErrSRVMaxHostsWithLoadBalanced } } @@ -570,7 +617,7 @@ func (p *parser) addHost(host string) error { } host, err := url.QueryUnescape(host) if err != nil { - return internal.WrapErrorf(err, "invalid host %q", host) + return fmt.Errorf("invalid host %q: %w", host, err) } _, port, err := net.SplitHostPort(host) @@ -585,7 +632,7 @@ func (p *parser) addHost(host string) error { if port != "" { d, err := strconv.Atoi(port) if err != nil { - return internal.WrapErrorf(err, "port must be an integer") + return fmt.Errorf("port must be an integer: %w", err) } if d <= 0 || d >= 65536 { return fmt.Errorf("port must be in the range [1, 65535]") @@ -595,6 +642,14 @@ func (p *parser) addHost(host string) error { return nil } +// IsValidServerMonitoringMode will return true if the given string matches a +// valid server monitoring mode. +func IsValidServerMonitoringMode(mode string) bool { + return mode == ServerMonitoringModeAuto || + mode == ServerMonitoringModeStream || + mode == ServerMonitoringModePoll +} + func (p *parser) addOption(pair string) error { kv := strings.SplitN(pair, "=", 2) if len(kv) != 2 || kv[0] == "" { @@ -603,12 +658,12 @@ func (p *parser) addOption(pair string) error { key, err := url.QueryUnescape(kv[0]) if err != nil { - return internal.WrapErrorf(err, "invalid option key %q", kv[0]) + return fmt.Errorf("invalid option key %q: %w", kv[0], err) } value, err := url.QueryUnescape(kv[1]) if err != nil { - return internal.WrapErrorf(err, "invalid option value %q", kv[1]) + return fmt.Errorf("invalid option value %q: %w", kv[1], err) } lowerKey := strings.ToLower(key) @@ -797,6 +852,12 @@ func (p *parser) addOption(pair string) error { } p.RetryReadsSet = true + case "servermonitoringmode": + if !IsValidServerMonitoringMode(value) { + return fmt.Errorf("invalid value for %q: %q", key, value) + } + + p.ServerMonitoringMode = value case "serverselectiontimeoutms": n, err := strconv.Atoi(value) if err != nil || n < 0 { @@ -1024,7 +1085,7 @@ func extractDatabaseFromURI(uri string) (extractedDatabase, error) { escapedDatabase, err := url.QueryUnescape(database) if err != nil { - return extractedDatabase{}, internal.WrapErrorf(err, "invalid database %q", database) + return extractedDatabase{}, fmt.Errorf("invalid database %q: %w", database, err) } uri = uri[len(database):] diff --git a/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/driver.go b/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/driver.go index 38a0a2d13..5fd3ddcb4 100644 --- a/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/driver.go +++ b/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/driver.go @@ -10,7 +10,7 @@ import ( "context" "time" - "go.mongodb.org/mongo-driver/internal" + "go.mongodb.org/mongo-driver/internal/csot" "go.mongodb.org/mongo-driver/mongo/address" "go.mongodb.org/mongo-driver/mongo/description" "go.mongodb.org/mongo-driver/x/bsonx/bsoncore" @@ -89,7 +89,7 @@ type RTTMonitor interface { Stats() string } -var _ RTTMonitor = &internal.ZeroRTTMonitor{} +var _ RTTMonitor = &csot.ZeroRTTMonitor{} // PinnedConnection represents a Connection that can be pinned by one or more cursors or transactions. Implementations // of this interface should maintain the following invariants: @@ -210,21 +210,21 @@ var _ Server = SingleConnectionDeployment{} // SelectServer implements the Deployment interface. This method does not use the // description.SelectedServer provided and instead returns itself. The Connections returned from the // Connection method have a no-op Close method. -func (ssd SingleConnectionDeployment) SelectServer(context.Context, description.ServerSelector) (Server, error) { - return ssd, nil +func (scd SingleConnectionDeployment) SelectServer(context.Context, description.ServerSelector) (Server, error) { + return scd, nil } // Kind implements the Deployment interface. It always returns description.Single. -func (ssd SingleConnectionDeployment) Kind() description.TopologyKind { return description.Single } +func (SingleConnectionDeployment) Kind() description.TopologyKind { return description.Single } // Connection implements the Server interface. It always returns the embedded connection. -func (ssd SingleConnectionDeployment) Connection(context.Context) (Connection, error) { - return ssd.C, nil +func (scd SingleConnectionDeployment) Connection(context.Context) (Connection, error) { + return scd.C, nil } // RTTMonitor implements the driver.Server interface. -func (ssd SingleConnectionDeployment) RTTMonitor() RTTMonitor { - return &internal.ZeroRTTMonitor{} +func (scd SingleConnectionDeployment) RTTMonitor() RTTMonitor { + return &csot.ZeroRTTMonitor{} } // TODO(GODRIVER-617): We can likely use 1 type for both the Type and the RetryMode by using 2 bits for the mode and 1 diff --git a/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/errors.go b/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/errors.go index cb56b84f5..3b8b9823b 100644 --- a/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/errors.go +++ b/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/errors.go @@ -8,16 +8,21 @@ package driver import ( "bytes" + "context" "errors" "fmt" "strings" "go.mongodb.org/mongo-driver/bson" - "go.mongodb.org/mongo-driver/internal" "go.mongodb.org/mongo-driver/mongo/description" "go.mongodb.org/mongo-driver/x/bsonx/bsoncore" ) +// LegacyNotPrimaryErrMsg is the error message that older MongoDB servers (see +// SERVER-50412 for versions) return when a write operation is erroneously sent +// to a non-primary node. +const LegacyNotPrimaryErrMsg = "not master" + var ( retryableCodes = []int32{11600, 11602, 10107, 13435, 13436, 189, 91, 7, 6, 89, 9001, 262} nodeIsRecoveringCodes = []int32{11600, 11602, 13436, 189, 91} @@ -35,7 +40,7 @@ var ( TransientTransactionError = "TransientTransactionError" // NetworkError is an error label for network errors. NetworkError = "NetworkError" - // RetryableWriteError is an error lable for retryable write errors. + // RetryableWriteError is an error label for retryable write errors. RetryableWriteError = "RetryableWriteError" // NoWritesPerformed is an error label indicated that no writes were performed for an operation. NoWritesPerformed = "NoWritesPerformed" @@ -47,9 +52,12 @@ var ( // ErrUnsupportedStorageEngine is returned when a retryable write is attempted against a server // that uses a storage engine that does not support retryable writes ErrUnsupportedStorageEngine = errors.New("this MongoDB deployment does not support retryable writes. Please add retryWrites=false to your connection string") - // ErrDeadlineWouldBeExceeded is returned when a Timeout set on an operation would be exceeded - // if the operation were sent to the server. - ErrDeadlineWouldBeExceeded = errors.New("operation not sent to server, as Timeout would be exceeded") + // ErrDeadlineWouldBeExceeded is returned when a Timeout set on an operation + // would be exceeded if the operation were sent to the server. It wraps + // context.DeadlineExceeded. + ErrDeadlineWouldBeExceeded = fmt.Errorf( + "operation not sent to server, as Timeout would be exceeded: %w", + context.DeadlineExceeded) // ErrNegativeMaxTime is returned when MaxTime on an operation is a negative value. ErrNegativeMaxTime = errors.New("a negative value was provided for MaxTime on an operation") ) @@ -206,7 +214,7 @@ func (wce WriteConcernError) NotPrimary() bool { } } hasNoCode := wce.Code == 0 - return hasNoCode && strings.Contains(wce.Message, internal.LegacyNotPrimary) + return hasNoCode && strings.Contains(wce.Message, LegacyNotPrimaryErrMsg) } // WriteError is a non-write concern failure that occurred as a result of a write @@ -256,10 +264,15 @@ func (e Error) UnsupportedStorageEngine() bool { // Error implements the error interface. func (e Error) Error() string { + var msg string if e.Name != "" { - return fmt.Sprintf("(%v) %v", e.Name, e.Message) + msg = fmt.Sprintf("(%v)", e.Name) } - return e.Message + msg += " " + e.Message + if e.Wrapped != nil { + msg += ": " + e.Wrapped.Error() + } + return msg } // Unwrap returns the underlying error. @@ -354,7 +367,7 @@ func (e Error) NotPrimary() bool { } } hasNoCode := e.Code == 0 - return hasNoCode && strings.Contains(e.Message, internal.LegacyNotPrimary) + return hasNoCode && strings.Contains(e.Message, LegacyNotPrimaryErrMsg) } // NamespaceNotFound returns true if this errors is a NamespaceNotFound error. @@ -392,6 +405,10 @@ func ExtractErrorFromServerResponse(doc bsoncore.Document) error { if elem.Value().Double() == 1 { ok = true } + case bson.TypeBoolean: + if elem.Value().Boolean() { + ok = true + } } case "errmsg": if str, okay := elem.Value().StringValueOK(); okay { diff --git a/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/legacy.go b/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/legacy.go index 9f3b8a39a..c40f1f809 100644 --- a/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/legacy.go +++ b/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/legacy.go @@ -19,4 +19,5 @@ const ( LegacyKillCursors LegacyListCollections LegacyListIndexes + LegacyHandshake ) diff --git a/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/list_collections_batch_cursor.go b/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/list_collections_batch_cursor.go deleted file mode 100644 index 3917218b7..000000000 --- a/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/list_collections_batch_cursor.go +++ /dev/null @@ -1,134 +0,0 @@ -// Copyright (C) MongoDB, Inc. 2017-present. -// -// Licensed under the Apache License, Version 2.0 (the "License"); you may -// not use this file except in compliance with the License. You may obtain -// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 - -package driver - -import ( - "context" - "errors" - "io" - "strings" - - "go.mongodb.org/mongo-driver/x/bsonx/bsoncore" -) - -// ListCollectionsBatchCursor is a special batch cursor returned from ListCollections that properly -// handles current and legacy ListCollections operations. -type ListCollectionsBatchCursor struct { - legacy bool // server version < 3.0 - bc *BatchCursor - currentBatch *bsoncore.DocumentSequence - err error -} - -// NewListCollectionsBatchCursor creates a new non-legacy ListCollectionsCursor. -func NewListCollectionsBatchCursor(bc *BatchCursor) (*ListCollectionsBatchCursor, error) { - if bc == nil { - return nil, errors.New("batch cursor must not be nil") - } - return &ListCollectionsBatchCursor{bc: bc, currentBatch: new(bsoncore.DocumentSequence)}, nil -} - -// NewLegacyListCollectionsBatchCursor creates a new legacy ListCollectionsCursor. -func NewLegacyListCollectionsBatchCursor(bc *BatchCursor) (*ListCollectionsBatchCursor, error) { - if bc == nil { - return nil, errors.New("batch cursor must not be nil") - } - return &ListCollectionsBatchCursor{legacy: true, bc: bc, currentBatch: new(bsoncore.DocumentSequence)}, nil -} - -// ID returns the cursor ID for this batch cursor. -func (lcbc *ListCollectionsBatchCursor) ID() int64 { - return lcbc.bc.ID() -} - -// Next indicates if there is another batch available. Returning false does not necessarily indicate -// that the cursor is closed. This method will return false when an empty batch is returned. -// -// If Next returns true, there is a valid batch of documents available. If Next returns false, there -// is not a valid batch of documents available. -func (lcbc *ListCollectionsBatchCursor) Next(ctx context.Context) bool { - if !lcbc.bc.Next(ctx) { - return false - } - - if !lcbc.legacy { - lcbc.currentBatch.Style = lcbc.bc.currentBatch.Style - lcbc.currentBatch.Data = lcbc.bc.currentBatch.Data - lcbc.currentBatch.ResetIterator() - return true - } - - lcbc.currentBatch.Style = bsoncore.SequenceStyle - lcbc.currentBatch.Data = lcbc.currentBatch.Data[:0] - - var doc bsoncore.Document - for { - doc, lcbc.err = lcbc.bc.currentBatch.Next() - if lcbc.err != nil { - if lcbc.err == io.EOF { - lcbc.err = nil - break - } - return false - } - doc, lcbc.err = lcbc.projectNameElement(doc) - if lcbc.err != nil { - return false - } - lcbc.currentBatch.Data = append(lcbc.currentBatch.Data, doc...) - } - - return true -} - -// Batch will return a DocumentSequence for the current batch of documents. The returned -// DocumentSequence is only valid until the next call to Next or Close. -func (lcbc *ListCollectionsBatchCursor) Batch() *bsoncore.DocumentSequence { return lcbc.currentBatch } - -// Server returns a pointer to the cursor's server. -func (lcbc *ListCollectionsBatchCursor) Server() Server { return lcbc.bc.server } - -// Err returns the latest error encountered. -func (lcbc *ListCollectionsBatchCursor) Err() error { - if lcbc.err != nil { - return lcbc.err - } - return lcbc.bc.Err() -} - -// Close closes this batch cursor. -func (lcbc *ListCollectionsBatchCursor) Close(ctx context.Context) error { return lcbc.bc.Close(ctx) } - -// project out the database name for a legacy server -func (*ListCollectionsBatchCursor) projectNameElement(rawDoc bsoncore.Document) (bsoncore.Document, error) { - elems, err := rawDoc.Elements() - if err != nil { - return nil, err - } - - var filteredElems []byte - for _, elem := range elems { - key := elem.Key() - if key != "name" { - filteredElems = append(filteredElems, elem...) - continue - } - - name := elem.Value().StringValue() - collName := name[strings.Index(name, ".")+1:] - filteredElems = bsoncore.AppendStringElement(filteredElems, "name", collName) - } - - var filteredDoc []byte - filteredDoc = bsoncore.BuildDocument(filteredDoc, filteredElems) - return filteredDoc, nil -} - -// SetBatchSize sets the batchSize for future getMores. -func (lcbc *ListCollectionsBatchCursor) SetBatchSize(size int32) { - lcbc.bc.SetBatchSize(size) -} diff --git a/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/mongocrypt/binary.go b/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/mongocrypt/binary.go index 9e887375a..4e4b51d74 100644 --- a/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/mongocrypt/binary.go +++ b/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/mongocrypt/binary.go @@ -9,7 +9,10 @@ package mongocrypt -// #include +/* +#include +#include +*/ import "C" import ( "unsafe" @@ -17,6 +20,7 @@ import ( // binary is a wrapper type around a mongocrypt_binary_t* type binary struct { + p *C.uint8_t wrapped *C.mongocrypt_binary_t } @@ -33,11 +37,11 @@ func newBinaryFromBytes(data []byte) *binary { return newBinary() } - // We don't need C.CBytes here because data cannot go out of scope. Any mongocrypt function that takes a - // mongocrypt_binary_t will make a copy of the data so the data can be garbage collected after calling. - addr := (*C.uint8_t)(unsafe.Pointer(&data[0])) // uint8_t* - dataLen := C.uint32_t(len(data)) // uint32_t + // TODO: Consider using runtime.Pinner to replace the C.CBytes after using go1.21.0. + addr := (*C.uint8_t)(C.CBytes(data)) // uint8_t* + dataLen := C.uint32_t(len(data)) // uint32_t return &binary{ + p: addr, wrapped: C.mongocrypt_binary_new_from_data(addr, dataLen), } } @@ -52,5 +56,8 @@ func (b *binary) toBytes() []byte { // close cleans up any resources associated with the given binary instance. func (b *binary) close() { + if b.p != nil { + C.free(unsafe.Pointer(b.p)) + } C.mongocrypt_binary_destroy(b.wrapped) } diff --git a/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/mongocrypt/mongocrypt.go b/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/mongocrypt/mongocrypt.go index 64e226508..20f6ff0aa 100644 --- a/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/mongocrypt/mongocrypt.go +++ b/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/mongocrypt/mongocrypt.go @@ -23,7 +23,7 @@ import ( "unsafe" "go.mongodb.org/mongo-driver/bson" - "go.mongodb.org/mongo-driver/internal" + "go.mongodb.org/mongo-driver/internal/httputil" "go.mongodb.org/mongo-driver/x/bsonx/bsoncore" "go.mongodb.org/mongo-driver/x/mongo/driver/auth/creds" "go.mongodb.org/mongo-driver/x/mongo/driver/mongocrypt/options" @@ -55,7 +55,7 @@ func NewMongoCrypt(opts *options.MongoCryptOptions) (*MongoCrypt, error) { } httpClient := opts.HTTPClient if httpClient == nil { - httpClient = internal.DefaultHTTPClient + httpClient = httputil.DefaultHTTPClient } kmsProviders := make(map[string]kmsProvider) if needsKmsProvider(opts.KmsProviders, "gcp") { @@ -381,8 +381,8 @@ func (m *MongoCrypt) CryptSharedLibVersionString() string { // Close cleans up any resources associated with the given MongoCrypt instance. func (m *MongoCrypt) Close() { C.mongocrypt_destroy(m.wrapped) - if m.httpClient == internal.DefaultHTTPClient { - internal.CloseIdleHTTPConnections(m.httpClient) + if m.httpClient == httputil.DefaultHTTPClient { + httputil.CloseIdleHTTPConnections(m.httpClient) } } @@ -511,7 +511,7 @@ func (m *MongoCrypt) GetKmsProviders(ctx context.Context) (bsoncore.Document, er for k, p := range m.kmsProviders { doc, err := p.GetCredentialsDoc(ctx) if err != nil { - return nil, internal.WrapErrorf(err, "unable to retrieve %s credentials", k) + return nil, fmt.Errorf("unable to retrieve %s credentials: %w", k, err) } builder.AppendDocument(k, doc) } diff --git a/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/ocsp/config.go b/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/ocsp/config.go index 4cb14e4d0..eac2aab7f 100644 --- a/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/ocsp/config.go +++ b/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/ocsp/config.go @@ -12,7 +12,7 @@ import ( "fmt" "net/http" - "go.mongodb.org/mongo-driver/internal" + "go.mongodb.org/mongo-driver/internal/httputil" "golang.org/x/crypto/ocsp" ) @@ -33,7 +33,7 @@ func newConfig(certChain []*x509.Certificate, opts *VerifyOptions) (config, erro } if cfg.httpClient == nil { - cfg.httpClient = internal.DefaultHTTPClient + cfg.httpClient = httputil.DefaultHTTPClient } if len(certChain) == 0 { diff --git a/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/ocsp/ocsp.go b/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/ocsp/ocsp.go index 0e7dbfe2d..849530fde 100644 --- a/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/ocsp/ocsp.go +++ b/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/ocsp/ocsp.go @@ -149,7 +149,7 @@ func processStaple(cfg config, staple []byte) (*ResponseDetails, error) { // If the server has a Must-Staple certificate and the server does not present a stapled OCSP response, error. if mustStaple && len(staple) == 0 { return nil, errors.New("server provided a certificate with the Must-Staple extension but did not " + - "provde a stapled OCSP response") + "provide a stapled OCSP response") } if len(staple) == 0 { diff --git a/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/operation.go b/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/operation.go index beb8651cf..905c9cfc5 100644 --- a/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/operation.go +++ b/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/operation.go @@ -22,7 +22,9 @@ import ( "go.mongodb.org/mongo-driver/bson/bsontype" "go.mongodb.org/mongo-driver/bson/primitive" "go.mongodb.org/mongo-driver/event" - "go.mongodb.org/mongo-driver/internal" + "go.mongodb.org/mongo-driver/internal/csot" + "go.mongodb.org/mongo-driver/internal/driverutil" + "go.mongodb.org/mongo-driver/internal/handshake" "go.mongodb.org/mongo-driver/internal/logger" "go.mongodb.org/mongo-driver/mongo/address" "go.mongodb.org/mongo-driver/mongo/description" @@ -36,8 +38,6 @@ import ( const defaultLocalThreshold = 15 * time.Millisecond -var dollarCmd = [...]byte{'.', '$', 'c', 'm', 'd'} - var ( // ErrNoDocCommandResponse occurs when the server indicated a response existed, but none was found. ErrNoDocCommandResponse = errors.New("command returned no documents") @@ -306,8 +306,15 @@ type Operation struct { Logger *logger.Logger - // cmdName is only set when serializing OP_MSG and is used internally in readWireMessage. - cmdName string + // Name is the name of the operation. This is used when serializing + // OP_MSG as well as for logging server selection data. + Name string + + // omitReadPreference is a boolean that indicates whether to omit the + // read preference from the command. This omition includes the case + // where a default read preference is used when the operation + // ReadPreference is not specified. + omitReadPreference bool } // shouldEncrypt returns true if this operation should automatically be encrypted. @@ -315,8 +322,73 @@ func (op Operation) shouldEncrypt() bool { return op.Crypt != nil && !op.Crypt.BypassAutoEncryption() } +// filterDeprioritizedServers will filter out the server candidates that have +// been deprioritized by the operation due to failure. +// +// The server selector should try to select a server that is not in the +// deprioritization list. However, if this is not possible (e.g. there are no +// other healthy servers in the cluster), the selector may return a +// deprioritized server. +func filterDeprioritizedServers(candidates, deprioritized []description.Server) []description.Server { + if len(deprioritized) == 0 { + return candidates + } + + dpaSet := make(map[address.Address]*description.Server) + for i, srv := range deprioritized { + dpaSet[srv.Addr] = &deprioritized[i] + } + + allowed := []description.Server{} + + // Iterate over the candidates and append them to the allowdIndexes slice if + // they are not in the deprioritizedServers list. + for _, candidate := range candidates { + if srv, ok := dpaSet[candidate.Addr]; !ok || !srv.Equal(candidate) { + allowed = append(allowed, candidate) + } + } + + // If nothing is allowed, then all available servers must have been + // deprioritized. In this case, return the candidates list as-is so that the + // selector can find a suitable server + if len(allowed) == 0 { + return candidates + } + + return allowed +} + +// opServerSelector is a wrapper for the server selector that is assigned to the +// operation. The purpose of this wrapper is to filter candidates with +// operation-specific logic, such as deprioritizing failing servers. +type opServerSelector struct { + selector description.ServerSelector + deprioritizedServers []description.Server +} + +// SelectServer will filter candidates with operation-specific logic before +// passing them onto the user-defined or default selector. +func (oss *opServerSelector) SelectServer( + topo description.Topology, + candidates []description.Server, +) ([]description.Server, error) { + selectedServers, err := oss.selector.SelectServer(topo, candidates) + if err != nil { + return nil, err + } + + filteredServers := filterDeprioritizedServers(selectedServers, oss.deprioritizedServers) + + return filteredServers, nil +} + // selectServer handles performing server selection for an operation. -func (op Operation) selectServer(ctx context.Context) (Server, error) { +func (op Operation) selectServer( + ctx context.Context, + requestID int32, + deprioritized []description.Server, +) (Server, error) { if err := op.Validate(); err != nil { return nil, err } @@ -333,12 +405,24 @@ func (op Operation) selectServer(ctx context.Context) (Server, error) { }) } - return op.Deployment.SelectServer(ctx, selector) + oss := &opServerSelector{ + selector: selector, + deprioritizedServers: deprioritized, + } + + ctx = logger.WithOperationName(ctx, op.Name) + ctx = logger.WithOperationID(ctx, requestID) + + return op.Deployment.SelectServer(ctx, oss) } // getServerAndConnection should be used to retrieve a Server and Connection to execute an operation. -func (op Operation) getServerAndConnection(ctx context.Context) (Server, Connection, error) { - server, err := op.selectServer(ctx) +func (op Operation) getServerAndConnection( + ctx context.Context, + requestID int32, + deprioritized []description.Server, +) (Server, Connection, error) { + server, err := op.selectServer(ctx, requestID, deprioritized) if err != nil { if op.Client != nil && !(op.Client.Committing || op.Client.Aborting) && op.Client.TransactionRunning() { @@ -417,8 +501,8 @@ func (op Operation) Execute(ctx context.Context) error { // If no deadline is set on the passed-in context, op.Timeout is set, and context is not already // a Timeout context, honor op.Timeout in new Timeout context for operation execution. - if _, deadlineSet := ctx.Deadline(); !deadlineSet && op.Timeout != nil && !internal.IsTimeoutContext(ctx) { - newCtx, cancelFunc := internal.MakeTimeoutContext(ctx, *op.Timeout) + if _, deadlineSet := ctx.Deadline(); !deadlineSet && op.Timeout != nil && !csot.IsTimeoutContext(ctx) { + newCtx, cancelFunc := csot.MakeTimeoutContext(ctx, *op.Timeout) // Redefine ctx to be the new timeout-derived context. ctx = newCtx // Cancel the timeout-derived context at the end of Execute to avoid a context leak. @@ -456,7 +540,7 @@ func (op Operation) Execute(ctx context.Context) error { // If context is a Timeout context, automatically set retries to -1 (infinite) if retrying is // enabled. retryEnabled := op.RetryMode != nil && op.RetryMode.Enabled() - if internal.IsTimeoutContext(ctx) && retryEnabled { + if csot.IsTimeoutContext(ctx) && retryEnabled { retries = -1 } @@ -471,6 +555,11 @@ func (op Operation) Execute(ctx context.Context) error { first := true currIndex := 0 + // deprioritizedServers are a running list of servers that should be + // deprioritized during server selection. Per the specifications, we should + // only ever deprioritize the "previous server". + var deprioritizedServers []description.Server + // resetForRetry records the error that caused the retry, decrements retries, and resets the // retry loop variables to request a new server and a new connection for the next attempt. resetForRetry := func(err error) { @@ -496,11 +585,18 @@ func (op Operation) Execute(ctx context.Context) error { } } - // If we got a connection, close it immediately to release pool resources for - // subsequent retries. + // If we got a connection, close it immediately to release pool resources + // for subsequent retries. if conn != nil { + // If we are dealing with a sharded cluster, then mark the failed server + // as "deprioritized". + if desc := conn.Description; desc != nil && op.Deployment.Kind() == description.Sharded { + deprioritizedServers = []description.Server{conn.Description()} + } + conn.Close() } + // Set the server and connection to nil to request a new server and connection. srvr = nil conn = nil @@ -521,9 +617,11 @@ func (op Operation) Execute(ctx context.Context) error { } }() for { + requestID := wiremessage.NextRequestID() + // If the server or connection are nil, try to select a new server and get a new connection. if srvr == nil || conn == nil { - srvr, conn, err = op.getServerAndConnection(ctx) + srvr, conn, err = op.getServerAndConnection(ctx, requestID, deprioritizedServers) if err != nil { // If the returned error is retryable and there are retries remaining (negative // retries means retry indefinitely), then retry the operation. Set the server @@ -618,7 +716,8 @@ func (op Operation) Execute(ctx context.Context) error { } var startedInfo startedInformation - *wm, startedInfo, err = op.createWireMessage(ctx, (*wm)[:0], desc, maxTimeMS, conn) + *wm, startedInfo, err = op.createWireMessage(ctx, maxTimeMS, (*wm)[:0], desc, conn, requestID) + if err != nil { return err } @@ -627,7 +726,15 @@ func (op Operation) Execute(ctx context.Context) error { startedInfo.connID = conn.ID() startedInfo.driverConnectionID = conn.DriverConnectionID() startedInfo.cmdName = op.getCommandName(startedInfo.cmd) - op.cmdName = startedInfo.cmdName + + // If the command name does not match the operation name, update + // the operation name as a sanity check. It's more correct to + // be aligned with the data passed to the server via the + // wire message. + if startedInfo.cmdName != op.Name { + op.Name = startedInfo.cmdName + } + startedInfo.redacted = op.redactCommand(startedInfo.cmdName, startedInfo.cmd) startedInfo.serviceID = conn.Description().ServiceID startedInfo.serverConnID = conn.ServerConnectionID() @@ -668,9 +775,12 @@ func (op Operation) Execute(ctx context.Context) error { if ctx.Err() != nil { err = ctx.Err() } else if deadline, ok := ctx.Deadline(); ok { - if internal.IsTimeoutContext(ctx) && time.Now().Add(srvr.RTTMonitor().P90()).After(deadline) { - err = internal.WrapErrorf(ErrDeadlineWouldBeExceeded, - "remaining time %v until context deadline is less than 90th percentile RTT\n%v", time.Until(deadline), srvr.RTTMonitor().Stats()) + if csot.IsTimeoutContext(ctx) && time.Now().Add(srvr.RTTMonitor().P90()).After(deadline) { + err = fmt.Errorf( + "remaining time %v until context deadline is less than 90th percentile RTT: %w\n%v", + time.Until(deadline), + ErrDeadlineWouldBeExceeded, + srvr.RTTMonitor().Stats()) } else if time.Now().Add(srvr.RTTMonitor().Min()).After(deadline) { err = context.DeadlineExceeded } @@ -736,7 +846,7 @@ func (op Operation) Execute(ctx context.Context) error { // If the error is no longer retryable and has the NoWritesPerformed label, then we should // set the error to the "previous indefinite error" unless the current error is already the - // "previous indefinite error". After reseting, repeat the error check. + // "previous indefinite error". After resetting, repeat the error check. if tt.HasErrorLabel(NoWritesPerformed) && !prevIndefiniteErrIsSet { err = prevIndefiniteErr prevIndefiniteErrIsSet = true @@ -833,7 +943,7 @@ func (op Operation) Execute(ctx context.Context) error { // If the error is no longer retryable and has the NoWritesPerformed label, then we should // set the error to the "previous indefinite error" unless the current error is already the - // "previous indefinite error". After reseting, repeat the error check. + // "previous indefinite error". After resetting, repeat the error check. if tt.HasErrorLabel(NoWritesPerformed) && !prevIndefiniteErrIsSet { err = prevIndefiniteErr prevIndefiniteErrIsSet = true @@ -903,7 +1013,7 @@ func (op Operation) Execute(ctx context.Context) error { } // Reset the retries number for RetryOncePerCommand unless context is a Timeout context, in // which case retries should remain as -1 (as many times as possible). - if *op.RetryMode == RetryOncePerCommand && !internal.IsTimeoutContext(ctx) { + if *op.RetryMode == RetryOncePerCommand && !csot.IsTimeoutContext(ctx) { retries = 1 } } @@ -987,7 +1097,7 @@ func (op Operation) readWireMessage(ctx context.Context, conn Connection) (resul op.Client.UpdateRecoveryToken(bson.Raw(res)) // Update snapshot time if operation was a "find", "aggregate" or "distinct". - if op.cmdName == "find" || op.cmdName == "aggregate" || op.cmdName == "distinct" { + if op.Name == driverutil.FindOp || op.Name == driverutil.AggregateOp || op.Name == driverutil.DistinctOp { op.Client.UpdateSnapshotTime(res) } @@ -1071,22 +1181,6 @@ func (Operation) decompressWireMessage(wm []byte) (wiremessage.OpCode, []byte, e return opcode, uncompressed, nil } -func (op Operation) createWireMessage( - ctx context.Context, - dst []byte, - desc description.SelectedServer, - maxTimeMS uint64, - conn Connection, -) ([]byte, startedInformation, error) { - // If topology is not LoadBalanced, API version is not declared, and wire version is unknown - // or less than 6, use OP_QUERY. Otherwise, use OP_MSG. - if desc.Kind != description.LoadBalanced && op.ServerAPI == nil && - (desc.WireVersion == nil || desc.WireVersion.Max < wiremessage.OpmsgWireVersion) { - return op.createQueryWireMessage(maxTimeMS, dst, desc) - } - return op.createMsgWireMessage(ctx, maxTimeMS, dst, desc, conn) -} - func (op Operation) addBatchArray(dst []byte) []byte { aidx, dst := bsoncore.AppendArrayElementStart(dst, op.Batches.Identifier) for i, doc := range op.Batches.Current { @@ -1096,13 +1190,20 @@ func (op Operation) addBatchArray(dst []byte) []byte { return dst } -func (op Operation) createQueryWireMessage(maxTimeMS uint64, dst []byte, desc description.SelectedServer) ([]byte, startedInformation, error) { +func (op Operation) createLegacyHandshakeWireMessage( + maxTimeMS uint64, + dst []byte, + desc description.SelectedServer, +) ([]byte, startedInformation, error) { var info startedInformation flags := op.secondaryOK(desc) var wmindex int32 info.requestID = wiremessage.NextRequestID() wmindex, dst = wiremessage.AppendHeaderStart(dst, info.requestID, 0, wiremessage.OpQuery) dst = wiremessage.AppendQueryFlags(dst, flags) + + dollarCmd := [...]byte{'.', '$', 'c', 'm', 'd'} + // FullCollectionName dst = append(dst, op.Database...) dst = append(dst, dollarCmd[:]...) @@ -1168,8 +1269,13 @@ func (op Operation) createQueryWireMessage(maxTimeMS uint64, dst []byte, desc de return bsoncore.UpdateLength(dst, wmindex, int32(len(dst[wmindex:]))), info, nil } -func (op Operation) createMsgWireMessage(ctx context.Context, maxTimeMS uint64, dst []byte, desc description.SelectedServer, +func (op Operation) createMsgWireMessage( + ctx context.Context, + maxTimeMS uint64, + dst []byte, + desc description.SelectedServer, conn Connection, + requestID int32, ) ([]byte, startedInformation, error) { var info startedInformation var flags wiremessage.MsgFlag @@ -1185,7 +1291,7 @@ func (op Operation) createMsgWireMessage(ctx context.Context, maxTimeMS uint64, flags |= wiremessage.ExhaustAllowed } - info.requestID = wiremessage.NextRequestID() + info.requestID = requestID wmindex, dst = wiremessage.AppendHeaderStart(dst, info.requestID, 0, wiremessage.OpMsg) dst = wiremessage.AppendMsgFlags(dst, flags) // Body @@ -1251,6 +1357,29 @@ func (op Operation) createMsgWireMessage(ctx context.Context, maxTimeMS uint64, return bsoncore.UpdateLength(dst, wmindex, int32(len(dst[wmindex:]))), info, nil } +// isLegacyHandshake returns True if the operation is the first message of +// the initial handshake and should use a legacy hello. +func isLegacyHandshake(op Operation, desc description.SelectedServer) bool { + isInitialHandshake := desc.WireVersion == nil || desc.WireVersion.Max == 0 + + return op.Legacy == LegacyHandshake && isInitialHandshake +} + +func (op Operation) createWireMessage( + ctx context.Context, + maxTimeMS uint64, + dst []byte, + desc description.SelectedServer, + conn Connection, + requestID int32, +) ([]byte, startedInformation, error) { + if isLegacyHandshake(op, desc) { + return op.createLegacyHandshakeWireMessage(maxTimeMS, dst, desc) + } + + return op.createMsgWireMessage(ctx, maxTimeMS, dst, desc, conn, requestID) +} + // addCommandFields adds the fields for a command to the wire message in dst. This assumes that the start of the document // has already been added and does not add the final 0 byte. func (op Operation) addCommandFields(ctx context.Context, dst []byte, desc description.SelectedServer) ([]byte, error) { @@ -1375,7 +1504,14 @@ func (op Operation) addWriteConcern(dst []byte, desc description.SelectedServer) func (op Operation) addSession(dst []byte, desc description.SelectedServer) ([]byte, error) { client := op.Client - if client == nil || !sessionsSupported(desc.WireVersion) || desc.SessionTimeoutMinutes == 0 { + + // If the operation is defined for an explicit session but the server + // does not support sessions, then throw an error. + if client != nil && !client.IsImplicit && desc.SessionTimeoutMinutesPtr == nil { + return nil, fmt.Errorf("current topology does not support sessions") + } + + if client == nil || !sessionsSupported(desc.WireVersion) || desc.SessionTimeoutMinutesPtr == nil { return dst, nil } if err := client.UpdateUseTime(); err != nil { @@ -1427,7 +1563,7 @@ func (op Operation) addClusterTime(dst []byte, desc description.SelectedServer) // operation's MaxTimeMS if set. If no MaxTimeMS is set on the operation, and context is // not a Timeout context, calculateMaxTimeMS returns 0. func (op Operation) calculateMaxTimeMS(ctx context.Context, rtt90 time.Duration, rttStats string) (uint64, error) { - if internal.IsTimeoutContext(ctx) { + if csot.IsTimeoutContext(ctx) { if deadline, ok := ctx.Deadline(); ok { remainingTimeout := time.Until(deadline) maxTime := remainingTimeout - rtt90 @@ -1436,9 +1572,11 @@ func (op Operation) calculateMaxTimeMS(ctx context.Context, rtt90 time.Duration, // maxTimeMS value (e.g. 400 microseconds evaluates to 1ms, not 0ms). maxTimeMS := int64((maxTime + (time.Millisecond - 1)) / time.Millisecond) if maxTimeMS <= 0 { - return 0, internal.WrapErrorf(ErrDeadlineWouldBeExceeded, - "remaining time %v until context deadline is less than or equal to 90th percentile RTT\n%v", - remainingTimeout, rttStats) + return 0, fmt.Errorf( + "remaining time %v until context deadline is less than or equal to 90th percentile RTT: %w\n%v", + remainingTimeout, + ErrDeadlineWouldBeExceeded, + rttStats) } return uint64(maxTimeMS), nil } @@ -1514,7 +1652,14 @@ func (op Operation) getReadPrefBasedOnTransaction() (*readpref.ReadPref, error) return op.ReadPreference, nil } +// createReadPref will attempt to create a document with the "readPreference" +// object and various related fields such as "mode", "tags", and +// "maxStalenessSeconds". func (op Operation) createReadPref(desc description.SelectedServer, isOpQuery bool) (bsoncore.Document, error) { + if op.omitReadPreference { + return nil, nil + } + // TODO(GODRIVER-2231): Instead of checking if isOutputAggregate and desc.Server.WireVersion.Max < 13, somehow check // TODO if supplied readPreference was "overwritten" with primary in description.selectForReplicaSet. if desc.Server.Kind == description.Standalone || (isOpQuery && desc.Server.Kind != description.Mongos) || @@ -1553,7 +1698,14 @@ func (op Operation) createReadPref(desc description.SelectedServer, isOpQuery bo doc, _ = bsoncore.AppendDocumentEnd(doc, idx) return doc, nil } - doc = bsoncore.AppendStringElement(doc, "mode", "primary") + + // OP_MSG requires never sending read preference "primary" + // except for topology "single". + // + // It is important to note that although the Go Driver does not + // support legacy opcodes, OP_QUERY has different rules for + // adding read preference to commands. + return nil, nil case readpref.PrimaryPreferredMode: doc = bsoncore.AppendStringElement(doc, "mode", "primaryPreferred") case readpref.SecondaryPreferredMode: @@ -1617,7 +1769,7 @@ func (op Operation) secondaryOK(desc description.SelectedServer) wiremessage.Que } func (Operation) canCompress(cmd string) bool { - if cmd == internal.LegacyHello || cmd == "hello" || cmd == "saslStart" || cmd == "saslContinue" || cmd == "getnonce" || cmd == "authenticate" || + if cmd == handshake.LegacyHello || cmd == "hello" || cmd == "saslStart" || cmd == "saslContinue" || cmd == "getnonce" || cmd == "authenticate" || cmd == "createUser" || cmd == "updateUser" || cmd == "copydbSaslStart" || cmd == "copydbgetnonce" || cmd == "copydb" { return false } @@ -1721,7 +1873,7 @@ func (op Operation) decodeResult(opcode wiremessage.OpCode, wm []byte) (bsoncore return nil, errors.New("malformed wire message: insufficient bytes to read document sequence") } default: - return nil, fmt.Errorf("malformed wire message: uknown section type %v", stype) + return nil, fmt.Errorf("malformed wire message: unknown section type %v", stype) } } @@ -1749,7 +1901,7 @@ func (op *Operation) redactCommand(cmd string, doc bsoncore.Document) bool { return true } - if strings.ToLower(cmd) != internal.LegacyHelloLowercase && cmd != "hello" { + if strings.ToLower(cmd) != handshake.LegacyHelloLowercase && cmd != "hello" { return false } @@ -1785,14 +1937,14 @@ func (op Operation) publishStartedEvent(ctx context.Context, info startedInforma DriverConnectionID: info.driverConnectionID, Message: logger.CommandStarted, Name: info.cmdName, + DatabaseName: op.Database, RequestID: int64(info.requestID), ServerConnectionID: info.serverConnID, ServerHost: host, ServerPort: port, ServiceID: info.serviceID, }, - logger.KeyCommand, formattedCmd, - logger.KeyDatabaseName, op.Database)...) + logger.KeyCommand, formattedCmd)...) } @@ -1838,6 +1990,7 @@ func (op Operation) publishFinishedEvent(ctx context.Context, info finishedInfor DriverConnectionID: info.driverConnectionID, Message: logger.CommandSucceeded, Name: info.cmdName, + DatabaseName: op.Database, RequestID: int64(info.requestID), ServerConnectionID: info.serverConnID, ServerHost: host, @@ -1860,6 +2013,7 @@ func (op Operation) publishFinishedEvent(ctx context.Context, info finishedInfor DriverConnectionID: info.driverConnectionID, Message: logger.CommandFailed, Name: info.cmdName, + DatabaseName: op.Database, RequestID: int64(info.requestID), ServerConnectionID: info.serverConnID, ServerHost: host, @@ -1877,6 +2031,7 @@ func (op Operation) publishFinishedEvent(ctx context.Context, info finishedInfor finished := event.CommandFinishedEvent{ CommandName: info.cmdName, + DatabaseName: op.Database, RequestID: int64(info.requestID), ConnectionID: info.connID, Duration: info.duration, @@ -1905,10 +2060,10 @@ func (op Operation) publishFinishedEvent(ctx context.Context, info finishedInfor // sessionsSupported returns true of the given server version indicates that it supports sessions. func sessionsSupported(wireVersion *description.VersionRange) bool { - return wireVersion != nil && wireVersion.Max >= 6 + return wireVersion != nil } // retryWritesSupported returns true if this description represents a server that supports retryable writes. func retryWritesSupported(s description.Server) bool { - return s.SessionTimeoutMinutes != 0 && s.Kind != description.Standalone + return s.SessionTimeoutMinutesPtr != nil && s.Kind != description.Standalone } diff --git a/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/operation/abort_transaction.go b/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/operation/abort_transaction.go index 42ff5e6fc..941372713 100644 --- a/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/operation/abort_transaction.go +++ b/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/operation/abort_transaction.go @@ -11,6 +11,7 @@ import ( "errors" "go.mongodb.org/mongo-driver/event" + "go.mongodb.org/mongo-driver/internal/driverutil" "go.mongodb.org/mongo-driver/mongo/description" "go.mongodb.org/mongo-driver/mongo/writeconcern" "go.mongodb.org/mongo-driver/x/bsonx/bsoncore" @@ -64,6 +65,7 @@ func (at *AbortTransaction) Execute(ctx context.Context) error { Selector: at.selector, WriteConcern: at.writeConcern, ServerAPI: at.serverAPI, + Name: driverutil.AbortTransactionOp, }.Execute(ctx) } diff --git a/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/operation/aggregate.go b/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/operation/aggregate.go index 4ea2263cb..ca0e79652 100644 --- a/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/operation/aggregate.go +++ b/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/operation/aggregate.go @@ -13,6 +13,7 @@ import ( "go.mongodb.org/mongo-driver/bson/bsontype" "go.mongodb.org/mongo-driver/event" + "go.mongodb.org/mongo-driver/internal/driverutil" "go.mongodb.org/mongo-driver/mongo/description" "go.mongodb.org/mongo-driver/mongo/readconcern" "go.mongodb.org/mongo-driver/mongo/readpref" @@ -111,6 +112,7 @@ func (a *Aggregate) Execute(ctx context.Context) error { IsOutputAggregate: a.hasOutputStage, MaxTime: a.maxTime, Timeout: a.timeout, + Name: driverutil.AggregateOp, }.Execute(ctx) } diff --git a/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/operation/commit_transaction.go b/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/operation/commit_transaction.go index 2eecc5163..11c6f69dd 100644 --- a/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/operation/commit_transaction.go +++ b/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/operation/commit_transaction.go @@ -12,6 +12,7 @@ import ( "time" "go.mongodb.org/mongo-driver/event" + "go.mongodb.org/mongo-driver/internal/driverutil" "go.mongodb.org/mongo-driver/mongo/description" "go.mongodb.org/mongo-driver/mongo/writeconcern" "go.mongodb.org/mongo-driver/x/bsonx/bsoncore" @@ -66,6 +67,7 @@ func (ct *CommitTransaction) Execute(ctx context.Context) error { Selector: ct.selector, WriteConcern: ct.writeConcern, ServerAPI: ct.serverAPI, + Name: driverutil.CommitTransactionOp, }.Execute(ctx) } diff --git a/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/operation/count.go b/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/operation/count.go index 0e4d0ec1f..8de1e9f8d 100644 --- a/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/operation/count.go +++ b/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/operation/count.go @@ -14,6 +14,7 @@ import ( "go.mongodb.org/mongo-driver/bson/bsontype" "go.mongodb.org/mongo-driver/event" + "go.mongodb.org/mongo-driver/internal/driverutil" "go.mongodb.org/mongo-driver/mongo/description" "go.mongodb.org/mongo-driver/mongo/readconcern" "go.mongodb.org/mongo-driver/mongo/readpref" @@ -126,6 +127,7 @@ func (c *Count) Execute(ctx context.Context) error { Selector: c.selector, ServerAPI: c.serverAPI, Timeout: c.timeout, + Name: driverutil.CountOp, }.Execute(ctx) // Swallow error if NamespaceNotFound(26) is returned from aggregate on non-existent namespace diff --git a/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/operation/create.go b/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/operation/create.go index c333c5a99..45b26cb70 100644 --- a/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/operation/create.go +++ b/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/operation/create.go @@ -78,7 +78,6 @@ func (c *Create) Execute(ctx context.Context) error { WriteConcern: c.writeConcern, ServerAPI: c.serverAPI, }.Execute(ctx) - } func (c *Create) command(dst []byte, desc description.SelectedServer) ([]byte, error) { diff --git a/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/operation/createIndexes.go b/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/operation/create_indexes.go similarity index 98% rename from vendor/go.mongodb.org/mongo-driver/x/mongo/driver/operation/createIndexes.go rename to vendor/go.mongodb.org/mongo-driver/x/mongo/driver/operation/create_indexes.go index 70f7b5495..77daf676a 100644 --- a/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/operation/createIndexes.go +++ b/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/operation/create_indexes.go @@ -14,6 +14,7 @@ import ( "go.mongodb.org/mongo-driver/bson/bsontype" "go.mongodb.org/mongo-driver/event" + "go.mongodb.org/mongo-driver/internal/driverutil" "go.mongodb.org/mongo-driver/mongo/description" "go.mongodb.org/mongo-driver/mongo/writeconcern" "go.mongodb.org/mongo-driver/x/bsonx/bsoncore" @@ -117,6 +118,7 @@ func (ci *CreateIndexes) Execute(ctx context.Context) error { WriteConcern: ci.writeConcern, ServerAPI: ci.serverAPI, Timeout: ci.timeout, + Name: driverutil.CreateIndexesOp, }.Execute(ctx) } diff --git a/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/operation/create_search_indexes.go b/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/operation/create_search_indexes.go new file mode 100644 index 000000000..a16f9d716 --- /dev/null +++ b/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/operation/create_search_indexes.go @@ -0,0 +1,245 @@ +// Copyright (C) MongoDB, Inc. 2023-present. +// +// Licensed under the Apache License, Version 2.0 (the "License"); you may +// not use this file except in compliance with the License. You may obtain +// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 + +package operation + +import ( + "context" + "errors" + "fmt" + "time" + + "go.mongodb.org/mongo-driver/bson" + "go.mongodb.org/mongo-driver/event" + "go.mongodb.org/mongo-driver/mongo/description" + "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/session" +) + +// CreateSearchIndexes performs a createSearchIndexes operation. +type CreateSearchIndexes struct { + indexes bsoncore.Document + session *session.Client + clock *session.ClusterClock + collection string + monitor *event.CommandMonitor + crypt driver.Crypt + database string + deployment driver.Deployment + selector description.ServerSelector + writeConcern *writeconcern.WriteConcern + result CreateSearchIndexesResult + serverAPI *driver.ServerAPIOptions + timeout *time.Duration +} + +// CreateSearchIndexResult represents a single search index result in CreateSearchIndexesResult. +type CreateSearchIndexResult struct { + Name string +} + +// CreateSearchIndexesResult represents a createSearchIndexes result returned by the server. +type CreateSearchIndexesResult struct { + IndexesCreated []CreateSearchIndexResult +} + +func buildCreateSearchIndexesResult(response bsoncore.Document) (CreateSearchIndexesResult, error) { + elements, err := response.Elements() + if err != nil { + return CreateSearchIndexesResult{}, err + } + csir := CreateSearchIndexesResult{} + for _, element := range elements { + switch element.Key() { + case "indexesCreated": + arr, ok := element.Value().ArrayOK() + if !ok { + return csir, fmt.Errorf("response field 'indexesCreated' is type array, but received BSON type %s", element.Value().Type) + } + + var values []bsoncore.Value + values, err = arr.Values() + if err != nil { + break + } + + for _, val := range values { + valDoc, ok := val.DocumentOK() + if !ok { + return csir, fmt.Errorf("indexesCreated value is type document, but received BSON type %s", val.Type) + } + var indexesCreated CreateSearchIndexResult + if err = bson.Unmarshal(valDoc, &indexesCreated); err != nil { + return csir, err + } + csir.IndexesCreated = append(csir.IndexesCreated, indexesCreated) + } + } + } + return csir, nil +} + +// NewCreateSearchIndexes constructs and returns a new CreateSearchIndexes. +func NewCreateSearchIndexes(indexes bsoncore.Document) *CreateSearchIndexes { + return &CreateSearchIndexes{ + indexes: indexes, + } +} + +// Result returns the result of executing this operation. +func (csi *CreateSearchIndexes) Result() CreateSearchIndexesResult { return csi.result } + +func (csi *CreateSearchIndexes) processResponse(info driver.ResponseInfo) error { + var err error + csi.result, err = buildCreateSearchIndexesResult(info.ServerResponse) + return err +} + +// Execute runs this operations and returns an error if the operation did not execute successfully. +func (csi *CreateSearchIndexes) Execute(ctx context.Context) error { + if csi.deployment == nil { + return errors.New("the CreateSearchIndexes operation must have a Deployment set before Execute can be called") + } + + return driver.Operation{ + CommandFn: csi.command, + ProcessResponseFn: csi.processResponse, + CommandMonitor: csi.monitor, + Database: csi.database, + Deployment: csi.deployment, + }.Execute(ctx) + +} + +func (csi *CreateSearchIndexes) command(dst []byte, _ description.SelectedServer) ([]byte, error) { + dst = bsoncore.AppendStringElement(dst, "createSearchIndexes", csi.collection) + if csi.indexes != nil { + dst = bsoncore.AppendArrayElement(dst, "indexes", csi.indexes) + } + return dst, nil +} + +// Indexes specifies an array containing index specification documents for the indexes being created. +func (csi *CreateSearchIndexes) Indexes(indexes bsoncore.Document) *CreateSearchIndexes { + if csi == nil { + csi = new(CreateSearchIndexes) + } + + csi.indexes = indexes + return csi +} + +// Session sets the session for this operation. +func (csi *CreateSearchIndexes) Session(session *session.Client) *CreateSearchIndexes { + if csi == nil { + csi = new(CreateSearchIndexes) + } + + csi.session = session + return csi +} + +// ClusterClock sets the cluster clock for this operation. +func (csi *CreateSearchIndexes) ClusterClock(clock *session.ClusterClock) *CreateSearchIndexes { + if csi == nil { + csi = new(CreateSearchIndexes) + } + + csi.clock = clock + return csi +} + +// Collection sets the collection that this command will run against. +func (csi *CreateSearchIndexes) Collection(collection string) *CreateSearchIndexes { + if csi == nil { + csi = new(CreateSearchIndexes) + } + + csi.collection = collection + return csi +} + +// CommandMonitor sets the monitor to use for APM events. +func (csi *CreateSearchIndexes) CommandMonitor(monitor *event.CommandMonitor) *CreateSearchIndexes { + if csi == nil { + csi = new(CreateSearchIndexes) + } + + csi.monitor = monitor + return csi +} + +// Crypt sets the Crypt object to use for automatic encryption and decryption. +func (csi *CreateSearchIndexes) Crypt(crypt driver.Crypt) *CreateSearchIndexes { + if csi == nil { + csi = new(CreateSearchIndexes) + } + + csi.crypt = crypt + return csi +} + +// Database sets the database to run this operation against. +func (csi *CreateSearchIndexes) Database(database string) *CreateSearchIndexes { + if csi == nil { + csi = new(CreateSearchIndexes) + } + + csi.database = database + return csi +} + +// Deployment sets the deployment to use for this operation. +func (csi *CreateSearchIndexes) Deployment(deployment driver.Deployment) *CreateSearchIndexes { + if csi == nil { + csi = new(CreateSearchIndexes) + } + + csi.deployment = deployment + return csi +} + +// ServerSelector sets the selector used to retrieve a server. +func (csi *CreateSearchIndexes) ServerSelector(selector description.ServerSelector) *CreateSearchIndexes { + if csi == nil { + csi = new(CreateSearchIndexes) + } + + csi.selector = selector + return csi +} + +// WriteConcern sets the write concern for this operation. +func (csi *CreateSearchIndexes) WriteConcern(writeConcern *writeconcern.WriteConcern) *CreateSearchIndexes { + if csi == nil { + csi = new(CreateSearchIndexes) + } + + csi.writeConcern = writeConcern + return csi +} + +// ServerAPI sets the server API version for this operation. +func (csi *CreateSearchIndexes) ServerAPI(serverAPI *driver.ServerAPIOptions) *CreateSearchIndexes { + if csi == nil { + csi = new(CreateSearchIndexes) + } + + csi.serverAPI = serverAPI + return csi +} + +// Timeout sets the timeout for this operation. +func (csi *CreateSearchIndexes) Timeout(timeout *time.Duration) *CreateSearchIndexes { + if csi == nil { + csi = new(CreateSearchIndexes) + } + + csi.timeout = timeout + return csi +} diff --git a/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/operation/delete.go b/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/operation/delete.go index ee2823342..bf95cf496 100644 --- a/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/operation/delete.go +++ b/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/operation/delete.go @@ -14,6 +14,7 @@ import ( "go.mongodb.org/mongo-driver/bson/bsontype" "go.mongodb.org/mongo-driver/event" + "go.mongodb.org/mongo-driver/internal/driverutil" "go.mongodb.org/mongo-driver/internal/logger" "go.mongodb.org/mongo-driver/mongo/description" "go.mongodb.org/mongo-driver/mongo/writeconcern" @@ -114,6 +115,7 @@ func (d *Delete) Execute(ctx context.Context) error { ServerAPI: d.serverAPI, Timeout: d.timeout, Logger: d.logger, + Name: driverutil.DeleteOp, }.Execute(ctx) } diff --git a/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/operation/distinct.go b/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/operation/distinct.go index af66654d6..b7e675ce4 100644 --- a/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/operation/distinct.go +++ b/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/operation/distinct.go @@ -13,6 +13,7 @@ import ( "go.mongodb.org/mongo-driver/bson/bsontype" "go.mongodb.org/mongo-driver/event" + "go.mongodb.org/mongo-driver/internal/driverutil" "go.mongodb.org/mongo-driver/mongo/description" "go.mongodb.org/mongo-driver/mongo/readconcern" "go.mongodb.org/mongo-driver/mongo/readpref" @@ -105,6 +106,7 @@ func (d *Distinct) Execute(ctx context.Context) error { Selector: d.selector, ServerAPI: d.serverAPI, Timeout: d.timeout, + Name: driverutil.DistinctOp, }.Execute(ctx) } diff --git a/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/operation/drop_collection.go b/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/operation/drop_collection.go index 5d9a03d38..8c6596756 100644 --- a/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/operation/drop_collection.go +++ b/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/operation/drop_collection.go @@ -13,6 +13,7 @@ import ( "time" "go.mongodb.org/mongo-driver/event" + "go.mongodb.org/mongo-driver/internal/driverutil" "go.mongodb.org/mongo-driver/mongo/description" "go.mongodb.org/mongo-driver/mongo/writeconcern" "go.mongodb.org/mongo-driver/x/bsonx/bsoncore" @@ -102,6 +103,7 @@ func (dc *DropCollection) Execute(ctx context.Context) error { WriteConcern: dc.writeConcern, ServerAPI: dc.serverAPI, Timeout: dc.timeout, + Name: driverutil.DropOp, }.Execute(ctx) } diff --git a/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/operation/drop_database.go b/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/operation/drop_database.go index 74c8db446..a8f9b45ba 100644 --- a/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/operation/drop_database.go +++ b/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/operation/drop_database.go @@ -11,6 +11,7 @@ import ( "errors" "go.mongodb.org/mongo-driver/event" + "go.mongodb.org/mongo-driver/internal/driverutil" "go.mongodb.org/mongo-driver/mongo/description" "go.mongodb.org/mongo-driver/mongo/writeconcern" "go.mongodb.org/mongo-driver/x/bsonx/bsoncore" @@ -53,6 +54,7 @@ func (dd *DropDatabase) Execute(ctx context.Context) error { Selector: dd.selector, WriteConcern: dd.writeConcern, ServerAPI: dd.serverAPI, + Name: driverutil.DropDatabaseOp, }.Execute(ctx) } diff --git a/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/operation/drop_indexes.go b/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/operation/drop_indexes.go index 5b2a56dde..0c3d45970 100644 --- a/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/operation/drop_indexes.go +++ b/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/operation/drop_indexes.go @@ -13,6 +13,7 @@ import ( "time" "go.mongodb.org/mongo-driver/event" + "go.mongodb.org/mongo-driver/internal/driverutil" "go.mongodb.org/mongo-driver/mongo/description" "go.mongodb.org/mongo-driver/mongo/writeconcern" "go.mongodb.org/mongo-driver/x/bsonx/bsoncore" @@ -99,6 +100,7 @@ func (di *DropIndexes) Execute(ctx context.Context) error { WriteConcern: di.writeConcern, ServerAPI: di.serverAPI, Timeout: di.timeout, + Name: driverutil.DropIndexesOp, }.Execute(ctx) } diff --git a/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/operation/drop_search_index.go b/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/operation/drop_search_index.go new file mode 100644 index 000000000..25cde8154 --- /dev/null +++ b/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/operation/drop_search_index.go @@ -0,0 +1,227 @@ +// Copyright (C) MongoDB, Inc. 2023-present. +// +// Licensed under the Apache License, Version 2.0 (the "License"); you may +// not use this file except in compliance with the License. You may obtain +// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 + +package operation + +import ( + "context" + "errors" + "fmt" + "time" + + "go.mongodb.org/mongo-driver/event" + "go.mongodb.org/mongo-driver/mongo/description" + "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/session" +) + +// DropSearchIndex performs an dropSearchIndex operation. +type DropSearchIndex struct { + index string + session *session.Client + clock *session.ClusterClock + collection string + monitor *event.CommandMonitor + crypt driver.Crypt + database string + deployment driver.Deployment + selector description.ServerSelector + writeConcern *writeconcern.WriteConcern + result DropSearchIndexResult + serverAPI *driver.ServerAPIOptions + timeout *time.Duration +} + +// DropSearchIndexResult represents a dropSearchIndex result returned by the server. +type DropSearchIndexResult struct { + Ok int32 +} + +func buildDropSearchIndexResult(response bsoncore.Document) (DropSearchIndexResult, error) { + elements, err := response.Elements() + if err != nil { + return DropSearchIndexResult{}, err + } + dsir := DropSearchIndexResult{} + for _, element := range elements { + switch element.Key() { + case "ok": + var ok bool + dsir.Ok, ok = element.Value().AsInt32OK() + if !ok { + return dsir, fmt.Errorf("response field 'ok' is type int32, but received BSON type %s", element.Value().Type) + } + } + } + return dsir, nil +} + +// NewDropSearchIndex constructs and returns a new DropSearchIndex. +func NewDropSearchIndex(index string) *DropSearchIndex { + return &DropSearchIndex{ + index: index, + } +} + +// Result returns the result of executing this operation. +func (dsi *DropSearchIndex) Result() DropSearchIndexResult { return dsi.result } + +func (dsi *DropSearchIndex) processResponse(info driver.ResponseInfo) error { + var err error + dsi.result, err = buildDropSearchIndexResult(info.ServerResponse) + return err +} + +// Execute runs this operations and returns an error if the operation did not execute successfully. +func (dsi *DropSearchIndex) Execute(ctx context.Context) error { + if dsi.deployment == nil { + return errors.New("the DropSearchIndex operation must have a Deployment set before Execute can be called") + } + + return driver.Operation{ + CommandFn: dsi.command, + ProcessResponseFn: dsi.processResponse, + Client: dsi.session, + Clock: dsi.clock, + CommandMonitor: dsi.monitor, + Crypt: dsi.crypt, + Database: dsi.database, + Deployment: dsi.deployment, + Selector: dsi.selector, + WriteConcern: dsi.writeConcern, + ServerAPI: dsi.serverAPI, + Timeout: dsi.timeout, + }.Execute(ctx) + +} + +func (dsi *DropSearchIndex) command(dst []byte, _ description.SelectedServer) ([]byte, error) { + dst = bsoncore.AppendStringElement(dst, "dropSearchIndex", dsi.collection) + dst = bsoncore.AppendStringElement(dst, "name", dsi.index) + return dst, nil +} + +// Index specifies the name of the index to drop. If '*' is specified, all indexes will be dropped. +func (dsi *DropSearchIndex) Index(index string) *DropSearchIndex { + if dsi == nil { + dsi = new(DropSearchIndex) + } + + dsi.index = index + return dsi +} + +// Session sets the session for this operation. +func (dsi *DropSearchIndex) Session(session *session.Client) *DropSearchIndex { + if dsi == nil { + dsi = new(DropSearchIndex) + } + + dsi.session = session + return dsi +} + +// ClusterClock sets the cluster clock for this operation. +func (dsi *DropSearchIndex) ClusterClock(clock *session.ClusterClock) *DropSearchIndex { + if dsi == nil { + dsi = new(DropSearchIndex) + } + + dsi.clock = clock + return dsi +} + +// Collection sets the collection that this command will run against. +func (dsi *DropSearchIndex) Collection(collection string) *DropSearchIndex { + if dsi == nil { + dsi = new(DropSearchIndex) + } + + dsi.collection = collection + return dsi +} + +// CommandMonitor sets the monitor to use for APM events. +func (dsi *DropSearchIndex) CommandMonitor(monitor *event.CommandMonitor) *DropSearchIndex { + if dsi == nil { + dsi = new(DropSearchIndex) + } + + dsi.monitor = monitor + return dsi +} + +// Crypt sets the Crypt object to use for automatic encryption and decryption. +func (dsi *DropSearchIndex) Crypt(crypt driver.Crypt) *DropSearchIndex { + if dsi == nil { + dsi = new(DropSearchIndex) + } + + dsi.crypt = crypt + return dsi +} + +// Database sets the database to run this operation against. +func (dsi *DropSearchIndex) Database(database string) *DropSearchIndex { + if dsi == nil { + dsi = new(DropSearchIndex) + } + + dsi.database = database + return dsi +} + +// Deployment sets the deployment to use for this operation. +func (dsi *DropSearchIndex) Deployment(deployment driver.Deployment) *DropSearchIndex { + if dsi == nil { + dsi = new(DropSearchIndex) + } + + dsi.deployment = deployment + return dsi +} + +// ServerSelector sets the selector used to retrieve a server. +func (dsi *DropSearchIndex) ServerSelector(selector description.ServerSelector) *DropSearchIndex { + if dsi == nil { + dsi = new(DropSearchIndex) + } + + dsi.selector = selector + return dsi +} + +// WriteConcern sets the write concern for this operation. +func (dsi *DropSearchIndex) WriteConcern(writeConcern *writeconcern.WriteConcern) *DropSearchIndex { + if dsi == nil { + dsi = new(DropSearchIndex) + } + + dsi.writeConcern = writeConcern + return dsi +} + +// ServerAPI sets the server API version for this operation. +func (dsi *DropSearchIndex) ServerAPI(serverAPI *driver.ServerAPIOptions) *DropSearchIndex { + if dsi == nil { + dsi = new(DropSearchIndex) + } + + dsi.serverAPI = serverAPI + return dsi +} + +// Timeout sets the timeout for this operation. +func (dsi *DropSearchIndex) Timeout(timeout *time.Duration) *DropSearchIndex { + if dsi == nil { + dsi = new(DropSearchIndex) + } + + dsi.timeout = timeout + return dsi +} diff --git a/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/operation/end_sessions.go b/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/operation/end_sessions.go index 26e215fbe..52f300bb7 100644 --- a/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/operation/end_sessions.go +++ b/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/operation/end_sessions.go @@ -11,6 +11,7 @@ import ( "errors" "go.mongodb.org/mongo-driver/event" + "go.mongodb.org/mongo-driver/internal/driverutil" "go.mongodb.org/mongo-driver/mongo/description" "go.mongodb.org/mongo-driver/x/bsonx/bsoncore" "go.mongodb.org/mongo-driver/x/mongo/driver" @@ -59,6 +60,7 @@ func (es *EndSessions) Execute(ctx context.Context) error { Deployment: es.deployment, Selector: es.selector, ServerAPI: es.serverAPI, + Name: driverutil.EndSessionsOp, }.Execute(ctx) } diff --git a/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/operation/find.go b/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/operation/find.go index ab8a8d80d..27bb5b4f9 100644 --- a/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/operation/find.go +++ b/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/operation/find.go @@ -13,6 +13,7 @@ import ( "go.mongodb.org/mongo-driver/bson/bsontype" "go.mongodb.org/mongo-driver/event" + "go.mongodb.org/mongo-driver/internal/driverutil" "go.mongodb.org/mongo-driver/internal/logger" "go.mongodb.org/mongo-driver/mongo/description" "go.mongodb.org/mongo-driver/mongo/readconcern" @@ -108,6 +109,7 @@ func (f *Find) Execute(ctx context.Context) error { ServerAPI: f.serverAPI, Timeout: f.timeout, Logger: f.logger, + Name: driverutil.FindOp, }.Execute(ctx) } diff --git a/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/operation/find_and_modify.go b/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/operation/find_and_modify.go index 38d001c71..7faf56113 100644 --- a/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/operation/find_and_modify.go +++ b/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/operation/find_and_modify.go @@ -15,6 +15,7 @@ import ( "go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/bson/bsontype" "go.mongodb.org/mongo-driver/event" + "go.mongodb.org/mongo-driver/internal/driverutil" "go.mongodb.org/mongo-driver/mongo/description" "go.mongodb.org/mongo-driver/mongo/writeconcern" "go.mongodb.org/mongo-driver/x/bsonx/bsoncore" @@ -143,6 +144,7 @@ func (fam *FindAndModify) Execute(ctx context.Context) error { Crypt: fam.crypt, ServerAPI: fam.serverAPI, Timeout: fam.timeout, + Name: driverutil.FindAndModifyOp, }.Execute(ctx) } diff --git a/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/operation/hello.go b/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/operation/hello.go index f0d98ee70..6e750fd03 100644 --- a/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/operation/hello.go +++ b/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/operation/hello.go @@ -15,7 +15,9 @@ import ( "strings" "go.mongodb.org/mongo-driver/bson" - "go.mongodb.org/mongo-driver/internal" + "go.mongodb.org/mongo-driver/internal/bsonutil" + "go.mongodb.org/mongo-driver/internal/driverutil" + "go.mongodb.org/mongo-driver/internal/handshake" "go.mongodb.org/mongo-driver/mongo/address" "go.mongodb.org/mongo-driver/mongo/description" "go.mongodb.org/mongo-driver/version" @@ -30,7 +32,6 @@ import ( // sharded clusters is 512. const maxClientMetadataSize = 512 -const awsLambdaPrefix = "AWS_Lambda_" const driverName = "mongo-go-driver" // Hello is used to run the handshake operation. @@ -124,48 +125,30 @@ func (h *Hello) Result(addr address.Address) description.Server { return description.NewServer(addr, bson.Raw(h.res)) } -const ( - // FaaS environment variable names - envVarAWSExecutionEnv = "AWS_EXECUTION_ENV" - envVarAWSLambdaRuntimeAPI = "AWS_LAMBDA_RUNTIME_API" - envVarFunctionsWorkerRuntime = "FUNCTIONS_WORKER_RUNTIME" - envVarKService = "K_SERVICE" - envVarFunctionName = "FUNCTION_NAME" - envVarVercel = "VERCEL" -) +const dockerEnvPath = "/.dockerenv" const ( - // FaaS environment variable names - envVarAWSRegion = "AWS_REGION" - envVarAWSLambdaFunctionMemorySize = "AWS_LAMBDA_FUNCTION_MEMORY_SIZE" - envVarFunctionMemoryMB = "FUNCTION_MEMORY_MB" - envVarFunctionTimeoutSec = "FUNCTION_TIMEOUT_SEC" - envVarFunctionRegion = "FUNCTION_REGION" - envVarVercelRegion = "VERCEL_REGION" -) + // Runtime names + runtimeNameDocker = "docker" -const ( - // FaaS environment names used by the client - envNameAWSLambda = "aws.lambda" - envNameAzureFunc = "azure.func" - envNameGCPFunc = "gcp.func" - envNameVercel = "vercel" + // Orchestrator names + orchestratorNameK8s = "kubernetes" ) // getFaasEnvName parses the FaaS environment variable name and returns the // corresponding name used by the client. If none of the variables or variables -// for multiple names are populated the client.env value MUST be entirely -// omitted. When variables for multiple "client.env.name" values are present, -// "vercel" takes precedence over "aws.lambda"; any other combination MUST cause -// "client.env" to be entirely omitted. +// for multiple names are populated the FaaS values MUST be entirely omitted. +// When variables for multiple "client.env.name" values are present, "vercel" +// takes precedence over "aws.lambda"; any other combination MUST cause FaaS +// values to be entirely omitted. func getFaasEnvName() string { envVars := []string{ - envVarAWSExecutionEnv, - envVarAWSLambdaRuntimeAPI, - envVarFunctionsWorkerRuntime, - envVarKService, - envVarFunctionName, - envVarVercel, + driverutil.EnvVarAWSExecutionEnv, + driverutil.EnvVarAWSLambdaRuntimeAPI, + driverutil.EnvVarFunctionsWorkerRuntime, + driverutil.EnvVarKService, + driverutil.EnvVarFunctionName, + driverutil.EnvVarVercel, } // If none of the variables are populated the client.env value MUST be @@ -181,23 +164,23 @@ func getFaasEnvName() string { var name string switch envVar { - case envVarAWSExecutionEnv: - if !strings.HasPrefix(val, awsLambdaPrefix) { + case driverutil.EnvVarAWSExecutionEnv: + if !strings.HasPrefix(val, driverutil.AwsLambdaPrefix) { continue } - name = envNameAWSLambda - case envVarAWSLambdaRuntimeAPI: - name = envNameAWSLambda - case envVarFunctionsWorkerRuntime: - name = envNameAzureFunc - case envVarKService, envVarFunctionName: - name = envNameGCPFunc - case envVarVercel: + name = driverutil.EnvNameAWSLambda + case driverutil.EnvVarAWSLambdaRuntimeAPI: + name = driverutil.EnvNameAWSLambda + case driverutil.EnvVarFunctionsWorkerRuntime: + name = driverutil.EnvNameAzureFunc + case driverutil.EnvVarKService, driverutil.EnvVarFunctionName: + name = driverutil.EnvNameGCPFunc + case driverutil.EnvVarVercel: // "vercel" takes precedence over "aws.lambda". - delete(names, envNameAWSLambda) + delete(names, driverutil.EnvNameAWSLambda) - name = envNameVercel + name = driverutil.EnvNameVercel } names[name] = struct{}{} @@ -217,6 +200,31 @@ func getFaasEnvName() string { return "" } +type containerInfo struct { + runtime string + orchestrator string +} + +// getContainerEnvInfo returns runtime and orchestrator of a container. +// If no fields is populated, the client.env.container value MUST be entirely +// omitted. +func getContainerEnvInfo() *containerInfo { + var runtime, orchestrator string + if _, err := os.Stat(dockerEnvPath); !os.IsNotExist(err) { + runtime = runtimeNameDocker + } + if v := os.Getenv(driverutil.EnvVarK8s); v != "" { + orchestrator = orchestratorNameK8s + } + if runtime != "" || orchestrator != "" { + return &containerInfo{ + runtime: runtime, + orchestrator: orchestrator, + } + } + return nil +} + // appendClientAppName appends the application metadata to the dst. It is the // responsibility of the caller to check that this appending does not cause dst // to exceed any size limitations. @@ -255,14 +263,20 @@ func appendClientEnv(dst []byte, omitNonName, omitDoc bool) ([]byte, error) { } name := getFaasEnvName() - if name == "" { + container := getContainerEnvInfo() + // Omit the entire 'env' if both name and container are empty because other + // fields depend on either of them. + if name == "" && container == nil { return dst, nil } var idx int32 idx, dst = bsoncore.AppendDocumentElementStart(dst, "env") - dst = bsoncore.AppendStringElement(dst, "name", name) + + if name != "" { + dst = bsoncore.AppendStringElement(dst, "name", name) + } addMem := func(envVar string) []byte { mem := os.Getenv(envVar) @@ -305,16 +319,33 @@ func appendClientEnv(dst []byte, omitNonName, omitDoc bool) ([]byte, error) { } if !omitNonName { + // No other FaaS fields will be populated if the name is empty. switch name { - case envNameAWSLambda: - dst = addMem(envVarAWSLambdaFunctionMemorySize) - dst = addRegion(envVarAWSRegion) - case envNameGCPFunc: - dst = addMem(envVarFunctionMemoryMB) - dst = addRegion(envVarFunctionRegion) - dst = addTimeout(envVarFunctionTimeoutSec) - case envNameVercel: - dst = addRegion(envVarVercelRegion) + case driverutil.EnvNameAWSLambda: + dst = addMem(driverutil.EnvVarAWSLambdaFunctionMemorySize) + dst = addRegion(driverutil.EnvVarAWSRegion) + case driverutil.EnvNameGCPFunc: + dst = addMem(driverutil.EnvVarFunctionMemoryMB) + dst = addRegion(driverutil.EnvVarFunctionRegion) + dst = addTimeout(driverutil.EnvVarFunctionTimeoutSec) + case driverutil.EnvNameVercel: + dst = addRegion(driverutil.EnvVarVercelRegion) + } + } + + if container != nil { + var idxCntnr int32 + idxCntnr, dst = bsoncore.AppendDocumentElementStart(dst, "container") + if container.runtime != "" { + dst = bsoncore.AppendStringElement(dst, "runtime", container.runtime) + } + if container.orchestrator != "" { + dst = bsoncore.AppendStringElement(dst, "orchestrator", container.orchestrator) + } + var err error + dst, err = bsoncore.AppendDocumentEnd(dst, idxCntnr) + if err != nil { + return dst, err } } @@ -357,21 +388,25 @@ func appendClientPlatform(dst []byte) []byte { // name: "" // }, // driver: { -// name: "", -// version: "" +// name: "", +// version: "" // }, // platform: "", // os: { -// type: "", -// name: "", -// architecture: "", -// version: "" +// type: "", +// name: "", +// architecture: "", +// version: "" // }, // env: { -// name: "", -// timeout_sec: 42, -// memory_mb: 1024, -// region: "", +// name: "", +// timeout_sec: 42, +// memory_mb: 1024, +// region: "", +// container: { +// runtime: "", +// orchestrator: "" +// } // } // } func encodeClientMetadata(appname string, maxLen int) ([]byte, error) { @@ -420,7 +455,7 @@ retry: } if len(dst) > maxLen { - // Implementors SHOULD cumulatively update fields in the + // Implementers SHOULD cumulatively update fields in the // following order until the document is under the size limit // // 1. Omit fields from ``env`` except ``env.name`` @@ -498,7 +533,7 @@ func (h *Hello) command(dst []byte, desc description.SelectedServer) ([]byte, er if desc.Kind == description.LoadBalanced || h.serverAPI != nil || desc.Server.HelloOK { dst = bsoncore.AppendInt32Element(dst, "hello", 1) } else { - dst = bsoncore.AppendInt32Element(dst, internal.LegacyHello, 1) + dst = bsoncore.AppendInt32Element(dst, handshake.LegacyHello, 1) } dst = bsoncore.AppendBooleanElement(dst, "helloOk", true) @@ -536,8 +571,16 @@ func (h *Hello) StreamResponse(ctx context.Context, conn driver.StreamerConnecti return h.createOperation().ExecuteExhaust(ctx, conn) } +// isLegacyHandshake returns True if server API version is not requested and +// loadBalanced is False. If this is the case, then the drivers MUST use legacy +// hello for the first message of the initial handshake with the OP_QUERY +// protocol +func isLegacyHandshake(srvAPI *driver.ServerAPIOptions, deployment driver.Deployment) bool { + return srvAPI == nil && deployment.Kind() != description.LoadBalanced +} + func (h *Hello) createOperation() driver.Operation { - return driver.Operation{ + op := driver.Operation{ Clock: h.clock, CommandFn: h.command, Database: "admin", @@ -548,23 +591,36 @@ func (h *Hello) createOperation() driver.Operation { }, ServerAPI: h.serverAPI, } + + if isLegacyHandshake(h.serverAPI, h.d) { + op.Legacy = driver.LegacyHandshake + } + + return op } // GetHandshakeInformation performs the MongoDB handshake for the provided connection and returns the relevant // information about the server. This function implements the driver.Handshaker interface. func (h *Hello) GetHandshakeInformation(ctx context.Context, _ address.Address, c driver.Connection) (driver.HandshakeInformation, error) { - err := driver.Operation{ + deployment := driver.SingleConnectionDeployment{C: c} + + op := driver.Operation{ Clock: h.clock, CommandFn: h.handshakeCommand, - Deployment: driver.SingleConnectionDeployment{C: c}, + Deployment: deployment, Database: "admin", ProcessResponseFn: func(info driver.ResponseInfo) error { h.res = info.ServerResponse return nil }, ServerAPI: h.serverAPI, - }.Execute(ctx) - if err != nil { + } + + if isLegacyHandshake(h.serverAPI, deployment) { + op.Legacy = driver.LegacyHandshake + } + + if err := op.Execute(ctx); err != nil { return driver.HandshakeInformation{}, err } @@ -577,10 +633,13 @@ func (h *Hello) GetHandshakeInformation(ctx context.Context, _ address.Address, if serverConnectionID, ok := h.res.Lookup("connectionId").AsInt64OK(); ok { info.ServerConnectionID = &serverConnectionID } + + var err error + // Cast to bson.Raw to lookup saslSupportedMechs to avoid converting from bsoncore.Value to bson.RawValue for the // StringSliceFromRawValue call. if saslSupportedMechs, lookupErr := bson.Raw(h.res).LookupErr("saslSupportedMechs"); lookupErr == nil { - info.SaslSupportedMechs, err = internal.StringSliceFromRawValue("saslSupportedMechs", saslSupportedMechs) + info.SaslSupportedMechs, err = bsonutil.StringSliceFromRawValue("saslSupportedMechs", saslSupportedMechs) } return info, err } diff --git a/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/operation/insert.go b/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/operation/insert.go index 601e47eac..7da4b8b0f 100644 --- a/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/operation/insert.go +++ b/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/operation/insert.go @@ -14,6 +14,7 @@ import ( "go.mongodb.org/mongo-driver/bson/bsontype" "go.mongodb.org/mongo-driver/event" + "go.mongodb.org/mongo-driver/internal/driverutil" "go.mongodb.org/mongo-driver/internal/logger" "go.mongodb.org/mongo-driver/mongo/description" "go.mongodb.org/mongo-driver/mongo/writeconcern" @@ -113,6 +114,7 @@ func (i *Insert) Execute(ctx context.Context) error { ServerAPI: i.serverAPI, Timeout: i.timeout, Logger: i.logger, + Name: driverutil.InsertOp, }.Execute(ctx) } diff --git a/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/operation/listDatabases.go b/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/operation/listDatabases.go index 514d1dc3e..c70248e2a 100644 --- a/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/operation/listDatabases.go +++ b/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/operation/listDatabases.go @@ -14,6 +14,7 @@ import ( "go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/event" + "go.mongodb.org/mongo-driver/internal/driverutil" "go.mongodb.org/mongo-driver/mongo/description" "go.mongodb.org/mongo-driver/mongo/readpref" "go.mongodb.org/mongo-driver/x/bsonx/bsoncore" @@ -163,6 +164,7 @@ func (ld *ListDatabases) Execute(ctx context.Context) error { Crypt: ld.crypt, ServerAPI: ld.serverAPI, Timeout: ld.timeout, + Name: driverutil.ListDatabasesOp, }.Execute(ctx) } diff --git a/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/operation/list_collections.go b/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/operation/list_collections.go index 6f15bbeec..6fe68fa03 100644 --- a/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/operation/list_collections.go +++ b/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/operation/list_collections.go @@ -12,6 +12,7 @@ import ( "time" "go.mongodb.org/mongo-driver/event" + "go.mongodb.org/mongo-driver/internal/driverutil" "go.mongodb.org/mongo-driver/mongo/description" "go.mongodb.org/mongo-driver/mongo/readpref" "go.mongodb.org/mongo-driver/x/bsonx/bsoncore" @@ -47,17 +48,10 @@ func NewListCollections(filter bsoncore.Document) *ListCollections { } // Result returns the result of executing this operation. -func (lc *ListCollections) Result(opts driver.CursorOptions) (*driver.ListCollectionsBatchCursor, error) { +func (lc *ListCollections) Result(opts driver.CursorOptions) (*driver.BatchCursor, error) { opts.ServerAPI = lc.serverAPI - bc, err := driver.NewBatchCursor(lc.result, lc.session, lc.clock, opts) - if err != nil { - return nil, err - } - desc := lc.result.Desc - if desc.WireVersion == nil || desc.WireVersion.Max < 3 { - return driver.NewLegacyListCollectionsBatchCursor(bc) - } - return driver.NewListCollectionsBatchCursor(bc) + + return driver.NewBatchCursor(lc.result, lc.session, lc.clock, opts) } func (lc *ListCollections) processResponse(info driver.ResponseInfo) error { @@ -88,6 +82,7 @@ func (lc *ListCollections) Execute(ctx context.Context) error { Legacy: driver.LegacyListCollections, ServerAPI: lc.serverAPI, Timeout: lc.timeout, + Name: driverutil.ListCollectionsOp, }.Execute(ctx) } diff --git a/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/operation/list_indexes.go b/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/operation/list_indexes.go index e9485cf63..79d50eca9 100644 --- a/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/operation/list_indexes.go +++ b/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/operation/list_indexes.go @@ -12,6 +12,7 @@ import ( "time" "go.mongodb.org/mongo-driver/event" + "go.mongodb.org/mongo-driver/internal/driverutil" "go.mongodb.org/mongo-driver/mongo/description" "go.mongodb.org/mongo-driver/x/bsonx/bsoncore" "go.mongodb.org/mongo-driver/x/mongo/driver" @@ -83,6 +84,7 @@ func (li *ListIndexes) Execute(ctx context.Context) error { Type: driver.Read, ServerAPI: li.serverAPI, Timeout: li.timeout, + Name: driverutil.ListIndexesOp, }.Execute(ctx) } diff --git a/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/operation/update.go b/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/operation/update.go index 474ccca31..881b1bcf7 100644 --- a/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/operation/update.go +++ b/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/operation/update.go @@ -15,6 +15,7 @@ import ( "go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/bson/bsontype" "go.mongodb.org/mongo-driver/event" + "go.mongodb.org/mongo-driver/internal/driverutil" "go.mongodb.org/mongo-driver/internal/logger" "go.mongodb.org/mongo-driver/mongo/description" "go.mongodb.org/mongo-driver/mongo/writeconcern" @@ -165,6 +166,7 @@ func (u *Update) Execute(ctx context.Context) error { ServerAPI: u.serverAPI, Timeout: u.timeout, Logger: u.logger, + Name: driverutil.UpdateOp, }.Execute(ctx) } diff --git a/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/operation/update_search_index.go b/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/operation/update_search_index.go new file mode 100644 index 000000000..ba807986c --- /dev/null +++ b/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/operation/update_search_index.go @@ -0,0 +1,240 @@ +// Copyright (C) MongoDB, Inc. 2023-present. +// +// Licensed under the Apache License, Version 2.0 (the "License"); you may +// not use this file except in compliance with the License. You may obtain +// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 + +package operation + +import ( + "context" + "errors" + "fmt" + "time" + + "go.mongodb.org/mongo-driver/event" + "go.mongodb.org/mongo-driver/mongo/description" + "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/session" +) + +// UpdateSearchIndex performs a updateSearchIndex operation. +type UpdateSearchIndex struct { + index string + definition bsoncore.Document + session *session.Client + clock *session.ClusterClock + collection string + monitor *event.CommandMonitor + crypt driver.Crypt + database string + deployment driver.Deployment + selector description.ServerSelector + writeConcern *writeconcern.WriteConcern + result UpdateSearchIndexResult + serverAPI *driver.ServerAPIOptions + timeout *time.Duration +} + +// UpdateSearchIndexResult represents a single index in the updateSearchIndexResult result. +type UpdateSearchIndexResult struct { + Ok int32 +} + +func buildUpdateSearchIndexResult(response bsoncore.Document) (UpdateSearchIndexResult, error) { + elements, err := response.Elements() + if err != nil { + return UpdateSearchIndexResult{}, err + } + usir := UpdateSearchIndexResult{} + for _, element := range elements { + switch element.Key() { + case "ok": + var ok bool + usir.Ok, ok = element.Value().AsInt32OK() + if !ok { + return usir, fmt.Errorf("response field 'ok' is type int32, but received BSON type %s", element.Value().Type) + } + } + } + return usir, nil +} + +// NewUpdateSearchIndex constructs and returns a new UpdateSearchIndex. +func NewUpdateSearchIndex(index string, definition bsoncore.Document) *UpdateSearchIndex { + return &UpdateSearchIndex{ + index: index, + definition: definition, + } +} + +// Result returns the result of executing this operation. +func (usi *UpdateSearchIndex) Result() UpdateSearchIndexResult { return usi.result } + +func (usi *UpdateSearchIndex) processResponse(info driver.ResponseInfo) error { + var err error + usi.result, err = buildUpdateSearchIndexResult(info.ServerResponse) + return err +} + +// Execute runs this operations and returns an error if the operation did not execute successfully. +func (usi *UpdateSearchIndex) Execute(ctx context.Context) error { + if usi.deployment == nil { + return errors.New("the UpdateSearchIndex operation must have a Deployment set before Execute can be called") + } + + return driver.Operation{ + CommandFn: usi.command, + ProcessResponseFn: usi.processResponse, + Client: usi.session, + Clock: usi.clock, + CommandMonitor: usi.monitor, + Crypt: usi.crypt, + Database: usi.database, + Deployment: usi.deployment, + Selector: usi.selector, + WriteConcern: usi.writeConcern, + ServerAPI: usi.serverAPI, + Timeout: usi.timeout, + }.Execute(ctx) + +} + +func (usi *UpdateSearchIndex) command(dst []byte, _ description.SelectedServer) ([]byte, error) { + dst = bsoncore.AppendStringElement(dst, "updateSearchIndex", usi.collection) + dst = bsoncore.AppendStringElement(dst, "name", usi.index) + dst = bsoncore.AppendDocumentElement(dst, "definition", usi.definition) + return dst, nil +} + +// Index specifies the index of the document being updated. +func (usi *UpdateSearchIndex) Index(name string) *UpdateSearchIndex { + if usi == nil { + usi = new(UpdateSearchIndex) + } + + usi.index = name + return usi +} + +// Definition specifies the definition for the document being created. +func (usi *UpdateSearchIndex) Definition(definition bsoncore.Document) *UpdateSearchIndex { + if usi == nil { + usi = new(UpdateSearchIndex) + } + + usi.definition = definition + return usi +} + +// Session sets the session for this operation. +func (usi *UpdateSearchIndex) Session(session *session.Client) *UpdateSearchIndex { + if usi == nil { + usi = new(UpdateSearchIndex) + } + + usi.session = session + return usi +} + +// ClusterClock sets the cluster clock for this operation. +func (usi *UpdateSearchIndex) ClusterClock(clock *session.ClusterClock) *UpdateSearchIndex { + if usi == nil { + usi = new(UpdateSearchIndex) + } + + usi.clock = clock + return usi +} + +// Collection sets the collection that this command will run against. +func (usi *UpdateSearchIndex) Collection(collection string) *UpdateSearchIndex { + if usi == nil { + usi = new(UpdateSearchIndex) + } + + usi.collection = collection + return usi +} + +// CommandMonitor sets the monitor to use for APM events. +func (usi *UpdateSearchIndex) CommandMonitor(monitor *event.CommandMonitor) *UpdateSearchIndex { + if usi == nil { + usi = new(UpdateSearchIndex) + } + + usi.monitor = monitor + return usi +} + +// Crypt sets the Crypt object to use for automatic encryption and decryption. +func (usi *UpdateSearchIndex) Crypt(crypt driver.Crypt) *UpdateSearchIndex { + if usi == nil { + usi = new(UpdateSearchIndex) + } + + usi.crypt = crypt + return usi +} + +// Database sets the database to run this operation against. +func (usi *UpdateSearchIndex) Database(database string) *UpdateSearchIndex { + if usi == nil { + usi = new(UpdateSearchIndex) + } + + usi.database = database + return usi +} + +// Deployment sets the deployment to use for this operation. +func (usi *UpdateSearchIndex) Deployment(deployment driver.Deployment) *UpdateSearchIndex { + if usi == nil { + usi = new(UpdateSearchIndex) + } + + usi.deployment = deployment + return usi +} + +// ServerSelector sets the selector used to retrieve a server. +func (usi *UpdateSearchIndex) ServerSelector(selector description.ServerSelector) *UpdateSearchIndex { + if usi == nil { + usi = new(UpdateSearchIndex) + } + + usi.selector = selector + return usi +} + +// WriteConcern sets the write concern for this operation. +func (usi *UpdateSearchIndex) WriteConcern(writeConcern *writeconcern.WriteConcern) *UpdateSearchIndex { + if usi == nil { + usi = new(UpdateSearchIndex) + } + + usi.writeConcern = writeConcern + return usi +} + +// ServerAPI sets the server API version for this operation. +func (usi *UpdateSearchIndex) ServerAPI(serverAPI *driver.ServerAPIOptions) *UpdateSearchIndex { + if usi == nil { + usi = new(UpdateSearchIndex) + } + + usi.serverAPI = serverAPI + return usi +} + +// Timeout sets the timeout for this operation. +func (usi *UpdateSearchIndex) Timeout(timeout *time.Duration) *UpdateSearchIndex { + if usi == nil { + usi = new(UpdateSearchIndex) + } + + usi.timeout = timeout + return usi +} diff --git a/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/session/server_session.go b/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/session/server_session.go index 044cbd497..b1e45552a 100644 --- a/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/session/server_session.go +++ b/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/session/server_session.go @@ -31,11 +31,11 @@ func (ss *Server) expired(topoDesc topologyDescription) bool { return false } - if topoDesc.timeoutMinutes <= 0 { + if topoDesc.timeoutMinutes == nil || *topoDesc.timeoutMinutes <= 0 { return true } timeUnused := time.Since(ss.LastUsed).Minutes() - return timeUnused > float64(topoDesc.timeoutMinutes-1) + return timeUnused > float64(*topoDesc.timeoutMinutes-1) } // update the last used time for this session. diff --git a/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/session/session_pool.go b/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/session/session_pool.go index 34b863c11..7336f5451 100644 --- a/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/session/session_pool.go +++ b/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/session/session_pool.go @@ -25,7 +25,7 @@ type Node struct { // relevant for determining session expiration. type topologyDescription struct { kind description.TopologyKind - timeoutMinutes uint32 + timeoutMinutes *int64 } // Pool is a pool of server sessions that can be reused. @@ -65,7 +65,7 @@ func (p *Pool) updateTimeout() { case newDesc := <-p.descChan: p.latestTopology = topologyDescription{ kind: newDesc.Kind, - timeoutMinutes: newDesc.SessionTimeoutMinutes, + timeoutMinutes: newDesc.SessionTimeoutMinutesPtr, } default: // no new description waiting diff --git a/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/topology/DESIGN.md b/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/topology/DESIGN.md index 6594a85d0..8a67dd993 100644 --- a/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/topology/DESIGN.md +++ b/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/topology/DESIGN.md @@ -1,7 +1,9 @@ # Topology Package Design + This document outlines the design for this package. ## Topology + The `Topology` type handles monitoring the state of a MongoDB deployment and selecting servers. Updating the description is handled by finite state machine which implements the server discovery and monitoring specification. A `Topology` can be connected and fully disconnected, which enables @@ -9,9 +11,11 @@ saving resources. The `Topology` type also handles server selection following th specification. ## Server + The `Server` type handles heartbeating a MongoDB server and holds a pool of connections. ## Connection + Connections are handled by two main types and an auxiliary type. The two main types are `connection` and `Connection`. The first holds most of the logic required to actually read and write wire messages. Instances can be created with the `newConnection` method. Inside the `newConnection` @@ -26,6 +30,7 @@ The connection implementations in this package are conduits for wire messages bu ability to encode, decode, or validate wire messages. That must be handled by consumers. ## Pool + The `pool` type implements a connection pool. It handles caching idle connections and dialing new ones, but it does not track a maximum number of connections. That is the responsibility of a wrapping type, like `Server`. @@ -37,4 +42,4 @@ There is a `close` method, but this is used to close a connection. There are three methods related to getting and putting connections: `get`, `close`, and `put`. The `get` method will either retrieve a connection from the cache or it will dial a new `connection`. The `close` method will close the underlying socket of a `connection`. The `put` method will put a -connection into the pool, placing it in the cahce if there is space, otherwise it will close it. +connection into the pool, placing it in the cache if there is space, otherwise it will close it. diff --git a/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/topology/connection.go b/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/topology/connection.go index 6e8fd5297..af25b1f68 100644 --- a/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/topology/connection.go +++ b/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/topology/connection.go @@ -18,7 +18,6 @@ import ( "sync/atomic" "time" - "go.mongodb.org/mongo-driver/internal" "go.mongodb.org/mongo-driver/mongo/address" "go.mongodb.org/mongo-driver/mongo/description" "go.mongodb.org/mongo-driver/x/bsonx/bsoncore" @@ -95,7 +94,7 @@ func newConnection(addr address.Address, opts ...ConnectionOption) *connection { connectDone: make(chan struct{}), config: cfg, connectContextMade: make(chan struct{}), - cancellationListener: internal.NewCancellationListener(), + cancellationListener: newCancellListener(), } // Connections to non-load balanced deployments should eagerly set the generation numbers so errors encountered // at any point during connection establishment can be processed without the connection being considered stale. @@ -840,3 +839,47 @@ func configureTLS(ctx context.Context, } return client, nil } + +// TODO: Naming? + +// cancellListener listens for context cancellation and notifies listeners via a +// callback function. +type cancellListener struct { + aborted bool + done chan struct{} +} + +// newCancellListener constructs a cancellListener. +func newCancellListener() *cancellListener { + return &cancellListener{ + done: make(chan struct{}), + } +} + +// Listen blocks until the provided context is cancelled or listening is aborted +// via the StopListening function. If this detects that the context has been +// cancelled (i.e. ctx.Err() == context.Canceled), the provided callback is +// called to abort in-progress work. Even if the context expires, this function +// will block until StopListening is called. +func (c *cancellListener) Listen(ctx context.Context, abortFn func()) { + c.aborted = false + + select { + case <-ctx.Done(): + if ctx.Err() == context.Canceled { + c.aborted = true + abortFn() + } + + <-c.done + case <-c.done: + } +} + +// StopListening stops the in-progress Listen call. This blocks if there is no +// in-progress Listen call. This function will return true if the provided abort +// callback was called when listening for cancellation on the previous context. +func (c *cancellListener) StopListening() bool { + c.done <- struct{}{} + return c.aborted +} diff --git a/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/topology/connection_options.go b/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/topology/connection_options.go index 6e6ea01d8..43e6f3f50 100644 --- a/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/topology/connection_options.go +++ b/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/topology/connection_options.go @@ -15,7 +15,7 @@ import ( "go.mongodb.org/mongo-driver/bson/primitive" "go.mongodb.org/mongo-driver/event" - "go.mongodb.org/mongo-driver/internal" + "go.mongodb.org/mongo-driver/internal/httputil" "go.mongodb.org/mongo-driver/x/mongo/driver" "go.mongodb.org/mongo-driver/x/mongo/driver/ocsp" ) @@ -72,7 +72,7 @@ func newConnectionConfig(opts ...ConnectionOption) *connectionConfig { connectTimeout: 30 * time.Second, dialer: nil, tlsConnectionSource: defaultTLSConnectionSource, - httpClient: internal.DefaultHTTPClient, + httpClient: httputil.DefaultHTTPClient, } for _, opt := range opts { @@ -83,6 +83,8 @@ func newConnectionConfig(opts ...ConnectionOption) *connectionConfig { } if cfg.dialer == nil { + // Use a zero value of net.Dialer when nothing is specified, so the Go driver applies default default behaviors + // such as Timeout, KeepAlive, DNS resolving, etc. See https://golang.org/pkg/net/#Dialer for more information. cfg.dialer = &net.Dialer{} } diff --git a/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/topology/errors.go b/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/topology/errors.go index 4f7b48540..7ce41864e 100644 --- a/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/topology/errors.go +++ b/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/topology/errors.go @@ -9,6 +9,7 @@ package topology import ( "context" "fmt" + "time" "go.mongodb.org/mongo-driver/mongo/description" ) @@ -69,11 +70,17 @@ func (e ServerSelectionError) Unwrap() error { // WaitQueueTimeoutError represents a timeout when requesting a connection from the pool type WaitQueueTimeoutError struct { - Wrapped error - PinnedCursorConnections uint64 - PinnedTransactionConnections uint64 - maxPoolSize uint64 - totalConnectionCount int + Wrapped error + pinnedConnections *pinnedConnections + maxPoolSize uint64 + totalConnections int + availableConnections int + waitDuration time.Duration +} + +type pinnedConnections struct { + cursorConnections uint64 + transactionConnections uint64 } // Error implements the error interface. @@ -95,14 +102,19 @@ func (w WaitQueueTimeoutError) Error() string { ) } - return fmt.Sprintf( - "%s; maxPoolSize: %d, connections in use by cursors: %d"+ - ", connections in use by transactions: %d, connections in use by other operations: %d", - errorMsg, - w.maxPoolSize, - w.PinnedCursorConnections, - w.PinnedTransactionConnections, - uint64(w.totalConnectionCount)-w.PinnedCursorConnections-w.PinnedTransactionConnections) + msg := fmt.Sprintf("%s; total connections: %d, maxPoolSize: %d, ", errorMsg, w.totalConnections, w.maxPoolSize) + if pinnedConnections := w.pinnedConnections; pinnedConnections != nil { + openConnectionCount := uint64(w.totalConnections) - + pinnedConnections.cursorConnections - + pinnedConnections.transactionConnections + msg += fmt.Sprintf("connections in use by cursors: %d, connections in use by transactions: %d, connections in use by other operations: %d, ", + pinnedConnections.cursorConnections, + pinnedConnections.transactionConnections, + openConnectionCount, + ) + } + msg += fmt.Sprintf("idle connections: %d, wait duration: %s", w.availableConnections, w.waitDuration.String()) + return msg } // Unwrap returns the underlying error. diff --git a/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/topology/fsm.go b/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/topology/fsm.go index 1251456c6..2acf527b9 100644 --- a/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/topology/fsm.go +++ b/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/topology/fsm.go @@ -12,6 +12,7 @@ import ( "sync/atomic" "go.mongodb.org/mongo-driver/bson/primitive" + "go.mongodb.org/mongo-driver/internal/ptrutil" "go.mongodb.org/mongo-driver/mongo/address" "go.mongodb.org/mongo-driver/mongo/description" ) @@ -38,6 +39,63 @@ func newFSM() *fsm { return &f } +// selectFSMSessionTimeout selects the timeout to return for the topology's +// finite state machine. If the logicalSessionTimeoutMinutes on the FSM exists +// and the server is data-bearing, then we determine this value by returning +// +// min{server timeout, FSM timeout} +// +// where a "nil" value is considered less than 0. +// +// Otherwise, if the FSM's logicalSessionTimeoutMinutes exist, then this +// function returns the FSM timeout. +// +// In the case where the FSM timeout DNE, we check all servers to see if any +// still do not have a timeout. This function chooses the lowest of the existing +// timeouts. +func selectFSMSessionTimeout(f *fsm, s description.Server) *int64 { + oldMinutes := f.SessionTimeoutMinutesPtr + comp := ptrutil.CompareInt64(oldMinutes, s.SessionTimeoutMinutesPtr) + + // If the server is data-bearing and the current timeout exists and is + // either: + // + // 1. larger than the server timeout, or + // 2. non-nil while the server timeout is nil + // + // then return the server timeout. + if s.DataBearing() && (comp == 1 || comp == 2) { + return s.SessionTimeoutMinutesPtr + } + + // If the current timeout exists and the server is not data-bearing OR + // min{server timeout, current timeout} = current timeout, then return + // the current timeout. + if oldMinutes != nil { + return oldMinutes + } + + timeout := s.SessionTimeoutMinutesPtr + for _, server := range f.Servers { + // If the server is not data-bearing, then we do not consider + // it's timeout whether set or not. + if !server.DataBearing() { + continue + } + + srvTimeout := server.SessionTimeoutMinutesPtr + comp := ptrutil.CompareInt64(timeout, srvTimeout) + + if comp <= 0 { // timeout <= srvTimout + continue + } + + timeout = server.SessionTimeoutMinutesPtr + } + + return timeout +} + // apply takes a new server description and modifies the FSM's topology description based on it. It returns the // updated topology description as well as a server description. The returned server description is either the same // one that was passed in, or a new one in the case that it had to be changed. @@ -48,30 +106,20 @@ func (f *fsm) apply(s description.Server) (description.Topology, description.Ser newServers := make([]description.Server, len(f.Servers)) copy(newServers, f.Servers) - oldMinutes := f.SessionTimeoutMinutes + // Reset the logicalSessionTimeoutMinutes to the minimum of the FSM + // and the description.server/f.servers. + serverTimeoutMinutes := selectFSMSessionTimeout(f, s) + f.Topology = description.Topology{ Kind: f.Kind, Servers: newServers, SetName: f.SetName, } - // For data bearing servers, set SessionTimeoutMinutes to the lowest among them - if oldMinutes == 0 { - // If timeout currently 0, check all servers to see if any still don't have a timeout - // If they all have timeout, pick the lowest. - timeout := s.SessionTimeoutMinutes - for _, server := range f.Servers { - if server.DataBearing() && server.SessionTimeoutMinutes < timeout { - timeout = server.SessionTimeoutMinutes - } - } - f.SessionTimeoutMinutes = timeout - } else { - if s.DataBearing() && oldMinutes > s.SessionTimeoutMinutes { - f.SessionTimeoutMinutes = s.SessionTimeoutMinutes - } else { - f.SessionTimeoutMinutes = oldMinutes - } + f.Topology.SessionTimeoutMinutesPtr = serverTimeoutMinutes + + if serverTimeoutMinutes != nil { + f.SessionTimeoutMinutes = uint32(*serverTimeoutMinutes) } if _, ok := f.findServer(s.Addr); !ok { @@ -124,6 +172,7 @@ func (f *fsm) apply(s description.Server) (description.Topology, description.Ser f.compatible.Store(true) f.compatibilityErr = nil + return f.Topology, updatedDesc } @@ -234,7 +283,7 @@ func hasStalePrimary(fsm fsm, srv description.Server) bool { compRes := bytes.Compare(srv.ElectionID[:], fsm.maxElectionID[:]) if wireVersion := srv.WireVersion; wireVersion != nil && wireVersion.Max >= 17 { - // In the Post-6.0 case, a primary is considered "stale" if the server's election ID is greather than the + // In the Post-6.0 case, a primary is considered "stale" if the server's election ID is greater than the // topology's max election ID. In these versions, the primary is also considered "stale" if the server's // election ID is LTE to the topologies election ID and the server's "setVersion" is less than the topology's // max "setVersion". diff --git a/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/topology/pool.go b/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/topology/pool.go index da40fd6a8..6e150344d 100644 --- a/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/topology/pool.go +++ b/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/topology/pool.go @@ -74,6 +74,7 @@ type poolConfig struct { MaxConnecting uint64 MaxIdleTime time.Duration MaintainInterval time.Duration + LoadBalanced bool PoolMonitor *event.PoolMonitor Logger *logger.Logger handshakeErrFn func(error, uint64, *primitive.ObjectID) @@ -93,6 +94,7 @@ type pool struct { minSize uint64 maxSize uint64 maxConnecting uint64 + loadBalanced bool monitor *event.PoolMonitor logger *logger.Logger @@ -206,6 +208,7 @@ func newPool(config poolConfig, connOpts ...ConnectionOption) *pool { minSize: config.MinPoolSize, maxSize: config.MaxPoolSize, maxConnecting: maxConnecting, + loadBalanced: config.LoadBalanced, monitor: config.PoolMonitor, logger: config.Logger, handshakeErrFn: config.handshakeErrFn, @@ -500,6 +503,7 @@ func (p *pool) checkOut(ctx context.Context) (conn *connection, err error) { Type: event.GetFailed, Address: p.address.String(), Reason: event.ReasonConnectionErrored, + Error: err, }) } return nil, err @@ -542,6 +546,7 @@ func (p *pool) checkOut(ctx context.Context) (conn *connection, err error) { Type: event.GetFailed, Address: p.address.String(), Reason: event.ReasonConnectionErrored, + Error: w.err, }) } return nil, w.err @@ -572,6 +577,7 @@ func (p *pool) checkOut(ctx context.Context) (conn *connection, err error) { p.stateMu.RUnlock() // Wait for either the wantConn to be ready or for the Context to time out. + start := time.Now() select { case <-w.ready: if w.err != nil { @@ -589,6 +595,7 @@ func (p *pool) checkOut(ctx context.Context) (conn *connection, err error) { Type: event.GetFailed, Address: p.address.String(), Reason: event.ReasonConnectionErrored, + Error: w.err, }) } @@ -612,6 +619,8 @@ func (p *pool) checkOut(ctx context.Context) (conn *connection, err error) { } return w.conn, nil case <-ctx.Done(): + duration := time.Since(start) + if mustLogPoolMessage(p) { keysAndValues := logger.KeyValues{ logger.KeyReason, logger.ReasonConnCheckoutFailedTimout, @@ -625,16 +634,24 @@ func (p *pool) checkOut(ctx context.Context) (conn *connection, err error) { Type: event.GetFailed, Address: p.address.String(), Reason: event.ReasonTimedOut, + Error: ctx.Err(), }) } - return nil, WaitQueueTimeoutError{ - Wrapped: ctx.Err(), - PinnedCursorConnections: atomic.LoadUint64(&p.pinnedCursorConnections), - PinnedTransactionConnections: atomic.LoadUint64(&p.pinnedTransactionConnections), - maxPoolSize: p.maxSize, - totalConnectionCount: p.totalConnectionCount(), + err := WaitQueueTimeoutError{ + Wrapped: ctx.Err(), + maxPoolSize: p.maxSize, + totalConnections: p.totalConnectionCount(), + availableConnections: p.availableConnectionCount(), + waitDuration: duration, + } + if p.loadBalanced { + err.pinnedConnections = &pinnedConnections{ + cursorConnections: atomic.LoadUint64(&p.pinnedCursorConnections), + transactionConnections: atomic.LoadUint64(&p.pinnedTransactionConnections), + } } + return nil, err } } @@ -878,6 +895,7 @@ func (p *pool) clear(err error, serviceID *primitive.ObjectID) { Type: event.PoolCleared, Address: p.address.String(), ServiceID: serviceID, + Error: err, }) } } diff --git a/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/topology/rtt_monitor.go b/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/topology/rtt_monitor.go index 998d2a025..0934beed8 100644 --- a/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/topology/rtt_monitor.go +++ b/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/topology/rtt_monitor.go @@ -39,7 +39,12 @@ type rttConfig struct { } type rttMonitor struct { - mu sync.RWMutex // mu guards samples, offset, minRTT, averageRTT, and averageRTTSet + mu sync.RWMutex // mu guards samples, offset, minRTT, averageRTT, and averageRTTSet + + // connMu guards connecting and disconnecting. This is necessary since + // disconnecting will await the cancellation of a started connection. The + // use case for rttMonitor.connect needs to be goroutine safe. + connMu sync.Mutex samples []time.Duration offset int minRTT time.Duration @@ -51,6 +56,7 @@ type rttMonitor struct { cfg *rttConfig ctx context.Context cancelFn context.CancelFunc + started bool } var _ driver.RTTMonitor = &rttMonitor{} @@ -74,19 +80,34 @@ func newRTTMonitor(cfg *rttConfig) *rttMonitor { } func (r *rttMonitor) connect() { + r.connMu.Lock() + defer r.connMu.Unlock() + + r.started = true r.closeWg.Add(1) - go r.start() + + go func() { + defer r.closeWg.Done() + + r.start() + }() } func (r *rttMonitor) disconnect() { - // Signal for the routine to stop. + r.connMu.Lock() + defer r.connMu.Unlock() + + if !r.started { + return + } + r.cancelFn() + + // Wait for the existing connection to complete. r.closeWg.Wait() } func (r *rttMonitor) start() { - defer r.closeWg.Done() - var conn *connection defer func() { if conn != nil { diff --git a/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/topology/server.go b/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/topology/server.go index f0a1c5b05..5823d3d7a 100644 --- a/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/topology/server.go +++ b/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/topology/server.go @@ -17,9 +17,12 @@ import ( "go.mongodb.org/mongo-driver/bson/primitive" "go.mongodb.org/mongo-driver/event" + "go.mongodb.org/mongo-driver/internal/driverutil" + "go.mongodb.org/mongo-driver/internal/logger" "go.mongodb.org/mongo-driver/mongo/address" "go.mongodb.org/mongo-driver/mongo/description" "go.mongodb.org/mongo-driver/x/mongo/driver" + "go.mongodb.org/mongo-driver/x/mongo/driver/connstring" "go.mongodb.org/mongo-driver/x/mongo/driver/operation" ) @@ -130,7 +133,12 @@ type updateTopologyCallback func(description.Server) description.Server // ConnectServer creates a new Server and then initializes it using the // Connect method. -func ConnectServer(addr address.Address, updateCallback updateTopologyCallback, topologyID primitive.ObjectID, opts ...ServerOption) (*Server, error) { +func ConnectServer( + addr address.Address, + updateCallback updateTopologyCallback, + topologyID primitive.ObjectID, + opts ...ServerOption, +) (*Server, error) { srvr := NewServer(addr, topologyID, opts...) err := srvr.Connect(updateCallback) if err != nil { @@ -176,6 +184,7 @@ func NewServer(addr address.Address, topologyID primitive.ObjectID, opts ...Serv MaxConnecting: cfg.maxConnecting, MaxIdleTime: cfg.poolMaxIdleTime, MaintainInterval: cfg.poolMaintainInterval, + LoadBalanced: cfg.loadBalanced, PoolMonitor: cfg.poolMonitor, Logger: cfg.logger, handshakeErrFn: s.ProcessHandshakeError, @@ -188,6 +197,39 @@ func NewServer(addr address.Address, topologyID primitive.ObjectID, opts ...Serv return s } +func mustLogServerMessage(srv *Server) bool { + return srv.cfg.logger != nil && srv.cfg.logger.LevelComponentEnabled( + logger.LevelDebug, logger.ComponentTopology) +} + +func logServerMessage(srv *Server, msg string, keysAndValues ...interface{}) { + serverHost, serverPort, err := net.SplitHostPort(srv.address.String()) + if err != nil { + serverHost = srv.address.String() + serverPort = "" + } + + var driverConnectionID uint64 + var serverConnectionID *int64 + + if srv.conn != nil { + driverConnectionID = srv.conn.driverConnectionID + serverConnectionID = srv.conn.serverConnectionID + } + + srv.cfg.logger.Print(logger.LevelDebug, + logger.ComponentTopology, + msg, + logger.SerializeServer(logger.Server{ + DriverConnectionID: driverConnectionID, + TopologyID: srv.topologyID, + Message: msg, + ServerConnectionID: serverConnectionID, + ServerHost: serverHost, + ServerPort: serverPort, + }, keysAndValues...)...) +} + // Connect initializes the Server by starting background monitoring goroutines. // This method must be called before a Server can be used. func (s *Server) Connect(updateCallback updateTopologyCallback) error { @@ -204,7 +246,6 @@ func (s *Server) Connect(updateCallback updateTopologyCallback) error { s.updateTopologyCallback.Store(updateCallback) if !s.cfg.monitoringDisabled && !s.cfg.loadBalanced { - s.rttMonitor.connect() s.closewg.Add(1) go s.update() } @@ -394,7 +435,7 @@ func (s *Server) ProcessError(err error, conn driver.Connection) driver.ProcessE } // Ignore errors from stale connections because the error came from a previous generation of the - // connection pool. The root cause of the error has aleady been handled, which is what caused + // connection pool. The root cause of the error has already been handled, which is what caused // the pool generation to increment. Processing errors for stale connections could result in // handling the same error root cause multiple times (e.g. a temporary network interrupt causing // all connections to the same server to return errors). @@ -497,7 +538,7 @@ func (s *Server) ProcessError(err error, conn driver.Connection) driver.ProcessE return driver.ConnectionPoolCleared } -// update handles performing heartbeats and updating any subscribers of the +// update handle performing heartbeats and updating any subscribers of the // newest description.Server retrieved. func (s *Server) update() { defer s.closewg.Done() @@ -613,12 +654,15 @@ func (s *Server) update() { // If the server supports streaming or we're already streaming, we want to move to streaming the next response // without waiting. If the server has transitioned to Unknown from a network error, we want to do another // check without waiting in case it was a transient error and the server isn't actually down. - serverSupportsStreaming := desc.Kind != description.Unknown && desc.TopologyVersion != nil connectionIsStreaming := s.conn != nil && s.conn.getCurrentlyStreaming() transitionedFromNetworkError := desc.LastError != nil && unwrapConnectionError(desc.LastError) != nil && previousDescription.Kind != description.Unknown - if serverSupportsStreaming || connectionIsStreaming || transitionedFromNetworkError { + if isStreamingEnabled(s) && isStreamable(s) && !s.rttMonitor.started { + s.rttMonitor.connect() + } + + if isStreamable(s) || connectionIsStreaming || transitionedFromNetworkError { continue } @@ -750,37 +794,55 @@ func (s *Server) createBaseOperation(conn driver.Connection) *operation.Hello { return operation. NewHello(). ClusterClock(s.cfg.clock). - Deployment(driver.SingleConnectionDeployment{conn}). + Deployment(driver.SingleConnectionDeployment{C: conn}). ServerAPI(s.cfg.serverAPI) } +func isStreamingEnabled(srv *Server) bool { + switch srv.cfg.serverMonitoringMode { + case connstring.ServerMonitoringModeStream: + return true + case connstring.ServerMonitoringModePoll: + return false + default: + return driverutil.GetFaasEnvName() == "" + } +} + +func isStreamable(srv *Server) bool { + return srv.Description().Kind != description.Unknown && srv.Description().TopologyVersion != nil +} + func (s *Server) check() (description.Server, error) { var descPtr *description.Server var err error var duration time.Duration start := time.Now() + + // Create a new connection if this is the first check, the connection was closed after an error during the previous + // check, or the previous check was cancelled. if s.conn == nil || s.conn.closed() || s.checkWasCancelled() { - // Create a new connection if this is the first check, the connection was closed after an error during the previous - // check, or the previous check was cancelled. + connID := "0" if s.conn != nil { - s.publishServerHeartbeatStartedEvent(s.conn.ID(), false) + connID = s.conn.ID() } + s.publishServerHeartbeatStartedEvent(connID, false) // Create a new connection and add it's handshake RTT as a sample. err = s.setupHeartbeatConnection() duration = time.Since(start) + connID = "0" + if s.conn != nil { + connID = s.conn.ID() + } if err == nil { // Use the description from the connection handshake as the value for this check. s.rttMonitor.addSample(s.conn.helloRTT) descPtr = &s.conn.desc - if s.conn != nil { - s.publishServerHeartbeatSucceededEvent(s.conn.ID(), duration, s.conn.desc, false) - } + s.publishServerHeartbeatSucceededEvent(connID, duration, s.conn.desc, false) } else { err = unwrapConnectionError(err) - if s.conn != nil { - s.publishServerHeartbeatFailedEvent(s.conn.ID(), duration, err, false) - } + s.publishServerHeartbeatFailedEvent(connID, duration, err, false) } } else { // An existing connection is being used. Use the server description properties to execute the right heartbeat. @@ -789,9 +851,10 @@ func (s *Server) check() (description.Server, error) { heartbeatConn := initConnection{s.conn} baseOperation := s.createBaseOperation(heartbeatConn) previousDescription := s.Description() - streamable := previousDescription.TopologyVersion != nil + streamable := isStreamingEnabled(s) && isStreamable(s) s.publishServerHeartbeatStartedEvent(s.conn.ID(), s.conn.getCurrentlyStreaming() || streamable) + switch { case s.conn.getCurrentlyStreaming(): // The connection is already in a streaming state, so we stream the next response. @@ -822,8 +885,16 @@ func (s *Server) check() (description.Server, error) { s.conn.setSocketTimeout(s.cfg.heartbeatTimeout) err = baseOperation.Execute(s.heartbeatCtx) } + duration = time.Since(start) + // We need to record an RTT sample in the polling case so that if the server + // is < 4.4, or if polling is specified by the user, then the + // RTT-short-circuit feature of CSOT is not disabled. + if !streamable { + s.rttMonitor.addSample(duration) + } + if err == nil { tempDesc := baseOperation.Result(s.address) descPtr = &tempDesc @@ -947,6 +1018,10 @@ func (s *Server) publishServerOpeningEvent(addr address.Address) { if s.cfg.serverMonitor != nil && s.cfg.serverMonitor.ServerOpening != nil { s.cfg.serverMonitor.ServerOpening(serverOpening) } + + if mustLogServerMessage(s) { + logServerMessage(s, logger.TopologyServerOpening) + } } // publishes a ServerHeartbeatStartedEvent to indicate a hello command has started @@ -959,6 +1034,11 @@ func (s *Server) publishServerHeartbeatStartedEvent(connectionID string, await b if s != nil && s.cfg.serverMonitor != nil && s.cfg.serverMonitor.ServerHeartbeatStarted != nil { s.cfg.serverMonitor.ServerHeartbeatStarted(serverHeartbeatStarted) } + + if mustLogServerMessage(s) { + logServerMessage(s, logger.TopologyServerHeartbeatStarted, + logger.KeyAwaited, await) + } } // publishes a ServerHeartbeatSucceededEvent to indicate hello has succeeded @@ -978,6 +1058,13 @@ func (s *Server) publishServerHeartbeatSucceededEvent(connectionID string, if s != nil && s.cfg.serverMonitor != nil && s.cfg.serverMonitor.ServerHeartbeatSucceeded != nil { s.cfg.serverMonitor.ServerHeartbeatSucceeded(serverHeartbeatSucceeded) } + + if mustLogServerMessage(s) { + logServerMessage(s, logger.TopologyServerHeartbeatStarted, + logger.KeyAwaited, await, + logger.KeyDurationMS, duration.Milliseconds(), + logger.KeyReply, desc) + } } // publishes a ServerHeartbeatFailedEvent to indicate hello has failed @@ -997,6 +1084,13 @@ func (s *Server) publishServerHeartbeatFailedEvent(connectionID string, if s != nil && s.cfg.serverMonitor != nil && s.cfg.serverMonitor.ServerHeartbeatFailed != nil { s.cfg.serverMonitor.ServerHeartbeatFailed(serverHeartbeatFailed) } + + if mustLogServerMessage(s) { + logServerMessage(s, logger.TopologyServerHeartbeatFailed, + logger.KeyAwaited, await, + logger.KeyDurationMS, duration.Milliseconds(), + logger.KeyFailure, err.Error()) + } } // unwrapConnectionError returns the connection error wrapped by err, or nil if err does not wrap a connection error. diff --git a/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/topology/server_options.go b/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/topology/server_options.go index 4272b3f75..4504a2535 100644 --- a/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/topology/server_options.go +++ b/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/topology/server_options.go @@ -14,23 +14,25 @@ import ( "go.mongodb.org/mongo-driver/event" "go.mongodb.org/mongo-driver/internal/logger" "go.mongodb.org/mongo-driver/x/mongo/driver" + "go.mongodb.org/mongo-driver/x/mongo/driver/connstring" "go.mongodb.org/mongo-driver/x/mongo/driver/session" ) var defaultRegistry = bson.NewRegistryBuilder().Build() type serverConfig struct { - clock *session.ClusterClock - compressionOpts []string - connectionOpts []ConnectionOption - appname string - heartbeatInterval time.Duration - heartbeatTimeout time.Duration - serverMonitor *event.ServerMonitor - registry *bsoncodec.Registry - monitoringDisabled bool - serverAPI *driver.ServerAPIOptions - loadBalanced bool + clock *session.ClusterClock + compressionOpts []string + connectionOpts []ConnectionOption + appname string + heartbeatInterval time.Duration + heartbeatTimeout time.Duration + serverMonitoringMode string + serverMonitor *event.ServerMonitor + registry *bsoncodec.Registry + monitoringDisabled bool + serverAPI *driver.ServerAPIOptions + loadBalanced bool // Connection pool options. maxConns uint64 @@ -202,3 +204,17 @@ func withLogger(fn func() *logger.Logger) ServerOption { cfg.logger = fn() } } + +// withServerMonitoringMode configures the mode (stream, poll, or auto) to use +// for monitoring. +func withServerMonitoringMode(mode *string) ServerOption { + return func(cfg *serverConfig) { + if mode != nil { + cfg.serverMonitoringMode = *mode + + return + } + + cfg.serverMonitoringMode = connstring.ServerMonitoringModeAuto + } +} diff --git a/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/topology/topology.go b/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/topology/topology.go index d5a27cbb3..bbffbd1da 100644 --- a/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/topology/topology.go +++ b/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/topology/topology.go @@ -16,6 +16,7 @@ import ( "fmt" "net" "net/url" + "strconv" "strings" "sync" "sync/atomic" @@ -23,6 +24,7 @@ import ( "go.mongodb.org/mongo-driver/bson/primitive" "go.mongodb.org/mongo-driver/event" + "go.mongodb.org/mongo-driver/internal/logger" "go.mongodb.org/mongo-driver/internal/randutil" "go.mongodb.org/mongo-driver/mongo/address" "go.mongodb.org/mongo-driver/mongo/description" @@ -96,7 +98,7 @@ type Topology struct { subscriptionsClosed bool subLock sync.Mutex - // We should redesign how we Connect and handle individal servers. This is + // We should redesign how we Connect and handle individual servers. This is // too difficult to maintain and it's rather easy to accidentally access // the servers without acquiring the lock or checking if the servers are // closed. This lock should also be an RWMutex. @@ -107,8 +109,10 @@ type Topology struct { id primitive.ObjectID } -var _ driver.Deployment = &Topology{} -var _ driver.Subscriber = &Topology{} +var ( + _ driver.Deployment = &Topology{} + _ driver.Subscriber = &Topology{} +) type serverSelectionState struct { selector description.ServerSelector @@ -157,6 +161,114 @@ func New(cfg *Config) (*Topology, error) { return t, nil } +func mustLogTopologyMessage(topo *Topology, level logger.Level) bool { + return topo.cfg.logger != nil && topo.cfg.logger.LevelComponentEnabled( + level, logger.ComponentTopology) +} + +func logTopologyMessage(topo *Topology, level logger.Level, msg string, keysAndValues ...interface{}) { + topo.cfg.logger.Print(level, + logger.ComponentTopology, + msg, + logger.SerializeTopology(logger.Topology{ + ID: topo.id, + Message: msg, + }, keysAndValues...)...) +} + +func logTopologyThirdPartyUsage(topo *Topology, parsedHosts []string) { + thirdPartyMessages := [2]string{ + `You appear to be connected to a CosmosDB cluster. For more information regarding feature compatibility and support please visit https://www.mongodb.com/supportability/cosmosdb`, + `You appear to be connected to a DocumentDB cluster. For more information regarding feature compatibility and support please visit https://www.mongodb.com/supportability/documentdb`, + } + + thirdPartySuffixes := map[string]int{ + ".cosmos.azure.com": 0, + ".docdb.amazonaws.com": 1, + ".docdb-elastic.amazonaws.com": 1, + } + + hostSet := make([]bool, len(thirdPartyMessages)) + for _, host := range parsedHosts { + if h, _, err := net.SplitHostPort(host); err == nil { + host = h + } + for suffix, env := range thirdPartySuffixes { + if !strings.HasSuffix(host, suffix) { + continue + } + if hostSet[env] { + break + } + hostSet[env] = true + logTopologyMessage(topo, logger.LevelInfo, thirdPartyMessages[env]) + } + } +} + +func mustLogServerSelection(topo *Topology, level logger.Level) bool { + return topo.cfg.logger != nil && topo.cfg.logger.LevelComponentEnabled( + level, logger.ComponentServerSelection) +} + +func logServerSelection( + ctx context.Context, + topo *Topology, + level logger.Level, + msg string, + srvSelector description.ServerSelector, + keysAndValues ...interface{}, +) { + var srvSelectorString string + + selectorStringer, ok := srvSelector.(fmt.Stringer) + if ok { + srvSelectorString = selectorStringer.String() + } + + operationName, _ := logger.OperationName(ctx) + operationID, _ := logger.OperationID(ctx) + + topo.cfg.logger.Print(level, + logger.ComponentServerSelection, + msg, + logger.SerializeServerSelection(logger.ServerSelection{ + Selector: srvSelectorString, + Operation: operationName, + OperationID: &operationID, + TopologyDescription: topo.String(), + }, keysAndValues...)...) +} + +func logServerSelectionSucceeded( + ctx context.Context, + topo *Topology, + srvSelector description.ServerSelector, + server *SelectedServer, +) { + host, port, err := net.SplitHostPort(server.address.String()) + if err != nil { + host = server.address.String() + port = "" + } + + portInt64, _ := strconv.ParseInt(port, 10, 32) + + logServerSelection(ctx, topo, logger.LevelDebug, logger.ServerSelectionSucceeded, srvSelector, + logger.KeyServerHost, host, + logger.KeyServerPort, portInt64) +} + +func logServerSelectionFailed( + ctx context.Context, + topo *Topology, + srvSelector description.ServerSelector, + err error, +) { + logServerSelection(ctx, topo, logger.LevelDebug, logger.ServerSelectionFailed, srvSelector, + logger.KeyFailure, err.Error()) +} + // Connect initializes a Topology and starts the monitoring process. This function // must be called to properly monitor the topology. func (t *Topology) Connect() error { @@ -218,8 +330,12 @@ func (t *Topology) Connect() error { // server monitoring goroutines. newDesc := description.Topology{ - Kind: t.fsm.Kind, - Servers: t.fsm.Servers, + Kind: t.fsm.Kind, + Servers: t.fsm.Servers, + SessionTimeoutMinutesPtr: t.fsm.SessionTimeoutMinutesPtr, + + // TODO(GODRIVER-2885): This field can be removed once + // legacy SessionTimeoutMinutes is removed. SessionTimeoutMinutes: t.fsm.SessionTimeoutMinutes, } t.desc.Store(newDesc) @@ -235,13 +351,17 @@ func (t *Topology) Connect() error { } t.serversLock.Unlock() + uri, err := url.Parse(t.cfg.URI) + if err != nil { + return err + } + parsedHosts := strings.Split(uri.Host, ",") + if mustLogTopologyMessage(t, logger.LevelInfo) { + logTopologyThirdPartyUsage(t, parsedHosts) + } if t.pollingRequired { - uri, err := url.Parse(t.cfg.URI) - if err != nil { - return err - } // sanity check before passing the hostname to resolver - if parsedHosts := strings.Split(uri.Host, ","); len(parsedHosts) != 1 { + if len(parsedHosts) != 1 { return fmt.Errorf("URI with SRV must include one and only one hostname") } _, _, err = net.SplitHostPort(uri.Host) @@ -380,6 +500,10 @@ func (t *Topology) RequestImmediateCheck() { // parent context is done. func (t *Topology) SelectServer(ctx context.Context, ss description.ServerSelector) (driver.Server, error) { if atomic.LoadInt64(&t.state) != topologyConnected { + if mustLogServerSelection(t, logger.LevelDebug) { + logServerSelectionFailed(ctx, t, ss, ErrTopologyClosed) + } + return nil, ErrTopologyClosed } var ssTimeoutCh <-chan time.Time @@ -393,11 +517,18 @@ func (t *Topology) SelectServer(ctx context.Context, ss description.ServerSelect var doneOnce bool var sub *driver.Subscription selectionState := newServerSelectionState(ss, ssTimeoutCh) + + // Record the start time. + startTime := time.Now() for { var suitable []description.Server var selectErr error if !doneOnce { + if mustLogServerSelection(t, logger.LevelDebug) { + logServerSelection(ctx, t, logger.LevelDebug, logger.ServerSelectionStarted, ss) + } + // for the first pass, select a server from the current description. // this improves selection speed for up-to-date topology descriptions. suitable, selectErr = t.selectServerFromDescription(t.Description(), selectionState) @@ -409,6 +540,10 @@ func (t *Topology) SelectServer(ctx context.Context, ss description.ServerSelect var err error sub, err = t.Subscribe() if err != nil { + if mustLogServerSelection(t, logger.LevelDebug) { + logServerSelectionFailed(ctx, t, ss, err) + } + return nil, err } defer t.Unsubscribe(sub) @@ -417,11 +552,23 @@ func (t *Topology) SelectServer(ctx context.Context, ss description.ServerSelect suitable, selectErr = t.selectServerFromSubscription(ctx, sub.Updates, selectionState) } if selectErr != nil { + if mustLogServerSelection(t, logger.LevelDebug) { + logServerSelectionFailed(ctx, t, ss, selectErr) + } + return nil, selectErr } if len(suitable) == 0 { // try again if there are no servers available + if mustLogServerSelection(t, logger.LevelInfo) { + elapsed := time.Since(startTime) + remainingTimeMS := t.cfg.ServerSelectionTimeout - elapsed + + logServerSelection(ctx, t, logger.LevelInfo, logger.ServerSelectionWaiting, ss, + logger.KeyRemainingTimeMS, remainingTimeMS.Milliseconds()) + } + continue } @@ -430,11 +577,20 @@ func (t *Topology) SelectServer(ctx context.Context, ss description.ServerSelect if len(suitable) == 1 { server, err := t.FindServer(suitable[0]) if err != nil { + if mustLogServerSelection(t, logger.LevelDebug) { + logServerSelectionFailed(ctx, t, ss, err) + } + return nil, err } if server == nil { continue } + + if mustLogServerSelection(t, logger.LevelDebug) { + logServerSelectionSucceeded(ctx, t, ss, server) + } + return server, nil } @@ -443,10 +599,18 @@ func (t *Topology) SelectServer(ctx context.Context, ss description.ServerSelect desc1, desc2 := pick2(suitable) server1, err := t.FindServer(desc1) if err != nil { + if mustLogServerSelection(t, logger.LevelDebug) { + logServerSelectionFailed(ctx, t, ss, err) + } + return nil, err } server2, err := t.FindServer(desc2) if err != nil { + if mustLogServerSelection(t, logger.LevelDebug) { + logServerSelectionFailed(ctx, t, ss, err) + } + return nil, err } @@ -458,9 +622,18 @@ func (t *Topology) SelectServer(ctx context.Context, ss description.ServerSelect if server1 == nil && server2 == nil { continue } + if server1 != nil { + if mustLogServerSelection(t, logger.LevelDebug) { + logServerSelectionSucceeded(ctx, t, ss, server1) + } return server1, nil } + + if mustLogServerSelection(t, logger.LevelDebug) { + logServerSelectionSucceeded(ctx, t, ss, server2) + } + return server2, nil } @@ -468,8 +641,16 @@ func (t *Topology) SelectServer(ctx context.Context, ss description.ServerSelect // We use in-use connections as an analog for in-progress operations because they are almost // always the same value for a given server. if server1.OperationCount() < server2.OperationCount() { + if mustLogServerSelection(t, logger.LevelDebug) { + logServerSelectionSucceeded(ctx, t, ss, server1) + } + return server1, nil } + + if mustLogServerSelection(t, logger.LevelDebug) { + logServerSelectionSucceeded(ctx, t, ss, server2) + } return server2, nil } } @@ -679,10 +860,14 @@ func (t *Topology) processSRVResults(parsedHosts []string) bool { t.fsm.addServer(addr) } - //store new description + // store new description newDesc := description.Topology{ - Kind: t.fsm.Kind, - Servers: t.fsm.Servers, + Kind: t.fsm.Kind, + Servers: t.fsm.Servers, + SessionTimeoutMinutesPtr: t.fsm.SessionTimeoutMinutesPtr, + + // TODO(GODRIVER-2885): This field can be removed once legacy + // SessionTimeoutMinutes is removed. SessionTimeoutMinutes: t.fsm.SessionTimeoutMinutes, } t.desc.Store(newDesc) @@ -818,6 +1003,20 @@ func (t *Topology) publishServerClosedEvent(addr address.Address) { if t.cfg.ServerMonitor != nil && t.cfg.ServerMonitor.ServerClosed != nil { t.cfg.ServerMonitor.ServerClosed(serverClosed) } + + if mustLogTopologyMessage(t, logger.LevelDebug) { + serverHost, serverPort, err := net.SplitHostPort(addr.String()) + if err != nil { + serverHost = addr.String() + serverPort = "" + } + + portInt64, _ := strconv.ParseInt(serverPort, 10, 32) + + logTopologyMessage(t, logger.LevelDebug, logger.TopologyServerClosed, + logger.KeyServerHost, serverHost, + logger.KeyServerPort, portInt64) + } } // publishes a TopologyDescriptionChangedEvent to indicate the topology description has changed @@ -831,6 +1030,12 @@ func (t *Topology) publishTopologyDescriptionChangedEvent(prev description.Topol if t.cfg.ServerMonitor != nil && t.cfg.ServerMonitor.TopologyDescriptionChanged != nil { t.cfg.ServerMonitor.TopologyDescriptionChanged(topologyDescriptionChanged) } + + if mustLogTopologyMessage(t, logger.LevelDebug) { + logTopologyMessage(t, logger.LevelDebug, logger.TopologyDescriptionChanged, + logger.KeyPreviousDescription, prev.String(), + logger.KeyNewDescription, current.String()) + } } // publishes a TopologyOpeningEvent to indicate the topology is being initialized @@ -842,6 +1047,10 @@ func (t *Topology) publishTopologyOpeningEvent() { if t.cfg.ServerMonitor != nil && t.cfg.ServerMonitor.TopologyOpening != nil { t.cfg.ServerMonitor.TopologyOpening(topologyOpening) } + + if mustLogTopologyMessage(t, logger.LevelDebug) { + logTopologyMessage(t, logger.LevelDebug, logger.TopologyOpening) + } } // publishes a TopologyClosedEvent to indicate the topology has been closed @@ -853,4 +1062,8 @@ func (t *Topology) publishTopologyClosedEvent() { if t.cfg.ServerMonitor != nil && t.cfg.ServerMonitor.TopologyClosed != nil { t.cfg.ServerMonitor.TopologyClosed(topologyClosed) } + + if mustLogTopologyMessage(t, logger.LevelDebug) { + logTopologyMessage(t, logger.LevelDebug, logger.TopologyClosed) + } } diff --git a/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/topology/topology_options.go b/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/topology/topology_options.go index 67e6cbf9f..b5eb4a972 100644 --- a/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/topology/topology_options.go +++ b/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/topology/topology_options.go @@ -38,6 +38,7 @@ type Config struct { SRVMaxHosts int SRVServiceName string LoadBalanced bool + logger *logger.Logger } // ConvertToDriverAPIOptions converts a options.ServerAPIOptions instance to a driver.ServerAPIOptions. @@ -52,8 +53,26 @@ func ConvertToDriverAPIOptions(s *options.ServerAPIOptions) *driver.ServerAPIOpt return driverOpts } +func newLogger(opts *options.LoggerOptions) (*logger.Logger, error) { + if opts == nil { + opts = options.Logger() + } + + componentLevels := make(map[logger.Component]logger.Level) + for component, level := range opts.ComponentLevels { + componentLevels[logger.Component(component)] = logger.Level(level) + } + + log, err := logger.New(opts.Sink, opts.MaxDocumentLength, componentLevels) + if err != nil { + return nil, fmt.Errorf("error creating logger: %w", err) + } + + return log, nil +} + // NewConfig will translate data from client options into a topology config for building non-default deployments. -// Server and topoplogy options are not honored if a custom deployment is used. +// Server and topology options are not honored if a custom deployment is used. func NewConfig(co *options.ClientOptions, clock *session.ClusterClock) (*Config, error) { var serverAPI *driver.ServerAPIOptions @@ -335,23 +354,18 @@ func NewConfig(co *options.ClientOptions, clock *session.ClusterClock) (*Config, ) } - if opts := co.LoggerOptions; opts != nil { - // Build an internal component-level mapping. - componentLevels := make(map[logger.Component]logger.Level) - for component, level := range opts.ComponentLevels { - componentLevels[logger.Component(component)] = logger.Level(level) - } + lgr, err := newLogger(co.LoggerOptions) + if err != nil { + return nil, err + } - log, err := logger.New(opts.Sink, opts.MaxDocumentLength, componentLevels) - if err != nil { - return nil, fmt.Errorf("error creating logger: %w", err) - } + serverOpts = append( + serverOpts, + withLogger(func() *logger.Logger { return lgr }), + withServerMonitoringMode(co.ServerMonitoringMode), + ) - serverOpts = append( - serverOpts, - withLogger(func() *logger.Logger { return log }), - ) - } + cfgp.logger = lgr serverOpts = append( serverOpts, diff --git a/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/wiremessage/wiremessage.go b/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/wiremessage/wiremessage.go index e3aa09673..abf09c15b 100644 --- a/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/wiremessage/wiremessage.go +++ b/vendor/go.mongodb.org/mongo-driver/x/mongo/driver/wiremessage/wiremessage.go @@ -19,9 +19,6 @@ type WireMessage []byte var globalRequestID int32 -// CurrentRequestID returns the current request ID. -func CurrentRequestID() int32 { return atomic.LoadInt32(&globalRequestID) } - // NextRequestID returns the next request ID. func NextRequestID() int32 { return atomic.AddInt32(&globalRequestID, 1) } @@ -32,11 +29,12 @@ type OpCode int32 // supported by this library. The skipped OpCodes are historical OpCodes that // are no longer used. const ( - OpReply OpCode = 1 - _ OpCode = 1001 - OpUpdate OpCode = 2001 - OpInsert OpCode = 2002 - _ OpCode = 2003 + OpReply OpCode = 1 + _ OpCode = 1001 + OpUpdate OpCode = 2001 + OpInsert OpCode = 2002 + _ OpCode = 2003 + // Deprecated: Use OpMsg instead. OpQuery OpCode = 2004 OpGetMore OpCode = 2005 OpDelete OpCode = 2006 @@ -174,9 +172,6 @@ const ( DocumentSequence ) -// OpmsgWireVersion is the minimum wire version needed to use OP_MSG -const OpmsgWireVersion = 6 - // CompressorID is the ID for each type of Compressor. type CompressorID uint8 @@ -432,32 +427,50 @@ func ReadMsgChecksum(src []byte) (checksum uint32, rem []byte, ok bool) { } // ReadQueryFlags reads OP_QUERY flags from src. +// +// Deprecated: Construct wiremessages with OpMsg and use the ReadMsg* functions +// instead. func ReadQueryFlags(src []byte) (flags QueryFlag, rem []byte, ok bool) { i32, rem, ok := readi32(src) return QueryFlag(i32), rem, ok } // ReadQueryFullCollectionName reads the full collection name from src. +// +// Deprecated: Construct wiremessages with OpMsg and use the ReadMsg* functions +// instead. func ReadQueryFullCollectionName(src []byte) (collname string, rem []byte, ok bool) { return readcstring(src) } // ReadQueryNumberToSkip reads the number to skip from src. +// +// Deprecated: Construct wiremessages with OpMsg and use the ReadMsg* functions +// instead. func ReadQueryNumberToSkip(src []byte) (nts int32, rem []byte, ok bool) { return readi32(src) } // ReadQueryNumberToReturn reads the number to return from src. +// +// Deprecated: Construct wiremessages with OpMsg and use the ReadMsg* functions +// instead. func ReadQueryNumberToReturn(src []byte) (ntr int32, rem []byte, ok bool) { return readi32(src) } // ReadQueryQuery reads the query from src. +// +// Deprecated: Construct wiremessages with OpMsg and use the ReadMsg* functions +// instead. func ReadQueryQuery(src []byte) (query bsoncore.Document, rem []byte, ok bool) { return bsoncore.ReadDocument(src) } // ReadQueryReturnFieldsSelector reads a return fields selector document from src. +// +// Deprecated: Construct wiremessages with OpMsg and use the ReadMsg* functions +// instead. func ReadQueryReturnFieldsSelector(src []byte) (rfs bsoncore.Document, rem []byte, ok bool) { return bsoncore.ReadDocument(src) } diff --git a/vendor/modules.txt b/vendor/modules.txt index 7e99ed360..cba68345a 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -359,7 +359,7 @@ github.com/xrash/smetrics # github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d ## explicit github.com/youmark/pkcs8 -# go.mongodb.org/mongo-driver v1.12.1 +# go.mongodb.org/mongo-driver v1.13.0 ## explicit; go 1.13 go.mongodb.org/mongo-driver/bson go.mongodb.org/mongo-driver/bson/bsoncodec @@ -368,15 +368,22 @@ go.mongodb.org/mongo-driver/bson/bsonrw go.mongodb.org/mongo-driver/bson/bsontype go.mongodb.org/mongo-driver/bson/primitive go.mongodb.org/mongo-driver/event -go.mongodb.org/mongo-driver/internal go.mongodb.org/mongo-driver/internal/aws go.mongodb.org/mongo-driver/internal/aws/awserr go.mongodb.org/mongo-driver/internal/aws/credentials go.mongodb.org/mongo-driver/internal/aws/signer/v4 +go.mongodb.org/mongo-driver/internal/bsonutil +go.mongodb.org/mongo-driver/internal/codecutil go.mongodb.org/mongo-driver/internal/credproviders +go.mongodb.org/mongo-driver/internal/csfle +go.mongodb.org/mongo-driver/internal/csot +go.mongodb.org/mongo-driver/internal/driverutil +go.mongodb.org/mongo-driver/internal/handshake +go.mongodb.org/mongo-driver/internal/httputil go.mongodb.org/mongo-driver/internal/logger +go.mongodb.org/mongo-driver/internal/ptrutil +go.mongodb.org/mongo-driver/internal/rand go.mongodb.org/mongo-driver/internal/randutil -go.mongodb.org/mongo-driver/internal/randutil/rand go.mongodb.org/mongo-driver/internal/uuid go.mongodb.org/mongo-driver/mongo go.mongodb.org/mongo-driver/mongo/address