Skip to content

Commit

Permalink
Fix race condition with tokenAwareHostPolicy SetPartitioner (apache#988)
Browse files Browse the repository at this point in the history
- it wasn't using the lock and causing data races
  • Loading branch information
inf-rno authored and Zariel committed Oct 16, 2017
1 parent 2416cf3 commit 04cbe7a
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -96,3 +96,4 @@ Dharmendra Parsaila <d4dharmu@gmail.com>
Nayef Ghattas <nayef.ghattas@datadoghq.com>
Michał Matczuk <mmatczuk@gmail.com>
Ben Krebsbach <ben.krebsbach@gmail.com>
Vivian Mathews <vivian.mathews.3@gmail.com>
12 changes: 9 additions & 3 deletions policies.go
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,9 @@ type tokenAwareHostPolicy struct {
}

func (t *tokenAwareHostPolicy) SetPartitioner(partitioner string) {
t.mu.Lock()
defer t.mu.Unlock()

if t.partitioner != partitioner {
t.fallback.SetPartitioner(partitioner)
t.partitioner = partitioner
Expand All @@ -306,13 +309,19 @@ func (t *tokenAwareHostPolicy) SetPartitioner(partitioner string) {
}

func (t *tokenAwareHostPolicy) AddHost(host *HostInfo) {
t.mu.Lock()
defer t.mu.Unlock()

t.hosts.add(host)
t.fallback.AddHost(host)

t.resetTokenRing()
}

func (t *tokenAwareHostPolicy) RemoveHost(host *HostInfo) {
t.mu.Lock()
defer t.mu.Unlock()

t.hosts.remove(host.ConnectAddress())
t.fallback.RemoveHost(host)

Expand All @@ -328,9 +337,6 @@ func (t *tokenAwareHostPolicy) HostDown(host *HostInfo) {
}

func (t *tokenAwareHostPolicy) resetTokenRing() {
t.mu.Lock()
defer t.mu.Unlock()

if t.partitioner == "" {
// partitioner not yet set
return
Expand Down

0 comments on commit 04cbe7a

Please sign in to comment.