Skip to content

fix(engine): use long affinity masks and limit workers to 64 - #3317

Merged
jfallows merged 2 commits into
developfrom
fix/3304-engine-default-affinity-mask
Nov 17, 2026
Merged

jfallows merged 2 commits into
developfrom
fix/3304-engine-default-affinity-mask

Conversation

@jfallows

Copy link
Copy Markdown
Contributor

Description

Tuning computed the default affinity mask as (1 << count) - 1 in int arithmetic. Java masks the shift distance to 5 bits, so 32, 64 and 96 workers gave mask 0 and EngineWorker.resolveAffinity threw affinity mask must specify at least one bit; 33..63 workers gave a truncated mask that silently pinned default-affinity bindings to fewer workers.

Changes:

  • Tuning: the available mask is a long: count >= Long.SIZE ? -1L : (1L << count) - 1L. Explicit masks using bits 31, 32 and 63 round-trip.
  • EngineManager: the same int-shift idiom in affinity &= ~(1 << bitindex) cleared the wrong bits from bit 31 upward; extracted limitAffinity and shifted a long. It was the only other instance in the engine (DefaultBudgetDebitor and the watcher loop already use 1L <<).
  • EngineConfiguration: worker indexes address bits of one long and index fixed 64-entry stream dispatch tables (EngineWorker.initDispatcher), so more than 64 workers cannot run (with -w 96, workers 64+ fail with ArrayIndexOutOfBoundsException when streams cross workers). The default is now min(availableProcessors, 64) and an explicit count above 64 is rejected at startup with zilla.engine.workers=N exceeds the maximum of 64 workers.

Fixes #3304

Tests first (failed before, pass after)

Before the fix, on unfixed code:

  • TuningTest: default mask 0 for 32 workers, mask 1 for 65 workers; explicit high-bit masks failed the assertion.
  • EngineManagerTest.shouldLimitAffinityToLowestBits: expected bit 63, was 0.
  • EngineAffinityIT.shouldPreferLocalWorkerForBindingWithDefaultAffinity (engine now runs 32 workers): IllegalStateException: affinity mask must specify at least one bit: test.net0 0.
  • EngineConfigurationTest.shouldRejectWorkersAboveLongSize: nothing was thrown.

After the fix: mvn clean verify -pl runtime/engine passes (347 unit tests, 268 ITs, checkstyle and jacoco check included).

Note: in one earlier full verify run on a 4-core sandbox, the JVM crashed (SIGSEGV in EventReader.read on an exporter thread, preceded by an ArrayIndexOutOfBoundsException: Index 19 out of bounds for length 10 in NamespaceTask) during EngineAffinityIT with its 32 workers. It did not reproduce in 3 isolated runs or in the next full run, but a 32-worker engine under load may be flaky; reviewers may want CI to confirm.

Real start

Unmodified examples/mqtt.kafka.proxy/etc/zilla.yaml, KAFKA_BOOTSTRAP_SERVER=localhost:9092 (Kafka unreachable), launched from a classpath built from this branch, not the Docker image:

start -e -w this branch develop
32 ENGINE_STARTED affinity mask must specify at least one bit, exit 1
33 ENGINE_STARTED wrong truncated mask 1 (unit test)
64 ENGINE_STARTED affinity mask must specify at least one bit, exit 1
65 rejected: exceeds the maximum of 64 workers, exit 1 n/a
96 rejected: exceeds the maximum of 64 workers, exit 1 affinity mask must specify at least one bit, exit 1
default ENGINE_STARTED n/a

Not delivered

  • Support for more than 64 workers: it would need a wider tuning file layout (a bit set per binding instead of one long), a dispatch table sized by worker count and a wider watcher mask. Follow-up if wanted.
  • The default clamp is covered by a unit test only; the sandbox has 4 cores, so a 65+ vCPU host was not exercised.
  • No documentation page for the 64-worker limit was changed.

Generated by Claude Code

Tuning computed the default affinity mask as (1 << count) - 1 in int
arithmetic, so a worker count of 32, 64 or 96 produced mask 0 (startup
failed with "affinity mask must specify at least one bit") and counts
33..63 produced a truncated mask. The mask is now a long: count bits for
counts below 64 and all 64 bits otherwise, matching the single long the
tuning file stores per binding.

EngineManager limited a binding's worker bits with ~(1 << bitindex), the
same int shift applied to a long mask, which cleared the wrong bits from
bit 31 upward. It now shifts a long, via a testable limitAffinity.

Fixes #3304

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01SiHVoWjcXhPwwz4JasA4BL
Worker indexes address bits of a single long (the tuning affinity mask)
and index fixed 64-entry stream dispatch tables, so an engine with more
than 64 workers cannot run: with 96 workers, workers 64 and above fail
with ArrayIndexOutOfBoundsException when streams cross workers.

The default worker count is now min(availableProcessors, 64) and an
explicit count above 64 is rejected with a clear message at startup.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01SiHVoWjcXhPwwz4JasA4BL
@jfallows
jfallows merged commit 9b2e38b into develop Nov 17, 2026
52 checks passed
@jfallows
jfallows deleted the fix/3304-engine-default-affinity-mask branch November 18, 2026 22:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

engine: Zilla fails to start with 32 workers (the default on a 32-vCPU host): "affinity mask must specify at least one bit"

2 participants