Search in sources :

Example 1 with InstancesDiff

use of org.apache.sling.discovery.commons.InstancesDiff in project sling by apache.

the class TopologyWebConsolePlugin method handleTopologyEvent.

/**
     * keep a truncated history of the log events for information purpose (to be shown in the webconsole)
     */
@Override
public void handleTopologyEvent(final TopologyEvent event) {
    if (event.getType() == Type.PROPERTIES_CHANGED) {
        this.currentView = event.getNewView();
        StringBuilder sb = new StringBuilder();
        InstancesDiff instanceDiff = new InstancesDiff(event.getOldView(), event.getNewView());
        // there shouldn't be any instances added, but for paranoia reason:
        Collection<InstanceDescription> added = instanceDiff.added().get();
        if (!added.isEmpty()) {
            sb.append("instances were added:");
            for (InstanceDescription instance : added) {
                sb.append(" ");
                sb.append(instance.getSlingId());
            }
            sb.append(".");
        }
        // there shouldn't be any instances removed as well, but again for paranoia reason:
        Collection<InstanceDescription> removed = instanceDiff.removed().get();
        if (!removed.isEmpty()) {
            sb.append("instances were removed:");
            for (InstanceDescription instance : added) {
                sb.append(" ");
                sb.append(instance.getSlingId());
            }
            sb.append(".");
        }
        Set<InstanceDescription> newInstances = event.getNewView().getInstances();
        for (Iterator<InstanceDescription> it = newInstances.iterator(); it.hasNext(); ) {
            final InstanceDescription newInstanceDescription = it.next();
            InstanceDescription oldInstanceDescription = findInstance(event.getOldView(), newInstanceDescription.getSlingId());
            if (oldInstanceDescription == null) {
                logger.error("handleTopologyEvent: got a property changed but did not find instance " + newInstanceDescription + " in oldview.. event=" + event);
                sb.append("did not find instance in old view: " + newInstanceDescription.getSlingId() + ".");
                continue;
            }
            Map<String, String> oldProps = oldInstanceDescription.getProperties();
            Map<String, String> newProps = newInstanceDescription.getProperties();
            StringBuilder diff = diff(oldProps, newProps);
            if (diff.length() > 0) {
                if (sb.length() != 0) {
                    sb.append(", ");
                }
                sb.append("on instance " + newInstanceDescription.getSlingId() + (newInstanceDescription.isLeader() ? " [isLeader]" : "") + ": " + diff + ". ");
            }
        }
        addEventLog(event.getType(), "details: " + sb.toString());
    } else if (event.getType() == Type.TOPOLOGY_INIT) {
        this.currentView = event.getNewView();
        StringBuilder details = new StringBuilder();
        for (Iterator<InstanceDescription> it = event.getNewView().getInstances().iterator(); it.hasNext(); ) {
            InstanceDescription newInstance = it.next();
            if (details.length() != 0) {
                details.append(", ");
            }
            details.append(newInstance.getSlingId());
            if (newInstance.isLeader()) {
                details.append(" [isLeader]");
            }
        }
        addEventLog(event.getType(), "view: " + shortViewInfo(event.getNewView()) + ". " + details);
    } else if (event.getType() == Type.TOPOLOGY_CHANGING) {
        this.currentView = event.getOldView();
        addEventLog(event.getType(), "old view: " + shortViewInfo(event.getOldView()));
    } else {
        this.currentView = event.getNewView();
        if (event.getOldView() == null) {
            addEventLog(event.getType(), "new view: " + shortViewInfo(event.getNewView()));
        } else {
            StringBuilder details = new StringBuilder();
            for (Iterator<InstanceDescription> it = event.getNewView().getInstances().iterator(); it.hasNext(); ) {
                InstanceDescription newInstance = it.next();
                if (findInstance(event.getOldView(), newInstance.getSlingId()) == null) {
                    if (details.length() != 0) {
                        details.append(", ");
                    }
                    details.append(newInstance.getSlingId() + " joined");
                }
            }
            for (Iterator<InstanceDescription> it = event.getOldView().getInstances().iterator(); it.hasNext(); ) {
                InstanceDescription oldInstance = it.next();
                if (findInstance(event.getNewView(), oldInstance.getSlingId()) == null) {
                    if (details.length() != 0) {
                        details.append(", ");
                    }
                    details.append(oldInstance.getSlingId() + " left");
                }
            }
            final InstanceDescription li = event.getNewView().getLocalInstance();
            if (li != null) {
                ClusterView clusterView = li.getClusterView();
                if (clusterView != null) {
                    final InstanceDescription leader = clusterView.getLeader();
                    if (leader != null) {
                        if (details.length() != 0) {
                            details.append(", ");
                        }
                        details.append("[isLeader: " + leader.getSlingId() + "]");
                    }
                }
            }
            addEventLog(event.getType(), "old view: " + shortViewInfo(event.getOldView()) + ", new view: " + shortViewInfo(event.getNewView()) + ". " + details);
        }
    }
    updateDiscoveryLiteHistory();
}
Also used : ClusterView(org.apache.sling.discovery.ClusterView) Iterator(java.util.Iterator) InstanceDescription(org.apache.sling.discovery.InstanceDescription) InstancesDiff(org.apache.sling.discovery.commons.InstancesDiff)

Aggregations

Iterator (java.util.Iterator)1 ClusterView (org.apache.sling.discovery.ClusterView)1 InstanceDescription (org.apache.sling.discovery.InstanceDescription)1 InstancesDiff (org.apache.sling.discovery.commons.InstancesDiff)1