Skip to content
This repository was archived by the owner on Jul 21, 2025. It is now read-only.

Commit f757a77

Browse files
committed
transport: cluster, add Nodes() in place of Topology() and setTopology()
The point of Topology() and setTopology() cluster methods was to give some sort of type safety as topology was an atomic.Value that can hold any type. As topology is now an atomic.Pointer they are no longer needed. Session sometimes needs to perform a query on all nodes of the cluster, to allow that cluster now has an exported Nodes() method.
1 parent ffef1b2 commit f757a77

File tree

4 files changed

+17
-21
lines changed

4 files changed

+17
-21
lines changed

session.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ func (s *Session) Prepare(ctx context.Context, content string) (Query, error) {
161161
stmt := transport.Statement{Content: content, Consistency: frame.ALL}
162162

163163
// Prepare on all nodes concurrently.
164-
nodes := s.cluster.Topology().Nodes
164+
nodes := s.cluster.Nodes()
165165
resStmt := make([]transport.Statement, len(nodes))
166166
resErr := make([]error, len(nodes))
167167
var wg sync.WaitGroup
@@ -234,7 +234,7 @@ func (s *Session) handleAutoAwaitSchemaAgreement(ctx context.Context, stmt strin
234234

235235
func (s *Session) CheckSchemaAgreement(ctx context.Context) (bool, error) {
236236
// Get schema version from all nodes concurrently.
237-
nodes := s.cluster.Topology().Nodes
237+
nodes := s.cluster.Nodes()
238238
versions := make([]frame.UUID, len(nodes))
239239
errors := make([]error, len(nodes))
240240
var wg sync.WaitGroup

transport/cluster.go

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ type Cluster struct {
3838
queryInfoCounter atomic.Uint64
3939
}
4040

41+
func (c *Cluster) Nodes() []*Node {
42+
return c.topology.Load().Nodes
43+
}
44+
4145
type topology struct {
4246
localDC string
4347
peers peerMap
@@ -86,13 +90,13 @@ type QueryInfo struct {
8690
func (c *Cluster) NewQueryInfo() QueryInfo {
8791
return QueryInfo{
8892
tokenAware: false,
89-
topology: c.Topology(),
93+
topology: c.topology.Load(),
9094
offset: c.generateOffset(),
9195
}
9296
}
9397

9498
func (c *Cluster) NewTokenAwareQueryInfo(t Token, ks string) (QueryInfo, error) {
95-
top := c.Topology()
99+
top := c.topology.Load()
96100
// When keyspace is not specified, we take default keyspace from ConnConfig.
97101
if ks == "" {
98102
if c.cfg.Keyspace == "" {
@@ -144,7 +148,7 @@ func NewCluster(ctx context.Context, cfg ConnConfig, p HostSelectionPolicy, e []
144148
if p, ok := p.(*TokenAwarePolicy); ok {
145149
localDC = p.localDC
146150
}
147-
c.setTopology(&topology{localDC: localDC})
151+
c.topology.Store(&topology{localDC: localDC})
148152

149153
if control, err := c.NewControl(ctx); err != nil {
150154
return nil, fmt.Errorf("create control connection: %w", err)
@@ -190,9 +194,9 @@ func (c *Cluster) refreshTopology(ctx context.Context) error {
190194
return fmt.Errorf("query info about nodes in cluster: %w", err)
191195
}
192196

193-
old := c.Topology().peers
197+
old := c.topology.Load().peers
194198
t := newTopology()
195-
t.localDC = c.Topology().localDC
199+
t.localDC = c.topology.Load().localDC
196200
t.keyspaces, err = c.updateKeyspace(ctx)
197201
if err != nil {
198202
return fmt.Errorf("query keyspaces: %w", err)
@@ -247,7 +251,7 @@ func (c *Cluster) refreshTopology(ctx context.Context) error {
247251
t.policyInfo.Preprocess(t, keyspace{})
248252
}
249253

250-
c.setTopology(t)
254+
c.topology.Store(t)
251255
drainChan(c.refreshChan)
252256
return nil
253257
}
@@ -442,14 +446,6 @@ func parseTokensFromRow(n *Node, r frame.Row, ring *Ring) error {
442446
return nil
443447
}
444448

445-
func (c *Cluster) Topology() *topology {
446-
return c.topology.Load()
447-
}
448-
449-
func (c *Cluster) setTopology(t *topology) {
450-
c.topology.Store(t)
451-
}
452-
453449
// handleEvent creates function which is passed to control connection
454450
// via registerEvents in order to handle events right away instead
455451
// of registering handlers for them.
@@ -478,7 +474,7 @@ func (c *Cluster) handleTopologyChange(v *TopologyChange) {
478474

479475
func (c *Cluster) handleStatusChange(v *StatusChange) {
480476
log.Printf("cluster: handle status change: %+#v", v)
481-
m := c.Topology().peers
477+
m := c.topology.Load().peers
482478
addr := v.Address.String()
483479
if n, ok := m[addr]; ok {
484480
switch v.Status {
@@ -549,7 +545,7 @@ func (c *Cluster) tryReopenControl(ctx context.Context) {
549545
func (c *Cluster) handleClose() {
550546
log.Printf("cluster: handle cluster close")
551547
c.control.Close()
552-
m := c.Topology().peers
548+
m := c.topology.Load().peers
553549
for _, v := range m {
554550
if v.pool != nil {
555551
v.pool.Close()

transport/cluster_integration_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import (
1717
const awaitingChanges = 100 * time.Millisecond
1818

1919
func compareNodes(c *Cluster, addr string, expected *Node) error {
20-
m := c.Topology().peers
20+
m := c.topology.Load().peers
2121
got, ok := m[addr]
2222
switch {
2323
case !ok:
@@ -80,7 +80,7 @@ func TestClusterIntegration(t *testing.T) {
8080
}
8181

8282
// There should be at least system keyspaces present.
83-
if len(c.Topology().keyspaces) == 0 {
83+
if len(c.topology.Load().keyspaces) == 0 {
8484
t.Fatalf("Keyspaces failed to load")
8585
}
8686

transport/policy_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ func mockCluster(t *topology, ks, localDC string) *Cluster {
3333
} else {
3434
t.policyInfo.Preprocess(t, keyspace{})
3535
}
36-
c.setTopology(t)
36+
c.topology.Store(t)
3737

3838
return &c
3939
}

0 commit comments

Comments
 (0)