diff --git a/admin-ui/src/main/java/eu/knowledge/engine/admin/AdminUI.java b/admin-ui/src/main/java/eu/knowledge/engine/admin/AdminUI.java index 6ec5209ec..e66f6e965 100644 --- a/admin-ui/src/main/java/eu/knowledge/engine/admin/AdminUI.java +++ b/admin-ui/src/main/java/eu/knowledge/engine/admin/AdminUI.java @@ -77,8 +77,8 @@ public BindingSet handleRemovedKnowledgeBase(ReactExchangeInfo ei) { } @Override - public void fetchInitialData() { - super.fetchInitialData(); + public void fetchMetaData() { + super.fetchMetaData(); // when result available (and the config is enabled), we print the // knowledge bases to the console. diff --git a/admin-ui/src/main/java/eu/knowledge/engine/admin/AdminUIConfig.java b/admin-ui/src/main/java/eu/knowledge/engine/admin/AdminUIConfig.java index 471522dc0..826fba26d 100644 --- a/admin-ui/src/main/java/eu/knowledge/engine/admin/AdminUIConfig.java +++ b/admin-ui/src/main/java/eu/knowledge/engine/admin/AdminUIConfig.java @@ -4,10 +4,19 @@ public class AdminUIConfig { /** * The key to configure how long (in milliseconds) should the MetadataKB wait - * until it tries to ask for all KBs in the network. This value should probably - * be higher in distributed mode, to allow the participants to reach equilibrium - * with respect to knowledge about each other. + * until it tries (for the first time) to ask for all KBs in the network. This + * value should probably be higher in distributed mode, to allow the + * participants to reach equilibrium with respect to knowledge about each other. */ public static final String CONF_KEY_INITIAL_METADATA_DELAY = "initial.metadata.delay"; + /** + * The key to configure what the delay (in milliseconds) should be between + * repeated metadata requests. Sometimes (especially in distributed mode) the + * metadata gets corrupted and certain KBs or KIs are (not) available while they + * should (not). This makes sure the metadata gets refreshed periodically. Set + * this value to {@code 0} disables repeated metadata checks (default). + */ + public static final String CONF_KEY_REPEATED_METADATA_DELAY = "repeated.metadata.delay"; + } diff --git a/admin-ui/src/main/java/eu/knowledge/engine/admin/MetadataKB.java b/admin-ui/src/main/java/eu/knowledge/engine/admin/MetadataKB.java index 775543170..3a289ef3b 100644 --- a/admin-ui/src/main/java/eu/knowledge/engine/admin/MetadataKB.java +++ b/admin-ui/src/main/java/eu/knowledge/engine/admin/MetadataKB.java @@ -3,6 +3,10 @@ import java.util.Arrays; import java.util.HashSet; import java.util.concurrent.ExecutionException; +import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.ScheduledFuture; +import java.util.concurrent.TimeUnit; import org.apache.jena.rdf.model.Model; import org.apache.jena.rdf.model.Resource; @@ -13,6 +17,7 @@ import org.apache.jena.update.UpdateFactory; import org.apache.jena.update.UpdateRequest; import org.apache.jena.vocabulary.RDF; +import org.eclipse.microprofile.config.Config; import org.eclipse.microprofile.config.ConfigProvider; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -38,6 +43,8 @@ */ public class MetadataKB extends KnowledgeBaseImpl { + private static final Config CONFIG = ConfigProvider.getConfig(); + private static final Logger LOG = LoggerFactory.getLogger(MetadataKB.class); private static final String META_GRAPH_PATTERN_STR = """ @@ -69,11 +76,40 @@ public class MetadataKB extends KnowledgeBaseImpl { // used triggered when knowledge about other KBs is deleted private ReactKnowledgeInteraction rKIRemoved; + /** + * The RDF metadata about all KBs and KIs in the network. It is kept up-to-date + * by periodically quering all KBs and subscribing to notifications about + * new/changed/removed KBs. + */ private Model metadata; + /** + * The metadata graph pattern object. + */ private GraphPattern metaGraphPattern; - private boolean timeToSleepAndFetch = true; + /** + * The metadata future for the polling. + */ + private ScheduledFuture pollMetadataFuture; + + /** + * The delay to wait between the end and begin of metadata polling. + */ + private long repeatedMetadataDelay = Long + .parseLong(CONFIG.getConfigValue(AdminUIConfig.CONF_KEY_REPEATED_METADATA_DELAY).getValue()); + + /** + * The delay to wait between the end and begin of metadata polling. + */ + private long initialMetadataDelay = Long + .parseLong(CONFIG.getConfigValue(AdminUIConfig.CONF_KEY_INITIAL_METADATA_DELAY).getValue()); + + /** + * Executor service for polling all Answer KIs in the network for (possibly) + * updating our stored binding sets. + */ + public ScheduledExecutorService executorService; /** * Intialize a MetadataKB that collects metadata about the available knowledge @@ -82,6 +118,9 @@ public class MetadataKB extends KnowledgeBaseImpl { public MetadataKB(String id, String name, String description) { super(id, name, description); + // create scheduled executor for regular tasks + executorService = Executors.newScheduledThreadPool(1); + // store some predefined prefixes this.prefixes = new PrefixMappingMem(); this.prefixes.setNsPrefixes(PrefixMapping.Standard); @@ -107,28 +146,25 @@ public MetadataKB(String id, String name, String description) { // register the knowledge interactions with the smart connector. this.register(this.aKI); - this.register(this.rKINew, (rki, ei) -> this.handleNewKnowledgeBase(ei)); - this.register(this.rKIChanged, (rki, ei) -> this.handleChangedKnowledgeBase(ei)); - this.register(this.rKIRemoved, (rki, ei) -> this.handleRemovedKnowledgeBase(ei)); + this.register(this.rKINew, (_, ei) -> this.handleNewKnowledgeBase(ei)); + this.register(this.rKIChanged, (_, ei) -> this.handleChangedKnowledgeBase(ei)); + this.register(this.rKIRemoved, (_, ei) -> this.handleRemovedKnowledgeBase(ei)); + scheduleMetadataPolling(); } - @Override - public void syncKIs() { - super.syncKIs(); - - if (timeToSleepAndFetch) { - // to receive the initial state, we do a single Ask (after sleeping for a - // specific amount of time) - try { - Thread.sleep(ConfigProvider.getConfig().getValue(AdminUIConfig.CONF_KEY_INITIAL_METADATA_DELAY, - Integer.class)); - } catch (InterruptedException e) { - LOG.error("Initial metadata KB delay should not fail.", e); - } - this.fetchInitialData(); - this.timeToSleepAndFetch = false; - } + /** + * Schedule polling task with executor service. + */ + public void scheduleMetadataPolling() { + if (repeatedMetadataDelay > 0) + this.pollMetadataFuture = this.executorService.scheduleWithFixedDelay(() -> { + MetadataKB.this.fetchMetaData(); + }, this.initialMetadataDelay, repeatedMetadataDelay, TimeUnit.MILLISECONDS); + else + this.pollMetadataFuture = this.executorService.schedule(() -> { + this.fetchMetaData(); + }, this.initialMetadataDelay, TimeUnit.MILLISECONDS); } public BindingSet handleNewKnowledgeBase(ReactExchangeInfo ei) { @@ -206,11 +242,10 @@ public BindingSet handleRemovedKnowledgeBase(ReactExchangeInfo ei) { return new BindingSet(); } - public void fetchInitialData() { - LOG.info("Retrieving initial other Knowledge Base info..."); + public void fetchMetaData() { + LOG.info("Retrieving Knowledge Base metadata..."); try { - // execute actual *ask* and use previously defined Knowledge Interaction. this.getSC().ask(this.aKI, new BindingSet()).thenAccept(askResult -> { try { @@ -224,7 +259,7 @@ public void fetchInitialData() { eu.knowledge.engine.smartconnector.impl.Util .translateFromApiBindingSet(askResult.getBindings())); this.metadata.setNsPrefixes(this.prefixes); - + LOG.debug("Metadata updated with '{}' RDF statements.", this.metadata.size()); } catch (ParseException e) { LOG.error("{}", e); } @@ -239,7 +274,6 @@ public void fetchInitialData() { } catch (ExecutionException | InterruptedException ee) { LOG.error("{}", ee); } - } protected boolean canReceiveUpdates() { @@ -248,6 +282,7 @@ protected boolean canReceiveUpdates() { public void close() { try { + this.pollMetadataFuture.cancel(true); this.stop().get(); } catch (InterruptedException | ExecutionException e) { LOG.error("Exceptions should not occur when closing the MetadataKB.", e); diff --git a/admin-ui/src/main/java/eu/knowledge/engine/admin/api/RestServer.java b/admin-ui/src/main/java/eu/knowledge/engine/admin/api/RestServer.java index 1335080c9..ea4fe6710 100644 --- a/admin-ui/src/main/java/eu/knowledge/engine/admin/api/RestServer.java +++ b/admin-ui/src/main/java/eu/knowledge/engine/admin/api/RestServer.java @@ -60,7 +60,7 @@ public static void main(String[] args) { AdminUI.newInstance(false); server.join(); } catch (Exception ex) { - LOG.error("{}", ex); + LOG.error("An error occurred running the REST server.", ex); } finally { try { server.stop(); diff --git a/admin-ui/src/main/java/eu/knowledge/engine/admin/api/impl/AdminApiServiceImpl.java b/admin-ui/src/main/java/eu/knowledge/engine/admin/api/impl/AdminApiServiceImpl.java index 937bee8d6..3e3b10cf7 100644 --- a/admin-ui/src/main/java/eu/knowledge/engine/admin/api/impl/AdminApiServiceImpl.java +++ b/admin-ui/src/main/java/eu/knowledge/engine/admin/api/impl/AdminApiServiceImpl.java @@ -111,7 +111,7 @@ public void getSCOverview( @ApiResponses(value = { @ApiResponse(responseCode = "200", description = "If the SC were reloaded."), @ApiResponse(responseCode = "500", description = "If a problem occurred.") }) public Response reloadSCs() { - AdminUI.newInstance(false).fetchInitialData(); + AdminUI.newInstance(false).fetchMetaData(); return Response.noContent().status(200).build(); } diff --git a/admin-ui/src/main/resources/META-INF/microprofile-config.properties b/admin-ui/src/main/resources/META-INF/microprofile-config.properties index 3849e889f..b7b7747ce 100644 --- a/admin-ui/src/main/resources/META-INF/microprofile-config.properties +++ b/admin-ui/src/main/resources/META-INF/microprofile-config.properties @@ -1 +1,2 @@ -initial.metadata.delay = 5000 \ No newline at end of file +initial.metadata.delay = 5000 +repeated.metadata.delay = 0 \ No newline at end of file