-
Notifications
You must be signed in to change notification settings - Fork 109
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Gracefully clean up resources on shutdown (#334)
* Gracefully clean up resources on shutdown Signed-off-by: Pierangelo Di Pilato <pierangelodipilato@gmail.com> * Extract commond logic into static methods Signed-off-by: Pierangelo Di Pilato <pierangelodipilato@gmail.com> * Apply suggestion Signed-off-by: Pierangelo Di Pilato <pierangelodipilato@gmail.com> * Increase close timeout Signed-off-by: Pierangelo Di Pilato <pierangelodipilato@gmail.com> * Close vertx when there is an exception Signed-off-by: Pierangelo Di Pilato <pierangelodipilato@gmail.com>
- Loading branch information
Showing
5 changed files
with
206 additions
and
80 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
40 changes: 40 additions & 0 deletions
40
data-plane/core/src/main/java/dev/knative/eventing/kafka/broker/core/utils/Shutdown.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,40 @@ | ||
package dev.knative.eventing.kafka.broker.core.utils; | ||
|
||
import dev.knative.eventing.kafka.broker.contract.DataPlaneContract.Contract; | ||
import io.vertx.core.Vertx; | ||
import java.io.Closeable; | ||
import java.io.IOException; | ||
import java.util.concurrent.CountDownLatch; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.function.Consumer; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
public class Shutdown { | ||
|
||
private static final Logger logger = LoggerFactory.getLogger(Shutdown.class); | ||
|
||
public static Runnable run(final Vertx vertx, final Closeable fw, final Consumer<Contract> publisher) { | ||
return () -> { | ||
try { | ||
fw.close(); | ||
} catch (final IOException e) { | ||
logger.error("Failed to close file watcher", e); | ||
} | ||
publisher.accept(Contract.newBuilder().build()); | ||
closeSync(vertx).run(); | ||
}; | ||
} | ||
|
||
public static Runnable closeSync(final Vertx vertx) { | ||
return () -> { | ||
final var wait = new CountDownLatch(1); | ||
vertx.close(ignore -> wait.countDown()); | ||
try { | ||
wait.await(2, TimeUnit.MINUTES); | ||
} catch (InterruptedException e) { | ||
logger.error("Timeout waiting for vertx close", e); | ||
} | ||
}; | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
data-plane/core/src/test/java/dev/knative/eventing/kafka/broker/core/utils/ShutdownTest.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,55 @@ | ||
package dev.knative.eventing.kafka.broker.core.utils; | ||
|
||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.Mockito.doAnswer; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.times; | ||
import static org.mockito.Mockito.verify; | ||
|
||
import dev.knative.eventing.kafka.broker.contract.DataPlaneContract.Contract; | ||
import io.vertx.core.AsyncResult; | ||
import io.vertx.core.Future; | ||
import io.vertx.core.Handler; | ||
import io.vertx.core.Vertx; | ||
import java.io.Closeable; | ||
import java.io.IOException; | ||
import java.util.function.Consumer; | ||
import org.junit.jupiter.api.Test; | ||
|
||
public class ShutdownTest { | ||
|
||
@Test | ||
public void run() throws IOException { | ||
final var vertx = mockVertxClose(); | ||
final var closeable = mock(Closeable.class); | ||
final Consumer<Contract> consumer = mock(Consumer.class); | ||
|
||
Shutdown.run(vertx, closeable, consumer).run(); | ||
|
||
verify(vertx, times(1)).close(any()); | ||
verify(closeable).close(); | ||
verify(consumer).accept(Contract.newBuilder().build()); | ||
} | ||
|
||
@Test | ||
public void closeSync() { | ||
final var vertx = mockVertxClose(); | ||
|
||
Shutdown.closeSync(vertx).run(); | ||
|
||
verify(vertx).close(any()); | ||
} | ||
|
||
private Vertx mockVertxClose() { | ||
final var vertx = mock(Vertx.class); | ||
|
||
doAnswer(invocation -> { | ||
final Handler<AsyncResult<Void>> callback = invocation.getArgument(0); | ||
callback.handle(Future.succeededFuture()); | ||
return null; | ||
}) | ||
.when(vertx).close(any()); | ||
|
||
return vertx; | ||
} | ||
} |
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