-
Notifications
You must be signed in to change notification settings - Fork 134
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
[#575] refactor: replace switch-case with EnumMap in ComposedClientReadHandler #570
Conversation
Codecov Report
@@ Coverage Diff @@
## master #570 +/- ##
============================================
+ Coverage 60.88% 61.11% +0.23%
- Complexity 1796 1798 +2
============================================
Files 213 213
Lines 12375 12332 -43
Branches 1049 1042 -7
============================================
+ Hits 7534 7537 +3
+ Misses 4434 4386 -48
- Partials 407 409 +2
📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more |
storage/src/main/java/org/apache/uniffle/storage/handler/impl/ComposedClientReadHandler.java
Outdated
Show resolved
Hide resolved
} | ||
if (topLevelOfHandler > 1) { | ||
this.warmDataReadHandler = handlers[1]; | ||
numTiers = Math.min(Tier.values().length, handlers.length); |
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.
If handlers.length
> Tier.values().length
, should an exception be thrown?
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.
Done.
ClientReadHandler handler = handlerMap.computeIfAbsent(currentTier, | ||
key -> supplierMap.getOrDefault(key, () -> null).get()); | ||
if (handler == null) { | ||
return null; |
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.
Should an exception be thrown here?
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.
The current logic is equivalent to the old logic.
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.
Yes, but it is unreasonable. It means there is a bug if handler=null
. It may increase the difficulty of discover problems.
storage/src/main/java/org/apache/uniffle/storage/handler/impl/ComposedClientReadHandler.java
Outdated
Show resolved
Hide resolved
assert (readBlokNumInfo.contains("Client read 0 blocks from [" + ssi + "]") | ||
&& readBlokNumInfo.contains("Skipped[ hot:3 warm:3 cold:2 frozen:0 ]") | ||
&& readBlokNumInfo.contains("Consumed[ hot:0 warm:0 cold:0 frozen:0 ]")); | ||
String readBlockNumInfo = composedClientReadHandler.getReadBlockNumInfo(); |
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.
👍
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.
Thanks @kaijchen ... I thought about to refactor this.
It's much better now.
private final Map<Tier, Supplier<ClientReadHandler>> supplierMap = new EnumMap<>(Tier.class); | ||
private final Map<Tier, ClientReadHandler> handlerMap = new EnumMap<>(Tier.class); | ||
private final Map<Tier, ClientReadHandlerMetric> metricsMap = new EnumMap<>(Tier.class); | ||
private Tier currentTier = Tier.values()[0]; |
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.
this seems a bit strange. Could we just use Hot
here?
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.
Just in case of we add some new Super Hot
tier before Hot
.
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.
I have added a comment here.
storage/src/main/java/org/apache/uniffle/storage/handler/impl/ComposedClientReadHandler.java
Outdated
Show resolved
Hide resolved
if (frozenDataReadHandler != null) { | ||
frozenDataReadHandler.close(); | ||
} | ||
handlerMap.values().stream().filter(Objects::nonNull).forEach(ClientReadHandler::close); |
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.
Is filter(Objects::nonNull)
necessary?
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.
Not required, but just in case.
And to be consistent with the old logic.
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.
I think we can remove it here, because there is an extra loop. WDYT?
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.
There are at most 4 elements in the map ...
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.
The key is whether it is necessary to keep it. It's OK for me too to keep it.
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.
It's just for safety.
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.
Actually, the values of EnumMap
will not be null. If we put null
to EnumMap
, it will be modified to an instance of Object
.
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.
OK, let me check.
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.
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.
My mistake. When we get value from EnumMap
, it will be modified to null again.
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.
LGTM
Thanks @xianjingfeng and @advancedxy for the review. |
What changes were proposed in this pull request?
Add enum Tier, use EnumMap to replace switch-case in ComposedClientReadHandler.
Why are the changes needed?
Make the code more concise.
Does this PR introduce any user-facing change?
No.
How was this patch tested?
Existing CI.