Skip to content

fix: update map rather than create a new map#3302

Merged
hindessm merged 4 commits intoIBM:mainfrom
hindessm:known-map-sizes
Sep 23, 2025
Merged

fix: update map rather than create a new map#3302
hindessm merged 4 commits intoIBM:mainfrom
hindessm:known-map-sizes

Conversation

@hindessm
Copy link
Copy Markdown
Collaborator

@hindessm hindessm commented Sep 22, 2025

This PR was originally just minor refactoring to improve map allocations. But a bug was found while responding to the excellent PR feedback from Cassondra.

Cassondra rightly points out that the explicit capacities are hints so don't always reduce (re)allocations. In fact, particularly when the capacities are small, they make no difference, but it is still good practice to be explicit when we know the size.

Copy link
Copy Markdown
Collaborator

@puellanivis puellanivis left a comment

Choose a reason for hiding this comment

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

N.B. that adding a capacity hint to make(map[K]V) is merely a hint, and could be ignored by the compiler/runtime…

Also, it’s still better that we provide it when we have it.

PS: most of my comments are merely further improvements, rather than code defects

admin.go Outdated
for broker, partitions := range partitionPerBroker {
topics := make(map[string]*DeleteRecordsRequestTopic)
recordsToDelete := make(map[int32]int64)
topics := make(map[string]*DeleteRecordsRequestTopic, 1)
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.

Move this down to line 623, and convert it and lines 623–625 to a complex literal.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Done.

admin.go Outdated
Comment on lines 1035 to 1036
groups := make(map[string]string, len(response.Groups))
maps.Copy(groups, response.Groups)
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.

Suggested change
groups := make(map[string]string, len(response.Groups))
maps.Copy(groups, response.Groups)
groups := maps.Clone(response.Groups)

Copy link
Copy Markdown
Collaborator Author

@hindessm hindessm Sep 22, 2025

Choose a reason for hiding this comment

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

Rather than just do this one, I decided to make an additional commit with all of the uncontentious changes from your earlier comments on #3297.

One of the changes, which looks logically correct to me, causes a linter error so I may live to regret making the wider changes.

currentAssignment = deepCopyAssignment(preBalanceAssignment)
currentPartitionConsumer = make(map[topicPartitionAssignment]string, len(preBalancePartitionConsumers))
maps.Copy(currentPartitionConsumer, preBalancePartitionConsumers)
currentPartitionConsumer = maps.Clone(preBalancePartitionConsumers)
Copy link
Copy Markdown
Collaborator

@puellanivis puellanivis Sep 22, 2025

Choose a reason for hiding this comment

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

Huh. This was even called out when it was implemented: https://github.com/IBM/sarama/pull/1416/files#r314581692

Turns out, the code it’s copying used currentPartitionConsumer.clear() which deletes all the entries from the underlying map referenced, rather than assigning a whole brand new map into it. In this way, the previous references to the map would reflect these updates, even when they’re not directly used later in this function. However, this code just constructs a whole new map, rather than clearing out the existing map, so that other references to the same map do not reflect the changes made… different maps.

The whole StickyAssignor.java file is completely different now… but updating to copy that code is obviously out of scope.

I would instead recommend here:

clear(currentPartitionConsumer)
maps.Copy(currentPartitionConsumer, preBalancePartitionConsumers)

This should be the intended original code, rather than this subtly bugged(?) code.

hindessm and others added 3 commits September 22, 2025 16:59
Co-authored-by: Cassondra Foesch <[email protected]>
Signed-off-by: Mark Hindess <[email protected]>
These are the uncontentious changes mentioned after the merge
of the earlier modernize PR. See:

  IBM#3297 (review)

Co-authored-by: Cassondra Foesch <[email protected]>
Signed-off-by: beanz <[email protected]>
Thanks to Cassondra for figuring out how this was intended to work.

Co-authored-by: Cassondra Foesch <[email protected]>
Signed-off-by: beanz <[email protected]>
@hindessm hindessm changed the title chore: allocate correct size maps when known bug: update map rather than create a new map Sep 22, 2025
@hindessm hindessm changed the title bug: update map rather than create a new map fix: update map rather than create a new map Sep 22, 2025
@puellanivis
Copy link
Copy Markdown
Collaborator

I can find nothing further to comment about.

Copy link
Copy Markdown
Collaborator

@dnwe dnwe left a comment

Choose a reason for hiding this comment

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

LGTM

@hindessm hindessm merged commit de074e2 into IBM:main Sep 23, 2025
16 checks passed
@hindessm hindessm deleted the known-map-sizes branch September 23, 2025 11:55
@hindessm
Copy link
Copy Markdown
Collaborator Author

FYI: Somewhat related to this, I added a comment about the use of maps in the API to https://github.com/IBM/sarama/wiki/Ideas-that-will-break-backwards-compatibility.

@puellanivis
Copy link
Copy Markdown
Collaborator

FYI: Somewhat related to this, I added a comment about the use of maps in the API to https://github.com/IBM/sarama/wiki/Ideas-that-will-break-backwards-compatibility.

I don’t seem to see any way to add any discussion of things, though? Like:

  • logrus is now in maintenance mode. It is advised that new code not use it; besides, we have log/slog now.

(Saving further comments for a better place to discuss.)

@dnwe dnwe added the fix label Oct 3, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants