Search in sources :

Example 6 with ContainerAddress

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

the class TestLeaderAndSubscriber method testLeaderWithMutipleSubscribers.

@Test
public void testLeaderWithMutipleSubscribers() throws Exception {
    final int NUM_SUBS = 10;
    final ClusterId cid = setTestName("testLeaderWithMutipleSubscribers");
    final AtomicBoolean isRunning = new AtomicBoolean(true);
    final List<Thread> threads = new ArrayList<>();
    try {
        final List<Subscriber<ContainerAddress>> subs = new ArrayList<>();
        final AtomicBoolean go = new AtomicBoolean(false);
        for (int i = 0; i < NUM_SUBS; i++) {
            final Utils<ContainerAddress> utils = new Utils<>(makeInfra(session, sched), cid.clusterName, new ContainerAddress(new DummyNodeAddress(), new int[] { 0 }));
            final Subscriber<ContainerAddress> s;
            subs.add(s = new Subscriber<>(utils, infra, isRunning, (l, m) -> {
            }, 256));
            final Thread t = new Thread(() -> {
                // wait for it.
                while (!go.get()) {
                    if (!isRunning.get())
                        return;
                    Thread.yield();
                }
                s.process();
            }, "testLeaderWithMutipleSubscribers-" + i);
            t.start();
            threads.add(t);
        }
        go.set(true);
        final Utils<ContainerAddress> utils = new Utils<>(makeInfra(session, sched), cid.clusterName, subs.get(3).getUtils().thisNodeAddress);
        final Leader<ContainerAddress> l = new Leader<>(utils, 256, 1, infra, isRunning, ContainerAddress[]::new);
        l.process();
        assertTrue(poll(o -> l.imIt()));
        // wait until ready
        assertTrue(poll(o -> subs.stream().filter(s -> s.isReady()).count() == NUM_SUBS));
        final int lowerNum = Math.floorDiv(256, NUM_SUBS);
        final int upperNum = (int) Math.ceil((double) 256 / NUM_SUBS);
        // do we have balanced subs?
        assertTrue(poll(o -> subs.stream().filter(s -> s.numShardsIOwn() >= lowerNum).filter(s -> s.numShardsIOwn() <= upperNum).count() == NUM_SUBS));
        isRunning.set(false);
        assertTrue(poll(o -> threads.stream().filter(t -> t.isAlive()).count() == 0));
    } 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) ClusterId(net.dempsy.config.ClusterId) ArrayList(java.util.ArrayList) ContainerAddress(net.dempsy.router.RoutingStrategy.ContainerAddress) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Test(org.junit.Test)

Example 7 with ContainerAddress

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

the class TestManagedRoutingStrategy method testInboundResillience.

@Test
public void testInboundResillience() 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 = super.setTestName("testInboundResillience");
        final ContainerAddress ca = new ContainerAddress(new DummyNodeAddress("theOnlyNode"), 0);
        final Infrastructure infra = makeInfra(session, sched);
        final Utils<ContainerAddress> msutils = new Utils<>(infra, clusterId.clusterName, ca);
        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) Utils(net.dempsy.router.shardutils.Utils) RoutingStrategy(net.dempsy.router.RoutingStrategy) TestInfrastructure(net.dempsy.util.TestInfrastructure) Infrastructure(net.dempsy.Infrastructure) RoutingStrategyManager(net.dempsy.router.RoutingStrategyManager) Manager(net.dempsy.Manager) ContainerAddress(net.dempsy.router.RoutingStrategy.ContainerAddress) Test(org.junit.Test)

Example 8 with ContainerAddress

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

the class TestManagedRoutingStrategy method testInboundDoubleHappyPathRegister.

@Test
public void testInboundDoubleHappyPathRegister() throws Exception {
    final int numShardsToExpect = Integer.parseInt(Utils.DEFAULT_TOTAL_SHARDS);
    try (final RoutingStrategy.Inbound ib1 = new Manager<>(RoutingStrategy.Inbound.class).getAssociatedInstance(ManagedInbound.class.getPackage().getName());
        final RoutingStrategy.Inbound ib2 = new Manager<>(RoutingStrategy.Inbound.class).getAssociatedInstance(ManagedInbound.class.getPackage().getName())) {
        final ClusterId clusterId = new ClusterId("test", "test");
        final ContainerAddress node1Ca = new ContainerAddress(new DummyNodeAddress("node1"), 0);
        final Utils<ContainerAddress> utils = new Utils<>(infra, clusterId.clusterName, node1Ca);
        ib1.setContainerDetails(clusterId, node1Ca, (l, m) -> {
        });
        ib1.start(infra);
        final ContainerAddress node2Ca = new ContainerAddress(new DummyNodeAddress("node2"), 0);
        ib2.setContainerDetails(clusterId, node2Ca, (l, m) -> {
        });
        try (final ClusterInfoSession session2 = sessFact.createSession()) {
            ib2.start(new TestInfrastructure(session2, infra.getScheduler()));
            assertTrue(waitForShards(session, utils, numShardsToExpect));
            // if this worked right then numShardsToExpect/2 should be owned by each ... eventually.
            checkForShardDistribution(session, utils, numShardsToExpect, 2);
            // disrupt the session. This should cause a reshuffle but not fail
            disruptor.accept(session2);
            // everything should settle back
            checkForShardDistribution(session, utils, numShardsToExpect, 2);
            // now kill the second session.
            // this will disconnect the second Inbound and so the first should take over
            session2.close();
            // see if we now have 1 session and it has all shards
            checkForShardDistribution(session, utils, numShardsToExpect, 1);
        }
    }
}
Also used : TestInfrastructure(net.dempsy.util.TestInfrastructure) ClusterId(net.dempsy.config.ClusterId) Utils(net.dempsy.router.shardutils.Utils) RoutingStrategy(net.dempsy.router.RoutingStrategy) ClusterInfoSession(net.dempsy.cluster.ClusterInfoSession) ContainerAddress(net.dempsy.router.RoutingStrategy.ContainerAddress) Test(org.junit.Test)

Example 9 with ContainerAddress

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

the class TestManagedRoutingStrategy method testInboundWithOutbound.

@Test
public void testInboundWithOutbound() 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 cid = setTestName("testInboundWithOutbound");
        final ContainerAddress oca = new ContainerAddress(new DummyNodeAddress("here"), 0);
        final Infrastructure infra = makeInfra(session, sched);
        final Utils<ContainerAddress> msutils = new Utils<>(infra, cid.clusterName, oca);
        ib.setContainerDetails(cid, oca, (l, m) -> {
        });
        ib.start(infra);
        checkForShardDistribution(session, msutils, numShardsToExpect, 1);
        try (final ClusterInfoSession ses2 = sessFact.createSession();
            final RoutingStrategyManager obman = chain(new RoutingStrategyManager(), o -> o.start(makeInfra(ses2, sched)));
            final RoutingStrategy.Factory obf = obman.getAssociatedInstance(ManagedInbound.class.getPackage().getName())) {
            obf.start(makeInfra(ses2, sched));
            assertTrue(poll(o -> obf.isReady()));
            final RoutingStrategy.Router ob = obf.getStrategy(cid);
            assertTrue(poll(o -> obf.isReady()));
            final KeyedMessageWithType km = new KeyedMessageWithType(new Object(), new Object(), "");
            assertTrue(poll(o -> ob.selectDestinationForMessage(km) != null));
            final ContainerAddress ca = ob.selectDestinationForMessage(km);
            assertNotNull(ca);
            assertEquals("here", ((DummyNodeAddress) ca.node).name);
            // now disrupt the session
            session.close();
            try (ClusterInfoSession ses3 = sessFact.createSession();
                RoutingStrategy.Inbound ib2 = manager.getAssociatedInstance(ManagedInbound.class.getPackage().getName())) {
                ib2.setContainerDetails(cid, ca, (l, m) -> {
                });
                ib2.start(makeInfra(ses3, sched));
                assertTrue(poll(o -> ob.selectDestinationForMessage(km) != null));
            }
        }
    }
}
Also used : ShardAssignment(net.dempsy.router.shardutils.Utils.ShardAssignment) Arrays(java.util.Arrays) ClusterInfoException(net.dempsy.cluster.ClusterInfoException) ClusterInfoSession(net.dempsy.cluster.ClusterInfoSession) ContainerAddress(net.dempsy.router.RoutingStrategy.ContainerAddress) KeyedMessageWithType(net.dempsy.messages.KeyedMessageWithType) NodeAddress(net.dempsy.transport.NodeAddress) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) Utils(net.dempsy.router.shardutils.Utils) Supplier(java.util.function.Supplier) RoutingStrategyManager(net.dempsy.router.RoutingStrategyManager) HashSet(java.util.HashSet) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) RoutingStrategy(net.dempsy.router.RoutingStrategy) Map(java.util.Map) Manager(net.dempsy.Manager) TestInfrastructure(net.dempsy.util.TestInfrastructure) ClusterId(net.dempsy.config.ClusterId) Logger(org.slf4j.Logger) Assert.assertNotNull(org.junit.Assert.assertNotNull) ClusterInfoSessionFactory(net.dempsy.cluster.ClusterInfoSessionFactory) Assert.assertTrue(org.junit.Assert.assertTrue) Set(java.util.Set) Test(org.junit.Test) Collectors(java.util.stream.Collectors) Consumer(java.util.function.Consumer) List(java.util.List) ConditionPoll.poll(net.dempsy.utils.test.ConditionPoll.poll) Infrastructure(net.dempsy.Infrastructure) Functional.chain(net.dempsy.util.Functional.chain) BaseRouterTestWithSession(net.dempsy.router.BaseRouterTestWithSession) Assert.assertEquals(org.junit.Assert.assertEquals) 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) Utils(net.dempsy.router.shardutils.Utils) RoutingStrategy(net.dempsy.router.RoutingStrategy) TestInfrastructure(net.dempsy.util.TestInfrastructure) Infrastructure(net.dempsy.Infrastructure) ClusterInfoSession(net.dempsy.cluster.ClusterInfoSession) Test(org.junit.Test)

Example 10 with ContainerAddress

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

the class TestSimpleRoutingStrategy method testInboundResillience.

@Test
public void testInboundResillience() 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()));
        ib.setContainerDetails(new ClusterId("test", "test"), new ContainerAddress(new DummyNodeAddress(), 0), (l, m) -> {
        });
        ib.start(infra);
        assertTrue(waitForReg(session));
        // get the current ephem dir
        final String actualDir = ((SimpleInboundSide) ib).getAddressSubdirectory();
        assertNotNull(actualDir);
        session.rmdir(actualDir);
        assertTrue(waitForReg(session));
        final String newDir = ((SimpleInboundSide) ib).getAddressSubdirectory();
        assertNotEquals(actualDir, newDir);
    }
}
Also used : ClusterId(net.dempsy.config.ClusterId) RoutingStrategy(net.dempsy.router.RoutingStrategy) RoutingStrategyManager(net.dempsy.router.RoutingStrategyManager) Manager(net.dempsy.Manager) ContainerAddress(net.dempsy.router.RoutingStrategy.ContainerAddress) 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