1.修改代码适配阿里云的服务器

This commit is contained in:
whm
2026-03-17 14:27:32 +08:00
parent 826617d737
commit 20e7f3a65d
1777 changed files with 775041 additions and 10 deletions

View File

@@ -0,0 +1,493 @@
// Copyright (C) MongoDB, Inc. 2024-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 (
"errors"
"fmt"
"time"
"go.mongodb.org/mongo-driver/v2/bson"
"go.mongodb.org/mongo-driver/v2/internal/bsonutil"
"go.mongodb.org/mongo-driver/v2/internal/handshake"
"go.mongodb.org/mongo-driver/v2/internal/ptrutil"
"go.mongodb.org/mongo-driver/v2/mongo/address"
"go.mongodb.org/mongo-driver/v2/tag"
"go.mongodb.org/mongo-driver/v2/x/mongo/driver/description"
)
const (
MinWireVersion = 8
MaxWireVersion = 25
)
func equalWireVersion(wv1, wv2 *description.VersionRange) bool {
if wv1 == nil && wv2 == nil {
return true
}
if wv1 == nil || wv2 == nil {
return false
}
return wv1.Min == wv2.Min && wv1.Max == wv2.Max
}
// EqualServers compares two server descriptions and returns true if they are
// equal.
func EqualServers(srv1, srv2 description.Server) bool {
if srv1.CanonicalAddr.String() != srv2.CanonicalAddr.String() {
return false
}
if !sliceStringEqual(srv1.Arbiters, srv2.Arbiters) {
return false
}
if !sliceStringEqual(srv1.Hosts, srv2.Hosts) {
return false
}
if !sliceStringEqual(srv1.Passives, srv2.Passives) {
return false
}
if srv1.Primary != srv2.Primary {
return false
}
if srv1.SetName != srv2.SetName {
return false
}
if srv1.Kind != srv2.Kind {
return false
}
if srv1.LastError != nil || srv2.LastError != nil {
if srv1.LastError == nil || srv2.LastError == nil {
return false
}
if srv1.LastError.Error() != srv2.LastError.Error() {
return false
}
}
if !equalWireVersion(srv1.WireVersion, srv2.WireVersion) {
return false
}
if len(srv1.Tags) != len(srv2.Tags) || !srv1.Tags.ContainsAll(srv2.Tags) {
return false
}
if srv1.SetVersion != srv2.SetVersion {
return false
}
if srv1.ElectionID != srv2.ElectionID {
return false
}
if ptrutil.CompareInt64(srv1.SessionTimeoutMinutes, srv2.SessionTimeoutMinutes) != 0 {
return false
}
// If TopologyVersion is nil for both servers, CompareToIncoming will return -1 because it assumes that the
// incoming response is newer. We want the descriptions to be considered equal in this case, though, so an
// explicit check is required.
if srv1.TopologyVersion == nil && srv2.TopologyVersion == nil {
return true
}
return CompareTopologyVersions(srv1.TopologyVersion, srv2.TopologyVersion) == 0
}
// IsServerLoadBalanced checks if a description.Server describes a server that
// is load balanced.
func IsServerLoadBalanced(srv description.Server) bool {
return srv.Kind == description.ServerKindLoadBalancer || srv.ServiceID != nil
}
// 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())
}
func decodeStringMap(element bson.RawElement, name string) (map[string]string, error) {
doc, ok := element.Value().DocumentOK()
if !ok {
return nil, fmt.Errorf("expected '%s' to be a document but it's a BSON %s", name, element.Value().Type)
}
elements, err := doc.Elements()
if err != nil {
return nil, err
}
m := make(map[string]string)
for _, element := range elements {
key := element.Key()
value, ok := element.Value().StringValueOK()
if !ok {
return nil, fmt.Errorf("expected '%s' to be a document of strings, but found a BSON %s", name, element.Value().Type)
}
m[key] = value
}
return m, nil
}
// NewTopologyVersion creates a TopologyVersion based on doc
func NewTopologyVersion(doc bson.Raw) (*description.TopologyVersion, error) {
elements, err := doc.Elements()
if err != nil {
return nil, err
}
var tv description.TopologyVersion
var ok bool
for _, element := range elements {
switch element.Key() {
case "processId":
tv.ProcessID, ok = element.Value().ObjectIDOK()
if !ok {
return nil, fmt.Errorf("expected 'processId' to be a objectID but it's a BSON %s", element.Value().Type)
}
case "counter":
tv.Counter, ok = element.Value().Int64OK()
if !ok {
return nil, fmt.Errorf("expected 'counter' to be an int64 but it's a BSON %s", element.Value().Type)
}
}
}
return &tv, nil
}
// NewVersionRange creates a new VersionRange given a min and a max.
func NewVersionRange(min, max int32) description.VersionRange {
return description.VersionRange{Min: min, Max: max}
}
// VersionRangeIncludes returns a bool indicating whether the supplied integer
// is included in the range.
func VersionRangeIncludes(versionRange description.VersionRange, v int32) bool {
return v >= versionRange.Min && v <= versionRange.Max
}
// CompareTopologyVersions compares the receiver, which represents the currently
// known TopologyVersion for a server, to an incoming TopologyVersion extracted
// from a server command response.
//
// This returns -1 if the receiver version is less than the response, 0 if the
// versions are equal, and 1 if the receiver version is greater than the
// response. This comparison is not commutative.
func CompareTopologyVersions(receiver, response *description.TopologyVersion) int {
if receiver == nil || response == nil {
return -1
}
if receiver.ProcessID != response.ProcessID {
return -1
}
if receiver.Counter == response.Counter {
return 0
}
if receiver.Counter < response.Counter {
return -1
}
return 1
}
// NewServerDescription creates a new server description from the given hello
// command response.
func NewServerDescription(addr address.Address, response bson.Raw) description.Server {
desc := description.Server{Addr: addr, CanonicalAddr: addr, LastUpdateTime: time.Now().UTC()}
elements, err := response.Elements()
if err != nil {
desc.LastError = err
return desc
}
var ok bool
var isReplicaSet, isWritablePrimary, hidden, secondary, arbiterOnly bool
var msg string
var versionRange description.VersionRange
for _, element := range elements {
switch element.Key() {
case "arbiters":
var err error
desc.Arbiters, err = stringSliceFromRawElement(element)
if err != nil {
desc.LastError = err
return desc
}
case "arbiterOnly":
arbiterOnly, ok = element.Value().BooleanOK()
if !ok {
desc.LastError = fmt.Errorf("expected 'arbiterOnly' to be a boolean but it's a BSON %s", element.Value().Type)
return desc
}
case "compression":
var err error
desc.Compression, err = stringSliceFromRawElement(element)
if err != nil {
desc.LastError = err
return desc
}
case "electionId":
desc.ElectionID, ok = element.Value().ObjectIDOK()
if !ok {
desc.LastError = fmt.Errorf("expected 'electionId' to be a objectID but it's a BSON %s", element.Value().Type)
return desc
}
case "iscryptd":
desc.IsCryptd, ok = element.Value().BooleanOK()
if !ok {
desc.LastError = fmt.Errorf("expected 'iscryptd' to be a boolean but it's a BSON %s", element.Value().Type)
return desc
}
case "helloOk":
desc.HelloOK, ok = element.Value().BooleanOK()
if !ok {
desc.LastError = fmt.Errorf("expected 'helloOk' to be a boolean but it's a BSON %s", element.Value().Type)
return desc
}
case "hidden":
hidden, ok = element.Value().BooleanOK()
if !ok {
desc.LastError = fmt.Errorf("expected 'hidden' to be a boolean but it's a BSON %s", element.Value().Type)
return desc
}
case "hosts":
var err error
desc.Hosts, err = stringSliceFromRawElement(element)
if err != nil {
desc.LastError = err
return desc
}
case "isWritablePrimary":
isWritablePrimary, ok = element.Value().BooleanOK()
if !ok {
desc.LastError = fmt.Errorf("expected 'isWritablePrimary' to be a boolean but it's a BSON %s", element.Value().Type)
return desc
}
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)
return desc
}
case "isreplicaset":
isReplicaSet, ok = element.Value().BooleanOK()
if !ok {
desc.LastError = fmt.Errorf("expected 'isreplicaset' to be a boolean but it's a BSON %s", element.Value().Type)
return desc
}
case "lastWrite":
lastWrite, ok := element.Value().DocumentOK()
if !ok {
desc.LastError = fmt.Errorf("expected 'lastWrite' to be a document but it's a BSON %s", element.Value().Type)
return desc
}
dateTime, err := lastWrite.LookupErr("lastWriteDate")
if err == nil {
dt, ok := dateTime.DateTimeOK()
if !ok {
desc.LastError = fmt.Errorf("expected 'lastWriteDate' to be a datetime but it's a BSON %s", dateTime.Type)
return desc
}
desc.LastWriteTime = time.Unix(dt/1000, dt%1000*1000000).UTC()
}
case "logicalSessionTimeoutMinutes":
i64, ok := element.Value().AsInt64OK()
if !ok {
desc.LastError = fmt.Errorf("expected 'logicalSessionTimeoutMinutes' to be an integer but it's a BSON %s", element.Value().Type)
return desc
}
desc.SessionTimeoutMinutes = &i64
case "maxBsonObjectSize":
i64, ok := element.Value().AsInt64OK()
if !ok {
desc.LastError = fmt.Errorf("expected 'maxBsonObjectSize' to be an integer but it's a BSON %s", element.Value().Type)
return desc
}
desc.MaxDocumentSize = uint32(i64)
case "maxMessageSizeBytes":
i64, ok := element.Value().AsInt64OK()
if !ok {
desc.LastError = fmt.Errorf("expected 'maxMessageSizeBytes' to be an integer but it's a BSON %s", element.Value().Type)
return desc
}
desc.MaxMessageSize = uint32(i64)
case "maxWriteBatchSize":
i64, ok := element.Value().AsInt64OK()
if !ok {
desc.LastError = fmt.Errorf("expected 'maxWriteBatchSize' to be an integer but it's a BSON %s", element.Value().Type)
return desc
}
desc.MaxBatchCount = uint32(i64)
case "me":
me, ok := element.Value().StringValueOK()
if !ok {
desc.LastError = fmt.Errorf("expected 'me' to be a string but it's a BSON %s", element.Value().Type)
return desc
}
desc.CanonicalAddr = address.Address(me).Canonicalize()
case "maxWireVersion":
verMax, ok := element.Value().AsInt64OK()
versionRange.Max = int32(verMax)
if !ok {
desc.LastError = fmt.Errorf("expected 'maxWireVersion' to be an integer but it's a BSON %s", element.Value().Type)
return desc
}
case "minWireVersion":
verMin, ok := element.Value().AsInt64OK()
versionRange.Min = int32(verMin)
if !ok {
desc.LastError = fmt.Errorf("expected 'minWireVersion' to be an integer but it's a BSON %s", element.Value().Type)
return desc
}
case "msg":
msg, ok = element.Value().StringValueOK()
if !ok {
desc.LastError = fmt.Errorf("expected 'msg' to be a string but it's a BSON %s", element.Value().Type)
return desc
}
case "ok":
okay, ok := element.Value().AsInt64OK()
if !ok {
desc.LastError = fmt.Errorf("expected 'ok' to be a boolean but it's a BSON %s", element.Value().Type)
return desc
}
if okay != 1 {
desc.LastError = errors.New("not ok")
return desc
}
case "passives":
var err error
desc.Passives, err = stringSliceFromRawElement(element)
if err != nil {
desc.LastError = err
return desc
}
case "passive":
desc.Passive, ok = element.Value().BooleanOK()
if !ok {
desc.LastError = fmt.Errorf("expected 'passive' to be a boolean but it's a BSON %s", element.Value().Type)
return desc
}
case "primary":
primary, ok := element.Value().StringValueOK()
if !ok {
desc.LastError = fmt.Errorf("expected 'primary' to be a string but it's a BSON %s", element.Value().Type)
return desc
}
desc.Primary = address.Address(primary)
case "readOnly":
desc.ReadOnly, ok = element.Value().BooleanOK()
if !ok {
desc.LastError = fmt.Errorf("expected 'readOnly' to be a boolean but it's a BSON %s", element.Value().Type)
return desc
}
case "secondary":
secondary, ok = element.Value().BooleanOK()
if !ok {
desc.LastError = fmt.Errorf("expected 'secondary' to be a boolean but it's a BSON %s", element.Value().Type)
return desc
}
case "serviceId":
oid, ok := element.Value().ObjectIDOK()
if !ok {
desc.LastError = fmt.Errorf("expected 'serviceId' to be an ObjectId but it's a BSON %s", element.Value().Type)
}
desc.ServiceID = &oid
case "setName":
desc.SetName, ok = element.Value().StringValueOK()
if !ok {
desc.LastError = fmt.Errorf("expected 'setName' to be a string but it's a BSON %s", element.Value().Type)
return desc
}
case "setVersion":
i64, ok := element.Value().AsInt64OK()
if !ok {
desc.LastError = fmt.Errorf("expected 'setVersion' to be an integer but it's a BSON %s", element.Value().Type)
return desc
}
desc.SetVersion = uint32(i64)
case "tags":
m, err := decodeStringMap(element, "tags")
if err != nil {
desc.LastError = err
return desc
}
desc.Tags = tag.NewTagSetFromMap(m)
case "topologyVersion":
doc, ok := element.Value().DocumentOK()
if !ok {
desc.LastError = fmt.Errorf("expected 'topologyVersion' to be a document but it's a BSON %s", element.Value().Type)
return desc
}
desc.TopologyVersion, err = NewTopologyVersion(doc)
if err != nil {
desc.LastError = err
return desc
}
}
}
for _, host := range desc.Hosts {
desc.Members = append(desc.Members, address.Address(host).Canonicalize())
}
for _, passive := range desc.Passives {
desc.Members = append(desc.Members, address.Address(passive).Canonicalize())
}
for _, arbiter := range desc.Arbiters {
desc.Members = append(desc.Members, address.Address(arbiter).Canonicalize())
}
desc.Kind = description.ServerKindStandalone
switch {
case isReplicaSet:
desc.Kind = description.ServerKindRSGhost
case desc.SetName != "":
switch {
case isWritablePrimary:
desc.Kind = description.ServerKindRSPrimary
case hidden:
desc.Kind = description.ServerKindRSMember
case secondary:
desc.Kind = description.ServerKindRSSecondary
case arbiterOnly:
desc.Kind = description.ServerKindRSArbiter
default:
desc.Kind = description.ServerKindRSMember
}
case msg == "isdbgrid":
desc.Kind = description.ServerKindMongos
}
desc.WireVersion = &versionRange
return desc
}
func sliceStringEqual(a []string, b []string) bool {
if len(a) != len(b) {
return false
}
for i, v := range a {
if v != b[i] {
return false
}
}
return true
}

View File

@@ -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 ""
}

View File

@@ -0,0 +1,69 @@
// 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 (
"context"
"math"
"time"
)
// 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
BulkWriteOp = "bulkWrite" // BulkWriteOp is the name for client-level bulk write
)
// CalculateMaxTimeMS calculates the maxTimeMS value to send to the server
// based on the context deadline and the minimum round trip time. If the
// calculated maxTimeMS is likely to cause a socket timeout, then this function
// will return 0 and false.
func CalculateMaxTimeMS(ctx context.Context, rttMin time.Duration) (int64, bool) {
deadline, ok := ctx.Deadline()
if !ok {
return 0, true
}
remainingTimeout := time.Until(deadline)
// Always round up to the next millisecond value so we never truncate the calculated
// maxTimeMS value (e.g. 400 microseconds evaluates to 1ms, not 0ms).
maxTimeMS := int64((remainingTimeout - rttMin + time.Millisecond - 1) / time.Millisecond)
if maxTimeMS <= 0 {
return 0, false
}
// The server will return a "BadValue" error if maxTimeMS is greater
// than the maximum positive int32 value (about 24.9 days). If the
// user specified a timeout value greater than that, omit maxTimeMS
// and let the client-side timeout handle cancelling the op if the
// timeout is ever reached.
if maxTimeMS > math.MaxInt32 {
return 0, true
}
return maxTimeMS, true
}