Skip to content

Commit

Permalink
GODRIVER-3255 [release/1.16] Await heartbeat checks upto freq when po…
Browse files Browse the repository at this point in the history
…lling (#1737)

Co-authored-by: Preston Vasquez <[email protected]>
  • Loading branch information
blink1073 and prestonvasquez authored Aug 7, 2024
1 parent 6ac5ab9 commit c8fb3ec
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
43 changes: 43 additions & 0 deletions mongo/integration/sdam_prose_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"net"
"os"
"runtime"
"sync"
"sync/atomic"
"testing"
"time"

Expand Down Expand Up @@ -232,4 +234,45 @@ func TestServerHeartbeatStartedEvent(t *testing.T) {
}
assert.Equal(t, expectedEvents, actualEvents)
})

mt := mtest.New(t)

mt.Run("polling must await frequency", func(mt *mtest.T) {
var heartbeatStartedCount atomic.Int64

servers := map[string]bool{}
serversMu := sync.RWMutex{} // Guard the servers set

serverMonitor := &event.ServerMonitor{
ServerHeartbeatStarted: func(*event.ServerHeartbeatStartedEvent) {
heartbeatStartedCount.Add(1)
},
TopologyDescriptionChanged: func(evt *event.TopologyDescriptionChangedEvent) {
serversMu.Lock()
defer serversMu.Unlock()

for _, srv := range evt.NewDescription.Servers {
servers[srv.Addr.String()] = true
}
},
}

// Create a client with heartbeatFrequency=100ms,
// serverMonitoringMode=poll. Use SDAM to record the number of times the
// a heartbeat is started and the number of servers discovered.
mt.ResetClient(options.Client().
SetServerMonitor(serverMonitor).
SetServerMonitoringMode(options.ServerMonitoringModePoll))

// Per specifications, minHeartbeatFrequencyMS=500ms. So, within the first
// 500ms the heartbeatStartedCount should be LEQ to the number of discovered
// servers.
time.Sleep(500 * time.Millisecond)

serversMu.Lock()
serverCount := int64(len(servers))
serversMu.Unlock()

assert.LessOrEqual(mt, heartbeatStartedCount.Load(), serverCount)
})
}
2 changes: 1 addition & 1 deletion x/mongo/driver/topology/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -666,7 +666,7 @@ func (s *Server) update() {
s.monitorOnce.Do(s.rttMonitor.connect)
}

if isStreamable(s) || connectionIsStreaming || transitionedFromNetworkError {
if isStreamingEnabled(s) && (isStreamable(s) || connectionIsStreaming) || transitionedFromNetworkError {
continue
}

Expand Down

0 comments on commit c8fb3ec

Please sign in to comment.