Skip to content

Commit

Permalink
Fix multiple lock acquire on membership update (cadence-workflow#5576)
Browse files Browse the repository at this point in the history
* Fix multiple lock acquire on membership update
  • Loading branch information
3vilhamster authored Jan 5, 2024
1 parent 185db70 commit a3ad939
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
14 changes: 11 additions & 3 deletions common/membership/hashring.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (

"github.com/dgryski/go-farm"
"github.com/uber/ringpop-go/hashring"
"github.com/uber/ringpop-go/membership"

"github.com/uber/cadence/common"
"github.com/uber/cadence/common/log"
Expand Down Expand Up @@ -239,9 +240,8 @@ func (r *ring) refresh() error {
}

ring := emptyHashring()
for _, member := range members {
ring.AddMembers(member)
}
ring.AddMembers(castToMembers(members)...)

r.members.keys = newMembersMap
r.members.refreshed = time.Now()
r.value.Store(ring)
Expand Down Expand Up @@ -292,3 +292,11 @@ func (r *ring) compareMembers(members []HostInfo) (map[string]HostInfo, bool) {
}
return newMembersMap, changed
}

func castToMembers[T membership.Member](members []T) []membership.Member {
result := make([]membership.Member, 0, len(members))
for _, h := range members {
result = append(result, h)
}
return result
}
4 changes: 2 additions & 2 deletions common/membership/hashring_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,15 +218,15 @@ func TestLookupAndRefreshRaceCondition(t *testing.T) {
wg.Add(2)
go func() {
for i := 0; i < 50; i++ {
hr.Lookup("a")
_, _ = hr.Lookup("a")
}
wg.Done()
}()
go func() {
for i := 0; i < 50; i++ {
// to bypass internal check
hr.members.refreshed = time.Now().AddDate(0, 0, -1)
hr.refresh()
assert.NoError(t, hr.refresh())
}
wg.Done()
}()
Expand Down

0 comments on commit a3ad939

Please sign in to comment.