Skip to content

Commit

Permalink
feat: update block cache (#364)
Browse files Browse the repository at this point in the history
  • Loading branch information
caojiajun committed Jan 10, 2025
1 parent e6e8afe commit 67e07b4
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 56 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.netease.nim.camellia.redis.proxy.upstream.local.storage.cache;

/**
* Created by caojiajun on 2025/1/10
*/
public enum CacheType {
read,
write,
none,
;
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.netease.nim.camellia.redis.proxy.upstream.local.storage.key.block;

import com.netease.nim.camellia.redis.proxy.upstream.local.storage.cache.CacheType;
import com.netease.nim.camellia.redis.proxy.upstream.local.storage.key.Key;
import com.netease.nim.camellia.redis.proxy.upstream.local.storage.file.IBlockReadWrite;
import com.netease.nim.camellia.redis.proxy.upstream.local.storage.key.KeyInfo;
Expand All @@ -12,23 +13,16 @@
*/
public interface IKeyBlockReadWrite extends IBlockReadWrite {

/**
* 获取一个key
* @param slot slot
* @param key key
* @return key
* @throws IOException exception
*/
KeyInfo get(short slot, Key key) throws IOException;

/**
* 获取一个key
* @param slot slot
* @param key key
* @param cacheType cache type
* @return key
* @throws IOException exception
*/
KeyInfo getForCompact(short slot, Key key) throws IOException;
KeyInfo get(short slot, Key key, CacheType cacheType) throws IOException;

/**
* get block
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.netease.nim.camellia.redis.proxy.upstream.local.storage.key.block;

import com.netease.nim.camellia.redis.proxy.upstream.local.storage.cache.CacheType;
import com.netease.nim.camellia.redis.proxy.upstream.local.storage.file.FileNames;
import com.netease.nim.camellia.redis.proxy.upstream.local.storage.key.Key;
import com.netease.nim.camellia.redis.proxy.upstream.local.storage.cache.LRUCache;
Expand Down Expand Up @@ -62,13 +63,37 @@ private void warm() {
}

@Override
public KeyInfo get(short slot, Key key) throws IOException {
return get0(slot, key, CacheType.read);
}
public KeyInfo get(short slot, Key key, CacheType cacheType) throws IOException {
SlotInfo slotInfo = keyManifest.get(slot);
if (slotInfo == null) {
return null;
}
long fileId = slotInfo.fileId();
long offset = slotInfo.offset();
int capacity = slotInfo.capacity();
int bucketSize = capacity / _4k;
int bucket = KeyHashUtils.hash(key.key()) % bucketSize;
long bucketOffset = offset + bucket * _4k;

@Override
public KeyInfo getForCompact(short slot, Key key) throws IOException {
return get0(slot, key, CacheType.write);
String cacheKey = fileId + "|" + bucketOffset;
byte[] block = readCache.get(cacheKey);
if (block == null) {
block = writeCache.get(cacheKey);
}
if (block == null) {
block = fileReadWrite.read(file(fileId), bucketOffset, _4k);
if (cacheType == CacheType.write) {
writeCache.put(cacheKey, block);
} else if (cacheType == CacheType.read) {
readCache.put(cacheKey, block);
}
}
Map<Key, KeyInfo> map = KeyCodec.decodeBucket(block);
KeyInfo data = map.get(key);
if (data == null) {
return KeyInfo.DELETE;
}
return data;
}

@Override
Expand Down Expand Up @@ -115,43 +140,4 @@ public byte[] readBlocks(long fileId, long offset, int size) throws IOException
return fileReadWrite.read(file(fileId), offset, size);
}

private static enum CacheType {
read,
write,
none,
;
}

private KeyInfo get0(short slot, Key key, CacheType cacheType) throws IOException {
SlotInfo slotInfo = keyManifest.get(slot);
if (slotInfo == null) {
return null;
}
long fileId = slotInfo.fileId();
long offset = slotInfo.offset();
int capacity = slotInfo.capacity();
int bucketSize = capacity / _4k;
int bucket = KeyHashUtils.hash(key.key()) % bucketSize;
long bucketOffset = offset + bucket * _4k;

String cacheKey = fileId + "|" + bucketOffset;
byte[] block = readCache.get(cacheKey);
if (block == null) {
block = writeCache.get(cacheKey);
}
if (block == null) {
block = fileReadWrite.read(file(fileId), bucketOffset, _4k);
if (cacheType == CacheType.write) {
writeCache.put(cacheKey, block);
} else if (cacheType == CacheType.read) {
readCache.put(cacheKey, block);
}
}
Map<Key, KeyInfo> map = KeyCodec.decodeBucket(block);
KeyInfo data = map.get(key);
if (data == null) {
return KeyInfo.DELETE;
}
return data;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.netease.nim.camellia.redis.proxy.upstream.local.storage.key.slot;

import com.netease.nim.camellia.redis.proxy.upstream.kv.cache.ValueWrapper;
import com.netease.nim.camellia.redis.proxy.upstream.local.storage.cache.CacheType;
import com.netease.nim.camellia.redis.proxy.upstream.local.storage.key.Key;
import com.netease.nim.camellia.redis.proxy.upstream.local.storage.enums.FlushResult;
import com.netease.nim.camellia.redis.proxy.upstream.local.storage.enums.FlushStatus;
Expand Down Expand Up @@ -55,7 +56,7 @@ public KeyInfo get(Key key) throws IOException {
if (keyInfo == KeyInfo.DELETE) {
return null;
}
keyInfo = keyBlockReadWrite.get(slot, key);
keyInfo = keyBlockReadWrite.get(slot, key, CacheType.read);
if (keyInfo == KeyInfo.DELETE) {
return null;
}
Expand All @@ -76,7 +77,7 @@ public KeyInfo getForCompact(Key key) throws IOException {
if (keyInfo == KeyInfo.DELETE) {
return null;
}
keyInfo = keyBlockReadWrite.getForCompact(slot, key);
keyInfo = keyBlockReadWrite.get(slot, key, CacheType.write);
if (keyInfo == KeyInfo.DELETE) {
return null;
}
Expand Down

0 comments on commit 67e07b4

Please sign in to comment.