"use strict";
/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.splitOp = exports.OpSplitter = exports.isChunkedMessage = void 0;
const internal_1 = require("@fluidframework/core-utils/internal");
const internal_2 = require("@fluidframework/telemetry-utils/internal");
const messageTypes_js_1 = require("../messageTypes.js");
const batchManager_js_1 = require("./batchManager.js");
function isChunkedMessage(message) {
    return isChunkedContents(message.contents);
}
exports.isChunkedMessage = isChunkedMessage;
function isChunkedContents(contents) {
    return contents?.type === messageTypes_js_1.ContainerMessageType.ChunkedOp;
}
/**
 * Responsible for creating and reconstructing chunked messages.
 */
class OpSplitter {
    constructor(chunks, submitBatchFn, chunkSizeInBytes, maxBatchSizeInBytes, logger) {
        this.submitBatchFn = submitBatchFn;
        this.chunkSizeInBytes = chunkSizeInBytes;
        this.maxBatchSizeInBytes = maxBatchSizeInBytes;
        this.chunkMap = new Map(chunks);
        this.logger = (0, internal_2.createChildLogger)({ logger, namespace: "OpSplitter" });
    }
    get isBatchChunkingEnabled() {
        return (this.chunkSizeInBytes < Number.POSITIVE_INFINITY && this.submitBatchFn !== undefined);
    }
    get chunks() {
        return this.chunkMap;
    }
    clearPartialChunks(clientId) {
        if (this.chunkMap.has(clientId)) {
            this.chunkMap.delete(clientId);
        }
    }
    addChunk(clientId, chunkedContent, originalMessage) {
        let map = this.chunkMap.get(clientId);
        if (map === undefined) {
            map = [];
            this.chunkMap.set(clientId, map);
        }
        if (chunkedContent.chunkId !== map.length + 1) {
            // We are expecting the chunks to be processed sequentially, in the same order as they are sent.
            // Therefore, the chunkId of the incoming op needs to match the length of the array (1-based indexing)
            // holding the existing chunks for that particular clientId.
            throw new internal_2.DataCorruptionError("Chunk Id mismatch", {
                ...(0, internal_2.extractSafePropertiesFromMessage)(originalMessage),
                chunkMapLength: map.length,
                chunkId: chunkedContent.chunkId,
                totalChunks: chunkedContent.totalChunks,
            });
        }
        map.push(chunkedContent.contents);
    }
    /**
     * Splits the first op of a compressed batch in chunks, sends the chunks separately and
     * returns a new batch composed of the last chunk and the rest of the ops in the original batch.
     *
     * A compressed batch is formed by one large op at the first position, followed by a series of placeholder ops
     * which are used in order to reserve the sequence numbers for when the first op gets unrolled into the original
     * uncompressed ops at ingestion in the runtime.
     *
     * If the first op is too large, it can be chunked (split into smaller op) which can be sent individually over the wire
     * and accumulate at ingestion, until the last op in the chunk is processed, when the original op is unrolled.
     *
     * This method will send the first N - 1 chunks separately and use the last chunk as the first message in the result batch
     * and then appends the original placeholder ops. This will ensure that the batch semantics of the original (non-compressed) batch
     * are preserved, as the original chunked op will be unrolled by the runtime when the first message in the batch is processed
     * (as it is the last chunk).
     *
     * To illustrate, if the input is `[largeOp, emptyOp, emptyOp]`, `largeOp` will be split into `[chunk1, chunk2, chunk3, chunk4]`.
     * `chunk1`, `chunk2` and `chunk3` will be sent individually and `[chunk4, emptyOp, emptyOp]` will be returned.
     *
     * @remarks - A side effect here is that 1 or more chunks are queued immediately for sending in next JS turn.
     *
     * @param batch - the compressed batch which needs to be processed
     * @returns A new adjusted batch (last chunk + empty placeholders) which can be sent over the wire
     */
    splitFirstBatchMessage(batch) {
        (0, internal_1.assert)(this.isBatchChunkingEnabled, 0x513 /* Chunking needs to be enabled */);
        (0, internal_1.assert)(batch.contentSizeInBytes > 0 && batch.messages.length > 0, 0x514 /* Batch needs to be non-empty */);
        (0, internal_1.assert)(batch.referenceSequenceNumber !== undefined, 0x58a /* Batch must have a reference sequence number if non-empty */);
        (0, internal_1.assert)(this.chunkSizeInBytes !== 0, 0x515 /* Chunk size needs to be non-zero */);
        (0, internal_1.assert)(this.chunkSizeInBytes < this.maxBatchSizeInBytes, 0x516 /* Chunk size needs to be smaller than the max batch size */);
        // Non null asserting here because of the length check above
        // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
        const firstMessage = batch.messages[0]; // we expect this to be the large compressed op, which needs to be split
        (0, internal_1.assert)((firstMessage.contents?.length ?? 0) >= this.chunkSizeInBytes, 0x518 /* First message in the batch needs to be chunkable */);
        const restOfMessages = batch.messages.slice(1); // we expect these to be empty ops, created to reserve sequence numbers
        const socketSize = (0, batchManager_js_1.estimateSocketSize)(batch);
        const chunks = (0, exports.splitOp)(firstMessage, this.chunkSizeInBytes, 
        // If we estimate that the socket batch size will exceed the batch limit
        // we will inject an empty op to minimize the risk of the payload failing due to
        // the overhead from the trailing empty ops in the batch.
        socketSize >= this.maxBatchSizeInBytes);
        (0, internal_1.assert)(this.submitBatchFn !== undefined, 0x519 /* We don't support old loaders */);
        // Send the first N-1 chunks immediately
        for (const chunk of chunks.slice(0, -1)) {
            this.submitBatchFn([chunkToBatchMessage(chunk, batch.referenceSequenceNumber)], batch.referenceSequenceNumber);
        }
        // The last chunk will be part of the new batch and needs to
        // preserve the batch metadata of the original batch
        const lastChunk = chunkToBatchMessage(
        // Non null asserting here because of the length assert above
        // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
        chunks[chunks.length - 1], batch.referenceSequenceNumber, { batch: firstMessage.metadata?.batch });
        this.logger.sendPerformanceEvent({
            // Used to be "Chunked compressed batch"
            eventName: "CompressedChunkedBatch",
            length: batch.messages.length,
            sizeInBytes: batch.contentSizeInBytes,
            chunks: chunks.length,
            chunkSizeInBytes: this.chunkSizeInBytes,
            socketSize,
        });
        return {
            messages: [lastChunk, ...restOfMessages],
            contentSizeInBytes: lastChunk.contents?.length ?? 0,
            referenceSequenceNumber: batch.referenceSequenceNumber,
        };
    }
    processChunk(message) {
        (0, internal_1.assert)(isChunkedContents(message.contents), 0x948 /* message not of type ChunkedOp */);
        const contents = message.contents;
        // TODO: Verify whether this should be able to handle server-generated ops (with null clientId)
        // eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
        const clientId = message.clientId;
        const chunkedContent = contents.contents;
        this.addChunk(clientId, chunkedContent, message);
        if (chunkedContent.chunkId < chunkedContent.totalChunks) {
            // We are processing the op in chunks but haven't reached
            // the last chunk yet in order to reconstruct the original op
            return {
                isFinalChunk: false,
            };
        }
        // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
        const serializedContent = this.chunkMap.get(clientId).join("");
        this.clearPartialChunks(clientId);
        // The final/complete message will contain the data from all the chunks.
        // It will have the sequenceNumber of the last chunk
        const completeMessage = { ...message };
        completeMessage.contents =
            serializedContent === "" ? undefined : JSON.parse(serializedContent);
        // back-compat with 1.x builds
        // This is only required / present for non-compressed, chunked ops
        // For compressed ops, we have op grouping enabled, and type of each op is preserved within compressed content.
        completeMessage.type = chunkedContent.originalType;
        completeMessage.metadata = chunkedContent.originalMetadata;
        completeMessage.compression = chunkedContent.originalCompression;
        return {
            message: completeMessage,
            isFinalChunk: true,
        };
    }
}
exports.OpSplitter = OpSplitter;
const chunkToBatchMessage = (chunk, referenceSequenceNumber, metadata = undefined) => {
    const payload = {
        type: messageTypes_js_1.ContainerMessageType.ChunkedOp,
        contents: chunk,
    };
    return {
        contents: JSON.stringify(payload),
        metadata,
        referenceSequenceNumber,
    };
};
/**
 * Splits an op into smaller ops (chunks), based on the size of the op and the `chunkSizeInBytes` parameter.
 *
 * The last op of the result will be bundled with empty ops in the same batch. There is a risk of the batch payload
 * exceeding the 1MB limit due to the overhead from the empty ops. If the last op is large, the risk is even higher.
 * To minimize the odds, an extra empty op can be added to the result using the `extraOp` parameter.
 *
 * @param op - the op to be split
 * @param chunkSizeInBytes - how large should the chunks be
 * @param extraOp - should an extra empty op be added to the result
 * @returns an array of chunked ops
 */
const splitOp = (op, chunkSizeInBytes, extraOp = false) => {
    const chunks = [];
    (0, internal_1.assert)(op.contents !== undefined && op.contents !== null, 0x51a /* We should have something to chunk */);
    const contentLength = op.contents.length;
    const chunkCount = Math.floor((contentLength - 1) / chunkSizeInBytes) + 1 + (extraOp ? 1 : 0);
    let offset = 0;
    for (let chunkId = 1; chunkId <= chunkCount; chunkId++) {
        const chunk = {
            chunkId,
            contents: op.contents.substr(offset, chunkSizeInBytes),
            totalChunks: chunkCount,
        };
        if (chunkId === chunkCount) {
            // We don't need to port these to all the chunks,
            // as we rebuild the original op when we process the
            // last chunk, therefore it is the only one that needs it.
            chunk.originalMetadata = op.metadata;
            chunk.originalCompression = op.compression;
            // back-compat with 1.x builds
            // 2.x builds only do chunking for compressed ops.
            // originalType is no longer used in such cases, as each op preserves its type within compressed payload.
            // But, if 1.x builds see this op, and there is no type on the message, then it will ignore this message silently.
            // This is really bad, as we will crash on later ops and it's very hard to debug these cases.
            // If we put some known type here, then we will crash on it (as 1.x does not understand compression, and thus will not
            // find info on the op like address of the channel to deliver the op)
            chunk.originalType = "component";
        }
        chunks.push(chunk);
        offset += chunkSizeInBytes;
        (0, internal_1.assert)(chunkId >= chunkCount - 1 || offset <= contentLength, 0x58b /* Content offset within bounds */);
    }
    (0, internal_1.assert)(offset >= contentLength, 0x58c /* Content offset equal or larger than content length */);
    (0, internal_1.assert)(chunks.length === chunkCount, 0x5a5 /* Expected number of chunks */);
    return chunks;
};
exports.splitOp = splitOp;
//# sourceMappingURL=opSplitter.js.map