-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(req-limit): implement request limiting for trace generation and …
…line counting (#1241) Signed-off-by: Tsvetan Dimitrov <[email protected]>
- Loading branch information
1 parent
fd5a92c
commit cf6cef9
Showing
24 changed files
with
774 additions
and
231 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
58 changes: 58 additions & 0 deletions
58
arithmetization/src/main/java/net/consensys/linea/plugins/BesuServiceProvider.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,58 @@ | ||
/* | ||
* 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; | ||
|
||
import org.hyperledger.besu.plugin.BesuContext; | ||
import org.hyperledger.besu.plugin.services.BesuService; | ||
import org.hyperledger.besu.plugin.services.PicoCLIOptions; | ||
import org.hyperledger.besu.plugin.services.RpcEndpointService; | ||
import org.hyperledger.besu.plugin.services.TraceService; | ||
import org.hyperledger.besu.plugin.services.sync.SynchronizationService; | ||
|
||
public class BesuServiceProvider { | ||
|
||
/** | ||
* Initialize a service of type {@link BesuService}. | ||
* | ||
* @return the initialized {@link BesuService}. | ||
*/ | ||
public static <T extends BesuService> T getBesuService( | ||
final BesuContext context, final Class<T> clazz) { | ||
return context | ||
.getService(clazz) | ||
.orElseThrow( | ||
() -> | ||
new RuntimeException( | ||
"Unable to find given Besu service. Please ensure %s is registered." | ||
.formatted(clazz.getName()))); | ||
} | ||
|
||
public static TraceService getTraceService(final BesuContext context) { | ||
return getBesuService(context, TraceService.class); | ||
} | ||
|
||
public static PicoCLIOptions getPicoCLIOptionsService(final BesuContext context) { | ||
return getBesuService(context, PicoCLIOptions.class); | ||
} | ||
|
||
public static RpcEndpointService getRpcEndpointService(final BesuContext context) { | ||
return getBesuService(context, RpcEndpointService.class); | ||
} | ||
|
||
public static SynchronizationService getSynchronizationService(final BesuContext context) { | ||
return getBesuService(context, SynchronizationService.class); | ||
} | ||
} |
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 = 8548; | ||
|
||
@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 {} |
140 changes: 140 additions & 0 deletions
140
...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,140 @@ | ||
/* | ||
* 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 static java.util.Collections.singletonMap; | ||
|
||
import java.util.Map; | ||
import java.util.Optional; | ||
|
||
import com.google.auto.service.AutoService; | ||
import io.vertx.core.Vertx; | ||
import io.vertx.core.http.HttpServer; | ||
import io.vertx.core.http.HttpServerOptions; | ||
import io.vertx.core.json.JsonObject; | ||
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 HttpServer server; | ||
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 | ||
final 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(statusResponse("UP")); | ||
} else { | ||
routingContext.response().setStatusCode(503).end(statusResponse("DOWN")); | ||
} | ||
}); | ||
|
||
TracerReadinessConfiguration configuration = | ||
(TracerReadinessConfiguration) | ||
getConfigurationByKey(TracerReadinessCliOptions.CONFIG_KEY).optionsConfig(); | ||
|
||
// Start the Vertx HTTP server | ||
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, | ||
result.cause().getMessage()); | ||
} | ||
}); | ||
} | ||
|
||
private String statusResponse(final String status) { | ||
return new JsonObject(singletonMap("status", status)).encodePrettily(); | ||
} | ||
|
||
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 { | ||
server.close().toCompletionStage().toCompletableFuture().get(); | ||
} catch (Exception e) { | ||
throw new RuntimeException("Error closing readiness endpoint HTTP server.", e); | ||
} | ||
} | ||
} |
Oops, something went wrong.