From 141921dba715c22047039c76263ccc29fd62c1cd Mon Sep 17 00:00:00 2001 From: Matt Peterson Date: Thu, 1 Aug 2024 10:30:48 -0600 Subject: [PATCH] sanding Signed-off-by: Matt Peterson --- .../hedera/block/server/data/ObjectEvent.java | 2 +- .../mediator/LiveStreamMediatorImpl.java | 2 +- .../persistence/storage/BlockAsDirReader.java | 5 +++-- .../storage/BlockAsDirRemover.java | 10 +++++----- .../persistence/storage/BlockAsDirWriter.java | 7 +++++-- .../persistence/storage/BlockRemover.java | 2 +- .../hedera/block/server/producer/Util.java | 19 ++++++++++--------- server/src/test/resources/producer.sh | 2 +- 8 files changed, 27 insertions(+), 22 deletions(-) diff --git a/server/src/main/java/com/hedera/block/server/data/ObjectEvent.java b/server/src/main/java/com/hedera/block/server/data/ObjectEvent.java index dc90c498..fa141547 100644 --- a/server/src/main/java/com/hedera/block/server/data/ObjectEvent.java +++ b/server/src/main/java/com/hedera/block/server/data/ObjectEvent.java @@ -19,7 +19,7 @@ public class ObjectEvent { T val; - public void set(T val) { + public void set(final T val) { this.val = val; } diff --git a/server/src/main/java/com/hedera/block/server/mediator/LiveStreamMediatorImpl.java b/server/src/main/java/com/hedera/block/server/mediator/LiveStreamMediatorImpl.java index b4aa2081..d7d78f0a 100644 --- a/server/src/main/java/com/hedera/block/server/mediator/LiveStreamMediatorImpl.java +++ b/server/src/main/java/com/hedera/block/server/mediator/LiveStreamMediatorImpl.java @@ -121,7 +121,7 @@ public void publishEvent(final BlockItem blockItem) throws IOException { LOGGER.log(System.Logger.Level.INFO, "Send a response to end the stream"); // Publish the block for all subscribers to receive - final var endStreamResponse = buildEndStreamResponse(); + final SubscribeStreamResponse endStreamResponse = buildEndStreamResponse(); ringBuffer.publishEvent((event, sequence) -> event.set(endStreamResponse)); // Unsubscribe all downstream consumers diff --git a/server/src/main/java/com/hedera/block/server/persistence/storage/BlockAsDirReader.java b/server/src/main/java/com/hedera/block/server/persistence/storage/BlockAsDirReader.java index f8663121..2c724f4b 100644 --- a/server/src/main/java/com/hedera/block/server/persistence/storage/BlockAsDirReader.java +++ b/server/src/main/java/com/hedera/block/server/persistence/storage/BlockAsDirReader.java @@ -127,7 +127,7 @@ private Optional readBlockItem(final String blockItemPath) throws IOE return Optional.empty(); } - // FileNotFound is thrown also when a file cannot be read. + // FileNotFound is also thrown when a file cannot be read. // So re-throw here to make a different decision upstream. throw io; } @@ -171,7 +171,8 @@ private boolean isVerified(final Path path) { return true; } - protected void setPerm(Path path, Set perms) throws IOException { + protected void setPerm(final Path path, final Set perms) + throws IOException { Files.setPosixFilePermissions(path, perms); } } diff --git a/server/src/main/java/com/hedera/block/server/persistence/storage/BlockAsDirRemover.java b/server/src/main/java/com/hedera/block/server/persistence/storage/BlockAsDirRemover.java index b294c016..dc347162 100644 --- a/server/src/main/java/com/hedera/block/server/persistence/storage/BlockAsDirRemover.java +++ b/server/src/main/java/com/hedera/block/server/persistence/storage/BlockAsDirRemover.java @@ -38,12 +38,12 @@ public BlockAsDirRemover( } @Override - public void remove(long id) throws IOException { + public void remove(final long id) throws IOException { // Calculate the block path and proactively set the permissions // for removal final Path blockPath = blockNodeRootPath.resolve(String.valueOf(id)); - if (!Files.exists(blockPath)) { + if (Files.notExists(blockPath)) { LOGGER.log(System.Logger.Level.ERROR, "Block does not exist: " + id); return; } @@ -56,14 +56,14 @@ public void remove(long id) throws IOException { } } - private static boolean delete(File file) { + private static boolean delete(final File file) { // Recursively delete the contents // of the directory if (file.isDirectory()) { - File[] files = file.listFiles(); + final File[] files = file.listFiles(); if (files != null) { - for (File f : files) { + for (final File f : files) { delete(f); } } diff --git a/server/src/main/java/com/hedera/block/server/persistence/storage/BlockAsDirWriter.java b/server/src/main/java/com/hedera/block/server/persistence/storage/BlockAsDirWriter.java index 652da4fb..a3a6f711 100644 --- a/server/src/main/java/com/hedera/block/server/persistence/storage/BlockAsDirWriter.java +++ b/server/src/main/java/com/hedera/block/server/persistence/storage/BlockAsDirWriter.java @@ -92,6 +92,8 @@ public void write(final BlockItem blockItem) throws IOException { System.Logger.Level.ERROR, "Error writing the BlockItem protobuf to a file", e); + + // Remove the block if repairing the permissions fails if (retries > 0) { // Attempt to remove the block blockRemover.remove(Long.parseLong(currentBlockDir.toString())); @@ -111,7 +113,7 @@ public void write(final BlockItem blockItem) throws IOException { protected void write(final Path blockItemFilePath, final BlockItem blockItem) throws IOException { - try (FileOutputStream fos = new FileOutputStream(blockItemFilePath.toString())) { + try (final FileOutputStream fos = new FileOutputStream(blockItemFilePath.toString())) { blockItem.writeTo(fos); LOGGER.log( System.Logger.Level.INFO, @@ -172,7 +174,8 @@ private Path calculateBlockPath() { return blockNodeRootPath.resolve(currentBlockDir); } - private void createPath(Path blockNodePath, System.Logger.Level logLevel) throws IOException { + private void createPath(final Path blockNodePath, final System.Logger.Level logLevel) + throws IOException { // Initialize the Block directory if it does not exist if (Files.notExists(blockNodePath)) { Files.createDirectory(blockNodePath, filePerms); diff --git a/server/src/main/java/com/hedera/block/server/persistence/storage/BlockRemover.java b/server/src/main/java/com/hedera/block/server/persistence/storage/BlockRemover.java index 21941823..e2dd649a 100644 --- a/server/src/main/java/com/hedera/block/server/persistence/storage/BlockRemover.java +++ b/server/src/main/java/com/hedera/block/server/persistence/storage/BlockRemover.java @@ -19,5 +19,5 @@ import java.io.IOException; public interface BlockRemover { - void remove(long id) throws IOException; + void remove(final long id) throws IOException; } diff --git a/server/src/main/java/com/hedera/block/server/producer/Util.java b/server/src/main/java/com/hedera/block/server/producer/Util.java index 14f925f0..7b5230b8 100644 --- a/server/src/main/java/com/hedera/block/server/producer/Util.java +++ b/server/src/main/java/com/hedera/block/server/producer/Util.java @@ -29,17 +29,18 @@ private Util() {} public static byte[] getFakeHash(BlockItem blockItem) throws IOException, NoSuchAlgorithmException { - ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); - try (ObjectOutputStream objectOutputStream = - new ObjectOutputStream(byteArrayOutputStream)) { + + try (final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); + final ObjectOutputStream objectOutputStream = + new ObjectOutputStream(byteArrayOutputStream)) { objectOutputStream.writeObject(blockItem); - } - // Get the serialized bytes - byte[] serializedObject = byteArrayOutputStream.toByteArray(); + // Get the serialized bytes + byte[] serializedObject = byteArrayOutputStream.toByteArray(); - // Calculate the SHA-256 hash - MessageDigest digest = MessageDigest.getInstance("SHA-384"); - return digest.digest(serializedObject); + // Calculate the SHA-256 hash + MessageDigest digest = MessageDigest.getInstance("SHA-384"); + return digest.digest(serializedObject); + } } } diff --git a/server/src/test/resources/producer.sh b/server/src/test/resources/producer.sh index 593b79ab..3f2d2ce5 100755 --- a/server/src/test/resources/producer.sh +++ b/server/src/test/resources/producer.sh @@ -57,7 +57,7 @@ trap cleanup SIGINT echo "{\"block_item\": {\"start_event\": {\"creator_id\": $i},\"value\": \"Payload[...]\"}}" fi - sleep 0.5 + sleep 0.01 done if [ $iter -eq $2 ]; then