Search in sources :

Example 21 with HelixDataAccessor

use of org.apache.helix.HelixDataAccessor in project helix by apache.

the class TestResourceAccessor method createDummyMapping.

/**
 * Creates a setup where the health API can be tested.
 *
 * @param clusterName
 * @param resourceName
 * @param idealStateParams
 * @param partitionReplicaStates maps partitionName to its replicas' states
 * @throws Exception
 */
private void createDummyMapping(String clusterName, String resourceName, Map<String, String> idealStateParams, Map<String, List<String>> partitionReplicaStates) throws Exception {
    IdealState idealState = new IdealState(resourceName);
    // 2
    idealState.setMinActiveReplicas(Integer.parseInt(idealStateParams.get("MinActiveReplicas")));
    // MasterSlave
    idealState.setStateModelDefRef(idealStateParams.get("StateModelDefRef"));
    // 3
    idealState.setMaxPartitionsPerInstance(Integer.parseInt(idealStateParams.get("MaxPartitionsPerInstance")));
    // 3
    idealState.setReplicas(idealStateParams.get("Replicas"));
    // 3
    idealState.setNumPartitions(Integer.parseInt(idealStateParams.get("NumPartitions")));
    idealState.enable(false);
    Map<String, List<String>> partitionNames = new LinkedHashMap<>();
    List<String> dummyPrefList = new ArrayList<>();
    for (int i = 0; i < Integer.parseInt(idealStateParams.get("MaxPartitionsPerInstance")); i++) {
        dummyPrefList.add(ANY_INSTANCE);
        partitionNames.put("p" + i, dummyPrefList);
    }
    idealState.getRecord().getListFields().putAll(partitionNames);
    if (!_gSetupTool.getClusterManagementTool().getClusters().contains(clusterName)) {
        _gSetupTool.getClusterManagementTool().addCluster(clusterName);
    }
    _gSetupTool.getClusterManagementTool().setResourceIdealState(clusterName, resourceName, idealState);
    // Set ExternalView's replica states for a given parameter map
    ExternalView externalView = new ExternalView(resourceName);
    Map<String, Map<String, String>> mappingCurrent = new LinkedHashMap<>();
    List<String> partitionReplicaStatesList = new ArrayList<>(partitionReplicaStates.keySet());
    for (int k = 0; k < partitionReplicaStatesList.size(); k++) {
        Map<String, String> replicaStatesForPartition = new LinkedHashMap<>();
        List<String> replicaStateList = partitionReplicaStates.get(partitionReplicaStatesList.get(k));
        for (int i = 0; i < replicaStateList.size(); i++) {
            replicaStatesForPartition.put("r" + i, replicaStateList.get(i));
        }
        mappingCurrent.put("p" + k, replicaStatesForPartition);
    }
    externalView.getRecord().getMapFields().putAll(mappingCurrent);
    HelixManager helixManager = HelixManagerFactory.getZKHelixManager(clusterName, "p1", InstanceType.ADMINISTRATOR, ZK_ADDR);
    helixManager.connect();
    HelixDataAccessor helixDataAccessor = helixManager.getHelixDataAccessor();
    helixDataAccessor.setProperty(helixDataAccessor.keyBuilder().externalView(resourceName), externalView);
}
Also used : ExternalView(org.apache.helix.model.ExternalView) HelixManager(org.apache.helix.HelixManager) ArrayList(java.util.ArrayList) IdealState(org.apache.helix.model.IdealState) LinkedHashMap(java.util.LinkedHashMap) HelixDataAccessor(org.apache.helix.HelixDataAccessor) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 22 with HelixDataAccessor

use of org.apache.helix.HelixDataAccessor in project helix by apache.

the class TestHelixAdminCli method testDropAddResource.

@Test
public void testDropAddResource() throws Exception {
    String className = TestHelper.getTestClassName();
    String methodName = TestHelper.getTestMethodName();
    String clusterName = className + "_" + methodName;
    String grandClusterName = clusterName + "_grand";
    final int n = 6;
    System.out.println("START " + clusterName + " at " + new Date(System.currentTimeMillis()));
    MockParticipantManager[] participants = new MockParticipantManager[n];
    ClusterDistributedController[] controllers = new ClusterDistributedController[2];
    setupCluster(clusterName, grandClusterName, n, participants, controllers);
    String command = "-zkSvr " + ZK_ADDR + " -activateCluster " + clusterName + " " + grandClusterName + " true";
    ClusterSetup.processCommandLineArgs(command.split("\\s+"));
    Thread.sleep(500);
    // save ideal state
    BaseDataAccessor<ZNRecord> baseAccessor = new ZkBaseDataAccessor<ZNRecord>(_gZkClient);
    HelixDataAccessor accessor = new ZKHelixDataAccessor(clusterName, baseAccessor);
    IdealState idealState = accessor.getProperty(accessor.keyBuilder().idealStates("db_11"));
    ZNRecordJsonSerializer serializer = new ZNRecordJsonSerializer();
    String tmpDir = System.getProperty("java.io.tmpdir");
    if (tmpDir == null) {
        tmpDir = "/tmp";
    }
    final String tmpIdealStateFile = tmpDir + "/" + clusterName + "_idealState.log";
    FileWriter fos = new FileWriter(tmpIdealStateFile);
    PrintWriter pw = new PrintWriter(fos);
    pw.write(new String(serializer.serialize(idealState.getRecord())));
    pw.close();
    command = "-zkSvr " + ZK_ADDR + " -dropResource " + clusterName + " db_11 ";
    ClusterSetup.processCommandLineArgs(command.split("\\s+"));
    boolean verifyResult = ClusterStateVerifier.verifyByZkCallback(new BestPossAndExtViewZkVerifier(ZK_ADDR, clusterName));
    Assert.assertTrue(verifyResult);
    command = "-zkSvr " + ZK_ADDR + " -addIdealState " + clusterName + " db_11 " + tmpIdealStateFile;
    ClusterSetup.processCommandLineArgs(command.split("\\s+"));
    verifyResult = ClusterStateVerifier.verifyByZkCallback(new BestPossAndExtViewZkVerifier(ZK_ADDR, clusterName));
    Assert.assertTrue(verifyResult);
    IdealState idealState2 = accessor.getProperty(accessor.keyBuilder().idealStates("db_11"));
    Assert.assertTrue(idealState2.getRecord().equals(idealState.getRecord()));
    // clean up
    for (int i = 0; i < controllers.length; i++) {
        controllers[i].syncStop();
    }
    for (int i = 0; i < participants.length; 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) ZNRecordJsonSerializer(org.apache.helix.store.ZNRecordJsonSerializer) FileWriter(java.io.FileWriter) BestPossAndExtViewZkVerifier(org.apache.helix.tools.ClusterStateVerifier.BestPossAndExtViewZkVerifier) Date(java.util.Date) IdealState(org.apache.helix.model.IdealState) ClusterDistributedController(org.apache.helix.integration.manager.ClusterDistributedController) ZKHelixDataAccessor(org.apache.helix.manager.zk.ZKHelixDataAccessor) HelixDataAccessor(org.apache.helix.HelixDataAccessor) ZNRecord(org.apache.helix.ZNRecord) ZKHelixDataAccessor(org.apache.helix.manager.zk.ZKHelixDataAccessor) PrintWriter(java.io.PrintWriter) Test(org.testng.annotations.Test)

Example 23 with HelixDataAccessor

use of org.apache.helix.HelixDataAccessor in project helix by apache.

the class TestHelixAdminCli method testInstanceOperations.

@Test
public void testInstanceOperations() throws Exception {
    String className = TestHelper.getTestClassName();
    String methodName = TestHelper.getTestMethodName();
    String clusterName = className + "_" + methodName;
    String grandClusterName = clusterName + "_grand";
    final int n = 6;
    System.out.println("START " + clusterName + " at " + new Date(System.currentTimeMillis()));
    MockParticipantManager[] participants = new MockParticipantManager[n];
    ClusterDistributedController[] controllers = new ClusterDistributedController[2];
    setupCluster(clusterName, grandClusterName, n, participants, controllers);
    String command = "-zkSvr " + ZK_ADDR + " -activateCluster " + clusterName + " " + grandClusterName + " true";
    ClusterSetup.processCommandLineArgs(command.split("\\s+"));
    Thread.sleep(500);
    // drop node should fail if the node is not disabled
    command = "-zkSvr " + ZK_ADDR + " -dropNode " + clusterName + " localhost:1232";
    try {
        ClusterSetup.processCommandLineArgs(command.split("\\s+"));
        Assert.fail("dropNode should fail since the node is not disabled");
    } catch (Exception e) {
    // OK
    }
    // disabled node
    command = "-zkSvr " + ZK_ADDR + " -enableInstance " + clusterName + " localhost:1232 false";
    ClusterSetup.processCommandLineArgs(command.split("\\s+"));
    // Cannot dropNode if the node is not disconnected
    command = "-zkSvr " + ZK_ADDR + " -dropNode " + clusterName + " localhost:1232";
    try {
        ClusterSetup.processCommandLineArgs(command.split("\\s+"));
        Assert.fail("dropNode should fail since the node is not disconnected");
    } catch (Exception e) {
    // OK
    }
    // Cannot swapNode if the node is not disconnected
    command = "-zkSvr " + ZK_ADDR + " -swapInstance " + clusterName + " localhost_1232 localhost_12320";
    try {
        ClusterSetup.processCommandLineArgs(command.split("\\s+"));
        Assert.fail("swapInstance should fail since the node is not disconnected");
    } catch (Exception e) {
    // OK
    }
    // disconnect localhost_1232
    participants[2].syncStop();
    // add new node then swap instance
    command = "-zkSvr " + ZK_ADDR + " -addNode " + clusterName + " localhost:12320";
    ClusterSetup.processCommandLineArgs(command.split("\\s+"));
    // swap instance. The instance get swapped out should not exist anymore
    command = "-zkSvr " + ZK_ADDR + " -swapInstance " + clusterName + " localhost_1232 localhost_12320";
    ClusterSetup.processCommandLineArgs(command.split("\\s+"));
    BaseDataAccessor<ZNRecord> baseAccessor = new ZkBaseDataAccessor<ZNRecord>(_gZkClient);
    HelixDataAccessor accessor = new ZKHelixDataAccessor(clusterName, baseAccessor);
    String path = accessor.keyBuilder().instanceConfig("localhost_1232").getPath();
    Assert.assertFalse(_gZkClient.exists(path), path + " should not exist since localhost_1232 has been swapped by localhost_12320");
    // clean up
    for (int i = 0; i < controllers.length; i++) {
        controllers[i].syncStop();
    }
    for (int i = 0; i < participants.length; 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) Date(java.util.Date) ClusterDistributedController(org.apache.helix.integration.manager.ClusterDistributedController) ZKHelixDataAccessor(org.apache.helix.manager.zk.ZKHelixDataAccessor) HelixDataAccessor(org.apache.helix.HelixDataAccessor) ZNRecord(org.apache.helix.ZNRecord) ZKHelixDataAccessor(org.apache.helix.manager.zk.ZKHelixDataAccessor) Test(org.testng.annotations.Test)

Example 24 with HelixDataAccessor

use of org.apache.helix.HelixDataAccessor in project helix by apache.

the class TestHelixAdminCli method testStartCluster.

@Test
public void testStartCluster() throws Exception {
    String className = TestHelper.getTestClassName();
    String methodName = TestHelper.getTestMethodName();
    String clusterName = className + "_" + methodName;
    String grandClusterName = clusterName + "_grand";
    final int n = 6;
    System.out.println("START " + clusterName + " at " + new Date(System.currentTimeMillis()));
    MockParticipantManager[] participants = new MockParticipantManager[n];
    ClusterDistributedController[] controllers = new ClusterDistributedController[2];
    setupCluster(clusterName, grandClusterName, n, participants, controllers);
    // activate clusters
    // wrong grand clusterName
    String command = "-zkSvr localhost:2183 -activateCluster " + clusterName + " nonExistGrandCluster true";
    try {
        ClusterSetup.processCommandLineArgs(command.split("\\s+"));
        Assert.fail("add " + clusterName + " to grandCluster should fail since grandCluster doesn't exists");
    } catch (Exception e) {
    // OK
    }
    // wrong cluster name
    command = "-zkSvr localhost:2183 -activateCluster nonExistCluster " + grandClusterName + " true";
    try {
        ClusterSetup.processCommandLineArgs(command.split("\\s+"));
        Assert.fail("add nonExistCluster to " + grandClusterName + " should fail since nonExistCluster doesn't exists");
    } catch (Exception e) {
    // OK
    }
    command = "-zkSvr localhost:2183 -activateCluster " + clusterName + " " + grandClusterName + " true";
    ClusterSetup.processCommandLineArgs(command.split("\\s+"));
    Thread.sleep(500);
    // drop a running cluster
    command = "-zkSvr localhost:2183 -dropCluster " + clusterName;
    try {
        ClusterSetup.processCommandLineArgs(command.split("\\s+"));
        Assert.fail("drop " + clusterName + " should fail since it's still running");
    } catch (Exception e) {
    // OK
    }
    // verify leader node
    BaseDataAccessor<ZNRecord> baseAccessor = new ZkBaseDataAccessor<ZNRecord>(_gZkClient);
    HelixDataAccessor accessor = new ZKHelixDataAccessor(grandClusterName, baseAccessor);
    LiveInstance controllerLeader = accessor.getProperty(accessor.keyBuilder().controllerLeader());
    Assert.assertNotNull(controllerLeader, "controllerLeader should be either controller_9000 or controller_9001");
    Assert.assertTrue(controllerLeader.getInstanceName().startsWith("controller_900"));
    accessor = new ZKHelixDataAccessor(clusterName, baseAccessor);
    LiveInstance leader = accessor.getProperty(accessor.keyBuilder().controllerLeader());
    for (int i = 0; i < 20; i++) {
        if (leader != null) {
            break;
        }
        Thread.sleep(200);
        leader = accessor.getProperty(accessor.keyBuilder().controllerLeader());
    }
    Assert.assertTrue(leader.getInstanceName().startsWith("controller_900"));
    boolean verifyResult = ClusterStateVerifier.verifyByZkCallback(new MasterNbInExtViewVerifier(ZK_ADDR, clusterName));
    Assert.assertTrue(verifyResult);
    verifyResult = ClusterStateVerifier.verifyByZkCallback(new BestPossAndExtViewZkVerifier(ZK_ADDR, clusterName));
    Assert.assertTrue(verifyResult);
    // clean up
    for (ClusterDistributedController controller : controllers) {
        controller.syncStop();
    }
    for (int i = 0; i < participants.length; i++) {
        participants[i].syncStop();
    }
    System.out.println("END " + clusterName + " at " + new Date(System.currentTimeMillis()));
}
Also used : ZkBaseDataAccessor(org.apache.helix.manager.zk.ZkBaseDataAccessor) MasterNbInExtViewVerifier(org.apache.helix.tools.ClusterStateVerifier.MasterNbInExtViewVerifier) MockParticipantManager(org.apache.helix.integration.manager.MockParticipantManager) BestPossAndExtViewZkVerifier(org.apache.helix.tools.ClusterStateVerifier.BestPossAndExtViewZkVerifier) Date(java.util.Date) ClusterDistributedController(org.apache.helix.integration.manager.ClusterDistributedController) 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 25 with HelixDataAccessor

use of org.apache.helix.HelixDataAccessor in project helix by apache.

the class ClusterAccessor method getClusterStateModelDefinition.

@GET
@Path("{clusterId}/statemodeldefs/{statemodel}")
public Response getClusterStateModelDefinition(@PathParam("clusterId") String clusterId, @PathParam("statemodel") String statemodel) {
    HelixDataAccessor dataAccessor = getDataAccssor(clusterId);
    StateModelDefinition stateModelDef = dataAccessor.getProperty(dataAccessor.keyBuilder().stateModelDef(statemodel));
    return JSONRepresentation(stateModelDef.getRecord());
}
Also used : HelixDataAccessor(org.apache.helix.HelixDataAccessor) StateModelDefinition(org.apache.helix.model.StateModelDefinition) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET)

Aggregations

HelixDataAccessor (org.apache.helix.HelixDataAccessor)173 ZNRecord (org.apache.helix.ZNRecord)91 PropertyKey (org.apache.helix.PropertyKey)69 Test (org.testng.annotations.Test)67 Builder (org.apache.helix.PropertyKey.Builder)59 ZKHelixDataAccessor (org.apache.helix.manager.zk.ZKHelixDataAccessor)40 Date (java.util.Date)39 HelixManager (org.apache.helix.HelixManager)35 IdealState (org.apache.helix.model.IdealState)33 LiveInstance (org.apache.helix.model.LiveInstance)31 HashMap (java.util.HashMap)30 MockParticipantManager (org.apache.helix.integration.manager.MockParticipantManager)30 Message (org.apache.helix.model.Message)30 ArrayList (java.util.ArrayList)28 ExternalView (org.apache.helix.model.ExternalView)26 PropertyPathBuilder (org.apache.helix.PropertyPathBuilder)25 Map (java.util.Map)19 HelixException (org.apache.helix.HelixException)19 ClusterControllerManager (org.apache.helix.integration.manager.ClusterControllerManager)19 InstanceConfig (org.apache.helix.model.InstanceConfig)17