Search in sources :

Example 1 with ClusterInformation

use of net.dempsy.ClusterInformation in project Dempsy by Dempsy.

the class ApplicationState method apply.

public ApplicationState apply(final ApplicationState.Update update, final TransportManager tmanager, final NodeStatsCollector statsCollector, final RoutingStrategyManager manager) {
    // apply toDelete first.
    final Set<NodeAddress> toDelete = update.toDelete;
    if (toDelete.size() > 0) {
        // just clear all senders.
        for (final NodeAddress a : toDelete) {
            final Sender s = senders.get(a);
            if (s != null)
                s.stop();
        }
    }
    final Map<NodeAddress, NodeInformation> newCurrent = new HashMap<>();
    // the one's to carry over.
    final Set<NodeInformation> leaveAlone = update.leaveAlone;
    for (final NodeInformation cur : leaveAlone) {
        newCurrent.put(cur.nodeAddress, cur);
    }
    // add new senders
    final Set<NodeInformation> toAdd = update.toAdd;
    for (final NodeInformation cur : toAdd) {
        newCurrent.put(cur.nodeAddress, cur);
    }
    // now flush out the remaining caches.
    // collapse all clusterInfos
    final Set<ClusterInformation> allCis = new HashSet<>();
    newCurrent.values().forEach(ni -> allCis.addAll(ni.clusterInfoByClusterId.values()));
    final Map<String, RoutingStrategy.Router> newOutboundByClusterName = new HashMap<>();
    final Map<String, Set<String>> cnByType = new HashMap<>();
    final Set<String> knownClusterOutbounds = new HashSet<>(outboundByClusterName_.keySet());
    for (final ClusterInformation ci : allCis) {
        final String clusterName = ci.clusterId.clusterName;
        final RoutingStrategy.Router ob = outboundByClusterName_.get(clusterName);
        knownClusterOutbounds.remove(clusterName);
        if (ob != null)
            newOutboundByClusterName.put(clusterName, ob);
        else {
            final RoutingStrategy.Factory obfactory = manager.getAssociatedInstance(ci.routingStrategyTypeId);
            final RoutingStrategy.Router nob = obfactory.getStrategy(ci.clusterId);
            newOutboundByClusterName.put(clusterName, nob);
        }
        // add all of the message types handled.
        ci.messageTypesHandled.forEach(mt -> {
            Set<String> entry = cnByType.get(mt);
            if (entry == null) {
                entry = new HashSet<>();
                cnByType.put(mt, entry);
            }
            entry.add(clusterName);
        });
    }
    return new ApplicationState(newOutboundByClusterName, cnByType, newCurrent, tmanager, thisNode);
}
Also used : NodeInformation(net.dempsy.NodeInformation) Set(java.util.Set) HashSet(java.util.HashSet) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ClusterInformation(net.dempsy.ClusterInformation) Sender(net.dempsy.transport.Sender) RoutingStrategy(net.dempsy.router.RoutingStrategy) NodeAddress(net.dempsy.transport.NodeAddress) HashSet(java.util.HashSet)

Example 2 with ClusterInformation

use of net.dempsy.ClusterInformation in project Dempsy by Dempsy.

the class ApplicationState method apply.

public ApplicationState apply(final ApplicationState.Update update, final TransportManager tmanager, final NodeStatsCollector statsCollector, final RoutingStrategyManager manager, final String thisNodeId) {
    // apply toDelete first.
    final Set<NodeAddress> toDelete = update.toDelete;
    final boolean infoEnabled = LOGGER_SESSION.isInfoEnabled();
    if (toDelete.size() > 0) {
        // just clear all senders.
        if (infoEnabled)
            LOGGER_SESSION.info("[{}] Applying update to topology resulting in removing several destinations:", thisNodeId);
        for (final NodeAddress a : toDelete) {
            final Sender s = senders.remove(a);
            if (infoEnabled)
                LOGGER_SESSION.info("[{}]      removing sender ({}) to {}", thisNodeId, s, a);
            if (s != null)
                s.stop();
        }
    }
    final Map<NodeAddress, NodeInformation> newCurrent = new HashMap<>();
    // the one's to carry over.
    final Set<NodeInformation> leaveAlone = update.leaveAlone;
    if (leaveAlone.size() > 0)
        if (infoEnabled)
            LOGGER_SESSION.info("[{}] Applying update to topology resulting in leaving several destinations:", thisNodeId);
    for (final NodeInformation cur : leaveAlone) {
        if (infoEnabled)
            LOGGER_SESSION.info("[{}]      leaving alone : {}", thisNodeId, Optional.ofNullable(cur).map(ni -> ni.nodeAddress).orElse(null));
        newCurrent.put(cur.nodeAddress, cur);
    }
    // add new senders
    final Set<NodeInformation> toAdd = update.toAdd;
    if (toAdd.size() > 0)
        if (infoEnabled)
            LOGGER_SESSION.info("[{}] Applying update to topology resulting in adding several destinations:", thisNodeId);
    for (final NodeInformation cur : toAdd) {
        if (infoEnabled)
            LOGGER_SESSION.info("[{}]      adding : {}", thisNodeId, Optional.ofNullable(cur).map(ni -> ni.nodeAddress).orElse(null));
        newCurrent.put(cur.nodeAddress, cur);
    }
    // now flush out the remaining caches.
    // collapse all clusterInfos
    final Set<ClusterInformation> allCis = new HashSet<>();
    newCurrent.values().forEach(ni -> allCis.addAll(ni.clusterInfoByClusterId.values()));
    final Map<String, RoutingStrategy.Router> newOutboundByClusterName = new HashMap<>();
    final Map<String, Set<String>> cnByType = new HashMap<>();
    final Set<String> knownClusterOutbounds = new HashSet<>(outboundByClusterName_.keySet());
    for (final ClusterInformation ci : allCis) {
        final String clusterName = ci.clusterId.clusterName;
        final RoutingStrategy.Router ob = outboundByClusterName_.get(clusterName);
        knownClusterOutbounds.remove(clusterName);
        if (ob != null)
            newOutboundByClusterName.put(clusterName, ob);
        else {
            final RoutingStrategy.Factory obfactory = manager.getAssociatedInstance(ci.routingStrategyTypeId);
            final RoutingStrategy.Router nob = obfactory.getStrategy(ci.clusterId);
            newOutboundByClusterName.put(clusterName, nob);
        }
        // add all of the message types handled.
        ci.messageTypesHandled.forEach(mt -> {
            Set<String> entry = cnByType.get(mt);
            if (entry == null) {
                entry = new HashSet<>();
                cnByType.put(mt, entry);
            }
            entry.add(clusterName);
        });
    }
    return new ApplicationState(newOutboundByClusterName, cnByType, newCurrent, tmanager, thisNode, senders);
}
Also used : Sender(net.dempsy.transport.Sender) TransportManager(net.dempsy.transport.TransportManager) ClusterInformation(net.dempsy.ClusterInformation) ContainerAddress(net.dempsy.router.RoutingStrategy.ContainerAddress) KeyedMessageWithType(net.dempsy.messages.KeyedMessageWithType) NodeAddress(net.dempsy.transport.NodeAddress) Collection(java.util.Collection) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) SenderFactory(net.dempsy.transport.SenderFactory) Set(java.util.Set) HashMap(java.util.HashMap) LOGGER(net.dempsy.intern.OutgoingDispatcher.LOGGER) ArrayList(java.util.ArrayList) RoutingStrategyManager(net.dempsy.router.RoutingStrategyManager) HashSet(java.util.HashSet) NodeStatsCollector(net.dempsy.monitoring.NodeStatsCollector) List(java.util.List) RoutingStrategy(net.dempsy.router.RoutingStrategy) Map(java.util.Map) Optional(java.util.Optional) LOGGER_SESSION(net.dempsy.intern.OutgoingDispatcher.LOGGER_SESSION) NodeInformation(net.dempsy.NodeInformation) NodeInformation(net.dempsy.NodeInformation) Set(java.util.Set) HashSet(java.util.HashSet) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ClusterInformation(net.dempsy.ClusterInformation) Sender(net.dempsy.transport.Sender) RoutingStrategy(net.dempsy.router.RoutingStrategy) NodeAddress(net.dempsy.transport.NodeAddress) HashSet(java.util.HashSet)

Aggregations

HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 Set (java.util.Set)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 ClusterInformation (net.dempsy.ClusterInformation)2 NodeInformation (net.dempsy.NodeInformation)2 RoutingStrategy (net.dempsy.router.RoutingStrategy)2 NodeAddress (net.dempsy.transport.NodeAddress)2 Sender (net.dempsy.transport.Sender)2 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 List (java.util.List)1 Map (java.util.Map)1 Optional (java.util.Optional)1 LOGGER (net.dempsy.intern.OutgoingDispatcher.LOGGER)1 LOGGER_SESSION (net.dempsy.intern.OutgoingDispatcher.LOGGER_SESSION)1 KeyedMessageWithType (net.dempsy.messages.KeyedMessageWithType)1 NodeStatsCollector (net.dempsy.monitoring.NodeStatsCollector)1 ContainerAddress (net.dempsy.router.RoutingStrategy.ContainerAddress)1 RoutingStrategyManager (net.dempsy.router.RoutingStrategyManager)1