Skip to content

feat: receiver verticle now has a grace period before closing #3896

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,17 @@ public class ReceiverVerticle extends AbstractVerticle implements Handler<HttpSe
private IngressProducerReconcilableStore ingressProducerStore;
private FileWatcher secretWatcher;

private final long terminationGracePeriodMs;

public ReceiverVerticle(
final ReceiverEnv env,
final HttpServerOptions httpServerOptions,
final HttpServerOptions httpsServerOptions,
final Function<Vertx, IngressProducerReconcilableStore> ingressProducerStoreFactory,
final IngressRequestHandler ingressRequestHandler,
final String secretVolumePath,
final OIDCDiscoveryConfig oidcDiscoveryConfig) {
final OIDCDiscoveryConfig oidcDiscoveryConfig,
final long terminationGracePeriodMs) {

Objects.requireNonNull(env);
Objects.requireNonNull(httpServerOptions);
Expand All @@ -122,6 +125,7 @@ public ReceiverVerticle(
this.tlsKeyFile = new File(secretVolumePath + "/tls.key");
this.tlsCrtFile = new File(secretVolumePath + "/tls.crt");
this.oidcDiscoveryConfig = oidcDiscoveryConfig;
this.terminationGracePeriodMs = terminationGracePeriodMs;
}

public HttpServerOptions getHttpsServerOptions() {
Expand Down Expand Up @@ -193,6 +197,20 @@ private void setupSecretWatcher() {

@Override
public void stop(Promise<Void> stopPromise) throws Exception {
if (this.terminationGracePeriodMs < 1) {
this.handleStop(stopPromise);
return;
}
this.vertx.setTimer(this.terminationGracePeriodMs, (ignored) -> {
try {
this.handleStop(stopPromise);
} catch (Exception e) {
stopPromise.fail(e);
}
});
}

private void handleStop(Promise<Void> stopPromise) throws Exception {
CompositeFuture.all(
(this.httpServer != null ? this.httpServer.close().mapEmpty() : Future.succeededFuture()),
(this.httpsServer != null ? this.httpsServer.close().mapEmpty() : Future.succeededFuture()),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ public class Main {

private static final Logger logger = LoggerFactory.getLogger(Main.class);

private static final long receiverGracePeriodMs = 20_000; // 20 seconds, in milliseconds

/**
* Start receiver.
*
Expand Down Expand Up @@ -165,7 +167,8 @@ public static void start(final String[] args, final ReactiveProducerFactory kafk
eventTypeClient,
eventTypeLister,
vertx,
oidcDiscoveryConfig);
oidcDiscoveryConfig,
receiverGracePeriodMs);
DeploymentOptions deploymentOptions =
new DeploymentOptions().setInstances(Runtime.getRuntime().availableProcessors());
// Deploy the receiver verticles
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ class ReceiverVerticleFactory implements Supplier<Verticle> {

private ReactiveProducerFactory<String, CloudEvent> kafkaProducerFactory;

private final long terminationGracePeriodMs;

ReceiverVerticleFactory(
final ReceiverEnv env,
final Properties producerConfigs,
Expand All @@ -62,7 +64,8 @@ class ReceiverVerticleFactory implements Supplier<Verticle> {
final MixedOperation<EventType, KubernetesResourceList<EventType>, Resource<EventType>> eventTypeClient,
final Lister<EventType> eventTypeLister,
Vertx vertx,
final OIDCDiscoveryConfig oidcDiscoveryConfig)
final OIDCDiscoveryConfig oidcDiscoveryConfig,
final long terminationGracePeriodMs)
throws NoSuchAlgorithmException {
{
this.env = env;
Expand All @@ -75,6 +78,7 @@ class ReceiverVerticleFactory implements Supplier<Verticle> {
new EventTypeCreatorImpl(eventTypeClient, eventTypeLister, vertx));
this.kafkaProducerFactory = kafkaProducerFactory;
this.oidcDiscoveryConfig = oidcDiscoveryConfig;
this.terminationGracePeriodMs = terminationGracePeriodMs;
}
}

Expand All @@ -90,6 +94,7 @@ public Verticle get() {
properties -> kafkaProducerFactory.create(v, properties)),
this.ingressRequestHandler,
secretVolumePath,
oidcDiscoveryConfig);
oidcDiscoveryConfig,
terminationGracePeriodMs);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,8 @@ public void setUpHTTP(final Vertx vertx, final VertxTestContext testContext) {
new IngressRequestHandlerImpl(
StrictRequestToRecordMapper.getInstance(), registry, ((event, reference) -> null)),
SECRET_VOLUME_PATH,
null);
null,
0);
vertx.deployVerticle(verticle, testContext.succeeding(ar -> testContext.completeNow()));

// Connect to the logger in ReceiverVerticle
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,8 @@ public void setup() throws ExecutionException, InterruptedException {
new IngressRequestHandlerImpl(
StrictRequestToRecordMapper.getInstance(), Metrics.getRegistry(), ((event, reference) -> null)),
SECRET_VOLUME_PATH,
null);
null,
0);

vertx.deployVerticle(verticle).toCompletionStage().toCompletableFuture().get();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ public void shouldCreateMultipleReceiverVerticleInstances(Vertx vertx) throws No
mockClient.resources(EventType.class),
mock(Lister.class),
vertx,
mock(OIDCDiscoveryConfig.class));
mock(OIDCDiscoveryConfig.class),
0);

assertThat(supplier.get()).isNotSameAs(supplier.get());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,8 @@ private ReceiverVerticle setUpReceiver(final Vertx vertx, final VertxTestContext
Metrics.getRegistry(),
(((event, reference) -> null))),
SECRET_VOLUME_PATH,
null);
null,
0);

final CountDownLatch latch = new CountDownLatch(1);
vertx.deployVerticle(verticle, context.succeeding(h -> latch.countDown()));
Expand Down
Loading