Skip to content

Commit

Permalink
feat(kv): optimize ZSetIndexLRUCache slot calc (#313)
Browse files Browse the repository at this point in the history
  • Loading branch information
caojiajun committed Sep 9, 2024
1 parent ce31260 commit 5b164de
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import com.netease.nim.camellia.redis.proxy.conf.ProxyDynamicConf;
import com.netease.nim.camellia.redis.proxy.upstream.kv.conf.RedisKvConf;
import com.netease.nim.camellia.redis.proxy.upstream.kv.utils.BytesUtils;
import com.netease.nim.camellia.redis.proxy.util.RedisClusterCRC16Utils;
import com.netease.nim.camellia.tools.utils.BytesKey;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -59,8 +58,7 @@ private void rebuild() {
this.capacity = capacity;
}

public byte[] getForRead(byte[] key, byte[] cacheKey, BytesKey ref) {
int slot = RedisClusterCRC16Utils.getSlot(key);
public byte[] getForRead(int slot, byte[] cacheKey, BytesKey ref) {
BytesKey indexCacheKey = new BytesKey(BytesUtils.merge(cacheKey, ref.getKey()));
byte[] bytes = localCache.get(slot, indexCacheKey);
if (bytes != null) {
Expand All @@ -75,8 +73,7 @@ public byte[] getForRead(byte[] key, byte[] cacheKey, BytesKey ref) {
return null;
}

public byte[] getForWrite(byte[] key, byte[] cacheKey, BytesKey ref) {
int slot = RedisClusterCRC16Utils.getSlot(key);
public byte[] getForWrite(int slot, byte[] cacheKey, BytesKey ref) {
BytesKey indexCacheKey = new BytesKey(BytesUtils.merge(cacheKey, ref.getKey()));
byte[] bytes = localCacheForWrite.get(slot, indexCacheKey);
if (bytes != null) {
Expand All @@ -85,20 +82,17 @@ public byte[] getForWrite(byte[] key, byte[] cacheKey, BytesKey ref) {
return localCache.get(slot, indexCacheKey);
}

public void putForWrite(byte[] key, byte[] cacheKey, BytesKey ref, byte[] raw) {
int slot = RedisClusterCRC16Utils.getSlot(key);
public void putForWrite(int slot, byte[] cacheKey, BytesKey ref, byte[] raw) {
BytesKey indexCacheKey = new BytesKey(BytesUtils.merge(cacheKey, ref.getKey()));
localCacheForWrite.put(slot, indexCacheKey, raw);
}

public void putForRead(byte[] key, byte[] cacheKey, BytesKey ref, byte[] raw) {
int slot = RedisClusterCRC16Utils.getSlot(key);
public void putForRead(int slot, byte[] cacheKey, BytesKey ref, byte[] raw) {
BytesKey indexCacheKey = new BytesKey(BytesUtils.merge(cacheKey, ref.getKey()));
localCache.put(slot, indexCacheKey, raw);
}

public void remove(byte[] key, byte[] cacheKey, BytesKey ref) {
int slot = RedisClusterCRC16Utils.getSlot(key);
public void remove(int slot, byte[] cacheKey, BytesKey ref) {
BytesKey indexCacheKey = new BytesKey(BytesUtils.merge(cacheKey, ref.getKey()));
localCache.remove(slot, indexCacheKey);
localCacheForWrite.remove(slot, indexCacheKey);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ private Reply zaddVersion1(int slot, KeyMeta keyMeta, byte[] key, byte[] cacheKe
//
if (cacheConfig.isZSetLocalCacheEnable()) {
ZSetIndexLRUCache lruCache = cacheConfig.getZSetIndexLRUCache();
lruCache.putForWrite(key, cacheKey, new BytesKey(index.getRef()), member);
lruCache.putForWrite(slot, cacheKey, new BytesKey(index.getRef()), member);
}
}
rewriteCmd[i] = Utils.doubleToBytes(entry.getValue());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ protected final Reply zremVersion1(int slot, KeyMeta keyMeta, byte[] key, byte[]
//
if (cacheConfig.isZSetLocalCacheEnable()) {
ZSetIndexLRUCache lruCache = cacheConfig.getZSetIndexLRUCache();
lruCache.remove(key, cacheKey, new BytesKey(index.getRef()));
lruCache.remove(slot, cacheKey, new BytesKey(index.getRef()));
}
}
i++;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,9 @@ private Reply checkReplyWithIndex(int slot, KeyMeta keyMeta, byte[] key, MultiBu
for (BytesKey ref : set) {
byte[] raw;
if (forRead) {
raw = lruCache.getForRead(key, cacheKey, ref);
raw = lruCache.getForRead(slot, cacheKey, ref);
} else {
raw = lruCache.getForWrite(key, cacheKey, ref);
raw = lruCache.getForWrite(slot, cacheKey, ref);
}
if (raw != null && raw.length > 0) {
memberMap.put(ref, raw);
Expand Down Expand Up @@ -197,9 +197,9 @@ private Reply checkReplyWithIndex(int slot, KeyMeta keyMeta, byte[] key, MultiBu
if (localCacheEnable) {
ZSetIndexLRUCache lruCache = cacheConfig.getZSetIndexLRUCache();
if (forRead) {
lruCache.putForRead(key, cacheKey, member, raw);
lruCache.putForRead(slot, cacheKey, member, raw);
} else {
lruCache.putForWrite(key, cacheKey, member, raw);
lruCache.putForWrite(slot, cacheKey, member, raw);
}
}
}
Expand Down Expand Up @@ -234,9 +234,9 @@ private Reply checkReplyWithIndex(int slot, KeyMeta keyMeta, byte[] key, MultiBu
if (localCacheEnable) {
ZSetIndexLRUCache lruCache = cacheConfig.getZSetIndexLRUCache();
if (forRead) {
lruCache.putForRead(key, cacheKey, bytesKey, keyValue.getValue());
lruCache.putForRead(slot, cacheKey, bytesKey, keyValue.getValue());
} else {
lruCache.putForWrite(key, cacheKey, bytesKey, keyValue.getValue());
lruCache.putForWrite(slot, cacheKey, bytesKey, keyValue.getValue());
}
}

Expand Down

0 comments on commit 5b164de

Please sign in to comment.