Skip to content

armgit / orca

Public Imported from GitHub
0

Orca is the place for working with a fleet of agents (e.g. Grok Bot, Claude Code). Run any coding agent with your own subscription. Available on desktop, mobile and remote runtime.

4.0 KB
Raw Blame
// Measure COMMIT-held lock durations to tune request waits. All sites feed shared max; labels identify holders, including drain-return regional rows as isolated-replacement.
export type CellLockHoldSite = 'inventory' | 'rehome-target-row' | 'isolated-replacement'

export type CellInventoryHoldCounts = {
  cellInventoryHoldMsMax: number
  cellInventoryHoldMsP95: number
  cellInventoryHolds: number
  cellInventoryHoldMaxSite: CellLockHoldSite | 'none'
  // The rehome commit holds one target row, not the inventory; rollout proof
  // reads its bound and its presence here.
  rehomeTargetRowHoldMsMax: number
  rehomeTargetRowHolds: number
  // Per-site p99: the shared max and p95 cannot say which lock a drain waits on.
  inventoryHoldMsP99: number
  rehomeTargetRowHoldMsP99: number
  isolatedReplacementHoldMsP99: number
  isolatedReplacementHolds: number
  // Failed acquisitions have no duration. Fail-fast counts sweep skips and initial request retries as pressure; expired bounded waits measure stalls separately.
  cellInventoryLockUnavailable: number
  cellInventoryLockTimeouts: number
}

// Bounded so a flush interval with heavy assignment traffic cannot grow the array
// without limit; the reservoir keeps the most recent holds.
const MAX_SAMPLES = 2_048

export function emptyCellInventoryHoldCounts(): CellInventoryHoldCounts {
  return {
    cellInventoryHoldMsMax: 0,
    cellInventoryHoldMsP95: 0,
    cellInventoryHolds: 0,
    cellInventoryHoldMaxSite: 'none',
    rehomeTargetRowHoldMsMax: 0,
    rehomeTargetRowHolds: 0,
    inventoryHoldMsP99: 0,
    rehomeTargetRowHoldMsP99: 0,
    isolatedReplacementHoldMsP99: 0,
    isolatedReplacementHolds: 0,
    cellInventoryLockUnavailable: 0,
    cellInventoryLockTimeouts: 0
  }
}

export class CellInventoryHoldSamples {
  private samples: { holdMs: number; site: CellLockHoldSite }[] = []
  private unavailable = 0
  private timeouts = 0

  record(holdMs: number, site: CellLockHoldSite = 'inventory'): void {
    if (!Number.isFinite(holdMs) || holdMs < 0) return
    if (this.samples.length === MAX_SAMPLES) this.samples.shift()
    this.samples.push({ holdMs, site })
  }

  // Counted, not sampled: a failed acquisition has no duration to record.
  recordUnavailable(count = 1): void {
    if (!Number.isFinite(count) || count <= 0) return
    this.unavailable += count
  }

  recordLockTimeout(count = 1): void {
    if (!Number.isFinite(count) || count <= 0) return
    this.timeouts += count
  }

  consumeCounts(): CellInventoryHoldCounts {
    const counts = this.readCounts()
    this.samples = []
    this.unavailable = 0
    this.timeouts = 0
    return counts
  }

  readCounts(): CellInventoryHoldCounts {
    const failures = {
      cellInventoryLockUnavailable: this.unavailable,
      cellInventoryLockTimeouts: this.timeouts
    }
    if (this.samples.length === 0) return { ...emptyCellInventoryHoldCounts(), ...failures }
    const sorted = [...this.samples].sort((left, right) => left.holdMs - right.holdMs)
    const max = sorted[sorted.length - 1]!
    const site = (name: CellLockHoldSite) => sorted.filter((sample) => sample.site === name)
    const rehome = site('rehome-target-row')
    const isolated = site('isolated-replacement')
    return {
      cellInventoryHoldMsMax: round(max.holdMs),
      cellInventoryHoldMsP95: nearestRank(sorted, 0.95),
      cellInventoryHolds: sorted.length,
      cellInventoryHoldMaxSite: max.site,
      rehomeTargetRowHoldMsMax: round(rehome[rehome.length - 1]?.holdMs ?? 0),
      rehomeTargetRowHolds: rehome.length,
      inventoryHoldMsP99: nearestRank(site('inventory'), 0.99),
      rehomeTargetRowHoldMsP99: nearestRank(rehome, 0.99),
      isolatedReplacementHoldMsP99: nearestRank(isolated, 0.99),
      isolatedReplacementHolds: isolated.length,
      ...failures
    }
  }
}

function nearestRank(sorted: { holdMs: number }[], rank: number): number {
  return round(sorted[Math.ceil(rank * sorted.length) - 1]?.holdMs ?? 0)
}

function round(value: number): number {
  return Number(value.toFixed(3))
}