From 9eb6d1e4cbb281566325a3471d6e2b3df4872aaf Mon Sep 17 00:00:00 2001 From: Walter Rafelsberger Date: Fri, 30 Jun 2023 12:39:15 +0200 Subject: [PATCH] [ML] Explain Log Rate Spikes: Fix slowness of `flushFix` random payload generation. (#160645) The random payload for the proxy flushing fix was regenerated for every push to the stream. This turned out to be quite slow. This PR updates the logic to create the payload only once and reuse it for every push. Note: Further testing in Cloud showed the differences there are not as big as in my local testing, might be related to entropy for `crypto.randomBytes`. --- x-pack/packages/ml/aiops_utils/src/stream_factory.ts | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/x-pack/packages/ml/aiops_utils/src/stream_factory.ts b/x-pack/packages/ml/aiops_utils/src/stream_factory.ts index d6e60e76efaa9..cb34d8aeeb156 100644 --- a/x-pack/packages/ml/aiops_utils/src/stream_factory.ts +++ b/x-pack/packages/ml/aiops_utils/src/stream_factory.ts @@ -73,6 +73,9 @@ export function streamFactory( ): StreamFactoryReturnType { let streamType: StreamType; const isCompressed = compressOverride && acceptCompression(headers); + const flushPayload = flushFix + ? crypto.randomBytes(FLUSH_PAYLOAD_SIZE).toString('hex') + : undefined; const stream = isCompressed ? zlib.createGzip() : new UncompressedResponseStream(); @@ -149,9 +152,7 @@ export function streamFactory( ? `${JSON.stringify({ ...d, // This is a temporary fix for response streaming with proxy configurations that buffer responses up to 4KB in size. - ...(flushFix - ? { flushPayload: crypto.randomBytes(FLUSH_PAYLOAD_SIZE).toString('hex') } - : {}), + ...(flushFix ? { flushPayload } : {}), })}${DELIMITER}` : d;