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

KAFKA-9668: Iterating over KafkaStreams.getAllMetadata() results in ConcurrentModificationException #8233

Merged

Conversation

big-andy-coates
Copy link
Contributor

Fixes KAFKA-9668

KafkaStreams.getAllMetadata() returns StreamsMetadataState.getAllMetadata(). All the latter methods is synchronized it returns a reference to internal mutable state. Not only does this break encapsulation, but it means any thread iterating over the returned collection when the metadata gets rebuilt will encounter a ConcurrentModificationException.

This change:

  • switches from clearing and rebuild allMetadata when onChange is called to building a new list and swapping this in. This is thread safe and has the benefit that the returned list is not empty during a rebuild: you either get the old or the new list.
  • removes synchronisation from getAllMetadata and getLocalMetadata. These are returning member variables. Synchronisation adds nothing.
  • changes getAllMetadata to wrap its return value in an unmodifiable wrapper to avoid breaking encapsulation.
  • changes the getters in StreamsMetadata to wrap their return values in unmodifiable wrapper to avoid breaking encapsulation.

Unit tests have been added to cover both changes classes to ensure encapsulation and thread-safety are maintained.

Committer Checklist (excluded from commit message)

  • Verify design and implementation
  • Verify test coverage and CI build status
  • Verify documentation (including upgrade notes)

…oncurrentModificationException

`KafkaStreams.getAllMetadata()` returns `StreamsMetadataState.getAllMetadata()`. All the latter methods is `synchronized` it returns a reference to internal mutable state.  Not only does this break encapsulation, but it means any thread iterating over the returned collection when the metadata gets rebuilt will encounter a `ConcurrentModificationException`.

This change:
 * switches from clearing and rebuild `allMetadata` when `onChange` is called to building a new list and swapping this in. This is thread safe and has the benefit that the returned list is not empty during a rebuild: you either get the old or the new list.
 * removes synchronisation from `getAllMetadata` and `getLocalMetadata`. These are returning member variables. Synchronisation adds nothing.
 * changes `getAllMetadata` to wrap its return value in an unmodifiable wrapper to avoid breaking encapsulation.
 * changes the getters in `StreamsMetadata` to wrap their return values in unmodifiable wrapper to avoid breaking encapsulation.
@big-andy-coates
Copy link
Contributor Author

This has obviously been a long standing bug. So no specific urgency to get it in. It is causing build failures in our build, but I'm going to try and work around it.

Copy link
Contributor

@guozhangwang guozhangwang left a comment

Choose a reason for hiding this comment

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

Good catch! Just a minor question otherwise LGTM.

if (activePartitionHostMap.isEmpty() && standbyPartitionHostMap.isEmpty()) {
allMetadata = Collections.emptyList();
Copy link
Contributor

Choose a reason for hiding this comment

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

Why move the reset into the condition? if the passed in values are empty we should still set it to empty because the previous map may be out-dated right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Well, with this change you're always going to get either the old or new metadata from getAllMetadata. Where as if I clear the list at the start of this method, then getAllMetadata can return empty.

Question is: which is preferable? I figured there's a strong possibility that at least some of the old metadata may still be correct - so why ditch it?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

By returning empty we're forcing callers to handle this case. But if we return stale data, then it's no different to if they had called getAllMetadata a few ns earlier.

@guozhangwang
Copy link
Contributor

test this please

@big-andy-coates
Copy link
Contributor Author

test this please

@big-andy-coates
Copy link
Contributor Author

retest this please

1 similar comment
@bbejeck
Copy link
Member

bbejeck commented Mar 6, 2020

retest this please

@guozhangwang
Copy link
Contributor

LGTM. Merging to trunk and 2.5 now.

@guozhangwang guozhangwang merged commit ca90a84 into apache:trunk Mar 6, 2020
guozhangwang pushed a commit that referenced this pull request Mar 6, 2020
…oncurrentModificationException (#8233)

`KafkaStreams.getAllMetadata()` returns `StreamsMetadataState.getAllMetadata()`. All the latter methods is `synchronized` it returns a reference to internal mutable state.  Not only does this break encapsulation, but it means any thread iterating over the returned collection when the metadata gets rebuilt will encounter a `ConcurrentModificationException`.

This change:
 * switches from clearing and rebuild `allMetadata` when `onChange` is called to building a new list and swapping this in. This is thread safe and has the benefit that the returned list is not empty during a rebuild: you either get the old or the new list.
 * removes synchronisation from `getAllMetadata` and `getLocalMetadata`. These are returning member variables. Synchronisation adds nothing.
 * changes `getAllMetadata` to wrap its return value in an unmodifiable wrapper to avoid breaking encapsulation.
 * changes the getters in `StreamsMetadata` to wrap their return values in unmodifiable wrapper to avoid breaking encapsulation.

Co-authored-by: Andy Coates <[email protected]>

Reviewers: Guozhang Wang <[email protected]>
@big-andy-coates big-andy-coates deleted the KAFKA-9668-ConcurrentMod branch March 9, 2020 12:57
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.

4 participants