From 95f2bb5d5e4103160e52d8f42f58919220f20816 Mon Sep 17 00:00:00 2001 From: caojiajun Date: Fri, 3 Jan 2025 17:25:00 +0800 Subject: [PATCH] fix: KeyCodec (#364) --- .../embedded/storage/codec/KeyCodec.java | 27 +++++++++++++------ .../storage/compress/ZstdCompressor.java | 2 +- 2 files changed, 20 insertions(+), 9 deletions(-) diff --git a/camellia-redis-proxy/camellia-redis-proxy-core/src/main/java/com/netease/nim/camellia/redis/proxy/upstream/embedded/storage/codec/KeyCodec.java b/camellia-redis-proxy/camellia-redis-proxy-core/src/main/java/com/netease/nim/camellia/redis/proxy/upstream/embedded/storage/codec/KeyCodec.java index 8d05995f8..55172a930 100644 --- a/camellia-redis-proxy/camellia-redis-proxy-core/src/main/java/com/netease/nim/camellia/redis/proxy/upstream/embedded/storage/codec/KeyCodec.java +++ b/camellia-redis-proxy/camellia-redis-proxy-core/src/main/java/com/netease/nim/camellia/redis/proxy/upstream/embedded/storage/codec/KeyCodec.java @@ -20,6 +20,11 @@ */ public class KeyCodec { + /** + * 解码一整个slot + * @param all data + * @return 解码结果 + */ public static Map decodeSlot(byte[] all) { ByteBuffer buffer = ByteBuffer.wrap(all); int bucketSize = all.length / EmbeddedStorageConstants._4k; @@ -40,14 +45,15 @@ public static Map decodeSlot(byte[] all) { */ public static Map decodeBucket(byte[] bytes) { int crc1 = BytesUtils.toInt(bytes, 0);//0,1,2,3 - int crc2 = RedisClusterCRC16Utils.getCRC16(bytes, 5, bytes.length); + int crc2 = RedisClusterCRC16Utils.getCRC16(bytes, 9, bytes.length); if (crc1 != crc2) { return new HashMap<>(); } int decompressLen = BytesUtils.toShort(bytes, 4);//4,5 - byte compressType = bytes[6];//6 + int compressLen = BytesUtils.toShort(bytes, 6);//6,7 + byte compressType = bytes[8];//8 ICompressor compressor = CompressUtils.get(CompressType.getByValue(compressType)); - byte[] decompressData = compressor.decompress(bytes, 7, bytes.length - 7, decompressLen); + byte[] decompressData = compressor.decompress(bytes, 9, compressLen, decompressLen); Unpack unpack = new Unpack(decompressData); int size = unpack.popVarUint(); Map map = new HashMap<>(); @@ -81,15 +87,20 @@ public static byte[] encodeBucket(Map keys) { compressType = CompressType.none; compressed = array; } - if (compressed.length + 5 > EmbeddedStorageConstants._4k) { + if (compressed.length + 9 > EmbeddedStorageConstants._4k) { return null; } - int crc = RedisClusterCRC16Utils.getCRC16(compressed, 0, compressed.length); + short compressLen = (short) compressed.length; + ByteBuffer buffer = ByteBuffer.allocate(EmbeddedStorageConstants._4k); - buffer.putInt(crc); - buffer.putShort(decompressLen); - buffer.put(compressType.getType()); + buffer.putInt(0);//0,1,2,3 + buffer.putShort(decompressLen);//4,5 + buffer.putShort(compressLen);//6,7 + buffer.put(compressType.getType());//8 buffer.put(compressed); + byte[] bytes = buffer.array(); + int crc = RedisClusterCRC16Utils.getCRC16(bytes, 9, bytes.length); + buffer.putInt(0, crc); return buffer.array(); } } diff --git a/camellia-redis-proxy/camellia-redis-proxy-core/src/main/java/com/netease/nim/camellia/redis/proxy/upstream/embedded/storage/compress/ZstdCompressor.java b/camellia-redis-proxy/camellia-redis-proxy-core/src/main/java/com/netease/nim/camellia/redis/proxy/upstream/embedded/storage/compress/ZstdCompressor.java index 6a19c3127..ba2e6525d 100644 --- a/camellia-redis-proxy/camellia-redis-proxy-core/src/main/java/com/netease/nim/camellia/redis/proxy/upstream/embedded/storage/compress/ZstdCompressor.java +++ b/camellia-redis-proxy/camellia-redis-proxy-core/src/main/java/com/netease/nim/camellia/redis/proxy/upstream/embedded/storage/compress/ZstdCompressor.java @@ -11,7 +11,7 @@ public class ZstdCompressor implements ICompressor { private final int compressionLevel; public ZstdCompressor() { - compressionLevel = Zstd.defaultCompressionLevel(); + compressionLevel = Zstd.maxCompressionLevel(); } @Override