-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix:refactor out FileSystemBlockStorage and split Read and Write
Signed-off-by: Matt Peterson <[email protected]>
- Loading branch information
1 parent
2721b4b
commit bbc4066
Showing
14 changed files
with
381 additions
and
312 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
server/src/main/java/com/hedera/block/server/persistence/storage/BlockWriter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/* | ||
* Copyright (C) 2024 Hedera Hashgraph, LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.hedera.block.server.persistence.storage; | ||
|
||
import java.io.IOException; | ||
|
||
public interface BlockWriter<V> { | ||
void write(final V blockItem) throws IOException; | ||
} |
112 changes: 112 additions & 0 deletions
112
server/src/main/java/com/hedera/block/server/persistence/storage/FileSystemBlockReader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
/* | ||
* Copyright (C) 2024 Hedera Hashgraph, LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.hedera.block.server.persistence.storage; | ||
|
||
import static com.hedera.block.protos.BlockStreamService.Block; | ||
import static com.hedera.block.protos.BlockStreamService.Block.Builder; | ||
import static com.hedera.block.protos.BlockStreamService.BlockItem; | ||
import static com.hedera.block.server.Constants.BLOCK_FILE_EXTENSION; | ||
|
||
import io.helidon.config.Config; | ||
import java.io.FileInputStream; | ||
import java.io.FileNotFoundException; | ||
import java.io.IOException; | ||
import java.nio.file.Path; | ||
import java.util.Optional; | ||
|
||
public class FileSystemBlockReader implements BlockReader<Block> { | ||
|
||
private final System.Logger LOGGER = System.getLogger(getClass().getName()); | ||
|
||
final Path blockNodeRootPath; | ||
|
||
public FileSystemBlockReader(final String key, final Config config) { | ||
|
||
LOGGER.log(System.Logger.Level.INFO, "Initializing FileSystemBlockReader"); | ||
|
||
final Path blockNodeRootPath = Path.of(config.get(key).asString().get()); | ||
|
||
LOGGER.log(System.Logger.Level.INFO, config.toString()); | ||
LOGGER.log(System.Logger.Level.INFO, "Block Node Root Path: " + blockNodeRootPath); | ||
|
||
this.blockNodeRootPath = blockNodeRootPath; | ||
} | ||
|
||
public Optional<Block> read(final long blockNumber) { | ||
|
||
// Construct the path to the requested Block | ||
final Path blockPath = blockNodeRootPath.resolve(String.valueOf(blockNumber)); | ||
|
||
// Verify attributes of the Block | ||
if (!isVerified(blockPath)) { | ||
return Optional.empty(); | ||
} | ||
|
||
// There may be thousands of BlockItem files in a Block directory. | ||
// The BlockItems must be written into the Block object in order. A | ||
// DirectoryStream will iterate in any guaranteed order. To avoid sorting, | ||
// and to keep the retrieval process linear with the number of BlockItems, | ||
// Run a loop to fetch in the order we need. The loop will break when | ||
// it looks for a BlockItem file that does not exist. | ||
final Builder builder = Block.newBuilder(); | ||
for (int i = 1; ; i++) { | ||
final Path blockItemPath = blockPath.resolve(i + BLOCK_FILE_EXTENSION); | ||
try { | ||
final Optional<BlockItem> blockItemOpt = readBlockItem(blockItemPath.toString()); | ||
if (blockItemOpt.isPresent()) { | ||
builder.addBlockItems(blockItemOpt.get()); | ||
continue; | ||
} | ||
} catch (IOException io) { | ||
LOGGER.log(System.Logger.Level.ERROR, "Error reading file: " + blockItemPath, io); | ||
return Optional.empty(); | ||
} | ||
|
||
break; | ||
} | ||
|
||
// Return the Block | ||
return Optional.of(builder.build()); | ||
} | ||
|
||
private Optional<BlockItem> readBlockItem(final String blockItemPath) throws IOException { | ||
try (FileInputStream fis = new FileInputStream(blockItemPath)) { | ||
return Optional.of(BlockItem.parseFrom(fis)); | ||
} catch (FileNotFoundException io) { | ||
// The outer loop caller will continue to query | ||
// for the next BlockItem file based on the index | ||
// until the FileNotFoundException is thrown. | ||
// It's expected that this exception will be caught | ||
// at the end of every query. | ||
return Optional.empty(); | ||
} | ||
} | ||
|
||
private boolean isVerified(final Path blockPath) { | ||
if (!blockPath.toFile().isDirectory()) { | ||
LOGGER.log(System.Logger.Level.ERROR, "Block directory not found: " + blockPath); | ||
return false; | ||
} | ||
|
||
if (!blockPath.toFile().canRead()) { | ||
LOGGER.log(System.Logger.Level.ERROR, "Block directory not readable: " + blockPath); | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
} |
Oops, something went wrong.