Skip to content

Fix OptionUtil.prefixEndOf carry handling for keys ending in 0xFF - #674

Open
amalsaidov wants to merge 1 commit into
apache:masterfrom
amalsaidov:fix/etcd-optionutil-prefixendof
Open

amalsaidov wants to merge 1 commit into
apache:masterfrom
amalsaidov:fix/etcd-optionutil-prefixendof

Conversation

@amalsaidov

Copy link
Copy Markdown

What this fixes

OptionUtil.prefixEndOf builds the exclusive end key of an etcd range query (used for prefix watches and child lookups in the etcd3 remoting). The current implementation only increments the last byte:

endKey[endKey.length - 1] = (byte) (endKey[endKey.length - 1] + 1);

When the last byte is 0xFF, incrementing wraps it to 0x00. The returned end key is then lexicographically smaller than the prefix itself, so the range [prefix, end) is empty and the watch/lookup silently returns nothing for keys that end in 0xFF.

This is the problem reported in #80. The original loop there used endKey[i] < 0xff, which is always true for a signed byte (max value 127 < 255), so the carry was never handled.

The fix

Walk the key from the end, increment the first byte that is not 0xFF, and drop the trailing 0xFF bytes, which is the standard etcd prefix-range semantics (same as jetcd's own OptionUtil). Comparing the unsigned value (endKey[i] & 0xff) < 0xff makes the check correct. When every byte is 0xFF there is no greater key, so it returns the existing NO_PREFIX_END marker to range over the rest of the keyspace. The empty-prefix guard added in #669 is preserved (an empty key has no non-0xFF byte, so it falls through to NO_PREFIX_END).

Tested

Added OptionUtilTest covering:

  • plain ASCII keys ("a" -> "b", last byte of a multi-byte key)
  • a trailing 0xFF carrying to the previous byte and being dropped
  • multiple trailing 0xFF bytes
  • all-0xFF and empty prefixes returning NO_PREFIX_END
  • the input ByteSequence not being mutated

./mvnw -pl dubbo-remoting-extensions/dubbo-remoting-etcd3 -am test -DskipIntegrationTests=false -> Tests run: 7, Failures: 0, Errors: 0. The three trailing-0xFF cases fail against the current code and pass with this change. The existing etcd integration tests stay @Disabled as before.

Fixes #80


This change was written with the help of Claude Code and directed, reviewed and tested by a human contributor.

🤖 Generated with Claude Code

prefixEndOf computes the exclusive end of an etcd range query by
incrementing the key. It only incremented the last byte, so a key whose
last byte is 0xFF wrapped to 0x00 and produced an end key that is
lexicographically smaller than the prefix. The resulting range is empty,
which breaks prefix watches and child lookups for such keys.

Walk the key from the end, increment the first byte that is not 0xFF and
drop the trailing 0xFF bytes, matching the standard etcd prefix range
semantics. When every byte is 0xFF there is no greater key, so return the
NO_PREFIX_END marker to range over the rest of the keyspace. The previous
loop that this replaced used a signed `endKey[i] < 0xff` check that was
always true; comparing the unsigned value fixes that.

Add OptionUtilTest covering the ASCII, trailing-0xFF, all-0xFF and empty
cases, and that the input is not mutated.

Fixes apache#80

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
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.

bug report: org.apache.dubbo.remoting.etcd.option.OptionUtil prefixEndOf

1 participant