From 449eeef6741c310db7c88c994967c4a0f2d6274e Mon Sep 17 00:00:00 2001 From: mapxn <16299874+mapxn@users.noreply.github.com> Date: Sun, 29 Nov 2026 19:20:23 +0800 Subject: [PATCH 1/2] chore: checkpoint KG0lN2E5ljGuxPcxxNQxn --- worker/src/routes/__tests__/livy.test.ts | 40 ++++++++++++++++++++---- worker/src/routes/livy.ts | 34 ++++++++++++++++++-- 2 files changed, 65 insertions(+), 9 deletions(-) diff --git a/worker/src/routes/__tests__/livy.test.ts b/worker/src/routes/__tests__/livy.test.ts index 35cc706..f42ed1b 100644 --- a/worker/src/routes/__tests__/livy.test.ts +++ b/worker/src/routes/__tests__/livy.test.ts @@ -2,7 +2,13 @@ import { afterEach, beforeEach, describe, expect, it } from 'vitest' import { getMigrations } from '../../lib/migrations' import { sealSecret } from '../../lib/secrets' import type { Env } from '../../types' -import { DEFAULT_LIVY_JARS_DIR, extractAppIdFromDriverLog, livyJarsDir, resolveSparkFile } from '../livy' +import { + DEFAULT_LIVY_JARS_DIR, + extractAppIdFromDriverLog, + livyJarsDir, + normalizeMemory, + resolveSparkFile, +} from '../livy' import { bearer, callWorker, fakeEnv, postJson } from './fakes' import { sqliteD1 } from './sqlite-d1' @@ -248,8 +254,8 @@ describe('Livy Batches API', () => { 'spark.hadoop.hive.metastore.schema.verification': 'false', 'spark.cores.max': '2', }, - driverMemory: '1g', - executorMemory: '1g', + driverMemory: '1024m', + executorMemory: '1024m', }) // Verify database row was inserted into livy_batches @@ -695,8 +701,8 @@ describe('Livy Batches API', () => { files: ['hdfs:///data/input.csv'], pyFiles: ['hdfs:///py/helper.py'], archives: ['hdfs:///arch/extra.zip'], - driverMemory: '1g', - executorMemory: '1g', + driverMemory: '1024m', + executorMemory: '1024m', }) }) @@ -732,7 +738,7 @@ describe('Livy Batches API', () => { postJson({ file: 'hdfs:///app.jar', executorMemory: '1.5g' }, { headers: u1Headers }) ) expect(okRes.status).toBe(201) - expect(capturedCalls[capturedCalls.length - 1]!.body).toMatchObject({ executorMemory: '1.5g' }) + expect(capturedCalls[capturedCalls.length - 1]!.body).toMatchObject({ executorMemory: '1536m' }) }) it('POST /batches rejects malformed driverMemory/executorMemory', async () => { @@ -1294,3 +1300,25 @@ describe('resolveSparkFile / livyJarsDir', () => { expect(livyJarsDir({ LIVY_JARS_DIR: '/team/jars' } as Env)).toBe('/team/jars') }) }) + +describe('normalizeMemory', () => { + it('scales decimal sizes down to a whole integer unit', () => { + expect(normalizeMemory('1.5g')).toBe('1536m') + expect(normalizeMemory('1.5gb')).toBe('1536m') + expect(normalizeMemory('0.5g')).toBe('512m') + }) + + it('normalises whole values to integer MB', () => { + expect(normalizeMemory('1g')).toBe('1024m') + expect(normalizeMemory('1536m')).toBe('1536m') + expect(normalizeMemory('512m')).toBe('512m') + expect(normalizeMemory('1t')).toBe('1048576m') + }) + + it('preserves sub-megabyte sizes as integer KB or bytes', () => { + expect(normalizeMemory('512k')).toBe('512k') + expect(normalizeMemory('1kb')).toBe('1k') + expect(normalizeMemory('1024b')).toBe('1k') + expect(normalizeMemory('1b')).toBe('1b') + }) +}) diff --git a/worker/src/routes/livy.ts b/worker/src/routes/livy.ts index 7fbbbd0..89f6946 100644 --- a/worker/src/routes/livy.ts +++ b/worker/src/routes/livy.ts @@ -102,6 +102,29 @@ function parseMemoryToBytes(value: string): number | null { return number * multiplier } +// Normalises a memory size that has already passed parseMemoryToBytes into an +// integer-unit string Spark 3.5.3's spark-submit accepts. Decimal strings are +// scaled down to a whole unit (e.g. '1.5g' -> '1536m'); whole-megabyte values +// become integer MB, the most compatible form. Values below 1 MB fall back to +// integer KB, then integer bytes, preserving the numeric size exactly. +export function normalizeMemory(value: string): string { + const bytes = parseMemoryToBytes(value) + if (bytes === null) return value.trim() + + if (bytes >= 1024 ** 2 && bytes % 1024 ** 2 === 0) { + return `${bytes / 1024 ** 2}m` + } + if (bytes >= 1024 && bytes % 1024 === 0) { + return `${bytes / 1024}k` + } + if (Number.isInteger(bytes)) { + return `${bytes}b` + } + // Only reachable for sub-byte decimal inputs; round up so the emitted size + // stays a positive whole number rather than a decimal string. + return `${Math.max(1, Math.round(bytes))}b` +} + // Terminal states the single-flight gate and the web UI agree on. Any other Livy state // (starting, running, idle, busy, recovering, not_started, …) is in-flight and blocks a // new submission for the same database. @@ -361,14 +384,19 @@ livy.post('/:id/livy/batches', async (c) => { if (typeof driverMemoryRaw !== 'string' || parseMemoryToBytes(driverMemoryRaw) === null) { return c.json({ error: 'driverMemory must be a memory size such as "1g"' }, 400) } + const driverMemory = normalizeMemory(driverMemoryRaw) const executorMemoryRaw = body.executorMemory === undefined ? '1g' : body.executorMemory - const executorMemoryBytes = typeof executorMemoryRaw === 'string' ? parseMemoryToBytes(executorMemoryRaw) : null + if (typeof executorMemoryRaw !== 'string') { + return c.json({ error: 'executorMemory must be a memory size such as "1g"' }, 400) + } + const executorMemoryBytes = parseMemoryToBytes(executorMemoryRaw) if (executorMemoryBytes === null) { return c.json({ error: 'executorMemory must be a memory size such as "1g"' }, 400) } if (executorMemoryBytes > MAX_EXECUTOR_MEMORY_BYTES) { return c.json({ error: 'executorMemory exceeds the 1.5g limit' }, 400) } + const executorMemory = normalizeMemory(executorMemoryRaw) // Extra conf section: non-reserved key/value pairs are merged after the platform fixed conf. const userConf: Record = {} @@ -394,8 +422,8 @@ livy.post('/:id/livy/batches', async (c) => { const livyPayload: Record = { file: file.file, conf: { ...FIXED_LIVY_CONF, 'spark.cores.max': '2', ...userConf }, - driverMemory: driverMemoryRaw, - executorMemory: executorMemoryRaw, + driverMemory, + executorMemory, ...dependencyArrays, } let className: string | null = null From 811d6ab1a8a660851a7f086b64153798cb2477fc Mon Sep 17 00:00:00 2001 From: WorkerHub Date: Sun, 29 Nov 2026 19:27:46 +0800 Subject: [PATCH 2/2] docs(livy): clarify non-whole-byte normalization fallback comment --- worker/src/routes/livy.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/worker/src/routes/livy.ts b/worker/src/routes/livy.ts index 89f6946..a006cb0 100644 --- a/worker/src/routes/livy.ts +++ b/worker/src/routes/livy.ts @@ -120,8 +120,9 @@ export function normalizeMemory(value: string): string { if (Number.isInteger(bytes)) { return `${bytes}b` } - // Only reachable for sub-byte decimal inputs; round up so the emitted size - // stays a positive whole number rather than a decimal string. + // Fallback for any non-whole-byte size (e.g. '1.1g'): round to the nearest + // whole byte and keep it positive, so the emitted size is an integer string + // Spark accepts rather than a decimal. return `${Math.max(1, Math.round(bytes))}b` }