Search in sources :

Example 1 with ClusterSyncService

use of org.apache.sling.discovery.commons.providers.spi.ClusterSyncService in project sling by apache.

the class ClusterTest method newMgr.

private ViewStateManagerImpl newMgr() {
    ViewStateManagerImpl mgr = new ViewStateManagerImpl(new ReentrantLock(), new ClusterSyncService() {

        public void sync(BaseTopologyView view, Runnable callback) {
            callback.run();
        }

        @Override
        public void cancelSync() {
        // nothing to cancel, we're auto-run
        }
    });
    mgrList.add(mgr);
    return mgr;
}
Also used : ReentrantLock(java.util.concurrent.locks.ReentrantLock) ClusterSyncService(org.apache.sling.discovery.commons.providers.spi.ClusterSyncService) ViewStateManagerImpl(org.apache.sling.discovery.commons.providers.base.ViewStateManagerImpl) BaseTopologyView(org.apache.sling.discovery.commons.providers.BaseTopologyView)

Example 2 with ClusterSyncService

use of org.apache.sling.discovery.commons.providers.spi.ClusterSyncService in project sling by apache.

the class TestMinEventDelayHandler method setup.

@Before
public void setup() throws Exception {
    mgr = new ViewStateManagerImpl(new ReentrantLock(), new ClusterSyncService() {

        @Override
        public void sync(BaseTopologyView view, Runnable callback) {
            callback.run();
        }

        @Override
        public void cancelSync() {
        // nothing to cancel, we're auto-run
        }
    });
    // I want randomness yes, but deterministic, for some methods at least
    defaultRandom = new Random(1234123412);
    scheduler = new DummyScheduler();
    sds = new DummyDiscoveryService();
    mgr.installMinEventDelayHandler(sds, scheduler, 1);
    final org.apache.log4j.Logger discoveryLogger = RootLogger.getLogger("org.apache.sling.discovery");
    logLevel = discoveryLogger.getLevel();
    // changed from Level.DEBUG
    discoveryLogger.setLevel(Level.INFO);
}
Also used : ReentrantLock(java.util.concurrent.locks.ReentrantLock) ClusterSyncService(org.apache.sling.discovery.commons.providers.spi.ClusterSyncService) Random(java.util.Random) ViewStateManagerImpl(org.apache.sling.discovery.commons.providers.base.ViewStateManagerImpl) BaseTopologyView(org.apache.sling.discovery.commons.providers.BaseTopologyView) Before(org.junit.Before)

Example 3 with ClusterSyncService

use of org.apache.sling.discovery.commons.providers.spi.ClusterSyncService in project sling by apache.

the class TestViewStateManager method setup.

@Before
public void setup() throws Exception {
    mgr = new ViewStateManagerImpl(new ReentrantLock(), new ClusterSyncService() {

        @Override
        public void sync(BaseTopologyView view, Runnable callback) {
            callback.run();
        }

        @Override
        public void cancelSync() {
        // nothing to cancel, we're auto-run
        }
    });
    // I want randomness yes, but deterministic, for some methods at least
    defaultRandom = new Random(1234123412);
    final org.apache.log4j.Logger discoveryLogger = RootLogger.getLogger("org.apache.sling.discovery");
    logLevel = discoveryLogger.getLevel();
    discoveryLogger.setLevel(Level.INFO);
}
Also used : ReentrantLock(java.util.concurrent.locks.ReentrantLock) ClusterSyncService(org.apache.sling.discovery.commons.providers.spi.ClusterSyncService) Random(java.util.Random) BaseTopologyView(org.apache.sling.discovery.commons.providers.BaseTopologyView) Before(org.junit.Before)

Example 4 with ClusterSyncService

use of org.apache.sling.discovery.commons.providers.spi.ClusterSyncService in project sling by apache.

the class DiscoveryServiceImpl method activate.

/**
     * Activate this service
     */
@Activate
protected void activate(final BundleContext bundleContext) {
    logger.debug("DiscoveryServiceImpl activating...");
    if (settingsService == null) {
        throw new IllegalStateException("settingsService not found");
    }
    if (heartbeatHandler == null) {
        throw new IllegalStateException("heartbeatHandler not found");
    }
    slingId = settingsService.getSlingId();
    final ClusterSyncService clusterSyncService;
    if (!config.useSyncTokenService()) {
        logger.info("activate: useSyncTokenService is configured to false. Using pass-through cluster-sync-service.");
        clusterSyncService = PASS_THROUGH_CLUSTER_SYNC_SERVICE;
    } else if (syncTokenService == null) {
        logger.warn("activate: useSyncTokenService is configured to true but there's no SyncTokenService! Using pass-through cluster-sync-service instead.");
        clusterSyncService = syncTokenService;
    } else {
        logger.info("activate: useSyncTokenService is configured to true, using the available SyncTokenService: " + syncTokenService);
        clusterSyncService = syncTokenService;
    }
    viewStateManager = ViewStateManagerFactory.newViewStateManager(viewStateManagerLock, clusterSyncService);
    if (config.getMinEventDelay() > 0) {
        viewStateManager.installMinEventDelayHandler(this, scheduler, config.getMinEventDelay());
    }
    final String isolatedClusterId = UUID.randomUUID().toString();
    {
        // create a pre-voting/isolated topologyView which would be used
        // until the first voting has finished.
        // this way for the single-instance case the clusterId can
        // remain the same between a getTopology() that is invoked before
        // the first TOPOLOGY_INIT and afterwards
        DefaultClusterView isolatedCluster = new DefaultClusterView(isolatedClusterId);
        Map<String, String> emptyProperties = new HashMap<String, String>();
        DefaultInstanceDescription isolatedInstance = new DefaultInstanceDescription(isolatedCluster, true, true, slingId, emptyProperties);
        Collection<InstanceDescription> col = new ArrayList<InstanceDescription>();
        col.add(isolatedInstance);
        final DefaultTopologyView topology = new DefaultTopologyView();
        topology.addInstances(col);
        topology.setNotCurrent();
        setOldView(topology);
    }
    setOldView((DefaultTopologyView) getTopology());
    getOldView().setNotCurrent();
    // make sure the first heartbeat is issued as soon as possible - which
    // is right after this service starts. since the two (discoveryservice
    // and heartbeatHandler need to know each other, the discoveryservice
    // is passed on to the heartbeatHandler in this initialize call).
    heartbeatHandler.initialize(this, isolatedClusterId);
    viewStateManagerLock.lock();
    try {
        viewStateManager.handleActivated();
        doUpdateProperties();
        DefaultTopologyView newView = (DefaultTopologyView) getTopology();
        if (newView.isCurrent()) {
            viewStateManager.handleNewView(newView);
        } else {
            // SLING-3750: just issue a log.info about the delaying
            logger.info("activate: this instance is in isolated mode and must yet finish voting before it can send out TOPOLOGY_INIT.");
        }
        activated = true;
        setOldView(newView);
        // bind them to the viewstatemanager too
        for (TopologyEventListener listener : pendingListeners) {
            viewStateManager.bind(listener);
        }
        pendingListeners.clear();
        viewStateManager.bind(changePropagationListener);
    } finally {
        if (viewStateManagerLock != null) {
            viewStateManagerLock.unlock();
        }
    }
    URL[] topologyConnectorURLs = config.getTopologyConnectorURLs();
    if (topologyConnectorURLs != null) {
        for (int i = 0; i < topologyConnectorURLs.length; i++) {
            final URL aURL = topologyConnectorURLs[i];
            if (aURL != null) {
                try {
                    logger.info("activate: registering outgoing topology connector to " + aURL);
                    connectorRegistry.registerOutgoingConnector(clusterViewService, aURL);
                } catch (final Exception e) {
                    logger.info("activate: could not register url: " + aURL + " due to: " + e, e);
                }
            }
        }
    }
    registerMBean(bundleContext);
    logger.debug("DiscoveryServiceImpl activated.");
}
Also used : DefaultInstanceDescription(org.apache.sling.discovery.commons.providers.DefaultInstanceDescription) URL(java.net.URL) PersistenceException(org.apache.sling.api.resource.PersistenceException) LoginException(org.apache.sling.api.resource.LoginException) UndefinedClusterViewException(org.apache.sling.discovery.base.commons.UndefinedClusterViewException) DefaultTopologyView(org.apache.sling.discovery.base.commons.DefaultTopologyView) ClusterSyncService(org.apache.sling.discovery.commons.providers.spi.ClusterSyncService) DefaultClusterView(org.apache.sling.discovery.commons.providers.DefaultClusterView) Collection(java.util.Collection) DefaultInstanceDescription(org.apache.sling.discovery.commons.providers.DefaultInstanceDescription) InstanceDescription(org.apache.sling.discovery.InstanceDescription) Map(java.util.Map) ModifiableValueMap(org.apache.sling.api.resource.ModifiableValueMap) HashMap(java.util.HashMap) TopologyEventListener(org.apache.sling.discovery.TopologyEventListener) Activate(org.apache.felix.scr.annotations.Activate)

Example 5 with ClusterSyncService

use of org.apache.sling.discovery.commons.providers.spi.ClusterSyncService in project sling by apache.

the class TestViewStateManager method testCancelSync.

@Test
public void testCancelSync() throws Exception {
    final List<Runnable> syncCallbacks = new LinkedList<Runnable>();
    mgr = new ViewStateManagerImpl(new ReentrantLock(), new ClusterSyncService() {

        @Override
        public void sync(BaseTopologyView view, Runnable callback) {
            synchronized (syncCallbacks) {
                syncCallbacks.add(callback);
            }
        }

        @Override
        public void cancelSync() {
            synchronized (syncCallbacks) {
                syncCallbacks.clear();
            }
        }
    });
    mgr.handleActivated();
    final DummyListener listener = new DummyListener();
    mgr.bind(listener);
    mgr.handleChanging();
    final BaseTopologyView view = new DummyTopologyView().addInstance();
    mgr.handleNewView(view);
    assertEquals(0, mgr.waitForAsyncEvents(1000));
    TestHelper.assertNoEvents(listener);
    synchronized (syncCallbacks) {
        assertEquals(1, syncCallbacks.size());
    }
    String id1 = UUID.randomUUID().toString();
    String id2 = UUID.randomUUID().toString();
    final BaseTopologyView view2 = TestHelper.newView(true, id1, id1, id1, id2);
    mgr.handleNewView(view2);
    assertEquals(0, mgr.waitForAsyncEvents(1000));
    TestHelper.assertNoEvents(listener);
    synchronized (syncCallbacks) {
        assertEquals(1, syncCallbacks.size());
        syncCallbacks.get(0).run();
        syncCallbacks.clear();
    }
    assertEquals(0, mgr.waitForAsyncEvents(1000));
    assertEvents(listener, EventHelper.newInitEvent(view2));
}
Also used : ReentrantLock(java.util.concurrent.locks.ReentrantLock) ClusterSyncService(org.apache.sling.discovery.commons.providers.spi.ClusterSyncService) DummyTopologyView(org.apache.sling.discovery.commons.providers.DummyTopologyView) LinkedList(java.util.LinkedList) BaseTopologyView(org.apache.sling.discovery.commons.providers.BaseTopologyView) Test(org.junit.Test)

Aggregations

ClusterSyncService (org.apache.sling.discovery.commons.providers.spi.ClusterSyncService)7 ReentrantLock (java.util.concurrent.locks.ReentrantLock)4 BaseTopologyView (org.apache.sling.discovery.commons.providers.BaseTopologyView)4 URL (java.net.URL)2 Collection (java.util.Collection)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 Random (java.util.Random)2 Activate (org.apache.felix.scr.annotations.Activate)2 LoginException (org.apache.sling.api.resource.LoginException)2 ModifiableValueMap (org.apache.sling.api.resource.ModifiableValueMap)2 PersistenceException (org.apache.sling.api.resource.PersistenceException)2 InstanceDescription (org.apache.sling.discovery.InstanceDescription)2 TopologyEventListener (org.apache.sling.discovery.TopologyEventListener)2 DefaultTopologyView (org.apache.sling.discovery.base.commons.DefaultTopologyView)2 DefaultClusterView (org.apache.sling.discovery.commons.providers.DefaultClusterView)2 DefaultInstanceDescription (org.apache.sling.discovery.commons.providers.DefaultInstanceDescription)2 ViewStateManagerImpl (org.apache.sling.discovery.commons.providers.base.ViewStateManagerImpl)2 Before (org.junit.Before)2 LinkedList (java.util.LinkedList)1