Skip to content

test(writer): pin label fingerprint values (M2 1/4, #991) - #1060

Merged
rita7lopes merged 1 commit into
metrico:masterfrom
Guess-Who-sec:test/fingerprint-pin
Nov 9, 2026
Merged

rita7lopes merged 1 commit into
metrico:masterfrom
Guess-Who-sec:test/fingerprint-pin

Conversation

@Guess-Who-sec

@Guess-Who-sec Guess-Who-sec commented Oct 29, 2026 •

Copy link
Copy Markdown
Contributor

Part of M2 (#920), PR 1 of 4. Closes #991. Epic: #917.

M2 is split into four PRs:

  1. this one: test(writer): pin label fingerprint values (M2 1/4, #991) #1060: fingerprint value pins (Pin the label-fingerprint contract: three implementations, zero tests #991)
  2. test(integration): seed package and write-read harness for logs (M2 2/4, #920) #1061: seed package and write-read harness for logs
  3. test(integration): write-read cases for metrics, traces and profiles (M2 3/4, #920) #1062: write-read cases for metrics, traces and profiles
  4. test(integration): schema migration and ruler boot-order checks (M2 4/4, #920) #1063: schema migration and ruler boot-order checks

What

Value pins for every label-fingerprint implementation. Tests only: no production code changes.

Test file Pins
writer/utils/unmarshal/fingerprint_test.go fingerprintLabels (used on every ingest path via builder.go) in both modes, CityHash and Bernstein. Also two behaviour checks: the result does not depend on label order (callers do not sort labels before hashing), and {ab="c"} and {a="bc"} get different fingerprints
writer/utils/fingerprint_test.go FingerprintLabels (FNV variant)
writer/utils/helputils/fingerprint_test.go FingerprintLabelsDJBHashPrometheus (writer copy)
ctrl/qryn/helputils/hash_test.go FingerprintLabelsDJBHashPrometheus (ctrl copy), pinned to the same values as the writer copy

Inputs: two labels, no labels, an empty value, a non-ASCII value (→) and three labels. For DJB: nil, empty, single byte, a label pair, non-ASCII and a high byte.

The expected values were generated once from the current implementations, since they are the contract. Each test says that a failure means data compatibility is broken, not that the expected values need updating.

Why

The fingerprint joins time_series to the samples tables and is persisted in every deployment. If its value changed for a given label set, new writes would stop matching stored series and queries would silently return less data. Nothing in CI would catch it. This should land before #972, so each step of the table refactor can show it does not change fingerprints.

How I checked the tests catch real changes

There is no bug to reproduce, so I mutated each implementation in a scratch copy and confirmed the matching test fails:

Mutation Failing test
FNV separator byte 255 → 254 TestFingerprintLabelsIsStable (utils)
fingerprintLabels product constant +1 TestFingerprintLabelsIsStable (both modes)
fingerprintLabels sum made order-dependent TestFingerprintLabelsIgnoresLabelOrder
fingerprintLabels hashing name+value as one string TestFingerprintLabelsSeparatesNameAndValue
writer DJB seed 5381 → 5382 TestFingerprintLabelsDJBHashPrometheusIsStable (writer)
ctrl DJB multiplier 33 → 31 TestFingerprintLabelsDJBHashPrometheusIsStable (ctrl)

Also ran: gofmt -l . (clean), CGO_ENABLED=0 go build, go vet ./... (same output as master, nothing from the new files), go test ./..., and go test -race on the four touched packages.

Findings worth a follow-up (not changed here)

  1. HashType: "bernstein" silently keeps CityHash. In metrico/cloki-config (setFastConfigSettings), case "bernstein": is empty, and Go does not fall through, so only "default" selects Bernstein. The struct default is FingerPrintType: 1 (CityHash).
  2. The two DJB copies must stay identical. writer/chwrapper (GetSetting/PutSetting) and ctrl/qryn/maintenance/rotate.go key the same settings rows with the same string, so the pins hold both copies to the same values.
  3. FingerprintLabels is not standard FNV-1a. It multiplies by the FNV offset basis instead of the FNV prime. It has no callers today. Is it still needed?
  4. Existing unmarshal tests run in Bernstein mode. They install an empty ClokiBaseSettingServer, where FingerPrintType is 0 = Bernstein, while real deployments default to CityHash. The new tests set the mode explicitly.

The label fingerprint is the join key between time_series and the
samples tables and is persisted in every deployment. Nothing tested it,
so a change to any of the hash implementations would silently detach
new writes from historical series.

Pin the exact output of each implementation, with values taken from the
current code:

- unmarshal.fingerprintLabels in both CityHash and Bernstein modes, plus
  its order independence (callers do not sort labels) and that moving
  characters between a label name and its value changes the series
- utils.FingerprintLabels (FNV variant)
- both copies of FingerprintLabelsDJBHashPrometheus (writer and ctrl),
  which key the same rows of the settings table and must agree

@rita7lopes rita7lopes left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @Guess-Who-sec, this is a solid first piece of M2.

The fingerprint is stored permanently and is what links time_series to the samples tables. Until now, a change to any of these hashes would have passed CI and quietly cut new writes off from historical series, so these pins close a real gap. I traced each one back to its callers:

  • fingerprintLabels is the hash every ingest path uses (via builder.go), and it's good to have both modes pinned.
  • The label-order test matters most. builder.go hashes the filtered labels unsorted, so order independence relies entirely on the sum/xor/product combination. This test makes that guarantee explicit.
  • Holding both DJB copies to the same values is right, since writer/chwrapper and ctrl/qryn/maintenance/rotate.go key the same settings rows.

The mutation table in the description makes this easy to trust. Thanks for doing that.

On your follow-ups:

  1. HashType: "bernstein": please open an issue on cloki-config. Any fix has to keep existing deployments on the hash they already use, so it's a config-compatibility decision first and a code fix second.
  2. DJB copies: agreed. The pins cover it for now.
  3. utils.FingerprintLabels: it has no callers, so it can go in a separate cleanup PR along with its pin.
  4. Unmarshal tests running in Bernstein mode: worth fixing. A follow-up that sets the mode explicitly in those tests (or defaults them to CityHash) would bring them in line with real deployments.

Approving. Over to #1061.

@rita7lopes
rita7lopes merged commit f86d165 into metrico:master Nov 9, 2026
9 checks passed
rita7lopes pushed a commit that referenced this pull request Nov 24, 2026
utils.FingerprintLabels has no callers. It is also not standard FNV-1a:
it multiplies by the offset basis instead of the FNV prime. Remove it,
its helpers and the pin #1060 added for it. Follow-up from #1060.
rita7lopes pushed a commit that referenced this pull request Nov 24, 2026
…1084)

The writer tests that install a minimal global config left
FingerPrintType at its zero value, which is Bernstein, while a real
deployment defaults to CityHash. Set CityHash explicitly in those
helpers (writer/controller, writer/grpc, writer/utils/unmarshal), so the
tests hash the way production does. Follow-up from #1060.
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.

Pin the label-fingerprint contract: three implementations, zero tests

2 participants