Search in sources :

Example 86 with ZKHelixDataAccessor

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

the class TestHelixCustomCodeRunner method testCustomCodeRunner.

@Test
public void testCustomCodeRunner() throws Exception {
    System.out.println("START " + _clusterName + " at " + new Date(System.currentTimeMillis()));
    // participant name
    TestHelper.setupCluster(// participant name
    _clusterName, // participant name
    ZK_ADDR, // participant name
    _startPort, // participant name
    "localhost", // resource name prefix
    "TestDB", // resourceNb
    1, // partitionNb
    5, // nodesNb
    _nodeNb, // replica
    _nodeNb, "MasterSlave", true);
    ClusterControllerManager controller = new ClusterControllerManager(ZK_ADDR, _clusterName, "controller_0");
    controller.syncStart();
    MockParticipantManager[] participants = new MockParticipantManager[5];
    for (int i = 0; i < _nodeNb; i++) {
        String instanceName = "localhost_" + (_startPort + i);
        participants[i] = new MockParticipantManager(ZK_ADDR, _clusterName, instanceName);
        registerCustomCodeRunner(participants[i]);
        participants[i].syncStart();
    }
    boolean result = ClusterStateVerifier.verifyByPolling(new ClusterStateVerifier.BestPossAndExtViewZkVerifier(ZK_ADDR, _clusterName));
    Assert.assertTrue(result);
    // wait for the INIT type callback to finish
    Thread.sleep(1000);
    Assert.assertTrue(_callback._isCallbackInvoked);
    _callback._isCallbackInvoked = false;
    // add a new live instance
    HelixDataAccessor accessor = new ZKHelixDataAccessor(_clusterName, new ZkBaseDataAccessor<ZNRecord>(_gZkClient));
    Builder keyBuilder = accessor.keyBuilder();
    LiveInstance newLiveIns = new LiveInstance("newLiveInstance");
    newLiveIns.setHelixVersion("0.6.0");
    newLiveIns.setSessionId("randomSessionId");
    accessor.setProperty(keyBuilder.liveInstance("newLiveInstance"), newLiveIns);
    // wait for the CALLBACK type callback to finish
    Thread.sleep(1000);
    Assert.assertTrue(_callback._isCallbackInvoked);
    // clean up
    controller.syncStop();
    for (int i = 0; i < _nodeNb; i++) {
        participants[i].syncStop();
    }
    System.out.println("END " + _clusterName + " at " + new Date(System.currentTimeMillis()));
}
Also used : MockParticipantManager(org.apache.helix.integration.manager.MockParticipantManager) Builder(org.apache.helix.PropertyKey.Builder) ClusterStateVerifier(org.apache.helix.tools.ClusterStateVerifier) Date(java.util.Date) ClusterControllerManager(org.apache.helix.integration.manager.ClusterControllerManager) ZKHelixDataAccessor(org.apache.helix.manager.zk.ZKHelixDataAccessor) HelixDataAccessor(org.apache.helix.HelixDataAccessor) LiveInstance(org.apache.helix.model.LiveInstance) ZNRecord(org.apache.helix.ZNRecord) ZKHelixDataAccessor(org.apache.helix.manager.zk.ZKHelixDataAccessor) Test(org.testng.annotations.Test)

Example 87 with ZKHelixDataAccessor

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

the class TestPartitionLevelTransitionConstraint method test.

@Test
public void test() throws Exception {
    // Logger.getRootLogger().setLevel(Level.INFO);
    String className = TestHelper.getTestClassName();
    String methodName = TestHelper.getTestMethodName();
    String clusterName = className + "_" + methodName;
    int n = 2;
    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
    1, // number of nodes
    n, // replicas
    2, "MasterSlave", // do not rebalance
    false);
    // setup semi-auto ideal-state
    BaseDataAccessor<ZNRecord> baseAccessor = new ZkBaseDataAccessor<ZNRecord>(_gZkClient);
    HelixDataAccessor accessor = new ZKHelixDataAccessor(clusterName, baseAccessor);
    StateModelDefinition stateModelDef = defineStateModel();
    accessor.setProperty(accessor.keyBuilder().stateModelDef("Bootstrap"), stateModelDef);
    IdealState idealState = accessor.getProperty(accessor.keyBuilder().idealStates("TestDB0"));
    idealState.setStateModelDefRef("Bootstrap");
    idealState.setReplicas("2");
    idealState.getRecord().setListField("TestDB0_0", Arrays.asList("localhost_12919", "localhost_12918"));
    accessor.setProperty(accessor.keyBuilder().idealStates("TestDB0"), idealState);
    // setup partition-level constraint
    ConstraintItemBuilder constraintItemBuilder = new ConstraintItemBuilder();
    constraintItemBuilder.addConstraintAttribute(ConstraintAttribute.MESSAGE_TYPE.toString(), "STATE_TRANSITION").addConstraintAttribute(ConstraintAttribute.PARTITION.toString(), ".*").addConstraintAttribute(ConstraintAttribute.CONSTRAINT_VALUE.toString(), "1");
    HelixAdmin admin = new ZKHelixAdmin(_gZkClient);
    admin.setConstraint(clusterName, ConstraintType.MESSAGE_CONSTRAINT, "constraint1", constraintItemBuilder.build());
    ClusterControllerManager controller = new ClusterControllerManager(ZK_ADDR, clusterName, "controller");
    controller.syncStart();
    // start 1st participant
    MockParticipantManager[] participants = new MockParticipantManager[n];
    String instanceName1 = "localhost_12918";
    participants[0] = new MockParticipantManager(ZK_ADDR, clusterName, instanceName1);
    participants[0].getStateMachineEngine().registerStateModelFactory("Bootstrap", new BootstrapStateModelFactory());
    participants[0].syncStart();
    boolean result = ClusterStateVerifier.verifyByZkCallback(new ClusterStateVerifier.BestPossAndExtViewZkVerifier(ZK_ADDR, clusterName));
    Assert.assertTrue(result);
    // start 2nd participant which will be the master for Test0_0
    String instanceName2 = "localhost_12919";
    participants[1] = new MockParticipantManager(ZK_ADDR, clusterName, instanceName2);
    participants[1].getStateMachineEngine().registerStateModelFactory("Bootstrap", new BootstrapStateModelFactory());
    participants[1].syncStart();
    result = ClusterStateVerifier.verifyByZkCallback(new ClusterStateVerifier.BestPossAndExtViewZkVerifier(ZK_ADDR, clusterName));
    Assert.assertTrue(result);
    // check we received the message in the right order
    Assert.assertEquals(_msgOrderList.size(), 7);
    Message[] _msgOrderArray = _msgOrderList.toArray(new Message[0]);
    assertMessage(_msgOrderArray[0], "OFFLINE", "BOOTSTRAP", instanceName1);
    assertMessage(_msgOrderArray[1], "BOOTSTRAP", "SLAVE", instanceName1);
    assertMessage(_msgOrderArray[2], "SLAVE", "MASTER", instanceName1);
    // after we start the 2nd instance, the messages should be received in the following order:
    // 1) offline->bootstrap for localhost_12919
    // 2) bootstrap->slave for localhost_12919
    // 3) master->slave for localhost_12918
    // 4) slave->master for localhost_12919
    assertMessage(_msgOrderArray[3], "OFFLINE", "BOOTSTRAP", instanceName2);
    assertMessage(_msgOrderArray[4], "BOOTSTRAP", "SLAVE", instanceName2);
    assertMessage(_msgOrderArray[5], "MASTER", "SLAVE", instanceName1);
    assertMessage(_msgOrderArray[6], "SLAVE", "MASTER", instanceName2);
    // clean up
    // wait for all zk callbacks done
    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) Message(org.apache.helix.model.Message) ConstraintItemBuilder(org.apache.helix.model.builder.ConstraintItemBuilder) ClusterStateVerifier(org.apache.helix.tools.ClusterStateVerifier) HelixAdmin(org.apache.helix.HelixAdmin) ZKHelixAdmin(org.apache.helix.manager.zk.ZKHelixAdmin) 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) ZKHelixAdmin(org.apache.helix.manager.zk.ZKHelixAdmin) StateModelDefinition(org.apache.helix.model.StateModelDefinition) ZNRecord(org.apache.helix.ZNRecord) ZKHelixDataAccessor(org.apache.helix.manager.zk.ZKHelixDataAccessor) Test(org.testng.annotations.Test)

Example 88 with ZKHelixDataAccessor

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

the class TestRenamePartition method testRenamePartitionAutoIS.

@Test()
public void testRenamePartitionAutoIS() throws Exception {
    String clusterName = "CLUSTER_" + getShortClassName() + "_auto";
    System.out.println("START " + clusterName + " at " + new Date(System.currentTimeMillis()));
    // participant start port
    TestHelper.setupCluster(// participant start port
    clusterName, // participant start port
    ZK_ADDR, // participant start port
    12918, // participant name prefix
    "localhost", // resource name prefix
    "TestDB", // resources
    1, // partitions per resource
    10, // number of nodes
    5, // replicas
    3, "MasterSlave", // do rebalance
    true);
    startAndVerify(clusterName);
    // rename partition name TestDB0_0 tp TestDB0_100
    ZKHelixDataAccessor accessor = new ZKHelixDataAccessor(clusterName, new ZkBaseDataAccessor<ZNRecord>(_gZkClient));
    Builder keyBuilder = accessor.keyBuilder();
    IdealState idealState = accessor.getProperty(keyBuilder.idealStates("TestDB0"));
    List<String> prioList = idealState.getRecord().getListFields().remove("TestDB0_0");
    idealState.getRecord().getListFields().put("TestDB0_100", prioList);
    accessor.setProperty(keyBuilder.idealStates("TestDB0"), idealState);
    boolean result = ClusterStateVerifier.verifyByPolling(new ClusterStateVerifier.BestPossAndExtViewZkVerifier(ZK_ADDR, clusterName));
    Assert.assertTrue(result);
    stop(clusterName);
    System.out.println("END " + clusterName + " at " + new Date(System.currentTimeMillis()));
}
Also used : Builder(org.apache.helix.PropertyKey.Builder) ClusterStateVerifier(org.apache.helix.tools.ClusterStateVerifier) Date(java.util.Date) ZNRecord(org.apache.helix.ZNRecord) IdealState(org.apache.helix.model.IdealState) ZKHelixDataAccessor(org.apache.helix.manager.zk.ZKHelixDataAccessor) Test(org.testng.annotations.Test)

Example 89 with ZKHelixDataAccessor

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

the class TestResourceWithSamePartitionKey method test.

@Test
public void test() throws Exception {
    String className = TestHelper.getTestClassName();
    String methodName = TestHelper.getTestMethodName();
    String clusterName = className + "_" + methodName;
    int n = 2;
    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
    2, // number of nodes
    n, // replicas
    2, "OnlineOffline", RebalanceMode.CUSTOMIZED, // do rebalance
    false);
    HelixDataAccessor accessor = new ZKHelixDataAccessor(clusterName, new ZkBaseDataAccessor<ZNRecord>(_gZkClient));
    PropertyKey.Builder keyBuilder = accessor.keyBuilder();
    IdealState idealState = accessor.getProperty(keyBuilder.idealStates("TestDB0"));
    idealState.setReplicas("2");
    idealState.setPartitionState("0", "localhost_12918", "ONLINE");
    idealState.setPartitionState("0", "localhost_12919", "ONLINE");
    idealState.setPartitionState("1", "localhost_12918", "ONLINE");
    idealState.setPartitionState("1", "localhost_12919", "ONLINE");
    accessor.setProperty(keyBuilder.idealStates("TestDB0"), idealState);
    ClusterControllerManager controller = new ClusterControllerManager(ZK_ADDR, clusterName, "controller_0");
    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);
    // add a second resource with the same partition-key
    IdealState newIdealState = new IdealState("TestDB1");
    newIdealState.getRecord().setSimpleFields(idealState.getRecord().getSimpleFields());
    newIdealState.setPartitionState("0", "localhost_12918", "ONLINE");
    newIdealState.setPartitionState("0", "localhost_12919", "ONLINE");
    newIdealState.setPartitionState("1", "localhost_12918", "ONLINE");
    newIdealState.setPartitionState("1", "localhost_12919", "ONLINE");
    accessor.setProperty(keyBuilder.idealStates("TestDB1"), newIdealState);
    result = ClusterStateVerifier.verifyByZkCallback(new ClusterStateVerifier.BestPossAndExtViewZkVerifier(ZK_ADDR, clusterName));
    Assert.assertTrue(result);
    // assert no ERROR
    for (int i = 0; i < n; i++) {
        String instanceName = "localhost_" + (12918 + i);
        List<String> errs = accessor.getChildNames(keyBuilder.errors(instanceName));
        Assert.assertTrue(errs.isEmpty());
    }
    // 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 : 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 90 with ZKHelixDataAccessor

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

the class TestClusterSetup method testDropInstance.

@Test
public void testDropInstance() throws Exception {
    // drop without stop, should throw 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
    10, // number of nodes
    5, // replicas
    3, "MasterSlave", // do rebalance
    true);
    // add fake liveInstance
    ZKHelixDataAccessor accessor = new ZKHelixDataAccessor(clusterName, new ZkBaseDataAccessor<ZNRecord>(_gZkClient));
    Builder keyBuilder = new Builder(clusterName);
    LiveInstance liveInstance = new LiveInstance("localhost_12918");
    liveInstance.setSessionId("session_0");
    liveInstance.setHelixVersion("version_0");
    accessor.setProperty(keyBuilder.liveInstance("localhost_12918"), liveInstance);
    // drop without stop the process, should throw exception
    try {
        ClusterSetup.processCommandLineArgs(new String[] { "--zkSvr", ZK_ADDR, "--dropNode", clusterName, "localhost:12918" });
        Assert.fail("Should throw exception since localhost_12918 is still in LIVEINSTANCES/");
    } catch (Exception e) {
    // OK
    }
    accessor.removeProperty(keyBuilder.liveInstance("localhost_12918"));
    // drop without disable, should throw exception
    try {
        ClusterSetup.processCommandLineArgs(new String[] { "--zkSvr", ZK_ADDR, "--dropNode", clusterName, "localhost:12918" });
        Assert.fail("Should throw exception since localhost_12918 is enabled");
    } catch (Exception e) {
    // e.printStackTrace();
    // OK
    }
    // drop it
    ClusterSetup.processCommandLineArgs(new String[] { "--zkSvr", ZK_ADDR, "--enableInstance", clusterName, "localhost_12918", "false" });
    ClusterSetup.processCommandLineArgs(new String[] { "--zkSvr", ZK_ADDR, "--dropNode", clusterName, "localhost:12918" });
    Assert.assertNull(accessor.getProperty(keyBuilder.instanceConfig("localhost_12918")), "Instance config should be dropped");
    Assert.assertFalse(_gZkClient.exists(PropertyPathBuilder.instance(clusterName, "localhost_12918")), "Instance/host should be dropped");
    System.out.println("END " + clusterName + " at " + new Date(System.currentTimeMillis()));
}
Also used : LiveInstance(org.apache.helix.model.LiveInstance) Builder(org.apache.helix.PropertyKey.Builder) PropertyPathBuilder(org.apache.helix.PropertyPathBuilder) Date(java.util.Date) ZNRecord(org.apache.helix.ZNRecord) HelixException(org.apache.helix.HelixException) IOException(java.io.IOException) ZKHelixDataAccessor(org.apache.helix.manager.zk.ZKHelixDataAccessor) Test(org.testng.annotations.Test)

Aggregations

ZKHelixDataAccessor (org.apache.helix.manager.zk.ZKHelixDataAccessor)104 ZNRecord (org.apache.helix.ZNRecord)78 Date (java.util.Date)66 Test (org.testng.annotations.Test)66 Builder (org.apache.helix.PropertyKey.Builder)47 MockParticipantManager (org.apache.helix.integration.manager.MockParticipantManager)41 HelixDataAccessor (org.apache.helix.HelixDataAccessor)38 ClusterControllerManager (org.apache.helix.integration.manager.ClusterControllerManager)37 IdealState (org.apache.helix.model.IdealState)33 ZkBaseDataAccessor (org.apache.helix.manager.zk.ZkBaseDataAccessor)30 PropertyKey (org.apache.helix.PropertyKey)29 ExternalView (org.apache.helix.model.ExternalView)21 BestPossAndExtViewZkVerifier (org.apache.helix.tools.ClusterStateVerifier.BestPossAndExtViewZkVerifier)21 LiveInstance (org.apache.helix.model.LiveInstance)20 ClusterStateVerifier (org.apache.helix.tools.ClusterStateVerifier)16 HelixManager (org.apache.helix.HelixManager)15 Message (org.apache.helix.model.Message)12 ZKHelixAdmin (org.apache.helix.manager.zk.ZKHelixAdmin)11 HashMap (java.util.HashMap)10 ZkClient (org.apache.helix.manager.zk.ZkClient)10