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

[improve][broker] Add the MessageExpirer interface to make code clear #20800

Merged
merged 2 commits into from
Jul 23, 2023

Conversation

BewareMyPower
Copy link
Contributor

@BewareMyPower BewareMyPower commented Jul 13, 2023

Motivation

When I reviewed #20597, the changes in PersistentTopicsBase are hard to read. The logic could be simplified to:

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

Does this pull request potentially affect one of the following parts:

If the box was checked, please highlight the changes

  • Dependencies (add or upgrade a dependency)
  • The public API
  • The schema
  • The default values of configurations
  • The threading model
  • The binary protocol
  • The REST endpoints
  • The admin CLI options
  • The metrics
  • Anything that affects deployment

Documentation

  • doc
  • doc-required
  • doc-not-needed
  • doc-complete

Matching PR in forked repository

PR in forked repository:

### 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
@github-actions
Copy link

@BewareMyPower Please add the following content to your PR description and select a checkbox:

- [ ] `doc` <!-- Your PR contains doc changes -->
- [ ] `doc-required` <!-- Your PR changes impact docs and you will update later -->
- [ ] `doc-not-needed` <!-- Your PR changes do not impact docs -->
- [ ] `doc-complete` <!-- Docs have been already added -->

@github-actions github-actions bot added doc-not-needed Your PR changes do not impact docs and removed doc-label-missing labels Jul 13, 2023
@lifepuzzlefun

This comment was marked as resolved.

@BewareMyPower

This comment was marked as off-topic.

@lifepuzzlefun

This comment was marked as off-topic.

Copy link
Contributor

@Technoboy- Technoboy- left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link
Member

@tisonkun tisonkun left a 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-commenter
Copy link

Codecov Report

Merging #20800 (e75d9d6) into master (9b6a123) will increase coverage by 0.51%.
The diff coverage is 25.00%.

Impacted file tree graph

@@             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     
Flag Coverage Δ
inttests 24.15% <0.00%> (?)
systests 25.07% <0.00%> (-0.10%) ⬇️
unittests 72.42% <25.00%> (+0.44%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
...org/apache/pulsar/broker/service/Subscription.java 69.23% <ø> (ø)
...ice/persistent/PersistentMessageExpiryMonitor.java 68.04% <ø> (-1.04%) ⬇️
...roker/service/persistent/PersistentReplicator.java 70.23% <ø> (+4.68%) ⬆️
...pulsar/broker/admin/impl/PersistentTopicsBase.java 64.08% <25.00%> (+0.32%) ⬆️

... and 124 files with indirect coverage changes

@tisonkun tisonkun merged commit 4ccb5bb into apache:master Jul 23, 2023
@BewareMyPower BewareMyPower deleted the bewaremypower/message-expirer branch July 24, 2023 00:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/broker doc-not-needed Your PR changes do not impact docs ready-to-test
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants