Skip to content
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
15 commits
Select commit Hold shift + click to select a range
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
14 changes: 14 additions & 0 deletions packages/runtime/container-runtime/src/metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,20 @@ export interface IBatchMetadata {
* Maybe set on first message of a batch, to the batchId generated when resubmitting (set/fixed on first resubmit)
*/
batchId?: BatchId;
/**
* Set on the envelope of a grouped batch op to the number of inner ops it contains.
* Exposed on the wire so consumers can record batch sizes in telemetry without parsing the grouped batch contents.
*
* Observable values:
* - Absent: this is not a grouped batch envelope (e.g. a singleton batch that bypassed grouping).
* - `0`: empty-grouped-batch placeholder produced when a resubmitted batch becomes empty.
* - `N` (N \> 0): grouped batch with N inner ops. For a chunked grouped batch this appears only on the last chunk's envelope (intermediate chunks carry no metadata).
Comment thread
anthony-murphy marked this conversation as resolved.
Comment thread
anthony-murphy marked this conversation as resolved.
*
* The field is intentionally advisory-only: the runtime does not validate that an inbound value matches the batch's actual inner op count. It is consumed exclusively by off-runtime telemetry.
Comment thread
anthony-murphy marked this conversation as resolved.
Outdated
*
* The field is always (re)stamped at outbound time from the current batch's actual size — `groupBatch` reads `batch.messages.length` directly, `createEmptyGroupedBatch` always writes `0`, and the chunking path only ever sees freshly-grouped envelopes from the same flush. It is never propagated from stashed pending state to the wire: on resubmit, ops re-enter grouping and the count is recomputed from the (possibly squashed, dropped, or added) outbound batch. This means the wire value always reflects the actual outbound size, even when the resubmitted batch differs from the original.
*/
groupedOpCount?: number;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ export class OpGroupingManager {
const serializedOp = JSON.stringify(emptyGroupedBatch);

const placeholderMessage: LocalEmptyBatchPlaceholder = {
metadata: { batchId: resubmittingBatchId },
metadata: { batchId: resubmittingBatchId, groupedOpCount: 0 },
localOpMetadata: { emptyBatch: true },
referenceSequenceNumber,
runtimeOp: emptyGroupedBatch,
Expand Down Expand Up @@ -169,7 +169,7 @@ export class OpGroupingManager {
...batch,
messages: [
{
metadata: { batchId: groupedBatchId },
metadata: { batchId: groupedBatchId, groupedOpCount: batch.messages.length },
referenceSequenceNumber: batch.messages[0].referenceSequenceNumber,
contents: serializedContent,
},
Expand Down
13 changes: 10 additions & 3 deletions packages/runtime/container-runtime/src/opLifecycle/opSplitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,12 +171,19 @@ export class OpSplitter {
);
}

// The last chunk will be part of the new batch and needs to
// preserve the batch metadata of the original batch
// The last chunk will be part of the new batch and needs to preserve the
// batch metadata of the original batch. groupedOpCount is surfaced here
// (and only here, not on intermediate chunks) because intermediate chunks
// don't carry ops — they carry parts of a payload that only become ops
// once the last chunk is processed and the payload is reassembled.
// Stamping every chunk would let an observer double-count messages.
const lastChunk = chunkToBatchMessage(
chunks[chunks.length - 1],
Comment thread
anthony-murphy marked this conversation as resolved.
Comment thread
anthony-murphy marked this conversation as resolved.
batch.referenceSequenceNumber,
{ batch: firstMessage.metadata?.batch },
{
batch: firstMessage.metadata?.batch,
groupedOpCount: firstMessage.metadata?.groupedOpCount,
},
);

this.logger.sendPerformanceEvent({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ describe("OpGroupingManager", () => {
{
contents:
'{"type":"groupedBatch","contents":[{"contents":0},{"contents":0},{"contents":0},{"contents":0},{"contents":0}]}',
metadata: { batchId: undefined },
metadata: { batchId: undefined, groupedOpCount: 5 },
referenceSequenceNumber: 0,
},
]);
Expand All @@ -90,6 +90,7 @@ describe("OpGroupingManager", () => {
).groupBatch(createBatch(5, false, false, batchId));
assert.strictEqual(result.messages.length, 1);
assert.strictEqual(result.messages[0].metadata?.batchId, batchId);
assert.strictEqual(result.messages[0].metadata?.groupedOpCount, 5);
});

it("empty grouped batching disabled", () => {
Expand All @@ -111,7 +112,7 @@ describe("OpGroupingManager", () => {
const batchId = "batchId";
const expectedOutboundMessage: OutboundBatchMessage = {
contents: '{"type":"groupedBatch","contents":[]}',
metadata: { batchId },
metadata: { batchId, groupedOpCount: 0 },
localOpMetadata: { emptyBatch: true },
referenceSequenceNumber: 0,
runtimeOp: undefined,
Expand All @@ -128,7 +129,7 @@ describe("OpGroupingManager", () => {

const expectedPlaceholderMessage: LocalEmptyBatchPlaceholder = {
runtimeOp: emptyGroupedBatch,
metadata: { batchId },
metadata: { batchId, groupedOpCount: 0 },
localOpMetadata: { emptyBatch: true },
referenceSequenceNumber: 0,
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ describe("OpCompressor", () => {
referenceSequenceNumber: messages[0].referenceSequenceNumber,
});
const createMessage = (contents: string) => ({
metadata: { flag: true },
metadata: { flag: true, groupedOpCount: 7 },
type: ContainerMessageType.FluidDataStoreOp,
contents,
referenceSequenceNumber: 0,
Expand All @@ -63,6 +63,7 @@ describe("OpCompressor", () => {
assert.strictEqual(compressedBatch.messages.length, batch.messages.length);
assert.strictEqual(compressedBatch.messages[0].compression, "lz4");
assert.strictEqual(compressedBatch.messages[0].metadata?.flag, true);
assert.strictEqual(compressedBatch.messages[0].metadata?.groupedOpCount, 7);
}).timeout(3000);
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,73 @@ describe("OpSplitter", () => {
);
});
}

it("Propagates groupedOpCount onto the last chunk only", () => {
const chunkSize = 20;
const opSplitter = new OpSplitter(
[],
mockSubmitBatchFn,
chunkSize,
maxBatchSizeInBytes,
mockLogger,
);
const groupedOpCount = 7;
const largeMessage: OutboundBatchMessage = {
...generateChunkableOp(100),
metadata: { batch: true, groupedOpCount },
};

const result = opSplitter.splitSingletonBatchMessage({
messages: [largeMessage],
contentSizeInBytes: largeMessage.contents?.length ?? 0,
referenceSequenceNumber: 0,
});

// Intermediate chunks (the ones submitted directly) carry no metadata.
for (const submitted of batchesSubmitted) {
assert.strictEqual(submitted.messages.length, 1);
assert.strictEqual(submitted.messages[0].metadata, undefined);
}

// Last chunk envelope carries both batch flag and groupedOpCount.
assert.strictEqual(result.messages.length, 1);
assert.deepStrictEqual(result.messages[0].metadata, {
batch: true,
groupedOpCount,
});

// Reassembly preserves groupedOpCount via originalMetadata on the final chunk's payload.
const reassembler = new OpSplitter(
[],
mockSubmitBatchFn,
chunkSize,
maxBatchSizeInBytes,
mockLogger,
);
const allChunks = [
...batchesSubmitted.map(
(b) =>
JSON.parse((b.messages[0] as OutboundBatchMessage).contents!) as {
contents: IChunkedOp;
},
),
JSON.parse(result.messages[0].contents!) as { contents: IChunkedOp },
].map((parsed) => parsed.contents);

let final: ISequencedDocumentMessage | undefined;
for (const chunk of allChunks) {
const wrapped = {
contents: { type: ContainerMessageType.ChunkedOp, contents: chunk },
clientId: "testClient",
} as unknown as ISequencedDocumentMessage;
const processed = reassembler.processChunk(wrapped);
if (processed.isFinalChunk) {
final = processed.message;
}
}
assert(final !== undefined, "Expected reassembly to complete");
assert.deepStrictEqual(final.metadata, { batch: true, groupedOpCount });
});
});
const assertSameMessage = (
result: ISequencedDocumentMessage,
Expand Down
Loading