Skip to content

Not use ConcurrentHashMap in CoordinatorRuleManager.rules#9302

Merged
jihoonson merged 1 commit intoapache:masterfrom
zhenxiao:map
Feb 5, 2020
Merged

Not use ConcurrentHashMap in CoordinatorRuleManager.rules#9302
jihoonson merged 1 commit intoapache:masterfrom
zhenxiao:map

Conversation

@zhenxiao
Copy link
Copy Markdown
Contributor

@zhenxiao zhenxiao commented Feb 2, 2020

@zhenxiao zhenxiao requested a review from leventov February 2, 2020 06:16
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Please use Collections.emptyMap().

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Please use Collections.emptyMap().

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

new HashMap() looks unnecessary.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

do you mean set rules directly, no need create newRules?

@zhenxiao zhenxiao changed the title Use HashMap for CoordinatorRuleManager.rules Not use for CoordinatorRuleManager.rules Feb 2, 2020
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think this should be an AtomicReference of an ImmutableMap otherwise we can not guarantee that no one updates the map after it is set here.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Good point. The value type (List<Rule>) should be immutable as well. I'm ok with using UnmodifiableMap and UnmodifiableList.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

+1 - I missed that.

Perhaps the best approach here is to keep the definition of the variable to AtomicReference<Map<String, List<Rule>>>

And add 2 unit tests to ensure
1 - we can not add to/ remove from the map
2 - we can not mutate the rules for an entry in a map.

This way if someone changes this behavior in the future we can catch it easily, and changing the type of Map/ List is easy as well.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Oh yeah, I didn't mean the value type of rules should be UnmodifiableList<Rule>.

@suneet-s do you regard your comment as a blocker for this PR? Those tests would be definitely nice to have, but since any tests haven't rewritten for this class, I'm fine with adding all necessary tests including the ones you mentioned in a follow-up PR at once. @zhenxiao you can do in this PR if you want though.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@jihoonson I think we need some way to verify that this change works and will not break in the future.

I understand it's not any worse than before, but I don't think we'll ever come back to add tests for this until we hit a bug - at which point it will be really hard to debug. It should be relatively quick to add those 2 tests so I think it's worth it. If there's a fast follow, that should be ok.

I'll leave it to you for final decision on whether this should be a blocker or not (I'm not sure what the apache rules are), but I think debugging this if someone changes the code to mutate the map would be very tough - I'd strongly recommend we add tests here to prevent that pain in the future.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@zhenxiao would you consider @suneet-s's comment above? I think it would be nice to have. If you want to add them in this PR, I think you may need to create a new class, CoordinatorRuleManagerTest, and expose the rules via a method annotated with @VisibleForTesting to verify that the map inside the AtomicRefence is immutable. Another option could be creating an issue for adding those tests and doing in a follow-up PR.

@zhenxiao @suneet-s What do you think?

Copy link
Copy Markdown
Contributor

@suneet-s suneet-s Feb 5, 2020

Choose a reason for hiding this comment

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

@jihoonson I'm not so familiar with this area of the code, so I'm happy to defer to your judgement. I like the tests you've described and think it's ok if tests come as a fast follow

@zhenxiao can you describe what manual testing you've done to verify this change? I'm sorry if this is coming across as nagging, I've just experienced a lot of pain trying to triage concurrency issues in the past.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@suneet-s what kind of manual testings you think need to be done? The change in this PR looks pretty straightforward and I don't think this PR would lead to any sort of concurrency issues at least for now.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

thanks for your comments @suneet-s @jihoonson
updated to unmodifiableMap
I will file an issue to add tests after this PR is merged

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I raised #9318.

@jihoonson
Copy link
Copy Markdown
Contributor

@zhenxiao would you please update the title to be more accurate? Perhaps "Use HashMap instead of ConcurrentHashMap in CoordinatorRuleManager".

@zhenxiao zhenxiao changed the title Not use for CoordinatorRuleManager.rules Not use ConcurrentHashMap in CoordinatorRuleManager.rules Feb 5, 2020
Copy link
Copy Markdown
Contributor

@suneet-s suneet-s left a comment

Choose a reason for hiding this comment

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

Thanks @zhenxiao

Copy link
Copy Markdown
Contributor

@jihoonson jihoonson left a comment

Choose a reason for hiding this comment

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

Thanks @zhenxiao!

@jihoonson jihoonson merged commit 98cefc6 into apache:master Feb 5, 2020
@jihoonson jihoonson added this to the 0.18.0 milestone Mar 26, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

CoordinatorRuleManager.rules doesn't need to store ConcurrentHashMap

4 participants