All files / packages/topic/src/writer writer-runtime.ts

93.02% Statements 80/86
85.71% Branches 54/63
94.11% Functions 16/17
93.02% Lines 80/86

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304                                                    15x     15x 15x 15x 15x 15x 15x 15x 15x                                                           15x 51394x   51137x   16x       108x   108x   25x         15x 16x 16x     15x 51394x   51137x   16x       108x   108x   25x       15x 51847x 51847x 173x 173x             15x 15x   15x 915x 915x   120x                   771x   1x   23x                           51121x             51121x   51121x                                           51121x                       51121x                           2238953x 2238953x     2238953x       51137x 51137x       51121x       1110x           1110x             1110x       11x           51394x   51394x       51394x   51394x 51394x 51394x   51394x 87x   18x 18x     51394x 51394x       453x       51121x     51121x   51121x 51203x   51121x   51121x 51121x 51121x           51121x   51121x   51121x   51121x    
import { create } from '@bufbuild/protobuf'
import {
	Codec,
	StreamWriteMessage_WriteRequestSchema,
	TransactionIdentitySchema,
} from '@ydbjs/api/topic'
import type { Driver } from '@ydbjs/core'
import { loggers } from '@ydbjs/debug'
import { type MachineRuntime, createMachineRuntime } from '@ydbjs/fsm'
 
import { type InitParams, WriterTransport } from './transport.js'
import type { TransportOutput } from './transport-state.js'
import type { TopicWriterOptions } from './types.js'
import {
	type GlobalTimerName,
	MAX_BATCH_BYTES,
	type WriterCtx,
	type WriterEffect,
	type WriterEvent,
	type WriterOutput,
	type WriterState,
	createWriterCtx,
	writerTransition,
} from './writer-state.js'
 
// Watchdog for a connect that never produces an init response.
let DEFAULT_START_TIMEOUT_MS = 30_000
// Single source of the option defaults — the facade imports these for its
// diagnostics config snapshot, so the published effective config cannot drift.
export const DEFAULT_MAX_BUFFER_BYTES = 256n * 1024n * 1024n
export const DEFAULT_MAX_INFLIGHT_COUNT = 1000
export const DEFAULT_FLUSH_INTERVAL_MS = 1000
export const DEFAULT_UPDATE_TOKEN_INTERVAL_MS = 60_000
export const DEFAULT_GRACEFUL_SHUTDOWN_TIMEOUT_MS = 30_000
export const DEFAULT_RECOVERY_WINDOW_MS = Infinity
let BACKOFF_BASE_MS = 50
let BACKOFF_MAX_MS = 30_000
 
// I/O handles and config — visible to effect handlers only, never to the transition.
type WriterEnv = {
	// I/O
	transport: WriterTransport
	codec: number
	txIdentity?: { id: string; session: string }
 
	// timer durations (ms)
	startTimeoutMs: number
	flushIntervalMs: number
	updateTokenIntervalMs: number
	gracefulShutdownTimeoutMs: number
 
	// lifecycle
	ac: AbortController
	closedDeferred: PromiseWithResolvers<void>
	isFinalized: boolean
	// Keyed by GlobalTimerName — string-keyed like the reader, whose keys also
	// carry a partition suffix (the writer has no partition timers).
	timers: Map<string, ReturnType<typeof setTimeout>>
}
 
type FullCtx = WriterCtx & WriterEnv
 
export type WriterRuntime = {
	machine: MachineRuntime<WriterState, WriterCtx, WriterEvent, WriterOutput>
}
 
let timerEvent = function timerEvent(which: GlobalTimerName): WriterEvent {
	switch (which) {
		case 'start_timeout':
			return { type: 'writer.timer.start_timeout' }
		case 'retry_backoff':
			return { type: 'writer.timer.retry_backoff' }
		case 'recovery_window':
			return { type: 'writer.timer.recovery_window' }
		case 'flush_tick':
			return { type: 'writer.timer.flush_tick' }
		case 'update_token':
			return { type: 'writer.timer.update_token' }
		case 'graceful_timeout':
			return { type: 'writer.timer.graceful_timeout' }
	}
}
 
// Equal-jitter exponential backoff: half fixed, half random in [d/2, d].
let backoffDelay = function backoffDelay(attempts: number): number {
	let capped = Math.min(BACKOFF_BASE_MS * 2 ** attempts, BACKOFF_MAX_MS)
	return Math.round(capped / 2 + Math.random() * (capped / 2))
}
 
let delayFor = function delayFor(ctx: FullCtx, which: GlobalTimerName): number {
	switch (which) {
		case 'start_timeout':
			return ctx.startTimeoutMs
		case 'retry_backoff':
			return backoffDelay(ctx.attempts)
		case 'recovery_window':
			return ctx.recoveryWindowMs
		case 'flush_tick':
			return ctx.flushIntervalMs
		case 'update_token':
			return ctx.updateTokenIntervalMs
		case 'graceful_timeout':
			return ctx.gracefulShutdownTimeoutMs
	}
}
 
let clearTimerByKey = function clearTimerByKey(ctx: FullCtx, key: string): void {
	let handle = ctx.timers.get(key)
	if (handle) {
		clearTimeout(handle)
		ctx.timers.delete(key)
	}
}
 
// `dbg` carries low-frequency lifecycle (connect, start); the per-event / per-batch
// trace goes to `evdbg` so enabling `ydb:topic:writer` doesn't flood with the
// per-message send/transition noise — opt into it with `ydb:topic:writer:event`.
let dbg = loggers.topic.extend('writer')
let evdbg = dbg.extend('event')
 
let mapTransportOutput = function mapTransportOutput(output: TransportOutput): WriterEvent | null {
	evdbg.log('transport → writer: %s', output.type)
	switch (output.type) {
		case 'transport.stream.init_response':
			return {
				type: 'writer.stream.init_response',
				sessionId: output.sessionId,
				lastSeqNo: output.lastSeqNo,
				...(output.partitionId !== undefined && { partitionId: output.partitionId }),
				...(output.supportedCodecs !== undefined && {
					supportedCodecs: output.supportedCodecs,
				}),
			}
		case 'transport.stream.write_response':
			return { type: 'writer.stream.write_response', acks: output.acks }
		case 'transport.stream.token_response':
			return { type: 'writer.stream.token_response' }
		case 'transport.stream.disconnected':
			return {
				type: 'writer.stream.disconnected',
				...('error' in output ? { error: output.error } : {}),
			}
		default:
			return null
	}
}
 
// Builds the writer FSM and binds its effects to real I/O: transport connect/send,
// timers with equal-jitter backoff, and transport-output → writer-event mapping.
// The returned machine is the only handle the facade drives; every side effect
// (sockets, timers, token refresh) fires from the effect handlers wired here.
export function createWriterRuntime(driver: Driver, options: TopicWriterOptions): WriterRuntime {
	let initParams: InitParams = {
		path: options.topic,
		producerId: options.producer!,
		...(options.partitionId !== undefined && { partitionId: options.partitionId }),
		...(options.messageGroupId !== undefined && { messageGroupId: options.messageGroupId }),
	}
 
	let transport = new WriterTransport(driver, initParams)
 
	let env: WriterEnv = {
		// I/O
		transport,
		codec: options.codec?.codec ?? Codec.RAW,
		...(options.tx && {
			txIdentity: { id: options.tx.transactionId, session: options.tx.sessionId },
		}),
 
		// timer durations (ms)
		startTimeoutMs: DEFAULT_START_TIMEOUT_MS,
		flushIntervalMs: options.flushIntervalMs ?? DEFAULT_FLUSH_INTERVAL_MS,
		updateTokenIntervalMs: options.updateTokenIntervalMs ?? DEFAULT_UPDATE_TOKEN_INTERVAL_MS,
		gracefulShutdownTimeoutMs:
			options.gracefulShutdownTimeoutMs ?? DEFAULT_GRACEFUL_SHUTDOWN_TIMEOUT_MS,
 
		// lifecycle
		ac: new AbortController(),
		closedDeferred: Promise.withResolvers<void>(),
		isFinalized: false,
		timers: new Map(),
	}
 
	let ctx = createWriterCtx(
		{
			maxInflightCount: options.maxInflightCount ?? DEFAULT_MAX_INFLIGHT_COUNT,
			maxBatchBytes: MAX_BATCH_BYTES,
		},
		{
			retryOnSchemeError: options.retryOnSchemeError ?? false,
			recoveryWindowMs: options.recoveryWindowMs ?? DEFAULT_RECOVERY_WINDOW_MS,
			codec: options.codec?.codec ?? Codec.RAW,
		}
	)
 
	let machine = createMachineRuntime<
		WriterState,
		WriterCtx,
		WriterEnv,
		WriterEvent,
		WriterEffect,
		WriterOutput
	>({
		initialState: 'idle',
		ctx,
		env,
		// Wrap the pure transition to log each event and state change without
		// putting side effects in the transition itself.
		transition: (fullCtx, event, runtime) => {
			let result = writerTransition(fullCtx, event, runtime)
			Iif (evdbg.enabled) {
				evdbg.log('%s: %s → %s', event.type, runtime.state, result?.state ?? runtime.state)
			}
			return result
		},
		effects: {
			'writer.effect.transport.connect': (fullCtx, effect) => {
				dbg.log('connect (getLastSeqNo=%s)', effect.getLastSeqNo)
				fullCtx.transport.connect(effect.getLastSeqNo)
			},
 
			'writer.effect.transport.close': (fullCtx) => {
				fullCtx.transport.close()
			},
 
			'writer.effect.send.write_request': (fullCtx, effect) => {
				evdbg.log(
					'send write_request: %d messages (seqNo %s..%s)',
					effect.messages.length,
					effect.messages[0]?.seqNo,
					effect.messages[effect.messages.length - 1]?.seqNo
				)
				let request = create(StreamWriteMessage_WriteRequestSchema, {
					codec: fullCtx.codec,
					messages: effect.messages,
					...(fullCtx.txIdentity && {
						tx: create(TransactionIdentitySchema, fullCtx.txIdentity),
					}),
				})
				fullCtx.transport.sendBatch(request)
			},
 
			'writer.effect.send.update_token': (fullCtx) => {
				void fullCtx.transport.sendUpdateToken().catch(() => {
					// Token refresh failures surface as a stream error on the next write.
				})
			},
 
			'writer.effect.timer.schedule': (fullCtx, effect, runtime) => {
				let which = effect.which
				// The recovery window is armed once per reconnect saga.
				Iif (which === 'recovery_window' && fullCtx.timers.has('recovery_window')) {
					return
				}
 
				clearTimerByKey(fullCtx, which)
 
				let repeating = which === 'flush_tick' || which === 'update_token'
				let delay = delayFor(fullCtx, which)
				let event = timerEvent(which)
 
				let handle = repeating
					? setInterval(() => runtime.dispatch(event), delay)
					: setTimeout(() => {
							fullCtx.timers.delete(which)
							runtime.dispatch(event)
						}, delay)
 
				handle.unref?.()
				fullCtx.timers.set(which, handle)
			},
 
			'writer.effect.timer.clear': (fullCtx, effect) => {
				clearTimerByKey(fullCtx, effect.which)
			},
 
			'writer.effect.finalize': (fullCtx, effect) => {
				Iif (fullCtx.isFinalized) {
					return
				}
				fullCtx.isFinalized = true
 
				for (let handle of fullCtx.timers.values()) {
					clearTimeout(handle)
				}
				fullCtx.timers.clear()
 
				fullCtx.transport.destroy(effect.reason)
				fullCtx.ac.abort(effect.reason)
				fullCtx.closedDeferred.resolve()
			},
		},
	})
 
	// Route transport lifecycle events into the writer FSM.
	machine.ingest(transport.events, mapTransportOutput)
 
	dbg.log('starting writer on %s (producer=%s)', options.topic, options.producer)
 
	machine.dispatch({ type: 'writer.start' })
 
	return { machine }
}