@514labs/moose-lib 0.6.251 → 0.6.252-ci-1-g901efb04
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/browserCompatible.js +14 -6
- package/dist/browserCompatible.js.map +1 -1
- package/dist/browserCompatible.mjs +14 -6
- package/dist/browserCompatible.mjs.map +1 -1
- package/dist/compilerPlugin.js.map +1 -1
- package/dist/compilerPlugin.mjs.map +1 -1
- package/dist/dmv2/index.js +14 -6
- package/dist/dmv2/index.js.map +1 -1
- package/dist/dmv2/index.mjs +14 -6
- package/dist/dmv2/index.mjs.map +1 -1
- package/dist/index.d.mts +25 -2
- package/dist/index.d.ts +25 -2
- package/dist/index.js +16 -6
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +15 -6
- package/dist/index.mjs.map +1 -1
- package/dist/moose-runner.js +78 -203
- package/dist/moose-runner.js.map +1 -1
- package/dist/moose-runner.mjs +78 -203
- package/dist/moose-runner.mjs.map +1 -1
- package/package.json +1 -1
package/dist/moose-runner.mjs
CHANGED
|
@@ -26,6 +26,22 @@ function isTruthy(value) {
|
|
|
26
26
|
return false;
|
|
27
27
|
}
|
|
28
28
|
}
|
|
29
|
+
function createProducerConfig(maxMessageBytes) {
|
|
30
|
+
return {
|
|
31
|
+
kafkaJS: {
|
|
32
|
+
idempotent: false,
|
|
33
|
+
// Not needed for at-least-once delivery
|
|
34
|
+
acks: ACKs,
|
|
35
|
+
retry: {
|
|
36
|
+
retries: MAX_RETRIES_PRODUCER,
|
|
37
|
+
maxRetryTime: MAX_RETRY_TIME_MS
|
|
38
|
+
}
|
|
39
|
+
},
|
|
40
|
+
"linger.ms": 0,
|
|
41
|
+
// Send immediately - batching happens at application level
|
|
42
|
+
...maxMessageBytes && { "message.max.bytes": maxMessageBytes }
|
|
43
|
+
};
|
|
44
|
+
}
|
|
29
45
|
var Kafka, compilerLog, getClickhouseClient, cliLog, MAX_RETRIES, MAX_RETRY_TIME_MS, RETRY_INITIAL_TIME_MS, MAX_RETRIES_PRODUCER, ACKs, parseBrokerString, logError, buildSaslConfig, getKafkaClient;
|
|
30
46
|
var init_commons = __esm({
|
|
31
47
|
"src/commons.ts"() {
|
|
@@ -1803,156 +1819,6 @@ var MAX_RETRIES_CONSUMER = 150;
|
|
|
1803
1819
|
var SESSION_TIMEOUT_CONSUMER = 3e4;
|
|
1804
1820
|
var HEARTBEAT_INTERVAL_CONSUMER = 3e3;
|
|
1805
1821
|
var DEFAULT_MAX_STREAMING_CONCURRENCY = 100;
|
|
1806
|
-
var KAFKAJS_BYTE_MESSAGE_OVERHEAD = 500;
|
|
1807
|
-
var isMessageTooLargeError = (error) => {
|
|
1808
|
-
if (KafkaJS2.isKafkaJSError && error instanceof Error && KafkaJS2.isKafkaJSError(error)) {
|
|
1809
|
-
return error.type === "ERR_MSG_SIZE_TOO_LARGE" || error.code === 10 || error.cause !== void 0 && isMessageTooLargeError(error.cause);
|
|
1810
|
-
}
|
|
1811
|
-
if (error && typeof error === "object") {
|
|
1812
|
-
const err = error;
|
|
1813
|
-
return err.type === "ERR_MSG_SIZE_TOO_LARGE" || err.code === 10 || err.cause !== void 0 && isMessageTooLargeError(err.cause);
|
|
1814
|
-
}
|
|
1815
|
-
return false;
|
|
1816
|
-
};
|
|
1817
|
-
var splitBatch = (messages, maxChunkSize) => {
|
|
1818
|
-
if (messages.length <= 1) {
|
|
1819
|
-
return [messages];
|
|
1820
|
-
}
|
|
1821
|
-
const chunks = [];
|
|
1822
|
-
let currentChunk = [];
|
|
1823
|
-
let currentSize = 0;
|
|
1824
|
-
for (const message of messages) {
|
|
1825
|
-
const messageSize = Buffer2.byteLength(message.value, "utf8") + KAFKAJS_BYTE_MESSAGE_OVERHEAD;
|
|
1826
|
-
if (currentSize + messageSize > maxChunkSize && currentChunk.length > 0) {
|
|
1827
|
-
chunks.push(currentChunk);
|
|
1828
|
-
currentChunk = [message];
|
|
1829
|
-
currentSize = messageSize;
|
|
1830
|
-
} else {
|
|
1831
|
-
currentChunk.push(message);
|
|
1832
|
-
currentSize += messageSize;
|
|
1833
|
-
}
|
|
1834
|
-
}
|
|
1835
|
-
if (currentChunk.length > 0) {
|
|
1836
|
-
chunks.push(currentChunk);
|
|
1837
|
-
}
|
|
1838
|
-
return chunks;
|
|
1839
|
-
};
|
|
1840
|
-
var sendChunkWithRetry = async (logger2, targetTopic, producer, messages, currentMaxSize, maxRetries = 3) => {
|
|
1841
|
-
const currentMessages = messages;
|
|
1842
|
-
let attempts = 0;
|
|
1843
|
-
while (attempts < maxRetries) {
|
|
1844
|
-
try {
|
|
1845
|
-
await producer.send({
|
|
1846
|
-
topic: targetTopic.name,
|
|
1847
|
-
messages: currentMessages
|
|
1848
|
-
});
|
|
1849
|
-
logger2.log(
|
|
1850
|
-
`Successfully sent ${currentMessages.length} messages to ${targetTopic.name}`
|
|
1851
|
-
);
|
|
1852
|
-
return;
|
|
1853
|
-
} catch (error) {
|
|
1854
|
-
if (isMessageTooLargeError(error) && currentMessages.length > 1) {
|
|
1855
|
-
logger2.warn(
|
|
1856
|
-
`Got MESSAGE_TOO_LARGE error, splitting batch of ${currentMessages.length} messages and retrying (${maxRetries - attempts} attempts left)`
|
|
1857
|
-
);
|
|
1858
|
-
const newMaxSize = Math.floor(currentMaxSize / 2);
|
|
1859
|
-
const splitChunks = splitBatch(currentMessages, newMaxSize);
|
|
1860
|
-
for (const chunk of splitChunks) {
|
|
1861
|
-
await sendChunkWithRetry(
|
|
1862
|
-
logger2,
|
|
1863
|
-
targetTopic,
|
|
1864
|
-
producer,
|
|
1865
|
-
chunk,
|
|
1866
|
-
newMaxSize,
|
|
1867
|
-
// this error does not count as one failed attempt
|
|
1868
|
-
maxRetries - attempts
|
|
1869
|
-
);
|
|
1870
|
-
}
|
|
1871
|
-
return;
|
|
1872
|
-
} else {
|
|
1873
|
-
attempts++;
|
|
1874
|
-
if (attempts >= maxRetries) {
|
|
1875
|
-
let messagesHandledByDLQ = 0;
|
|
1876
|
-
let messagesWithoutDLQ = 0;
|
|
1877
|
-
const dlqErrors = [];
|
|
1878
|
-
for (const failedMessage of currentMessages) {
|
|
1879
|
-
const dlqTopic = failedMessage.dlq;
|
|
1880
|
-
if (dlqTopic && failedMessage.originalValue) {
|
|
1881
|
-
const dlqTopicName = dlqTopic.name;
|
|
1882
|
-
const deadLetterRecord = {
|
|
1883
|
-
originalRecord: {
|
|
1884
|
-
...failedMessage.originalValue,
|
|
1885
|
-
// Include original Kafka message metadata
|
|
1886
|
-
__sourcePartition: failedMessage.originalMessage.partition,
|
|
1887
|
-
__sourceOffset: failedMessage.originalMessage.offset,
|
|
1888
|
-
__sourceTimestamp: failedMessage.originalMessage.timestamp
|
|
1889
|
-
},
|
|
1890
|
-
errorMessage: error instanceof Error ? error.message : String(error),
|
|
1891
|
-
errorType: error instanceof Error ? error.constructor.name : "Unknown",
|
|
1892
|
-
failedAt: /* @__PURE__ */ new Date(),
|
|
1893
|
-
source: "transform"
|
|
1894
|
-
};
|
|
1895
|
-
cliLog({
|
|
1896
|
-
action: "DeadLetter",
|
|
1897
|
-
message: `Sending failed message to DLQ ${dlqTopicName}: ${error instanceof Error ? error.message : String(error)}`,
|
|
1898
|
-
message_type: "Error"
|
|
1899
|
-
});
|
|
1900
|
-
try {
|
|
1901
|
-
await producer.send({
|
|
1902
|
-
topic: dlqTopicName,
|
|
1903
|
-
messages: [{ value: JSON.stringify(deadLetterRecord) }]
|
|
1904
|
-
});
|
|
1905
|
-
logger2.log(`Sent failed message to DLQ ${dlqTopicName}`);
|
|
1906
|
-
messagesHandledByDLQ++;
|
|
1907
|
-
} catch (dlqError) {
|
|
1908
|
-
const errorMsg = `Failed to send message to DLQ: ${dlqError}`;
|
|
1909
|
-
logger2.error(errorMsg);
|
|
1910
|
-
dlqErrors.push(errorMsg);
|
|
1911
|
-
}
|
|
1912
|
-
} else if (!dlqTopic) {
|
|
1913
|
-
messagesWithoutDLQ++;
|
|
1914
|
-
logger2.warn(
|
|
1915
|
-
`Cannot send to DLQ: no DLQ configured for message (batch has mixed DLQ configurations)`
|
|
1916
|
-
);
|
|
1917
|
-
} else {
|
|
1918
|
-
messagesWithoutDLQ++;
|
|
1919
|
-
logger2.warn(
|
|
1920
|
-
`Cannot send to DLQ: original message value not available`
|
|
1921
|
-
);
|
|
1922
|
-
}
|
|
1923
|
-
}
|
|
1924
|
-
const allMessagesHandled = messagesHandledByDLQ === currentMessages.length && messagesWithoutDLQ === 0 && dlqErrors.length === 0;
|
|
1925
|
-
if (allMessagesHandled) {
|
|
1926
|
-
logger2.log(
|
|
1927
|
-
`All ${messagesHandledByDLQ} failed message(s) sent to DLQ, not throwing original error`
|
|
1928
|
-
);
|
|
1929
|
-
return;
|
|
1930
|
-
}
|
|
1931
|
-
if (messagesWithoutDLQ > 0) {
|
|
1932
|
-
logger2.error(
|
|
1933
|
-
`Cannot handle batch failure: ${messagesWithoutDLQ} message(s) have no DLQ configured`
|
|
1934
|
-
);
|
|
1935
|
-
}
|
|
1936
|
-
if (dlqErrors.length > 0) {
|
|
1937
|
-
logger2.error(
|
|
1938
|
-
`Some messages failed to send to DLQ: ${dlqErrors.join(", ")}`
|
|
1939
|
-
);
|
|
1940
|
-
}
|
|
1941
|
-
if (messagesHandledByDLQ > 0) {
|
|
1942
|
-
logger2.warn(
|
|
1943
|
-
`Partial DLQ success: ${messagesHandledByDLQ}/${currentMessages.length} message(s) sent to DLQ, but throwing due to incomplete batch handling`
|
|
1944
|
-
);
|
|
1945
|
-
}
|
|
1946
|
-
throw error;
|
|
1947
|
-
}
|
|
1948
|
-
logger2.warn(
|
|
1949
|
-
`Send ${currentMessages.length} messages failed (attempt ${attempts}/${maxRetries}), retrying: ${error}`
|
|
1950
|
-
);
|
|
1951
|
-
await new Promise((resolve2) => setTimeout(resolve2, 100 * attempts));
|
|
1952
|
-
}
|
|
1953
|
-
}
|
|
1954
|
-
}
|
|
1955
|
-
};
|
|
1956
1822
|
var MAX_STREAMING_CONCURRENCY = process3.env.MAX_STREAMING_CONCURRENCY ? parseInt(process3.env.MAX_STREAMING_CONCURRENCY, 10) : DEFAULT_MAX_STREAMING_CONCURRENCY;
|
|
1957
1823
|
var metricsLog = (log) => {
|
|
1958
1824
|
const req = http3.request({
|
|
@@ -2098,56 +1964,71 @@ var handleMessage = async (logger2, streamingFunctionWithConfigList, message, pr
|
|
|
2098
1964
|
}
|
|
2099
1965
|
return void 0;
|
|
2100
1966
|
};
|
|
2101
|
-
var
|
|
2102
|
-
|
|
2103
|
-
|
|
2104
|
-
|
|
2105
|
-
|
|
2106
|
-
|
|
2107
|
-
|
|
2108
|
-
|
|
2109
|
-
|
|
2110
|
-
|
|
2111
|
-
|
|
2112
|
-
|
|
2113
|
-
|
|
2114
|
-
|
|
2115
|
-
|
|
2116
|
-
|
|
2117
|
-
|
|
2118
|
-
|
|
2119
|
-
|
|
2120
|
-
|
|
2121
|
-
)
|
|
2122
|
-
|
|
2123
|
-
|
|
2124
|
-
|
|
2125
|
-
|
|
2126
|
-
|
|
2127
|
-
|
|
1967
|
+
var handleDLQForFailedMessages = async (logger2, producer, messages, error) => {
|
|
1968
|
+
let messagesHandledByDLQ = 0;
|
|
1969
|
+
let messagesWithoutDLQ = 0;
|
|
1970
|
+
for (const msg of messages) {
|
|
1971
|
+
if (msg.dlq && msg.originalValue) {
|
|
1972
|
+
const deadLetterRecord = {
|
|
1973
|
+
originalRecord: {
|
|
1974
|
+
...msg.originalValue,
|
|
1975
|
+
// Include original Kafka message metadata
|
|
1976
|
+
__sourcePartition: msg.originalMessage.partition,
|
|
1977
|
+
__sourceOffset: msg.originalMessage.offset,
|
|
1978
|
+
__sourceTimestamp: msg.originalMessage.timestamp
|
|
1979
|
+
},
|
|
1980
|
+
errorMessage: error instanceof Error ? error.message : String(error),
|
|
1981
|
+
errorType: error instanceof Error ? error.constructor.name : "Unknown",
|
|
1982
|
+
failedAt: /* @__PURE__ */ new Date(),
|
|
1983
|
+
source: "transform"
|
|
1984
|
+
};
|
|
1985
|
+
cliLog({
|
|
1986
|
+
action: "DeadLetter",
|
|
1987
|
+
message: `Sending failed message to DLQ ${msg.dlq.name}: ${error instanceof Error ? error.message : String(error)}`,
|
|
1988
|
+
message_type: "Error"
|
|
1989
|
+
});
|
|
1990
|
+
try {
|
|
1991
|
+
await producer.send({
|
|
1992
|
+
topic: msg.dlq.name,
|
|
1993
|
+
messages: [{ value: JSON.stringify(deadLetterRecord) }]
|
|
1994
|
+
});
|
|
1995
|
+
logger2.log(`Sent failed message to DLQ ${msg.dlq.name}`);
|
|
1996
|
+
messagesHandledByDLQ++;
|
|
1997
|
+
} catch (dlqError) {
|
|
1998
|
+
logger2.error(`Failed to send to DLQ: ${dlqError}`);
|
|
2128
1999
|
}
|
|
2000
|
+
} else if (!msg.dlq) {
|
|
2001
|
+
messagesWithoutDLQ++;
|
|
2002
|
+
logger2.warn(`Cannot send to DLQ: no DLQ configured for message`);
|
|
2003
|
+
} else {
|
|
2004
|
+
messagesWithoutDLQ++;
|
|
2005
|
+
logger2.warn(`Cannot send to DLQ: original message value not available`);
|
|
2129
2006
|
}
|
|
2130
|
-
|
|
2131
|
-
|
|
2132
|
-
|
|
2133
|
-
|
|
2134
|
-
|
|
2135
|
-
|
|
2136
|
-
|
|
2137
|
-
|
|
2138
|
-
|
|
2139
|
-
|
|
2140
|
-
|
|
2141
|
-
|
|
2142
|
-
|
|
2143
|
-
|
|
2144
|
-
|
|
2145
|
-
|
|
2007
|
+
}
|
|
2008
|
+
if (messagesHandledByDLQ > 0 && messagesWithoutDLQ > 0) {
|
|
2009
|
+
logger2.warn(
|
|
2010
|
+
`Partial DLQ success: ${messagesHandledByDLQ}/${messages.length} message(s) sent to DLQ`
|
|
2011
|
+
);
|
|
2012
|
+
}
|
|
2013
|
+
};
|
|
2014
|
+
var sendMessages = async (logger2, metrics, targetTopic, producer, messages) => {
|
|
2015
|
+
if (messages.length === 0) return;
|
|
2016
|
+
for (const msg of messages) {
|
|
2017
|
+
metrics.bytes += Buffer2.byteLength(msg.value, "utf8");
|
|
2018
|
+
}
|
|
2019
|
+
metrics.count_out += messages.length;
|
|
2020
|
+
try {
|
|
2021
|
+
await producer.send({
|
|
2022
|
+
topic: targetTopic.name,
|
|
2023
|
+
messages
|
|
2024
|
+
});
|
|
2025
|
+
logger2.log(`Sent ${messages.length} messages to ${targetTopic.name}`);
|
|
2146
2026
|
} catch (e) {
|
|
2147
2027
|
logger2.error(`Failed to send transformed data`);
|
|
2148
2028
|
if (e instanceof Error) {
|
|
2149
2029
|
logError(logger2, e);
|
|
2150
2030
|
}
|
|
2031
|
+
await handleDLQForFailedMessages(logger2, producer, messages, e);
|
|
2151
2032
|
throw e;
|
|
2152
2033
|
}
|
|
2153
2034
|
};
|
|
@@ -2390,16 +2271,10 @@ var runStreamingFunctions = async (args) => {
|
|
|
2390
2271
|
fromBeginning: true
|
|
2391
2272
|
}
|
|
2392
2273
|
});
|
|
2393
|
-
const
|
|
2394
|
-
|
|
2395
|
-
|
|
2396
|
-
|
|
2397
|
-
retry: {
|
|
2398
|
-
retries: MAX_RETRIES_PRODUCER,
|
|
2399
|
-
maxRetryTime: MAX_RETRY_TIME_MS
|
|
2400
|
-
}
|
|
2401
|
-
}
|
|
2402
|
-
});
|
|
2274
|
+
const maxMessageBytes = args.targetTopic?.max_message_bytes || 1024 * 1024;
|
|
2275
|
+
const producer = kafka.producer(
|
|
2276
|
+
createProducerConfig(maxMessageBytes)
|
|
2277
|
+
);
|
|
2403
2278
|
try {
|
|
2404
2279
|
logger2.log("Starting producer...");
|
|
2405
2280
|
await startProducer(logger2, producer);
|