Search in sources :

Example 96 with IdealState

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

the class TestSchemataSM method testSchemataSM.

@Test
public void testSchemataSM() throws Exception {
    String className = TestHelper.getTestClassName();
    String methodName = TestHelper.getTestMethodName();
    String clusterName = className + "_" + methodName;
    int n = 5;
    MockParticipantManager[] participants = new MockParticipantManager[n];
    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
    "TestSchemata", // resources
    1, // partitions per resource
    1, // number of nodes
    n, // replicas
    0, "STORAGE_DEFAULT_SM_SCHEMATA", // don't rebalance
    false);
    // rebalance ideal-state to use ANY_LIVEINSTANCE for preference list
    ZKHelixDataAccessor accessor = new ZKHelixDataAccessor(clusterName, new ZkBaseDataAccessor<ZNRecord>(_gZkClient));
    PropertyKey.Builder keyBuilder = accessor.keyBuilder();
    PropertyKey key = keyBuilder.idealStates("TestSchemata0");
    IdealState idealState = accessor.getProperty(key);
    idealState.setReplicas(IdealState.IdealStateConstants.ANY_LIVEINSTANCE.toString());
    idealState.getRecord().setListField("TestSchemata0_0", Arrays.asList(IdealState.IdealStateConstants.ANY_LIVEINSTANCE.toString()));
    accessor.setProperty(key, idealState);
    ClusterControllerManager controller = new ClusterControllerManager(ZK_ADDR, clusterName, "controller");
    controller.syncStart();
    // start n-1 participants
    for (int i = 1; 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);
    // start the remaining 1 participant
    participants[0] = new MockParticipantManager(ZK_ADDR, clusterName, "localhost_12918");
    participants[0].syncStart();
    // make sure we have all participants in MASTER state
    result = ClusterStateVerifier.verifyByZkCallback(new BestPossAndExtViewZkVerifier(ZK_ADDR, clusterName));
    Assert.assertTrue(result);
    key = keyBuilder.externalView("TestSchemata0");
    ExternalView externalView = accessor.getProperty(key);
    Map<String, String> stateMap = externalView.getStateMap("TestSchemata0_0");
    Assert.assertNotNull(stateMap);
    Assert.assertEquals(stateMap.size(), n, "all " + n + " participants should be in Master state");
    for (int i = 0; i < n; i++) {
        String instanceName = "localhost_" + (12918 + i);
        Assert.assertNotNull(stateMap.get(instanceName));
        Assert.assertEquals(stateMap.get(instanceName), "MASTER");
    }
    // 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 : ExternalView(org.apache.helix.model.ExternalView) MockParticipantManager(org.apache.helix.integration.manager.MockParticipantManager) BestPossAndExtViewZkVerifier(org.apache.helix.tools.ClusterStateVerifier.BestPossAndExtViewZkVerifier) Date(java.util.Date) IdealState(org.apache.helix.model.IdealState) ClusterControllerManager(org.apache.helix.integration.manager.ClusterControllerManager) ZNRecord(org.apache.helix.ZNRecord) PropertyKey(org.apache.helix.PropertyKey) ZKHelixDataAccessor(org.apache.helix.manager.zk.ZKHelixDataAccessor) Test(org.testng.annotations.Test)

Example 97 with IdealState

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

the class TestResourceComputationStage method testSimple.

/**
 * Case where we have one resource in IdealState
 * @throws Exception
 */
@Test
public void testSimple() throws Exception {
    int nodes = 5;
    List<String> instances = new ArrayList<String>();
    for (int i = 0; i < nodes; i++) {
        instances.add("localhost_" + i);
    }
    int partitions = 10;
    int replicas = 1;
    String resourceName = "testResource";
    ZNRecord record = DefaultIdealStateCalculator.calculateIdealState(instances, partitions, replicas, resourceName, "MASTER", "SLAVE");
    IdealState idealState = new IdealState(record);
    idealState.setStateModelDefRef("MasterSlave");
    HelixDataAccessor accessor = manager.getHelixDataAccessor();
    Builder keyBuilder = accessor.keyBuilder();
    accessor.setProperty(keyBuilder.idealStates(resourceName), idealState);
    ResourceComputationStage stage = new ResourceComputationStage();
    runStage(event, new ReadClusterDataStage());
    runStage(event, stage);
    Map<String, Resource> resource = event.getAttribute(AttributeName.RESOURCES_TO_REBALANCE.name());
    AssertJUnit.assertEquals(1, resource.size());
    AssertJUnit.assertEquals(resource.keySet().iterator().next(), resourceName);
    AssertJUnit.assertEquals(resource.values().iterator().next().getResourceName(), resourceName);
    AssertJUnit.assertEquals(resource.values().iterator().next().getStateModelDefRef(), idealState.getStateModelDefRef());
    AssertJUnit.assertEquals(resource.values().iterator().next().getPartitions().size(), partitions);
}
Also used : HelixDataAccessor(org.apache.helix.HelixDataAccessor) Builder(org.apache.helix.PropertyKey.Builder) ArrayList(java.util.ArrayList) Resource(org.apache.helix.model.Resource) ZNRecord(org.apache.helix.ZNRecord) IdealState(org.apache.helix.model.IdealState) Test(org.testng.annotations.Test)

Example 98 with IdealState

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

the class TestResourceComputationStage method testMultipleResources.

@Test
public void testMultipleResources() throws Exception {
    // List<IdealState> idealStates = new ArrayList<IdealState>();
    String[] resources = new String[] { "testResource1", "testResource2" };
    List<IdealState> idealStates = setupIdealState(5, resources, 10, 1, RebalanceMode.SEMI_AUTO);
    ResourceComputationStage stage = new ResourceComputationStage();
    runStage(event, new ReadClusterDataStage());
    runStage(event, stage);
    Map<String, Resource> resourceMap = event.getAttribute(AttributeName.RESOURCES_TO_REBALANCE.name());
    AssertJUnit.assertEquals(resources.length, resourceMap.size());
    for (int i = 0; i < resources.length; i++) {
        String resourceName = resources[i];
        IdealState idealState = idealStates.get(i);
        AssertJUnit.assertTrue(resourceMap.containsKey(resourceName));
        AssertJUnit.assertEquals(resourceMap.get(resourceName).getResourceName(), resourceName);
        AssertJUnit.assertEquals(resourceMap.get(resourceName).getStateModelDefRef(), idealState.getStateModelDefRef());
        AssertJUnit.assertEquals(resourceMap.get(resourceName).getPartitions().size(), idealState.getNumPartitions());
    }
}
Also used : Resource(org.apache.helix.model.Resource) IdealState(org.apache.helix.model.IdealState) Test(org.testng.annotations.Test)

Example 99 with IdealState

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

the class TestResourceValidationStage method createIS.

private void createIS(HelixDataAccessor accessor, String resourceId, String stateModelDefRef, RebalanceMode rebalanceMode) {
    IdealState idealState = new IdealState(resourceId);
    idealState.setRebalanceMode(rebalanceMode);
    idealState.setStateModelDefRef(stateModelDefRef);
    idealState.setNumPartitions(1);
    idealState.setReplicas("1");
    idealState.getRecord().setListField(resourceId + "_0", ImmutableList.of(PARTICIPANT));
    idealState.getRecord().setMapField(resourceId + "_0", ImmutableMap.of(PARTICIPANT, STATE));
    accessor.setProperty(accessor.keyBuilder().idealStates(resourceId), idealState);
}
Also used : IdealState(org.apache.helix.model.IdealState)

Example 100 with IdealState

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

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