BewareMyPower opened a new pull request, #24780:
URL: https://github.com/apache/pulsar/pull/24780

   ### Motivation
   
   I'm working on optimizing the long topic loading path recently and I found 
these two methods just work the same. The ownership validation does not have a 
single source of truth, which makes code hard to read.
   
   1. `BrokerService#checkTopicNsOwnership`
   
   This method returns a **void** future that **fails** if the topic is not 
owned by the current broker. It's called by:
   - `TransactionMetadataStoreService#handleTcClientConnect`
   - `AbstractTopic#addProducer`
   - `BrokerService#createNonPersistentTopic`
   - `BrokerService#loadOrCreatePersistentTopic`
   - `BrokerService#createPendingLoadTopic`
   - `NonPersistentTopic#checkTopicNsOwnership`
   - `PersistentTopic#internalSubscribe`
   
   2. `NamespaceService#isServiceUnitActiveAsync`
   
   This method returns a **boolean** future **that returns false** if the topic 
is not owned by the current broker. It's only used in 
`BrokerService#checkOwnershipAndCreatePersistentTopic`.
   The implementations of these two methods are slightly different, which might 
also mislead users that they are different.
   
   Here is how `isServiceUnitActiveAsync` handles topics' bundle: 
https://github.com/apache/pulsar/blob/1b74fe07bd24e079434769de2c52222761d88ca1/pulsar-broker/src/main/java/org/apache/pulsar/broker/namespace/NamespaceService.java#L1249-L1253
   
   However, the logic is duplicated with
   
   
https://github.com/apache/pulsar/blob/1b74fe07bd24e079434769de2c52222761d88ca1/pulsar-broker/src/main/java/org/apache/pulsar/broker/namespace/OwnershipCache.java#L147-L153
   
   
https://github.com/apache/pulsar/blob/1b74fe07bd24e079434769de2c52222761d88ca1/pulsar-broker/src/main/java/org/apache/pulsar/broker/namespace/NamespaceService.java#L1280-L1286
   
   which is called by `checkTopicNsOwnership`: 
https://github.com/apache/pulsar/blob/1b74fe07bd24e079434769de2c52222761d88ca1/pulsar-broker/src/main/java/org/apache/pulsar/broker/service/BrokerService.java#L2376
   
   The only place that calls `isServiceUnitActiveAsync` is here: 
https://github.com/apache/pulsar/blob/1b74fe07bd24e079434769de2c52222761d88ca1/pulsar-broker/src/main/java/org/apache/pulsar/broker/service/BrokerService.java#L1739
   
   when it returns false, it just completes another future exceptionally: 
https://github.com/apache/pulsar/blob/1b74fe07bd24e079434769de2c52222761d88ca1/pulsar-broker/src/main/java/org/apache/pulsar/broker/service/BrokerService.java#L1761-L1764
   
   where the error message does not have bundle info included like 
`checkTopicNsOwnership`.
   
   ### Modifications
   
   Use `checkTopicNsOwnership` instead of `isServiceUnitActiveAsync` in 
`BrokerService#checkOwnershipAndCreatePersistentTopic`.
   
   Remove the `isServiceUnitActiveAsync` and `isServiceUnitActive` methods from 
`NamespaceService`. The same function can be implemented easily by composing 
existing methods like:
   
   ```java
       public static CompletableFuture<Boolean> 
isServiceUnitActiveAsync(NamespaceService namespaceService,
                                                                         
TopicName topicName) {
           return namespaceService.getBundleAsync(topicName).thenCompose(bundle 
->
                   namespaceService.checkBundleOwnership(topicName, bundle));
       }
   ```
   
   ### Verifying this change
   
   This change is a trivial rework / code cleanup without any test coverage.
   
   ### Documentation
   
   <!-- DO NOT REMOVE THIS SECTION. CHECK THE PROPER BOX ONLY. -->
   
   - [ ] `doc` <!-- Your PR contains doc changes. -->
   - [ ] `doc-required` <!-- Your PR changes impact docs and you will update 
later -->
   - [x] `doc-not-needed` <!-- Your PR changes do not impact docs -->
   - [ ] `doc-complete` <!-- Docs have been already added -->
   
   ### Matching PR in forked repository
   
   PR in forked repository: <!-- ENTER URL HERE -->
   
   <!--
   After opening this PR, the build in apache/pulsar will fail and instructions 
will
   be provided for opening a PR in the PR author's forked repository.
   
   apache/pulsar pull requests should be first tested in your own fork since 
the 
   apache/pulsar CI based on GitHub Actions has constrained resources and quota.
   GitHub Actions provides separate quota for pull requests that are executed 
in 
   a forked repository.
   
   The tests will be run in the forked repository until all PR review comments 
have
   been handled, the tests pass and the PR is approved by a reviewer.
   -->
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to