Skip to content

feat: Add support for high resolution fractional#1740

Open
chrfwow wants to merge 6 commits intoopen-feature:mainfrom
chrfwow:high-resolution-fractional
Open

feat: Add support for high resolution fractional#1740
chrfwow wants to merge 6 commits intoopen-feature:mainfrom
chrfwow:high-resolution-fractional

Conversation

@chrfwow
Copy link
Contributor

@chrfwow chrfwow commented Mar 12, 2026

This PR

Adds high resolution fractional support

Related Issues

Fixes #1738

Signed-off-by: christian.lutnik <christian.lutnik@dynatrace.com>
@chrfwow chrfwow requested a review from a team as a code owner March 12, 2026 14:20
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request significantly enhances the fractional targeting mechanism by extending its compatibility to include Number and Boolean types as bucketing keys, in addition to the existing String support. The core change involves a refactored hashing and bucketing algorithm that utilizes a high-resolution approach, aiming to provide more precise and consistent fractional evaluations across diverse input types.

Highlights

  • Expanded Input Types for Fractional Targeting: The fractional targeting mechanism now supports Number and Boolean types as bucketing keys, in addition to the existing String support. This allows for more flexible and diverse input values when determining fractional distributions.
  • High-Resolution Fractional Bucketing: Implemented a new high-resolution fractional bucketing algorithm. This enhancement utilizes MurmurHash3 and bit shifting operations to provide more precise and consistent fractional evaluations, especially for large numbers.
  • Robust Number to Byte Array Conversion: Introduced a utility method, numberToByteArray, to consistently convert various numeric types (including Integer, Double, Long, BigInteger, Byte, Short, Float, and BigDecimal) into byte arrays. This ensures uniform input for the hashing function, improving reliability across different number representations.
Changelog
  • tools/flagd-core/src/main/java/dev/openfeature/contrib/tools/flagd/core/targeting/Fractional.java
    • Updated the bucketBy variable type from String to byte[] to accommodate various input types.
    • Added logic to convert Number and Boolean arguments into byte arrays for consistent hashing.
    • Introduced a numberToByteArray helper method to handle conversions for different numeric types.
    • Modified the distributeValue method to use a new high-resolution bucketing algorithm based on MurmurHash3 and bitwise operations.
    • Adjusted the FractionProperty constructor's error message formatting.
  • tools/flagd-core/src/test/resources/fractional/boolean.json
    • Added a new test case for boolean input in fractional targeting.
  • tools/flagd-core/src/test/resources/fractional/largeDouble.json
    • Added a new test case for large double input in fractional targeting.
  • tools/flagd-core/src/test/resources/fractional/largeInt.json
    • Added a new test case for large integer input in fractional targeting.
  • tools/flagd-core/src/test/resources/fractional/selfContainedFractionalB.json
    • Modified the expected result in a fractional targeting test case.
  • tools/flagd-core/src/test/resources/fractional/string.json
    • Added a new test case for string input in fractional targeting.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

]
],
"result": "blue"
"result": "red"
Copy link
Contributor Author

Choose a reason for hiding this comment

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

In some cases, we will compute a different bucket now

@chrfwow
Copy link
Contributor Author

chrfwow commented Mar 12, 2026

Do we want more tests for this? I stuck to the current test suite

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

The pull request refactors the fractional targeting logic to support various input types (numbers, booleans) for bucketing by converting them to byte arrays and introduces a new numberToByteArray helper. The core bucketing algorithm was also revised to use an integer-based bit shift operation. Review comments highlight two key areas for improvement: the numberToByteArray method has a potential precision loss issue for BigDecimal and could be made more concise using ByteBuffer, and the bucketing logic has a subtle bug related to handling signed vs. unsigned integers from the MurmurHash3 function, which requires a bitmask for correct range mapping.

Comment on lines +84 to +121
private byte[] numberToByteArray(Number number) {
if (number instanceof Integer) {
return new byte[] {
(byte) ((int) number >> 24),
(byte) ((int) number >> 16),
(byte) ((int) number >> 8),
(byte) ((int) number)
};
} else if (number instanceof Double) {
return numberToByteArray(Double.doubleToLongBits((Double) number));
} else if (number instanceof Long) {
return new byte[] {
(byte) ((long) number >> 56),
(byte) ((long) number >> 48),
(byte) ((long) number >> 40),
(byte) ((long) number >> 32),
(byte) ((long) number >> 24),
(byte) ((long) number >> 16),
(byte) ((long) number >> 8),
(byte) ((long) number)
};
} else if (number instanceof BigInteger) {
return ((BigInteger) number).toByteArray();
} else if (number instanceof Byte) {
return new byte[] {(byte) number};
} else if (number instanceof Short) {
return new byte[] {
(byte) ((short) number >> 8),
(byte) ((short) number)
};
} else if (number instanceof Float) {
return numberToByteArray(Float.floatToIntBits((Float) number));
} else if (number instanceof BigDecimal) {
return numberToByteArray(Double.doubleToLongBits(number.doubleValue()));
} else {
throw new IllegalArgumentException("Unsupported number type: " + number.getClass());
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

high

The numberToByteArray method can be improved in two ways:

  1. Precision Loss with BigDecimal: For BigDecimal, converting to double via doubleValue() can cause a loss of precision, which undermines the goal of supporting high-resolution fractional targeting. A better approach is to convert the BigDecimal to its string representation to preserve its full precision.
  2. Readability and Conciseness: The manual byte array creation for primitive wrapper types like Integer, Long, Double, Float, and Short is verbose. Using java.nio.ByteBuffer would make the code more concise and idiomatic.
private byte[] numberToByteArray(Number number) {
    if (number instanceof Integer) {
        return ByteBuffer.allocate(4).putInt(number.intValue()).array();
    } else if (number instanceof Double) {
        return ByteBuffer.allocate(8).putDouble(number.doubleValue()).array();
    } else if (number instanceof Long) {
        return ByteBuffer.allocate(8).putLong(number.longValue()).array();
    } else if (number instanceof BigInteger) {
        return ((BigInteger) number).toByteArray();
    } else if (number instanceof Byte) {
        return new byte[] {number.byteValue()};
    } else if (number instanceof Short) {
        return ByteBuffer.allocate(2).putShort(number.shortValue()).array();
    } else if (number instanceof Float) {
        return ByteBuffer.allocate(4).putFloat(number.floatValue()).array();
    } else if (number instanceof BigDecimal) {
        // Using doubleValue() can lead to precision loss for high-resolution numbers.
        // A string representation preserves the precision.
        return number.toString().getBytes(StandardCharsets.UTF_8);
    } else {
        throw new IllegalArgumentException("Unsupported number type: " + number.getClass());
    }
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Disagree with point 1. Other implementation will also convert large numbers to a Double equivalent type, so we will be consistent.
As for point 2, do we want the java.nio.Bytebuffer?

Comment on lines +127 to +128
long mmrHash = MurmurHash3.hash32x86(hashKey, 0, hashKey.length, 0);
int bucket = (int) (((mmrHash * totalWeight) >> 32) & 0xFFFFFFFFL);
Copy link
Contributor

Choose a reason for hiding this comment

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

high

The bucketing logic has a subtle issue with signed vs. unsigned integers. MurmurHash3.hash32x86 returns a signed int. To correctly use it for range mapping as an unsigned 32-bit value, it should be converted to a long using a bitmask to avoid sign extension. This ensures the bucketing is fair and consistent across different hash values.

        long mmrHash = MurmurHash3.hash32x86(hashKey, 0, hashKey.length, 0) & 0xFFFFFFFFL;
        int bucket = (int) ((mmrHash * totalWeight) >> 32);

chrfwow added 4 commits March 12, 2026 15:30
Signed-off-by: christian.lutnik <christian.lutnik@dynatrace.com>
Signed-off-by: christian.lutnik <christian.lutnik@dynatrace.com>
Signed-off-by: christian.lutnik <christian.lutnik@dynatrace.com>
Signed-off-by: christian.lutnik <christian.lutnik@dynatrace.com>
@chrfwow chrfwow marked this pull request as draft March 12, 2026 16:34
Signed-off-by: christian.lutnik <christian.lutnik@dynatrace.com>
@chrfwow
Copy link
Contributor Author

chrfwow commented Mar 12, 2026

Idk why there are PMD errors, but I think they are unrelated and false positives

@chrfwow chrfwow marked this pull request as ready for review March 12, 2026 16:48
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.

[FEATURE] High-resolution fractional bucketing in flagd provider

1 participant