Search in sources :

Example 51 with LiveInstance

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

the class ZkUnitTestBase method setupLiveInstances.

protected void setupLiveInstances(String clusterName, int[] liveInstances) {
    ZKHelixDataAccessor accessor = new ZKHelixDataAccessor(clusterName, new ZkBaseDataAccessor<ZNRecord>(_gZkClient));
    Builder keyBuilder = accessor.keyBuilder();
    for (int i = 0; i < liveInstances.length; i++) {
        String instance = "localhost_" + liveInstances[i];
        LiveInstance liveInstance = new LiveInstance(instance);
        liveInstance.setSessionId("session_" + liveInstances[i]);
        liveInstance.setHelixVersion("0.0.0");
        accessor.setProperty(keyBuilder.liveInstance(instance), liveInstance);
    }
}
Also used : LiveInstance(org.apache.helix.model.LiveInstance) Builder(org.apache.helix.PropertyKey.Builder) ZKHelixDataAccessor(org.apache.helix.manager.zk.ZKHelixDataAccessor)

Example 52 with LiveInstance

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

the class BaseStageTest method setupLiveInstances.

protected List<String> setupLiveInstances(int numLiveInstances) {
    List<String> instances = new ArrayList<>();
    for (int i = 0; i < numLiveInstances; i++) {
        LiveInstance liveInstance = new LiveInstance(HOSTNAME_PREFIX + i);
        liveInstance.setSessionId(SESSION_PREFIX + i);
        Builder keyBuilder = accessor.keyBuilder();
        accessor.setProperty(keyBuilder.liveInstance(HOSTNAME_PREFIX + i), liveInstance);
        instances.add(liveInstance.getInstanceName());
    }
    return instances;
}
Also used : LiveInstance(org.apache.helix.model.LiveInstance) Builder(org.apache.helix.PropertyKey.Builder) ArrayList(java.util.ArrayList)

Example 53 with LiveInstance

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

the class TestResourceComputationStage method testMultipleResourcesWithSomeDropped.

@Test
public void testMultipleResourcesWithSomeDropped() throws Exception {
    int nodes = 5;
    List<String> instances = new ArrayList<String>();
    for (int i = 0; i < nodes; i++) {
        instances.add("localhost_" + i);
    }
    String[] resources = new String[] { "testResource1", "testResource2" };
    List<IdealState> idealStates = new ArrayList<IdealState>();
    for (int i = 0; i < resources.length; i++) {
        int partitions = 10;
        int replicas = 1;
        String resourceName = resources[i];
        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);
        idealStates.add(idealState);
    }
    // ADD A LIVE INSTANCE WITH A CURRENT STATE THAT CONTAINS RESOURCE WHICH NO
    // LONGER EXISTS IN IDEALSTATE
    String instanceName = "localhost_" + 3;
    LiveInstance liveInstance = new LiveInstance(instanceName);
    String sessionId = UUID.randomUUID().toString();
    liveInstance.setSessionId(sessionId);
    HelixDataAccessor accessor = manager.getHelixDataAccessor();
    Builder keyBuilder = accessor.keyBuilder();
    accessor.setProperty(keyBuilder.liveInstance(instanceName), liveInstance);
    String oldResource = "testResourceOld";
    CurrentState currentState = new CurrentState(oldResource);
    currentState.setState("testResourceOld_0", "OFFLINE");
    currentState.setState("testResourceOld_1", "SLAVE");
    currentState.setState("testResourceOld_2", "MASTER");
    currentState.setStateModelDefRef("MasterSlave");
    accessor.setProperty(keyBuilder.currentState(instanceName, sessionId, oldResource), currentState);
    ResourceComputationStage stage = new ResourceComputationStage();
    runStage(event, new ReadClusterDataStage());
    runStage(event, stage);
    Map<String, Resource> resourceMap = event.getAttribute(AttributeName.RESOURCES.name());
    // +1 because it will have one for current state
    AssertJUnit.assertEquals(resources.length + 1, 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());
    }
    // Test the data derived from CurrentState
    AssertJUnit.assertTrue(resourceMap.containsKey(oldResource));
    AssertJUnit.assertEquals(resourceMap.get(oldResource).getResourceName(), oldResource);
    AssertJUnit.assertEquals(resourceMap.get(oldResource).getStateModelDefRef(), currentState.getStateModelDefRef());
    AssertJUnit.assertEquals(resourceMap.get(oldResource).getPartitions().size(), currentState.getPartitionStateMap().size());
    AssertJUnit.assertNotNull(resourceMap.get(oldResource).getPartition("testResourceOld_0"));
    AssertJUnit.assertNotNull(resourceMap.get(oldResource).getPartition("testResourceOld_1"));
    AssertJUnit.assertNotNull(resourceMap.get(oldResource).getPartition("testResourceOld_2"));
}
Also used : Builder(org.apache.helix.PropertyKey.Builder) ArrayList(java.util.ArrayList) Resource(org.apache.helix.model.Resource) IdealState(org.apache.helix.model.IdealState) HelixDataAccessor(org.apache.helix.HelixDataAccessor) LiveInstance(org.apache.helix.model.LiveInstance) CurrentState(org.apache.helix.model.CurrentState) ZNRecord(org.apache.helix.ZNRecord) Test(org.testng.annotations.Test)

Example 54 with LiveInstance

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

the class TestStandAloneCMMain method testStandAloneCMMain.

@Test()
public void testStandAloneCMMain() throws Exception {
    logger.info("RUN testStandAloneCMMain() at " + new Date(System.currentTimeMillis()));
    ClusterControllerManager newController = null;
    for (int i = 1; i <= 2; i++) {
        String controllerName = "controller_" + i;
        newController = new ClusterControllerManager(ZK_ADDR, CLUSTER_NAME, controllerName);
        newController.syncStart();
    }
    // stopCurrentLeader(_zkClient, CLUSTER_NAME, _startCMResultMap);
    _controller.syncStop();
    final HelixDataAccessor accessor = new ZKHelixDataAccessor(CLUSTER_NAME, new ZkBaseDataAccessor<ZNRecord>(_gZkClient));
    final PropertyKey.Builder keyBuilder = accessor.keyBuilder();
    final String newControllerName = newController.getInstanceName();
    TestHelper.verify(new Verifier() {

        @Override
        public boolean verify() throws Exception {
            LiveInstance leader = accessor.getProperty(keyBuilder.controllerLeader());
            if (leader == null) {
                return false;
            }
            return leader.getInstanceName().equals(newControllerName);
        }
    }, 30 * 1000);
    boolean result = ClusterStateVerifier.verifyByPolling(new ClusterStateVerifier.BestPossAndExtViewZkVerifier(ZK_ADDR, CLUSTER_NAME));
    Assert.assertTrue(result);
    logger.info("STOP testStandAloneCMMain() at " + new Date(System.currentTimeMillis()));
}
Also used : ClusterStateVerifier(org.apache.helix.tools.ClusterStateVerifier) Verifier(org.apache.helix.TestHelper.Verifier) 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) PropertyKey(org.apache.helix.PropertyKey) ZKHelixDataAccessor(org.apache.helix.manager.zk.ZKHelixDataAccessor) Test(org.testng.annotations.Test)

Example 55 with LiveInstance

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

the class ZkIntegrationTestBase method getCurrentLeader.

protected String getCurrentLeader(ZkClient zkClient, String clusterName) {
    ZKHelixDataAccessor accessor = new ZKHelixDataAccessor(clusterName, new ZkBaseDataAccessor<ZNRecord>(zkClient));
    Builder keyBuilder = accessor.keyBuilder();
    LiveInstance leader = accessor.getProperty(keyBuilder.controllerLeader());
    if (leader == null) {
        return null;
    }
    return leader.getInstanceName();
}
Also used : LiveInstance(org.apache.helix.model.LiveInstance) ConfigScopeBuilder(org.apache.helix.model.builder.ConfigScopeBuilder) Builder(org.apache.helix.PropertyKey.Builder) ZNRecord(org.apache.helix.ZNRecord) ZKHelixDataAccessor(org.apache.helix.manager.zk.ZKHelixDataAccessor)

Aggregations

LiveInstance (org.apache.helix.model.LiveInstance)74 HelixDataAccessor (org.apache.helix.HelixDataAccessor)31 ZNRecord (org.apache.helix.ZNRecord)28 Builder (org.apache.helix.PropertyKey.Builder)25 Test (org.testng.annotations.Test)25 Date (java.util.Date)20 ZKHelixDataAccessor (org.apache.helix.manager.zk.ZKHelixDataAccessor)20 PropertyKey (org.apache.helix.PropertyKey)19 HashMap (java.util.HashMap)18 ArrayList (java.util.ArrayList)17 CurrentState (org.apache.helix.model.CurrentState)14 Message (org.apache.helix.model.Message)13 MockParticipantManager (org.apache.helix.integration.manager.MockParticipantManager)11 HelixManager (org.apache.helix.HelixManager)10 IdealState (org.apache.helix.model.IdealState)9 BestPossAndExtViewZkVerifier (org.apache.helix.tools.ClusterStateVerifier.BestPossAndExtViewZkVerifier)9 HelixException (org.apache.helix.HelixException)8 InstanceConfig (org.apache.helix.model.InstanceConfig)7 ClusterStateVerifier (org.apache.helix.tools.ClusterStateVerifier)7 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)6