Skip to content

Commit

Permalink
fix: added static imports for Logger.Level
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 22, 2024
1 parent 99ad840 commit ebf333a
Show file tree
Hide file tree
Showing 15 changed files with 92 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import static com.hedera.block.server.Constants.SINGLE_BLOCK_METHOD_NAME;
import static com.hedera.block.server.Translator.toProtocSingleBlockResponse;
import static com.hedera.block.server.Translator.toProtocSubscribeStreamResponse;
import static java.lang.System.Logger.Level.DEBUG;
import static java.lang.System.Logger.Level.ERROR;

import com.google.protobuf.Descriptors;
import com.google.protobuf.InvalidProtocolBufferException;
Expand Down Expand Up @@ -130,7 +132,7 @@ StreamObserver<com.hedera.hapi.block.protoc.PublishStreamRequest> protocPublishB
final StreamObserver<com.hedera.hapi.block.protoc.PublishStreamResponse>
publishStreamResponseObserver) {
LOGGER.log(
System.Logger.Level.DEBUG,
DEBUG,
"Executing bidirectional publishBlockStream gRPC method");

return new ProducerBlockItemObserver(
Expand All @@ -145,7 +147,7 @@ void protocSubscribeBlockStream(
final StreamObserver<com.hedera.hapi.block.protoc.SubscribeStreamResponse>
subscribeStreamResponseObserver) {
LOGGER.log(
System.Logger.Level.DEBUG,
DEBUG,
"Executing Server Streaming subscribeBlockStream gRPC method");

// Return a custom StreamObserver to handle streaming blocks from the producer.
Expand All @@ -160,7 +162,7 @@ void protocSubscribeBlockStream(
streamMediator.subscribe(streamObserver);
} else {
LOGGER.log(
System.Logger.Level.ERROR,
ERROR,
"Server Streaming subscribeBlockStream gRPC Service is not currently running");

subscribeStreamResponseObserver.onNext(buildSubscribeStreamNotAvailableResponse());
Expand All @@ -172,15 +174,15 @@ void protocSingleBlock(
@NonNull
final StreamObserver<com.hedera.hapi.block.protoc.SingleBlockResponse>
singleBlockResponseStreamObserver) {
LOGGER.log(System.Logger.Level.DEBUG, "Executing Unary singleBlock gRPC method");
LOGGER.log(DEBUG, "Executing Unary singleBlock gRPC method");

try {
final SingleBlockRequest pbjSingleBlockRequest =
toPbjSingleBlockRequest(singleBlockRequest);
singleBlock(pbjSingleBlockRequest, singleBlockResponseStreamObserver);
} catch (ParseException e) {
LOGGER.log(
System.Logger.Level.ERROR,
ERROR,
"Error parsing protoc SingleBlockRequest: {0}",
singleBlockRequest);
singleBlockResponseStreamObserver.onNext(buildSingleBlockNotAvailableResponse());
Expand All @@ -193,15 +195,15 @@ private void singleBlock(
final StreamObserver<com.hedera.hapi.block.protoc.SingleBlockResponse>
singleBlockResponseStreamObserver) {

LOGGER.log(System.Logger.Level.DEBUG, "Executing Unary singleBlock gRPC method");
LOGGER.log(DEBUG, "Executing Unary singleBlock gRPC method");

if (serviceStatus.isRunning()) {
final long blockNumber = singleBlockRequest.blockNumber();
try {
final Optional<Block> blockOpt = blockReader.read(blockNumber);
if (blockOpt.isPresent()) {
LOGGER.log(
System.Logger.Level.DEBUG,
DEBUG,
"Successfully returning block number: {0}",
blockNumber);
singleBlockResponseStreamObserver.onNext(
Expand All @@ -211,17 +213,17 @@ private void singleBlock(
metricsService.singleBlocksRetrieved.increment();
} else {
LOGGER.log(
System.Logger.Level.DEBUG, "Block number {0} not found", blockNumber);
DEBUG, "Block number {0} not found", blockNumber);
singleBlockResponseStreamObserver.onNext(buildSingleBlockNotFoundResponse());
}
} catch (IOException e) {
LOGGER.log(
System.Logger.Level.ERROR, "Error reading block number: {0}", blockNumber);
ERROR, "Error reading block number: {0}", blockNumber);
singleBlockResponseStreamObserver.onNext(buildSingleBlockNotAvailableResponse());
}
} else {
LOGGER.log(
System.Logger.Level.ERROR,
ERROR,
"Unary singleBlock gRPC method is not currently running");
singleBlockResponseStreamObserver.onNext(buildSingleBlockNotAvailableResponse());
}
Expand Down
6 changes: 4 additions & 2 deletions server/src/main/java/com/hedera/block/server/Server.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
import io.helidon.webserver.http.HttpRouting;
import java.io.IOException;

import static java.lang.System.Logger.Level.INFO;

/** Main class for the block node server */
public class Server {

Expand All @@ -51,7 +53,7 @@ private Server() {}
*/
public static void main(final String[] args) {

LOGGER.log(System.Logger.Level.INFO, "Starting BlockNode Server");
LOGGER.log(INFO, "Starting BlockNode Server");

try {
// init context, metrics, and configuration.
Expand Down Expand Up @@ -103,7 +105,7 @@ public static void main(final String[] args) {

// Log the server status
LOGGER.log(
System.Logger.Level.INFO,
INFO,
"Block Node Server started at port: " + webServer.port());
} catch (IOException e) {
throw new RuntimeException(e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
package com.hedera.block.server.consumer;

import static com.hedera.block.server.Translator.toProtocSubscribeStreamResponse;
import static java.lang.System.Logger.Level.DEBUG;
import static java.lang.System.Logger.Level.ERROR;

import com.hedera.block.server.config.BlockNodeContext;
import com.hedera.block.server.data.ObjectEvent;
Expand Down Expand Up @@ -108,7 +110,7 @@ public ConsumerStreamResponseObserver(
isResponsePermitted.set(false);
subscriptionHandler.unsubscribe(this);
LOGGER.log(
System.Logger.Level.DEBUG,
DEBUG,
"Consumer cancelled stream. Observer unsubscribed.");
};
serverCallStreamObserver.setOnCancelHandler(onCancel);
Expand All @@ -120,7 +122,7 @@ public ConsumerStreamResponseObserver(
isResponsePermitted.set(false);
subscriptionHandler.unsubscribe(this);
LOGGER.log(
System.Logger.Level.DEBUG,
DEBUG,
"Consumer completed stream. Observer unsubscribed.");
};
serverCallStreamObserver.setOnCloseHandler(onClose);
Expand Down Expand Up @@ -155,7 +157,7 @@ public void onEvent(
if (currentMillis - producerLivenessMillis > timeoutThresholdMillis) {
subscriptionHandler.unsubscribe(this);
LOGGER.log(
System.Logger.Level.DEBUG,
DEBUG,
"Unsubscribed ConsumerBlockItemObserver due to producer timeout");
} else {
// Refresh the producer liveness and pass the BlockItem to the downstream observer.
Expand Down Expand Up @@ -196,7 +198,7 @@ public void send(@NonNull final SubscribeStreamResponse subscribeStreamResponse)
final BlockItem blockItem = subscribeStreamResponse.blockItem();
if (blockItem == null) {
LOGGER.log(
System.Logger.Level.ERROR,
ERROR,
"SubscribeStreamResponse was of type BLOCK_ITEM but block_item is null."
+ " This is a protocol violation: {0}",
subscribeStreamResponse.toString());
Expand All @@ -209,7 +211,7 @@ public void send(@NonNull final SubscribeStreamResponse subscribeStreamResponse)

if (streamStarted) {
LOGGER.log(
System.Logger.Level.DEBUG,
DEBUG,
"Send BlockItem downstream: {0} ",
blockItem);
subscribeStreamResponseObserver.onNext(
Expand All @@ -222,7 +224,7 @@ public void send(@NonNull final SubscribeStreamResponse subscribeStreamResponse)
private final class StatusResponseSender implements ResponseSender {
public void send(@NonNull final SubscribeStreamResponse subscribeStreamResponse) {
LOGGER.log(
System.Logger.Level.DEBUG,
DEBUG,
"Send SubscribeStreamResponse downstream: {0} ",
subscribeStreamResponse);
subscribeStreamResponseObserver.onNext(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import static java.lang.System.Logger.Level.DEBUG;
import static java.lang.System.Logger.Level.ERROR;

/**
* LiveStreamMediatorImpl is an implementation of the StreamMediator interface. It is responsible
* for managing the subscribe and unsubscribe operations of downstream consumers. It also proxies
Expand Down Expand Up @@ -111,7 +114,7 @@ public void publish(@NonNull final BlockItem blockItem) throws IOException {
if (serviceStatus.isRunning()) {

// Publish the block for all subscribers to receive
LOGGER.log(System.Logger.Level.DEBUG, "Publishing BlockItem: {0}", blockItem);
LOGGER.log(DEBUG, "Publishing BlockItem: {0}", blockItem);
final var subscribeStreamResponse =
SubscribeStreamResponse.newBuilder().blockItem(blockItem).build();
ringBuffer.publishEvent((event, sequence) -> event.set(subscribeStreamResponse));
Expand All @@ -126,27 +129,27 @@ public void publish(@NonNull final BlockItem blockItem) throws IOException {
// Disable BlockItem publication for upstream producers
serviceStatus.setRunning(false);
LOGGER.log(
System.Logger.Level.ERROR,
ERROR,
"An exception occurred while attempting to persist the BlockItem: "
+ blockItem,
e);

LOGGER.log(System.Logger.Level.DEBUG, "Send a response to end the stream");
LOGGER.log(DEBUG, "Send a response to end the stream");

// Publish the block for all subscribers to receive
final SubscribeStreamResponse endStreamResponse = buildEndStreamResponse();
ringBuffer.publishEvent((event, sequence) -> event.set(endStreamResponse));

// Unsubscribe all downstream consumers
for (final var subscriber : subscribers.keySet()) {
LOGGER.log(System.Logger.Level.DEBUG, "Unsubscribing: {0}", subscriber);
LOGGER.log(DEBUG, "Unsubscribing: {0}", subscriber);
unsubscribe(subscriber);
}

throw e;
}
} else {
LOGGER.log(System.Logger.Level.ERROR, "StreamMediator is not accepting BlockItems");
LOGGER.log(ERROR, "StreamMediator is not accepting BlockItems");
}
}

Expand Down Expand Up @@ -175,7 +178,7 @@ public void unsubscribe(
// Remove the subscriber
final var batchEventProcessor = subscribers.remove(handler);
if (batchEventProcessor == null) {
LOGGER.log(System.Logger.Level.ERROR, "Subscriber not found: {0}", handler);
LOGGER.log(ERROR, "Subscriber not found: {0}", handler);

} else {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import java.nio.file.Path;
import java.nio.file.Paths;

import static java.lang.System.Logger.Level.ERROR;

/**
* Use this configuration across the persistent storage package
*
Expand Down Expand Up @@ -50,7 +52,7 @@ public record PersistenceStorageConfig(@ConfigProperty(defaultValue = "") String
if (Files.notExists(path)) {
try {
FileUtils.createPathIfNotExists(
path, System.Logger.Level.ERROR, FileUtils.defaultPerms);
path, ERROR, FileUtils.defaultPerms);
} catch (IOException e) {
throw new RuntimeException(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
package com.hedera.block.server.persistence.storage.read;

import static com.hedera.block.server.Constants.BLOCK_FILE_EXTENSION;
import static java.lang.System.Logger.Level.DEBUG;
import static java.lang.System.Logger.Level.ERROR;
import static java.lang.System.Logger.Level.INFO;

import com.hedera.block.server.persistence.storage.PersistenceStorageConfig;
import com.hedera.hapi.block.stream.Block;
Expand Down Expand Up @@ -59,12 +62,12 @@ class BlockAsDirReader implements BlockReader<Block> {
@NonNull final PersistenceStorageConfig config,
@NonNull final FileAttribute<Set<PosixFilePermission>> filePerms) {

LOGGER.log(System.Logger.Level.INFO, "Initializing FileSystemBlockReader");
LOGGER.log(INFO, "Initializing FileSystemBlockReader");

final Path blockNodeRootPath = Path.of(config.rootPath());

LOGGER.log(System.Logger.Level.INFO, config.toString());
LOGGER.log(System.Logger.Level.INFO, "Block Node Root Path: " + blockNodeRootPath);
LOGGER.log(INFO, config.toString());
LOGGER.log(INFO, "Block Node Root Path: " + blockNodeRootPath);

this.blockNodeRootPath = blockNodeRootPath;
this.filePerms = filePerms;
Expand Down Expand Up @@ -123,7 +126,7 @@ public Optional<Block> read(final long blockNumber) throws IOException {
// Return the Block
return Optional.of(builder.build());
} catch (IOException io) {
LOGGER.log(System.Logger.Level.ERROR, "Error reading block: " + blockPath, io);
LOGGER.log(ERROR, "Error reading block: " + blockPath, io);
throw io;
}
}
Expand Down Expand Up @@ -151,7 +154,7 @@ private Optional<BlockItem> readBlockItem(@NonNull final String blockItemPath)
// So re-throw here to make a different decision upstream.
throw io;
} catch (ParseException e) {
LOGGER.log(System.Logger.Level.ERROR, "Error parsing block item: " + blockItemPath, e);
LOGGER.log(ERROR, "Error parsing block item: " + blockItemPath, e);
throw new IOException(e);
}
}
Expand All @@ -162,14 +165,14 @@ private boolean isPathDisqualified(@NonNull final Path path) {
// This code path gets hit if a consumer
// requests a block that does not exist.
// Only log this as a debug message.
LOGGER.log(System.Logger.Level.DEBUG, "Path not found: {0}", path);
LOGGER.log(DEBUG, "Path not found: {0}", path);
return true;
}

if (!path.toFile().canRead()) {
LOGGER.log(System.Logger.Level.ERROR, "Path not readable: {0}", path);
LOGGER.log(ERROR, "Path not readable: {0}", path);
LOGGER.log(
System.Logger.Level.ERROR,
ERROR,
"Attempting to repair the path permissions: {0}",
path);

Expand All @@ -182,13 +185,13 @@ private boolean isPathDisqualified(@NonNull final Path path) {
}
} catch (IOException e) {
LOGGER.log(
System.Logger.Level.ERROR, "Error setting permissions on: {0}" + path, e);
ERROR, "Error setting permissions on: {0}" + path, e);
return true;
}
}

if (!path.toFile().isDirectory()) {
LOGGER.log(System.Logger.Level.ERROR, "Path is not a directory: {0}", path);
LOGGER.log(ERROR, "Path is not a directory: {0}", path);
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
import java.nio.file.attribute.PosixFilePermission;
import java.util.Set;

import static java.lang.System.Logger.Level.ERROR;

/**
* The BlockAsDirRemover class removes a block from the file system. The block is stored as a
* directory containing block items. The block items are stored as files within the block directory.
Expand Down Expand Up @@ -62,15 +64,15 @@ public void remove(final long id) throws IOException {
// for removal
final Path blockPath = blockNodeRootPath.resolve(String.valueOf(id));
if (Files.notExists(blockPath)) {
LOGGER.log(System.Logger.Level.ERROR, "Block does not exist: {0}", id);
LOGGER.log(ERROR, "Block does not exist: {0}", id);
return;
}

Files.setPosixFilePermissions(blockPath, filePerms.value());

// Best effort to delete the block
if (!delete(blockPath.toFile())) {
LOGGER.log(System.Logger.Level.ERROR, "Failed to delete block: {0}", id);
LOGGER.log(ERROR, "Failed to delete block: {0}", id);
}
}

Expand Down
Loading

0 comments on commit ebf333a

Please sign in to comment.