-
Notifications
You must be signed in to change notification settings - Fork 3.6k
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
[improve][broker] Add the MessageExpirer interface to make code clear #20800
[improve][broker] Add the MessageExpirer interface to make code clear #20800
Conversation
### Motivation When I reviewed apache#20597, the unrelated changes in `PersistentTopicsBase` are hard to read. The logic could be simplified to: ```java PersistentSubscription sub = null; PersistentReplicator repl = null; if (metSomeCondition()) { repl = /* ... */; if (repl == null) { /* ... */ return; } } else { sub = /* ... */; if (repl == null) { /* ... */ return; } } final PersistentSubscription finalSub = sub; final PersistentReplicator finalRepl = repl; future.thenAccept(__ -> { if (metSomeCondition()) { repl.expireMessages(/* ... */); } else { sub.expireMessages(/* ... */); } }); ``` The code above is such a mess. It adds two final variables because the lambda can only capture final variables. The `metSomeCondition` check is performed unnecessarily twice. The original code is more hard to read because the logic in `/* ... */` takes a few lines so that the two calls of `metSomeCondition()` are not near. From the code search I see all these classes implement two `expireMessages` methods that accept an integer or a position. - PersistentMessageExpiryMonitor - PersistentSubscription - PersistentReplicator - NonPersistentSubscription The code can be simplified to introduce a new interface. ### Modifications Introduce a `MessageExpirer` interface and change the class hierarchy to: ``` // [I] is interface, [C] is class [I] MessageExpirer [I] Subscription [C] PersistentSubscription [C] NonPersistentSubscription [C] PersistentReplicator [C] PersistentMessageExpiryMonitor ``` The method invocation can be simplified much as shown in this patch. P.S. Inserting such an interface in the type hierarchy does not even break the ABI compatibility, see https://docs.oracle.com/javase/specs/jls/se7/html/jls-13.html
@BewareMyPower Please add the following content to your PR description and select a checkbox:
|
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
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
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 for your cleaning up.
Codecov Report
@@ Coverage Diff @@
## master #20800 +/- ##
============================================
+ Coverage 72.61% 73.13% +0.51%
+ Complexity 31966 3755 -28211
============================================
Files 1868 1868
Lines 139185 139170 -15
Branches 15315 15314 -1
============================================
+ Hits 101071 101776 +705
+ Misses 30052 29341 -711
+ Partials 8062 8053 -9
Flags with carried forward coverage won't be shown. Click here to find out more.
|
Motivation
When I reviewed #20597, the changes in
PersistentTopicsBase
are hard to read. The logic could be simplified to:The code above is bad. It adds two final variables because the lambda can only capture final variables. The
metSomeCondition
check is performed unnecessarily twice. The original code is more hard to read because the logic in/* ... */
takes a few lines so that the two calls ofmetSomeCondition()
are not near.From the code search I see all these classes implement two
expireMessages
methods that accept an integer or a position.The code can be simplified to introduce a new interface.
Modifications
Introduce a
MessageExpirer
interface and change the class hierarchy to:The method invocation can be simplified much as shown in this patch.
P.S. Inserting such an interface in the type hierarchy does not even break the ABI compatibility, see https://docs.oracle.com/javase/specs/jls/se7/html/jls-13.html
Does this pull request potentially affect one of the following parts:
If the box was checked, please highlight the changes
Documentation
doc
doc-required
doc-not-needed
doc-complete
Matching PR in forked repository
PR in forked repository: