Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Healthcheck #715

Merged
merged 5 commits into from
Apr 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions besu/src/main/java/org/hyperledger/besu/Runner.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,11 @@

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.util.Optional;
import java.util.Properties;
import java.util.concurrent.CompletableFuture;
Expand All @@ -53,6 +57,7 @@ public class Runner implements AutoCloseable {

private final NetworkRunner networkRunner;
private final NatService natService;
private final Optional<Path> pidPath;
private final Optional<JsonRpcHttpService> jsonRpc;
private final Optional<GraphQLHttpService> graphQLHttp;
private final Optional<WebSocketService> websocketRpc;
Expand All @@ -75,12 +80,14 @@ public class Runner implements AutoCloseable {
final Optional<MetricsService> metrics,
final BesuController<?> besuController,
final Path dataDir,
final Optional<Path> pidPath,
final Optional<TransactionLogBloomCacher> transactionLogBloomCacher,
final Blockchain blockchain) {
this.vertx = vertx;
this.networkRunner = networkRunner;
this.natService = natService;
this.graphQLHttp = graphQLHttp;
this.pidPath = pidPath;
this.jsonRpc = jsonRpc;
this.websocketRpc = websocketRpc;
this.metrics = metrics;
Expand Down Expand Up @@ -114,6 +121,7 @@ public void start() {
writeBesuPortsToFile();
writeBesuNetworksToFile();
autoTransactionLogBloomCachingService.ifPresent(AutoTransactionLogBloomCachingService::start);
writePidFile();
} catch (final Exception ex) {
LOG.error("Startup failed", ex);
throw new IllegalStateException(ex);
Expand Down Expand Up @@ -256,6 +264,28 @@ private void writeBesuNetworksToFile() {
"This file contains the IP Addresses (global and local) used by the running instance of Besu");
}

private void writePidFile() {
pidPath.ifPresent(
path -> {
String pid = "";
try {
pid = Long.toString(ProcessHandle.current().pid());
} catch (Throwable t) {
}
try {
Files.write(
path,
pid.getBytes(StandardCharsets.UTF_8),
StandardOpenOption.CREATE,
StandardOpenOption.TRUNCATE_EXISTING,
StandardOpenOption.WRITE);
path.toFile().deleteOnExit();
} catch (IOException e) {
LOG.error("Error writing PID file", e);
}
});
}

public Optional<Integer> getJsonRpcPort() {
return jsonRpc.map(service -> service.socketAddress().getPort());
}
Expand Down
7 changes: 7 additions & 0 deletions besu/src/main/java/org/hyperledger/besu/RunnerBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ public class RunnerBuilder {
private GraphQLConfiguration graphQLConfiguration;
private WebSocketConfiguration webSocketConfiguration;
private Path dataDir;
private Optional<Path> pidPath = Optional.empty();
private MetricsConfiguration metricsConfiguration;
private ObservableMetricsSystem metricsSystem;
private Optional<PermissioningConfiguration> permissioningConfiguration = Optional.empty();
Expand Down Expand Up @@ -241,6 +242,11 @@ public RunnerBuilder permissioningConfiguration(
return this;
}

public RunnerBuilder pidPath(final Path pidPath) {
this.pidPath = Optional.ofNullable(pidPath);
return this;
}

public RunnerBuilder dataDir(final Path dataDir) {
this.dataDir = dataDir;
return this;
Expand Down Expand Up @@ -546,6 +552,7 @@ public Runner build() {
metricsService,
besuController,
dataDir,
pidPath,
autoLogBloomCaching ? blockchainQueries.getTransactionLogBloomCacher() : Optional.empty(),
context.getBlockchain());
}
Expand Down
13 changes: 11 additions & 2 deletions besu/src/main/java/org/hyperledger/besu/cli/BesuCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -848,6 +848,12 @@ void setBannedNodeIds(final List<String> values) {
private final Integer pruningBlockConfirmations =
PrunerConfiguration.DEFAULT_PRUNING_BLOCK_CONFIRMATIONS;

@CommandLine.Option(
names = {"--pid-path"},
paramLabel = MANDATORY_PATH_FORMAT_HELP,
description = "Path to PID file (optional)")
private final Path pidPath = null;

private EthNetworkConfig ethNetworkConfig;
private JsonRpcConfiguration jsonRpcConfiguration;
private GraphQLConfiguration graphQLConfiguration;
Expand Down Expand Up @@ -1068,7 +1074,8 @@ private void startSynchronization() {
webSocketConfiguration,
metricsConfiguration,
permissioningConfiguration,
staticNodes);
staticNodes,
pidPath);
}

private BesuCommand startPlugins() {
Expand Down Expand Up @@ -1727,7 +1734,8 @@ private void synchronize(
final WebSocketConfiguration webSocketConfiguration,
final MetricsConfiguration metricsConfiguration,
final Optional<PermissioningConfiguration> permissioningConfiguration,
final Collection<EnodeURL> staticNodes) {
final Collection<EnodeURL> staticNodes,
final Path pidPath) {

checkNotNull(runnerBuilder);

Expand All @@ -1753,6 +1761,7 @@ private void synchronize(
.graphQLConfiguration(graphQLConfiguration)
.jsonRpcConfiguration(jsonRpcConfiguration)
.webSocketConfiguration(webSocketConfiguration)
.pidPath(pidPath)
.dataDir(dataDir())
.bannedNodeIds(bannedNodeIds)
.metricsSystem(metricsSystem)
Expand Down
3 changes: 3 additions & 0 deletions besu/src/test/java/org/hyperledger/besu/RunnerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ private void syncFromGenesis(final SyncMode mode, final GenesisConfigFile genesi
final GraphQLConfiguration aheadGraphQLConfiguration = graphQLConfiguration();
final WebSocketConfiguration aheadWebSocketConfiguration = wsRpcConfiguration();
final MetricsConfiguration aheadMetricsConfiguration = metricsConfiguration();
final Path pidPath = temp.getRoot().toPath().resolve("pid");
final RunnerBuilder runnerBuilder =
new RunnerBuilder()
.vertx(vertx)
Expand All @@ -213,11 +214,13 @@ private void syncFromGenesis(final SyncMode mode, final GenesisConfigFile genesi
.webSocketConfiguration(aheadWebSocketConfiguration)
.metricsConfiguration(aheadMetricsConfiguration)
.dataDir(dbAhead)
.pidPath(pidPath)
.besuPluginContext(new BesuPluginContextImpl())
.build();
try {

runnerAhead.start();
assertThat(pidPath.toFile().exists()).isTrue();

final SynchronizerConfiguration syncConfigBehind =
SynchronizerConfiguration.builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@ public void initMocks() throws Exception {
when(mockRunnerBuilder.identityString(any())).thenReturn(mockRunnerBuilder);
when(mockRunnerBuilder.besuPluginContext(any())).thenReturn(mockRunnerBuilder);
when(mockRunnerBuilder.autoLogBloomCaching(anyBoolean())).thenReturn(mockRunnerBuilder);
when(mockRunnerBuilder.pidPath(any())).thenReturn(mockRunnerBuilder);
when(mockRunnerBuilder.build()).thenReturn(mockRunner);

lenient()
Expand Down
1 change: 1 addition & 0 deletions besu/src/test/resources/everything_config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
data-path="~/besudata"
logging="INFO"
node-private-key-file="./path/to/privateKey"
pid-path="~/.pid"

# P2P network
identity="PegaSysEng"
Expand Down
2 changes: 2 additions & 0 deletions docker/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@ EXPOSE 8545 8546 8547 30303
ENV BESU_RPC_HTTP_HOST 0.0.0.0
ENV BESU_RPC_WS_HOST 0.0.0.0
ENV BESU_GRAPHQL_HTTP_HOST 0.0.0.0
ENV BESU_PID_PATH "/tmp/pid"

ENV PATH="/opt/besu/bin:${PATH}"
ENTRYPOINT ["besu"]
HEALTHCHECK --start-period=5s --interval=5s --timeout=1s --retries=10 CMD bash -c "[ -f /tmp/pid ]"

# Build-time metadata as defined at http://label-schema.org
ARG BUILD_DATE
Expand Down