From 50b07990df26981d22ef12987999708c9f2788fe Mon Sep 17 00:00:00 2001 From: Barry Nouwt Date: Tue, 1 Apr 2025 14:33:09 +0200 Subject: [PATCH] Fix synchronization problem and implement graceful shutdown. --- .../engine/smartconnector/runtime/KeRuntime.java | 13 +++++++++++++ .../runtime/messaging/RemoteKerConnection.java | 2 +- .../messaging/RemoteKerConnectionManager.java | 12 ++++++++++++ 3 files changed, 26 insertions(+), 1 deletion(-) diff --git a/smart-connector/src/main/java/eu/knowledge/engine/smartconnector/runtime/KeRuntime.java b/smart-connector/src/main/java/eu/knowledge/engine/smartconnector/runtime/KeRuntime.java index 06d823444..c57db66fe 100644 --- a/smart-connector/src/main/java/eu/knowledge/engine/smartconnector/runtime/KeRuntime.java +++ b/smart-connector/src/main/java/eu/knowledge/engine/smartconnector/runtime/KeRuntime.java @@ -32,6 +32,19 @@ public class KeRuntime { static { + // although no guarantees can be made, let's try and shutdown gracefully and let + // others know. + Runtime.getRuntime().addShutdownHook(new Thread(() -> { + LOG.warn("Graceful shutdown requested."); + + // Perform cleanup tasks here + try { + getMessageDispatcher().stop(); + } catch (Exception e) { + LOG.error("No error should occur when stopping the message dispatcher.", e); + } + })); + Config config = ConfigProvider.getConfig(); ConfigValue exposedUrl = config.getConfigValue(SmartConnectorConfig.CONF_KEY_KE_RUNTIME_EXPOSED_URL); ConfigValue hostname = config.getConfigValue(SmartConnectorConfig.CONF_KEY_KE_RUNTIME_HOSTNAME); diff --git a/smart-connector/src/main/java/eu/knowledge/engine/smartconnector/runtime/messaging/RemoteKerConnection.java b/smart-connector/src/main/java/eu/knowledge/engine/smartconnector/runtime/messaging/RemoteKerConnection.java index ef821eeb4..1b1b92033 100644 --- a/smart-connector/src/main/java/eu/knowledge/engine/smartconnector/runtime/messaging/RemoteKerConnection.java +++ b/smart-connector/src/main/java/eu/knowledge/engine/smartconnector/runtime/messaging/RemoteKerConnection.java @@ -235,7 +235,7 @@ public void stop() { .header("Content-Type", "application/json").DELETE().build(); HttpResponse response = this.httpClient.send(request, BodyHandlers.ofString()); - if (response.statusCode() == 200) { + if (response.statusCode() == 204) { LOG.trace("Successfully said goodbye to {}", this.remoteKerUri); } else { this.remoteKerDetails = null; diff --git a/smart-connector/src/main/java/eu/knowledge/engine/smartconnector/runtime/messaging/RemoteKerConnectionManager.java b/smart-connector/src/main/java/eu/knowledge/engine/smartconnector/runtime/messaging/RemoteKerConnectionManager.java index 62500c79b..63ba0a47c 100644 --- a/smart-connector/src/main/java/eu/knowledge/engine/smartconnector/runtime/messaging/RemoteKerConnectionManager.java +++ b/smart-connector/src/main/java/eu/knowledge/engine/smartconnector/runtime/messaging/RemoteKerConnectionManager.java @@ -116,6 +116,7 @@ private synchronized void queryKnowledgeDirectory() { RemoteKerConnection messageSender = new RemoteKerConnection(messageDispatcher, knowledgeEngineRuntime); remoteKerConnections.put(knowledgeEngineRuntime.getId(), messageSender); messageSender.start(); + messageSender.sendMyKerDetailsToPeer(messageDispatcher.getMyKnowledgeEngineRuntimeDetails()); } } // Check if there are KERs that need to be removed @@ -151,6 +152,11 @@ private synchronized void queryKnowledgeDirectory() { public void stop() { this.scheduledScheduleFuture.cancel(false); + + // let other KERs know we are stopping + for (RemoteKerConnection remoteKerConnection : this.remoteKerConnections.values()) { + remoteKerConnection.stop(); + } } public RemoteKerConnection getRemoteKerConnection(URI toKnowledgeBase) { @@ -203,6 +209,12 @@ public Response runtimedetailsKerIdDelete(String kerId, SecurityContext security // That one didn't exist return Response.status(404).build(); } else { + // make sure all SCs are notified correctly. We use fake/empty details for that. + KnowledgeEngineRuntimeDetails details = new KnowledgeEngineRuntimeDetails(); + details.setRuntimeId(kerConnection.getRemoteKerUri().toString()); + details.setSmartConnectorIds(new ArrayList<>()); + kerConnection.updateKerDetails(details); + // Done! return Response.status(204).build(); }