Skip to content

Commit

Permalink
shift creation of plugin context to BesuCommand for now (hyperledger#…
Browse files Browse the repository at this point in the history
…7625)

* shift creation of plugin context to BesuCommand for now
* mock component will provide a no-op metrics sys

---------

Signed-off-by: Justin Florentine <[email protected]>
  • Loading branch information
jflo authored Sep 18, 2024
1 parent 6df4149 commit 7d3e376
Show file tree
Hide file tree
Showing 14 changed files with 148 additions and 114 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
### Bug fixes
- Fix mounted data path directory permissions for besu user [#7575](https://github.com/hyperledger/besu/pull/7575)
- Fix for `debug_traceCall` to handle transactions without specified gas price. [#7510](https://github.com/hyperledger/besu/pull/7510)
- Corrects a regression where custom plugin services are not initialized correctly. [#7625](https://github.com/hyperledger/besu/pull/7625)


## 24.9.1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,70 @@ public void startNode(final BesuNode node) {

final Path dataDir = node.homeDirectory();

final List<String> params = commandlineArgs(node, dataDir);

LOG.info("Creating besu process with params {}", params);
final ProcessBuilder processBuilder =
new ProcessBuilder(params)
.directory(new File(System.getProperty("user.dir")).getParentFile().getParentFile())
.redirectErrorStream(true)
.redirectInput(Redirect.INHERIT);
if (!node.getPlugins().isEmpty()) {
processBuilder
.environment()
.put(
"BESU_OPTS",
"-Dbesu.plugins.dir=" + dataDir.resolve("plugins").toAbsolutePath().toString());
}
// Use non-blocking randomness for acceptance tests
processBuilder
.environment()
.put(
"JAVA_OPTS",
"-Djava.security.properties="
+ "acceptance-tests/tests/build/resources/test/acceptanceTesting.security");
// add additional environment variables
processBuilder.environment().putAll(node.getEnvironment());

try {
int debugPort = Integer.parseInt(System.getenv("BESU_DEBUG_CHILD_PROCESS_PORT"));
LOG.warn("Waiting for debugger to attach to SUSPENDED child process");
String debugOpts =
" -agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=*:" + debugPort;
String prevJavaOpts = processBuilder.environment().get("JAVA_OPTS");
if (prevJavaOpts == null) {
processBuilder.environment().put("JAVA_OPTS", debugOpts);
} else {
processBuilder.environment().put("JAVA_OPTS", prevJavaOpts + debugOpts);
}

} catch (NumberFormatException e) {
LOG.debug(
"Child process may be attached to by exporting BESU_DEBUG_CHILD_PROCESS_PORT=<port> to env");
}

try {
checkState(
isNotAliveOrphan(node.getName()),
"A live process with name: %s, already exists. Cannot create another with the same name as it would orphan the first",
node.getName());

final Process process = processBuilder.start();
process.onExit().thenRun(() -> node.setExitCode(process.exitValue()));
outputProcessorExecutor.execute(() -> printOutput(node, process));
besuProcesses.put(node.getName(), process);
} catch (final IOException e) {
LOG.error("Error starting BesuNode process", e);
}

if (node.getRunCommand().isEmpty()) {
waitForFile(dataDir, "besu.ports");
waitForFile(dataDir, "besu.networks");
}
MDC.remove("node");
}

private List<String> commandlineArgs(final BesuNode node, final Path dataDir) {
final List<String> params = new ArrayList<>();
params.add("build/install/besu/bin/besu");

Expand Down Expand Up @@ -388,66 +452,7 @@ public void startNode(final BesuNode node) {
}

params.addAll(node.getRunCommand());

LOG.info("Creating besu process with params {}", params);
final ProcessBuilder processBuilder =
new ProcessBuilder(params)
.directory(new File(System.getProperty("user.dir")).getParentFile().getParentFile())
.redirectErrorStream(true)
.redirectInput(Redirect.INHERIT);
if (!node.getPlugins().isEmpty()) {
processBuilder
.environment()
.put(
"BESU_OPTS",
"-Dbesu.plugins.dir=" + dataDir.resolve("plugins").toAbsolutePath().toString());
}
// Use non-blocking randomness for acceptance tests
processBuilder
.environment()
.put(
"JAVA_OPTS",
"-Djava.security.properties="
+ "acceptance-tests/tests/build/resources/test/acceptanceTesting.security");
// add additional environment variables
processBuilder.environment().putAll(node.getEnvironment());

try {
int debugPort = Integer.parseInt(System.getenv("BESU_DEBUG_CHILD_PROCESS_PORT"));
LOG.warn("Waiting for debugger to attach to SUSPENDED child process");
String debugOpts =
" -agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=*:" + debugPort;
String prevJavaOpts = processBuilder.environment().get("JAVA_OPTS");
if (prevJavaOpts == null) {
processBuilder.environment().put("JAVA_OPTS", debugOpts);
} else {
processBuilder.environment().put("JAVA_OPTS", prevJavaOpts + debugOpts);
}

} catch (NumberFormatException e) {
LOG.debug(
"Child process may be attached to by exporting BESU_DEBUG_CHILD_PROCESS_PORT=<port> to env");
}

try {
checkState(
isNotAliveOrphan(node.getName()),
"A live process with name: %s, already exists. Cannot create another with the same name as it would orphan the first",
node.getName());

final Process process = processBuilder.start();
process.onExit().thenRun(() -> node.setExitCode(process.exitValue()));
outputProcessorExecutor.execute(() -> printOutput(node, process));
besuProcesses.put(node.getName(), process);
} catch (final IOException e) {
LOG.error("Error starting BesuNode process", e);
}

if (node.getRunCommand().isEmpty()) {
waitForFile(dataDir, "besu.ports");
waitForFile(dataDir, "besu.networks");
}
MDC.remove("node");
return params;
}

private boolean isNotAliveOrphan(final String name) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,8 @@ public void startNode(final BesuNode node) {
final PermissioningServiceImpl permissioningService = new PermissioningServiceImpl();

GlobalOpenTelemetry.resetForTest();
final ObservableMetricsSystem metricsSystem = component.getObservableMetricsSystem();
final ObservableMetricsSystem metricsSystem =
(ObservableMetricsSystem) component.getMetricsSystem();
final List<EnodeURL> bootnodes =
node.getConfiguration().getBootnodes().stream().map(EnodeURLImpl::fromURI).toList();

Expand Down Expand Up @@ -280,6 +281,16 @@ public BesuNodeProviderModule(final BesuNode toProvide) {
this.toProvide = toProvide;
}

@Provides
@Singleton
MetricsConfiguration provideMetricsConfiguration() {
if (toProvide.getMetricsConfiguration() != null) {
return toProvide.getMetricsConfiguration();
} else {
return MetricsConfiguration.builder().build();
}
}

@Provides
public BesuNode provideBesuNodeRunner() {
return toProvide;
Expand Down Expand Up @@ -410,13 +421,13 @@ public BesuControllerBuilder provideBesuControllerBuilder(
public BesuController provideBesuController(
final SynchronizerConfiguration synchronizerConfiguration,
final BesuControllerBuilder builder,
final ObservableMetricsSystem metricsSystem,
final MetricsSystem metricsSystem,
final KeyValueStorageProvider storageProvider,
final MiningParameters miningParameters) {

builder
.synchronizerConfiguration(synchronizerConfiguration)
.metricsSystem(metricsSystem)
.metricsSystem((ObservableMetricsSystem) metricsSystem)
.dataStorageConfiguration(DataStorageConfiguration.DEFAULT_FOREST_CONFIG)
.ethProtocolConfiguration(EthProtocolConfiguration.defaultConfig())
.clock(Clock.systemUTC())
Expand Down Expand Up @@ -562,12 +573,6 @@ BesuCommand provideBesuCommand(final BesuPluginContextImpl pluginContext) {
return besuCommand;
}

@Provides
@Singleton
MetricsConfiguration provideMetricsConfiguration() {
return MetricsConfiguration.builder().build();
}

@Provides
@Named("besuCommandLogger")
@Singleton
Expand Down
5 changes: 4 additions & 1 deletion besu/src/main/java/org/hyperledger/besu/Besu.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import org.hyperledger.besu.cli.BesuCommand;
import org.hyperledger.besu.cli.logging.BesuLoggingConfigurationFactory;
import org.hyperledger.besu.components.BesuComponent;
import org.hyperledger.besu.components.DaggerBesuComponent;

import io.netty.util.internal.logging.InternalLoggerFactory;
Expand All @@ -36,13 +37,15 @@ public Besu() {}
*/
public static void main(final String... args) {
setupLogging();
final BesuCommand besuCommand = DaggerBesuComponent.create().getBesuCommand();
final BesuComponent besuComponent = DaggerBesuComponent.create();
final BesuCommand besuCommand = besuComponent.getBesuCommand();
int exitCode =
besuCommand.parse(
new RunLast(),
besuCommand.parameterExceptionHandler(),
besuCommand.executionExceptionHandler(),
System.in,
besuComponent,
args);

System.exit(exitCode);
Expand Down
38 changes: 25 additions & 13 deletions besu/src/main/java/org/hyperledger/besu/cli/BesuCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@
import org.hyperledger.besu.cli.util.CommandLineUtils;
import org.hyperledger.besu.cli.util.ConfigDefaultValueProviderStrategy;
import org.hyperledger.besu.cli.util.VersionProvider;
import org.hyperledger.besu.components.BesuComponent;
import org.hyperledger.besu.config.CheckpointConfigOptions;
import org.hyperledger.besu.config.GenesisConfigFile;
import org.hyperledger.besu.config.GenesisConfigOptions;
Expand Down Expand Up @@ -145,7 +146,6 @@
import org.hyperledger.besu.metrics.BesuMetricCategory;
import org.hyperledger.besu.metrics.MetricCategoryRegistryImpl;
import org.hyperledger.besu.metrics.MetricsProtocol;
import org.hyperledger.besu.metrics.MetricsSystemFactory;
import org.hyperledger.besu.metrics.ObservableMetricsSystem;
import org.hyperledger.besu.metrics.StandardMetricCategory;
import org.hyperledger.besu.metrics.prometheus.MetricsConfiguration;
Expand Down Expand Up @@ -423,6 +423,7 @@ void setUserName(final String userName) {
private final TransactionPoolValidatorServiceImpl transactionValidatorServiceImpl;
private final TransactionSimulationServiceImpl transactionSimulationServiceImpl;
private final BlockchainServiceImpl blockchainServiceImpl;
private BesuComponent besuComponent;

static class P2PDiscoveryOptionGroup {

Expand Down Expand Up @@ -897,9 +898,6 @@ static class PrivacyOptionGroup {
private BesuController besuController;
private BesuConfigurationImpl pluginCommonConfiguration;

private final Supplier<ObservableMetricsSystem> metricsSystem =
Suppliers.memoize(() -> MetricsSystemFactory.create(metricsConfiguration()));

private Vertx vertx;
private EnodeDnsConfiguration enodeDnsConfiguration;
private KeyValueStorageProvider keyValueStorageProvider;
Expand Down Expand Up @@ -1029,6 +1027,7 @@ protected BesuCommand(
* @param parameterExceptionHandler Handler for exceptions related to command line parameters.
* @param executionExceptionHandler Handler for exceptions during command execution.
* @param in The input stream for commands.
* @param besuComponent The Besu component.
* @param args The command line arguments.
* @return The execution result status code.
*/
Expand All @@ -1037,8 +1036,12 @@ public int parse(
final BesuParameterExceptionHandler parameterExceptionHandler,
final BesuExecutionExceptionHandler executionExceptionHandler,
final InputStream in,
final BesuComponent besuComponent,
final String... args) {

if (besuComponent == null) {
throw new IllegalArgumentException("BesuComponent must be provided");
}
this.besuComponent = besuComponent;
initializeCommandLineSettings(in);

// Create the execution strategy chain.
Expand Down Expand Up @@ -1142,7 +1145,7 @@ public void run() {
logger.info("Starting Besu");

// Need to create vertx after cmdline has been parsed, such that metricsSystem is configurable
vertx = createVertx(createVertxOptions(metricsSystem.get()));
vertx = createVertx(createVertxOptions(besuComponent.getMetricsSystem()));

validateOptions();

Expand Down Expand Up @@ -1527,8 +1530,8 @@ private void validatePrivacyPluginOptions() {
}

private void setReleaseMetrics() {
metricsSystem
.get()
besuComponent
.getMetricsSystem()
.createLabelledGauge(
StandardMetricCategory.PROCESS, "release", "Release information", "version")
.labels(() -> 1, BesuInfo.version());
Expand Down Expand Up @@ -1992,7 +1995,7 @@ public BesuControllerBuilder setupControllerBuilder() {
.miningParameters(miningParametersSupplier.get())
.transactionPoolConfiguration(buildTransactionPoolConfiguration())
.nodeKey(new NodeKey(securityModule()))
.metricsSystem(metricsSystem.get())
.metricsSystem((ObservableMetricsSystem) besuComponent.getMetricsSystem())
.messagePermissioningProviders(permissioningService.getMessagePermissioningProviders())
.privacyParameters(privacyParameters())
.clock(Clock.systemUTC())
Expand All @@ -2012,7 +2015,8 @@ public BesuControllerBuilder setupControllerBuilder() {
.randomPeerPriority(p2PDiscoveryOptionGroup.randomPeerPriority)
.chainPruningConfiguration(unstableChainPruningOptions.toDomainObject())
.cacheLastBlocks(numberOfblocksToCache)
.genesisStateHashCacheEnabled(genesisStateHashCacheEnabled);
.genesisStateHashCacheEnabled(genesisStateHashCacheEnabled)
.besuComponent(besuComponent);
}

private JsonRpcConfiguration createEngineJsonRpcConfiguration(
Expand Down Expand Up @@ -2414,7 +2418,6 @@ private Runner synchronize(

p2pTLSConfiguration.ifPresent(runnerBuilder::p2pTLSConfiguration);

final ObservableMetricsSystem metricsSystem = this.metricsSystem.get();
final Runner runner =
runnerBuilder
.vertx(vertx)
Expand All @@ -2441,7 +2444,7 @@ private Runner synchronize(
.pidPath(pidPath)
.dataDir(dataDir())
.bannedNodeIds(p2PDiscoveryOptionGroup.bannedNodeIds)
.metricsSystem(metricsSystem)
.metricsSystem((ObservableMetricsSystem) besuComponent.getMetricsSystem())
.permissioningService(permissioningService)
.metricsConfiguration(metricsConfiguration)
.staticNodes(staticNodes)
Expand Down Expand Up @@ -2608,7 +2611,7 @@ private File resolveNodePrivateKeyFile(final File nodePrivateKeyFile) {
* @return Instance of MetricsSystem
*/
public MetricsSystem getMetricsSystem() {
return metricsSystem.get();
return besuComponent.getMetricsSystem();
}

private Set<EnodeURL> loadStaticNodes() throws IOException {
Expand Down Expand Up @@ -2946,4 +2949,13 @@ && getDataStorageConfiguration().getBonsaiLimitTrieLogsEnabled()) {

return builder.build();
}

/**
* Returns the plugin context.
*
* @return the plugin context.
*/
public BesuPluginContextImpl getBesuPluginContext() {
return besuPluginContext;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,15 @@ public BesuCommandModule() {}

@Provides
@Singleton
BesuCommand provideBesuCommand(
final BesuPluginContextImpl pluginContext,
final @Named("besuCommandLogger") Logger commandLogger) {
BesuCommand provideBesuCommand(final @Named("besuCommandLogger") Logger commandLogger) {
final BesuCommand besuCommand =
new BesuCommand(
RlpBlockImporter::new,
JsonBlockImporter::new,
RlpBlockExporter::new,
new RunnerBuilder(),
new BesuController.Builder(),
pluginContext,
new BesuPluginContextImpl(),
System.getenv(),
commandLogger);
besuCommand.toCommandLine();
Expand All @@ -71,4 +69,10 @@ MetricsConfiguration provideMetricsConfiguration(final BesuCommand provideFrom)
Logger provideBesuCommandLogger() {
return Besu.getFirstLogger();
}

@Provides
@Singleton
BesuPluginContextImpl provideBesuPluginContextImpl(final BesuCommand provideFrom) {
return provideFrom.getBesuPluginContext();
}
}
Loading

0 comments on commit 7d3e376

Please sign in to comment.