Skip to content

Commit

Permalink
WIP proof of concept write blocks as file to disk
Browse files Browse the repository at this point in the history
Signed-off-by: Atanas Atanasov <[email protected]>
  • Loading branch information
ata-nas committed Nov 20, 2024
1 parent 5c0a707 commit 6126411
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ public static int requirePositive(final int toCheck) {
public static int requirePositive(final int toCheck, final String errorMessage) {
if (0 >= toCheck) {
final String message = Objects.isNull(errorMessage)
? "The input integer [%d] is required be positive.".formatted(toCheck)
: errorMessage;
? "The input integer [%d] is required be positive.".formatted(toCheck)
: errorMessage;
throw new IllegalArgumentException(message);
} else {
return toCheck;
Expand Down Expand Up @@ -126,8 +126,8 @@ public static long requirePositive(final long toCheck) {
public static long requirePositive(final long toCheck, final String errorMessage) {
if (0 >= toCheck) {
final String message = Objects.isNull(errorMessage)
? "The input long [%d] is required be positive.".formatted(toCheck)
: errorMessage;
? "The input long [%d] is required be positive.".formatted(toCheck)
: errorMessage;
throw new IllegalArgumentException(message);
} else {
return toCheck;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,7 @@ void testRequirePositiveFail(final int toTest) {
@ParameterizedTest
@MethodSource("com.hedera.block.common.CommonsTestUtility#positiveIntegers")
void testRequirePositiveLongPass(final long toTest) {
final Consumer<Long> asserts =
actual -> assertThat(actual).isPositive().isEqualTo(toTest);
final Consumer<Long> asserts = actual -> assertThat(actual).isPositive().isEqualTo(toTest);

final long actual = Preconditions.requirePositive(toTest);
assertThat(actual).satisfies(asserts);
Expand All @@ -180,8 +179,8 @@ void testRequirePositiveLongFail(final long toTest) {

final String testErrorMessage = "test error message";
assertThatIllegalArgumentException()
.isThrownBy(() -> Preconditions.requirePositive(toTest, testErrorMessage))
.withMessage(testErrorMessage);
.isThrownBy(() -> Preconditions.requirePositive(toTest, testErrorMessage))
.withMessage(testErrorMessage);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,13 @@
* A class that extends {@link MappedConfigSource} ir order to have project-relevant initialization.
*/
public final class ServerMappedConfigSourceInitializer {
private static final List<ConfigMapping> MAPPINGS =
List.of(
new ConfigMapping(
"consumer.timeoutThresholdMillis", "CONSUMER_TIMEOUT_THRESHOLD_MILLIS"),
new ConfigMapping(
"persistence.storage.rootPath", "PERSISTENCE_STORAGE_ROOT_PATH"),
new ConfigMapping("service.delayMillis", "SERVICE_DELAY_MILLIS"),
new ConfigMapping("mediator.ringBufferSize", "MEDIATOR_RING_BUFFER_SIZE"),
new ConfigMapping("notifier.ringBufferSize", "NOTIFIER_RING_BUFFER_SIZE"),
new ConfigMapping("persistence.storage.type", "PERSISTENCE_STORAGE_TYPE"));
private static final List<ConfigMapping> MAPPINGS = List.of(
new ConfigMapping("consumer.timeoutThresholdMillis", "CONSUMER_TIMEOUT_THRESHOLD_MILLIS"),
new ConfigMapping("persistence.storage.rootPath", "PERSISTENCE_STORAGE_ROOT_PATH"),
new ConfigMapping("service.delayMillis", "SERVICE_DELAY_MILLIS"),
new ConfigMapping("mediator.ringBufferSize", "MEDIATOR_RING_BUFFER_SIZE"),
new ConfigMapping("notifier.ringBufferSize", "NOTIFIER_RING_BUFFER_SIZE"),
new ConfigMapping("persistence.storage.type", "PERSISTENCE_STORAGE_TYPE"));

private ServerMappedConfigSourceInitializer() {}

Expand All @@ -48,8 +45,7 @@ private ServerMappedConfigSourceInitializer() {}
*/
@NonNull
public static MappedConfigSource getMappedConfigSource() {
final MappedConfigSource config =
new MappedConfigSource(SystemEnvironmentConfigSource.getInstance());
final MappedConfigSource config = new MappedConfigSource(SystemEnvironmentConfigSource.getInstance());
MAPPINGS.forEach(config::addMapping);
return config;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ public Path resolvePathToBlock(final long blockNumber) {
Preconditions.requirePositive(blockNumber); // todo do we have block number 0?
final String inputString = String.format("%0" + MAX_LONG_DIGITS + "d", blockNumber);
final String[] blockPath = inputString.split("");
final String blockFileName = blockPath[blockPath.length - 1].concat(Constants.BLOCK_FILE_EXTENSION);
blockPath[blockPath.length -1] = blockFileName;
final String blockFileName = inputString.concat(Constants.BLOCK_FILE_EXTENSION);
blockPath[blockPath.length - 1] = blockFileName;
return Paths.get(root.toAbsolutePath().toString(), blockPath);

Check warning on line 42 in server/src/main/java/com/hedera/block/server/persistence/storage/path/BlockAsFilePathResolver.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/com/hedera/block/server/persistence/storage/path/BlockAsFilePathResolver.java#L37-L42

Added lines #L37 - L42 were not covered by tests
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
* TODO: add documentation
*/
class BlockAsFileWriter extends AbstractBlockWriter<List<BlockItem>> {
private Block curentBlock; // fixme this is temporary just to explore the workflow and make proof of concept

BlockAsFileWriter(
@NonNull final BlockNodeContext blockNodeContext,
@NonNull final BlockRemover blockRemover,
Expand All @@ -43,19 +45,27 @@ class BlockAsFileWriter extends AbstractBlockWriter<List<BlockItem>> {

@Override
public Optional<List<BlockItem>> write(@NonNull final List<BlockItem> toWrite) throws IOException {
if (!toWrite.getFirst().hasBlockHeader()) {
throw new RuntimeException("Block header is missing");
// todo handle this properly for now just not to forget
// order of the items is significant
// also do we need to check for proof here or below like in dir writer?
if (toWrite.getFirst().hasBlockHeader()) {
curentBlock = Block.newBuilder().items(toWrite).build();

Check warning on line 49 in server/src/main/java/com/hedera/block/server/persistence/storage/write/BlockAsFileWriter.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/com/hedera/block/server/persistence/storage/write/BlockAsFileWriter.java#L49

Added line #L49 was not covered by tests
} else {
final List<BlockItem> currentItems = curentBlock.items();
currentItems.addAll(toWrite);
curentBlock = Block.newBuilder().items(currentItems).build();

Check warning on line 53 in server/src/main/java/com/hedera/block/server/persistence/storage/write/BlockAsFileWriter.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/com/hedera/block/server/persistence/storage/write/BlockAsFileWriter.java#L51-L53

Added lines #L51 - L53 were not covered by tests
}

if (toWrite.getLast().hasBlockProof()) {
return writeToFs(curentBlock);

Check warning on line 57 in server/src/main/java/com/hedera/block/server/persistence/storage/write/BlockAsFileWriter.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/com/hedera/block/server/persistence/storage/write/BlockAsFileWriter.java#L57

Added line #L57 was not covered by tests
} else {
return Optional.empty();

Check warning on line 59 in server/src/main/java/com/hedera/block/server/persistence/storage/write/BlockAsFileWriter.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/com/hedera/block/server/persistence/storage/write/BlockAsFileWriter.java#L59

Added line #L59 was not covered by tests
}
final Block blockToWrite = Block.newBuilder().items(toWrite).build();
final long number = toWrite.getFirst().blockHeader().number(); // fixme could be null, handle!
}

private Optional<List<BlockItem>> writeToFs(final Block blockToWrite) throws IOException {
final long number = blockToWrite.items().getFirst().blockHeader().number(); // fixme could be null, handle!

Check warning on line 64 in server/src/main/java/com/hedera/block/server/persistence/storage/write/BlockAsFileWriter.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/com/hedera/block/server/persistence/storage/write/BlockAsFileWriter.java#L64

Added line #L64 was not covered by tests

// todo we should handle cases where the block path exists, we do not expect it to
// exist at this stage, if it does, there is something wrong here
final Path blockToWritePathResolved = pathResolver.resolvePathToBlock(number);

Files.createDirectories(blockToWritePathResolved.getParent());
Files.createFile(blockToWritePathResolved);

Check warning on line 70 in server/src/main/java/com/hedera/block/server/persistence/storage/write/BlockAsFileWriter.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/com/hedera/block/server/persistence/storage/write/BlockAsFileWriter.java#L68-L70

Added lines #L68 - L70 were not covered by tests

Expand All @@ -73,6 +83,7 @@ public Optional<List<BlockItem>> write(@NonNull final List<BlockItem> toWrite) t
// todo handle properly
throw new RuntimeException(e);
}
return Optional.of(toWrite);
curentBlock = null;
return Optional.of(blockToWrite.items());

Check warning on line 87 in server/src/main/java/com/hedera/block/server/persistence/storage/write/BlockAsFileWriter.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/com/hedera/block/server/persistence/storage/write/BlockAsFileWriter.java#L84-L87

Added lines #L84 - L87 were not covered by tests
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,19 +60,16 @@ void test_VerifyAllSupportedMappingsAreAddedToInstance() throws ReflectiveOperat

for (final ConfigMapping current : SUPPORTED_MAPPINGS) {
final Predicate<ConfigMapping> predicate =
cm ->
current.mappedName().equals(cm.mappedName())
&& current.originalName().equals(cm.originalName());
cm -> current.mappedName().equals(cm.mappedName())
&& current.originalName().equals(cm.originalName());
assertTrue(
actual.stream().anyMatch(predicate),
() ->
"when testing for: [%s] it is not contained in mappings of the actual initialized object %s"
.formatted(current, actual));
() -> "when testing for: [%s] it is not contained in mappings of the actual initialized object %s"
.formatted(current, actual));
}
}

private static Queue<ConfigMapping> extractConfigMappings()
throws ReflectiveOperationException {
private static Queue<ConfigMapping> extractConfigMappings() throws ReflectiveOperationException {
final Field configMappings = MappedConfigSource.class.getDeclaredField("configMappings");
try {
configMappings.setAccessible(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ void setup() throws IOException {
@Test
void testProvidesBlockWriter() {

BlockWriter<List<BlockItem>> blockWriter =
PersistenceInjectionModule.providesBlockWriter(blockNodeContext, new NoOpRemover(), new NoOpPathResolver());
BlockWriter<List<BlockItem>> blockWriter = PersistenceInjectionModule.providesBlockWriter(
blockNodeContext, new NoOpRemover(), new NoOpPathResolver());

assertNotNull(blockWriter);
}
Expand All @@ -105,7 +105,8 @@ void testProvidesBlockWriter_IOException() {
// Expect a RuntimeException due to the IOException
assertThatRuntimeException()
.isThrownBy(() -> {
PersistenceInjectionModule.providesBlockWriter(blockNodeContext, new NoOpRemover(), new NoOpPathResolver());
PersistenceInjectionModule.providesBlockWriter(
blockNodeContext, new NoOpRemover(), new NoOpPathResolver());
})
.withCauseInstanceOf(IOException.class)
.withMessage("Failed to create BlockWriter");
Expand Down

0 comments on commit 6126411

Please sign in to comment.