Skip to content

Commit

Permalink
Enabling BlobDB for column families with static data
Browse files Browse the repository at this point in the history
Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net>
  • Loading branch information
fab-10 committed May 19, 2023
1 parent ab4020e commit f944e0b
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import org.bouncycastle.util.Arrays;

public enum KeyValueSegmentIdentifier implements SegmentIdentifier {
BLOCKCHAIN(new byte[] {1}),
BLOCKCHAIN(new byte[] {1}, true),
WORLD_STATE(new byte[] {2}, new int[] {0, 1}),
PRIVATE_TRANSACTIONS(new byte[] {3}),
PRIVATE_STATE(new byte[] {4}),
Expand All @@ -28,7 +28,7 @@ public enum KeyValueSegmentIdentifier implements SegmentIdentifier {
CODE_STORAGE(new byte[] {7}, new int[] {2}),
ACCOUNT_STORAGE_STORAGE(new byte[] {8}, new int[] {2}),
TRIE_BRANCH_STORAGE(new byte[] {9}, new int[] {2}),
TRIE_LOG_STORAGE(new byte[] {10}, new int[] {2}),
TRIE_LOG_STORAGE(new byte[] {10}, new int[] {2}, true),
VARIABLES(new byte[] {11}), // formerly GOQUORUM_PRIVATE_WORLD_STATE

// previously supported GoQuorum private states
Expand All @@ -44,14 +44,25 @@ public enum KeyValueSegmentIdentifier implements SegmentIdentifier {

private final byte[] id;
private final int[] versionList;
private final boolean containsStaticData;

KeyValueSegmentIdentifier(final byte[] id) {
this(id, new int[] {0, 1, 2});
}

KeyValueSegmentIdentifier(final byte[] id, final boolean containsStaticData) {
this(id, new int[] {0, 1, 2}, containsStaticData);
}

KeyValueSegmentIdentifier(final byte[] id, final int[] versionList) {
this(id, versionList, false);
}

KeyValueSegmentIdentifier(
final byte[] id, final int[] versionList, final boolean containsStaticData) {
this.id = id;
this.versionList = versionList;
this.containsStaticData = containsStaticData;
}

@Override
Expand All @@ -64,6 +75,11 @@ public byte[] getId() {
return id;
}

@Override
public boolean containsStaticData() {
return containsStaticData;
}

@Override
public boolean includeInDatabaseVersion(final int version) {
return Arrays.contains(versionList, version);
Expand Down
2 changes: 1 addition & 1 deletion plugin-api/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ Calculated : ${currentHash}
tasks.register('checkAPIChanges', FileStateChecker) {
description = "Checks that the API for the Plugin-API project does not change without deliberate thought"
files = sourceSets.main.allJava.files
knownHash = '3/qsZ9+jA10YbctpPtQ5Rn7cvYHwKRWBv6jXa+7WQMY='
knownHash = 'kfPcUkAYaOMIvnckj9QVufV9Qg0BJJsF17u0itdjY9o='
}
check.dependsOn('checkAPIChanges')

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,14 @@ public interface SegmentIdentifier {
default boolean includeInDatabaseVersion(final int version) {
return true;
}

/**
* Define if this segment contains data that is never updated, but only added and optionally
* deleted. Example is append only data like the blockchain.
* This information can be used by the underlying implementation to apply specific
* optimization for this use case.
*
* @return true if the segment contains only static data
*/
boolean containsStaticData();
}
Original file line number Diff line number Diff line change
Expand Up @@ -151,16 +151,7 @@ public RocksDBColumnarKeyValueStorage(
.noneMatch(existed -> Arrays.equals(existed, ignorableSegment.getId())))
.forEach(trimmedSegments::remove);
columnDescriptors =
trimmedSegments.stream()
.map(
segment ->
new ColumnFamilyDescriptor(
segment.getId(),
new ColumnFamilyOptions()
.setTtl(0)
.setCompressionType(CompressionType.LZ4_COMPRESSION)
.setTableFormatConfig(createBlockBasedTableConfig(configuration))))
.collect(Collectors.toList());
trimmedSegments.stream().map(this::createColumnDescriptor).collect(Collectors.toList());
columnDescriptors.add(
new ColumnFamilyDescriptor(
DEFAULT_COLUMN.getBytes(StandardCharsets.UTF_8),
Expand All @@ -178,6 +169,24 @@ public RocksDBColumnarKeyValueStorage(
}
}

private ColumnFamilyDescriptor createColumnDescriptor(final SegmentIdentifier segment) {
final var options =
new ColumnFamilyOptions()
.setTtl(0)
.setCompressionType(CompressionType.LZ4_COMPRESSION)
.setTableFormatConfig(createBlockBasedTableConfig(configuration));

if (segment.containsStaticData()) {
options
.setEnableBlobFiles(true)
.setEnableBlobGarbageCollection(false)
.setMinBlobSize(100)
.setBlobCompressionType(CompressionType.LZ4_COMPRESSION);
}

return new ColumnFamilyDescriptor(segment.getId(), options);
}

private void setGlobalOptions(final RocksDBConfiguration configuration, final Statistics stats) {
options = new DBOptions();
options
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -261,14 +261,22 @@ public void dbWillBeBackwardIncompatibleAfterExperimentalSegmentsAreAdded() thro
public enum TestSegment implements SegmentIdentifier {
FOO(new byte[] {1}),
BAR(new byte[] {2}),
EXPERIMENTAL(new byte[] {3});
EXPERIMENTAL(new byte[] {3}),

STATIC_DATA(new byte[] {4}, true);

private final byte[] id;
private final String nameAsUtf8;
private final boolean containsStaticData;

TestSegment(final byte[] id) {
this(id, false);
}

TestSegment(final byte[] id, final boolean containsStaticData) {
this.id = id;
this.nameAsUtf8 = new String(id, StandardCharsets.UTF_8);
this.containsStaticData = containsStaticData;
}

@Override
Expand All @@ -280,6 +288,11 @@ public String getName() {
public byte[] getId() {
return id;
}

@Override
public boolean containsStaticData() {
return containsStaticData;
}
}

protected abstract SegmentedKeyValueStorage<RocksDbSegmentIdentifier> createSegmentedStore()
Expand Down

0 comments on commit f944e0b

Please sign in to comment.