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

[#575] refactor: replace switch-case with EnumMap in ComposedClientReadHandler #570

Merged
merged 12 commits into from
Feb 10, 2023

Conversation

kaijchen
Copy link
Contributor

@kaijchen kaijchen commented Feb 9, 2023

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.

@kaijchen kaijchen changed the title refactor: replace switch-case with enum and EnumMap in ComposedClientReadHandler refactor: replace switch-case with EnumMap in ComposedClientReadHandler Feb 9, 2023
@codecov-commenter
Copy link

codecov-commenter commented Feb 9, 2023

Codecov Report

Merging #570 (3b725d5) into master (d0f57ef) will increase coverage by 0.23%.
The diff coverage is 0.00%.

@@             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     
Impacted Files Coverage Δ
...uniffle/storage/factory/ShuffleHandlerFactory.java 0.00% <0.00%> (ø)
...torage/handler/impl/ComposedClientReadHandler.java 0.00% <0.00%> (ø)
...he/uniffle/server/storage/LocalStorageManager.java 85.87% <0.00%> (-1.13%) ⬇️
...he/uniffle/server/buffer/ShuffleBufferManager.java 83.09% <0.00%> (-0.89%) ⬇️
...rg/apache/uniffle/server/ShuffleServerMetrics.java 97.34% <0.00%> (+0.02%) ⬆️
...rg/apache/uniffle/storage/common/LocalStorage.java 49.05% <0.00%> (+0.69%) ⬆️

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

@kaijchen kaijchen marked this pull request as draft February 9, 2023 08:31
}
if (topLevelOfHandler > 1) {
this.warmDataReadHandler = handlers[1];
numTiers = Math.min(Tier.values().length, handlers.length);
Copy link
Member

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?

Copy link
Contributor Author

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;
Copy link
Member

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?

Copy link
Contributor Author

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.

Copy link
Member

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.

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();
Copy link
Contributor

Choose a reason for hiding this comment

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

👍

Copy link
Contributor

@advancedxy advancedxy left a 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];
Copy link
Contributor

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?

Copy link
Contributor Author

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.

Copy link
Contributor Author

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.

@kaijchen kaijchen changed the title refactor: replace switch-case with EnumMap in ComposedClientReadHandler [#575] refactor: replace switch-case with EnumMap in ComposedClientReadHandler Feb 9, 2023
if (frozenDataReadHandler != null) {
frozenDataReadHandler.close();
}
handlerMap.values().stream().filter(Objects::nonNull).forEach(ClientReadHandler::close);
Copy link
Member

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?

Copy link
Contributor Author

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.

Copy link
Member

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?

Copy link
Contributor Author

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 ...

Copy link
Member

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.

Copy link
Contributor Author

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.

Copy link
Member

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

OK, let me check.

Copy link
Contributor Author

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.

No, it could be null. The following test passed.

test

Copy link
Member

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.

@xianjingfeng xianjingfeng linked an issue Feb 10, 2023 that may be closed by this pull request
3 tasks
Copy link
Member

@xianjingfeng xianjingfeng left a comment

Choose a reason for hiding this comment

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

LGTM

@xianjingfeng xianjingfeng merged commit 9da9bb6 into apache:master Feb 10, 2023
@kaijchen
Copy link
Contributor Author

Thanks @xianjingfeng and @advancedxy for the review.

@kaijchen kaijchen deleted the tier branch February 10, 2023 03:51
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.

[Improvement] Refactor ComposedClientReadHandler
4 participants