Search in sources :

Example 11 with RoutingTableProvider

use of org.apache.helix.spectator.RoutingTableProvider in project helix by apache.

the class TestRoutingTableSnapshot method testRoutingTableSnapshot.

@Test
public void testRoutingTableSnapshot() throws InterruptedException {
    RoutingTableProvider routingTableProvider = new RoutingTableProvider(_manager, PropertyType.EXTERNALVIEW);
    String db1 = "TestDB-1";
    _setupTool.addResourceToCluster(CLUSTER_NAME, db1, NUM_PARTITIONS, "MasterSlave", IdealState.RebalanceMode.FULL_AUTO.name());
    _setupTool.rebalanceStorageCluster(CLUSTER_NAME, db1, NUM_REPLICAS);
    Thread.sleep(200);
    HelixClusterVerifier clusterVerifier = new BestPossibleExternalViewVerifier.Builder(CLUSTER_NAME).setZkAddr(ZK_ADDR).build();
    Assert.assertTrue(clusterVerifier.verify());
    IdealState idealState1 = _setupTool.getClusterManagementTool().getResourceIdealState(CLUSTER_NAME, db1);
    RoutingTableSnapshot routingTableSnapshot = routingTableProvider.getRoutingTableSnapshot();
    validateMapping(idealState1, routingTableSnapshot);
    Assert.assertEquals(routingTableSnapshot.getInstanceConfigs().size(), NUM_NODES);
    Assert.assertEquals(routingTableSnapshot.getResources().size(), 1);
    Assert.assertEquals(routingTableSnapshot.getLiveInstances().size(), NUM_NODES);
    // add new DB and shutdown an instance
    String db2 = "TestDB-2";
    _setupTool.addResourceToCluster(CLUSTER_NAME, db2, NUM_PARTITIONS, "MasterSlave", IdealState.RebalanceMode.FULL_AUTO.name());
    _setupTool.rebalanceStorageCluster(CLUSTER_NAME, db2, NUM_REPLICAS);
    // shutdown an instance
    _participants[0].syncStop();
    Thread.sleep(200);
    Assert.assertTrue(clusterVerifier.verify());
    // the original snapshot should not change
    Assert.assertEquals(routingTableSnapshot.getInstanceConfigs().size(), NUM_NODES);
    Assert.assertEquals(routingTableSnapshot.getResources().size(), 1);
    Assert.assertEquals(routingTableSnapshot.getLiveInstances().size(), NUM_NODES);
    RoutingTableSnapshot newRoutingTableSnapshot = routingTableProvider.getRoutingTableSnapshot();
    Assert.assertEquals(newRoutingTableSnapshot.getInstanceConfigs().size(), NUM_NODES);
    Assert.assertEquals(newRoutingTableSnapshot.getResources().size(), 2);
    Assert.assertEquals(newRoutingTableSnapshot.getLiveInstances().size(), NUM_NODES - 1);
}
Also used : HelixClusterVerifier(org.apache.helix.tools.ClusterVerifiers.HelixClusterVerifier) RoutingTableSnapshot(org.apache.helix.spectator.RoutingTableSnapshot) RoutingTableProvider(org.apache.helix.spectator.RoutingTableProvider) IdealState(org.apache.helix.model.IdealState) Test(org.testng.annotations.Test)

Example 12 with RoutingTableProvider

use of org.apache.helix.spectator.RoutingTableProvider in project helix by apache.

the class TestRoutingTable method testMultiThread.

@Test()
public void testMultiThread() throws Exception {
    final RoutingTableProvider routingTable = new RoutingTableProvider();
    List<ExternalView> externalViewList = new ArrayList<>();
    ZNRecord record = new ZNRecord("TESTDB");
    for (int i = 0; i < 1000; i++) {
        add(record, "TESTDB_" + i, "localhost_8900", "MASTER");
    }
    externalViewList.add(new ExternalView(record));
    routingTable.onExternalViewChange(externalViewList, changeContext);
    Callable<Boolean> runnable = new Callable<Boolean>() {

        @Override
        public Boolean call() throws Exception {
            try {
                int count = 0;
                while (count < 100) {
                    List<InstanceConfig> instancesList = routingTable.getInstances("TESTDB", "TESTDB_0", "MASTER");
                    AssertJUnit.assertEquals(instancesList.size(), 1);
                    // System.out.println(System.currentTimeMillis() + "-->"
                    // + instancesList.size());
                    Thread.sleep(5);
                    count++;
                }
            } catch (InterruptedException e) {
            // e.printStackTrace();
            }
            return true;
        }
    };
    ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
    Future<Boolean> submit = executor.submit(runnable);
    int count = 0;
    while (count < 10) {
        try {
            Thread.sleep(10);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        routingTable.onExternalViewChange(externalViewList, changeContext);
        count++;
    }
    Boolean result = submit.get(60, TimeUnit.SECONDS);
    AssertJUnit.assertEquals(result, Boolean.TRUE);
}
Also used : ExternalView(org.apache.helix.model.ExternalView) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) ArrayList(java.util.ArrayList) Callable(java.util.concurrent.Callable) InstanceConfig(org.apache.helix.model.InstanceConfig) RoutingTableProvider(org.apache.helix.spectator.RoutingTableProvider) Test(org.testng.annotations.Test)

Example 13 with RoutingTableProvider

use of org.apache.helix.spectator.RoutingTableProvider in project helix by apache.

the class TestRoutingTable method testSimple.

@Test()
public void testSimple() {
    List<InstanceConfig> instances;
    RoutingTableProvider routingTable = new RoutingTableProvider();
    ZNRecord record = new ZNRecord("TESTDB");
    // one master
    add(record, "TESTDB_0", "localhost_8900", "MASTER");
    List<ExternalView> externalViewList = new ArrayList<>();
    externalViewList.add(new ExternalView(record));
    routingTable.onExternalViewChange(externalViewList, changeContext);
    instances = routingTable.getInstances("TESTDB", "TESTDB_0", "MASTER");
    AssertJUnit.assertNotNull(instances);
    AssertJUnit.assertEquals(instances.size(), 1);
    // additions
    add(record, "TESTDB_0", "localhost_8901", "MASTER");
    add(record, "TESTDB_1", "localhost_8900", "SLAVE");
    externalViewList = new ArrayList<ExternalView>();
    externalViewList.add(new ExternalView(record));
    routingTable.onExternalViewChange(externalViewList, changeContext);
    instances = routingTable.getInstances("TESTDB", "TESTDB_0", "MASTER");
    AssertJUnit.assertNotNull(instances);
    AssertJUnit.assertEquals(instances.size(), 2);
    instances = routingTable.getInstances("TESTDB", "TESTDB_1", "SLAVE");
    AssertJUnit.assertNotNull(instances);
    AssertJUnit.assertEquals(instances.size(), 1);
    // updates
    add(record, "TESTDB_0", "localhost_8901", "SLAVE");
    externalViewList = new ArrayList<>();
    externalViewList.add(new ExternalView(record));
    routingTable.onExternalViewChange(externalViewList, changeContext);
    instances = routingTable.getInstances("TESTDB", "TESTDB_0", "SLAVE");
    AssertJUnit.assertNotNull(instances);
    AssertJUnit.assertEquals(instances.size(), 1);
}
Also used : ExternalView(org.apache.helix.model.ExternalView) InstanceConfig(org.apache.helix.model.InstanceConfig) ArrayList(java.util.ArrayList) RoutingTableProvider(org.apache.helix.spectator.RoutingTableProvider) Test(org.testng.annotations.Test)

Aggregations

RoutingTableProvider (org.apache.helix.spectator.RoutingTableProvider)13 Test (org.testng.annotations.Test)11 ArrayList (java.util.ArrayList)6 ExternalView (org.apache.helix.model.ExternalView)6 InstanceConfig (org.apache.helix.model.InstanceConfig)5 IdealState (org.apache.helix.model.IdealState)3 HashSet (java.util.HashSet)2 ClusterControllerManager (org.apache.helix.integration.manager.ClusterControllerManager)2 HelixClusterVerifier (org.apache.helix.tools.ClusterVerifiers.HelixClusterVerifier)2 BeforeClass (org.testng.annotations.BeforeClass)2 Date (java.util.Date)1 Map (java.util.Map)1 Callable (java.util.concurrent.Callable)1 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)1 HelixManager (org.apache.helix.HelixManager)1 MockParticipantManager (org.apache.helix.integration.manager.MockParticipantManager)1 ZKHelixAdmin (org.apache.helix.manager.zk.ZKHelixAdmin)1 ClusterConfig (org.apache.helix.model.ClusterConfig)1 RoutingTableSnapshot (org.apache.helix.spectator.RoutingTableSnapshot)1 ClusterStateVerifier (org.apache.helix.tools.ClusterStateVerifier)1