Search in sources :

Example 11 with TopologyEventListener

use of org.apache.sling.discovery.TopologyEventListener 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 12 with TopologyEventListener

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

the class JobManagerConfiguration method activate.

/**
     * Activate this component.
     * @param props Configuration properties
     * @param config Configuration properties
     * @throws RuntimeException If the default paths can't be created
     */
@Activate
protected void activate(final Map<String, Object> props, final Config config) {
    this.update(props, config);
    this.jobsBasePathWithSlash = PropertiesUtil.toString(props.get(PROPERTY_REPOSITORY_PATH), DEFAULT_REPOSITORY_PATH) + '/';
    // create initial resources
    this.assignedJobsPath = this.jobsBasePathWithSlash + "assigned";
    this.unassignedJobsPath = this.jobsBasePathWithSlash + "unassigned";
    this.localJobsPath = this.assignedJobsPath.concat("/").concat(Environment.APPLICATION_ID);
    this.localJobsPathWithSlash = this.localJobsPath.concat("/");
    this.previousVersionAnonPath = this.jobsBasePathWithSlash + "anon";
    this.previousVersionIdentifiedPath = this.jobsBasePathWithSlash + "identified";
    this.storedCancelledJobsPath = this.jobsBasePathWithSlash + "cancelled";
    this.storedSuccessfulJobsPath = this.jobsBasePathWithSlash + "finished";
    this.scheduledJobsPath = PropertiesUtil.toString(props.get(PROPERTY_SCHEDULED_JOBS_PATH), DEFAULT_SCHEDULED_JOBS_PATH);
    this.scheduledJobsPathWithSlash = this.scheduledJobsPath + "/";
    // create initial resources
    final ResourceResolver resolver = this.createResourceResolver();
    try {
        ResourceHelper.getOrCreateBasePath(resolver, this.getLocalJobsPath());
        ResourceHelper.getOrCreateBasePath(resolver, this.getUnassignedJobsPath());
    } catch (final PersistenceException pe) {
        logger.error("Unable to create default paths: " + pe.getMessage(), pe);
        throw new RuntimeException(pe);
    } finally {
        resolver.close();
    }
    this.active.set(true);
    // SLING-5560 : use an InitDelayingTopologyEventListener
    if (this.startupDelay > 0) {
        logger.debug("activate: job manager will start in {} sec. ({})", this.startupDelay, config.startup_delay());
        this.startupDelayListener = new InitDelayingTopologyEventListener(startupDelay, new TopologyEventListener() {

            @Override
            public void handleTopologyEvent(TopologyEvent event) {
                doHandleTopologyEvent(event);
            }
        }, logger);
    } else {
        logger.debug("activate: job manager will start without delay. ({}:{})", config.startup_delay(), this.startupDelay);
    }
}
Also used : TopologyEvent(org.apache.sling.discovery.TopologyEvent) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) PersistenceException(org.apache.sling.api.resource.PersistenceException) InitDelayingTopologyEventListener(org.apache.sling.discovery.commons.InitDelayingTopologyEventListener) InitDelayingTopologyEventListener(org.apache.sling.discovery.commons.InitDelayingTopologyEventListener) TopologyEventListener(org.apache.sling.discovery.TopologyEventListener) Activate(org.osgi.service.component.annotations.Activate)

Example 13 with TopologyEventListener

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

the class JobManagerConfigurationTest method testTopologyChange.

@Test
public void testTopologyChange() throws Exception {
    // mock scheduler
    final ChangeListener ccl = new ChangeListener();
    // add change listener and verify
    ccl.init(1);
    final JobManagerConfiguration config = new JobManagerConfiguration();
    ((AtomicBoolean) TestUtil.getFieldValue(config, "active")).set(true);
    InitDelayingTopologyEventListener startupDelayListener = new InitDelayingTopologyEventListener(1, new TopologyEventListener() {

        @Override
        public void handleTopologyEvent(TopologyEvent event) {
            config.doHandleTopologyEvent(event);
        }
    });
    TestUtil.setFieldValue(config, "startupDelayListener", startupDelayListener);
    config.addListener(ccl);
    ccl.await();
    assertEquals(1, ccl.events.size());
    assertFalse(ccl.events.get(0));
    // create init view
    ccl.init(1);
    final TopologyView initView = createView();
    final TopologyEvent init = new TopologyEvent(TopologyEvent.Type.TOPOLOGY_INIT, null, initView);
    config.handleTopologyEvent(init);
    ccl.await();
    assertEquals(1, ccl.events.size());
    assertTrue(ccl.events.get(0));
    // change view, followed by change props
    ccl.init(2);
    final TopologyView view2 = createView();
    Mockito.when(initView.isCurrent()).thenReturn(false);
    final TopologyEvent change1 = new TopologyEvent(TopologyEvent.Type.TOPOLOGY_CHANGED, initView, view2);
    final TopologyView view3 = createView();
    final TopologyEvent change2 = new TopologyEvent(TopologyEvent.Type.PROPERTIES_CHANGED, view2, view3);
    config.handleTopologyEvent(change1);
    Mockito.when(view2.isCurrent()).thenReturn(false);
    config.handleTopologyEvent(change2);
    ccl.await();
    assertEquals(2, ccl.events.size());
    assertFalse(ccl.events.get(0));
    assertTrue(ccl.events.get(1));
    // we wait another 4 secs to see if there is no another event
    Thread.sleep(4000);
    assertEquals(2, ccl.events.size());
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) TopologyEvent(org.apache.sling.discovery.TopologyEvent) InitDelayingTopologyEventListener(org.apache.sling.discovery.commons.InitDelayingTopologyEventListener) TopologyView(org.apache.sling.discovery.TopologyView) InitDelayingTopologyEventListener(org.apache.sling.discovery.commons.InitDelayingTopologyEventListener) TopologyEventListener(org.apache.sling.discovery.TopologyEventListener) Test(org.junit.Test)

Aggregations

TopologyEventListener (org.apache.sling.discovery.TopologyEventListener)13 TopologyEvent (org.apache.sling.discovery.TopologyEvent)10 Test (org.junit.Test)6 ArrayList (java.util.ArrayList)5 HashMap (java.util.HashMap)3 PersistenceException (org.apache.sling.api.resource.PersistenceException)3 DiscoveryService (org.apache.sling.discovery.DiscoveryService)3 InstanceDescription (org.apache.sling.discovery.InstanceDescription)3 TopologyView (org.apache.sling.discovery.TopologyView)3 URL (java.net.URL)2 Collection (java.util.Collection)2 Map (java.util.Map)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 DefaultTopologyView (org.apache.sling.discovery.base.commons.DefaultTopologyView)2 InitDelayingTopologyEventListener (org.apache.sling.discovery.commons.InitDelayingTopologyEventListener)2 DefaultClusterView (org.apache.sling.discovery.commons.providers.DefaultClusterView)2 DefaultInstanceDescription (org.apache.sling.discovery.commons.providers.DefaultInstanceDescription)2 ClusterSyncService (org.apache.sling.discovery.commons.providers.spi.ClusterSyncService)2