Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

memberlist: get rid of exclusive mutex on get worker channel method #171

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions kv/memberlist/memberlist_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ type KV struct {
messageCounter int // Used to give each message in the sentMessages and receivedMessages a unique ID, for UI.

// Per-key value update workers
workersMu sync.Mutex
workersMu sync.RWMutex
workersChannels map[string]chan valueUpdate

// closed on shutdown
Expand Down Expand Up @@ -976,11 +976,19 @@ func (m *KV) NotifyMsg(msg []byte) {
}

func (m *KV) getKeyWorkerChannel(key string) chan<- valueUpdate {
m.workersMu.Lock()
defer m.workersMu.Unlock()

m.workersMu.RLock()
ch := m.workersChannels[key]
m.workersMu.RUnlock()

if ch == nil {
m.workersMu.Lock()
defer m.workersMu.Unlock()

ch = m.workersChannels[key] // double-checked locking
if ch != nil {
return ch
}

// spawn a key associated worker goroutine to process updates in background
ch = make(chan valueUpdate, notifyMsgQueueSize)
go m.processValueUpdate(ch, key)
Expand Down
22 changes: 22 additions & 0 deletions kv/memberlist/memberlist_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"net"
"reflect"
"sort"
"strconv"
"sync"
"testing"
"time"
Expand Down Expand Up @@ -1244,6 +1245,27 @@ func TestSendingOldTombstoneShouldNotForwardMessage(t *testing.T) {
}
}

func BenchmarkKV_GetWorkerChannel(b *testing.B) {
kv := NewKV(KVConfig{}, log.NewNopLogger(), &dnsProviderMock{}, prometheus.NewPedanticRegistry())

require.NoError(b, services.StartAndAwaitRunning(context.Background(), kv))
defer services.StopAndAwaitTerminated(context.Background(), kv) //nolint:errcheck

const totalNumberOfKeys = 4

wg := sync.WaitGroup{}
wg.Add(b.N)

b.ResetTimer()
for i := 0; i < b.N; i++ {
go func(idx int) {
_ = kv.getKeyWorkerChannel(strconv.Itoa(idx % totalNumberOfKeys))
wg.Done()
}(i)
Comment on lines +1261 to +1264
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Benchmark should not spawn new goroutine in each iteration.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can have constant number of goroutines, each doing N iterations.

}
wg.Wait()
}

func decodeDataFromMarshalledKeyValuePair(t *testing.T, marshalledKVP []byte, key string, codec dataCodec) *data {
kvp := KeyValuePair{}
require.NoError(t, kvp.Unmarshal(marshalledKVP))
Expand Down