Search in sources :

Example 61 with LiveInstance

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

the class RoutingTable method refresh.

private void refresh(Map<String, Map<String, Map<String, CurrentState>>> currentStateMap) {
    Map<String, InstanceConfig> instanceConfigMap = new HashMap<>();
    for (InstanceConfig config : _instanceConfigs) {
        instanceConfigMap.put(config.getId(), config);
    }
    for (LiveInstance liveInstance : _liveInstances) {
        String instanceName = liveInstance.getInstanceName();
        String sessionId = liveInstance.getSessionId();
        InstanceConfig instanceConfig = instanceConfigMap.get(instanceName);
        if (instanceConfig == null) {
            logger.error("Invalid instance name. " + instanceName + " .Not found in /cluster/configs/. instanceName: ");
        }
        Map<String, CurrentState> currentStates = Collections.emptyMap();
        if (currentStateMap.containsKey(instanceName) && currentStateMap.get(instanceName).containsKey(sessionId)) {
            currentStates = currentStateMap.get(instanceName).get(sessionId);
        }
        for (CurrentState currentState : currentStates.values()) {
            String resourceName = currentState.getResourceName();
            Map<String, String> stateMap = currentState.getPartitionStateMap();
            for (String partitionName : stateMap.keySet()) {
                String state = stateMap.get(partitionName);
                addEntry(resourceName, partitionName, state, instanceConfig);
            }
        }
    }
}
Also used : InstanceConfig(org.apache.helix.model.InstanceConfig) LiveInstance(org.apache.helix.model.LiveInstance) HashMap(java.util.HashMap) CurrentState(org.apache.helix.model.CurrentState)

Example 62 with LiveInstance

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

Example 63 with LiveInstance

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

the class InstanceAccessor method getInstance.

@GET
@Path("{instanceName}")
public Response getInstance(@PathParam("clusterId") String clusterId, @PathParam("instanceName") String instanceName) throws IOException {
    HelixDataAccessor accessor = getDataAccssor(clusterId);
    Map<String, Object> instanceMap = new HashMap<>();
    instanceMap.put(Properties.id.name(), JsonNodeFactory.instance.textNode(instanceName));
    instanceMap.put(InstanceProperties.liveInstance.name(), null);
    InstanceConfig instanceConfig = accessor.getProperty(accessor.keyBuilder().instanceConfig(instanceName));
    LiveInstance liveInstance = accessor.getProperty(accessor.keyBuilder().liveInstance(instanceName));
    if (instanceConfig != null) {
        instanceMap.put(InstanceProperties.config.name(), instanceConfig.getRecord());
    } else {
        return notFound();
    }
    if (liveInstance != null) {
        instanceMap.put(InstanceProperties.liveInstance.name(), liveInstance.getRecord());
    }
    return JSONRepresentation(instanceMap);
}
Also used : HelixDataAccessor(org.apache.helix.HelixDataAccessor) InstanceConfig(org.apache.helix.model.InstanceConfig) LiveInstance(org.apache.helix.model.LiveInstance) HashMap(java.util.HashMap) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET)

Example 64 with LiveInstance

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

the class ClusterAccessor method getClusterController.

@GET
@Path("{clusterId}/controller")
public Response getClusterController(@PathParam("clusterId") String clusterId) {
    HelixDataAccessor dataAccessor = getDataAccssor(clusterId);
    Map<String, Object> controllerInfo = new HashMap<>();
    controllerInfo.put(Properties.id.name(), clusterId);
    LiveInstance leader = dataAccessor.getProperty(dataAccessor.keyBuilder().controllerLeader());
    if (leader != null) {
        controllerInfo.put(ClusterProperties.controller.name(), leader.getInstanceName());
        controllerInfo.putAll(leader.getRecord().getSimpleFields());
    } else {
        controllerInfo.put(ClusterProperties.controller.name(), "No Lead Controller!");
    }
    return JSONRepresentation(controllerInfo);
}
Also used : HelixDataAccessor(org.apache.helix.HelixDataAccessor) LiveInstance(org.apache.helix.model.LiveInstance) HashMap(java.util.HashMap) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET)

Example 65 with LiveInstance

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

the class ClusterResource method getClusterRepresentation.

StringRepresentation getClusterRepresentation(String clusterName) throws JsonGenerationException, JsonMappingException, IOException {
    ZkClient zkClient = ResourceUtil.getAttributeFromCtx(getContext(), ResourceUtil.ContextKey.ZKCLIENT);
    ClusterSetup setupTool = new ClusterSetup(zkClient);
    List<String> instances = setupTool.getClusterManagementTool().getInstancesInCluster(clusterName);
    ZNRecord clusterSummayRecord = new ZNRecord("Cluster Summary");
    clusterSummayRecord.setListField("participants", instances);
    List<String> resources = setupTool.getClusterManagementTool().getResourcesInCluster(clusterName);
    clusterSummayRecord.setListField("resources", resources);
    List<String> models = setupTool.getClusterManagementTool().getStateModelDefs(clusterName);
    clusterSummayRecord.setListField("stateModelDefs", models);
    HelixDataAccessor accessor = ClusterRepresentationUtil.getClusterDataAccessor(zkClient, clusterName);
    Builder keyBuilder = accessor.keyBuilder();
    LiveInstance leader = accessor.getProperty(keyBuilder.controllerLeader());
    if (leader != null) {
        clusterSummayRecord.setSimpleField("LEADER", leader.getInstanceName());
    } else {
        clusterSummayRecord.setSimpleField("LEADER", "");
    }
    StringRepresentation representation = new StringRepresentation(ClusterRepresentationUtil.ZNRecordToJson(clusterSummayRecord), MediaType.APPLICATION_JSON);
    return representation;
}
Also used : ZkClient(org.apache.helix.manager.zk.ZkClient) HelixDataAccessor(org.apache.helix.HelixDataAccessor) LiveInstance(org.apache.helix.model.LiveInstance) StringRepresentation(org.restlet.representation.StringRepresentation) Builder(org.apache.helix.PropertyKey.Builder) ClusterSetup(org.apache.helix.tools.ClusterSetup) ZNRecord(org.apache.helix.ZNRecord)

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