Skip to content

Add hash collision test for NodeType ID generation#4815

Open
tmchow wants to merge 3 commits intoarmadaproject:masterfrom
tmchow:test/node-type-hash-collision
Open

Add hash collision test for NodeType ID generation#4815
tmchow wants to merge 3 commits intoarmadaproject:masterfrom
tmchow:test/node-type-hash-collision

Conversation

@tmchow
Copy link
Copy Markdown

@tmchow tmchow commented Apr 4, 2026

Adds tests for nodeTypeIdFromTaintsAndLabels that verify:

  1. No hash collisions across realistic Kubernetes taint/label combinations (taint keys x effects, label keys x values, mixed taint+label inputs)
  2. The hash is never zero, even for empty inputs

Tests 60+ unique inputs with zero collisions observed. Removes the TODO comment that requested this coverage.

Closes #4778

This PR was authored by a human with AI coding assistance.

Compound Engineering

@greptile-apps
Copy link
Copy Markdown
Contributor

greptile-apps bot commented Apr 4, 2026

Greptile Summary

This PR delivers two improvements: hash collision tests for nodeTypeIdFromTaintsAndLabels and a migration of all plain-string context keys to a typed ctxkeys.ContextKey type.

Context key migration (ctxkeys, auth, armadacontext, grpc, requestid, submit_test): A new internal/common/ctxkeys leaf package centralises three typed constants — PrincipalKey, UserKey, RequestIDKey. Because Go context lookups are type-sensitive, ctx.Value(ctxkeys.PrincipalKey) and ctx.Value(\"principal\") are distinct; the migration correctly prevents key collisions between packages. All five write/read sites are updated and a codebase-wide grep confirms no plain-string usages remain. The submit_test.go fixes are a required consequence of the key-type change — without them auth.GetPrincipal would silently return anonymousPrincipal in those tests.

Hash collision tests (node_type.go, node_type_test.go): TestNodeTypeIdFromTaintsAndLabels_NoCollisions exercises 88+ unique inputs across single-taint, single-label, unset-label, and mixed categories using real Kubernetes taint/label names. TestNodeTypeIdFromTaintsAndLabels_NeverEmpty confirms the FNV-1a offset basis prevents a zero hash on empty input. The stale TODO comment in node_type.go is correctly removed.

  • Minor: the mixed-input loop omits the assert.NotEqual(0) zero-hash check present in all three other loops — inconsistent coverage, though not a practical correctness risk given FNV-1a's properties.

Confidence Score: 5/5

Safe to merge — no P0 or P1 issues found; context key migration is complete and consistent across the codebase

All remaining findings are P2 (a missing zero-hash assertion in one test loop). The typed-key migration is mechanically complete with no stray plain-string lookups remaining, and the hash collision tests provide solid coverage of realistic Kubernetes inputs.

node_type_test.go — minor: mixed section missing zero-hash assert for consistency

Important Files Changed

Filename Overview
internal/common/ctxkeys/ctxkeys.go New leaf package defining typed ContextKey constants; clean design with no internal imports
internal/common/auth/common.go Uses package-local alias principalKey = ctxkeys.PrincipalKey and migrates UserKey write site; correct and complete
internal/common/armadacontext/armada_context.go ctx.Value reads updated to typed UserKey and RequestIDKey; consistent with write sites
internal/common/grpc/grpc.go InterceptorLogger updated to read UserKey and RequestIDKey from context; matches writers
internal/common/requestid/interceptors.go AddToIncomingContext now stores RequestIDKey with typed key; consistent with grpc.go reader
internal/scheduler/internaltypes/node_type.go Removes stale TODO comment requesting collision test coverage — now addressed by the new tests
internal/scheduler/internaltypes/node_type_test.go Adds comprehensive collision and non-zero hash tests; mixed section omits the zero-hash assertion present in all other sections (P2)
internal/server/submit/submit_test.go Updates 4 test helpers from plain string 'principal' to ctxkeys.PrincipalKey; required fix to prevent silent auth fallback after key-type migration

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart TD
    A["Input: taints, labels, unsetIndexedLabels"] --> B["h = fnv1a.Init64\n(offset basis: 14695981039346656037)"]
    B --> C{"for each taint\n(sorted by Key)"}
    C --> D["AddString64(key + '=' + value + ':' + effect + '$')"]
    D --> C
    C -- done --> E["AddString64('&')\nsection separator"]
    E --> F{"for each label key\n(sorted)"}
    F --> G["AddString64(key + '=' + value + '$')"]
    G --> F
    F -- done --> H["AddString64('&')\nsection separator"]
    H --> I{"for each unset label key\n(sorted)"}
    I --> J["AddString64(key + '$')"]
    J --> I
    I -- done --> K["return h (uint64 node type ID)"]
Loading

Reviews (2): Last reviewed commit: "Fix submit_test.go to use typed context ..." | Re-trigger Greptile

tmchow added 3 commits April 4, 2026 20:56
Introduce a ctxkeys package with a custom ContextKey type to prevent
context value collisions from bare string keys. Replace all plain
string context keys ("principal", "user", "requestId") with typed
constants from the new package.

The ctxkeys package is a leaf dependency with no imports, avoiding
circular dependencies between auth, requestid, and armadacontext.

Closes armadaproject#4780

Signed-off-by: Trevin Chow <trevin@trevinchow.com>
Add tests verifying that nodeTypeIdFromTaintsAndLabels produces unique
hashes for distinct combinations of taints, labels, and unset indexed
labels using realistic Kubernetes inputs. Also verify the hash is
never zero, even for empty inputs.

Removes the TODO comment that requested these tests.

Closes armadaproject#4778

Signed-off-by: Trevin Chow <trevin@trevinchow.com>
Update four test call-sites to use ctxkeys.PrincipalKey instead of the
plain string key "principal". Without this, auth.GetPrincipal silently
falls back to anonymousPrincipal since Go context lookups are type-sensitive.

Signed-off-by: Trevin Chow <trevin@trevinchow.com>
@tmchow tmchow force-pushed the test/node-type-hash-collision branch from e73d5c1 to b6801d3 Compare April 5, 2026 03:56
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.

Add hash collision test for NodeType ID generation

1 participant