Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions client/CHANGES.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
CHANGES

2.0
- Transitioning to Murmur3 for key hashing
- Hashing Algorithm dependent on Split algo field

1.1.7
- We can now specify a percentage of traffic that can be in the Split - i.e. traffic allocation.
- You can now register your own ImpressionListener. SplitClientConfig#impressionListener.
Expand Down
3 changes: 3 additions & 0 deletions client/NEWS.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
NEWS

2.0.0
- The internal hashing algorith is now murmur2 and still honors legacy algo for exsiting splits.

1.1.7
- This release gives the ability to specify a percentage of traffic that will not be in a Split.
- In addition, we now provide the ability to provide your own ImpressionListener.
Expand Down
2 changes: 1 addition & 1 deletion client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>io.split.client</groupId>
<artifactId>java-client-parent</artifactId>
<version>1.1.7</version>
<version>2.0.0-RC1</version>
</parent>
<artifactId>java-client</artifactId>
<packaging>jar</packaging>
Expand Down
4 changes: 2 additions & 2 deletions client/src/main/java/io/split/client/SplitClientImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ private TreatmentLabelAndChangeNumber getTreatment(String matchingKey, String bu

if (parsedSplit.trafficAllocation() < 100) {
// if the traffic allocation is 100%, no need to do anything special.
int bucket = Splitter.getBucket(bk, parsedSplit.trafficAllocationSeed());
int bucket = Splitter.getBucket(bk, parsedSplit.trafficAllocationSeed(), parsedSplit.algo());

if (bucket >= parsedSplit.trafficAllocation()) {
// out of split
Expand All @@ -185,7 +185,7 @@ private TreatmentLabelAndChangeNumber getTreatment(String matchingKey, String bu
}

if (parsedCondition.matcher().match(matchingKey, attributes)) {
String treatment = Splitter.getTreatment(bk, parsedSplit.seed(), parsedCondition.partitions());
String treatment = Splitter.getTreatment(bk, parsedSplit.seed(), parsedCondition.partitions(), parsedSplit.algo());
return new TreatmentLabelAndChangeNumber(treatment, parsedCondition.label(), parsedSplit.changeNumber());
}
}
Expand Down
1 change: 1 addition & 0 deletions client/src/main/java/io/split/client/dtos/Split.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ public class Split {
public long changeNumber;
public Integer trafficAllocation;
public Integer trafficAllocationSeed;
public int algo;
}
165 changes: 165 additions & 0 deletions client/src/main/java/io/split/client/utils/MurmurHash3.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
package io.split.client.utils;

/**
* The MurmurHash3 algorithm was created by Austin Appleby and placed in the public domain.
* This java port was authored by Yonik Seeley and also placed into the public domain.
* The author hereby disclaims copyright to this source code.
* <p>
* This produces exactly the same hash values as the final C++
* version of MurmurHash3 and is thus suitable for producing the same hash values across
* platforms.
* <p>
* The 32 bit x86 version of this hash should be the fastest variant for relatively short keys like ids.
* murmurhash3_x64_128 is a good choice for longer strings or if you need more than 32 bits of hash.
* <p>
* Note - The x86 and x64 versions do _not_ produce the same results, as the
* algorithms are optimized for their respective platforms.
* <p>
* See http://github.com/yonik/java_util for future updates to this file.
*/
public final class MurmurHash3 {

/**
* 128 bits of state
*/
public static final class LongPair {
public long val1;
public long val2;
}

public static final int fmix32(int h) {
h ^= h >>> 16;
h *= 0x85ebca6b;
h ^= h >>> 13;
h *= 0xc2b2ae35;
h ^= h >>> 16;
return h;
}

public static final long fmix64(long k) {
k ^= k >>> 33;
k *= 0xff51afd7ed558ccdL;
k ^= k >>> 33;
k *= 0xc4ceb9fe1a85ec53L;
k ^= k >>> 33;
return k;
}

/**
* Gets a long from a byte buffer in little endian byte order.
*/
public static final long getLongLittleEndian(byte[] buf, int offset) {
return ((long) buf[offset + 7] << 56) // no mask needed
| ((buf[offset + 6] & 0xffL) << 48)
| ((buf[offset + 5] & 0xffL) << 40)
| ((buf[offset + 4] & 0xffL) << 32)
| ((buf[offset + 3] & 0xffL) << 24)
| ((buf[offset + 2] & 0xffL) << 16)
| ((buf[offset + 1] & 0xffL) << 8)
| ((buf[offset] & 0xffL)); // no shift needed
}


/**
* Returns the MurmurHash3_x86_32 hash of the UTF-8 bytes of the String without actually encoding
* the string to a temporary buffer. This is more than 2x faster than hashing the result
* of String.getBytes().
*/
public static long murmurhash3_x86_32(CharSequence data, int offset, int len, int seed) {

final int c1 = 0xcc9e2d51;
final int c2 = 0x1b873593;

int h1 = seed;

int pos = offset;
int end = offset + len;
int k1 = 0;
int k2 = 0;
int shift = 0;
int bits = 0;
int nBytes = 0; // length in UTF8 bytes


while (pos < end) {
int code = data.charAt(pos++);
if (code < 0x80) {
k2 = code;
bits = 8;

} else if (code < 0x800) {
k2 = (0xC0 | (code >> 6))
| ((0x80 | (code & 0x3F)) << 8);
bits = 16;
} else if (code < 0xD800 || code > 0xDFFF || pos >= end) {
// we check for pos>=end to encode an unpaired surrogate as 3 bytes.
k2 = (0xE0 | (code >> 12))
| ((0x80 | ((code >> 6) & 0x3F)) << 8)
| ((0x80 | (code & 0x3F)) << 16);
bits = 24;
} else {
// surrogate pair
// int utf32 = pos < end ? (int) data.charAt(pos++) : 0;
int utf32 = (int) data.charAt(pos++);
utf32 = ((code - 0xD7C0) << 10) + (utf32 & 0x3FF);
k2 = (0xff & (0xF0 | (utf32 >> 18)))
| ((0x80 | ((utf32 >> 12) & 0x3F))) << 8
| ((0x80 | ((utf32 >> 6) & 0x3F))) << 16
| (0x80 | (utf32 & 0x3F)) << 24;
bits = 32;
}


k1 |= k2 << shift;

// int used_bits = 32 - shift; // how many bits of k2 were used in k1.
// int unused_bits = bits - used_bits; // (bits-(32-shift)) == bits+shift-32 == bits-newshift

shift += bits;
if (shift >= 32) {
// mix after we have a complete word

k1 *= c1;
k1 = (k1 << 15) | (k1 >>> 17); // ROTL32(k1,15);
k1 *= c2;

h1 ^= k1;
h1 = (h1 << 13) | (h1 >>> 19); // ROTL32(h1,13);
h1 = h1 * 5 + 0xe6546b64;

shift -= 32;
// unfortunately, java won't let you shift 32 bits off, so we need to check for 0
if (shift != 0) {
k1 = k2 >>> (bits - shift); // bits used == bits - newshift
} else {
k1 = 0;
}
nBytes += 4;
}

} // inner

// handle tail
if (shift > 0) {
nBytes += shift >> 3;
k1 *= c1;
k1 = (k1 << 15) | (k1 >>> 17); // ROTL32(k1,15);
k1 *= c2;
h1 ^= k1;
}

// finalization
h1 ^= nBytes;

// fmix(h1);
h1 ^= h1 >>> 16;
h1 *= 0x85ebca6b;
h1 ^= h1 >>> 13;
h1 *= 0xc2b2ae35;
h1 ^= h1 >>> 16;

return h1 & 0xFFFFFFFFL;
}


}
59 changes: 44 additions & 15 deletions client/src/main/java/io/split/engine/experiments/ParsedSplit.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,29 +23,52 @@ public class ParsedSplit {
private final long _changeNumber;
private final int _trafficAllocation;
private final int _trafficAllocationSeed;

public static ParsedSplit createParsedSplitForTests(String feature, int seed, boolean killed, String defaultTreatment, List<ParsedCondition> matcherAndSplits, String trafficTypeName, long changeNumber) {
return new ParsedSplit(feature, seed, killed,
defaultTreatment, matcherAndSplits, trafficTypeName, changeNumber,
100, seed);
private final int _algo;

public static ParsedSplit createParsedSplitForTests(
String feature,
int seed,
boolean killed,
String defaultTreatment,
List<ParsedCondition> matcherAndSplits,
String trafficTypeName,
long changeNumber,
int algo
) {
return new ParsedSplit(
feature,
seed,
killed,
defaultTreatment,
matcherAndSplits,
trafficTypeName,
changeNumber,
100,
seed,
algo
);
}

public ParsedSplit(String feature,
int seed,
boolean killed,
String defaultTreatment,
List<ParsedCondition> matcherAndSplits,
String trafficTypeName,
long changeNumber,
int trafficAllocation,
int trafficAllocationSeed) {
public ParsedSplit(
String feature,
int seed,
boolean killed,
String defaultTreatment,
List<ParsedCondition> matcherAndSplits,
String trafficTypeName,
long changeNumber,
int trafficAllocation,
int trafficAllocationSeed,
int algo
) {
_split = feature;
_seed = seed;
_killed = killed;
_defaultTreatment = defaultTreatment;
_parsedCondition = ImmutableList.copyOf(matcherAndSplits);
_trafficTypeName = trafficTypeName;
_changeNumber = changeNumber;
_algo = algo;
if (_defaultTreatment == null) {
throw new IllegalArgumentException("DefaultTreatment is null");
}
Expand Down Expand Up @@ -87,6 +110,8 @@ public List<ParsedCondition> parsedConditions() {

public long changeNumber() {return _changeNumber;}

public int algo() {return _algo;}

@Override
public int hashCode() {
int result = 17;
Expand All @@ -97,6 +122,7 @@ public int hashCode() {
result = 31 * result + _parsedCondition.hashCode();
result = 31 * result + (_trafficTypeName == null ? 0 : _trafficTypeName.hashCode());
result = 31 * result + (int)(_changeNumber ^ (_changeNumber >>> 32));
result = 31 * result + (_algo ^ (_algo >>> 32));
return result;
}

Expand All @@ -114,7 +140,8 @@ public boolean equals(Object obj) {
&& _defaultTreatment.equals(other._defaultTreatment)
&& _parsedCondition.equals(other._parsedCondition)
&& _trafficTypeName == null ? other._trafficTypeName == null : _trafficTypeName.equals(other._trafficTypeName)
&& _changeNumber == other._changeNumber;
&& _changeNumber == other._changeNumber
&& _algo == other._algo;
}

@Override
Expand All @@ -134,6 +161,8 @@ public String toString() {
bldr.append(_trafficTypeName);
bldr.append(", changeNumber:");
bldr.append(_changeNumber);
bldr.append(", algo:");
bldr.append(_algo);
return bldr.toString();

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ private ParsedSplit parseWithoutExceptionHandling(Split split) {
parsedConditionList.add(new ParsedCondition(condition.conditionType, matcher, partitions, condition.label));
}

return new ParsedSplit(split.name, split.seed, split.killed, split.defaultTreatment, parsedConditionList, split.trafficTypeName, split.changeNumber, split.trafficAllocation, split.trafficAllocationSeed);
return new ParsedSplit(split.name, split.seed, split.killed, split.defaultTreatment, parsedConditionList, split.trafficTypeName, split.changeNumber, split.trafficAllocation, split.trafficAllocationSeed, split.algo);
}

private CombiningMatcher toMatcher(MatcherGroup matcherGroup) {
Expand Down
Loading