Search in sources :

Example 16 with InstanceDescription

use of org.apache.sling.discovery.InstanceDescription in project sling by apache.

the class TestInitDelayingTopologyEventListener method createView.

private TopologyView createView(boolean current) {
    final TopologyView view = Mockito.mock(TopologyView.class);
    Mockito.when(view.isCurrent()).thenReturn(current);
    final InstanceDescription local = Mockito.mock(InstanceDescription.class);
    Mockito.when(local.isLeader()).thenReturn(true);
    Mockito.when(local.isLocal()).thenReturn(true);
    Mockito.when(local.getSlingId()).thenReturn("id");
    Mockito.when(view.getLocalInstance()).thenReturn(local);
    final ClusterView localView = Mockito.mock(ClusterView.class);
    Mockito.when(localView.getId()).thenReturn("1");
    Mockito.when(localView.getInstances()).thenReturn(Collections.singletonList(local));
    Mockito.when(view.getClusterViews()).thenReturn(Collections.singleton(localView));
    Mockito.when(local.getClusterView()).thenReturn(localView);
    return view;
}
Also used : ClusterView(org.apache.sling.discovery.ClusterView) InstanceDescription(org.apache.sling.discovery.InstanceDescription) TopologyView(org.apache.sling.discovery.TopologyView)

Example 17 with InstanceDescription

use of org.apache.sling.discovery.InstanceDescription in project sling by apache.

the class DummyTopologyView method clone.

public static DummyTopologyView clone(final DummyTopologyView view) {
    final DummyTopologyView result = new DummyTopologyView(view.id);
    final Iterator<InstanceDescription> it = view.getInstances().iterator();
    Map<String, DefaultClusterView> clusters = new HashMap<String, DefaultClusterView>();
    while (it.hasNext()) {
        InstanceDescription id = it.next();
        String clusterId = id.getClusterView().getId();
        DefaultClusterView cluster = clusters.get(clusterId);
        if (cluster == null) {
            cluster = new DefaultClusterView(clusterId);
            clusters.put(clusterId, cluster);
        }
        DefaultInstanceDescription clone = clone(cluster, id);
        result.addInstanceDescription(clone);
    }
    if (!view.isCurrent()) {
        result.setNotCurrent();
    }
    return result;
}
Also used : HashMap(java.util.HashMap) InstanceDescription(org.apache.sling.discovery.InstanceDescription)

Example 18 with InstanceDescription

use of org.apache.sling.discovery.InstanceDescription in project sling by apache.

the class ViewStateManagerImpl method onlyDiffersInProperties.

protected boolean onlyDiffersInProperties(BaseTopologyView newView) {
    if (previousView == null) {
        return false;
    }
    if (newView == null) {
        throw new IllegalArgumentException("newView must not be null");
    }
    String previousSyncTokenId = null;
    String newSyncTokenId = null;
    try {
        previousSyncTokenId = previousView.getLocalClusterSyncTokenId();
    } catch (IllegalStateException re) {
        previousSyncTokenId = null;
    }
    try {
        newSyncTokenId = newView.getLocalClusterSyncTokenId();
    } catch (IllegalStateException re) {
        newSyncTokenId = null;
    }
    if ((previousSyncTokenId == null && newSyncTokenId != null) || (newSyncTokenId == null && previousSyncTokenId != null) || (previousSyncTokenId != null && !previousSyncTokenId.equals(newSyncTokenId))) {
        return false;
    }
    if (previousView.getInstances().size() != newView.getInstances().size()) {
        return false;
    }
    if (previousView.equals(newView)) {
        return false;
    }
    Set<String> newIds = new HashSet<String>();
    for (InstanceDescription newInstance : newView.getInstances()) {
        newIds.add(newInstance.getSlingId());
    }
    for (InstanceDescription oldInstance : previousView.getInstances()) {
        InstanceDescription newInstance = newView.getInstance(oldInstance.getSlingId());
        if (newInstance == null) {
            return false;
        }
        if (oldInstance.isLeader() != newInstance.isLeader()) {
            return false;
        }
        if (!oldInstance.getClusterView().getId().equals(newInstance.getClusterView().getId())) {
            return false;
        }
    }
    return true;
}
Also used : InstanceDescription(org.apache.sling.discovery.InstanceDescription) HashSet(java.util.HashSet)

Example 19 with InstanceDescription

use of org.apache.sling.discovery.InstanceDescription in project sling by apache.

the class TopologyWebConsolePlugin method printCluster.

/**
     * Render a particular cluster
     */
private void printCluster(final PrintWriter pw, final ClusterView renderCluster, final ClusterView localCluster) {
    final Collection<Announcement> announcements = announcementRegistry.listAnnouncementsInSameCluster(localCluster);
    for (final InstanceDescription instanceDescription : renderCluster.getInstances()) {
        final boolean inLocalCluster = renderCluster == localCluster;
        Announcement parentAnnouncement = null;
        for (Iterator<Announcement> it2 = announcements.iterator(); it2.hasNext(); ) {
            Announcement announcement = it2.next();
            for (Iterator<InstanceDescription> it3 = announcement.listInstances().iterator(); it3.hasNext(); ) {
                InstanceDescription announcedInstance = it3.next();
                if (announcedInstance.getSlingId().equals(instanceDescription.getSlingId())) {
                    parentAnnouncement = announcement;
                    break;
                }
            }
        }
        final boolean isLocal = instanceDescription.isLocal();
        final String slingId = instanceDescription.getSlingId();
        pw.print("Sling ID : ");
        pw.print(slingId);
        pw.println();
        pw.print("Cluster View ID : ");
        pw.print(instanceDescription.getClusterView() == null ? "null" : instanceDescription.getClusterView().getId());
        pw.println();
        pw.print("Local instance : ");
        pw.print(isLocal);
        pw.println();
        pw.print("Leader instance : ");
        pw.print(instanceDescription.isLeader());
        pw.println();
        pw.print("In local cluster : ");
        if (inLocalCluster) {
            pw.print("local");
        } else {
            pw.print("remote");
        }
        pw.println();
        pw.print("Announced by : ");
        if (inLocalCluster) {
            pw.print("n/a");
        } else {
            if (parentAnnouncement != null) {
                pw.print(parentAnnouncement.getOwnerId());
            } else {
                pw.print("(changing)");
            }
        }
        pw.println();
        pw.println("Properties:");
        for (final Map.Entry<String, String> entry : instanceDescription.getProperties().entrySet()) {
            pw.print("- ");
            pw.print(entry.getKey());
            pw.print(" : ");
            pw.print(entry.getValue());
            pw.println();
        }
        pw.println();
        pw.println();
    }
}
Also used : Announcement(org.apache.sling.discovery.base.connectors.announcement.Announcement) CachedAnnouncement(org.apache.sling.discovery.base.connectors.announcement.CachedAnnouncement) InstanceDescription(org.apache.sling.discovery.InstanceDescription) Map(java.util.Map)

Example 20 with InstanceDescription

use of org.apache.sling.discovery.InstanceDescription in project sling by apache.

the class TestViewStateManager method testChangedPropertiesChanged.

@Test
public void testChangedPropertiesChanged() throws Exception {
    final DummyListener listener = new DummyListener();
    mgr.installMinEventDelayHandler(new DiscoveryService() {

        @Override
        public TopologyView getTopology() {
            throw new IllegalStateException("not yet impl");
        }
    }, new DummyScheduler(), 1);
    mgr.handleActivated();
    TestHelper.assertNoEvents(listener);
    mgr.bind(listener);
    TestHelper.assertNoEvents(listener);
    mgr.handleChanging();
    TestHelper.assertNoEvents(listener);
    final BaseTopologyView view1 = new DummyTopologyView().addInstance();
    InstanceDescription instance1 = view1.getInstances().iterator().next();
    ClusterView cluster1 = instance1.getClusterView();
    mgr.handleNewView(view1);
    assertEvents(listener, EventHelper.newInitEvent(view1));
    DefaultClusterView cluster2 = new DefaultClusterView(new String(cluster1.getId()));
    final BaseTopologyView view2 = new DummyTopologyView(view1.getLocalClusterSyncTokenId()).addInstance(instance1.getSlingId(), cluster2, instance1.isLeader(), instance1.isLocal());
    DefaultInstanceDescription instance2 = (DefaultInstanceDescription) view2.getLocalInstance();
    instance2.setProperty("foo", "bar");
    mgr.handleNewView(view2);
    assertEvents(listener, EventHelper.newPropertiesChangedEvent(view1, view2));
}
Also used : ClusterView(org.apache.sling.discovery.ClusterView) DefaultClusterView(org.apache.sling.discovery.commons.providers.DefaultClusterView) DummyTopologyView(org.apache.sling.discovery.commons.providers.DummyTopologyView) DefaultInstanceDescription(org.apache.sling.discovery.commons.providers.DefaultInstanceDescription) DefaultClusterView(org.apache.sling.discovery.commons.providers.DefaultClusterView) DefaultInstanceDescription(org.apache.sling.discovery.commons.providers.DefaultInstanceDescription) InstanceDescription(org.apache.sling.discovery.InstanceDescription) DiscoveryService(org.apache.sling.discovery.DiscoveryService) BaseTopologyView(org.apache.sling.discovery.commons.providers.BaseTopologyView) TopologyView(org.apache.sling.discovery.TopologyView) DummyTopologyView(org.apache.sling.discovery.commons.providers.DummyTopologyView) BaseTopologyView(org.apache.sling.discovery.commons.providers.BaseTopologyView) Test(org.junit.Test)

Aggregations

InstanceDescription (org.apache.sling.discovery.InstanceDescription)59 ClusterView (org.apache.sling.discovery.ClusterView)16 DefaultInstanceDescription (org.apache.sling.discovery.commons.providers.DefaultInstanceDescription)11 Map (java.util.Map)10 PersistenceException (org.apache.sling.api.resource.PersistenceException)10 TopologyView (org.apache.sling.discovery.TopologyView)9 HashMap (java.util.HashMap)8 LoginException (org.apache.sling.api.resource.LoginException)8 ResourceResolver (org.apache.sling.api.resource.ResourceResolver)8 Test (org.junit.Test)8 Announcement (org.apache.sling.discovery.base.connectors.announcement.Announcement)7 UndefinedClusterViewException (org.apache.sling.discovery.base.commons.UndefinedClusterViewException)6 DefaultClusterView (org.apache.sling.discovery.commons.providers.DefaultClusterView)6 HashSet (java.util.HashSet)5 Resource (org.apache.sling.api.resource.Resource)5 ValueMap (org.apache.sling.api.resource.ValueMap)5 LocalClusterView (org.apache.sling.discovery.commons.providers.spi.LocalClusterView)5 ArrayList (java.util.ArrayList)4 Iterator (java.util.Iterator)4 LinkedList (java.util.LinkedList)4