Skip to content

Commit

Permalink
log before SRV lookup
Browse files Browse the repository at this point in the history
  • Loading branch information
qingyang-hu committed Sep 7, 2023
1 parent 60d159d commit ff92957
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 77 deletions.
117 changes: 49 additions & 68 deletions x/mongo/driver/topology/topology.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,57 +74,26 @@ type hostEnv int
const (
genuine hostEnv = iota
cosmosDB
doumentDB
documentDB
)

var (
cosmosDBLog = `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`
documentDBLog = `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`
const (
cosmosDBMsg = `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`
documentDBMsg = `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`
)

type envMap struct {
host string
env hostEnv
}

type hostLogger struct {
logger *logger.Logger
envs []envMap
logs map[hostEnv]*string
}

func newHostLogger(l *logger.Logger) *hostLogger {
return &hostLogger{
logger: l,
envs: []envMap{
{".cosmos.azure.com", cosmosDB},
{".docdb.amazonaws.com", doumentDB},
{".docdb-elastic.amazonaws.com", doumentDB},
},
logs: map[hostEnv]*string{
cosmosDB: &cosmosDBLog,
doumentDB: &documentDBLog,
},
var (
thirdPartySuffixes = map[string]hostEnv{
".cosmos.azure.com": cosmosDB,
".docdb.amazonaws.com": documentDB,
".docdb-elastic.amazonaws.com": documentDB,
}
}

func (l *hostLogger) log(host string) {
if h, _, err := net.SplitHostPort(host); err == nil {
host = h
}
for _, em := range l.envs {
if strings.HasSuffix(host, em.host) {
if log, ok := l.logs[em.env]; ok && log != nil {
l.logger.Print(logger.LevelInfo,
logger.ComponentTopology,
*log,
)
l.logs[em.env] = nil
}
break
}
thirdPartyMessages = map[hostEnv]string{
cosmosDB: cosmosDBMsg,
documentDB: documentDBMsg,
}
}
)

// Topology represents a MongoDB deployment.
type Topology struct {
Expand Down Expand Up @@ -218,13 +187,13 @@ func New(cfg *Config) (*Topology, error) {
return t, nil
}

func mustLogTopologyMessage(topo *Topology) bool {
func mustLogTopologyMessage(topo *Topology, level logger.Level) bool {
return topo.cfg.logger != nil && topo.cfg.logger.LevelComponentEnabled(
logger.LevelDebug, logger.ComponentTopology)
level, logger.ComponentTopology)
}

func logTopologyMessage(topo *Topology, msg string, keysAndValues ...interface{}) {
topo.cfg.logger.Print(logger.LevelDebug,
func logTopologyMessage(topo *Topology, level logger.Level, msg string, keysAndValues ...interface{}) {
topo.cfg.logger.Print(level,
logger.ComponentTopology,
msg,
logger.SerializeTopology(logger.Topology{
Expand All @@ -240,8 +209,8 @@ func mustLogServerSelection(topo *Topology, level logger.Level) bool {

func logServerSelection(
ctx context.Context,
level logger.Level,
topo *Topology,
level logger.Level,
msg string,
srvSelector description.ServerSelector,
keysAndValues ...interface{},
Expand Down Expand Up @@ -281,7 +250,7 @@ func logServerSelectionSucceeded(

portInt64, _ := strconv.ParseInt(port, 10, 32)

logServerSelection(ctx, logger.LevelDebug, topo, logger.ServerSelectionSucceeded, srvSelector,
logServerSelection(ctx, topo, logger.LevelDebug, logger.ServerSelectionSucceeded, srvSelector,
logger.KeyServerHost, host,
logger.KeyServerPort, portInt64)
}
Expand All @@ -292,7 +261,7 @@ func logServerSelectionFailed(
srvSelector description.ServerSelector,
err error,
) {
logServerSelection(ctx, logger.LevelDebug, topo, logger.ServerSelectionFailed, srvSelector,
logServerSelection(ctx, topo, logger.LevelDebug, logger.ServerSelectionFailed, srvSelector,
logger.KeyFailure, err.Error())
}

Expand Down Expand Up @@ -383,6 +352,25 @@ func (t *Topology) Connect() error {
return err
}
parsedHosts := strings.Split(uri.Host, ",")
if mustLogTopologyMessage(t, logger.LevelInfo) {
hostSet := make(map[hostEnv]bool)
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 logged, ok := hostSet[env]; ok && logged {
break
} else {
hostSet[env] = true
logTopologyMessage(t, logger.LevelInfo, thirdPartyMessages[env])
}
}
}
}
if t.pollingRequired {
// sanity check before passing the hostname to resolver
if len(parsedHosts) != 1 {
Expand All @@ -396,11 +384,6 @@ func (t *Topology) Connect() error {
}
go t.pollSRVRecords(uri.Host)
t.pollingwg.Add(1)
} else {
logger := newHostLogger(t.cfg.logger)
for _, host := range parsedHosts {
logger.log(host)
}
}

t.subscriptionsClosed = false // explicitly set in case topology was disconnected and then reconnected
Expand Down Expand Up @@ -555,7 +538,7 @@ func (t *Topology) SelectServer(ctx context.Context, ss description.ServerSelect

if !doneOnce {
if mustLogServerSelection(t, logger.LevelDebug) {
logServerSelection(ctx, logger.LevelDebug, t, logger.ServerSelectionStarted, ss)
logServerSelection(ctx, t, logger.LevelDebug, logger.ServerSelectionStarted, ss)
}

// for the first pass, select a server from the current description.
Expand Down Expand Up @@ -594,7 +577,7 @@ func (t *Topology) SelectServer(ctx context.Context, ss description.ServerSelect
elapsed := time.Since(startTime)
remainingTimeMS := t.cfg.ServerSelectionTimeout - elapsed

logServerSelection(ctx, logger.LevelInfo, t, logger.ServerSelectionWaiting, ss,
logServerSelection(ctx, t, logger.LevelInfo, logger.ServerSelectionWaiting, ss,
logger.KeyRemainingTimeMS, remainingTimeMS.Milliseconds())
}

Expand Down Expand Up @@ -880,12 +863,10 @@ func (t *Topology) processSRVResults(parsedHosts []string) bool {
})
}
// Add all added hosts until the number of servers reaches srvMaxHosts.
logger := newHostLogger(t.cfg.logger)
for _, a := range diff.Added {
if t.cfg.SRVMaxHosts > 0 && len(t.servers) >= t.cfg.SRVMaxHosts {
break
}
logger.log(a)
addr := address.Address(a).Canonicalize()
_ = t.addServer(addr)
t.fsm.addServer(addr)
Expand Down Expand Up @@ -1035,7 +1016,7 @@ func (t *Topology) publishServerClosedEvent(addr address.Address) {
t.cfg.ServerMonitor.ServerClosed(serverClosed)
}

if mustLogTopologyMessage(t) {
if mustLogTopologyMessage(t, logger.LevelDebug) {
serverHost, serverPort, err := net.SplitHostPort(addr.String())
if err != nil {
serverHost = addr.String()
Expand All @@ -1044,7 +1025,7 @@ func (t *Topology) publishServerClosedEvent(addr address.Address) {

portInt64, _ := strconv.ParseInt(serverPort, 10, 32)

logTopologyMessage(t, logger.TopologyServerClosed,
logTopologyMessage(t, logger.LevelDebug, logger.TopologyServerClosed,
logger.KeyServerHost, serverHost,
logger.KeyServerPort, portInt64)
}
Expand All @@ -1062,8 +1043,8 @@ func (t *Topology) publishTopologyDescriptionChangedEvent(prev description.Topol
t.cfg.ServerMonitor.TopologyDescriptionChanged(topologyDescriptionChanged)
}

if mustLogTopologyMessage(t) {
logTopologyMessage(t, logger.TopologyDescriptionChanged,
if mustLogTopologyMessage(t, logger.LevelDebug) {
logTopologyMessage(t, logger.LevelDebug, logger.TopologyDescriptionChanged,
logger.KeyPreviousDescription, prev.String(),
logger.KeyNewDescription, current.String())
}
Expand All @@ -1079,8 +1060,8 @@ func (t *Topology) publishTopologyOpeningEvent() {
t.cfg.ServerMonitor.TopologyOpening(topologyOpening)
}

if mustLogTopologyMessage(t) {
logTopologyMessage(t, logger.TopologyOpening)
if mustLogTopologyMessage(t, logger.LevelDebug) {
logTopologyMessage(t, logger.LevelDebug, logger.TopologyOpening)
}
}

Expand All @@ -1094,7 +1075,7 @@ func (t *Topology) publishTopologyClosedEvent() {
t.cfg.ServerMonitor.TopologyClosed(topologyClosed)
}

if mustLogTopologyMessage(t) {
logTopologyMessage(t, logger.TopologyClosed)
if mustLogTopologyMessage(t, logger.LevelDebug) {
logTopologyMessage(t, logger.LevelDebug, logger.TopologyClosed)
}
}
23 changes: 14 additions & 9 deletions x/mongo/driver/topology/topology_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -721,12 +721,12 @@ func TestTopologyConstructionLogging(t *testing.T) {
{
name: "normal",
uri: "mongodb://a.mongo.cosmos.azure.com:19555/",
msgs: []string{cosmosDBLog},
msgs: []string{cosmosDBMsg},
},
{
name: "multiple hosts",
uri: "mongodb://a.mongo.cosmos.azure.com:1955,b.mongo.cosmos.azure.com:19555/",
msgs: []string{cosmosDBLog},
msgs: []string{cosmosDBMsg},
},
{
name: "case-insensitive matching",
Expand All @@ -736,7 +736,7 @@ func TestTopologyConstructionLogging(t *testing.T) {
{
name: "Mixing genuine and nongenuine hosts (unlikely in practice)",
uri: "mongodb://a.example.com:27017,b.mongo.cosmos.azure.com:19555/",
msgs: []string{cosmosDBLog},
msgs: []string{cosmosDBMsg},
},
}
for _, tc := range testCases {
Expand Down Expand Up @@ -766,17 +766,17 @@ func TestTopologyConstructionLogging(t *testing.T) {
{
name: "normal",
uri: "mongodb://a.docdb.amazonaws.com:27017/",
msgs: []string{documentDBLog},
msgs: []string{documentDBMsg},
},
{
name: "normal",
uri: "mongodb://a.docdb-elastic.amazonaws.com:27017/",
msgs: []string{documentDBLog},
msgs: []string{documentDBMsg},
},
{
name: "multiple hosts",
uri: "mongodb://a.docdb.amazonaws.com:27017,a.docdb-elastic.amazonaws.com:27017/",
msgs: []string{documentDBLog},
msgs: []string{documentDBMsg},
},
{
name: "case-insensitive matching",
Expand All @@ -791,12 +791,12 @@ func TestTopologyConstructionLogging(t *testing.T) {
{
name: "Mixing genuine and nongenuine hosts (unlikely in practice)",
uri: "mongodb://a.example.com:27017,b.docdb.amazonaws.com:27017/",
msgs: []string{documentDBLog},
msgs: []string{documentDBMsg},
},
{
name: "Mixing genuine and nongenuine hosts (unlikely in practice)",
uri: "mongodb://a.example.com:27017,b.docdb-elastic.amazonaws.com:27017/",
msgs: []string{documentDBLog},
msgs: []string{documentDBMsg},
},
}
for _, tc := range testCases {
Expand Down Expand Up @@ -826,7 +826,7 @@ func TestTopologyConstructionLogging(t *testing.T) {
{
name: "Mixing hosts",
uri: "mongodb://a.mongo.cosmos.azure.com:19555,a.docdb.amazonaws.com:27017/",
msgs: []string{cosmosDBLog, documentDBLog},
msgs: []string{cosmosDBMsg, documentDBMsg},
},
}
for _, tc := range testCases {
Expand Down Expand Up @@ -858,6 +858,11 @@ func TestTopologyConstructionLogging(t *testing.T) {
uri: "mongodb://a.example.com:27017/",
msgs: []string{},
},
{
name: "srv",
uri: "mongodb+srv://test22.test.build.10gen.cc/?srvServiceName=customname",
msgs: []string{},
},
{
name: "multiple hosts",
uri: "mongodb://a.example.com:27017,b.example.com:27017/",
Expand Down

0 comments on commit ff92957

Please sign in to comment.