56
56
import com .alipay .sofa .jraft .util .BytesUtil ;
57
57
import com .alipay .sofa .jraft .util .DebugStatistics ;
58
58
import com .alipay .sofa .jraft .util .Describer ;
59
+ import com .alipay .sofa .jraft .util .OnlyForTest ;
59
60
import com .alipay .sofa .jraft .util .Requires ;
60
61
import com .alipay .sofa .jraft .util .StorageOptionsFactory ;
61
62
import com .alipay .sofa .jraft .util .Utils ;
@@ -435,18 +436,7 @@ public LogEntry getEntry(final long index) {
435
436
if (this .hasLoadFirstLogIndex && index < this .firstLogIndex ) {
436
437
return null ;
437
438
}
438
- final byte [] keyBytes = getKeyBytes (index );
439
- final byte [] bs = onDataGet (index , getValueFromRocksDB (keyBytes ));
440
- if (bs != null ) {
441
- final LogEntry entry = this .logEntryDecoder .decode (bs );
442
- if (entry != null ) {
443
- return entry ;
444
- } else {
445
- LOG .error ("Bad log entry format for index={}, the log data is: {}." , index , BytesUtil .toHex (bs ));
446
- // invalid data remove? TODO
447
- return null ;
448
- }
449
- }
439
+ return getEntryFromDB (index );
450
440
} catch (final RocksDBException | IOException e ) {
451
441
LOG .error ("Fail to get log entry at index {} in data path: {}." , index , this .path , e );
452
442
} finally {
@@ -455,6 +445,23 @@ public LogEntry getEntry(final long index) {
455
445
return null ;
456
446
}
457
447
448
+ @ OnlyForTest
449
+ LogEntry getEntryFromDB (final long index ) throws IOException , RocksDBException {
450
+ final byte [] keyBytes = getKeyBytes (index );
451
+ final byte [] bs = onDataGet (index , getValueFromRocksDB (keyBytes ));
452
+ if (bs != null ) {
453
+ final LogEntry entry = this .logEntryDecoder .decode (bs );
454
+ if (entry != null ) {
455
+ return entry ;
456
+ } else {
457
+ LOG .error ("Bad log entry format for index={}, the log data is: {}." , index , BytesUtil .toHex (bs ));
458
+ // invalid data remove? TODO
459
+ return null ;
460
+ }
461
+ }
462
+ return null ;
463
+ }
464
+
458
465
protected byte [] getValueFromRocksDB (final byte [] keyBytes ) throws RocksDBException {
459
466
checkState ();
460
467
return this .db .get (this .defaultHandle , keyBytes );
@@ -589,11 +596,12 @@ private void truncatePrefixInBackground(final long startIndex, final long firstI
589
596
// Note https://github.com/facebook/rocksdb/wiki/Delete-A-Range-Of-Keys
590
597
final byte [] startKey = getKeyBytes (startIndex );
591
598
final byte [] endKey = getKeyBytes (firstIndexKept );
599
+ // deleteRange to delete all keys in range.
600
+ db .deleteRange (this .defaultHandle , startKey , endKey );
601
+ db .deleteRange (this .confHandle , startKey , endKey );
602
+ // deleteFilesInRanges to speedup reclaiming disk space on write-heavy load.
592
603
db .deleteFilesInRanges (this .defaultHandle , Arrays .asList (startKey , endKey ), false );
593
604
db .deleteFilesInRanges (this .confHandle , Arrays .asList (startKey , endKey ), false );
594
- // After deleteFilesInrange, some keys in the range may still exist in the database, so we have to compactionRange.
595
- db .compactRange (this .defaultHandle , startKey , endKey );
596
- db .compactRange (this .confHandle , startKey , endKey );
597
605
} catch (final RocksDBException | IOException e ) {
598
606
LOG .error ("Fail to truncatePrefix in data path: {}, firstIndexKept={}." , this .path , firstIndexKept , e );
599
607
} finally {
0 commit comments