Skip to content
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,18 @@ volumes:
cache-data:
```

## Resource limits

All optional.

| Variable | Default | Effect |
| ------------------------------------- | -------- | ------------------------------------------------------------------------------------------------------------------------------------------------- |
| `STORAGE_S3_MAX_SOCKETS` | `50` | Sockets per S3 data pool, for `http://` and `https://` endpoints alike. Reads (GetObject) and writes (uploads, Merges) each get a pool this size. |
| `STORAGE_S3_CONTROL_MAX_SOCKETS` | `16` | Sockets for HEAD, LIST and DELETE requests (cache lookups, cleanup), kept apart from bulk transfers. |
| `STORAGE_S3_CONTROL_QUEUE_TIMEOUT_MS` | `5000` | How long a control request waits for a socket and connection before failing; `0` waits forever. |
| `MAX_CONCURRENT_MERGES` | `16` | Merges one process runs at once. Beyond it, restores stream the parts and a later download merges. |
| `DOWNLOAD_IDLE_TIMEOUT_MS` | `300000` | Closes a download whose client has not read a byte for this long; `0` disables. |

## Documentation

👉 <https://gha-cache-server.falcondev.io/getting-started> 👈
252 changes: 251 additions & 1 deletion lib/metrics.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import type { Agent, ClientRequest } from 'node:http'
import { performance } from 'node:perf_hooks'
import { sql } from 'kysely'
import { collectDefaultMetrics, Counter, Gauge, Registry } from 'prom-client'
import { collectDefaultMetrics, Counter, Gauge, Histogram, Registry } from 'prom-client'
import { getDatabase } from './db'

// Per-process registry. Correct for the shipped single-worker-per-container
Expand Down Expand Up @@ -42,3 +44,251 @@ export const cacheStorageBytes = new Gauge({
cacheStorageBytes.set(Number(bytes))
},
})

const SECONDS_BUCKETS = [0.005, 0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1, 2.5, 5, 10, 30, 60]

export const cacheLookupDurationSeconds = new Histogram({
name: 'cache_lookup_duration_seconds',
help: 'Download-URL lookup handling time by result, including the storage existence check.',
labelNames: ['result'],
buckets: SECONDS_BUCKETS,
registers: [metricsRegistry],
})

export const cacheDownloadsTotal = new Counter({
name: 'cache_downloads_total',
help: 'Proxied /download requests by outcome. client_gone: the client hung up before the body finished; idle_timeout: no bytes moved for DOWNLOAD_IDLE_TIMEOUT_MS.',
labelNames: ['result'],
registers: [metricsRegistry],
})
for (const result of ['completed', 'client_gone', 'idle_timeout', 'not_found', 'error'])
cacheDownloadsTotal.inc({ result }, 0)

export type MergeKind = 'stream' | 'compose'

export const cacheMergesTotal = new Counter({
name: 'cache_merges_total',
help: 'Finished Merges by kind (stream: parts piped through this process; compose: server-side copy) and result.',
labelNames: ['kind', 'result'],
registers: [metricsRegistry],
})

export const cacheMergesSkippedTotal = new Counter({
name: 'cache_merges_skipped_total',
help: 'Merges not started because this process already runs MAX_CONCURRENT_MERGES; a later download retries them.',
registers: [metricsRegistry],
})

export const cacheMergeDurationSeconds = new Histogram({
name: 'cache_merge_duration_seconds',
help: 'Wall time of finished Merges by kind and result.',
labelNames: ['kind', 'result'],
buckets: [1, 5, 15, 30, 60, 120, 300, 600, 1800, 3600],
registers: [metricsRegistry],
})

const mergeStartTimes = new Set<{ startedAt: number }>()

export const cacheMergesInProgress = new Gauge({
name: 'cache_merges_in_progress',
help: 'Merges running now in this process.',
registers: [metricsRegistry],
collect() {
cacheMergesInProgress.set(mergeStartTimes.size)
},
})

export const cacheMergeOldestAgeSeconds = new Gauge({
name: 'cache_merge_oldest_age_seconds',
help: 'Age of the longest-running in-progress Merge; 0 when none is running.',
registers: [metricsRegistry],
collect() {
let oldest = Infinity
for (const { startedAt } of mergeStartTimes) oldest = Math.min(oldest, startedAt)
cacheMergeOldestAgeSeconds.set(oldest === Infinity ? 0 : (performance.now() - oldest) / 1000)
},
})

/** Call when a Merge starts; call the returned function exactly once when it settles. */
export function trackMerge(kind: MergeKind) {
const entry = { startedAt: performance.now() }
mergeStartTimes.add(entry)
return (result: 'success' | 'failure') => {
if (!mergeStartTimes.delete(entry)) return
cacheMergesTotal.inc({ kind, result })
cacheMergeDurationSeconds.observe(
{ kind, result },
(performance.now() - entry.startedAt) / 1000,
)
}
}

// Lease rows, computed at scrape time. Expired rows mean a holder died or stopped
// renewing; a growing active count with no finished Merges means Merges are stuck.
export const cacheLeases = new Gauge({
name: 'cache_leases',
help: 'Merge and Storage Reader Lease rows by state (active: not yet expired).',
labelNames: ['type', 'state'],
registers: [metricsRegistry],
async collect() {
const db = await getDatabase()
const now = Date.now()
const count = (table: 'merge_leases' | 'storage_reader_leases') =>
db
.selectFrom(table)
.select([
sql<number>`count(*)`.as('total'),
sql<number>`coalesce(sum(case when ${sql.ref('expiresAt')} < ${now} then 1 else 0 end), 0)`.as(
'expired',
),
])
.executeTakeFirstOrThrow()
const [merge, reader] = await Promise.all([
count('merge_leases'),
count('storage_reader_leases'),
])
for (const [type, row] of [
['merge', merge],
['reader', reader],
] as const) {
const expired = Number(row.expired)
cacheLeases.set({ type, state: 'expired' }, expired)
cacheLeases.set({ type, state: 'active' }, Number(row.total) - expired)
}
},
})

// --- S3 client ---------------------------------------------------------------

/** reads: GetObject bodies; writes: uploads and Merges; control: HEAD/LIST/DELETE. */
export type S3Pool = 'reads' | 'writes' | 'control'

const s3Agents: { pool: S3Pool; agent: Agent }[] = []

export const s3HttpSockets = new Gauge({
name: 's3_http_sockets',
help: 'S3 agent sockets by pool and state (active: assigned to a request, idle: kept alive). Counts the agent view; a socket the remote end already closed still reads active until it is read or destroyed.',
labelNames: ['pool', 'state'],
registers: [metricsRegistry],
collect() {
const counts = new Map<S3Pool, { active: number; idle: number }>()
for (const { pool, agent } of s3Agents) {
const count = counts.get(pool) ?? { active: 0, idle: 0 }
for (const sockets of Object.values(agent.sockets)) count.active += sockets?.length ?? 0
for (const sockets of Object.values(agent.freeSockets)) count.idle += sockets?.length ?? 0
counts.set(pool, count)
}
for (const [pool, { active, idle }] of counts) {
s3HttpSockets.set({ pool, state: 'active' }, active)
s3HttpSockets.set({ pool, state: 'idle' }, idle)
}
},
})

export const s3HttpQueuedRequests = new Gauge({
name: 's3_http_queued_requests',
help: 'Live S3 requests waiting for a free agent socket, by pool (maxSockets reached). Sustained > 0 means pool starvation.',
labelNames: ['pool'],
registers: [metricsRegistry],
collect() {
const counts = new Map<S3Pool, number>()
for (const { pool, agent } of s3Agents) {
let queued = counts.get(pool) ?? 0
// Aborted requests stay in the queue until a socket frees; Node then skips them.
for (const requests of Object.values(agent.requests))
queued += requests?.filter((req) => !req.destroyed).length ?? 0
counts.set(pool, queued)
}
for (const [pool, queued] of counts) s3HttpQueuedRequests.set({ pool }, queued)
},
})

export const s3HttpMaxSockets = new Gauge({
name: 's3_http_max_sockets',
help: 'Configured S3 agent socket limit by pool (STORAGE_S3_MAX_SOCKETS, STORAGE_S3_CONTROL_MAX_SOCKETS).',
labelNames: ['pool'],
registers: [metricsRegistry],
})

export const s3SocketWaitSeconds = new Histogram({
name: 's3_socket_wait_seconds',
help: 'Time an S3 request waited for an agent socket, by pool. Requests aborted while queued are not observed; pair with s3_http_queued_requests.',
labelNames: ['pool'],
buckets: [0.001, 0.005, 0.01, 0.05, 0.1, 0.5, 1, 5, 10, 30, 60],
registers: [metricsRegistry],
})

/** Exposes `agent`'s pool on /metrics and times how long each request waits for a socket. */
export function instrumentS3Agent(pool: S3Pool, agent: Agent) {
s3Agents.push({ pool, agent })
s3HttpMaxSockets.set({ pool }, agent.maxSockets)
// `addRequest` is where Node's Agent either hands out a socket or queues the
// request; it is undocumented but the hook every agent wrapper relies on.
const pooled = agent as Agent & { addRequest: (req: ClientRequest, options: unknown) => void }
const addRequest = pooled.addRequest.bind(agent)
pooled.addRequest = (req, options) => {
const queuedAt = performance.now()
req.once('socket', () =>
s3SocketWaitSeconds.observe({ pool }, (performance.now() - queuedAt) / 1000),
)
addRequest(req, options)
}
}

export const s3RequestsTotal = new Counter({
name: 's3_requests_total',
help: 'S3 commands by outcome (ok, not_found, aborted, timeout, network, error).',
labelNames: ['command', 'outcome'],
registers: [metricsRegistry],
})

export const s3RequestDurationSeconds = new Histogram({
name: 's3_request_duration_seconds',
help: 'S3 command time until the response headers, including socket wait and SDK retries; excludes streaming a GetObject body.',
labelNames: ['command'],
buckets: SECONDS_BUCKETS,
registers: [metricsRegistry],
})

export const s3RequestRetriesTotal = new Counter({
name: 's3_request_retries_total',
help: 'SDK retry attempts beyond the first, by command.',
labelNames: ['command'],
registers: [metricsRegistry],
})

const S3_NETWORK_ERROR_CODES: Record<string, true> = {
ECONNRESET: true,
ECONNREFUSED: true,
EPIPE: true,
ENOTFOUND: true,
EAI_AGAIN: true,
}

function s3Outcome(err: unknown) {
const { name, code, $metadata } = (err ?? {}) as {
name?: string
code?: string
$metadata?: { httpStatusCode?: number }
}
if (name === 'AbortError') return 'aborted'
// The SDK renames ECONNRESET/EPIPE to TimeoutError, so check the code first.
if (S3_NETWORK_ERROR_CODES[code ?? '']) return 'network'
if (name === 'TimeoutError' || name === 'RequestTimeout' || code === 'ETIMEDOUT') return 'timeout'
if (name === 'NotFound' || name === 'NoSuchKey' || $metadata?.httpStatusCode === 404)
return 'not_found'
return 'error'
}

/** Records one S3 command: `err` is the thrown error, absent on success. */
export function observeS3Request(
commandName: string,
startedAt: number,
attempts: number | undefined,
err?: unknown,
) {
const command = commandName.replace(/Command$/, '')
s3RequestDurationSeconds.observe({ command }, (performance.now() - startedAt) / 1000)
s3RequestsTotal.inc({ command, outcome: err === undefined ? 'ok' : s3Outcome(err) })
if (attempts && attempts > 1) s3RequestRetriesTotal.inc({ command }, attempts - 1)
}
5 changes: 5 additions & 0 deletions lib/schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ export const envStorageDriverSchema = type.or(
'STORAGE_DRIVER': type.unit('s3'),
'STORAGE_S3_BUCKET': 'string',
'STORAGE_S3_SOCKET_TIMEOUT_MS': 'number.integer >= 0 = 10000',
'STORAGE_S3_MAX_SOCKETS': 'number.integer >= 1 = 50',
'STORAGE_S3_CONTROL_MAX_SOCKETS': 'number.integer >= 1 = 16',
'STORAGE_S3_CONTROL_QUEUE_TIMEOUT_MS': 'number.integer >= 0 = 5000',
'AWS_REGION': "string = 'us-east-1'",
'AWS_ENDPOINT_URL?': 'string.url',
'AWS_ACCESS_KEY_ID?': 'string',
Expand Down Expand Up @@ -84,6 +87,8 @@ export const envBaseSchema = type({
'DEBUG?': 'unknown',
'ENABLE_DIRECT_DOWNLOADS': 'boolean = false',
'EAGER_MERGE': 'boolean = false',
'MAX_CONCURRENT_MERGES': 'number.integer >= 1 = 16',
'DOWNLOAD_IDLE_TIMEOUT_MS': 'number.integer >= 0 = 300000',
'BENCHMARK': 'boolean = false',
'SKIP_TOKEN_VALIDATION': 'boolean = false',
'MANAGEMENT_API_KEY?': 'string',
Expand Down
Loading
Loading