Search in sources :

Example 16 with TopologyEvent

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

the class ClusterTest method assertCountEvents.

private void assertCountEvents(ViewStateManagerImpl mgr, DummyListener l, TopologyEvent.Type... types) throws InterruptedException {
    waitForInflightEvents(mgr);
    assertEquals(types.length, l.countEvents());
    Iterator<TopologyEvent> it = l.getEvents().iterator();
    int i = 0;
    while (it.hasNext() && (i < types.length)) {
        TopologyEvent expectedEvent = it.next();
        Type gotType = types[i++];
        assertEquals(expectedEvent.getType(), gotType);
    }
    if (it.hasNext()) {
        StringBuffer additionalTypes = new StringBuffer();
        while (it.hasNext()) {
            additionalTypes.append(",");
            additionalTypes.append(it.next().getType());
        }
        fail("got more events than expected : " + additionalTypes);
    }
    if (i < types.length) {
        StringBuffer additionalTypes = new StringBuffer();
        while (i < types.length) {
            additionalTypes.append(",");
            additionalTypes.append(types[i++]);
        }
        fail("did not get all events, also expected : " + additionalTypes);
    }
}
Also used : Type(org.apache.sling.discovery.TopologyEvent.Type) TopologyEvent(org.apache.sling.discovery.TopologyEvent)

Example 17 with TopologyEvent

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

the class JobManagerConfiguration method doHandleTopologyEvent.

void doHandleTopologyEvent(final TopologyEvent event) {
    // check if there is a change of properties which doesn't affect us
    // but we need to use the new view !
    boolean stopProcessing = true;
    if (event.getType() == Type.PROPERTIES_CHANGED) {
        final Map<String, String> newAllInstances = TopologyCapabilities.getAllInstancesMap(event.getNewView());
        if (this.topologyCapabilities != null && this.topologyCapabilities.isSame(newAllInstances)) {
            logger.debug("No changes in capabilities - updating topology capabilities with new view");
            stopProcessing = false;
        }
    }
    final TopologyEvent.Type eventType = event.getType();
    if (eventType == Type.TOPOLOGY_CHANGING) {
        this.stopProcessing();
    } else if (eventType == Type.TOPOLOGY_INIT || event.getType() == Type.TOPOLOGY_CHANGED || event.getType() == Type.PROPERTIES_CHANGED) {
        if (stopProcessing) {
            this.stopProcessing();
        }
        this.startProcessing(eventType, new TopologyCapabilities(event.getNewView(), this));
    }
}
Also used : Type(org.apache.sling.discovery.TopologyEvent.Type) TopologyEvent(org.apache.sling.discovery.TopologyEvent)

Example 18 with TopologyEvent

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

use of org.apache.sling.discovery.TopologyEvent 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)

Example 20 with TopologyEvent

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

the class TestInitDelayingTopologyEventListener method createEvent.

private TopologyEvent createEvent(Type type) {
    TopologyView oldView = createView(false);
    TopologyView newView = createView(true);
    switch(type) {
        case TOPOLOGY_CHANGING:
            {
                return new TopologyEvent(type, oldView, null);
            }
        case PROPERTIES_CHANGED:
        case TOPOLOGY_CHANGED:
            {
                return new TopologyEvent(type, oldView, newView);
            }
        case TOPOLOGY_INIT:
            {
                return new TopologyEvent(type, null, newView);
            }
        default:
            {
                throw new IllegalArgumentException("unknown type: " + type);
            }
    }
}
Also used : TopologyEvent(org.apache.sling.discovery.TopologyEvent) TopologyView(org.apache.sling.discovery.TopologyView)

Aggregations

TopologyEvent (org.apache.sling.discovery.TopologyEvent)21 TopologyEventListener (org.apache.sling.discovery.TopologyEventListener)10 Test (org.junit.Test)10 TopologyView (org.apache.sling.discovery.TopologyView)7 ArrayList (java.util.ArrayList)5 Type (org.apache.sling.discovery.TopologyEvent.Type)4 DiscoveryService (org.apache.sling.discovery.DiscoveryService)3 InstanceDescription (org.apache.sling.discovery.InstanceDescription)2 InitDelayingTopologyEventListener (org.apache.sling.discovery.commons.InitDelayingTopologyEventListener)2 DummyTopologyView (org.apache.sling.discovery.commons.providers.DummyTopologyView)2 HashMap (java.util.HashMap)1 Random (java.util.Random)1 Semaphore (java.util.concurrent.Semaphore)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 ReentrantLock (java.util.concurrent.locks.ReentrantLock)1 Level (org.apache.log4j.Level)1 PersistenceException (org.apache.sling.api.resource.PersistenceException)1 ResourceResolver (org.apache.sling.api.resource.ResourceResolver)1 Scheduler (org.apache.sling.commons.scheduler.Scheduler)1 PropertyProvider (org.apache.sling.discovery.PropertyProvider)1