-
Notifications
You must be signed in to change notification settings - Fork 1.9k
chore: finish up the move to atomic types #3399
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -348,20 +348,20 @@ type testFuncConsumerGroupMessage struct { | |
|
|
||
| type testFuncConsumerGroupSink struct { | ||
| msgs chan testFuncConsumerGroupMessage | ||
| count int32 | ||
| count atomic.Int32 | ||
| } | ||
|
|
||
| func (s *testFuncConsumerGroupSink) Len() int { | ||
| if s == nil { | ||
| return -1 | ||
| } | ||
| return int(atomic.LoadInt32(&s.count)) | ||
| return int(s.count.Load()) | ||
| } | ||
|
|
||
| func (s *testFuncConsumerGroupSink) Push(clientID string, m *ConsumerMessage) { | ||
| if s != nil { | ||
| s.msgs <- testFuncConsumerGroupMessage{ClientID: clientID, ConsumerMessage: m} | ||
| atomic.AddInt32(&s.count, 1) | ||
| s.count.Add(1) | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -378,18 +378,19 @@ func (s *testFuncConsumerGroupSink) Close() map[string][]string { | |
|
|
||
| type testFuncConsumerGroupMember struct { | ||
| ConsumerGroup | ||
| clientID string | ||
| claims map[string]int | ||
| generationId int32 | ||
| state int32 | ||
| handlers int32 | ||
| errs []error | ||
| maxMessages int32 | ||
| isCapped bool | ||
| sink *testFuncConsumerGroupSink | ||
| t *testing.T | ||
| clientID string | ||
| isCapped bool | ||
| sink *testFuncConsumerGroupSink | ||
|
|
||
| t *testing.T | ||
| mu sync.RWMutex | ||
| generationId atomic.Int32 | ||
| state atomic.Int32 | ||
| handlers atomic.Int32 | ||
| maxMessages atomic.Int32 | ||
|
|
||
| mu sync.RWMutex | ||
| claims map[string]int | ||
| errs []error | ||
|
Comment on lines
+392
to
+393
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These are the two fields that can update after initialization, and are already correctly protected by mutexes. Their movement to here is just to match convention of putting mutex protected values below the mutex that protects them. |
||
| } | ||
|
|
||
| func defaultConfig(clientID string) *Config { | ||
|
|
@@ -443,11 +444,11 @@ func runTestFuncConsumerGroupMemberWithConfig( | |
| ConsumerGroup: group, | ||
| clientID: config.ClientID, | ||
| claims: make(map[string]int), | ||
| maxMessages: maxMessages, | ||
| isCapped: maxMessages != 0, | ||
| sink: sink, | ||
| t: t, | ||
| } | ||
| member.maxMessages.Store(maxMessages) | ||
| go member.loop(topics) | ||
| return member | ||
| } | ||
|
|
@@ -480,15 +481,15 @@ func (m *testFuncConsumerGroupMember) WaitForState(expected int32) { | |
| m.t.Helper() | ||
|
|
||
| m.waitFor("state", expected, func() (interface{}, error) { | ||
| return atomic.LoadInt32(&m.state), nil | ||
| return m.state.Load(), nil | ||
| }) | ||
| } | ||
|
|
||
| func (m *testFuncConsumerGroupMember) WaitForHandlers(expected int) { | ||
| m.t.Helper() | ||
|
|
||
| m.waitFor("handlers", expected, func() (interface{}, error) { | ||
| return int(atomic.LoadInt32(&m.handlers)), nil | ||
| return int(m.handlers.Load()), nil | ||
| }) | ||
| } | ||
|
|
||
|
|
@@ -518,17 +519,17 @@ func (m *testFuncConsumerGroupMember) Setup(s ConsumerGroupSession) error { | |
| m.mu.Unlock() | ||
|
|
||
| // store generationID | ||
| atomic.StoreInt32(&m.generationId, s.GenerationID()) | ||
| m.generationId.Store(s.GenerationID()) | ||
|
|
||
| // enter post-setup state | ||
| atomic.StoreInt32(&m.state, 2) | ||
| m.state.Store(2) | ||
| return nil | ||
| } | ||
|
|
||
| func (m *testFuncConsumerGroupMember) Cleanup(s ConsumerGroupSession) error { | ||
| m.t.Logf("Consumer %s: session ended %s in generation %d", m.clientID, s.MemberID(), s.GenerationID()) | ||
| // enter post-cleanup state | ||
| atomic.StoreInt32(&m.state, 3) | ||
| m.state.Store(3) | ||
| return nil | ||
| } | ||
|
|
||
|
|
@@ -538,8 +539,8 @@ func (m *testFuncConsumerGroupMember) ConsumeClaim(s ConsumerGroupSession, c Con | |
| m.t.Errorf("panic in ConsumeClaim: %v", r) | ||
| } | ||
| }() | ||
| atomic.AddInt32(&m.handlers, 1) | ||
| defer atomic.AddInt32(&m.handlers, -1) | ||
| m.handlers.Add(1) | ||
| defer m.handlers.Add(-1) | ||
|
|
||
| consumed := 0 | ||
| for { | ||
|
|
@@ -549,7 +550,7 @@ func (m *testFuncConsumerGroupMember) ConsumeClaim(s ConsumerGroupSession, c Con | |
| m.t.Logf("Consumer %s: message channel closed, consumed %d messages", m.clientID, consumed) | ||
| return nil | ||
| } | ||
| if n := atomic.AddInt32(&m.maxMessages, -1); m.isCapped && n < 0 { | ||
| if n := m.maxMessages.Add(-1); m.isCapped && n < 0 { | ||
| m.t.Logf("Consumer %s: reached max messages, consumed %d messages", m.clientID, consumed) | ||
| return nil | ||
| } | ||
|
|
@@ -597,7 +598,7 @@ func (m *testFuncConsumerGroupMember) loop(topics []string) { | |
| if r := recover(); r != nil { | ||
| m.t.Errorf("panic in loop for %s: %v", m.clientID, r) | ||
| } | ||
| atomic.StoreInt32(&m.state, 4) | ||
| m.state.Store(4) | ||
| }() | ||
|
|
||
| go func() { | ||
|
|
@@ -614,7 +615,7 @@ func (m *testFuncConsumerGroupMember) loop(topics []string) { | |
| ctx := context.Background() | ||
| for { | ||
| // set state to pre-consume | ||
| atomic.StoreInt32(&m.state, 1) | ||
| m.state.Store(1) | ||
|
|
||
| if err := m.Consume(ctx, topics, m); errors.Is(err, ErrClosedConsumerGroup) { | ||
| m.t.Logf("Consumer %s: closed consumer group", m.clientID) | ||
|
|
@@ -634,7 +635,7 @@ func (m *testFuncConsumerGroupMember) loop(topics []string) { | |
| } | ||
|
|
||
| // return if capped | ||
| if n := atomic.LoadInt32(&m.maxMessages); m.isCapped && n < 0 { | ||
| if n := m.maxMessages.Load(); m.isCapped && n < 0 { | ||
| m.t.Logf("Consumer %s: reached max messages, returning from loop", m.clientID) | ||
| return | ||
| } | ||
|
|
@@ -651,7 +652,7 @@ func newTestStatefulStrategy(t *testing.T) *testStatefulStrategy { | |
| type testStatefulStrategy struct { | ||
| BalanceStrategy | ||
| t *testing.T | ||
| initial int32 | ||
| initial atomic.Int32 | ||
| state sync.Map | ||
| } | ||
|
|
||
|
|
@@ -664,7 +665,7 @@ func (h *testStatefulStrategy) Plan(members map[string]ConsumerGroupMemberMetada | |
| for memberID, metadata := range members { | ||
| if !strings.HasSuffix(string(metadata.UserData), "-stateful") { | ||
| metadata.UserData = []byte(string(metadata.UserData) + "-stateful") | ||
| atomic.AddInt32(&h.initial, 1) | ||
| h.initial.Add(1) | ||
| } | ||
| h.state.Store(memberID, metadata.UserData) | ||
| } | ||
|
|
@@ -680,7 +681,7 @@ func (h *testStatefulStrategy) AssignmentData(memberID string, topics map[string | |
|
|
||
| func (h *testStatefulStrategy) AssertInitialValues(count int32) { | ||
| h.t.Helper() | ||
| actual := atomic.LoadInt32(&h.initial) | ||
| actual := h.initial.Load() | ||
| if actual != count { | ||
| h.t.Fatalf("unexpected count of initial values: %d, expected: %d", actual, count) | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -398,7 +398,8 @@ func (pc *PartitionConsumer) YieldMessage(msg *sarama.ConsumerMessage) *Partitio | |
| msg.Partition = pc.partition | ||
|
|
||
| if pc.paused { | ||
| msg.Offset = atomic.AddInt64(&pc.suppressedHighWaterMarkOffset, 1) - 1 | ||
| msg.Offset = pc.suppressedHighWaterMarkOffset | ||
| pc.suppressedHighWaterMarkOffset++ | ||
|
Comment on lines
+401
to
+402
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Rechecked this, and yes, the usage is indeed always made under lock. |
||
| pc.suppressedMessages <- msg | ||
| } else { | ||
| msg.Offset = pc.highWaterMarkOffset.Add(1) - 1 | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These are set at initialisation and never changed after, and thus can be accessed freely without mutex.