Skip to content

Commit

Permalink
GODRIVER-2572 Clean up code per review
Browse files Browse the repository at this point in the history
  • Loading branch information
prestonvasquez committed Aug 14, 2023
1 parent cb497c0 commit 3da9902
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 83 deletions.
50 changes: 24 additions & 26 deletions mongo/collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -1870,49 +1870,47 @@ func (coll *Collection) drop(ctx context.Context) error {

type pinnedServerSelector struct {
stringer fmt.Stringer
fn description.ServerSelectorFunc
fallback description.ServerSelector
session *session.Client
}

func (pss pinnedServerSelector) String() string {
if pss.stringer == nil {
return ""
}

return pss.stringer.String()
}

func (pss pinnedServerSelector) SelectServer(
t description.Topology,
s []description.Server,
svrs []description.Server,
) ([]description.Server, error) {
return pss.fn(t, s)
}

// pinnedSelectorFunc makes a selector for a pinned session with a pinned
// server. It attempts to do server selection on the pinned server, but if that
// fails, it will go through a list of default selectors.
func pinnedSelectorFunc(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
}
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 nil, nil
}

return defaultSelector.SelectServer(t, svrs)
return nil, nil
}

return pss.fallback.SelectServer(t, svrs)
}

func makePinnedSelector(sess *session.Client, defaultSelector description.ServerSelector) description.ServerSelector {
if srvSelectorStringer, ok := defaultSelector.(fmt.Stringer); ok {
return pinnedServerSelector{
stringer: srvSelectorStringer,
fn: pinnedSelectorFunc(sess, defaultSelector),
}
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 pinnedSelectorFunc(sess, defaultSelector)
return pss
}

func makeReadPrefSelector(sess *session.Client, selector description.ServerSelector, localThreshold time.Duration) description.ServerSelector {
Expand Down
7 changes: 3 additions & 4 deletions mongo/description/server_selector.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,15 @@ type compositeSelector struct {
}

func (cs *compositeSelector) info() serverSelectorInfo {
cssInfo := &serverSelectorInfo{Type: "compositeSelector"}
csInfo := serverSelectorInfo{Type: "compositeSelector"}

for _, sel := range cs.selectors {
if getter, ok := sel.(serverSelectorInfoGetter); ok {
cssInfo.Selectors = append(cssInfo.Selectors, getter.info())
csInfo.Selectors = append(csInfo.Selectors, getter.info())
}
}

return *cssInfo
return csInfo
}

// String returns the JSON string representation of the compositeSelector.
Expand Down Expand Up @@ -204,7 +204,6 @@ func ReadPrefSelector(rp *readpref.ReadPref) ServerSelector {
rp: rp,
isOutputAggregate: false,
}

}

func (selector readPrefServerSelector) info() serverSelectorInfo {
Expand Down
14 changes: 7 additions & 7 deletions mongo/integration/unified/client_entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ type clientEntity struct {
observedEvents map[monitoringEventType]struct{}
storedEvents map[monitoringEventType][]string
eventsCount map[monitoringEventType]int32
serverDescriptionChangedEventsCount map[serverDescriptionChangedEvent]int32
serverDescriptionChangedEventsCount map[serverDescriptionChangedEventInfo]int32

eventsCountLock sync.RWMutex
serverDescriptionChangedEventsCountLock sync.RWMutex
Expand All @@ -89,7 +89,7 @@ func newClientEntity(ctx context.Context, em *EntityMap, entityOptions *entityOp
observedEvents: make(map[monitoringEventType]struct{}),
storedEvents: make(map[monitoringEventType][]string),
eventsCount: make(map[monitoringEventType]int32),
serverDescriptionChangedEventsCount: make(map[serverDescriptionChangedEvent]int32),
serverDescriptionChangedEventsCount: make(map[serverDescriptionChangedEventInfo]int32),
entityMap: em,
observeSensitiveCommands: entityOptions.ObserveSensitiveCommands,
}
Expand Down Expand Up @@ -302,7 +302,7 @@ func (c *clientEntity) addEventsCount(eventType monitoringEventType) {
c.eventsCount[eventType]++
}

func (c *clientEntity) addServerDescriptionChangedEventCount(evt serverDescriptionChangedEvent) {
func (c *clientEntity) addServerDescriptionChangedEventCount(evt serverDescriptionChangedEventInfo) {
c.serverDescriptionChangedEventsCountLock.Lock()
defer c.serverDescriptionChangedEventsCountLock.Unlock()

Expand All @@ -316,7 +316,7 @@ func (c *clientEntity) getEventCount(eventType monitoringEventType) int32 {
return c.eventsCount[eventType]
}

func (c *clientEntity) getServerDescriptionChangedEventCount(evt serverDescriptionChangedEvent) int32 {
func (c *clientEntity) getServerDescriptionChangedEventCount(evt serverDescriptionChangedEventInfo) int32 {
c.serverDescriptionChangedEventsCountLock.Lock()
defer c.serverDescriptionChangedEventsCountLock.Unlock()

Expand Down Expand Up @@ -475,15 +475,15 @@ func (c *clientEntity) processServerDescriptionChangedEvent(evt *event.ServerDes
return
}

if _, ok := c.observedEvents[serverDescriptionChangedInfo]; ok {
if _, ok := c.observedEvents[serverDescriptionChangedEvent]; ok {
c.serverDescriptionChanged = append(c.serverDescriptionChanged, evt)
}

// Record object-specific unified spec test data on an event.
c.addServerDescriptionChangedEventCount(*newServerDescriptionChangedEvent(evt))
c.addServerDescriptionChangedEventCount(*newServerDescriptionChangedEventInfo(evt))

// Record the event generally.
c.addEventsCount(serverDescriptionChangedInfo)
c.addEventsCount(serverDescriptionChangedEvent)
}

func (c *clientEntity) processServerHeartbeatFailedEvent(evt *event.ServerHeartbeatFailedEvent) {
Expand Down
16 changes: 8 additions & 8 deletions mongo/integration/unified/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const (
connectionCheckOutFailedEvent monitoringEventType = "ConnectionCheckOutFailedEvent"
connectionCheckedOutEvent monitoringEventType = "ConnectionCheckedOutEvent"
connectionCheckedInEvent monitoringEventType = "ConnectionCheckedInEvent"
serverDescriptionChangedInfo monitoringEventType = "ServerDescriptionChangedEvent"
serverDescriptionChangedEvent monitoringEventType = "ServerDescriptionChangedEvent"
serverHeartbeatFailedEvent monitoringEventType = "ServerHeartbeatFailedEvent"
serverHeartbeatStartedEvent monitoringEventType = "ServerHeartbeatStartedEvent"
serverHeartbeatSucceededEvent monitoringEventType = "ServerHeartbeatSucceededEvent"
Expand Down Expand Up @@ -69,7 +69,7 @@ func monitoringEventTypeFromString(eventStr string) (monitoringEventType, bool)
case "connectioncheckedinevent":
return connectionCheckedInEvent, true
case "serverdescriptionchangedevent":
return serverDescriptionChangedInfo, true
return serverDescriptionChangedEvent, true
case "serverheartbeatfailedevent":
return serverHeartbeatFailedEvent, true
case "serverheartbeatstartedevent":
Expand Down Expand Up @@ -119,9 +119,9 @@ type serverDescription struct {
Type string
}

// serverDescriptionChangedEvent represents an event generated when the server
// serverDescriptionChangedEventInfo represents an event generated when the server
// description changes.
type serverDescriptionChangedEvent struct {
type serverDescriptionChangedEventInfo struct {
// NewDescription corresponds to the server description as it was after
// the change that triggered this event.
NewDescription serverDescription
Expand All @@ -131,10 +131,10 @@ type serverDescriptionChangedEvent struct {
PreviousDescription serverDescription
}

// newServerDescriptionChangedEvent returns a new serverDescriptionChangedEvent
// newServerDescriptionChangedEventInfo returns a new serverDescriptionChangedEvent
// instance for the given event.
func newServerDescriptionChangedEvent(evt *event.ServerDescriptionChangedEvent) *serverDescriptionChangedEvent {
return &serverDescriptionChangedEvent{
func newServerDescriptionChangedEventInfo(evt *event.ServerDescriptionChangedEvent) *serverDescriptionChangedEventInfo {
return &serverDescriptionChangedEventInfo{
NewDescription: serverDescription{
Type: evt.NewDescription.Kind.String(),
},
Expand All @@ -146,7 +146,7 @@ func newServerDescriptionChangedEvent(evt *event.ServerDescriptionChangedEvent)

// UnmarshalBSON unmarshals the event from BSON, used when trying to create the
// expected event from a unified spec test.
func (evt *serverDescriptionChangedEvent) UnmarshalBSON(data []byte) error {
func (evt *serverDescriptionChangedEventInfo) UnmarshalBSON(data []byte) error {
if len(data) == 0 {
return nil
}
Expand Down
6 changes: 3 additions & 3 deletions mongo/integration/unified/testrunner_operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -302,10 +302,10 @@ func getServerDescriptionChangedEventCount(client *clientEntity, raw bson.Raw) i
// If the document has no values, then we assume that the UST only
// intends to check that the event happened.
if values, _ := raw.Values(); len(values) == 0 {
return client.getEventCount(serverDescriptionChangedInfo)
return client.getEventCount(serverDescriptionChangedEvent)
}

var expectedEvt serverDescriptionChangedEvent
var expectedEvt serverDescriptionChangedEventInfo
if err := bson.Unmarshal(raw, &expectedEvt); err != nil {
return 0
}
Expand All @@ -323,7 +323,7 @@ func (args waitForEventArguments) eventCompleted(client *clientEntity) bool {
}

switch eventType {
case serverDescriptionChangedInfo:
case serverDescriptionChangedEvent:
if getServerDescriptionChangedEventCount(client, eventDoc) < args.Count {
return false
}
Expand Down
Loading

0 comments on commit 3da9902

Please sign in to comment.