You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pulsar.apache.org by pe...@apache.org on 2020/10/22 03:08:17 UTC
[pulsar] 04/04: Support reset cursor to a batch index for Pulsar
Admin. (#8329)
This is an automated email from the ASF dual-hosted git repository.
penghui pushed a commit to branch branch-2.6
in repository https://gitbox.apache.org/repos/asf/pulsar.git
commit c6359fc7016bb40106ba544791b54f4a30133d5d
Author: lipenghui <pe...@apache.org>
AuthorDate: Thu Oct 22 09:51:47 2020 +0800
Support reset cursor to a batch index for Pulsar Admin. (#8329)
Need merge #8306 before
### Motivation
Support reset cursor to a batch index for Pulsar Admin.
### Verifying this change
Unit test added
(cherry picked from commit e31d18c00e4967779f84f55b489468ccc0b83a40)
---
.../broker/admin/impl/PersistentTopicsBase.java | 113 +++++++++++++++++----
.../pulsar/broker/admin/v1/PersistentTopics.java | 7 +-
.../pulsar/broker/admin/v2/PersistentTopics.java | 2 +-
.../broker/service/SubscriptionSeekTest.java | 83 +++++++++++++++
.../apache/pulsar/client/impl/ResetCursorData.java | 10 +-
5 files changed, 192 insertions(+), 23 deletions(-)
diff --git a/pulsar-broker/src/main/java/org/apache/pulsar/broker/admin/impl/PersistentTopicsBase.java b/pulsar-broker/src/main/java/org/apache/pulsar/broker/admin/impl/PersistentTopicsBase.java
index fea4622..c50b92f 100644
--- a/pulsar-broker/src/main/java/org/apache/pulsar/broker/admin/impl/PersistentTopicsBase.java
+++ b/pulsar-broker/src/main/java/org/apache/pulsar/broker/admin/impl/PersistentTopicsBase.java
@@ -115,6 +115,7 @@ import org.apache.pulsar.common.policies.data.SubscriptionStats;
import org.apache.pulsar.common.policies.data.TopicStats;
import org.apache.pulsar.common.util.DateFormatter;
import org.apache.pulsar.common.util.FutureUtil;
+import org.apache.pulsar.common.util.collections.BitSetRecyclable;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.data.Stat;
import org.slf4j.Logger;
@@ -1820,7 +1821,7 @@ public class PersistentTopicsBase extends AdminResource {
}
protected void internalResetCursorOnPosition(AsyncResponse asyncResponse, String subName, boolean authoritative,
- MessageIdImpl messageId, boolean isExcluded) {
+ MessageIdImpl messageId, boolean isExcluded, int batchIndex) {
if (topicName.isGlobal()) {
validateGlobalNamespaceOwnership(namespaceName);
}
@@ -1834,6 +1835,8 @@ public class PersistentTopicsBase extends AdminResource {
"Reset-cursor at position is not allowed for partitioned-topic");
} else {
validateAdminAccessForSubscriber(subName, authoritative);
+ // will redirect if the topic not owned by current broker
+ validateReadOperationOnTopic(authoritative);
PersistentTopic topic = (PersistentTopic) getTopicReference(topicName);
if (topic == null) {
throw new RestException(Status.NOT_FOUND, "Topic not found");
@@ -1841,25 +1844,99 @@ public class PersistentTopicsBase extends AdminResource {
try {
PersistentSubscription sub = topic.getSubscription(subName);
Preconditions.checkNotNull(sub);
- PositionImpl position = PositionImpl.get(messageId.getLedgerId(), messageId.getEntryId());
- position = isExcluded ? position.getNext() : position;
- sub.resetCursor(position).thenRun(() -> {
- log.info("[{}][{}] successfully reset cursor on subscription {} to position {}", clientAppId(),
- topicName, subName, messageId);
- asyncResponse.resume(Response.noContent().build());
- }).exceptionally(ex -> {
- Throwable t = (ex instanceof CompletionException ? ex.getCause() : ex);
- log.warn("[{}][{}] Failed to reset cursor on subscription {} to position {}", clientAppId(),
- topicName, subName, messageId, t);
- if (t instanceof SubscriptionInvalidCursorPosition) {
- asyncResponse.resume(new RestException(Status.PRECONDITION_FAILED,
- "Unable to find position for position specified: " + t.getMessage()));
- } else if (t instanceof SubscriptionBusyException) {
- asyncResponse.resume(new RestException(Status.PRECONDITION_FAILED,
- "Failed for Subscription Busy: " + t.getMessage()));
+ CompletableFuture<Integer> batchSizeFuture = new CompletableFuture<>();
+ if (batchIndex >= 0) {
+ try {
+ ManagedLedgerImpl ledger = (ManagedLedgerImpl) topic.getManagedLedger();
+ ledger.asyncReadEntry(new PositionImpl(messageId.getLedgerId(), messageId.getEntryId()), new AsyncCallbacks.ReadEntryCallback() {
+ @Override
+ public void readEntryFailed(ManagedLedgerException exception, Object ctx) {
+ // Since we can't read the message from the storage layer, it might be an already delete message ID or an invalid message ID
+ // We should fall back to non batch index seek.
+ batchSizeFuture.complete(0);
+ }
+
+ @Override
+ public void readEntryComplete(Entry entry, Object ctx) {
+ try {
+ try {
+ if (entry == null) {
+ batchSizeFuture.complete(0);
+ } else {
+ MessageMetadata metadata = Commands.parseMessageMetadata(entry.getDataBuffer());
+ batchSizeFuture.complete(metadata.getNumMessagesInBatch());
+ }
+ } catch (Exception e) {
+ batchSizeFuture.completeExceptionally(new RestException(e));
+ }
+ } finally {
+ if (entry != null) {
+ entry.release();
+ }
+ }
+ }
+ }, null);
+ } catch (NullPointerException npe) {
+ batchSizeFuture.completeExceptionally(new RestException(Status.NOT_FOUND, "Message not found"));
+ } catch (Exception exception) {
+ log.error("[{}] Failed to get message with ledgerId {} entryId {} from {}",
+ clientAppId(), messageId.getLedgerId(), messageId.getEntryId(), topicName, exception);
+ batchSizeFuture.completeExceptionally(new RestException(exception));
+ }
+ } else {
+ batchSizeFuture.complete(0);
+ }
+ batchSizeFuture.thenAccept(bi -> {
+ PositionImpl seekPosition;
+ if (bi > 0) {
+ long[] ackSet;
+ BitSetRecyclable bitSet = BitSetRecyclable.create();
+ bitSet.set(0, bi);
+ if (isExcluded) {
+ bitSet.clear(0, Math.max(batchIndex + 1, 0));
+ if (bitSet.length() > 0) {
+ ackSet = bitSet.toLongArray();
+ seekPosition = PositionImpl.get(messageId.getLedgerId(), messageId.getEntryId(), ackSet);
+ } else {
+ seekPosition = PositionImpl.get(messageId.getLedgerId(), messageId.getEntryId());
+ seekPosition = seekPosition.getNext();
+ }
+ } else {
+ if (batchIndex - 1 >= 0) {
+ bitSet.clear(0, batchIndex);
+ ackSet = bitSet.toLongArray();
+ seekPosition = PositionImpl.get(messageId.getLedgerId(), messageId.getEntryId(), ackSet);
+ } else {
+ seekPosition = PositionImpl.get(messageId.getLedgerId(), messageId.getEntryId());
+ }
+ }
+ bitSet.recycle();
} else {
- resumeAsyncResponseExceptionally(asyncResponse, t);
+ seekPosition = PositionImpl.get(messageId.getLedgerId(), messageId.getEntryId());
+ seekPosition = isExcluded ? seekPosition.getNext() : seekPosition;
}
+
+ sub.resetCursor(seekPosition).thenRun(() -> {
+ log.info("[{}][{}] successfully reset cursor on subscription {} to position {}", clientAppId(),
+ topicName, subName, messageId);
+ asyncResponse.resume(Response.noContent().build());
+ }).exceptionally(ex -> {
+ Throwable t = (ex instanceof CompletionException ? ex.getCause() : ex);
+ log.warn("[{}][{}] Failed to reset cursor on subscription {} to position {}", clientAppId(),
+ topicName, subName, messageId, t);
+ if (t instanceof SubscriptionInvalidCursorPosition) {
+ asyncResponse.resume(new RestException(Status.PRECONDITION_FAILED,
+ "Unable to find position for position specified: " + t.getMessage()));
+ } else if (t instanceof SubscriptionBusyException) {
+ asyncResponse.resume(new RestException(Status.PRECONDITION_FAILED,
+ "Failed for Subscription Busy: " + t.getMessage()));
+ } else {
+ resumeAsyncResponseExceptionally(asyncResponse, t);
+ }
+ return null;
+ });
+ }).exceptionally(e -> {
+ asyncResponse.resume(e);
return null;
});
} catch (Exception e) {
diff --git a/pulsar-broker/src/main/java/org/apache/pulsar/broker/admin/v1/PersistentTopics.java b/pulsar-broker/src/main/java/org/apache/pulsar/broker/admin/v1/PersistentTopics.java
index ad4e2f3..c9adbf4 100644
--- a/pulsar-broker/src/main/java/org/apache/pulsar/broker/admin/v1/PersistentTopics.java
+++ b/pulsar-broker/src/main/java/org/apache/pulsar/broker/admin/v1/PersistentTopics.java
@@ -44,6 +44,7 @@ import org.apache.pulsar.client.admin.LongRunningProcessStatus;
import org.apache.pulsar.client.admin.OffloadProcessStatus;
import org.apache.pulsar.client.api.MessageId;
import org.apache.pulsar.client.impl.MessageIdImpl;
+import org.apache.pulsar.client.impl.ResetCursorData;
import org.apache.pulsar.common.partition.PartitionedTopicMetadata;
import org.apache.pulsar.common.policies.data.AuthAction;
import org.apache.pulsar.common.policies.data.PersistentOfflineTopicStats;
@@ -508,10 +509,12 @@ public class PersistentTopics extends PersistentTopicsBase {
@PathParam("property") String property, @PathParam("cluster") String cluster,
@PathParam("namespace") String namespace, @PathParam("topic") @Encoded String encodedTopic,
@PathParam("subName") String encodedSubName,
- @QueryParam("authoritative") @DefaultValue("false") boolean authoritative, MessageIdImpl messageId) {
+ @QueryParam("authoritative") @DefaultValue("false") boolean authoritative, ResetCursorData resetCursorData) {
try {
validateTopicName(property, cluster, namespace, encodedTopic);
- internalResetCursorOnPosition(asyncResponse, decode(encodedSubName), authoritative, messageId, false);
+ internalResetCursorOnPosition(asyncResponse, decode(encodedSubName), authoritative,
+ new MessageIdImpl(resetCursorData.getLedgerId(), resetCursorData.getEntryId(), resetCursorData.getPartitionIndex())
+ , resetCursorData.isExcluded(), resetCursorData.getBatchIndex());
} catch (Exception e) {
resumeAsyncResponseExceptionally(asyncResponse, e);
}
diff --git a/pulsar-broker/src/main/java/org/apache/pulsar/broker/admin/v2/PersistentTopics.java b/pulsar-broker/src/main/java/org/apache/pulsar/broker/admin/v2/PersistentTopics.java
index 2230b21..de4aa5b 100644
--- a/pulsar-broker/src/main/java/org/apache/pulsar/broker/admin/v2/PersistentTopics.java
+++ b/pulsar-broker/src/main/java/org/apache/pulsar/broker/admin/v2/PersistentTopics.java
@@ -899,7 +899,7 @@ public class PersistentTopics extends PersistentTopicsBase {
validateTopicName(tenant, namespace, encodedTopic);
internalResetCursorOnPosition(asyncResponse, decode(encodedSubName), authoritative
, new MessageIdImpl(resetCursorData.getLedgerId(), resetCursorData.getEntryId(), resetCursorData.getPartitionIndex())
- , resetCursorData.isExcluded());
+ , resetCursorData.isExcluded(), resetCursorData.getBatchIndex());
} catch (Exception e) {
resumeAsyncResponseExceptionally(asyncResponse, e);
}
diff --git a/pulsar-broker/src/test/java/org/apache/pulsar/broker/service/SubscriptionSeekTest.java b/pulsar-broker/src/test/java/org/apache/pulsar/broker/service/SubscriptionSeekTest.java
index 31a46cf..e28eeb8 100644
--- a/pulsar-broker/src/test/java/org/apache/pulsar/broker/service/SubscriptionSeekTest.java
+++ b/pulsar-broker/src/test/java/org/apache/pulsar/broker/service/SubscriptionSeekTest.java
@@ -31,7 +31,9 @@ import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
+import java.util.UUID;
import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import lombok.extern.slf4j.Slf4j;
@@ -44,6 +46,7 @@ import org.apache.pulsar.client.api.Producer;
import org.apache.pulsar.client.api.PulsarClientException;
import org.apache.pulsar.client.api.Schema;
import org.apache.pulsar.client.api.SubscriptionType;
+import org.apache.pulsar.client.impl.BatchMessageIdImpl;
import org.apache.pulsar.client.impl.MessageIdImpl;
import org.apache.pulsar.common.naming.TopicName;
import org.apache.pulsar.common.util.RelativeTimeUtil;
@@ -130,6 +133,7 @@ public class SubscriptionSeekTest extends BrokerTestBase {
Producer<String> producer = pulsarClient.newProducer(Schema.STRING)
.enableBatching(true)
.batchingMaxMessages(3)
+ .batchingMaxPublishDelay(100, TimeUnit.MILLISECONDS)
.topic(topicName).create();
@@ -176,6 +180,85 @@ public class SubscriptionSeekTest extends BrokerTestBase {
assertEquals(receiveId, messageId);
}
}
+ @Test
+ public void testSeekForBatchByAdmin() throws PulsarClientException, ExecutionException, InterruptedException, PulsarAdminException {
+ final String topicName = "persistent://prop/use/ns-abcd/testSeekForBatchByAdmin-" + UUID.randomUUID().toString();
+ String subscriptionName = "my-subscription-batch";
+
+ Producer<String> producer = pulsarClient.newProducer(Schema.STRING)
+ .enableBatching(true)
+ .batchingMaxMessages(3)
+ .batchingMaxPublishDelay(100, TimeUnit.MILLISECONDS)
+ .topic(topicName).create();
+
+
+ List<MessageId> messageIds = new ArrayList<>();
+ List<CompletableFuture<MessageId>> futureMessageIds = new ArrayList<>();
+
+ List<String> messages = new ArrayList<>();
+ for (int i = 0; i < 10; i++) {
+ String message = "my-message-" + i;
+ messages.add(message);
+ CompletableFuture<MessageId> messageIdCompletableFuture = producer.sendAsync(message);
+ futureMessageIds.add(messageIdCompletableFuture);
+ }
+
+ for (CompletableFuture<MessageId> futureMessageId : futureMessageIds) {
+ MessageId messageId = futureMessageId.get();
+ messageIds.add(messageId);
+ }
+
+ producer.close();
+
+
+ org.apache.pulsar.client.api.Consumer<String> consumer = pulsarClient.newConsumer(Schema.STRING)
+ .topic(topicName)
+ .subscriptionName(subscriptionName)
+ .subscribe();
+
+ admin.topics().resetCursor(topicName, subscriptionName, MessageId.earliest);
+
+ // Wait consumer reconnect
+ Thread.sleep(1000);
+ Message<String> receiveBeforeEarliest = consumer.receive();
+ assertEquals(receiveBeforeEarliest.getValue(), messages.get(0));
+
+ admin.topics().resetCursor(topicName, subscriptionName, MessageId.latest);
+ // Wait consumer reconnect
+ Thread.sleep(1000);
+ Message<String> receiveAfterLatest = consumer.receive(1, TimeUnit.SECONDS);
+ assertNull(receiveAfterLatest);
+
+ admin.topics().resetCursor(topicName, subscriptionName, messageIds.get(0), true);
+ // Wait consumer reconnect
+ Thread.sleep(1000);
+ Message<String> received = consumer.receive();
+ assertEquals(received.getMessageId(), messageIds.get(1));
+
+ admin.topics().resetCursor(topicName, subscriptionName, messageIds.get(0), false);
+ // Wait consumer reconnect
+ Thread.sleep(1000);
+ received = consumer.receive();
+ assertEquals(received.getMessageId(), messageIds.get(0));
+
+ admin.topics().resetCursor(topicName, subscriptionName, messageIds.get(messageIds.size() - 1), true);
+ // Wait consumer reconnect
+ Thread.sleep(1000);
+ received = consumer.receive(1, TimeUnit.SECONDS);
+ assertNull(received);
+
+ admin.topics().resetCursor(topicName, subscriptionName, messageIds.get(messageIds.size() - 1), false);
+ // Wait consumer reconnect
+ Thread.sleep(1000);
+ received = consumer.receive();
+ assertEquals(received.getMessageId(), messageIds.get(messageIds.size() - 1));
+
+ admin.topics().resetCursor(topicName, subscriptionName, new BatchMessageIdImpl(-1, -1, -1 ,10), true);
+ // Wait consumer reconnect
+ Thread.sleep(1000);
+ received = consumer.receive();
+ assertEquals(received.getMessageId(), messageIds.get(0));
+ }
@Test
diff --git a/pulsar-client/src/main/java/org/apache/pulsar/client/impl/ResetCursorData.java b/pulsar-client/src/main/java/org/apache/pulsar/client/impl/ResetCursorData.java
index 873ac15..9311fa0 100644
--- a/pulsar-client/src/main/java/org/apache/pulsar/client/impl/ResetCursorData.java
+++ b/pulsar-client/src/main/java/org/apache/pulsar/client/impl/ResetCursorData.java
@@ -30,6 +30,7 @@ public class ResetCursorData {
protected long entryId;
protected int partitionIndex = -1;
protected boolean isExcluded = false;
+ protected int batchIndex = -1;
public ResetCursorData(long ledgerId, long entryId) {
this.ledgerId = ledgerId;
@@ -43,11 +44,16 @@ public class ResetCursorData {
}
public ResetCursorData(MessageId messageId) {
- if (messageId instanceof MessageIdImpl) {
+ if (messageId instanceof BatchMessageIdImpl) {
+ BatchMessageIdImpl batchMessageId = (BatchMessageIdImpl) messageId;
+ this.ledgerId = batchMessageId.getLedgerId();
+ this.entryId = batchMessageId.getEntryId();
+ this.batchIndex = batchMessageId.getBatchIndex();
+ } else if (messageId instanceof MessageIdImpl) {
MessageIdImpl messageIdImpl = (MessageIdImpl) messageId;
this.ledgerId = messageIdImpl.getLedgerId();
this.entryId = messageIdImpl.getEntryId();
- } else if (messageId instanceof TopicMessageIdImpl) {
+ } else if (messageId instanceof TopicMessageIdImpl) {
throw new IllegalArgumentException("Not supported operation on partitioned-topic");
}
}