Search in sources :

Example 1 with WonNodeInfo

use of won.protocol.service.WonNodeInfo in project webofneeds by researchstudio-sat.

the class WonNodeControllerActor method onReceive.

/**
 * Receive messages about newly discovered won node and decide to crawl or skip
 * processing these won nodes.
 *
 * @param message
 * @throws Exception
 */
@Override
public void onReceive(final Object message) {
    if (message instanceof Terminated) {
        // if it is some other actor handle it differently
        handleConnectionErrors((Terminated) message);
        return;
    }
    if (message.equals(LIFE_CHECK_TICK)) {
        lifeCheck();
        return;
    }
    if (message instanceof WonNodeEvent) {
        WonNodeEvent event = (WonNodeEvent) message;
        if (event.getStatus().equals(WonNodeEvent.STATUS.NEW_WON_NODE_DISCOVERED) || event.getStatus().equals(WonNodeEvent.STATUS.GET_WON_NODE_INFO_FOR_CRAWLING) || event.getStatus().equals(WonNodeEvent.STATUS.RETRY_REGISTER_FAILED_WON_NODE)) {
            // won node has already been discovered and connected
            if (crawlWonNodes.containsKey(event.getWonNodeUri())) {
                log.debug("Won node uri '{}' already discovered", event.getWonNodeUri());
                if (event.getStatus().equals(WonNodeEvent.STATUS.GET_WON_NODE_INFO_FOR_CRAWLING)) {
                    WonNodeInfo wonNodeInfo = crawlWonNodes.get(event.getWonNodeUri()).getWonNodeInfo();
                    WonNodeEvent e = new WonNodeEvent(event.getWonNodeUri(), WonNodeEvent.STATUS.CONNECTED_TO_WON_NODE, wonNodeInfo);
                    pubSubMediator.tell(new DistributedPubSubMediator.Publish(e.getClass().getName(), e), getSelf());
                }
                return;
            }
            // skip crawling of won nodes in the skip list
            if (skipWonNodeUris.contains(event.getWonNodeUri())) {
                log.debug("Skip crawling won node with uri '{}'", event.getWonNodeUri());
                WonNodeEvent e = new WonNodeEvent(event.getWonNodeUri(), WonNodeEvent.STATUS.SKIP_WON_NODE);
                pubSubMediator.tell(new DistributedPubSubMediator.Publish(e.getClass().getName(), e), getSelf());
                return;
            }
            // shall we try to connect to the won node or has it failed already ?
            if (failedWonNodeUris.contains(event.getWonNodeUri())) {
                log.debug("Suppress connection to already failed won node with uri {} , will try to connect later ...", event.getWonNodeUri());
                return;
            }
            // try the connect to won node
            boolean logRegisterWarningForWonNode = event.getStatus().equals(WonNodeEvent.STATUS.RETRY_REGISTER_FAILED_WON_NODE);
            WonNodeConnection wonNodeConnection = addWonNodeForCrawling(event.getWonNodeUri(), logRegisterWarningForWonNode);
            // connection failed ?
            if (failedWonNodeUris.contains(event.getWonNodeUri())) {
                log.debug("Still could not connect to won node with uri: {}, will retry later ...", event.getWonNodeUri());
                return;
            }
            // tell the crawler about discovered won nodes
            if (wonNodeConnection == null || wonNodeConnection.getWonNodeInfo() == null) {
                log.error("Cannot retrieve won node info from won node connection!");
                return;
            }
            WonNodeEvent e = new WonNodeEvent(event.getWonNodeUri(), WonNodeEvent.STATUS.CONNECTED_TO_WON_NODE, wonNodeConnection.getWonNodeInfo());
            pubSubMediator.tell(new DistributedPubSubMediator.Publish(e.getClass().getName(), e), getSelf());
            return;
        }
    }
    // send back hints to won nodes
    if (message instanceof HintEvent) {
        processHint((HintEvent) message);
        return;
    } else if (message instanceof BulkHintEvent) {
        BulkHintEvent bulkHintEvent = (BulkHintEvent) message;
        for (HintEvent hint : bulkHintEvent.getHintEvents()) {
            processHint(hint);
        }
        return;
    }
    unhandled(message);
}
Also used : BulkHintEvent(won.matcher.service.common.event.BulkHintEvent) DistributedPubSubMediator(akka.cluster.pubsub.DistributedPubSubMediator) WonNodeInfo(won.protocol.service.WonNodeInfo) WonNodeConnection(won.matcher.service.nodemanager.pojo.WonNodeConnection) WonNodeEvent(won.matcher.service.common.event.WonNodeEvent) HintEvent(won.matcher.service.common.event.HintEvent) BulkHintEvent(won.matcher.service.common.event.BulkHintEvent)

Example 2 with WonNodeInfo

use of won.protocol.service.WonNodeInfo in project webofneeds by researchstudio-sat.

the class WonNodeControllerActor method preStart.

@Override
public void preStart() {
    // Create a scheduler to execute the life check for each won node regularly
    getContext().system().scheduler().schedule(config.getLifeCheckDuration(), config.getLifeCheckDuration(), getSelf(), LIFE_CHECK_TICK, getContext().dispatcher(), null);
    // Subscribe for won node events
    pubSubMediator = DistributedPubSub.get(getContext().system()).mediator();
    pubSubMediator.tell(new DistributedPubSubMediator.Subscribe(WonNodeEvent.class.getName(), getSelf()), getSelf());
    // Subscribe for hint events
    pubSubMediator.tell(new DistributedPubSubMediator.Subscribe(HintEvent.class.getName(), getSelf()), getSelf());
    pubSubMediator.tell(new DistributedPubSubMediator.Subscribe(BulkHintEvent.class.getName(), getSelf()), getSelf());
    // set won nodes to skip by configuration
    skipWonNodeUris.addAll(config.getSkipWonNodes());
    // get all known won node uris from RDF store
    Set<WonNodeInfo> wonNodeInfo = new HashSet<>();
    try {
        wonNodeInfo = sparqlService.retrieveAllWonNodeInfo();
    } catch (Exception e) {
        log.error("Error querying SPARQL endpoint {}. SPARQL endpoint must be running at matcher service startup!", sparqlService.getSparqlEndpoint());
        log.error("Exception was: {}", e);
        log.info("Shut down matcher service!");
        System.exit(-1);
    }
    // Treat the known won nodes as newly discovered won nodes to register them again at startup of matcher service
    for (WonNodeInfo nodeInfo : wonNodeInfo) {
        if (!config.getCrawlWonNodes().contains(nodeInfo.getWonNodeURI())) {
            WonNodeEvent e = new WonNodeEvent(nodeInfo.getWonNodeURI(), WonNodeEvent.STATUS.NEW_WON_NODE_DISCOVERED);
            pubSubMediator.tell(new DistributedPubSubMediator.Publish(e.getClass().getName(), e), getSelf());
        }
    }
    // initialize the won nodes from the config file to crawl
    for (String nodeUri : config.getCrawlWonNodes()) {
        if (!skipWonNodeUris.contains(nodeUri)) {
            if (!crawlWonNodes.containsKey(nodeUri)) {
                WonNodeEvent e = new WonNodeEvent(nodeUri, WonNodeEvent.STATUS.NEW_WON_NODE_DISCOVERED);
                pubSubMediator.tell(new DistributedPubSubMediator.Publish(e.getClass().getName(), e), getSelf());
            }
        }
    }
    // initialize the crawler
    crawler = getContext().actorOf(SpringExtension.SpringExtProvider.get(getContext().system()).props(MasterCrawlerActor.class), "MasterCrawlerActor");
    // initialize the need event save actor
    saveNeedActor = getContext().actorOf(SpringExtension.SpringExtProvider.get(getContext().system()).props(SaveNeedEventActor.class), "SaveNeedEventActor");
}
Also used : DistributedPubSubMediator(akka.cluster.pubsub.DistributedPubSubMediator) WonNodeInfo(won.protocol.service.WonNodeInfo) WonNodeEvent(won.matcher.service.common.event.WonNodeEvent)

Example 3 with WonNodeInfo

use of won.protocol.service.WonNodeInfo in project webofneeds by researchstudio-sat.

the class WonNodeControllerActor method addWonNodeForCrawling.

/**
 * Try to register at won nodes and add them for crawling
 *
 * @param wonNodeUri URI of the won node meta data resource
 * @param logWonNodeRegisterWarning if true then log the failed register attempts as warning, otherwise as debug level
 * @return won node connection if successfully connected, otherwise null
 */
private WonNodeConnection addWonNodeForCrawling(String wonNodeUri, boolean logWonNodeRegisterWarning) {
    WonNodeConnection con = null;
    Dataset ds = null;
    WonNodeInfo nodeInfo = null;
    // try register at won node
    try {
        registrationClient.register(wonNodeUri);
        ds = linkedDataSource.getDataForResource(URI.create(wonNodeUri));
    } catch (Exception e) {
        addFailedWonNode(wonNodeUri, con);
        if (logWonNodeRegisterWarning) {
            log.warning("Error requesting won node information from {}", wonNodeUri);
            log.warning("Exception message: {} \nCause: {} ", e.getMessage(), e.getCause());
        } else {
            log.debug("Error requesting won node information from {}", wonNodeUri);
            log.debug("Exception message: {} \nCause: {} ", e.getMessage(), e.getCause());
        }
        return null;
    }
    // try save won node info in local rdf store
    try {
        sparqlService.updateNamedGraphsOfDataset(ds);
        nodeInfo = sparqlService.getWonNodeInfoFromDataset(ds);
    } catch (Exception e) {
        addFailedWonNode(wonNodeUri, con);
        log.error("Error saving won node information from {} into RDF store with SPARQL endpoint {}", wonNodeUri, sparqlService.getSparqlEndpoint());
        log.error("Exception message: {} \nCause: {} ", e.getMessage(), e.getCause());
        return null;
    }
    // try subscribe need updates at won node
    try {
        con = subscribeNeedUpdates(nodeInfo);
        crawlWonNodes.put(nodeInfo.getWonNodeURI(), con);
        failedWonNodeUris.remove(nodeInfo.getWonNodeURI());
        log.info("registered won node {} and start crawling it", nodeInfo.getWonNodeURI());
    } catch (Exception e) {
        addFailedWonNode(wonNodeUri, con);
        log.error("Error subscribing for need updates at won node {}", wonNodeUri);
        log.error("Exception message: {} \nCause: {} ", e.getMessage(), e.getCause());
    }
    return con;
}
Also used : Dataset(org.apache.jena.query.Dataset) WonNodeInfo(won.protocol.service.WonNodeInfo) WonNodeConnection(won.matcher.service.nodemanager.pojo.WonNodeConnection)

Example 4 with WonNodeInfo

use of won.protocol.service.WonNodeInfo in project webofneeds by researchstudio-sat.

the class WonNodeInformationServiceImpl method getWonNodeInformation.

@Override
public WonNodeInfo getWonNodeInformation(URI wonNodeURI) {
    assert wonNodeURI != null;
    Dataset nodeDataset = linkedDataSource.getDataForResource(wonNodeURI);
    WonNodeInfo info = WonRdfUtils.WonNodeUtils.getWonNodeInfo(wonNodeURI, nodeDataset);
    if (info == null)
        throw new IllegalStateException("Could not obtain WonNodeInformation for URI " + wonNodeURI);
    return info;
}
Also used : Dataset(org.apache.jena.query.Dataset) WonNodeInfo(won.protocol.service.WonNodeInfo)

Example 5 with WonNodeInfo

use of won.protocol.service.WonNodeInfo in project webofneeds by researchstudio-sat.

the class UriConsistencyCheckingWonMessageProcessor method process.

@Override
public WonMessage process(final WonMessage message) throws UriAlreadyInUseException {
    // extract info about own, sender and receiver nodes:
    URI senderNode = message.getSenderNodeURI();
    URI receiverNode = message.getReceiverNodeURI();
    WonNodeInfo senderNodeInfo = null;
    WonNodeInfo receiverNodeInfo = null;
    if (senderNode != null && !WonMessageType.HINT_MESSAGE.equals(message.getMessageType())) {
        // do not check the sender node for a hint
        // TODO: change this behaviour as soon as a matcher uses a WoN node
        senderNodeInfo = wonNodeInformationService.getWonNodeInformation(senderNode);
    }
    if (receiverNode != null) {
        receiverNodeInfo = wonNodeInformationService.getWonNodeInformation(receiverNode);
    }
    WonNodeInfo ownNodeInfo = null;
    URI msgUri = message.getMessageURI();
    if (msgUri.getScheme().equals(senderNode.getScheme()) && msgUri.getAuthority().equals(senderNode.getAuthority())) {
        ownNodeInfo = senderNodeInfo;
    } else if (msgUri.getScheme().equals(receiverNode.getScheme()) && msgUri.getAuthority().equals(receiverNode.getAuthority())) {
        ownNodeInfo = receiverNodeInfo;
    }
    URI ownNode = URI.create(ownNodeInfo.getWonNodeURI());
    // do checks for consistency between these nodes and message direction, as well as needs,
    // events and connection uris:
    // my node should be either receiver or sender node
    checkHasMyNode(message, ownNode);
    // Any message URI used must conform to a URI pattern specified by the respective publishing service:
    // Check that event URI corresponds to my pattern
    checkLocalEventURI(message, ownNodeInfo);
    // Check that remote URI, if any, correspond to ?senderNode's event pattern
    checkRemoteEventURI(message, senderNodeInfo);
    // Check that need URI for create_need message corresponds to my pattern
    checkCreateMsgNeedURI(message, ownNodeInfo);
    // Specified sender-receiverNeed/Connection must conform to sender-receiverNode URI pattern
    checkSenders(senderNodeInfo, message);
    checkReceivers(receiverNodeInfo, message);
    // Check that my node is sender or receiver node URI, depending on the message direction
    checkDirection(message, ownNode);
    return message;
}
Also used : WonNodeInfo(won.protocol.service.WonNodeInfo) URI(java.net.URI)

Aggregations

WonNodeInfo (won.protocol.service.WonNodeInfo)7 DistributedPubSubMediator (akka.cluster.pubsub.DistributedPubSubMediator)2 Dataset (org.apache.jena.query.Dataset)2 WonNodeEvent (won.matcher.service.common.event.WonNodeEvent)2 WonNodeConnection (won.matcher.service.nodemanager.pojo.WonNodeConnection)2 URI (java.net.URI)1 HashSet (java.util.HashSet)1 RDFNode (org.apache.jena.rdf.model.RDFNode)1 BulkHintEvent (won.matcher.service.common.event.BulkHintEvent)1 HintEvent (won.matcher.service.common.event.HintEvent)1 DataIntegrityException (won.protocol.exception.DataIntegrityException)1