Search in sources :

Example 1 with WonNodeConnection

use of won.matcher.service.nodemanager.pojo.WonNodeConnection 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
 */
@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()) {
            expandToSocketHintsIfAppropriate(hint).forEach(h -> processHint(h));
        }
        return;
    }
    unhandled(message);
}
Also used : LinkedDataSource(won.protocol.util.linkeddata.LinkedDataSource) java.util(java.util) won.matcher.service.common.event(won.matcher.service.common.event) RegistrationClient(won.cryptography.service.RegistrationClient) MasterCrawlerActor(won.matcher.service.crawler.actor.MasterCrawlerActor) ActiveMqWonNodeConnectionFactory(won.matcher.service.nodemanager.config.ActiveMqWonNodeConnectionFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) akka.actor(akka.actor) WonNodeSparqlService(won.matcher.service.nodemanager.service.WonNodeSparqlService) Logging(akka.event.Logging) Scope(org.springframework.context.annotation.Scope) WonNodeConnection(won.matcher.service.nodemanager.pojo.WonNodeConnection) IteratorUtils(org.apache.commons.collections.IteratorUtils) DistributedPubSub(akka.cluster.pubsub.DistributedPubSub) MessagingContext(won.cryptography.ssl.MessagingContext) URI(java.net.URI) Dataset(org.apache.jena.query.Dataset) WonLinkedDataUtils(won.protocol.util.linkeddata.WonLinkedDataUtils) LoggingAdapter(akka.event.LoggingAdapter) Function(akka.japi.Function) Pair(won.protocol.util.RdfUtils.Pair) Collectors(java.util.stream.Collectors) DistributedPubSubMediator(akka.cluster.pubsub.DistributedPubSubMediator) WonNodeInfo(won.protocol.service.WonNodeInfo) WonNodeControllerConfig(won.matcher.service.nodemanager.config.WonNodeControllerConfig) Component(org.springframework.stereotype.Component) Duration(scala.concurrent.duration.Duration) SpringExtension(won.matcher.service.common.spring.SpringExtension) HintDBService(won.matcher.service.nodemanager.service.HintDBService) DistributedPubSubMediator(akka.cluster.pubsub.DistributedPubSubMediator) WonNodeInfo(won.protocol.service.WonNodeInfo) WonNodeConnection(won.matcher.service.nodemanager.pojo.WonNodeConnection)

Example 2 with WonNodeConnection

use of won.matcher.service.nodemanager.pojo.WonNodeConnection in project webofneeds by researchstudio-sat.

the class WonNodeControllerActor method sendHint.

/**
 * Send hint event out to won node
 *
 * @param hint
 */
private void sendHint(HintEvent hint) {
    if (!crawlWonNodes.containsKey(hint.getRecipientWonNodeUri())) {
        log.warning("cannot send hint to won node {}! Is registered with the won node controller?", hint.getRecipientWonNodeUri());
        return;
    }
    // send hint to first won node
    WonNodeConnection fromWonNodeConnection = crawlWonNodes.get(hint.getRecipientWonNodeUri());
    log.info("Send hint {} to won node {}", hint, hint.getRecipientWonNodeUri());
    fromWonNodeConnection.getHintProducer().tell(hint, getSelf());
}
Also used : WonNodeConnection(won.matcher.service.nodemanager.pojo.WonNodeConnection)

Example 3 with WonNodeConnection

use of won.matcher.service.nodemanager.pojo.WonNodeConnection 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.getDataForPublicResource(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 atom updates at won node
    try {
        con = subscribeAtomUpdates(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 atom 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 WonNodeConnection

use of won.matcher.service.nodemanager.pojo.WonNodeConnection in project webofneeds by researchstudio-sat.

the class ActiveMqWonNodeConnectionFactory method createWonNodeConnection.

/**
 * Create a {@link won.matcher.service.nodemanager.pojo.WonNodeConnection} for
 * active mq
 *
 * @param context actor context to create the message consuming actors in
 * @param wonNodeInfo info about the won node (e.g. topics to subscribe)
 * @return the connection
 * @throws FailedToCreateConsumerException
 */
public static WonNodeConnection createWonNodeConnection(UntypedActorContext context, WonNodeInfo wonNodeInfo, MessagingContext messagingContext) {
    // read won node info
    String activeMq = WON.WonOverActiveMq.toString();
    String brokerUri = wonNodeInfo.getSupportedProtocolImplParamValue(activeMq, WON.brokerUri.toString());
    String createdTopic = wonNodeInfo.getSupportedProtocolImplParamValue(activeMq, WON.atomCreatedTopic.toString());
    String activatedTopic = wonNodeInfo.getSupportedProtocolImplParamValue(activeMq, WON.atomActivatedTopic.toString());
    String deactivatedTopic = wonNodeInfo.getSupportedProtocolImplParamValue(activeMq, WON.atomDeactivatedTopic.toString());
    String hintQueue = wonNodeInfo.getSupportedProtocolImplParamValue(activeMq, WON.matcherQueue.toString());
    // create the activemq component for this won node
    String uuid = UUID.randomUUID().toString();
    String componentName = "activemq-" + uuid;
    ActiveMQConnectionFactory connectionFactory = createConnectionFactory(brokerUri, messagingContext);
    // connectionFactory.setExceptionListener( ... )
    Camel camel = CamelExtension.get(context.system());
    camel.context().addComponent(componentName, JmsComponent.jmsComponent(connectionFactory));
    // create the actors that receive the messages (atom events)
    String createdComponent = componentName + ":topic:" + createdTopic + "?testConnectionOnStartup=false&transacted=false";
    Props createdProps = SpringExtension.SpringExtProvider.get(context.system()).props(AtomConsumerProtocolActor.class, createdComponent);
    ActorRef created = context.actorOf(createdProps, "ActiveMqAtomCreatedConsumerProtocolActor-" + uuid);
    logger.info("Create camel component JMS listener {} for won node {}", createdComponent, wonNodeInfo.getWonNodeURI());
    ActorRef activated = created;
    if (!activatedTopic.equals(createdTopic)) {
        String activatedComponent = componentName + ":topic:" + activatedTopic + "?testConnectionOnStartup=false&transacted=false";
        Props activatedProps = SpringExtension.SpringExtProvider.get(context.system()).props(AtomConsumerProtocolActor.class, activatedComponent);
        activated = context.actorOf(activatedProps, "ActiveMqAtomActivatedConsumerProtocolActor-" + uuid);
        logger.info("Create camel component JMS listener {} for won node {}", activatedComponent, wonNodeInfo.getWonNodeURI());
    }
    ActorRef deactivated;
    if (deactivatedTopic.equals(createdTopic)) {
        deactivated = created;
    } else if (deactivatedTopic.equals(activatedTopic)) {
        deactivated = activated;
    } else {
        String deactivatedComponent = componentName + ":topic:" + deactivatedTopic + "?testConnectionOnStartup=false&transacted=false";
        Props deactivatedProps = SpringExtension.SpringExtProvider.get(context.system()).props(AtomConsumerProtocolActor.class, deactivatedComponent);
        deactivated = context.actorOf(deactivatedProps, "ActiveMqAtomDeactivatedConsumerProtocolActor-" + uuid);
        logger.info("Create camel component JMS listener {} for won node {}", deactivatedComponent, wonNodeInfo.getWonNodeURI());
    }
    // create the actor that sends messages (hint events)
    String hintComponent = componentName + ":queue:" + hintQueue + "?transacted=false";
    Props hintProps = SpringExtension.SpringExtProvider.get(context.system()).props(HintProducerProtocolActor.class, hintComponent, null);
    ActorRef hintProducer = context.actorOf(hintProps, "ActiveMqHintProducerProtocolActor-" + uuid);
    logger.info("Create camel component JMS listener {} for won node {}", hintComponent, wonNodeInfo.getWonNodeURI());
    // watch the created consumers from the context to get informed when they are
    // terminated
    context.watch(created);
    context.watch(activated);
    context.watch(deactivated);
    context.watch(hintProducer);
    // create the connection
    WonNodeConnection jmsConnection = new WonNodeConnection(wonNodeInfo, created, activated, deactivated, hintProducer);
    return jmsConnection;
}
Also used : ActiveMQConnectionFactory(org.apache.activemq.ActiveMQConnectionFactory) ActorRef(akka.actor.ActorRef) AtomConsumerProtocolActor(won.matcher.service.nodemanager.actor.AtomConsumerProtocolActor) Camel(akka.camel.Camel) Props(akka.actor.Props) WonNodeConnection(won.matcher.service.nodemanager.pojo.WonNodeConnection)

Example 5 with WonNodeConnection

use of won.matcher.service.nodemanager.pojo.WonNodeConnection in project webofneeds by researchstudio-sat.

the class WonNodeControllerActor method handleConnectionErrors.

/**
 * Handles connections errors that occur when the atom consumer actors are
 * terminated.
 *
 * @param t messages that holds a reference to consumer actor that was
 * terminated
 */
private void handleConnectionErrors(Terminated t) {
    for (String uri : crawlWonNodes.keySet()) {
        WonNodeConnection con = crawlWonNodes.get(uri);
        if (con != null) {
            if (con.getAtomCreatedConsumer().equals(t.getActor())) {
                log.error("AtomCreatedConsumer '{}' of won '{}' has been shut down", t.getActor(), uri);
                addFailedWonNode(con.getWonNodeInfo().getWonNodeURI(), con);
            } else if (con.getAtomActivatedConsumer().equals(t.getActor())) {
                log.error("AtomActivatedConsumer '{}' of won '{}' has been shut down", t.getActor(), uri);
                addFailedWonNode(con.getWonNodeInfo().getWonNodeURI(), con);
            } else if (con.getAtomDeactivatedConsumer().equals(t.getActor())) {
                log.error("AtomDeactivatedConsumer '{}' of won '{}' has been shut down", t.getActor(), uri);
                addFailedWonNode(con.getWonNodeInfo().getWonNodeURI(), con);
            } else if (con.getHintProducer().equals(t.getActor())) {
                log.error("HintProducer '{}' of won '{}' has been shut down", t.getActor(), uri);
                addFailedWonNode(con.getWonNodeInfo().getWonNodeURI(), con);
            }
        }
    }
}
Also used : WonNodeConnection(won.matcher.service.nodemanager.pojo.WonNodeConnection)

Aggregations

WonNodeConnection (won.matcher.service.nodemanager.pojo.WonNodeConnection)5 Dataset (org.apache.jena.query.Dataset)2 WonNodeInfo (won.protocol.service.WonNodeInfo)2 akka.actor (akka.actor)1 ActorRef (akka.actor.ActorRef)1 Props (akka.actor.Props)1 Camel (akka.camel.Camel)1 DistributedPubSub (akka.cluster.pubsub.DistributedPubSub)1 DistributedPubSubMediator (akka.cluster.pubsub.DistributedPubSubMediator)1 Logging (akka.event.Logging)1 LoggingAdapter (akka.event.LoggingAdapter)1 Function (akka.japi.Function)1 URI (java.net.URI)1 java.util (java.util)1 Collectors (java.util.stream.Collectors)1 ActiveMQConnectionFactory (org.apache.activemq.ActiveMQConnectionFactory)1 IteratorUtils (org.apache.commons.collections.IteratorUtils)1 Autowired (org.springframework.beans.factory.annotation.Autowired)1 Scope (org.springframework.context.annotation.Scope)1 Component (org.springframework.stereotype.Component)1