Skip to content

Commit

Permalink
Try to use Java9 CRC32C when JNI based CRC is not available (#3309)
Browse files Browse the repository at this point in the history
* Try to use Java9 CRC32C when JNI based CRC is not available

* Fixed shading test dependency

* Fixed dlog shading deps
  • Loading branch information
merlimat authored Jun 8, 2022
1 parent 35f66a0 commit 168e4c6
Show file tree
Hide file tree
Showing 11 changed files with 257 additions and 182 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,15 @@
*/

import com.scurrilous.circe.checksum.Crc32cIntChecksum;
import com.scurrilous.circe.crc.Sse42Crc32C;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;
import io.netty.util.concurrent.FastThreadLocal;

import lombok.extern.slf4j.Slf4j;

import org.apache.commons.lang3.mutable.MutableInt;

@Slf4j
class CRC32CDigestManager extends DigestManager {

private static boolean nonSupportedMessagePrinted = false;

private static final FastThreadLocal<MutableInt> currentCrc = new FastThreadLocal<MutableInt>() {
@Override
protected MutableInt initialValue() throws Exception {
Expand All @@ -43,11 +37,6 @@ protected MutableInt initialValue() throws Exception {

public CRC32CDigestManager(long ledgerId, boolean useV2Protocol, ByteBufAllocator allocator) {
super(ledgerId, useV2Protocol, allocator);

if (!Sse42Crc32C.isSupported() && !nonSupportedMessagePrinted) {
log.warn("Sse42Crc32C is not supported, will use a slower CRC32C implementation.");
nonSupportedMessagePrinted = true;
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,30 +18,20 @@
*/
package com.scurrilous.circe.checksum;

import static com.scurrilous.circe.params.CrcParameters.CRC32C;

import com.google.common.annotations.VisibleForTesting;
import com.scurrilous.circe.IncrementalIntHash;
import com.scurrilous.circe.crc.Sse42Crc32C;
import com.scurrilous.circe.crc.StandardCrcProvider;
import io.netty.buffer.ByteBuf;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Crc32cIntChecksum {

private static final Logger log = LoggerFactory.getLogger(Crc32cIntChecksum.class);

@VisibleForTesting
final static IncrementalIntHash CRC32C_HASH;
private final static IntHash CRC32C_HASH;

static {
if (Sse42Crc32C.isSupported()) {
CRC32C_HASH = new Crc32cSse42Provider().getIncrementalInt(CRC32C);
log.info("SSE4.2 CRC32C provider initialized");
CRC32C_HASH = new JniIntHash();
} else if (Java9IntHash.HAS_JAVA9_CRC32C) {
CRC32C_HASH = new Java9IntHash();
} else {
CRC32C_HASH = new StandardCrcProvider().getIncrementalInt(CRC32C);
log.warn("Failed to load Circe JNI library. Falling back to Java based CRC32c provider");
CRC32C_HASH = new Java8IntHash();
}
}

Expand All @@ -53,17 +43,9 @@ public class Crc32cIntChecksum {
* @return
*/
public static int computeChecksum(ByteBuf payload) {
if (payload.hasMemoryAddress() && (CRC32C_HASH instanceof Sse42Crc32C)) {
return CRC32C_HASH.calculate(payload.memoryAddress() + payload.readerIndex(), payload.readableBytes());
} else if (payload.hasArray()) {
return CRC32C_HASH.calculate(payload.array(), payload.arrayOffset() + payload.readerIndex(),
payload.readableBytes());
} else {
return CRC32C_HASH.calculate(payload.nioBuffer());
}
return CRC32C_HASH.calculate(payload);
}


/**
* Computes incremental checksum with input previousChecksum and input payload
*
Expand All @@ -72,15 +54,7 @@ public static int computeChecksum(ByteBuf payload) {
* @return
*/
public static int resumeChecksum(int previousChecksum, ByteBuf payload) {
if (payload.hasMemoryAddress() && (CRC32C_HASH instanceof Sse42Crc32C)) {
return CRC32C_HASH.resume(previousChecksum, payload.memoryAddress() + payload.readerIndex(),
payload.readableBytes());
} else if (payload.hasArray()) {
return CRC32C_HASH.resume(previousChecksum, payload.array(), payload.arrayOffset() + payload.readerIndex(),
payload.readableBytes());
} else {
return CRC32C_HASH.resume(previousChecksum, payload.nioBuffer());
}
return CRC32C_HASH.resume(previousChecksum, payload);
}

}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package com.scurrilous.circe.checksum;

import io.netty.buffer.ByteBuf;

public interface IntHash {
int calculate(ByteBuf buffer);
int resume(int current, ByteBuf buffer);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package com.scurrilous.circe.checksum;

import static com.scurrilous.circe.params.CrcParameters.CRC32C;
import com.scurrilous.circe.IncrementalIntHash;
import com.scurrilous.circe.crc.StandardCrcProvider;
import io.netty.buffer.ByteBuf;
import java.nio.ByteBuffer;
import java.util.zip.Checksum;

public class Java8IntHash implements IntHash {

private final IncrementalIntHash hash = new StandardCrcProvider().getIncrementalInt(CRC32C);

@Override
public int calculate(ByteBuf buffer) {
return resume(0, buffer);
}

@Override
public int resume(int current, ByteBuf buffer) {
if (buffer.hasArray()) {
return hash.resume(current, buffer.array(), buffer.arrayOffset() + buffer.readerIndex(),
buffer.readableBytes());
} else {
return hash.resume(current, buffer.nioBuffer());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package com.scurrilous.circe.checksum;

import io.netty.buffer.ByteBuf;
import io.netty.util.concurrent.FastThreadLocal;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class Java9IntHash implements IntHash {
static final boolean HAS_JAVA9_CRC32C;
private static final Method UPDATE_BYTES;
private static final Method UPDATE_DIRECT_BYTEBUFFER;

private static final String CRC32C_CLASS_NAME = "java.util.zip.CRC32C";

private static final FastThreadLocal<byte[]> TL_BUFFER = new FastThreadLocal<byte[]>() {
@Override
protected byte[] initialValue() {
return new byte[4096];
}
};

static {
boolean hasJava9CRC32C = false;
Method updateBytes = null;
Method updateDirectByteBuffer = null;

try {
Class<?> c = Class.forName(CRC32C_CLASS_NAME);
updateBytes = c.getDeclaredMethod("updateBytes", int.class, byte[].class, int.class, int.class);
updateBytes.setAccessible(true);
updateDirectByteBuffer =
c.getDeclaredMethod("updateDirectByteBuffer", int.class, long.class, int.class, int.class);
updateDirectByteBuffer.setAccessible(true);

hasJava9CRC32C = true;
} catch (Exception e) {
log.debug("Unable to use reflected methods: ", e);
updateBytes = null;
updateDirectByteBuffer = null;
}

HAS_JAVA9_CRC32C = hasJava9CRC32C;
UPDATE_BYTES = updateBytes;
UPDATE_DIRECT_BYTEBUFFER = updateDirectByteBuffer;
}

@Override
public int calculate(ByteBuf buffer) {
return resume(0, buffer);
}

private int resume(int current, long address, int offset, int length) {
try {
return (int) UPDATE_DIRECT_BYTEBUFFER.invoke(null, current, address, offset, offset + length);
} catch (IllegalAccessException | InvocationTargetException e) {
throw new RuntimeException(e);
}
}

private int resume(int current, byte[] array, int offset, int length) {
try {
return (int) UPDATE_BYTES.invoke(null, current, array, offset, offset + length);
} catch (IllegalAccessException | InvocationTargetException e) {
throw new RuntimeException(e);
}
}

@Override
public int resume(int current, ByteBuf buffer) {
int negCrc = ~current;

if (buffer.hasMemoryAddress()) {
negCrc = resume(negCrc, buffer.memoryAddress(), buffer.readerIndex(), buffer.readableBytes());
} else if (buffer.hasArray()) {
int offset = buffer.arrayOffset() + buffer.readerIndex();
negCrc = resume(negCrc, buffer.array(), offset, buffer.readableBytes());
} else {
byte[] b = TL_BUFFER.get();
int toRead = buffer.readableBytes();
while (toRead > 0) {
int length = Math.min(toRead, b.length);
buffer.readBytes(b, 0, length);
negCrc = resume(negCrc, b, 0, length);
toRead -= length;
}
}

return ~negCrc;
}
}
Loading

0 comments on commit 168e4c6

Please sign in to comment.