Skip to content

Reject truncated consistent hashing replica keys - #3585

Merged
chenBright merged 1 commit into
apache:masterfrom
wasphin:fix-consistent-hash-key-length
Oct 7, 2026
Merged

chenBright merged 1 commit into
apache:masterfrom
wasphin:fix-consistent-hash-key-length

Conversation

@wasphin

@wasphin wasphin commented Oct 6, 2026

Copy link
Copy Markdown
Member

What problem does this PR solve?

Issue Number: N/A

Problem Summary:
The consistent hashing replica builders use the length returned by
snprintf even when the formatted key does not fit the buffer. Oversized
server tags can therefore produce an invalid key length for hashing.

What is changed and the side effects?

Changed:

  • Validate formatting results before hashing in the default and Ketama
    replica policies, rejecting formatting errors and truncated keys.
  • Add regression coverage for Murmur3, MD5, and Ketama: key-length
    boundaries, rejection without partial ring updates, mixed batch additions,
    and oversized tags when tag hashing is disabled.

Side effects:

  • Performance effects: adds a length check when building each replica key.
    Request selection is unchanged.
  • Breaking backward compatibility: keys that fit the buffer retain their
    existing mappings. When server tags participate in hashing, servers whose
    generated keys exceed the existing buffer capacity are rejected rather
    than hashing an invalid length. Batch additions still retain valid servers.

Check List:

Check snprintf results before hashing replica keys in the default and
Ketama policies. Reject formatting errors and truncated keys without
partially updating the hash ring. Keep existing mappings for keys that
fit the buffer.

Cover key-length boundaries, batch additions, and disabled tag hashing
for Murmur3, MD5, and Ketama.

Copilot AI 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.

Copilot review overview

🟢 Approval recommended

The bounds checks address the invalid-length issue, and regression coverage exercises the affected policies and ring-update behavior.

Review effort: Balanced
Findings: None

What changed in this PR

Prevents bRPC’s consistent-hashing policies from hashing invalid or truncated replica keys.

Changes:

  • Validate formatting results before hashing in default and Ketama policies.
  • Add regression coverage for key-length boundaries, failed additions, mixed batches, and disabled tag hashing.
File Description
test/​brpc_load_balancer_unittest.cpp Tests all three hashing policies and rejection behavior.
src/​brpc/​policy/​consistent_hashing_load_balancer.cpp Rejects invalid key lengths before hashing.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@chenBright chenBright 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.

LGTM

@chenBright
chenBright merged commit 2dca429 into apache:master Oct 7, 2026
45 of 46 checks passed
@wasphin
wasphin deleted the fix-consistent-hash-key-length branch October 7, 2026 08:12
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.

3 participants