Skip to content

Commit 5f52e42

Browse files
author
Divjot Arora
authored
GODRIVER-1740 Clean up address and description packages (#514)
1 parent a325a99 commit 5f52e42

20 files changed

+246
-391
lines changed

mongo/collection.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1633,7 +1633,14 @@ func (coll *Collection) Drop(ctx context.Context) error {
16331633
func makePinnedSelector(sess *session.Client, defaultSelector description.ServerSelector) description.ServerSelectorFunc {
16341634
return func(t description.Topology, svrs []description.Server) ([]description.Server, error) {
16351635
if sess != nil && sess.PinnedServer != nil {
1636-
return sess.PinnedServer.SelectServer(t, svrs)
1636+
// If there is a pinned server, try to find it in the list of candidates.
1637+
for _, candidate := range svrs {
1638+
if candidate.Addr == sess.PinnedServer.Addr {
1639+
return []description.Server{candidate}, nil
1640+
}
1641+
}
1642+
1643+
return nil, nil
16371644
}
16381645

16391646
return defaultSelector.SelectServer(t, svrs)

mongo/database.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ func (db *Database) processRunCommand(ctx context.Context, cmd interface{},
162162
description.LatencySelector(db.client.localThreshold),
163163
})
164164
if sess != nil && sess.PinnedServer != nil {
165-
readSelect = sess.PinnedServer
165+
readSelect = makePinnedSelector(sess, readSelect)
166166
}
167167

168168
return operation.NewCommand(runCmdDoc).

mongo/description/feature.go

Lines changed: 0 additions & 36 deletions
This file was deleted.

mongo/description/feature_test.go

Lines changed: 0 additions & 46 deletions
This file was deleted.

mongo/description/selector_write_test.go

Lines changed: 0 additions & 7 deletions
This file was deleted.

mongo/description/server.go

Lines changed: 6 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,14 @@ import (
1818
"go.mongodb.org/mongo-driver/tag"
1919
)
2020

21-
// UnsetRTT is the unset value for a round trip time.
22-
const UnsetRTT = -1 * time.Millisecond
23-
24-
// SelectedServer represents a selected server that is a member of a topology.
21+
// SelectedServer augments the Server type by also including the TopologyKind of the topology that includes the server.
22+
// This type should be used to track the state of a server that was selected to perform an operation.
2523
type SelectedServer struct {
2624
Server
2725
Kind TopologyKind
2826
}
2927

30-
// Server represents a description of a server. This is created from an isMaster
31-
// command.
28+
// Server contains information about a node in a cluster. This is created from isMaster command responses.
3229
type Server struct {
3330
Addr address.Address
3431

@@ -59,7 +56,7 @@ type Server struct {
5956
WireVersion *VersionRange
6057
}
6158

62-
// NewServer creates a new server description from the given parameters.
59+
// NewServer creates a new server description from the given isMaster command response.
6360
func NewServer(addr address.Address, response bson.Raw) Server {
6461
desc := Server{Addr: addr, CanonicalAddr: addr, LastUpdateTime: time.Now().UTC()}
6562
elements, err := response.Elements()
@@ -318,12 +315,7 @@ func NewServerFromError(addr address.Address, err error, tv *TopologyVersion) Se
318315
// SetAverageRTT sets the average round trip time for this server description.
319316
func (s Server) SetAverageRTT(rtt time.Duration) Server {
320317
s.AverageRTT = rtt
321-
if rtt == UnsetRTT {
322-
s.AverageRTTSet = false
323-
} else {
324-
s.AverageRTTSet = true
325-
}
326-
318+
s.AverageRTTSet = true
327319
return s
328320
}
329321

@@ -335,16 +327,6 @@ func (s Server) DataBearing() bool {
335327
s.Kind == Standalone
336328
}
337329

338-
// SelectServer selects this server if it is in the list of given candidates.
339-
func (s Server) SelectServer(_ Topology, candidates []Server) ([]Server, error) {
340-
for _, candidate := range candidates {
341-
if candidate.Addr == s.Addr {
342-
return []Server{candidate}, nil
343-
}
344-
}
345-
return nil, nil
346-
}
347-
348330
// String implements the Stringer interface
349331
func (s Server) String() string {
350332
str := fmt.Sprintf("Addr: %s, Type: %s",
@@ -382,11 +364,6 @@ func decodeStringMap(element bson.RawElement, name string) (map[string]string, e
382364
return m, nil
383365
}
384366

385-
// SupportsRetryWrites returns true if this description represents a server that supports retryable writes.
386-
func (s Server) SupportsRetryWrites() bool {
387-
return s.SessionTimeoutMinutes != 0 && s.Kind != Standalone
388-
}
389-
390367
// Equal compares two server descriptions and returns true if they are equal
391368
func (s Server) Equal(other Server) bool {
392369
if s.CanonicalAddr.String() != other.CanonicalAddr.String() {
@@ -446,7 +423,7 @@ func (s Server) Equal(other Server) bool {
446423
return false
447424
}
448425

449-
if s.TopologyVersion != other.TopologyVersion && CompareTopologyVersion(s.TopologyVersion, other.TopologyVersion) != 0 {
426+
if s.TopologyVersion.CompareToIncoming(other.TopologyVersion) != 0 {
450427
return false
451428
}
452429

mongo/description/server_kind.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
package description
88

9-
// ServerKind represents the type of a server.
9+
// ServerKind represents the type of a single server in a topology.
1010
type ServerKind uint32
1111

1212
// These constants are the possible types of servers.
@@ -20,7 +20,7 @@ const (
2020
Mongos ServerKind = 256
2121
)
2222

23-
// String implements the fmt.Stringer interface.
23+
// String returns a stringified version of the kind or "Unknown" if the kind is invalid.
2424
func (kind ServerKind) String() string {
2525
switch kind {
2626
case Standalone:

mongo/description/server_selector.go

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@ import (
1515
"go.mongodb.org/mongo-driver/tag"
1616
)
1717

18-
// ServerSelector is an interface implemented by types that can select a server given a
19-
// topology description.
18+
// ServerSelector is an interface implemented by types that can perform server selection given a topology description
19+
// and list of candidate servers. The selector should filter the provided candidates list and return a subset that
20+
// matches some criteria.
2021
type ServerSelector interface {
2122
SelectServer(Topology, []Server) ([]Server, error)
2223
}
@@ -33,7 +34,16 @@ type compositeSelector struct {
3334
selectors []ServerSelector
3435
}
3536

36-
// CompositeSelector combines multiple selectors into a single selector.
37+
// CompositeSelector combines multiple selectors into a single selector by applying them in order to the candidates
38+
// list.
39+
//
40+
// For example, if the initial candidates list is [s0, s1, s2, s3] and two selectors are provided where the first
41+
// matches s0 and s1 and the second matches s1 and s2, the following would occur during server selection:
42+
//
43+
// 1. firstSelector([s0, s1, s2, s3]) -> [s0, s1]
44+
// 2. secondSelector([s0, s1]) -> [s1]
45+
//
46+
// The final list of candidates returned by the composite selector would be [s1].
3747
func CompositeSelector(selectors []ServerSelector) ServerSelector {
3848
return &compositeSelector{selectors: selectors}
3949
}
@@ -53,7 +63,7 @@ type latencySelector struct {
5363
latency time.Duration
5464
}
5565

56-
// LatencySelector creates a ServerSelector which selects servers based on their latency.
66+
// LatencySelector creates a ServerSelector which selects servers based on their average RTT values.
5767
func LatencySelector(latency time.Duration) ServerSelector {
5868
return &latencySelector{latency: latency}
5969
}
@@ -120,7 +130,7 @@ func ReadPrefSelector(rp *readpref.ReadPref) ServerSelector {
120130
if _, set := rp.MaxStaleness(); set {
121131
for _, s := range candidates {
122132
if s.Kind != Unknown {
123-
if err := MaxStalenessSupported(s.WireVersion); err != nil {
133+
if err := maxStalenessSupported(s.WireVersion); err != nil {
124134
return nil, err
125135
}
126136
}
@@ -140,6 +150,15 @@ func ReadPrefSelector(rp *readpref.ReadPref) ServerSelector {
140150
})
141151
}
142152

153+
// maxStalenessSupported returns an error if the given server version does not support max staleness.
154+
func maxStalenessSupported(wireVersion *VersionRange) error {
155+
if wireVersion != nil && wireVersion.Max < 5 {
156+
return fmt.Errorf("max staleness is only supported for servers 3.4 or newer")
157+
}
158+
159+
return nil
160+
}
161+
143162
func selectForReplicaSet(rp *readpref.ReadPref, t Topology, candidates []Server) ([]Server, error) {
144163
if err := verifyMaxStaleness(rp, t); err != nil {
145164
return nil, err

0 commit comments

Comments
 (0)