Skip to content

Commit

Permalink
fix: adjusted exception handling to throw ParseException
Browse files Browse the repository at this point in the history
Signed-off-by: Matt Peterson <[email protected]>
  • Loading branch information
mattp-swirldslabs committed Aug 23, 2024
1 parent e2f3605 commit 262347b
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,9 @@ private void singleBlock(
} catch (IOException e) {
LOGGER.log(ERROR, "Error reading block number: {0}", blockNumber);
singleBlockResponseStreamObserver.onNext(buildSingleBlockNotAvailableResponse());
} catch (ParseException e) {
LOGGER.log(ERROR, "Error parsing block number: {0}", blockNumber);
singleBlockResponseStreamObserver.onNext(buildSingleBlockNotAvailableResponse());
}
} else {
LOGGER.log(ERROR, "Unary singleBlock gRPC method is not currently running");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ class BlockAsDirReader implements BlockReader<Block> {
*/
@NonNull
@Override
public Optional<Block> read(final long blockNumber) throws IOException {
public Optional<Block> read(final long blockNumber) throws IOException, ParseException {

// Verify path attributes of the block node root path
if (isPathDisqualified(blockNodeRootPath)) {
Expand Down Expand Up @@ -134,7 +134,7 @@ public Optional<Block> read(final long blockNumber) throws IOException {

@NonNull
private Optional<BlockItem> readBlockItem(@NonNull final String blockItemPath)
throws IOException {
throws IOException, ParseException {

try (final FileInputStream fis = new FileInputStream(blockItemPath)) {

Expand All @@ -156,7 +156,7 @@ private Optional<BlockItem> readBlockItem(@NonNull final String blockItemPath)
throw io;
} catch (ParseException e) {
LOGGER.log(ERROR, "Error parsing block item: " + blockItemPath, e);
throw new IOException(e);
throw e;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.hedera.block.server.persistence.storage.read;

import com.hedera.pbj.runtime.ParseException;
import edu.umd.cs.findbugs.annotations.NonNull;
import java.io.IOException;
import java.util.Optional;
Expand All @@ -33,7 +34,10 @@ public interface BlockReader<V> {
* @param blockNumber the block number of the block to read
* @return the block with the given block number
* @throws IOException if an I/O error occurs fetching the block
* @throws ParseException if the PBJ codec encounters a problem caused by I/O issues, malformed
* input data, or any other reason that prevents the parse() method from completing the
* operation when fetching the block.
*/
@NonNull
Optional<V> read(final long blockNumber) throws IOException;
Optional<V> read(final long blockNumber) throws IOException, ParseException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import com.hedera.hapi.block.*;
import com.hedera.hapi.block.stream.Block;
import com.hedera.hapi.block.stream.BlockItem;
import com.hedera.pbj.runtime.ParseException;
import com.hedera.pbj.runtime.io.buffer.Bytes;
import com.lmax.disruptor.BatchEventProcessor;
import com.lmax.disruptor.EventHandler;
Expand Down Expand Up @@ -429,7 +430,8 @@ public void testSubAndUnsubWhileStreaming() throws IOException {
}

@Test
public void testMediatorExceptionHandlingWhenPersistenceFailure() throws IOException {
public void testMediatorExceptionHandlingWhenPersistenceFailure()
throws IOException, ParseException {
final ConcurrentHashMap<
EventHandler<ObjectEvent<SubscribeStreamResponse>>,
BatchEventProcessor<ObjectEvent<SubscribeStreamResponse>>>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import com.hedera.hapi.block.SubscribeStreamResponse;
import com.hedera.hapi.block.stream.Block;
import com.hedera.hapi.block.stream.BlockItem;
import com.hedera.pbj.runtime.ParseException;
import io.grpc.stub.ServerCalls;
import io.grpc.stub.StreamObserver;
import io.helidon.webserver.grpc.GrpcService;
Expand Down Expand Up @@ -127,7 +128,7 @@ public void testProto() throws IOException, NoSuchAlgorithmException {
}

@Test
void testSingleBlockHappyPath() throws IOException {
void testSingleBlockHappyPath() throws IOException, ParseException {

final BlockReader<Block> blockReader = BlockAsDirReaderBuilder.newBuilder(config).build();
final BlockStreamService blockStreamService =
Expand Down Expand Up @@ -168,7 +169,7 @@ void testSingleBlockHappyPath() throws IOException {
}

@Test
void testSingleBlockNotFoundPath() throws IOException {
void testSingleBlockNotFoundPath() throws IOException, ParseException {

// Get the block so we can verify the response payload
when(blockReader.read(1)).thenReturn(Optional.empty());
Expand Down Expand Up @@ -218,12 +219,11 @@ void testSingleBlockServiceNotAvailable() throws InvalidProtocolBufferException
}

@Test
public void testSingleBlockIOExceptionPath() throws IOException {
public void testSingleBlockIOExceptionPath() throws IOException, ParseException {
final BlockStreamService blockStreamService =
new BlockStreamService(
streamMediator, blockReader, serviceStatus, blockNodeContext);

// Set the service status to not running
when(serviceStatus.isRunning()).thenReturn(true);
when(blockReader.read(1)).thenThrow(new IOException("Test exception"));

Expand All @@ -239,6 +239,27 @@ public void testSingleBlockIOExceptionPath() throws IOException {
verify(responseObserver, times(1)).onNext(expectedNotAvailable);
}

@Test
public void testSingleBlockParseExceptionPath() throws IOException, ParseException {
final BlockStreamService blockStreamService =
new BlockStreamService(
streamMediator, blockReader, serviceStatus, blockNodeContext);

when(serviceStatus.isRunning()).thenReturn(true);
when(blockReader.read(1)).thenThrow(new ParseException("Test exception"));

final com.hedera.hapi.block.protoc.SingleBlockResponse expectedNotAvailable =
buildSingleBlockNotAvailableResponse();

// Build a request to invoke the service
final com.hedera.hapi.block.protoc.SingleBlockRequest singleBlockRequest =
com.hedera.hapi.block.protoc.SingleBlockRequest.newBuilder()
.setBlockNumber(1)
.build();
blockStreamService.protocSingleBlock(singleBlockRequest, responseObserver);
verify(responseObserver, times(1)).onNext(expectedNotAvailable);
}

@Test
public void testUpdateInvokesRoutingWithLambdas() {

Expand All @@ -260,7 +281,7 @@ public void testUpdateInvokesRoutingWithLambdas() {
}

@Test
public void testProtocParseExceptionHandling() throws IOException {
public void testProtocParseExceptionHandling() {
// TODO: We might be able to remove this test once we can remove the Translator class

final BlockStreamService blockStreamService =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import com.hedera.block.server.util.TestUtils;
import com.hedera.hapi.block.stream.Block;
import com.hedera.hapi.block.stream.BlockItem;
import com.hedera.pbj.runtime.ParseException;
import edu.umd.cs.findbugs.annotations.NonNull;
import java.io.FileInputStream;
import java.io.FileOutputStream;
Expand Down Expand Up @@ -82,14 +83,14 @@ public void tearDown() {
}

@Test
public void testReadBlockDoesNotExist() throws IOException {
public void testReadBlockDoesNotExist() throws IOException, ParseException {
final BlockReader<Block> blockReader = BlockAsDirReaderBuilder.newBuilder(config).build();
final Optional<Block> blockOpt = blockReader.read(10000);
assertTrue(blockOpt.isEmpty());
}

@Test
public void testReadPermsRepairSucceeded() throws IOException {
public void testReadPermsRepairSucceeded() throws IOException, ParseException {
final List<BlockItem> blockItems = generateBlockItems(1);

final BlockWriter<BlockItem> blockWriter =
Expand All @@ -109,7 +110,7 @@ public void testReadPermsRepairSucceeded() throws IOException {
}

@Test
public void testRemoveBlockReadPermsRepairFailed() throws IOException {
public void testRemoveBlockReadPermsRepairFailed() throws IOException, ParseException {
final List<BlockItem> blockItems = generateBlockItems(1);

final BlockWriter<BlockItem> blockWriter =
Expand Down Expand Up @@ -148,7 +149,7 @@ public void testRemoveBlockItemReadPerms() throws IOException {
}

@Test
public void testPathIsNotDirectory() throws IOException {
public void testPathIsNotDirectory() throws IOException, ParseException {
final List<BlockItem> blockItems = generateBlockItems(1);
final Path blockNodeRootPath = Path.of(config.rootPath());

Expand All @@ -163,7 +164,7 @@ public void testPathIsNotDirectory() throws IOException {
}

@Test
public void testRepairReadPermsFails() throws IOException {
public void testRepairReadPermsFails() throws IOException, ParseException {

final List<BlockItem> blockItems = generateBlockItems(1);

Expand All @@ -186,7 +187,7 @@ public void testRepairReadPermsFails() throws IOException {
}

@Test
public void testBlockNodePathReadFails() throws IOException {
public void testBlockNodePathReadFails() throws IOException, ParseException {

// Remove read perm on the root path
removePathReadPerms(Path.of(config.rootPath()));
Expand All @@ -202,7 +203,7 @@ public void testBlockNodePathReadFails() throws IOException {
}

@Test
public void testParseExceptionHandling() throws IOException {
public void testParseExceptionHandling() throws IOException, ParseException {
final List<BlockItem> blockItems = generateBlockItems(1);

final BlockWriter<BlockItem> blockWriter =
Expand Down Expand Up @@ -236,7 +237,7 @@ public void testParseExceptionHandling() throws IOException {

// Read the block. The block item file is corrupted, so the read should fail with a
// ParseException
assertThrows(IOException.class, () -> blockReader.read(1));
assertThrows(ParseException.class, () -> blockReader.read(1));
}

public static void removeBlockReadPerms(int blockNumber, final PersistenceStorageConfig config)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import com.hedera.block.server.util.TestUtils;
import com.hedera.hapi.block.stream.Block;
import com.hedera.hapi.block.stream.BlockItem;
import com.hedera.pbj.runtime.ParseException;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
Expand Down Expand Up @@ -63,7 +64,7 @@ public void setUp() throws IOException {
}

@Test
public void testRemoveNonExistentBlock() throws IOException {
public void testRemoveNonExistentBlock() throws IOException, ParseException {

// Write a block
final var blockItems = PersistTestUtils.generateBlockItems(1);
Expand Down Expand Up @@ -96,7 +97,7 @@ public void testRemoveNonExistentBlock() throws IOException {
}

@Test
public void testRemoveBlockWithPermException() throws IOException {
public void testRemoveBlockWithPermException() throws IOException, ParseException {

// Write a block
final List<BlockItem> blockItems = PersistTestUtils.generateBlockItems(1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import com.hedera.block.server.util.TestUtils;
import com.hedera.hapi.block.stream.Block;
import com.hedera.hapi.block.stream.BlockItem;
import com.hedera.pbj.runtime.ParseException;
import edu.umd.cs.findbugs.annotations.NonNull;
import java.io.IOException;
import java.nio.file.Files;
Expand Down Expand Up @@ -79,7 +80,7 @@ public void tearDown() {
}

@Test
public void testWriterAndReaderHappyPath() throws IOException {
public void testWriterAndReaderHappyPath() throws IOException, ParseException {

// Write a block
final List<BlockItem> blockItems = PersistTestUtils.generateBlockItems(1);
Expand Down Expand Up @@ -116,7 +117,7 @@ public void testWriterAndReaderHappyPath() throws IOException {
}

@Test
public void testRemoveBlockWritePerms() throws IOException {
public void testRemoveBlockWritePerms() throws IOException, ParseException {

final List<BlockItem> blockItems = PersistTestUtils.generateBlockItems(1);

Expand Down Expand Up @@ -177,7 +178,7 @@ public void testUnrecoverableIOExceptionOnWrite() throws IOException {
}

@Test
public void testRemoveRootDirReadPerm() throws IOException {
public void testRemoveRootDirReadPerm() throws IOException, ParseException {
final List<BlockItem> blockItems = PersistTestUtils.generateBlockItems(1);

final BlockWriter<BlockItem> blockWriter =
Expand Down Expand Up @@ -205,7 +206,7 @@ public void testRemoveRootDirReadPerm() throws IOException {
}

@Test
public void testPartialBlockRemoval() throws IOException {
public void testPartialBlockRemoval() throws IOException, ParseException {
final List<BlockItem> blockItems = PersistTestUtils.generateBlockItems(3);
final BlockRemover blockRemover =
new BlockAsDirRemover(Path.of(testConfig.rootPath()), FileUtils.defaultPerms);
Expand Down

0 comments on commit 262347b

Please sign in to comment.