Skip to content

Commit

Permalink
(fix) key not deleted in truncatePrefix (#806)
Browse files Browse the repository at this point in the history
  • Loading branch information
killme2008 authored Mar 30, 2022
1 parent 08b5348 commit 274e6d9
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
import com.alipay.sofa.jraft.util.BytesUtil;
import com.alipay.sofa.jraft.util.DebugStatistics;
import com.alipay.sofa.jraft.util.Describer;
import com.alipay.sofa.jraft.util.OnlyForTest;
import com.alipay.sofa.jraft.util.Requires;
import com.alipay.sofa.jraft.util.StorageOptionsFactory;
import com.alipay.sofa.jraft.util.Utils;
Expand Down Expand Up @@ -435,18 +436,7 @@ public LogEntry getEntry(final long index) {
if (this.hasLoadFirstLogIndex && index < this.firstLogIndex) {
return null;
}
final byte[] keyBytes = getKeyBytes(index);
final byte[] bs = onDataGet(index, getValueFromRocksDB(keyBytes));
if (bs != null) {
final LogEntry entry = this.logEntryDecoder.decode(bs);
if (entry != null) {
return entry;
} else {
LOG.error("Bad log entry format for index={}, the log data is: {}.", index, BytesUtil.toHex(bs));
// invalid data remove? TODO
return null;
}
}
return getEntryFromDB(index);
} catch (final RocksDBException | IOException e) {
LOG.error("Fail to get log entry at index {} in data path: {}.", index, this.path, e);
} finally {
Expand All @@ -455,6 +445,23 @@ public LogEntry getEntry(final long index) {
return null;
}

@OnlyForTest
LogEntry getEntryFromDB(final long index) throws IOException, RocksDBException {
final byte[] keyBytes = getKeyBytes(index);
final byte[] bs = onDataGet(index, getValueFromRocksDB(keyBytes));
if (bs != null) {
final LogEntry entry = this.logEntryDecoder.decode(bs);
if (entry != null) {
return entry;
} else {
LOG.error("Bad log entry format for index={}, the log data is: {}.", index, BytesUtil.toHex(bs));
// invalid data remove? TODO
return null;
}
}
return null;
}

protected byte[] getValueFromRocksDB(final byte[] keyBytes) throws RocksDBException {
checkState();
return this.db.get(this.defaultHandle, keyBytes);
Expand Down Expand Up @@ -589,11 +596,12 @@ private void truncatePrefixInBackground(final long startIndex, final long firstI
// Note https://github.com/facebook/rocksdb/wiki/Delete-A-Range-Of-Keys
final byte[] startKey = getKeyBytes(startIndex);
final byte[] endKey = getKeyBytes(firstIndexKept);
// deleteRange to delete all keys in range.
db.deleteRange(this.defaultHandle, startKey, endKey);
db.deleteRange(this.confHandle, startKey, endKey);
// deleteFilesInRanges to speedup reclaiming disk space on write-heavy load.
db.deleteFilesInRanges(this.defaultHandle, Arrays.asList(startKey, endKey), false);
db.deleteFilesInRanges(this.confHandle, Arrays.asList(startKey, endKey), false);
// After deleteFilesInrange, some keys in the range may still exist in the database, so we have to compactionRange.
db.compactRange(this.defaultHandle, startKey, endKey);
db.compactRange(this.confHandle, startKey, endKey);
} catch (final RocksDBException | IOException e) {
LOG.error("Fail to truncatePrefix in data path: {}, firstIndexKept={}.", this.path, firstIndexKept, e);
} finally {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,16 +164,20 @@ public void testReset() {
}

@Test
public void testTruncatePrefix() {
public void testTruncatePrefix() throws Exception {
final List<LogEntry> entries = TestUtils.mockEntries();

assertEquals(10, this.logStorage.appendEntries(entries));
this.logStorage.truncatePrefix(5);
Thread.sleep(1000);
assertEquals(5, this.logStorage.getFirstLogIndex());
assertEquals(9, this.logStorage.getLastLogIndex());
for (int i = 0; i < 10; i++) {
if (i < 5) {
assertNull(this.logStorage.getEntry(i));
if (this.logStorage instanceof RocksDBLogStorage) {
assertNull(((RocksDBLogStorage) this.logStorage).getEntryFromDB(i));
}
} else {
Assert.assertEquals(entries.get(i), this.logStorage.getEntry(i));
}
Expand Down

0 comments on commit 274e6d9

Please sign in to comment.