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

[fix][broker] Fix unexpected subscription deletion caused by the cursor last active time not updated in time #17573

Merged
merged 2 commits into from
Sep 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,9 @@ public void operationComplete(ManagedCursorInfo info, Stat stat) {

cursorLedgerStat = stat;
lastActive = info.getLastActive() != 0 ? info.getLastActive() : lastActive;

if (log.isDebugEnabled()) {
log.debug("[{}] [{}] Recover cursor last active to [{}]", ledger.getName(), name, lastActive);
}

Map<String, String> recoveredCursorProperties = Collections.emptyMap();
if (info.getCursorPropertiesCount() > 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,8 +215,9 @@ public void asyncGetCursorInfo(String ledgerName, String cursorName,
public void asyncUpdateCursorInfo(String ledgerName, String cursorName, ManagedCursorInfo info, Stat stat,
MetaStoreCallback<Void> callback) {
if (log.isDebugEnabled()) {
log.debug("[{}] [{}] Updating cursor info ledgerId={} mark-delete={}:{}", ledgerName, cursorName,
info.getCursorsLedgerId(), info.getMarkDeleteLedgerId(), info.getMarkDeleteEntryId());
log.debug("[{}] [{}] Updating cursor info ledgerId={} mark-delete={}:{} lastActive={}",
ledgerName, cursorName, info.getCursorsLedgerId(), info.getMarkDeleteLedgerId(),
info.getMarkDeleteEntryId(), info.getLastActive());
}

String path = PREFIX + ledgerName + "/" + cursorName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1001,8 +1001,8 @@ public void checkInactiveSubscriptions() {
return;
}
if (System.currentTimeMillis() - sub.getLastActive() > expirationTimeMillis) {
sub.delete().thenAccept(v -> log.info("[{}][{}] The subscription was deleted due to expiration",
topic, subName));
sub.delete().thenAccept(v -> log.info("[{}][{}] The subscription was deleted due to expiration "
+ "with last active [{}]", topic, subName, sub.getLastActive()));
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,7 @@ public void consumerFlow(Consumer consumer, int additionalNumberOfMessages) {

@Override
public void acknowledgeMessage(List<Position> positions, AckType ackType, Map<String, Long> properties) {
cursor.updateLastActive();
Position previousMarkDeletePosition = cursor.getMarkDeletedPosition();

if (ackType == AckType.Cumulative) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2352,8 +2352,8 @@ public void checkInactiveSubscriptions() {
return;
}
if (System.currentTimeMillis() - sub.cursor.getLastActive() > expirationTimeMillis) {
sub.delete().thenAccept(v -> log.info("[{}][{}] The subscription was deleted due to expiration",
topic, subName));
sub.delete().thenAccept(v -> log.info("[{}][{}] The subscription was deleted due to expiration "
+ "with last active [{}]", topic, subName, sub.cursor.getLastActive()));
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import static org.apache.pulsar.broker.auth.MockedPulsarServiceBaseTest.createMockZooKeeper;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.doCallRealMethod;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
Expand Down Expand Up @@ -348,4 +349,25 @@ public void testCanAcknowledgeAndAbortForTransaction() throws Exception {

persistentSubscription.transactionIndividualAcknowledge(txnID2, positionsPair);
}

@Test
public void testAcknowledgeUpdateCursorLastActive() throws Exception {
doAnswer((invocationOnMock) -> {
((AsyncCallbacks.DeleteCallback) invocationOnMock.getArguments()[1])
.deleteComplete(invocationOnMock.getArguments()[2]);
return null;
}).when(cursorMock).asyncDelete(any(List.class), any(AsyncCallbacks.DeleteCallback.class), any());

doCallRealMethod().when(cursorMock).updateLastActive();
doCallRealMethod().when(cursorMock).getLastActive();

List<Position> positionList = new ArrayList<>();
positionList.add(new PositionImpl(1, 1));
long beforeAcknowledgeTimestamp = System.currentTimeMillis();
Thread.sleep(1);
persistentSubscription.acknowledgeMessage(positionList, AckType.Individual, Collections.emptyMap());

// `acknowledgeMessage` should update cursor last active
assertTrue(persistentSubscription.cursor.getLastActive() > beforeAcknowledgeTimestamp);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -347,4 +347,59 @@ public void testDelayedDeliveryTrackerMemoryUsageMetric(String topic, boolean ex
Assert.assertEquals(topicLevelNum, 0);
}
}

@Test
public void testUpdateCursorLastActive() throws Exception {
final String topicName = "persistent://prop/ns-abc/aTopic";
final String sharedSubName = "shared";
final String failoverSubName = "failOver";

long beforeAddConsumerTimestamp = System.currentTimeMillis();
Thread.sleep(1);
Consumer<String> consumer = pulsarClient.newConsumer(Schema.STRING).topic(topicName)
.subscriptionType(SubscriptionType.Shared).subscriptionName(sharedSubName)
.acknowledgmentGroupTime(0, TimeUnit.MILLISECONDS).subscribe();
Consumer<String> consumer2 = pulsarClient.newConsumer(Schema.STRING).topic(topicName)
.subscriptionType(SubscriptionType.Failover).subscriptionName(failoverSubName)
.acknowledgmentGroupTime(0, TimeUnit.MILLISECONDS).subscribe();
Producer<String> producer = pulsarClient.newProducer(Schema.STRING).topic(topicName).create();

PersistentTopic topic = (PersistentTopic) pulsar.getBrokerService().getTopicReference(topicName).get();
PersistentSubscription persistentSubscription = topic.getSubscription(sharedSubName);
PersistentSubscription persistentSubscription2 = topic.getSubscription(failoverSubName);

// `addConsumer` should update last active
assertTrue(persistentSubscription.getCursor().getLastActive() > beforeAddConsumerTimestamp);
assertTrue(persistentSubscription2.getCursor().getLastActive() > beforeAddConsumerTimestamp);

long beforeAckTimestamp = System.currentTimeMillis();
Thread.sleep(1);
producer.newMessage().value("test").send();
Message<String> msg = consumer.receive();
assertNotNull(msg);
consumer.acknowledge(msg);
msg = consumer2.receive();
assertNotNull(msg);
consumer2.acknowledge(msg);

// Make sure ack commands have been sent to broker
Awaitility.waitAtMost(5, TimeUnit.SECONDS)
.until(() -> persistentSubscription.getCursor().getLastActive() > beforeAckTimestamp);
Awaitility.waitAtMost(5, TimeUnit.SECONDS)
.until(() -> persistentSubscription2.getCursor().getLastActive() > beforeAckTimestamp);

// `acknowledgeMessage` should update last active
assertTrue(persistentSubscription.getCursor().getLastActive() > beforeAckTimestamp);
assertTrue(persistentSubscription2.getCursor().getLastActive() > beforeAckTimestamp);

long beforeRemoveConsumerTimestamp = System.currentTimeMillis();
Thread.sleep(1);
consumer.unsubscribe();
consumer2.unsubscribe();
producer.close();

// `removeConsumer` should update last active
assertTrue(persistentSubscription.getCursor().getLastActive() > beforeRemoveConsumerTimestamp);
assertTrue(persistentSubscription2.getCursor().getLastActive() > beforeRemoveConsumerTimestamp);
}
}