Skip to content

chore: refactor to use modern atomic types - #3277

Merged
dnwe merged 3 commits into
IBM:mainfrom
Sahil-4555:refactor-atomic-types
Sep 2, 2025
Merged

chore: refactor to use modern atomic types#3277
dnwe merged 3 commits into
IBM:mainfrom
Sahil-4555:refactor-atomic-types

Conversation

@Sahil-4555

@Sahil-4555 Sahil-4555 commented Sep 2, 2025

Copy link
Copy Markdown
Contributor

What type of PR is this?

Other

What does this PR do? Why is it needed?
This PR updates our atomic operations to use Go 1.19's newer typed atomic approach instead of the old function-based method. We're changing fields like int64 to atomic.Int64 so we can write cleaner code like child.retries.Add(1) instead of atomic.AddInt32(&child.retries, 1). This removes the need for pointer handling and gives us better compile-time safety when multiple threads access the same variables in operations.

Which issues(s) does this PR fix?
The function-based atomics allowed accidental non-atomic access to shared variables, causing race conditions in concurrent operations. The verbose atomic.AddInt32(&child.retries, 1) syntax was harder to maintain. Typed atomics enforce atomic-only access at compile time and use cleaner method calls, preventing threading bugs.

@Sahil-4555
Sahil-4555 force-pushed the refactor-atomic-types branch from 198bd93 to a36a9b8 Compare September 2, 2025 05:59

@puellanivis puellanivis left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Just a few suggestions.

Comment thread broker.go Outdated
Comment thread consumer.go Outdated
Comment thread mocks/consumer.go Outdated
Comment thread mocks/consumer.go Outdated
Signed-off-by: Sahil-4555 <sahilsojitra4555@gmail.com>
Signed-off-by: Sahil-4555 <sahilsojitra4555@gmail.com>
@Sahil-4555
Sahil-4555 force-pushed the refactor-atomic-types branch from 659dc7a to 32abe92 Compare September 2, 2025 14:49
Signed-off-by: Sahil-4555 <sahilsojitra4555@gmail.com>
@Sahil-4555
Sahil-4555 force-pushed the refactor-atomic-types branch from aaab49a to 2fb0acd Compare September 2, 2025 14:59
@dnwe dnwe added the chore label Sep 2, 2025
@dnwe dnwe changed the title refactor to use atomic types Sep 2, 2025

@puellanivis puellanivis left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I don’t see anything to comment on. 👍

@dnwe dnwe left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks! A good piece of housekeeping

@dnwe
dnwe merged commit 72c7448 into IBM:main Sep 2, 2025
17 checks passed
@Sahil-4555
Sahil-4555 deleted the refactor-atomic-types branch November 27, 2025 05:21
dnwe pushed a commit that referenced this pull request Dec 17, 2025
This completes the change in #3277 and rolls out all atomics usage to
using the types, rather than the bare functions.

This already found a few usages that had inconsistently used atomic
functions on some values.

---------

Signed-off-by: Cassondra Foesch <puellanivis@gmail.com>
3AceShowHand pushed a commit to 3AceShowHand/sarama that referenced this pull request May 7, 2026
3AceShowHand added a commit to pingcap/sarama that referenced this pull request May 7, 2026
* fix: close broken tcp connections

Prevent TCP connection leaks that error occurred to kernel space's socket.
Previously, when errors occurred on TCP sockets, connections would
remain in TCP_CLOSE state indefinitely without being properly freed.

The leaked connections caused a cascade of kernel-level issues:
- TCP sockets remain in TCP_CLOSE state indefinitely
- Their sk_receive_queues retain FINACK skb packets
- These skbs hold pages allocated from page_pool
- page_pool_release_retry() stalls because pages cannot be freed
  while still referenced by the TCP stack

The fix ensures that TCP connections are properly closed when socket
errors occur, preventing resource leaks at both the application and
kernel levels.

Signed-off-by: Leon Hwang <leon.hwang@linux.dev>

* fix: keep bootstrap brokers and unblock async shutdown

Skip socket health checks while a broker is still opening so metadata refresh does not block behind the broker mutex.

Keep seed and dead-seed brokers in their bootstrap pools after socket errors so they can be reopened on a later refresh.

Close broker sockets before waiting for the response reader so health-check cleanup and SASL v1 auth failures cannot hang while async responses are still in flight.

Signed-off-by: Dominic Evans <dominic.evans@uk.ibm.com>

* fix: skip broker health tests without socket probing

Signed-off-by: Dominic Evans <dominic.evans@uk.ibm.com>

* fix: remove return from checkSeedBrokersHealth

Signed-off-by: Dominic Evans <dominic.evans@uk.ibm.com>

* fix: add Unwrap() to DescribeConfigError and AlterConfigError (IBM#3487)

## Summary
`TopicError` and `TopicPartitionError` implement `Unwrap()` returning
their `KError` field, enabling `errors.Is`/`errors.As` to traverse the
error chain. However, `DescribeConfigError` and `AlterConfigError` have
the same structure (`Err KError` + `ErrMsg`) but were missing
`Unwrap()`, making `errors.Is`/`errors.As` unable to match the
underlying `KError`.

This adds the missing `Unwrap()` methods for consistency, allowing
callers to use `errors.Is(err, sarama.ErrInvalidConfig)` instead of
type-asserting to extract the error code.

## Changes
- Add `Unwrap() error` to `*DescribeConfigError`
(`describe_configs_response.go`)
- Add `Unwrap() error` to `*AlterConfigError`
(`alter_configs_response.go`)
- Add tests for both, following the existing `TestTopicError` pattern

## Test plan
- [x] `go test -run
'TestDescribeConfigError|TestAlterConfigError|TestTopicError'` — all
pass
- [x] `go vet ./...` — clean

Signed-off-by: Lingnan Liu <xmxu00@gmail.com>

* fix conflicts

* fix conflicts

* chore: refactor to use modern atomic types (IBM#3277)

---------

Signed-off-by: Leon Hwang <leon.hwang@linux.dev>
Signed-off-by: Dominic Evans <dominic.evans@uk.ibm.com>
Signed-off-by: Lingnan Liu <xmxu00@gmail.com>
Co-authored-by: Leon Hwang <leon.hwang@linux.dev>
Co-authored-by: Dominic Evans <dominic.evans@uk.ibm.com>
Co-authored-by: Lingnan Liu <xmxu00@gmail.com>
Co-authored-by: Sahil Sojitra <88416181+Sahil-4555@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

3 participants