-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'arith-dev' into feat/issue-1240/req-limiting
- Loading branch information
Showing
22 changed files
with
607 additions
and
189 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
...zation/src/main/java/net/consensys/linea/plugins/readiness/TracerReadinessCliOptions.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/* | ||
* Copyright Consensys Software Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations under the License. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package net.consensys.linea.plugins.readiness; | ||
|
||
import com.google.common.base.MoreObjects; | ||
import net.consensys.linea.plugins.LineaCliOptions; | ||
import picocli.CommandLine; | ||
|
||
public class TracerReadinessCliOptions implements LineaCliOptions { | ||
public static final String CONFIG_KEY = "tracer-readiness"; | ||
|
||
static final String TRACER_READINESS_SERVER_HOST = "--plugin-linea-tracer-readiness-server-host"; | ||
static final String TRACER_READINESS_SERVER_PORT = "--plugin-linea-tracer-readiness-server-port"; | ||
|
||
@CommandLine.Option( | ||
names = {TRACER_READINESS_SERVER_PORT}, | ||
hidden = true, | ||
paramLabel = "<SERVER_PORT>", | ||
description = "HTTP server port for tracer readiness plugin") | ||
private int serverPort = 8546; | ||
|
||
@CommandLine.Option( | ||
names = {TRACER_READINESS_SERVER_HOST}, | ||
hidden = true, | ||
paramLabel = "<SERVER_HOST>", | ||
description = "HTTP server host for tracer readiness plugin") | ||
private String serverHost = "0.0.0.0"; | ||
|
||
private TracerReadinessCliOptions() {} | ||
|
||
/** | ||
* Create Linea cli options. | ||
* | ||
* @return the Linea cli options | ||
*/ | ||
public static TracerReadinessCliOptions create() { | ||
return new TracerReadinessCliOptions(); | ||
} | ||
|
||
/** | ||
* Linea cli options from config. | ||
* | ||
* @param config the config | ||
* @return the Linea cli options | ||
*/ | ||
static TracerReadinessCliOptions fromConfig(final TracerReadinessConfiguration config) { | ||
final TracerReadinessCliOptions options = create(); | ||
options.serverHost = config.serverHost(); | ||
options.serverPort = config.serverPort(); | ||
return options; | ||
} | ||
|
||
/** | ||
* To domain object Linea factory configuration. | ||
* | ||
* @return the Linea factory configuration | ||
*/ | ||
@Override | ||
public TracerReadinessConfiguration toDomainObject() { | ||
return TracerReadinessConfiguration.builder() | ||
.serverHost(serverHost) | ||
.serverPort(serverPort) | ||
.build(); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return MoreObjects.toStringHelper(this) | ||
.add(TRACER_READINESS_SERVER_HOST, serverHost) | ||
.add(TRACER_READINESS_SERVER_PORT, serverPort) | ||
.toString(); | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
...ion/src/main/java/net/consensys/linea/plugins/readiness/TracerReadinessConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/* | ||
* Copyright Consensys Software Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations under the License. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package net.consensys.linea.plugins.readiness; | ||
|
||
import lombok.Builder; | ||
import net.consensys.linea.plugins.LineaOptionsConfiguration; | ||
|
||
/** The Linea tracer configuration private to this repo. */ | ||
@Builder(toBuilder = true) | ||
public record TracerReadinessConfiguration(String serverHost, int serverPort) | ||
implements LineaOptionsConfiguration {} |
127 changes: 127 additions & 0 deletions
127
...metization/src/main/java/net/consensys/linea/plugins/readiness/TracerReadinessPlugin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
/* | ||
* Copyright Consensys Software Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations under the License. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package net.consensys.linea.plugins.readiness; | ||
|
||
import java.util.Map; | ||
import java.util.Optional; | ||
|
||
import com.google.auto.service.AutoService; | ||
import io.vertx.core.Vertx; | ||
import io.vertx.core.http.HttpServerOptions; | ||
import io.vertx.ext.web.Router; | ||
import lombok.extern.slf4j.Slf4j; | ||
import net.consensys.linea.plugins.AbstractLineaOptionsPlugin; | ||
import net.consensys.linea.plugins.BesuServiceProvider; | ||
import net.consensys.linea.plugins.LineaOptionsPluginConfiguration; | ||
import net.consensys.linea.plugins.rpc.RequestLimiter; | ||
import net.consensys.linea.plugins.rpc.RequestLimiterDispatcher; | ||
import org.hyperledger.besu.plugin.BesuContext; | ||
import org.hyperledger.besu.plugin.BesuPlugin; | ||
import org.hyperledger.besu.plugin.services.sync.SynchronizationService; | ||
|
||
@Slf4j | ||
@AutoService(BesuPlugin.class) | ||
public class TracerReadinessPlugin extends AbstractLineaOptionsPlugin { | ||
private static final String TRACER_READINESS_ENDPOINT_NAME = "/tracer-readiness"; | ||
private SynchronizationService synchronizationService; | ||
private Vertx vertx; | ||
private BesuContext besuContext; | ||
|
||
/** | ||
* Register the needed Besu services. | ||
* | ||
* @param context the BesuContext to be used. | ||
*/ | ||
@Override | ||
public void register(final BesuContext context) { | ||
super.register(context); | ||
this.besuContext = context; | ||
} | ||
|
||
@Override | ||
protected Map<String, LineaOptionsPluginConfiguration> getLineaPluginConfigMap() { | ||
final TracerReadinessCliOptions tracerReadinessCliOptions = TracerReadinessCliOptions.create(); | ||
|
||
return Map.of(TracerReadinessCliOptions.CONFIG_KEY, tracerReadinessCliOptions.asPluginConfig()); | ||
} | ||
|
||
@Override | ||
public void start() { | ||
// Initialize Vertx | ||
vertx = Vertx.vertx(); | ||
|
||
final Router router = Router.router(vertx); | ||
|
||
// Register the readiness check handler | ||
router | ||
.get(TRACER_READINESS_ENDPOINT_NAME) | ||
.handler( | ||
routingContext -> { | ||
if (isTracerReady()) { | ||
routingContext.response().setStatusCode(200).end("UP"); | ||
} else { | ||
routingContext.response().setStatusCode(503).end("DOWN"); | ||
} | ||
}); | ||
|
||
TracerReadinessConfiguration configuration = | ||
(TracerReadinessConfiguration) | ||
getConfigurationByKey(TracerReadinessCliOptions.CONFIG_KEY).optionsConfig(); | ||
|
||
// Start the Vertx HTTP server | ||
vertx | ||
.createHttpServer(httpServerOptions(configuration)) | ||
.requestHandler(router) | ||
.listen( | ||
configuration.serverPort(), | ||
result -> { | ||
final String pluginName = getClass().getSimpleName(); | ||
final int port = configuration.serverPort(); | ||
|
||
if (result.succeeded()) { | ||
log.info("[{}] Started listening on port {}", pluginName, port); | ||
} else { | ||
log.error("[{}] Failed to start listening on port {}", pluginName, port); | ||
} | ||
}); | ||
} | ||
|
||
private HttpServerOptions httpServerOptions(final TracerReadinessConfiguration config) { | ||
return new HttpServerOptions().setHost(config.serverHost()).setPort(config.serverPort()); | ||
} | ||
|
||
private boolean isTracerReady() { | ||
this.synchronizationService = | ||
Optional.ofNullable(synchronizationService) | ||
.orElse(BesuServiceProvider.getSynchronizationService(besuContext)); | ||
|
||
final RequestLimiter requestLimiter = | ||
RequestLimiterDispatcher.getLimiter( | ||
RequestLimiterDispatcher.SINGLE_INSTANCE_REQUEST_LIMITER_KEY); | ||
|
||
return !requestLimiter.isNodeAtMaxCapacity() && synchronizationService.isInitialSyncPhaseDone(); | ||
} | ||
|
||
@Override | ||
public void stop() { | ||
super.stop(); | ||
try { | ||
vertx.close().toCompletionStage().toCompletableFuture().get(); | ||
} catch (Exception e) { | ||
throw new RuntimeException("Error closing readiness endpoint HTTP server.", e); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
arithmetization/src/main/java/net/consensys/linea/plugins/rpc/RequestLimiterDispatcher.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* | ||
* Copyright Consensys Software Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations under the License. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package net.consensys.linea.plugins.rpc; | ||
|
||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.concurrent.ConcurrentMap; | ||
|
||
public class RequestLimiterDispatcher { | ||
public static final String SINGLE_INSTANCE_REQUEST_LIMITER_KEY = "single-instance-request-limit"; | ||
private static final ConcurrentMap<String, RequestLimiter> ENDPOINT_LIMITER_MAP = | ||
new ConcurrentHashMap<>(); | ||
|
||
public static RequestLimiter getLimiter(final String serviceKey) { | ||
return ENDPOINT_LIMITER_MAP.get(serviceKey); | ||
} | ||
|
||
public static void setLimiterIfMissing( | ||
final String serviceKey, final int concurrentRequestsLimit) { | ||
ENDPOINT_LIMITER_MAP.putIfAbsent( | ||
serviceKey, | ||
RequestLimiter.builder().concurrentRequestsCount(concurrentRequestsLimit).build()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.