Skip to content
Merged
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
4 changes: 2 additions & 2 deletions admin-ui/src/main/java/eu/knowledge/engine/admin/AdminUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";

}
85 changes: 60 additions & 25 deletions admin-ui/src/main/java/eu/knowledge/engine/admin/MetadataKB.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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 = """
Expand Down Expand Up @@ -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
Expand All @@ -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);
Expand All @@ -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) {
Expand Down Expand Up @@ -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 {
Expand All @@ -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);
}
Expand All @@ -239,7 +274,6 @@ public void fetchInitialData() {
} catch (ExecutionException | InterruptedException ee) {
LOG.error("{}", ee);
}

}

protected boolean canReceiveUpdates() {
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
initial.metadata.delay = 5000
initial.metadata.delay = 5000
repeated.metadata.delay = 0
Loading