GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — [fix][client] Keep transactional and non-transactional messages in separate batches by SongOf · Pull Request #26547 · apache/pulsar

Conversation

@SongOf

@SongOf SongOf commented Sep 11, 2026

Copy link
Copy Markdown
Contributor

Motivation

A batch carries a single transaction id in its metadata, so every message inside it inherits that
transaction — there is no per-message override:

// BatchMessageContainerImpl.createOpSendMsg
if (currentTxnidMostBits != -1) {
    messageMetadata.setTxnidMostBits(currentTxnidMostBits);
}

hasSameTxn, the admission check meant to enforce that invariant, cannot express it:

public boolean hasSameTxn(MessageImpl<?> msg) {
    if (!msg.getMessageBuilder().hasTxnidMostBits() || !msg.getMessageBuilder().hasTxnidLeastBits()) {
        return true;                                    // (1)
    }
    if (currentTxnidMostBits == -1 || currentTxnidLeastBits == -1) {
        currentTxnidMostBits = msg.getMessageBuilder().getTxnidMostBits();   // (2) mutates the batch
        currentTxnidLeastBits = msg.getMessageBuilder().getTxnidLeastBits();
        return true;
    }
    return currentTxnidMostBits == msg.getMessageBuilder().getTxnidMostBits()   // (3)
            && currentTxnidLeastBits == msg.getMessageBuilder().getTxnidLeastBits();
}

Branch (1) inspects only the message, never the batch, so a plain message is admitted into any batch
including a transactional one. Branch (2) performs a write inside a hasSameXxx query, and makes a
plain batch adopt the transaction of the first transactional message merely offered to it. Only
branch (3) actually compares, and it is reachable only when both sides carry a transaction. The check
can answer "are these two transactions the same?" but never "is one of them absent?".

Three consequences, all reachable from a single batching producer used for both newMessage(txn) and
newMessage() — legal API usage, with batching enabled by default:

  1. A plain message batched after transactional ones inherits the transaction: it stays invisible until
    commit and is silently discarded if the transaction aborts, although the application never sent
    it inside a transaction.
  2. A transactional message joining a batch that already holds plain messages stamps its transaction
    onto the whole batch, retroactively enrolling those plain messages in it.
  3. The side effect in branch (2) also fires on the duplicate-sequence-id path in ProducerImpl:
    canAddToCurrentBatch adopts the transaction, and the caller then flushes the current batch via
    doBatchSendAndAdd and puts the new message into a fresh one — so a batch of plain messages is
    published stamped with a transaction that none of its messages belongs to.

BatchMessageKeyBasedContainer fails the other way round. The guard reads the outer container's
transaction state, while the batch metadata is built by the per-key inner container from its own
first message. A transactional message landing in a key bucket whose first message was plain is
therefore published with no transaction id at all — outside the transaction, and not rolled back when
it aborts.

For reference, the sibling admission check on the same line of canAddToCurrentBatch is already the
correct shape: hasSameSchema is a pure query that starts with numMessagesInBatch == 0 and treats
"absent" as an identity that must match.

Modifications

  • AbstractBatchMessageContainer.hasSameTxn is now a pure query comparing transaction identities, with
    "no transaction" as an identity of its own that is incompatible with any transaction. An empty batch
    (numMessagesInBatch == 0) still accepts anything; testing the message count rather than the -1
    sentinel is what distinguishes "no identity yet" from "identity is no transaction". Adopting the
    identity is left solely to add, where it already happened — which also removes consequence 3 above.
  • BatchMessageKeyBasedContainer.add captures the transaction id from the container's first message.
    This is required rather than cosmetic: with the side effect gone, the outer container's transaction
    state would otherwise stay unset forever, batchHasTxn would always be false, and plain messages
    would again be admitted into a transactional container.

Nothing changes at the call site. When the guard rejects a message, ProducerImpl.doBatchSendAndAdd
already flushes the current batch and opens a new one with that message, exactly as it does today when
the batch is full or the schema differs.

Applications that mix both kinds of send on one producer will see batches split more often. That is the
necessary cost: the two cannot share a batch without one of them inheriting the wrong transaction
identity. There is no wire format or API change, and no behaviour change for applications that do not
mix.

Verifying this change

  • Make sure that the change passes the CI checks.

This change added tests and can be verified as follows:

  • Added BatchMessageContainerImplTest.testPlainMessageIsNotBatchedWithTransactionalMessages and
    ...testTransactionalMessageIsNotBatchedWithPlainMessages, covering both directions, and
    ...testKeyBasedContainerKeepsTransactionalAndPlainMessagesApart for the key-based batcher. All
    three fail before the change.
  • Added BatchMessageContainerImplTest.testSameTransactionStillBatchesTogether and
    ...testPlainMessagesStillBatchTogether as guards that messages of one transaction still share a
    batch, that two different transactions still do not, and that plain messages still batch together.
  • Added TransactionProduceTest.testAbortedTransactionDoesNotDiscardPlainMessageBatchedWithIt: a
    batching producer sends a transactional and a plain message into the same batch, the transaction is
    aborted, and the plain message must still be readable. It fails before the change with "the plain
    message was discarded by the aborted transaction".
  • BatchMessageContainerImplTest passes 9/9, the pulsar-client module suite shows no new failures,
    and ./gradlew quickCheck is clean.

Does this pull request potentially affect one of the following parts:

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

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@song-of-bot
[fix][client] Keep transactional and non-transactional messages in se…
d619e89
…parate batches
lhotari
lhotari requested changes Sep 11, 2026
View reviewed changes

@lhotari lhotari left a comment

Copy link
Copy Markdown
Member

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.

Thanks for fixing this transaction isolation issue and adding the abort regression test. The normal batching paths look correct. There is one remaining allocation-recovery path that can still mix transactional and plain messages, plus a small cleanup issue in the new unit tests.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread pulsar-client/src/main/java/org/apache/pulsar/client/impl/BatchMessageKeyBasedContainer.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread pulsar-client/src/test/java/org/apache/pulsar/client/impl/BatchMessageContainerImplTest.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

@song-of-bot
[fix][client] Reset the key-based batch transaction identity when the…
d18e8a1
… first add fails
lhotari
lhotari approved these changes Sep 12, 2026
View reviewed changes

@lhotari lhotari left a comment

Copy link
Copy Markdown
Member

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.

LGTM. The follow-up resets the key-based transaction identity on every first add, including a plain message after a failed allocation, and adds the requested recovery regression test. The container tests also release their batch buffers in finally blocks.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@lhotari lhotari added this to the 5.0.0-M2 milestone Sep 12, 2026
@lhotari lhotari added release/4.2.5 release/4.0.14 labels Sep 12, 2026
@lhotari
lhotari merged commit fa05c3e into apache:master Sep 12, 2026
81 of 83 checks passed

Uh oh!

There was an error while loading. Please reload this page.

nodece added a commit to nodece/pulsar that referenced this pull request Sep 14, 2026
@nodece
Merge upstream/master into fix-batch-buffer-use-after-release
47de49e
Conflict with apache#26547 (transactional/non-transactional batch isolation)
in BatchMessageContainerImplTest: both sides appended tests at the end
of the class. Resolution keeps both sets — apache#26547's transaction-batch
tests and this PR's buffer-ownership tests — with the import block
rebuilt as the sorted union.

Verified: BatchMessageContainerImplTest 21/21, ProducerImplTest 23/23,
spotless + checkstyle clean.
lhotari pushed a commit that referenced this pull request Sep 14, 2026
@SongOf @song-of-bot @lhotari
[fix][client] Keep transactional and non-transactional messages in se…
89184b5
…parate batches (#26547)

Co-authored-by: maxlisongsong <[email protected]>
(cherry picked from commit fa05c3e)
@lhotari lhotari added the cherry-picked/branch-4.2 label Sep 14, 2026
lhotari pushed a commit that referenced this pull request Sep 14, 2026
@SongOf @song-of-bot @lhotari
[fix][client] Keep transactional and non-transactional messages in se…
84f009a
…parate batches (#26547)

Co-authored-by: maxlisongsong <[email protected]>
(cherry picked from commit fa05c3e)
@lhotari lhotari added the cherry-picked/branch-4.0 label Sep 14, 2026
@nodece nodece mentioned this pull request Sep 28, 2026
[fix] Verify backports with CI ascentstream/pulsar#104
Closed
3 of 4 tasks
Radiancebobo pushed a commit to Radiancebobo/pulsar that referenced this pull request Oct 8, 2026
@SongOf @song-of-bot hechengbo
[fix][client] Keep transactional and non-transactional messages in se…
f82237f
…parate batches (apache#26547)

Co-authored-by: maxlisongsong <[email protected]>
@merlimat merlimat mentioned this pull request Oct 23, 2026
[fix][client] Keep non-transactional messages out of transactional batches on scalable topics #27079
Merged
11 tasks
@nodece nodece mentioned this pull request Nov 5, 2026
[fix][ml][broker][client] Sync branch-as-4.0 with apache branch-4.0 (2026-09 to 2026-11, 159 commits) ascentstream/pulsar#109
Merged
4 of 5 tasks
nodece pushed a commit to ascentstream/pulsar that referenced this pull request Nov 16, 2026
@SongOf @song-of-bot @nodece
[fix][client] Keep transactional and non-transactional messages in se…
b1b4c9d
…parate batches (apache#26547)

Co-authored-by: maxlisongsong <[email protected]>
(cherry picked from commit fa05c3e)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@lhotari lhotari lhotari approved these changes

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

cherry-picked/branch-4.0 cherry-picked/branch-4.2 release/4.0.14 release/4.2.5

Projects

None yet

Milestone

5.0.0-M2

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

3 participants

@SongOf @lhotari @song-of-bot
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.