Skip to content

JAVA-1392: Reduce lock contention in RPTokenFactory #795

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 16, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
- [improvement] JAVA-1308: CodecRegistry performance improvements.
- [improvement] JAVA-1241: Upgrade Netty to 4.1.x.
- [improvement] JAVA-1287: Add CDC to TableOptionsMetadata and Schema Builder.
- [improvement] JAVA-1392: Reduce lock contention in RPTokenFactory.

Merged from 3.1.x branch:

Expand Down
38 changes: 33 additions & 5 deletions driver-core/src/main/java/com/datastax/driver/core/Token.java
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,6 @@ private long murmur(ByteBuffer data) {
k1 *= c2;
h1 ^= k1;
}
;

//----------
// finalization
Expand Down Expand Up @@ -569,16 +568,45 @@ private static class RPTokenFactory extends Factory {
private static final Token MIN_TOKEN = new RPToken(MIN_VALUE);
private static final Token MAX_TOKEN = new RPToken(MAX_VALUE);

private BigInteger md5(ByteBuffer data) {
private final MessageDigest prototype;
private final boolean supportsClone;

private RPTokenFactory() {
prototype = createMessageDigest();
boolean supportsClone;
try {
MessageDigest digest = MessageDigest.getInstance("MD5");
digest.update(data.duplicate());
return new BigInteger(digest.digest()).abs();
prototype.clone();
supportsClone = true;
} catch (CloneNotSupportedException e) {
supportsClone = false;
}
this.supportsClone = supportsClone;
}

private static MessageDigest createMessageDigest() {
try {
return MessageDigest.getInstance("MD5");
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException("MD5 doesn't seem to be available on this JVM", e);
}
}

private MessageDigest newMessageDigest() {
if (supportsClone) {
try {
return (MessageDigest) prototype.clone();
} catch (CloneNotSupportedException ignored) {
}
}
return createMessageDigest();
}

private BigInteger md5(ByteBuffer data) {
MessageDigest digest = newMessageDigest();
digest.update(data.duplicate());
return new BigInteger(digest.digest()).abs();
}

@Override
RPToken fromString(String tokenStr) {
return new RPToken(new BigInteger(tokenStr));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,27 @@
*/
package com.datastax.driver.core;

import com.datastax.driver.core.utils.Bytes;
import org.testng.annotations.Test;

import java.nio.ByteBuffer;
import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;

public class RPTokenFactoryTest {
Token.Factory factory = Token.RPToken.FACTORY;

@Test(groups = "unit")
public void should_hash_consistently() {
ByteBuffer byteBuffer = Bytes.fromHexString("0xCAFEBABE");
Token tokenA = factory.hash(byteBuffer);
Token tokenB = factory.hash(byteBuffer);
assertThat(tokenA)
.isEqualTo(factory.fromString("59959303159920881837560881824507314222"))
.isEqualTo(tokenB);
}

@Test(groups = "unit")
public void should_split_range() {
List<Token> splits = factory.split(factory.fromString("0"), factory.fromString("127605887595351923798765477786913079296"), 3);
Expand Down