-
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
Fix messages in TopicPolicies will never be cleaned up #11928
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,6 +33,7 @@ | |
import org.apache.pulsar.broker.systopic.NamespaceEventsSystemTopicFactory; | ||
import org.apache.pulsar.broker.systopic.SystemTopicClient; | ||
import org.apache.pulsar.client.api.Message; | ||
import org.apache.pulsar.client.api.MessageId; | ||
import org.apache.pulsar.client.api.PulsarClientException; | ||
import org.apache.pulsar.common.events.ActionType; | ||
import org.apache.pulsar.common.events.EventType; | ||
|
@@ -95,19 +96,10 @@ private CompletableFuture<Void> sendTopicPolicyEvent(TopicName topicName, Action | |
if (ex != null) { | ||
result.completeExceptionally(ex); | ||
} else { | ||
writer.writeAsync( | ||
PulsarEvent.builder() | ||
.actionType(actionType) | ||
.eventType(EventType.TOPIC_POLICY) | ||
.topicPoliciesEvent( | ||
TopicPoliciesEvent.builder() | ||
.domain(topicName.getDomain().toString()) | ||
.tenant(topicName.getTenant()) | ||
.namespace(topicName.getNamespaceObject().getLocalName()) | ||
.topic(TopicName.get(topicName.getPartitionedTopicName()).getLocalName()) | ||
.policies(policies) | ||
.build()) | ||
.build()).whenComplete(((messageId, e) -> { | ||
PulsarEvent event = getPulsarEvent(topicName, actionType, policies); | ||
CompletableFuture<MessageId> actionFuture = | ||
ActionType.DELETE.equals(actionType) ? writer.deleteAsync(event) : writer.writeAsync(event); | ||
actionFuture.whenComplete(((messageId, e) -> { | ||
if (e != null) { | ||
result.completeExceptionally(e); | ||
} else { | ||
|
@@ -133,6 +125,21 @@ private CompletableFuture<Void> sendTopicPolicyEvent(TopicName topicName, Action | |
return result; | ||
} | ||
|
||
private PulsarEvent getPulsarEvent(TopicName topicName, ActionType actionType, TopicPolicies policies) { | ||
return PulsarEvent.builder() | ||
.actionType(actionType) | ||
.eventType(EventType.TOPIC_POLICY) | ||
.topicPoliciesEvent( | ||
TopicPoliciesEvent.builder() | ||
.domain(topicName.getDomain().toString()) | ||
.tenant(topicName.getTenant()) | ||
.namespace(topicName.getNamespaceObject().getLocalName()) | ||
.topic(TopicName.get(topicName.getPartitionedTopicName()).getLocalName()) | ||
.policies(policies) | ||
.build()) | ||
.build(); | ||
} | ||
|
||
private void notifyListener(Message<PulsarEvent> msg) { | ||
if (!EventType.TOPIC_POLICY.equals(msg.getValue().getEventType())) { | ||
return; | ||
|
@@ -316,6 +323,11 @@ private void readMorePolicies(SystemTopicClient.Reader<PulsarEvent> reader) { | |
} | ||
|
||
private void refreshTopicPoliciesCache(Message<PulsarEvent> msg) { | ||
// delete policies | ||
if (msg.getValue() == null) { | ||
policiesCache.remove(TopicName.get(TopicName.get(msg.getKey()).getPartitionedTopicName())); | ||
return; | ||
} | ||
if (EventType.TOPIC_POLICY.equals(msg.getValue().getEventType())) { | ||
TopicPoliciesEvent event = msg.getValue().getTopicPoliciesEvent(); | ||
TopicName topicName = | ||
|
@@ -331,7 +343,14 @@ private void refreshTopicPoliciesCache(Message<PulsarEvent> msg) { | |
policiesCache.put(topicName, event.getPolicies()); | ||
break; | ||
case DELETE: | ||
// Since PR #11928, this branch is no longer needed. | ||
// However, due to compatibility, it is temporarily retained here | ||
// and can be deleted in the future. | ||
policiesCache.remove(topicName); | ||
SystemTopicClient<PulsarEvent> systemTopicClient = namespaceEventsSystemTopicFactory | ||
.createTopicPoliciesSystemTopicClient(topicName.getNamespaceObject()); | ||
systemTopicClient.newWriterAsync().thenAccept(writer | ||
-> writer.deleteAsync(getPulsarEvent(topicName, ActionType.DELETE, null))); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The writer should be closed and for replay the historical policies, we should use one writer instead create writer for each policy data? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have closed the Writer. |
||
break; | ||
case NONE: | ||
break; | ||
|
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.
what about old data present in existing clusters ?
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.
Good catch, maybe we could call the delete method to publish a message with a null value in branch
DELETE
?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.
Indeed, for the compatibility, this code cannot be deleted now, I will add a comment here.
But Topic's existing old data seems to need to provide additional tools to delete it.
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.
When recovering the topic policies, the reader will read messages from the earliest position, it could read the
DELETE
type Pulsar event, does it handle theDELETE
event messages at this time?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.
I added a compatibility adaptation