Skip to content

[improve][client] Avoid reconnecting when RawReader is seeking. - #14912

Closed
mattisonchao wants to merge 2 commits into
apache:masterfrom
mattisonchao:avoid_reconnect_when_seeking
Closed

mattisonchao wants to merge 2 commits into
apache:masterfrom
mattisonchao:avoid_reconnect_when_seeking

Conversation

@mattisonchao

@mattisonchao mattisonchao commented Mar 28, 2022 •

Copy link
Copy Markdown
Member

Fixes #14882

Master Issue: #14882

Motivation

compactor uses a durable RawReader to read messages. When compactor invoke doCompact at TwoPhaseCompactor#phaseTwo, they will do a seek operation. code show as below:

// TwoPhaseCompactor.class  lines 198 - 207 
private CompletableFuture<Long> phaseTwoSeekThenLoop(RawReader reader, MessageId from, MessageId to,
            MessageId lastReadId, Map<String, MessageId> latestForKey, BookKeeper bk, LedgerHandle ledger) {
        CompletableFuture<Long> promise = new CompletableFuture<>();

        reader.seekAsync(from).thenCompose((v) -> {
            Semaphore outstanding = new Semaphore(MAX_OUTSTANDING);
            CompletableFuture<Void> loopPromise = new CompletableFuture<Void>();
            phaseTwoLoop(reader, to, latestForKey, ledger, outstanding, loopPromise);
            return loopPromise;
        })

And then, the broker will disconnect all consumers and reset the cursor.

Since the RawReader is a durable consumer in the broker, if it reconnects when it wants to reset the cursor by the broker and store the cursor information in the metadata. It will get a Subscription is fenced exception until the cursor is reset successfully.

Modifications

  • Avoid reconnecting when RawReader is seeking.

Verifying this change

  • Make sure that the change passes the CI checks.

It's hard to write tests to mock the reset cursor delay(or does anyone has a good idea? )

I add a test code in source code as follow:

// asyncResetCursor#asyncResetCursor
                cursor.asyncResetCursor(finalPosition, forceReset, new AsyncCallbacks.ResetCursorCallback() {
                    @Override
                    public void resetComplete(Object ctx) {
                        if (log.isDebugEnabled()) {
                            log.debug("[{}][{}] Successfully reset subscription to position {}", topicName, subName,
                                    finalPosition);
                        }
                        if (dispatcher != null) {
                            dispatcher.cursorIsReset();
                        }
                        // ----- test code
                        try {
                            Thread.sleep(2000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        // ----- test code
                        IS_FENCED_UPDATER.set(PersistentSubscription.this, FALSE);
                        future.complete(null);
                    }

And write a test here to verify.

ReaderTest.class
  public void testSeek() throws Exception {
        final String topic = "persistent://my-property/my-ns/seek";
        @Cleanup
        Producer<byte[]> producer = pulsarClient.newProducer()
                .topic(topic)
                .create();
        MessageId firstMessageId = null;
        for (int i = 0; i < 300; i++) {
            MessageId msgId = producer.send(UUID.randomUUID().toString().getBytes(StandardCharsets.UTF_8));
            if (firstMessageId == null) {
                firstMessageId = msgId;
            }
        }
        CompletableFuture<Consumer<byte[]>> future = new CompletableFuture<>();
        RawReader reader = new RawReaderImpl((PulsarClientImpl) pulsarClient, topic, "test", future);
        future.get();
        reader.seekAsync(firstMessageId).get();
        Thread.sleep(10000);
    }

Documentation

  • no-need-doc

@github-actions github-actions Bot added the doc-not-needed Your PR changes do not impact docs label Mar 28, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

doc-not-needed Your PR changes do not impact docs

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Compaction stop on topic __transaction_buffer_snapshot

1 participant