Skip to content

Commit

Permalink
feat: optimize key/value manifest (#364)
Browse files Browse the repository at this point in the history
  • Loading branch information
caojiajun committed Jan 14, 2025
1 parent 8ad38b9 commit cf0d772
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,9 @@ public SlotInfo expand(short slot) throws IOException {
long fileId = slotInfo.fileId();
long offset = slotInfo.offset();
int capacity = slotInfo.capacity();
if (capacity * 2 <= 0) {
throw new IOException("slot capacity exceed");
}
int bitsStep = capacity / LocalStorageConstants._64k;
BitSet bits = fileBitsMap.get(fileId);
int bitsStart = (int)(offset / LocalStorageConstants._64k);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.locks.ReentrantLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

import static com.netease.nim.camellia.redis.proxy.upstream.local.storage.constants.LocalStorageConstants.data_file_size;

Expand All @@ -35,7 +36,7 @@ public class ValueManifest implements IValueManifest {
private final ConcurrentHashMap<Long, MappedByteBuffer> bitsMmp = new ConcurrentHashMap<>();
private final ConcurrentHashMap<Long, MappedByteBuffer> slotsMmp = new ConcurrentHashMap<>();

private final ReentrantLock lock = new ReentrantLock();
private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();

public ValueManifest(String dir) {
this.dir = dir;
Expand Down Expand Up @@ -203,7 +204,18 @@ private BlockLocation allocate0(long fileId) {
}

private long selectFileId(BlockType blockType, int index) throws IOException {
lock.lock();
lock.readLock().lock();
try {
List<Long> list = fileIdMap.get(blockType);
if (list != null && index < list.size()) {
return list.get(index);
}
} catch (Exception e) {
logger.error("select fileId error", e);
} finally {
lock.readLock().unlock();
}
lock.writeLock().lock();
try {
List<Long> list = fileIdMap.computeIfAbsent(blockType, k -> new ArrayList<>());
if (index < list.size()) {
Expand All @@ -213,7 +225,7 @@ private long selectFileId(BlockType blockType, int index) throws IOException {
list.add(fileId);
return fileId;
} finally {
lock.unlock();
lock.writeLock().unlock();
}
}

Expand Down Expand Up @@ -285,5 +297,7 @@ private void loadIndexFile(File file) throws IOException {
FileChannel slotFileChannel = FileChannel.open(Paths.get(slotFile), StandardOpenOption.READ, StandardOpenOption.WRITE);
MappedByteBuffer map = slotFileChannel.map(FileChannel.MapMode.READ_WRITE, 0, blockType.valueSlotManifestSize(data_file_size));
slotsMmp.put(fileId, map);

logger.info("load index file = {}", file.getName());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package com.netease.nim.camellia.redis.proxy.test;

import org.junit.Assert;
import org.junit.Test;

import java.util.BitSet;

import static com.netease.nim.camellia.redis.proxy.upstream.local.storage.constants.LocalStorageConstants._4k;
import static com.netease.nim.camellia.redis.proxy.upstream.local.storage.constants.LocalStorageConstants.data_file_size;

/**
* Created by caojiajun on 2025/1/14
*/
public class BitSetTest {

@Test
public void test() {
BitSet bitSet = new BitSet((int) (data_file_size / _4k));
for (int i=0; i<100; i++) {
bitSet.set(i, true);
}
for (int i=1000; i<2000; i++) {
bitSet.set(i, true);
}

long[] longArray = bitSet.toLongArray();

BitSet bitSet1 = BitSet.valueOf(longArray);
for (int i=0; i<100; i++) {
Assert.assertTrue(bitSet1.get(i));
}
for (int i=200; i<900; i++) {
Assert.assertFalse(bitSet1.get(i));
}
for (int i=1000; i<2000; i++) {
Assert.assertTrue(bitSet1.get(i));
}
for (int i=3000; i<5000; i++) {
Assert.assertFalse(bitSet1.get(i));
}

for (int i=6000; i<7000; i++) {
bitSet1.set(i, true);
}

long[] longArray1 = bitSet1.toLongArray();
BitSet bitSet2 = BitSet.valueOf(longArray1);

for (int i=0; i<100; i++) {
Assert.assertTrue(bitSet2.get(i));
}
for (int i=200; i<900; i++) {
Assert.assertFalse(bitSet2.get(i));
}
for (int i=1000; i<2000; i++) {
Assert.assertTrue(bitSet2.get(i));
}
for (int i=3000; i<5000; i++) {
Assert.assertFalse(bitSet2.get(i));
}
for (int i=6000; i<7000; i++) {
Assert.assertTrue(bitSet2.get(i));
}

}
}

0 comments on commit cf0d772

Please sign in to comment.