Skip to content

RATIS-2816. Preserve stateMachineContext in FileStoreStateMachine.startTransaction() - #1696

Merged
amaliujia merged 2 commits into
apache:masterfrom
lugency:RATIS-2816
Nov 18, 2026
Merged

amaliujia merged 2 commits into
apache:masterfrom
lugency:RATIS-2816

Conversation

@lugency

@lugency lugency commented Nov 17, 2026

Copy link
Copy Markdown
Contributor

What changes were proposed in this pull request?

FileStoreStateMachine#startTransaction(LogEntryProto, RaftPeerRole) parses the log data and sets the parsed request on the transaction context builder. The log-entry branch of TransactionContext.Builder#build() drops that value, so write(), read(), and applyTransaction() fall back to parsing the log data again.

  • Create the transaction context with super.startTransaction(entry, role) and set the parsed request on the returned context so it can be reused.

What is the link to the Apache JIRA

RATIS-2816

How was this patch tested?

Tested locally.

  • TestFileStoreWithGrpc
  • TestFileStoreAsyncWithGrpc
  • TestFileStoreStreamingWithGrpcCluster

@amaliujia amaliujia left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the change.

We could also have a unit test in TestStateMachine.java as:

  @Test
  public void testTransactionContextFromLogEntry() {
    final LogEntryProto entry = LogEntryProto.newBuilder()
        .setTerm(1)
        .setIndex(1)
        .setStateMachineLogEntry(StateMachineLogEntryProto.newBuilder()
            .setLogData(ByteString.copyFromUtf8("log-data")))
        .build();
    final StateMachine sm = new SimpleStateMachine4Testing();

    final Object context = new Object();
    final TransactionContext withContext = TransactionContext.newBuilder()
        .setStateMachine(sm)
        .setLogEntry(entry)
        .setServerRole(RaftPeerRole.FOLLOWER)
        .setStateMachineContext(context)
        .build();
    Assertions.assertSame(context, withContext.getStateMachineContext());
    Assertions.assertSame(entry, withContext.getLogEntry());
    assertEquals(RaftPeerRole.FOLLOWER, withContext.getServerRole());

    final TransactionContext withoutContext = sm.startTransaction(entry, RaftPeerRole.FOLLOWER);
    assertNull(withoutContext.getStateMachineContext());
  }

.setStateMachineContext(getProto(entry))
.build();
final TransactionContext trx = super.startTransaction(entry, role);
return trx.setStateMachineContext(getProto(entry));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for reporting this. The real issue then is inside the builder actually. So we should fix there directly at

return newTransactionContext(serverRole, stateMachine, logEntry);
:

} else {
  Objects.requireNonNull(logEntry, "logEntry must not be null since clientRequest == null");
  Preconditions.assertTrue(logEntry.hasStateMachineLogEntry(),
      () -> "Unexpected logEntry: stateMachineLogEntry not found, logEntry=" + logEntry);
  return newTransactionContext(serverRole, stateMachine, logEntry)
      .setStateMachineContext(stateMachineContext);
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, thanks!

@amaliujia
amaliujia merged commit b886ca2 into apache:master Nov 18, 2026
17 checks passed
@lugency

lugency commented Nov 18, 2026

Copy link
Copy Markdown
Contributor Author

Thanks @amaliujia for reviewing and merging!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants