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

   ### Motivation
   
   Calling these two method synchronously to set the retention and ttl of one 
topic will result into unexpected results.
   ```
   admin.topics().setRetention
   admin.topics().setMessageTTL
   ```
   For example, if we call 
   ```
   int retentionTimeInMinutes = 2880;
   int messageTTLInSecond = retentionTimeInMinutes * 60;
   admin.topics().setRetention(topic,new 
RetentionPolicies(retentionTimeInMinute, -1));
   admin.topics().setMessageTTL(topic, messageTTLInSecond);
   ```
   
   We expect that the retention is set to 2880 minutes, ttl is set to 
`2880*16=172800` seconds. But the result is that the retention policy is null !
   We read the messages from `__change_events` derectly and find out the reason:
   ```
   TimeStamp:1692157480789 ,Message received: 
PulsarEvent(eventType=TOPIC_POLICY, actionType=UPDATE, 
topicPoliciesEvent=TopicPoliciesEvent(, 
policies=TopicPolicies(backLogQuotaMap={}, subscriptionTypesEnabled=[], 
persistence=null, 
**retentionPolicies=RetentionPolicies{retentionTimeInMinutes=2880, 
retentionSizeInMB=-1},** deduplicationEnabled=null, 
**messageTTLInSeconds=null**, maxProducerPerTopic=null, 
maxConsumerPerTopic=null, maxConsumersPerSubscription=null, 
maxUnackedMessagesOnConsumer=null, maxUnackedMessagesOnSubscription=null, 
delayedDeliveryTickTimeMillis=null, delayedDeliveryEnabled=null, 
offloadPolicies=null, inactiveTopicPolicies=null, dispatchRate=null, 
subscriptionDispatchRate=null, compactionThreshold=null, publishRate=null, 
subscribeRate=null, deduplicationSnapshotIntervalSeconds=null, 
maxMessageSize=null, maxSubscriptionsPerTopic=null, 
replicatorDispatchRate=null)))
   
   TimeStamp:1692157480811 ,Message received: 
PulsarEvent(eventType=TOPIC_POLICY, actionType=UPDATE, 
topicPoliciesEvent=TopicPoliciesEvent(, 
policies=TopicPolicies(backLogQuotaMap={}, subscriptionTypesEnabled=[], 
persistence=null, **retentionPolicies=null**, deduplicationEnabled=null, 
**messageTTLInSeconds=172800**, maxProducerPerTopic=null, 
maxConsumerPerTopic=null, maxConsumersPerSubscription=null, 
maxUnackedMessagesOnConsumer=null, maxUnackedMessagesOnSubscription=null, 
delayedDeliveryTickTimeMillis=null, delayedDeliveryEnabled=null, 
offloadPolicies=null, inactiveTopicPolicies=null, dispatchRate=null, 
subscriptionDispatchRate=null, compactionThreshold=null, publishRate=null, 
subscribeRate=null, deduplicationSnapshotIntervalSeconds=null, 
maxMessageSize=null, maxSubscriptionsPerTopic=null, 
replicatorDispatchRate=null)))
   ```
   We can see that the retention policy in the second message is null, which 
result into the null retention policy. 
   
   Diving into the implementation of `SystemTopicBasedTopicPoliciesService`, we 
can see that 
`org.apache.pulsar.broker.service.SystemTopicBasedTopicPoliciesService#updateTopicPoliciesAsync`
 returns as soon as the message is written into `__change_events`, but do not 
guranteen that the topic policy in cache is updated. However, topic policy 
setting api will retrieve the topic policy from cache.
   ```
   org.apache.pulsar.broker.admin.v2.PersistentTopics#setRetention
   org.apache.pulsar.broker.admin.v2.PersistentTopics#setMessageTTL
   ```
   Before the cache is updated, the second rest api for setting ttl arrived, 
and change the topic policy based on the old one, which result into the problem.
   
   ### Modifications
   
   The solution is:  Ensure policy cache is updated before 
updateTopicPoliciesAsync returns.
   
   ### Verifying this change
   
   - [ ] Make sure that the change passes the CI checks.
   
   *(Please pick either of the following options)*
   
   This change added tests and can be verified as follows:
   
   
   ### Does this pull request potentially affect one of the following parts:
   
   <!-- DO NOT REMOVE THIS SECTION. CHECK THE PROPER BOX ONLY. -->
   
   *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
   
   <!-- 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 -->
   - [ ] `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 -->
   


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