From b6e536972fe834cb30f807251c090d0fcc1ef444 Mon Sep 17 00:00:00 2001 From: georgi-l95 Date: Wed, 27 Nov 2024 23:39:49 +0200 Subject: [PATCH] fix loading block manager in consumer mode Signed-off-by: georgi-l95 --- .../BlockAsDirBlockStreamManager.java | 7 +++ .../BlockAsFileBlockStreamManager.java | 7 +++ .../generator/BlockAsFileLargeDataSets.java | 8 ++++ .../generator/BlockStreamManager.java | 5 +++ .../simulator/mode/PublisherModeHandler.java | 1 + .../BlockAsDirBlockStreamManagerTest.java | 7 ++- .../BlockAsFileBlockStreamManagerTest.java | 45 ++++++++++--------- 7 files changed, 56 insertions(+), 24 deletions(-) diff --git a/simulator/src/main/java/com/hedera/block/simulator/generator/BlockAsDirBlockStreamManager.java b/simulator/src/main/java/com/hedera/block/simulator/generator/BlockAsDirBlockStreamManager.java index 42f4aa625..c5a5ab4a3 100644 --- a/simulator/src/main/java/com/hedera/block/simulator/generator/BlockAsDirBlockStreamManager.java +++ b/simulator/src/main/java/com/hedera/block/simulator/generator/BlockAsDirBlockStreamManager.java @@ -63,6 +63,13 @@ public class BlockAsDirBlockStreamManager implements BlockStreamManager { @Inject public BlockAsDirBlockStreamManager(@NonNull BlockGeneratorConfig blockGeneratorConfig) { this.rootFolder = blockGeneratorConfig.folderRootPath(); + } + + /** + * Initialize the block stream manager and load blocks into memory. + */ + @Override + public void init() { try { this.loadBlocks(); } catch (IOException | ParseException | IllegalArgumentException e) { diff --git a/simulator/src/main/java/com/hedera/block/simulator/generator/BlockAsFileBlockStreamManager.java b/simulator/src/main/java/com/hedera/block/simulator/generator/BlockAsFileBlockStreamManager.java index a9339c16e..cdb9cfd4a 100644 --- a/simulator/src/main/java/com/hedera/block/simulator/generator/BlockAsFileBlockStreamManager.java +++ b/simulator/src/main/java/com/hedera/block/simulator/generator/BlockAsFileBlockStreamManager.java @@ -59,6 +59,13 @@ public class BlockAsFileBlockStreamManager implements BlockStreamManager { @Inject public BlockAsFileBlockStreamManager(@NonNull BlockGeneratorConfig blockStreamConfig) { this.rootFolder = blockStreamConfig.folderRootPath(); + } + + /** + * Initialize the block stream manager and load blocks into memory. + */ + @Override + public void init() { try { this.loadBlocks(); } catch (IOException | ParseException | IllegalArgumentException e) { diff --git a/simulator/src/main/java/com/hedera/block/simulator/generator/BlockAsFileLargeDataSets.java b/simulator/src/main/java/com/hedera/block/simulator/generator/BlockAsFileLargeDataSets.java index 6fd8ef4d3..ff775e9bf 100644 --- a/simulator/src/main/java/com/hedera/block/simulator/generator/BlockAsFileLargeDataSets.java +++ b/simulator/src/main/java/com/hedera/block/simulator/generator/BlockAsFileLargeDataSets.java @@ -65,6 +65,14 @@ public BlockAsFileLargeDataSets(@NonNull BlockGeneratorConfig config) { this.formatString = "%0" + config.paddedLength() + "d" + config.fileExtension(); } + /** + * Initialize the block stream manager and load blocks into memory. + */ + @Override + public void init() { + // Do nothing, because we don't have real initializing and loading blocks into memory for this implementation. + } + @Override public GenerationMode getGenerationMode() { return GenerationMode.DIR; diff --git a/simulator/src/main/java/com/hedera/block/simulator/generator/BlockStreamManager.java b/simulator/src/main/java/com/hedera/block/simulator/generator/BlockStreamManager.java index b6012f068..2f62f3022 100644 --- a/simulator/src/main/java/com/hedera/block/simulator/generator/BlockStreamManager.java +++ b/simulator/src/main/java/com/hedera/block/simulator/generator/BlockStreamManager.java @@ -25,6 +25,11 @@ /** The block stream manager interface. */ public interface BlockStreamManager { + /** + * Initialize the block stream manager and load blocks into memory. + */ + void init(); + /** * Get the generation mode. * diff --git a/simulator/src/main/java/com/hedera/block/simulator/mode/PublisherModeHandler.java b/simulator/src/main/java/com/hedera/block/simulator/mode/PublisherModeHandler.java index 7b3ca104e..6e80b8580 100644 --- a/simulator/src/main/java/com/hedera/block/simulator/mode/PublisherModeHandler.java +++ b/simulator/src/main/java/com/hedera/block/simulator/mode/PublisherModeHandler.java @@ -89,6 +89,7 @@ public PublisherModeHandler( } public void init() { + blockStreamManager.init(); publishStreamGrpcClient.init(); LOGGER.log(INFO, "gRPC Channel initialized for publishing blocks."); } diff --git a/simulator/src/test/java/com/hedera/block/simulator/generator/BlockAsDirBlockStreamManagerTest.java b/simulator/src/test/java/com/hedera/block/simulator/generator/BlockAsDirBlockStreamManagerTest.java index 54236fd7c..430a2795b 100644 --- a/simulator/src/test/java/com/hedera/block/simulator/generator/BlockAsDirBlockStreamManagerTest.java +++ b/simulator/src/test/java/com/hedera/block/simulator/generator/BlockAsDirBlockStreamManagerTest.java @@ -46,6 +46,7 @@ void getGenerationMode() { @Test void getNextBlockItem() throws IOException, BlockSimulatorParsingException { BlockStreamManager blockStreamManager = getBlockAsDirBlockStreamManager(getAbsoluteFolder(rootFolder)); + blockStreamManager.init(); for (int i = 0; i < 1000; i++) { assertNotNull(blockStreamManager.getNextBlockItem()); @@ -55,6 +56,7 @@ void getNextBlockItem() throws IOException, BlockSimulatorParsingException { @Test void getNextBlock() throws IOException, BlockSimulatorParsingException { BlockStreamManager blockStreamManager = getBlockAsDirBlockStreamManager(getAbsoluteFolder(rootFolder)); + blockStreamManager.init(); for (int i = 0; i < 3000; i++) { assertNotNull(blockStreamManager.getNextBlock()); @@ -71,7 +73,8 @@ void BlockAsFileBlockStreamManagerInvalidRootPath() { private BlockStreamManager getBlockAsDirBlockStreamManager(String rootFolder) { final BlockGeneratorConfig blockGeneratorConfig = new BlockGeneratorConfig( GenerationMode.DIR, rootFolder, "BlockAsDirBlockStreamManager", 36, ".blk", 0, 0); - - return new BlockAsDirBlockStreamManager(blockGeneratorConfig); + final BlockStreamManager blockStreamManager = new BlockAsDirBlockStreamManager(blockGeneratorConfig); + blockStreamManager.init(); + return blockStreamManager; } } diff --git a/simulator/src/test/java/com/hedera/block/simulator/generator/BlockAsFileBlockStreamManagerTest.java b/simulator/src/test/java/com/hedera/block/simulator/generator/BlockAsFileBlockStreamManagerTest.java index 882db4c0d..685e2b234 100644 --- a/simulator/src/test/java/com/hedera/block/simulator/generator/BlockAsFileBlockStreamManagerTest.java +++ b/simulator/src/test/java/com/hedera/block/simulator/generator/BlockAsFileBlockStreamManagerTest.java @@ -23,27 +23,31 @@ import com.hedera.block.simulator.exception.BlockSimulatorParsingException; import java.io.IOException; import java.nio.file.Paths; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; class BlockAsFileBlockStreamManagerTest { private final String gzRootFolder = "src/main/resources/block-0.0.3/"; + private BlockStreamManager blockStreamManager; private String getAbsoluteFolder(String relativePath) { return Paths.get(relativePath).toAbsolutePath().toString(); } + @BeforeEach + void setUp() { + blockStreamManager = getBlockAsFileBlockStreamManager(getAbsoluteFolder(gzRootFolder)); + blockStreamManager.init(); + } + @Test void getGenerationMode() { - BlockStreamManager blockStreamManager = - getBlockAsFileBlockStreamManager(getAbsoluteFolder(gzRootFolder)); assertEquals(GenerationMode.DIR, blockStreamManager.getGenerationMode()); } @Test void getNextBlock() throws IOException, BlockSimulatorParsingException { - BlockStreamManager blockStreamManager = - getBlockAsFileBlockStreamManager(getAbsoluteFolder(gzRootFolder)); for (int i = 0; i < 3000; i++) { assertNotNull(blockStreamManager.getNextBlock()); } @@ -51,8 +55,6 @@ void getNextBlock() throws IOException, BlockSimulatorParsingException { @Test void getNextBlockItem() throws IOException, BlockSimulatorParsingException { - BlockStreamManager blockStreamManager = - getBlockAsFileBlockStreamManager(getAbsoluteFolder(gzRootFolder)); for (int i = 0; i < 35000; i++) { assertNotNull(blockStreamManager.getNextBlockItem()); } @@ -61,8 +63,9 @@ void getNextBlockItem() throws IOException, BlockSimulatorParsingException { @Test void loadBlockBlk() throws IOException, BlockSimulatorParsingException { String blkRootFolder = "src/test/resources/block-0.0.3-blk/"; - BlockStreamManager blockStreamManager = - getBlockAsFileBlockStreamManager(getAbsoluteFolder(blkRootFolder)); + BlockStreamManager blockStreamManager = getBlockAsFileBlockStreamManager(getAbsoluteFolder(blkRootFolder)); + blockStreamManager.init(); + assertNotNull(blockStreamManager.getNextBlock()); } @@ -70,22 +73,20 @@ void loadBlockBlk() throws IOException, BlockSimulatorParsingException { void BlockAsFileBlockStreamManagerInvalidRootPath() { assertThrows( RuntimeException.class, - () -> - getBlockAsFileBlockStreamManager( - getAbsoluteFolder("src/test/resources/BlockAsDirException/1/"))); + () -> getBlockAsFileBlockStreamManager(getAbsoluteFolder("src/test/resources/BlockAsDirException/1/"))); } - private BlockAsFileBlockStreamManager getBlockAsFileBlockStreamManager(String rootFolder) { - - BlockGeneratorConfig blockGeneratorConfig = - BlockGeneratorConfig.builder() - .generationMode(GenerationMode.DIR) - .folderRootPath(rootFolder) - .managerImplementation("BlockAsFileBlockStreamManager") - .paddedLength(36) - .fileExtension(".blk") - .build(); + private BlockStreamManager getBlockAsFileBlockStreamManager(String rootFolder) { + BlockGeneratorConfig blockGeneratorConfig = BlockGeneratorConfig.builder() + .generationMode(GenerationMode.DIR) + .folderRootPath(rootFolder) + .managerImplementation("BlockAsFileBlockStreamManager") + .paddedLength(36) + .fileExtension(".blk") + .build(); - return new BlockAsFileBlockStreamManager(blockGeneratorConfig); + BlockStreamManager blockStreamManager = new BlockAsFileBlockStreamManager(blockGeneratorConfig); + blockStreamManager.init(); + return blockStreamManager; } }