-
Notifications
You must be signed in to change notification settings - Fork 10
RFC-64 M1 4/7: add adaptive capacity primitives #2015
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
branarakic
wants to merge
9
commits into
codex/rfc64-m1-core-priority-scheduler
Choose a base branch
from
codex/rfc64-m1-adaptive-primitives
base: codex/rfc64-m1-core-priority-scheduler
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
078be07
feat(sync): add policy-owned adaptive admission
1c0e33f
feat(sync): adapt Core coverage breadth
1a1f00d
feat(sync): add adaptive capacity controller
fb66e5a
feat(sync): sample host and store capacity
e44e6d0
refactor(sync): reduce adaptive capacity transitions
dec30a7
fix(sync): clarify adaptive coverage contracts
2b24b2e
fix(sync): preserve normal store headroom
58b9656
test(sync): preserve health queue pressure
4376835
refactor(sync): derive adaptive capacity status state
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,153 @@ | ||
| import { availableParallelism, cpus } from 'node:os'; | ||
| import { performance, type EventLoopUtilization } from 'node:perf_hooks'; | ||
| import { memoryUsage } from 'node:process'; | ||
| import { getHeapStatistics } from 'node:v8'; | ||
| import type { StorePressureSnapshot, TripleStore } from '@origintrail-official/dkg-storage'; | ||
| import { | ||
| MAX_SYNC_ADAPTIVE_INFLIGHT, | ||
| type AdaptiveCapacitySample, | ||
| } from './adaptive-capacity.js'; | ||
|
|
||
| export interface CpuTimeSnapshot { | ||
| idle: number; | ||
| total: number; | ||
| } | ||
|
|
||
| export interface AdaptiveCapacitySamplerDependencies { | ||
| readCpuTimes?: () => CpuTimeSnapshot; | ||
| readEventLoopUtilization?: () => EventLoopUtilization; | ||
| eventLoopUtilizationDelta?: ( | ||
| current: EventLoopUtilization, | ||
| previous: EventLoopUtilization, | ||
| ) => number; | ||
| readHeapRatio?: () => number | undefined; | ||
| } | ||
|
|
||
| function defaultCpuTimes(): CpuTimeSnapshot { | ||
| let idle = 0; | ||
| let total = 0; | ||
| for (const cpu of cpus()) { | ||
| idle += cpu.times.idle; | ||
| total += Object.values(cpu.times).reduce((sum, value) => sum + value, 0); | ||
| } | ||
| return { idle, total }; | ||
| } | ||
|
|
||
| function intervalCpuUtilization( | ||
| current: CpuTimeSnapshot, | ||
| previous: CpuTimeSnapshot, | ||
| ): number | undefined { | ||
| const totalDelta = current.total - previous.total; | ||
| const idleDelta = current.idle - previous.idle; | ||
| if (totalDelta <= 0 || idleDelta < 0) return undefined; | ||
| return Math.max(0, Math.min(1, 1 - (idleDelta / totalDelta))); | ||
| } | ||
|
|
||
| function defaultHeapRatio(): number | undefined { | ||
| const heapLimit = getHeapStatistics().heap_size_limit; | ||
| if (!Number.isFinite(heapLimit) || heapLimit <= 0) return undefined; | ||
| return Math.max(0, Math.min(1, memoryUsage().heapUsed / heapLimit)); | ||
| } | ||
|
|
||
| function storeSample(store: TripleStore): AdaptiveCapacitySample['store'] { | ||
| let pressure: StorePressureSnapshot | undefined; | ||
| try { | ||
| pressure = store.getPressureSnapshot?.(); | ||
| } catch { | ||
| return { telemetryAvailable: false }; | ||
| } | ||
| if (!pressure) return { telemetryAvailable: false }; | ||
| return { | ||
| telemetryAvailable: true, | ||
| ackQueued: pressure.ackQueued, | ||
| healthQueued: pressure.healthQueued ?? 0, | ||
| normalQueued: pressure.normalQueued, | ||
| backgroundQueued: pressure.backgroundQueued, | ||
| }; | ||
| } | ||
|
|
||
| /** | ||
| * Node-local interval sampler. Ordinary full store utilization is deliberately | ||
| * not classified as saturation: critical store state requires queue-age or | ||
| * no-progress evidence that the current pressure API does not expose. | ||
| */ | ||
| export class AdaptiveCapacitySampler { | ||
| private readonly readCpuTimes: () => CpuTimeSnapshot; | ||
| private readonly readEventLoopUtilization: () => EventLoopUtilization; | ||
| private readonly eventLoopUtilizationDelta: ( | ||
| current: EventLoopUtilization, | ||
| previous: EventLoopUtilization, | ||
| ) => number; | ||
| private readonly readHeapRatio: () => number | undefined; | ||
| private previousCpuTimes: CpuTimeSnapshot; | ||
| private previousEventLoopUtilization: EventLoopUtilization; | ||
|
|
||
| constructor( | ||
| private readonly store: TripleStore, | ||
| dependencies: AdaptiveCapacitySamplerDependencies = {}, | ||
| ) { | ||
| this.readCpuTimes = dependencies.readCpuTimes ?? defaultCpuTimes; | ||
| this.readEventLoopUtilization = dependencies.readEventLoopUtilization | ||
| ?? (() => performance.eventLoopUtilization()); | ||
| this.eventLoopUtilizationDelta = dependencies.eventLoopUtilizationDelta | ||
| ?? ((current, previous) => performance.eventLoopUtilization(current, previous).utilization); | ||
| this.readHeapRatio = dependencies.readHeapRatio ?? defaultHeapRatio; | ||
| this.previousCpuTimes = this.readCpuTimes(); | ||
| this.previousEventLoopUtilization = this.readEventLoopUtilization(); | ||
| } | ||
|
|
||
| sample(demand: boolean): AdaptiveCapacitySample { | ||
| const currentCpuTimes = this.readCpuTimes(); | ||
| const cpuUtilization = intervalCpuUtilization(currentCpuTimes, this.previousCpuTimes); | ||
| this.previousCpuTimes = currentCpuTimes; | ||
|
|
||
| const currentEventLoopUtilization = this.readEventLoopUtilization(); | ||
| const eventLoopUtilization = this.eventLoopUtilizationDelta( | ||
| currentEventLoopUtilization, | ||
| this.previousEventLoopUtilization, | ||
| ); | ||
| this.previousEventLoopUtilization = currentEventLoopUtilization; | ||
|
|
||
| const heapRatio = this.readHeapRatio(); | ||
| return { | ||
| demand, | ||
| ...(cpuUtilization !== undefined ? { cpuUtilization } : {}), | ||
| ...(Number.isFinite(eventLoopUtilization) | ||
| ? { eventLoopUtilization: Math.max(0, Math.min(1, eventLoopUtilization)) } | ||
| : {}), | ||
| ...(heapRatio !== undefined ? { heapRatio } : {}), | ||
| store: storeSample(this.store), | ||
| }; | ||
| } | ||
| } | ||
|
|
||
| export interface AdaptiveInflightHardMaxInput { | ||
| operatorMax?: number; | ||
| parallelism?: number; | ||
| storePressure?: StorePressureSnapshot; | ||
| } | ||
|
|
||
| /** Resolve the largest requester cap the controller may ever reach. */ | ||
| export function deriveAdaptiveInflightHardMax( | ||
| input: AdaptiveInflightHardMaxInput = {}, | ||
| ): number { | ||
| const operatorMax = input.operatorMax ?? MAX_SYNC_ADAPTIVE_INFLIGHT; | ||
| const parallelism = input.parallelism ?? availableParallelism(); | ||
| if (!Number.isInteger(operatorMax) || operatorMax < 1) { | ||
| throw new TypeError('adaptive operator maximum must be a positive integer'); | ||
| } | ||
| if (!Number.isInteger(parallelism) || parallelism < 1) { | ||
| throw new TypeError('available parallelism must be a positive integer'); | ||
| } | ||
| const hardwareMax = Math.max(1, Math.floor(parallelism / 2)); | ||
| const storeMax = input.storePressure | ||
| ? Math.max( | ||
| 1, | ||
| input.storePressure.maxConcurrent | ||
|
branarakic marked this conversation as resolved.
|
||
| - input.storePressure.ackReservedSlots | ||
| - (input.storePressure.healthReservedSlots ?? 0) | ||
| - (input.storePressure.normalReservedSlots ?? 0), | ||
| ) | ||
| : MAX_SYNC_ADAPTIVE_INFLIGHT; | ||
| return Math.min(MAX_SYNC_ADAPTIVE_INFLIGHT, operatorMax, hardwareMax, storeMax); | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.