Skip to content

Commit 9cf7e81

Browse files
author
Dorian Jaminais-Grellier
committed
Improve host_source locking and ring refresh concurrency
Remove ringDescriber mutex around GetHosts I/O; prevHosts/prevPartitioner were never written. Use read locks for ConnectAddressAndPort and a read-fast path for HostnameAndPort. Read peer validity under a single RLock in isValidPeer. Release errorBroadcaster mutex before sending to listeners. remove useless lock in isValidPeer
1 parent 941f298 commit 9cf7e81

2 files changed

Lines changed: 19 additions & 16 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1010
### Added
1111

1212
- Security-model discoverability (CASSANDRA-21464)
13+
- Improve host_source locking and ring refresh concurrency (CASSGO-121)
1314

1415
## [2.1.2]
1516

host_source.go

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,14 @@ func (h *HostInfo) IsUp() bool {
453453
}
454454

455455
func (h *HostInfo) HostnameAndPort() string {
456+
h.mu.RLock()
457+
if h.hostname != "" {
458+
s := net.JoinHostPort(h.hostname, strconv.Itoa(h.port))
459+
h.mu.RUnlock()
460+
return s
461+
}
462+
h.mu.RUnlock()
463+
456464
h.mu.Lock()
457465
defer h.mu.Unlock()
458466
if h.hostname == "" {
@@ -463,8 +471,8 @@ func (h *HostInfo) HostnameAndPort() string {
463471
}
464472

465473
func (h *HostInfo) ConnectAddressAndPort() string {
466-
h.mu.Lock()
467-
defer h.mu.Unlock()
474+
h.mu.RLock()
475+
defer h.mu.RUnlock()
468476
addr, _ := h.connectAddressLocked()
469477
return net.JoinHostPort(addr.String(), strconv.Itoa(h.port))
470478
}
@@ -484,10 +492,7 @@ func (h *HostInfo) String() string {
484492

485493
// Polls system.peers at a specific interval to find new hosts
486494
type ringDescriber struct {
487-
session *Session
488-
mu sync.Mutex
489-
prevHosts []*HostInfo
490-
prevPartitioner string
495+
session *Session
491496
}
492497

493498
// Returns true if we are using system_schema.keyspaces instead of system.schema_keyspaces
@@ -806,7 +811,7 @@ func (r *ringDescriber) getClusterPeerInfo(localHost *HostInfo) ([]*HostInfo, er
806811

807812
// Return true if the host is a valid peer
808813
func isValidPeer(host *HostInfo) bool {
809-
return !(len(host.RPCAddress()) == 0 ||
814+
return !(len(host.rpcAddress) == 0 ||
810815
host.hostId == "" ||
811816
host.dataCenter == "" ||
812817
host.missingRack ||
@@ -815,17 +820,14 @@ func isValidPeer(host *HostInfo) bool {
815820

816821
// GetHosts returns a list of hosts found via queries to system.local and system.peers
817822
func (r *ringDescriber) GetHosts() ([]*HostInfo, string, error) {
818-
r.mu.Lock()
819-
defer r.mu.Unlock()
820-
821823
localHost, err := r.getLocalHostInfo()
822824
if err != nil {
823-
return r.prevHosts, r.prevPartitioner, err
825+
return nil, "", err
824826
}
825827

826828
peerHosts, err := r.getClusterPeerInfo(localHost)
827829
if err != nil {
828-
return r.prevHosts, r.prevPartitioner, err
830+
return nil, "", err
829831
}
830832

831833
hosts := append([]*HostInfo{localHost}, peerHosts...)
@@ -1048,13 +1050,13 @@ func (b *errorBroadcaster) newListener() <-chan error {
10481050

10491051
func (b *errorBroadcaster) broadcast(err error) {
10501052
b.mu.Lock()
1051-
defer b.mu.Unlock()
10521053
curListeners := b.listeners
1053-
if len(curListeners) > 0 {
1054-
b.listeners = nil
1055-
} else {
1054+
if len(curListeners) == 0 {
1055+
b.mu.Unlock()
10561056
return
10571057
}
1058+
b.listeners = nil
1059+
b.mu.Unlock()
10581060

10591061
for _, listener := range curListeners {
10601062
listener <- err

0 commit comments

Comments
 (0)