Search in sources :

Example 26 with ClusterId

use of net.dempsy.config.ClusterId in project Dempsy by Dempsy.

the class TestLeaderAndSubscriber method testLeaderWithSubscriber.

@Test
public void testLeaderWithSubscriber() throws Exception {
    final ClusterId cid = setTestName("testLeaderWithSubscriber");
    final Utils<ContainerAddress> utils = new Utils<>(makeInfra(session, sched), cid.clusterName, new ContainerAddress(new DummyNodeAddress(), new int[] { 0 }));
    final AtomicBoolean isRunning = new AtomicBoolean(true);
    try {
        final Subscriber<ContainerAddress> s = new Subscriber<>(utils, infra, isRunning, (l, m) -> {
        }, 256);
        s.process();
        final Leader<ContainerAddress> l = new Leader<>(utils, 256, 1, infra, isRunning, ContainerAddress[]::new);
        l.process();
        assertTrue(poll(o -> l.imIt()));
        // wait until it owns 1
        assertTrue(poll(o -> s.isReady()));
        // s should now own all shards.
        for (int i = 0; i < 256; i++) assertTrue(s.doIOwnShard(i));
    } finally {
        isRunning.set(false);
    }
}
Also used : Logger(org.slf4j.Logger) ClusterInfoSession(net.dempsy.cluster.ClusterInfoSession) ContainerAddress(net.dempsy.router.RoutingStrategy.ContainerAddress) ClusterInfoSessionFactory(net.dempsy.cluster.ClusterInfoSessionFactory) NodeAddress(net.dempsy.transport.NodeAddress) LoggerFactory(org.slf4j.LoggerFactory) Assert.assertTrue(org.junit.Assert.assertTrue) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Test(org.junit.Test) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) Consumer(java.util.function.Consumer) AtomicLong(java.util.concurrent.atomic.AtomicLong) List(java.util.List) ConditionPoll.poll(net.dempsy.utils.test.ConditionPoll.poll) BaseRouterTestWithSession(net.dempsy.router.BaseRouterTestWithSession) Assert.assertEquals(org.junit.Assert.assertEquals) ClusterId(net.dempsy.config.ClusterId) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ClusterId(net.dempsy.config.ClusterId) ContainerAddress(net.dempsy.router.RoutingStrategy.ContainerAddress) Test(org.junit.Test)

Example 27 with ClusterId

use of net.dempsy.config.ClusterId in project Dempsy by Dempsy.

the class TestLeaderAndSubscriber method testMultiLeader.

@Test
public void testMultiLeader() throws Exception {
    final int NUM_THREADS = 10;
    final ClusterId cid = setTestName("testMultiLeader");
    final AtomicBoolean isRunning = new AtomicBoolean(true);
    final AtomicBoolean go = new AtomicBoolean(false);
    final List<ListenerHolder> allHolders = new ArrayList<>();
    try {
        final List<ListenerHolder> holders = new ArrayList<>();
        for (int i = 0; i < NUM_THREADS; i++) {
            final ClusterInfoSession session = sessFact.createSession();
            final Utils<ContainerAddress> utils = new Utils<>(makeInfra(session, sched), cid.clusterName, new ContainerAddress(new DummyNodeAddress(), new int[] { 0 }));
            final Leader<ContainerAddress> l = new Leader<>(utils, 256, 1, infra, isRunning, ContainerAddress[]::new);
            final Thread t = new Thread(() -> {
                // wait for it.
                while (!go.get()) {
                    if (!isRunning.get())
                        return;
                    Thread.yield();
                }
                l.process();
            }, "testMultiLeader-" + i);
            holders.add(new ListenerHolder(session, l, t));
            t.start();
        }
        // all threads running.
        Thread.sleep(50);
        // we're going to modify holders
        allHolders.addAll(holders);
        // let 'em go.
        go.set(true);
        // once they're all ready, one should be 'it'
        assertTrue(poll(o -> holders.stream().map(h -> h.l).filter(l -> l.isReady()).count() == NUM_THREADS));
        assertEquals(1, holders.stream().map(h -> h.l).filter(l -> l.imIt()).count());
        // find the leader and kill it.
        final ListenerHolder h = holders.stream().filter(l -> l.l.imIt()).findFirst().get();
        disruptor.accept(h.session);
        holders.remove(h);
        assertTrue(poll(o -> holders.stream().filter(l -> l.l.imIt()).count() == 1L));
        Thread.sleep(50);
        assertEquals(1, holders.stream().filter(l -> l.l.imIt()).count());
        isRunning.set(false);
        assertTrue(poll(o -> holders.stream().filter(l -> l.t.isAlive()).count() == 0));
    } finally {
        allHolders.forEach(h -> h.session.close());
        isRunning.set(false);
    }
}
Also used : Logger(org.slf4j.Logger) ClusterInfoSession(net.dempsy.cluster.ClusterInfoSession) ContainerAddress(net.dempsy.router.RoutingStrategy.ContainerAddress) ClusterInfoSessionFactory(net.dempsy.cluster.ClusterInfoSessionFactory) NodeAddress(net.dempsy.transport.NodeAddress) LoggerFactory(org.slf4j.LoggerFactory) Assert.assertTrue(org.junit.Assert.assertTrue) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Test(org.junit.Test) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) Consumer(java.util.function.Consumer) AtomicLong(java.util.concurrent.atomic.AtomicLong) List(java.util.List) ConditionPoll.poll(net.dempsy.utils.test.ConditionPoll.poll) BaseRouterTestWithSession(net.dempsy.router.BaseRouterTestWithSession) Assert.assertEquals(org.junit.Assert.assertEquals) ClusterId(net.dempsy.config.ClusterId) ClusterId(net.dempsy.config.ClusterId) ArrayList(java.util.ArrayList) ContainerAddress(net.dempsy.router.RoutingStrategy.ContainerAddress) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ClusterInfoSession(net.dempsy.cluster.ClusterInfoSession) Test(org.junit.Test)

Example 28 with ClusterId

use of net.dempsy.config.ClusterId in project Dempsy by Dempsy.

the class BaseRouterTestWithSession method setTestName.

protected ClusterId setTestName(final String testName) {
    final ClusterId cid = clusterId(testName);
    this.testName = cid.applicationName;
    return cid;
}
Also used : ClusterId(net.dempsy.config.ClusterId)

Example 29 with ClusterId

use of net.dempsy.config.ClusterId in project Dempsy by Dempsy.

the class TestManagedRoutingStrategy method testInboundSimpleHappyPathRegister.

@Test
public void testInboundSimpleHappyPathRegister() throws Exception {
    final int numShardsToExpect = Integer.parseInt(Utils.DEFAULT_TOTAL_SHARDS);
    final Manager<RoutingStrategy.Inbound> manager = new Manager<>(RoutingStrategy.Inbound.class);
    try (final RoutingStrategy.Inbound ib = manager.getAssociatedInstance(ManagedInbound.class.getPackage().getName())) {
        final ClusterId clusterId = new ClusterId("test", "test");
        final Utils<ContainerAddress> msutils = new Utils<>(infra, clusterId.clusterName, new ContainerAddress(new DummyNodeAddress("testInboundSimpleHappyPathRegister"), 0));
        assertNotNull(ib);
        assertTrue(ManagedInbound.class.isAssignableFrom(ib.getClass()));
        ib.setContainerDetails(clusterId, new ContainerAddress(new DummyNodeAddress("testInboundSimpleHappyPathRegister"), 0), (l, m) -> {
        });
        ib.start(infra);
        assertTrue(waitForShards(session, msutils, numShardsToExpect));
    }
}
Also used : ClusterId(net.dempsy.config.ClusterId) Utils(net.dempsy.router.shardutils.Utils) RoutingStrategy(net.dempsy.router.RoutingStrategy) RoutingStrategyManager(net.dempsy.router.RoutingStrategyManager) Manager(net.dempsy.Manager) ContainerAddress(net.dempsy.router.RoutingStrategy.ContainerAddress) Test(org.junit.Test)

Example 30 with ClusterId

use of net.dempsy.config.ClusterId in project Dempsy by Dempsy.

the class TestInstanceManager method setupContainer.

@SuppressWarnings("resource")
public LockingContainer setupContainer(final MessageProcessorLifecycle<?> prototype) throws ContainerException {
    dispatcher = new DummyDispatcher();
    statsCollector = new BasicClusterStatsCollector();
    nodeStats = new BasicNodeStatsCollector();
    container = (LockingContainer) new LockingContainer().setMessageProcessor(prototype).setClusterId(new ClusterId("test", "test"));
    container.setDispatcher(dispatcher);
    container.setInbound(new DummyInbound());
    tm = new DefaultThreadingModel(TestInstanceManager.class.getName());
    tm.start(TestInstanceManager.class.getName());
    container.start(new TestInfrastructure(tm) {

        @Override
        public BasicClusterStatsCollector getClusterStatsCollector(final ClusterId clusterId) {
            return statsCollector;
        }

        @Override
        public NodeStatsCollector getNodeStatsCollector() {
            return nodeStats;
        }
    });
    return container;
}
Also used : DummyInbound(net.dempsy.container.mocks.DummyInbound) BasicNodeStatsCollector(net.dempsy.monitoring.basic.BasicNodeStatsCollector) NodeStatsCollector(net.dempsy.monitoring.NodeStatsCollector) TestInfrastructure(net.dempsy.util.TestInfrastructure) ClusterId(net.dempsy.config.ClusterId) BasicClusterStatsCollector(net.dempsy.monitoring.basic.BasicClusterStatsCollector) DefaultThreadingModel(net.dempsy.threading.DefaultThreadingModel) BasicNodeStatsCollector(net.dempsy.monitoring.basic.BasicNodeStatsCollector)

Aggregations

ClusterId (net.dempsy.config.ClusterId)30 Test (org.junit.Test)24 ContainerAddress (net.dempsy.router.RoutingStrategy.ContainerAddress)16 List (java.util.List)15 ConditionPoll.poll (net.dempsy.utils.test.ConditionPoll.poll)15 Assert.assertEquals (org.junit.Assert.assertEquals)15 Assert.assertTrue (org.junit.Assert.assertTrue)15 Logger (org.slf4j.Logger)14 LoggerFactory (org.slf4j.LoggerFactory)14 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)13 ArrayList (java.util.ArrayList)12 RoutingStrategy (net.dempsy.router.RoutingStrategy)12 ClusterInfoSession (net.dempsy.cluster.ClusterInfoSession)11 NodeAddress (net.dempsy.transport.NodeAddress)11 Map (java.util.Map)10 Set (java.util.Set)10 Collectors (java.util.stream.Collectors)9 Assert.assertNotNull (org.junit.Assert.assertNotNull)9 Before (org.junit.Before)9 Collections (java.util.Collections)8