Search in sources :

Example 16 with ContainerAddress

use of net.dempsy.router.RoutingStrategy.ContainerAddress in project Dempsy by Dempsy.

the class TestSimpleRoutingStrategy method testInboundWithOutbound.

@Test
public void testInboundWithOutbound() throws Exception {
    final Manager<RoutingStrategy.Inbound> manager = new Manager<>(RoutingStrategy.Inbound.class);
    try (final RoutingStrategy.Inbound ib = manager.getAssociatedInstance(SimpleRoutingStrategy.class.getPackage().getName())) {
        assertNotNull(ib);
        assertTrue(SimpleInboundSide.class.isAssignableFrom(ib.getClass()));
        final ClusterId cid = new ClusterId("test", "test");
        ib.setContainerDetails(cid, new ContainerAddress(new DummyNodeAddress("here"), 0), (l, m) -> {
        });
        ib.start(infra);
        assertTrue(waitForReg(session));
        try (final ClusterInfoSession ses2 = sessFact.createSession()) {
            try (final RoutingStrategyManager obman = chain(new RoutingStrategyManager(), o -> o.start(makeInfra(ses2, sched)));
                final RoutingStrategy.Factory obf = obman.getAssociatedInstance(SimpleRoutingStrategy.class.getPackage().getName())) {
                obf.start(makeInfra(ses2, sched));
                final RoutingStrategy.Router ob = obf.getStrategy(cid);
                final KeyedMessageWithType km = new KeyedMessageWithType(null, null, "");
                assertTrue(poll(o -> ob.selectDestinationForMessage(km) != null));
                final ContainerAddress ca = ob.selectDestinationForMessage(km);
                assertNotNull(ca);
                assertEquals("here", ((DummyNodeAddress) ca.node).name);
                // now distupt the session
                session.close();
                // the destination should clear until a new in runs
                assertTrue(poll(o -> ob.selectDestinationForMessage(km) == null));
                try (ClusterInfoSession ses3 = sessFact.createSession();
                    RoutingStrategy.Inbound ib2 = manager.getAssociatedInstance(SimpleRoutingStrategy.class.getPackage().getName())) {
                    ib2.setContainerDetails(cid, ca, (l, m) -> {
                    });
                    ib2.start(makeInfra(ses3, sched));
                    assertTrue(poll(o -> ob.selectDestinationForMessage(km) != null));
                }
            }
        }
    }
}
Also used : ClusterInfoException(net.dempsy.cluster.ClusterInfoException) ClusterInfoSession(net.dempsy.cluster.ClusterInfoSession) ContainerAddress(net.dempsy.router.RoutingStrategy.ContainerAddress) Assert.assertNotNull(org.junit.Assert.assertNotNull) KeyedMessageWithType(net.dempsy.messages.KeyedMessageWithType) NodeAddress(net.dempsy.transport.NodeAddress) Collection(java.util.Collection) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Assert.assertNotEquals(org.junit.Assert.assertNotEquals) RoutingStrategyManager(net.dempsy.router.RoutingStrategyManager) AutoDisposeSingleThreadScheduler(net.dempsy.util.executor.AutoDisposeSingleThreadScheduler) ConditionPoll.poll(net.dempsy.utils.test.ConditionPoll.poll) Infrastructure(net.dempsy.Infrastructure) RoutingStrategy(net.dempsy.router.RoutingStrategy) Functional.chain(net.dempsy.util.Functional.chain) After(org.junit.After) Manager(net.dempsy.Manager) TestInfrastructure(net.dempsy.util.TestInfrastructure) Assert.assertEquals(org.junit.Assert.assertEquals) ClusterId(net.dempsy.config.ClusterId) Before(org.junit.Before) LocalClusterSessionFactory(net.dempsy.cluster.local.LocalClusterSessionFactory) RoutingStrategyManager(net.dempsy.router.RoutingStrategyManager) ClusterId(net.dempsy.config.ClusterId) RoutingStrategyManager(net.dempsy.router.RoutingStrategyManager) Manager(net.dempsy.Manager) ContainerAddress(net.dempsy.router.RoutingStrategy.ContainerAddress) KeyedMessageWithType(net.dempsy.messages.KeyedMessageWithType) RoutingStrategy(net.dempsy.router.RoutingStrategy) ClusterInfoSession(net.dempsy.cluster.ClusterInfoSession) Test(org.junit.Test)

Example 17 with ContainerAddress

use of net.dempsy.router.RoutingStrategy.ContainerAddress in project Dempsy by Dempsy.

the class TestGroupRoutingStrategy method testInboundSimpleHappyPathRegister.

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

Example 18 with ContainerAddress

use of net.dempsy.router.RoutingStrategy.ContainerAddress in project Dempsy by Dempsy.

the class TestGroupRoutingStrategy method testInboundResillience.

@Test
public void testInboundResillience() throws Exception {
    final int numShardsToExpect = Integer.parseInt(Utils.DEFAULT_TOTAL_SHARDS);
    final String groupName = "testInboundResillience";
    final Manager<RoutingStrategy.Inbound> manager = new RoutingInboundManager();
    try (final RoutingStrategy.Inbound ib = manager.getAssociatedInstance(ClusterGroupInbound.class.getPackage().getName() + ":" + groupName)) {
        final ClusterId clusterId = super.setTestName("testInboundResillience");
        final NodeAddress na = new DummyNodeAddress("theOnlyNode");
        final ContainerAddress ca = new ContainerAddress(na, 0);
        final GroupDetails gd = new GroupDetails(groupName, na);
        final Infrastructure infra = makeInfra(session, sched);
        final Utils<GroupDetails> msutils = new Utils<>(infra, groupName, gd);
        ib.setContainerDetails(clusterId, ca, (l, m) -> {
        });
        ib.start(infra);
        checkForShardDistribution(session, msutils, numShardsToExpect, 1);
        disruptor.accept(session);
        checkForShardDistribution(session, msutils, numShardsToExpect, 1);
    }
}
Also used : ClusterId(net.dempsy.config.ClusterId) ContainerAddress(net.dempsy.router.RoutingStrategy.ContainerAddress) RoutingInboundManager(net.dempsy.router.RoutingInboundManager) GroupDetails(net.dempsy.router.group.intern.GroupDetails) Utils(net.dempsy.router.shardutils.Utils) RoutingStrategy(net.dempsy.router.RoutingStrategy) TestInfrastructure(net.dempsy.util.TestInfrastructure) Infrastructure(net.dempsy.Infrastructure) NodeAddress(net.dempsy.transport.NodeAddress) Test(org.junit.Test)

Example 19 with ContainerAddress

use of net.dempsy.router.RoutingStrategy.ContainerAddress 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 20 with ContainerAddress

use of net.dempsy.router.RoutingStrategy.ContainerAddress 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)

Aggregations

ContainerAddress (net.dempsy.router.RoutingStrategy.ContainerAddress)21 ClusterId (net.dempsy.config.ClusterId)16 RoutingStrategy (net.dempsy.router.RoutingStrategy)15 Test (org.junit.Test)15 NodeAddress (net.dempsy.transport.NodeAddress)13 ClusterInfoSession (net.dempsy.cluster.ClusterInfoSession)10 RoutingStrategyManager (net.dempsy.router.RoutingStrategyManager)8 Utils (net.dempsy.router.shardutils.Utils)8 List (java.util.List)7 Manager (net.dempsy.Manager)7 ClusterInfoSessionFactory (net.dempsy.cluster.ClusterInfoSessionFactory)7 TestInfrastructure (net.dempsy.util.TestInfrastructure)7 ConditionPoll.poll (net.dempsy.utils.test.ConditionPoll.poll)7 Assert.assertEquals (org.junit.Assert.assertEquals)7 Assert.assertTrue (org.junit.Assert.assertTrue)7 Logger (org.slf4j.Logger)7 LoggerFactory (org.slf4j.LoggerFactory)7 HashMap (java.util.HashMap)6 Map (java.util.Map)6 Consumer (java.util.function.Consumer)6