Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
40 changes: 34 additions & 6 deletions worker/src/routes/__tests__/livy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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',
})
})

Expand Down Expand Up @@ -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 () => {
Expand Down Expand Up @@ -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')
})
})
35 changes: 32 additions & 3 deletions worker/src/routes/livy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,30 @@ 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`
}
// 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`
}

// 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.
Expand Down Expand Up @@ -361,14 +385,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<string, string> = {}
Expand All @@ -394,8 +423,8 @@ livy.post('/:id/livy/batches', async (c) => {
const livyPayload: Record<string, unknown> = {
file: file.file,
conf: { ...FIXED_LIVY_CONF, 'spark.cores.max': '2', ...userConf },
driverMemory: driverMemoryRaw,
executorMemory: executorMemoryRaw,
driverMemory,
executorMemory,
...dependencyArrays,
}
let className: string | null = null
Expand Down