Skip to content

Defer IoTConsensus insert serialization until after local apply - #19432

Open
jt2594838 wants to merge 1 commit into
apache:masterfrom
Caideyipi:codex/iot-consensus-serialize-outside-lock
Open

jt2594838 wants to merge 1 commit into
apache:masterfrom
Caideyipi:codex/iot-consensus-serialize-outside-lock

Conversation

@jt2594838

@jt2594838 jt2594838 commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

Summary

IoTConsensus currently materializes serialized insert requests in the synchronous replication offer path while the state machine write lock is held. This change lets InsertNode requests defer serialization until after local application. The write-thread path serializes after releasing stateMachineLock, while the replication batch builder retains an idempotent fallback.

Deferred requests remain accounted for in replication queue memory. Their source plan nodes are released after serialization when no subscription queue needs them. Pipe-enriched inserts include their wrapper size in the memory estimate.

Example

An InsertRowNode can arrive with string values and unresolved types. Local application infers DOUBLE, FLOAT, INT64, INT32, and BOOLEAN, with values 1.0, 2.0f, 10000L, 100, and false. Deferred serialization encodes the applied request state for replication.

Validation

  • IndexedConsensusRequestTest and IoTConsensusServerImplTest passed, 10 tests total.
  • InsertRowNodeSerdeTest and PipeEnrichedInsertNodeSerdeTest passed, 14 tests total.
  • Spotless applied successfully for the consensus and DataNode modules.
  • git diff --check passed.

Write benchmark

The A/B comparison used one ConfigNode and two DataNodes. The workload used 200 devices, 100 fields per device, 12 clients, 200 loops, and batches of 100 rows. Each of three runs per version wrote 400 million points with a 1 GiB benchmark JVM. Median throughput increased from 1,585,283 to 1,725,553 points/s, an 8.85% increase. Median P50 latency decreased 6.66%, while median P99 latency increased 2.59%.

Rounds 2 and 3 reused each version's pre-created schema in disjoint time ranges because creating additional databases reached the DataNode region direct-buffer limit. Schema creation was excluded from write elapsed time. The initial baseline run used an 8.5 GiB JVM due to the Windows launcher ignoring the requested heap size, so it was excluded and rerun with a 1 GiB JVM.

* implementations may postpone {@link #serializeToByteBuffer()} for such requests and keep the
* source objects alive until then.
* <p>A deferred request keeps its source objects while it waits in the replication queues, so
* {@link #getMemorySize()} must account for them and the request must be able to rebuild the same

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.

This contract clarifies that a deferred request keeps its source objects resident until serialization, so getMemorySize() must include them for accurate queue accounting.

* Clears source requests after deferred serialization when no subscription queue needs them. The
* serialized buffers remain available to replication retries.
*/
public synchronized void clearRequestsAfterSerialization() {

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.

This lifecycle hook lets replication-only requests release source plan nodes once serialized buffers are available, while keeping those buffers for retries and preserving the original queue reservation.

// Deferred requests are materialized after releasing the per-region lock and before the
// caller can reuse the applied plan node.
if (requestToSerialize != null) {
requestToSerialize.buildSerializedRequests();

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.

The local apply is complete and stateMachineLock has been released before the writer-thread fallback materializes bytes. The synchronous call also ensures the caller cannot reuse the plan node first.

if (!keepRequestsForSubscription && !serializationDeferred) {
request.clearRequests();
} else if (!keepRequestsForSubscription) {
request.clearRequestsAfterSerialization();

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.

Deferred requests skip eager encoding here. When subscription delivery did not retain the source node, this flag releases it as soon as serialization produces the replication buffer.

}
}
}
if (!keepRequestsForSubscription && serializationDeferred && !offeredToAnyThread) {

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.

When no peer queue accepts the entry, no batch will materialize its deferred bytes. Releasing the source here avoids retaining it while the dispatcher fills the index gap from WAL.


@Override
public long getMemorySize() {
return insertNode.getMemorySize() + INSTANCE_SIZE;

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.

The queued request is the pipe wrapper, so the estimate includes both the wrapped insert payload and the wrapper instance rather than undercounting replication queue memory.


// A serialized replication request releases its source object when no subscription retains it.
@Test
public void testDeferredSourceCanBeClearedAfterSerialization() {

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.

This test covers deferred cleanup after bytes are built and verifies source release, retained buffer availability, and stable queue-reservation accounting.

// A deferred local write is serialized after the state machine lock is released, before callers
// can reuse the request, and subscription consumers still receive its serialized bytes.
@Test
public void testDeferredWriteIsSerializedAfterStateMachineLockRelease() throws Exception {

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.

This regression test verifies the write-thread serialization happens after it releases stateMachineLock and that an accepted subscription retains both the source request and encoded bytes.


// Deferred replication of a pipe-enriched insert must reserve the wrapped insert and wrapper.
@Test
public void testDeferredRequestKeepsWrappedInsertMemoryEstimate() throws IllegalPathException {

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.

This test protects the pipe wrapper contract by checking that it inherits deferred serialization and accounts for its own instance memory.


// Type inference after queue creation must be reflected when IoTConsensus later serializes it.
@Test
public void testInsertRequestSerializationIsDeferred() throws IllegalPathException {

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.

This scenario builds the indexed request before type inference, then checks its eventual encoded form reflects the inferred types and values.

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.

1 participant