Search in sources :

Example 16 with ZkBaseDataAccessor

use of org.apache.helix.manager.zk.ZkBaseDataAccessor in project helix by apache.

the class TestDisableResource method testDisableResourceInCustomMode.

@Test
public void testDisableResourceInCustomMode() throws Exception {
    String className = TestHelper.getTestClassName();
    String methodName = TestHelper.getTestMethodName();
    String clusterName = className + "_" + methodName;
    System.out.println("START " + clusterName + " at " + new Date(System.currentTimeMillis()));
    // participant port
    TestHelper.setupCluster(// participant port
    clusterName, // participant port
    ZK_ADDR, // participant port
    12918, // participant name prefix
    "localhost", // resource name prefix
    "TestDB", // resources
    1, // partitions per resource
    PARTITION_NUM, // number of nodes
    N, // replicas
    2, "MasterSlave", RebalanceMode.CUSTOMIZED, // do rebalance
    true);
    // set up custom ideal-state
    BaseDataAccessor<ZNRecord> baseAccessor = new ZkBaseDataAccessor<ZNRecord>(_gZkClient);
    HelixDataAccessor accessor = new ZKHelixDataAccessor(clusterName, baseAccessor);
    PropertyKey.Builder keyBuilder = accessor.keyBuilder();
    IdealState idealState = accessor.getProperty(keyBuilder.idealStates("TestDB0"));
    idealState.setPartitionState("TestDB0_0", "localhost_12918", "SLAVE");
    idealState.setPartitionState("TestDB0_0", "localhost_12919", "SLAVE");
    accessor.setProperty(keyBuilder.idealStates("TestDB0"), idealState);
    ClusterControllerManager controller = new ClusterControllerManager(ZK_ADDR, clusterName, "controller");
    controller.syncStart();
    // start participants
    MockParticipantManager[] participants = new MockParticipantManager[N];
    for (int i = 0; i < N; i++) {
        String instanceName = "localhost_" + (12918 + i);
        participants[i] = new MockParticipantManager(ZK_ADDR, clusterName, instanceName);
        participants[i].syncStart();
    }
    boolean result = ClusterStateVerifier.verifyByZkCallback(new ClusterStateVerifier.BestPossAndExtViewZkVerifier(ZK_ADDR, clusterName));
    Assert.assertTrue(result);
    // Disable TestDB0
    enableResource(clusterName, false);
    checkExternalView(clusterName);
    // Re-enable TestDB0
    enableResource(clusterName, true);
    result = ClusterStateVerifier.verifyByPolling(new ClusterStateVerifier.BestPossAndExtViewZkVerifier(ZK_ADDR, clusterName));
    Assert.assertTrue(result);
    // Clean up
    controller.syncStop();
    for (int i = 0; i < N; i++) {
        participants[i].syncStop();
    }
    System.out.println("END " + clusterName + " at " + new Date(System.currentTimeMillis()));
}
Also used : ZkBaseDataAccessor(org.apache.helix.manager.zk.ZkBaseDataAccessor) MockParticipantManager(org.apache.helix.integration.manager.MockParticipantManager) ClusterStateVerifier(org.apache.helix.tools.ClusterStateVerifier) Date(java.util.Date) IdealState(org.apache.helix.model.IdealState) ClusterControllerManager(org.apache.helix.integration.manager.ClusterControllerManager) ZKHelixDataAccessor(org.apache.helix.manager.zk.ZKHelixDataAccessor) HelixDataAccessor(org.apache.helix.HelixDataAccessor) ZNRecord(org.apache.helix.ZNRecord) PropertyKey(org.apache.helix.PropertyKey) ZKHelixDataAccessor(org.apache.helix.manager.zk.ZKHelixDataAccessor) Test(org.testng.annotations.Test)

Example 17 with ZkBaseDataAccessor

use of org.apache.helix.manager.zk.ZkBaseDataAccessor in project helix by apache.

the class TestDistributedCMMain method testDistributedCMMain.

@Test
public void testDistributedCMMain() throws Exception {
    String className = TestHelper.getTestClassName();
    String methodName = TestHelper.getTestMethodName();
    String clusterNamePrefix = className + "_" + methodName;
    final int n = 5;
    final int clusterNb = 10;
    System.out.println("START " + clusterNamePrefix + " at " + new Date(System.currentTimeMillis()));
    // setup 10 clusters
    for (int i = 0; i < clusterNb; i++) {
        String clusterName = clusterNamePrefix + "0_" + i;
        String participantName = "localhost" + i;
        String resourceName = "TestDB" + i;
        // participant port
        TestHelper.setupCluster(// participant port
        clusterName, // participant port
        ZK_ADDR, // participant port
        12918, // participant name prefix
        participantName, // resource name prefix
        resourceName, // resources
        1, // partitions per resource
        8, // number of nodes
        n, // replicas
        3, "MasterSlave", // do rebalance
        true);
    }
    // setup controller cluster
    final String controllerClusterName = "CONTROLLER_" + clusterNamePrefix;
    // controller
    TestHelper.setupCluster(// controller
    "CONTROLLER_" + clusterNamePrefix, // controller
    ZK_ADDR, // controller
    0, // participant name prefix
    "controller", // resource name prefix
    clusterNamePrefix, // resources
    1, // partitions per resource
    clusterNb, // number of nodes
    n, // replicas
    3, "LeaderStandby", // do rebalance
    true);
    // start distributed cluster controllers
    ClusterDistributedController[] controllers = new ClusterDistributedController[n + n];
    for (int i = 0; i < n; i++) {
        controllers[i] = new ClusterDistributedController(ZK_ADDR, controllerClusterName, "controller_" + i);
        controllers[i].syncStart();
    }
    boolean result = ClusterStateVerifier.verifyByZkCallback(new ClusterStateVerifier.BestPossAndExtViewZkVerifier(ZK_ADDR, controllerClusterName), 30000);
    Assert.assertTrue(result, "Controller cluster NOT in ideal state");
    // start first cluster
    MockParticipantManager[] participants = new MockParticipantManager[n];
    final String firstClusterName = clusterNamePrefix + "0_0";
    for (int i = 0; i < n; i++) {
        String instanceName = "localhost0_" + (12918 + i);
        participants[i] = new MockParticipantManager(ZK_ADDR, firstClusterName, instanceName);
        participants[i].syncStart();
    }
    result = ClusterStateVerifier.verifyByZkCallback(new BestPossAndExtViewZkVerifier(ZK_ADDR, firstClusterName));
    Assert.assertTrue(result, "first cluster NOT in ideal state");
    // add more controllers to controller cluster
    ClusterSetup setupTool = new ClusterSetup(ZK_ADDR);
    for (int i = 0; i < n; i++) {
        String controller = "controller_" + (n + i);
        setupTool.addInstanceToCluster(controllerClusterName, controller);
    }
    setupTool.rebalanceStorageCluster(controllerClusterName, clusterNamePrefix + "0", 6);
    for (int i = n; i < 2 * n; i++) {
        controllers[i] = new ClusterDistributedController(ZK_ADDR, controllerClusterName, "controller_" + i);
        controllers[i].syncStart();
    }
    // verify controller cluster
    result = ClusterStateVerifier.verifyByZkCallback(new ClusterStateVerifier.BestPossAndExtViewZkVerifier(ZK_ADDR, controllerClusterName));
    Assert.assertTrue(result, "Controller cluster NOT in ideal state");
    // verify first cluster
    result = ClusterStateVerifier.verifyByZkCallback(new BestPossAndExtViewZkVerifier(ZK_ADDR, firstClusterName));
    Assert.assertTrue(result, "first cluster NOT in ideal state");
    // stop controller_0-5
    ZkBaseDataAccessor<ZNRecord> baseAccessor = new ZkBaseDataAccessor<ZNRecord>(_gZkClient);
    ZKHelixDataAccessor accessor = new ZKHelixDataAccessor(controllerClusterName, baseAccessor);
    Builder keyBuilder = accessor.keyBuilder();
    for (int i = 0; i < n; i++) {
        LiveInstance leader = accessor.getProperty(keyBuilder.controllerLeader());
        String leaderName = leader.getId();
        int j = Integer.parseInt(leaderName.substring(leaderName.lastIndexOf('_') + 1));
        controllers[j].syncStop();
        result = ClusterStateVerifier.verifyByZkCallback(new ClusterStateVerifier.BestPossAndExtViewZkVerifier(ZK_ADDR, controllerClusterName));
        Assert.assertTrue(result, "Controller cluster NOT in ideal state");
        result = ClusterStateVerifier.verifyByZkCallback(new BestPossAndExtViewZkVerifier(ZK_ADDR, firstClusterName));
        Assert.assertTrue(result, "first cluster NOT in ideal state");
    }
    // clean up
    // wait for all zk callbacks done
    System.out.println("Cleaning up...");
    for (int i = 0; i < 5; i++) {
        result = ClusterStateVerifier.verifyByZkCallback(new ClusterStateVerifier.BestPossAndExtViewZkVerifier(ZK_ADDR, controllerClusterName));
        controllers[i].syncStop();
    }
    for (int i = 0; i < 5; i++) {
        participants[i].syncStop();
    }
    System.out.println("END " + clusterNamePrefix + " at " + new Date(System.currentTimeMillis()));
}
Also used : ZkBaseDataAccessor(org.apache.helix.manager.zk.ZkBaseDataAccessor) MockParticipantManager(org.apache.helix.integration.manager.MockParticipantManager) Builder(org.apache.helix.PropertyKey.Builder) ClusterStateVerifier(org.apache.helix.tools.ClusterStateVerifier) ClusterSetup(org.apache.helix.tools.ClusterSetup) BestPossAndExtViewZkVerifier(org.apache.helix.tools.ClusterStateVerifier.BestPossAndExtViewZkVerifier) Date(java.util.Date) BestPossAndExtViewZkVerifier(org.apache.helix.tools.ClusterStateVerifier.BestPossAndExtViewZkVerifier) ClusterDistributedController(org.apache.helix.integration.manager.ClusterDistributedController) LiveInstance(org.apache.helix.model.LiveInstance) ZNRecord(org.apache.helix.ZNRecord) ZKHelixDataAccessor(org.apache.helix.manager.zk.ZKHelixDataAccessor) Test(org.testng.annotations.Test)

Example 18 with ZkBaseDataAccessor

use of org.apache.helix.manager.zk.ZkBaseDataAccessor in project helix by apache.

the class TestDistributedClusterController method testDistributedClusterController.

@Test
public void testDistributedClusterController() throws Exception {
    String className = TestHelper.getTestClassName();
    String methodName = TestHelper.getTestMethodName();
    String clusterNamePrefix = className + "_" + methodName;
    final int n = 5;
    final int clusterNb = 10;
    System.out.println("START " + clusterNamePrefix + " at " + new Date(System.currentTimeMillis()));
    // setup 10 clusters
    for (int i = 0; i < clusterNb; i++) {
        String clusterName = clusterNamePrefix + "0_" + i;
        String participantName = "localhost" + i;
        String resourceName = "TestDB" + i;
        // participant port
        TestHelper.setupCluster(// participant port
        clusterName, // participant port
        ZK_ADDR, // participant port
        12918, // participant name prefix
        participantName, // resource name prefix
        resourceName, // resources
        1, // partitions per resource
        8, // number of nodes
        n, // replicas
        3, "MasterSlave", // do rebalance
        true);
    }
    // setup controller cluster
    final String controllerClusterName = "CONTROLLER_" + clusterNamePrefix;
    // controller
    TestHelper.setupCluster(// controller
    "CONTROLLER_" + clusterNamePrefix, // controller
    ZK_ADDR, // controller
    0, // participant name prefix
    "controller", // resource name prefix
    clusterNamePrefix, // resources
    1, // partitions per resource
    clusterNb, // number of nodes
    n, // replicas
    3, "LeaderStandby", // do rebalance
    true);
    // start distributed cluster controllers
    ClusterDistributedController[] controllers = new ClusterDistributedController[n];
    for (int i = 0; i < n; i++) {
        controllers[i] = new ClusterDistributedController(ZK_ADDR, controllerClusterName, "controller_" + i);
        controllers[i].syncStart();
    }
    boolean result = ClusterStateVerifier.verifyByZkCallback(new ClusterStateVerifier.BestPossAndExtViewZkVerifier(ZK_ADDR, controllerClusterName), 30000);
    Assert.assertTrue(result, "Controller cluster NOT in ideal state");
    // start first cluster
    MockParticipantManager[] participants = new MockParticipantManager[n];
    final String firstClusterName = clusterNamePrefix + "0_0";
    for (int i = 0; i < n; i++) {
        String instanceName = "localhost0_" + (12918 + i);
        participants[i] = new MockParticipantManager(ZK_ADDR, firstClusterName, instanceName);
        participants[i].syncStart();
    }
    result = ClusterStateVerifier.verifyByZkCallback(new BestPossAndExtViewZkVerifier(ZK_ADDR, firstClusterName));
    Assert.assertTrue(result, "first cluster NOT in ideal state");
    // stop current leader in controller cluster
    ZkBaseDataAccessor<ZNRecord> baseAccessor = new ZkBaseDataAccessor<ZNRecord>(_gZkClient);
    ZKHelixDataAccessor accessor = new ZKHelixDataAccessor(controllerClusterName, baseAccessor);
    Builder keyBuilder = accessor.keyBuilder();
    LiveInstance leader = accessor.getProperty(keyBuilder.controllerLeader());
    String leaderName = leader.getId();
    int j = Integer.parseInt(leaderName.substring(leaderName.lastIndexOf('_') + 1));
    controllers[j].syncStop();
    // setup the second cluster
    MockParticipantManager[] participants2 = new MockParticipantManager[n];
    final String secondClusterName = clusterNamePrefix + "0_1";
    for (int i = 0; i < n; i++) {
        String instanceName = "localhost1_" + (12918 + i);
        participants2[i] = new MockParticipantManager(ZK_ADDR, secondClusterName, instanceName);
        participants2[i].syncStart();
    }
    result = ClusterStateVerifier.verifyByZkCallback(new BestPossAndExtViewZkVerifier(ZK_ADDR, secondClusterName));
    Assert.assertTrue(result, "second cluster NOT in ideal state");
    // clean up
    // wait for all zk callbacks done
    System.out.println("Cleaning up...");
    for (int i = 0; i < 5; i++) {
        result = ClusterStateVerifier.verifyByZkCallback(new BestPossAndExtViewZkVerifier(ZK_ADDR, controllerClusterName));
        controllers[i].syncStop();
    }
    for (int i = 0; i < 5; i++) {
        participants[i].syncStop();
    }
    System.out.println("END " + clusterNamePrefix + " at " + new Date(System.currentTimeMillis()));
}
Also used : ZkBaseDataAccessor(org.apache.helix.manager.zk.ZkBaseDataAccessor) MockParticipantManager(org.apache.helix.integration.manager.MockParticipantManager) Builder(org.apache.helix.PropertyKey.Builder) ClusterStateVerifier(org.apache.helix.tools.ClusterStateVerifier) BestPossAndExtViewZkVerifier(org.apache.helix.tools.ClusterStateVerifier.BestPossAndExtViewZkVerifier) Date(java.util.Date) BestPossAndExtViewZkVerifier(org.apache.helix.tools.ClusterStateVerifier.BestPossAndExtViewZkVerifier) ClusterDistributedController(org.apache.helix.integration.manager.ClusterDistributedController) LiveInstance(org.apache.helix.model.LiveInstance) ZNRecord(org.apache.helix.ZNRecord) ZKHelixDataAccessor(org.apache.helix.manager.zk.ZKHelixDataAccessor) Test(org.testng.annotations.Test)

Example 19 with ZkBaseDataAccessor

use of org.apache.helix.manager.zk.ZkBaseDataAccessor in project helix by apache.

the class TestSyncSessionToController method testSyncSessionToController.

@Test
public void testSyncSessionToController() throws Exception {
    System.out.println("START testSyncSessionToController at " + new Date(System.currentTimeMillis()));
    String clusterName = getShortClassName();
    MockParticipantManager[] participants = new MockParticipantManager[5];
    int resourceNb = 10;
    // participant port
    TestHelper.setupCluster(// participant port
    clusterName, // participant port
    ZK_ADDR, // participant port
    12918, // participant name prefix
    "localhost", // resource name prefix
    "TestDB", // resources
    resourceNb, // partitions per resource
    1, // number of nodes
    5, // replicas
    1, "MasterSlave", // do rebalance
    true);
    ClusterControllerManager controller = new ClusterControllerManager(ZK_ADDR, clusterName, "controller_0");
    controller.syncStart();
    // start participants
    for (int i = 0; i < 5; i++) {
        String instanceName = "localhost_" + (12918 + i);
        participants[i] = new MockParticipantManager(ZK_ADDR, clusterName, instanceName);
        participants[i].syncStart();
    }
    ZKHelixManager zkHelixManager = new ZKHelixManager(clusterName, "controllerMessageListener", InstanceType.CONTROLLER, ZK_ADDR);
    zkHelixManager.connect();
    MockMessageListener mockMessageListener = new MockMessageListener();
    zkHelixManager.addControllerMessageListener(mockMessageListener);
    PropertyKey.Builder keyBuilder = new PropertyKey.Builder(clusterName);
    ZkBaseDataAccessor<ZNRecord> accessor = new ZkBaseDataAccessor<ZNRecord>(_gZkClient);
    String path = keyBuilder.liveInstance("localhost_12918").getPath();
    Stat stat = new Stat();
    ZNRecord data = accessor.get(path, stat, 2);
    data.getSimpleFields().put("SESSION_ID", "invalid-id");
    accessor.set(path, data, 2);
    Thread.sleep(2000);
    Assert.assertTrue(mockMessageListener.isSessionSyncMessageSent());
}
Also used : ZkBaseDataAccessor(org.apache.helix.manager.zk.ZkBaseDataAccessor) MockParticipantManager(org.apache.helix.integration.manager.MockParticipantManager) ZKHelixManager(org.apache.helix.manager.zk.ZKHelixManager) Date(java.util.Date) ClusterControllerManager(org.apache.helix.integration.manager.ClusterControllerManager) Stat(org.apache.zookeeper.data.Stat) PropertyKey(org.apache.helix.PropertyKey) ZNRecord(org.apache.helix.ZNRecord) Test(org.testng.annotations.Test)

Example 20 with ZkBaseDataAccessor

use of org.apache.helix.manager.zk.ZkBaseDataAccessor in project helix by apache.

the class TestAddStateModelFactoryAfterConnect method testBasic.

@Test
public void testBasic() throws Exception {
    // Logger.getRootLogger().setLevel(Level.INFO);
    String className = TestHelper.getTestClassName();
    String methodName = TestHelper.getTestMethodName();
    String clusterName = className + "_" + methodName;
    final int n = 5;
    System.out.println("START " + clusterName + " at " + new Date(System.currentTimeMillis()));
    MockParticipantManager[] participants = new MockParticipantManager[n];
    // participant port
    TestHelper.setupCluster(// participant port
    clusterName, // participant port
    ZK_ADDR, // participant port
    12918, // participant name prefix
    "localhost", // resource name prefix
    "TestDB", // resources
    1, // partitions per resource
    10, // number of nodes
    n, // replicas
    3, "MasterSlave", // do rebalance
    true);
    ClusterControllerManager controller = new ClusterControllerManager(ZK_ADDR, clusterName, "controller_0");
    controller.syncStart();
    // start participants
    for (int i = 0; i < n; i++) {
        String instanceName = "localhost_" + (12918 + i);
        participants[i] = new MockParticipantManager(ZK_ADDR, clusterName, instanceName);
        participants[i].syncStart();
    }
    boolean result = ClusterStateVerifier.verifyByZkCallback(new BestPossAndExtViewZkVerifier(ZK_ADDR, clusterName));
    Assert.assertTrue(result);
    // add a new idealState without registering message handling factory
    ClusterSetup setupTool = new ClusterSetup(ZK_ADDR);
    setupTool.addResourceToCluster(clusterName, "TestDB1", 16, "MasterSlave");
    ZkBaseDataAccessor<ZNRecord> baseAccessor = new ZkBaseDataAccessor<ZNRecord>(_gZkClient);
    ZKHelixDataAccessor accessor = new ZKHelixDataAccessor(clusterName, baseAccessor);
    Builder keyBuilder = accessor.keyBuilder();
    IdealState idealState = accessor.getProperty(keyBuilder.idealStates("TestDB1"));
    idealState.setStateModelFactoryName("TestDB1_Factory");
    accessor.setProperty(keyBuilder.idealStates("TestDB1"), idealState);
    setupTool.rebalanceStorageCluster(clusterName, "TestDB1", 3);
    // assert that we have received OFFLINE->SLAVE messages for all partitions
    int totalMsgs = 0;
    for (int retry = 0; retry < 5; retry++) {
        Thread.sleep(100);
        totalMsgs = 0;
        for (int i = 0; i < n; i++) {
            List<Message> msgs = accessor.getChildValues(keyBuilder.messages(participants[i].getInstanceName()));
            totalMsgs += msgs.size();
        }
        if (// partition# x replicas
        totalMsgs == 48)
            break;
    }
    Assert.assertEquals(totalMsgs, 48, "Should accumulated 48 unprocessed messages (1 O->S per partition per replica) because TestDB1 is added without state-model-factory but was " + totalMsgs);
    // Logger.getRootLogger().setLevel(Level.INFO);
    for (int i = 0; i < n; i++) {
        participants[i].getStateMachineEngine().registerStateModelFactory("MasterSlave", new MockMSModelFactory(), "TestDB1_Factory");
    }
    result = ClusterStateVerifier.verifyByZkCallback(new BestPossAndExtViewZkVerifier(ZK_ADDR, clusterName));
    Assert.assertTrue(result);
    // clean up
    // wait for all zk callbacks done
    controller.syncStop();
    for (int i = 0; i < 5; i++) {
        participants[i].syncStop();
    }
    System.out.println("END " + clusterName + " at " + new Date(System.currentTimeMillis()));
}
Also used : ZkBaseDataAccessor(org.apache.helix.manager.zk.ZkBaseDataAccessor) MockParticipantManager(org.apache.helix.integration.manager.MockParticipantManager) Message(org.apache.helix.model.Message) Builder(org.apache.helix.PropertyKey.Builder) ClusterSetup(org.apache.helix.tools.ClusterSetup) BestPossAndExtViewZkVerifier(org.apache.helix.tools.ClusterStateVerifier.BestPossAndExtViewZkVerifier) Date(java.util.Date) IdealState(org.apache.helix.model.IdealState) ClusterControllerManager(org.apache.helix.integration.manager.ClusterControllerManager) MockMSModelFactory(org.apache.helix.mock.participant.MockMSModelFactory) ZNRecord(org.apache.helix.ZNRecord) ZKHelixDataAccessor(org.apache.helix.manager.zk.ZKHelixDataAccessor) Test(org.testng.annotations.Test)

Aggregations

ZkBaseDataAccessor (org.apache.helix.manager.zk.ZkBaseDataAccessor)44 ZNRecord (org.apache.helix.ZNRecord)40 Date (java.util.Date)37 Test (org.testng.annotations.Test)37 ZKHelixDataAccessor (org.apache.helix.manager.zk.ZKHelixDataAccessor)30 Builder (org.apache.helix.PropertyKey.Builder)18 HelixDataAccessor (org.apache.helix.HelixDataAccessor)15 MockParticipantManager (org.apache.helix.integration.manager.MockParticipantManager)15 IdealState (org.apache.helix.model.IdealState)12 ClusterControllerManager (org.apache.helix.integration.manager.ClusterControllerManager)10 ArrayList (java.util.ArrayList)8 PropertyKey (org.apache.helix.PropertyKey)8 ExternalView (org.apache.helix.model.ExternalView)8 BestPossAndExtViewZkVerifier (org.apache.helix.tools.ClusterStateVerifier.BestPossAndExtViewZkVerifier)8 ClusterDistributedController (org.apache.helix.integration.manager.ClusterDistributedController)6 LiveInstance (org.apache.helix.model.LiveInstance)6 ClusterStateVerifier (org.apache.helix.tools.ClusterStateVerifier)6 Stat (org.apache.zookeeper.data.Stat)6 HashMap (java.util.HashMap)5 ZKHelixAdmin (org.apache.helix.manager.zk.ZKHelixAdmin)5