Search in sources :

Example 86 with IdealState

use of org.apache.helix.model.IdealState in project helix by apache.

the class TestAbstractRebalancer method testComputeBestPossibleState.

@Test(dataProvider = "TestComputeBestPossibleStateInput")
public void testComputeBestPossibleState(String comment, String stateModelName, List<String> liveInstances, List<String> preferenceList, Map<String, String> currentStateMap, List<String> disabledInstancesForPartition, Map<String, String> expectedBestPossibleMap) {
    System.out.println("Test case comment: " + comment);
    AutoRebalancer rebalancer = new AutoRebalancer();
    Map<String, String> bestPossibleMap = rebalancer.computeBestPossibleStateForPartition(new HashSet<String>(liveInstances), BuiltInStateModelDefinitions.valueOf(stateModelName).getStateModelDefinition(), preferenceList, currentStateMap, new HashSet<String>(disabledInstancesForPartition), new IdealState("test"));
    Assert.assertTrue(bestPossibleMap.equals(expectedBestPossibleMap));
}
Also used : IdealState(org.apache.helix.model.IdealState) Test(org.testng.annotations.Test)

Example 87 with IdealState

use of org.apache.helix.model.IdealState in project helix by apache.

the class TestZeroReplicaAvoidance method testZeroReplicaAvoidanceDuringRebalance.

@Test(dataProvider = "zeroReplicaInput")
public void testZeroReplicaAvoidanceDuringRebalance(StateModelDefinition stateModelDef, List<String> instancePreferenceList, Map<String, String> currentStateMap, Map<String, String> expectedBestPossibleMap) {
    System.out.println("START TestDelayedAutoRebalancer at " + new Date(System.currentTimeMillis()));
    System.err.println("Test input: " + instancePreferenceList + ":" + currentStateMap + ":");
    int numNode = 6;
    Set<String> liveInstances = new HashSet<String>();
    for (int i = 0; i < numNode; i++) {
        liveInstances.add("localhost_" + i);
    }
    IdealState is = new IdealState("test");
    is.setReplicas("3");
    DelayedAutoRebalancer rebalancer = new DelayedAutoRebalancer();
    Map<String, String> bestPossibleMap = rebalancer.computeBestPossibleStateForPartition(liveInstances, stateModelDef, instancePreferenceList, currentStateMap, Collections.<String>emptySet(), is);
    Assert.assertEquals(bestPossibleMap, expectedBestPossibleMap, "Differs, get " + bestPossibleMap + "\nexpected: " + expectedBestPossibleMap + "\ncurrentState: " + currentStateMap + "\npreferenceList: " + instancePreferenceList);
    System.out.println("END TestBestPossibleStateCalcStage at " + new Date(System.currentTimeMillis()));
}
Also used : Date(java.util.Date) IdealState(org.apache.helix.model.IdealState) HashSet(java.util.HashSet) Test(org.testng.annotations.Test) BaseStageTest(org.apache.helix.controller.stages.BaseStageTest)

Example 88 with IdealState

use of org.apache.helix.model.IdealState in project helix by apache.

the class TestDisable method testDisablePartitionCustomIS.

@Test
public void testDisablePartitionCustomIS() 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
    8, // number of nodes
    n, // replicas
    3, "MasterSlave", // do rebalance
    true);
    // set ideal state to customized mode
    ZkBaseDataAccessor<ZNRecord> baseAccessor = new ZkBaseDataAccessor<ZNRecord>(_gZkClient);
    ZKHelixDataAccessor accessor = new ZKHelixDataAccessor(clusterName, baseAccessor);
    Builder keyBuilder = accessor.keyBuilder();
    IdealState idealState = accessor.getProperty(keyBuilder.idealStates("TestDB0"));
    idealState.setRebalanceMode(RebalanceMode.CUSTOMIZED);
    accessor.setProperty(keyBuilder.idealStates("TestDB0"), idealState);
    // start controller
    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);
    // disable [TestDB0_0, TestDB0_5] on localhost_12919
    String command = "--zkSvr " + ZK_ADDR + " --enablePartition false " + clusterName + " localhost_12919 TestDB0 TestDB0_0 TestDB0_5";
    ClusterSetup.processCommandLineArgs(command.split("\\s+"));
    result = ClusterStateVerifier.verifyByZkCallback(new BestPossAndExtViewZkVerifier(ZK_ADDR, clusterName));
    Assert.assertTrue(result);
    // make sure localhost_12918 is in OFFLINE state for [TestDB0_0, TestDB0_5]
    Map<String, Map<String, String>> expectStateMap = new HashMap<String, Map<String, String>>();
    Map<String, String> expectInstanceStateMap = new HashMap<String, String>();
    expectInstanceStateMap.put("localhost_12919", "OFFLINE");
    expectStateMap.put("TestDB0_0", expectInstanceStateMap);
    expectStateMap.put("TestDB0_5", expectInstanceStateMap);
    result = ZkTestHelper.verifyState(_gZkClient, clusterName, "TestDB0", expectStateMap, "==");
    Assert.assertTrue(result, "localhost_12919" + " should be in OFFLINE for [TestDB0_0, TestDB0_5]");
    // re-enable localhost_12919 for [TestDB0_0, TestDB0_5]
    command = "--zkSvr " + ZK_ADDR + " --enablePartition true " + clusterName + " localhost_12919 TestDB0 TestDB0_0 TestDB0_5";
    ClusterSetup.processCommandLineArgs(command.split("\\s+"));
    result = ClusterStateVerifier.verifyByZkCallback(new BestPossAndExtViewZkVerifier(ZK_ADDR, clusterName));
    Assert.assertTrue(result);
    // make sure localhost_12919 is NOT in OFFLINE state for [TestDB0_0, TestDB0_5]
    result = ZkTestHelper.verifyState(_gZkClient, clusterName, "TestDB0", expectStateMap, "!=");
    Assert.assertTrue(result, "localhost_12919" + " should NOT be in OFFLINE");
    // 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) HashMap(java.util.HashMap) Builder(org.apache.helix.PropertyKey.Builder) BestPossAndExtViewZkVerifier(org.apache.helix.tools.ClusterStateVerifier.BestPossAndExtViewZkVerifier) Date(java.util.Date) IdealState(org.apache.helix.model.IdealState) ClusterControllerManager(org.apache.helix.integration.manager.ClusterControllerManager) HashMap(java.util.HashMap) Map(java.util.Map) ZNRecord(org.apache.helix.ZNRecord) ZKHelixDataAccessor(org.apache.helix.manager.zk.ZKHelixDataAccessor) Test(org.testng.annotations.Test)

Example 89 with IdealState

use of org.apache.helix.model.IdealState 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 90 with IdealState

use of org.apache.helix.model.IdealState in project helix by apache.

the class TestPreferenceListAsQueue method runTest.

private void runTest() throws Exception {
    final int NUM_PARTITIONS = 1;
    final int NUM_REPLICAS = 2;
    final int NUM_INSTANCES = 2;
    final String RESOURCE_NAME = "MyResource";
    // Setup instances
    String[] instanceInfoArray = { "localhost_1", "localhost_2" };
    _clusterSetup.addInstancesToCluster(_clusterName, instanceInfoArray);
    // Add resource
    _clusterSetup.addResourceToCluster(_clusterName, RESOURCE_NAME, NUM_PARTITIONS, _stateModel, RebalanceMode.SEMI_AUTO.toString());
    // Update resource with empty preference lists
    IdealState idealState = _admin.getResourceIdealState(_clusterName, RESOURCE_NAME);
    for (int i = 0; i < NUM_PARTITIONS; i++) {
        String partitionName = RESOURCE_NAME + "_" + i;
        List<String> dummyPreferences = Lists.newArrayList();
        for (int j = 0; j < NUM_REPLICAS; j++) {
            // hack: need to have some dummy values in the preference list to pass validation
            dummyPreferences.add("");
        }
        idealState.getRecord().setListField(partitionName, dummyPreferences);
    }
    idealState.setReplicas(String.valueOf(NUM_REPLICAS));
    _admin.setResourceIdealState(_clusterName, RESOURCE_NAME, idealState);
    // Start some instances
    HelixManager[] participants = new HelixManager[NUM_INSTANCES];
    for (int i = 0; i < NUM_INSTANCES; i++) {
        participants[i] = HelixManagerFactory.getZKHelixManager(_clusterName, instanceInfoArray[i], InstanceType.PARTICIPANT, ZK_ADDR);
        participants[i].getStateMachineEngine().registerStateModelFactory(_stateModel, new PrefListTaskOnlineOfflineStateModelFactory());
        participants[i].connect();
    }
    // Start the controller
    HelixManager controller = HelixManagerFactory.getZKHelixManager(_clusterName, null, InstanceType.CONTROLLER, ZK_ADDR);
    controller.connect();
    // Disable controller immediately
    _admin.enableCluster(_clusterName, false);
    // This resource only has 1 partition
    String partitionName = RESOURCE_NAME + "_" + 0;
    // There should be no preference lists yet
    Assert.assertTrue(preferenceListIsCorrect(_admin, _clusterName, RESOURCE_NAME, partitionName, Arrays.asList("", "")));
    // Add one instance
    addInstanceToPreferences(participants[0].getHelixDataAccessor(), participants[0].getInstanceName(), RESOURCE_NAME, Arrays.asList(partitionName));
    Assert.assertTrue(preferenceListIsCorrect(_admin, _clusterName, RESOURCE_NAME, partitionName, Arrays.asList("localhost_1", "")));
    // Add a second instance immediately; the first one should still exist
    addInstanceToPreferences(participants[1].getHelixDataAccessor(), participants[1].getInstanceName(), RESOURCE_NAME, Arrays.asList(partitionName));
    Assert.assertTrue(preferenceListIsCorrect(_admin, _clusterName, RESOURCE_NAME, partitionName, Arrays.asList("localhost_1", "localhost_2")));
    // Add the first instance again; it should already exist
    addInstanceToPreferences(participants[0].getHelixDataAccessor(), participants[0].getInstanceName(), RESOURCE_NAME, Arrays.asList(partitionName));
    Assert.assertTrue(preferenceListIsCorrect(_admin, _clusterName, RESOURCE_NAME, partitionName, Arrays.asList("localhost_1", "localhost_2")));
    // Prepare for synchronization
    _onlineLatch = new CountDownLatch(2);
    _offlineLatch = new CountDownLatch(2);
    _prefListHistory.clear();
    // Now reenable the controller
    _admin.enableCluster(_clusterName, true);
    // Now wait for both instances to be done
    boolean countReached = _onlineLatch.await(10000, TimeUnit.MILLISECONDS);
    Assert.assertTrue(countReached);
    List<String> top = _prefListHistory.poll();
    Assert.assertTrue(top.equals(Arrays.asList("localhost_1", "")) || top.equals(Arrays.asList("localhost_2", "")));
    Assert.assertEquals(_prefListHistory.poll(), Arrays.asList("", ""));
    // Wait for everything to be fully offline
    countReached = _offlineLatch.await(10000, TimeUnit.MILLISECONDS);
    Assert.assertTrue(countReached);
    // Add back the instances in the opposite order
    _admin.enableCluster(_clusterName, false);
    addInstanceToPreferences(participants[0].getHelixDataAccessor(), participants[1].getInstanceName(), RESOURCE_NAME, Arrays.asList(partitionName));
    addInstanceToPreferences(participants[0].getHelixDataAccessor(), participants[0].getInstanceName(), RESOURCE_NAME, Arrays.asList(partitionName));
    Assert.assertTrue(preferenceListIsCorrect(_admin, _clusterName, RESOURCE_NAME, partitionName, Arrays.asList("localhost_2", "localhost_1")));
    // Reset the latch
    _onlineLatch = new CountDownLatch(2);
    _prefListHistory.clear();
    _admin.enableCluster(_clusterName, true);
    // Now wait both to be done again
    countReached = _onlineLatch.await(10000, TimeUnit.MILLISECONDS);
    Assert.assertTrue(countReached);
    top = _prefListHistory.poll();
    Assert.assertTrue(top.equals(Arrays.asList("localhost_1", "")) || top.equals(Arrays.asList("localhost_2", "")));
    Assert.assertEquals(_prefListHistory.poll(), Arrays.asList("", ""));
    Assert.assertEquals(_instanceList.size(), 0);
    // Cleanup
    controller.disconnect();
    for (HelixManager participant : participants) {
        participant.disconnect();
    }
}
Also used : HelixManager(org.apache.helix.HelixManager) CountDownLatch(java.util.concurrent.CountDownLatch) IdealState(org.apache.helix.model.IdealState)

Aggregations

IdealState (org.apache.helix.model.IdealState)250 Test (org.testng.annotations.Test)110 ZNRecord (org.apache.helix.ZNRecord)69 ExternalView (org.apache.helix.model.ExternalView)54 ArrayList (java.util.ArrayList)50 HelixAdmin (org.apache.helix.HelixAdmin)42 HashMap (java.util.HashMap)38 MockParticipantManager (org.apache.helix.integration.manager.MockParticipantManager)35 Date (java.util.Date)33 HelixDataAccessor (org.apache.helix.HelixDataAccessor)33 Builder (org.apache.helix.PropertyKey.Builder)33 ZKHelixDataAccessor (org.apache.helix.manager.zk.ZKHelixDataAccessor)33 HashSet (java.util.HashSet)31 ZKHelixAdmin (org.apache.helix.manager.zk.ZKHelixAdmin)28 Map (java.util.Map)26 PropertyKey (org.apache.helix.PropertyKey)26 ClusterControllerManager (org.apache.helix.integration.manager.ClusterControllerManager)24 StateModelDefinition (org.apache.helix.model.StateModelDefinition)23 List (java.util.List)21 HelixException (org.apache.helix.HelixException)21