Search in sources :

Example 16 with LiveInstance

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

the class TestDistControllerElection method testControllerParticipant.

@Test()
public void testControllerParticipant() throws Exception {
    String className = getShortClassName();
    LOG.info("RUN " + className + " at " + new Date(System.currentTimeMillis()));
    final String clusterName = CONTROLLER_CLUSTER_PREFIX + "_" + className + "_" + "testControllerParticipant";
    String path = "/" + clusterName;
    if (_gZkClient.exists(path)) {
        _gZkClient.deleteRecursively(path);
    }
    ZKHelixDataAccessor accessor = new ZKHelixDataAccessor(clusterName, new ZkBaseDataAccessor(_gZkClient));
    Builder keyBuilder = accessor.keyBuilder();
    TestHelper.setupEmptyCluster(_gZkClient, clusterName);
    final String controllerName = "controller_0";
    HelixManager manager = new MockZKHelixManager(clusterName, controllerName, InstanceType.CONTROLLER_PARTICIPANT, _gZkClient);
    GenericHelixController controller0 = new GenericHelixController();
    List<HelixTimerTask> timerTasks = Collections.emptyList();
    DistributedLeaderElection election = new DistributedLeaderElection(manager, controller0, timerTasks);
    NotificationContext context = new NotificationContext(manager);
    context.setType(NotificationContext.Type.CALLBACK);
    election.onControllerChange(context);
    LiveInstance liveInstance = accessor.getProperty(keyBuilder.controllerLeader());
    AssertJUnit.assertEquals(controllerName, liveInstance.getInstanceName());
    // path = PropertyPathConfig.getPath(PropertyType.LEADER, clusterName);
    // ZNRecord leaderRecord = _gZkClient.<ZNRecord> readData(path);
    // AssertJUnit.assertEquals(controllerName, leaderRecord.getSimpleField("LEADER"));
    // AssertJUnit.assertNotNull(election.getController());
    // AssertJUnit.assertNotNull(election.getLeader());
    manager = new MockZKHelixManager(clusterName, "controller_1", InstanceType.CONTROLLER_PARTICIPANT, _gZkClient);
    GenericHelixController controller1 = new GenericHelixController();
    election = new DistributedLeaderElection(manager, controller1, timerTasks);
    context = new NotificationContext(manager);
    context.setType(NotificationContext.Type.CALLBACK);
    election.onControllerChange(context);
    liveInstance = accessor.getProperty(keyBuilder.controllerLeader());
    AssertJUnit.assertEquals(controllerName, liveInstance.getInstanceName());
    // leaderRecord = _gZkClient.<ZNRecord> readData(path);
    // AssertJUnit.assertEquals(controllerName, leaderRecord.getSimpleField("LEADER"));
    // AssertJUnit.assertNull(election.getController());
    // AssertJUnit.assertNull(election.getLeader());
    LOG.info("END " + getShortClassName() + " at " + new Date(System.currentTimeMillis()));
}
Also used : ZkBaseDataAccessor(org.apache.helix.manager.zk.ZkBaseDataAccessor) HelixManager(org.apache.helix.HelixManager) Builder(org.apache.helix.PropertyKey.Builder) PropertyPathBuilder(org.apache.helix.PropertyPathBuilder) DistributedLeaderElection(org.apache.helix.manager.zk.DistributedLeaderElection) Date(java.util.Date) NotificationContext(org.apache.helix.NotificationContext) HelixTimerTask(org.apache.helix.HelixTimerTask) LiveInstance(org.apache.helix.model.LiveInstance) GenericHelixController(org.apache.helix.controller.GenericHelixController) ZKHelixDataAccessor(org.apache.helix.manager.zk.ZKHelixDataAccessor) Test(org.testng.annotations.Test)

Example 17 with LiveInstance

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

the class TestDistControllerElection method testController.

@Test()
public void testController() throws Exception {
    System.out.println("START TestDistControllerElection at " + new Date(System.currentTimeMillis()));
    String className = getShortClassName();
    final String clusterName = CLUSTER_PREFIX + "_" + className + "_" + "testController";
    String path = "/" + clusterName;
    if (_gZkClient.exists(path)) {
        _gZkClient.deleteRecursively(path);
    }
    ZKHelixDataAccessor accessor = new ZKHelixDataAccessor(clusterName, new ZkBaseDataAccessor(_gZkClient));
    Builder keyBuilder = accessor.keyBuilder();
    TestHelper.setupEmptyCluster(_gZkClient, clusterName);
    final String controllerName = "controller_0";
    HelixManager manager = new MockZKHelixManager(clusterName, controllerName, InstanceType.CONTROLLER, _gZkClient);
    GenericHelixController controller0 = new GenericHelixController();
    List<HelixTimerTask> timerTasks = Collections.emptyList();
    DistributedLeaderElection election = new DistributedLeaderElection(manager, controller0, timerTasks);
    NotificationContext context = new NotificationContext(manager);
    context.setType(NotificationContext.Type.INIT);
    election.onControllerChange(context);
    // path = PropertyPathConfig.getPath(PropertyType.LEADER, clusterName);
    // ZNRecord leaderRecord = _gZkClient.<ZNRecord> readData(path);
    LiveInstance liveInstance = accessor.getProperty(keyBuilder.controllerLeader());
    AssertJUnit.assertEquals(controllerName, liveInstance.getInstanceName());
    // AssertJUnit.assertNotNull(election.getController());
    // AssertJUnit.assertNull(election.getLeader());
    manager = new MockZKHelixManager(clusterName, "controller_1", InstanceType.CONTROLLER, _gZkClient);
    GenericHelixController controller1 = new GenericHelixController();
    election = new DistributedLeaderElection(manager, controller1, timerTasks);
    context = new NotificationContext(manager);
    context.setType(NotificationContext.Type.INIT);
    election.onControllerChange(context);
    // leaderRecord = _gZkClient.<ZNRecord> readData(path);
    liveInstance = accessor.getProperty(keyBuilder.controllerLeader());
    AssertJUnit.assertEquals(controllerName, liveInstance.getInstanceName());
    // AssertJUnit.assertNull(election.getController());
    // AssertJUnit.assertNull(election.getLeader());
    System.out.println("END TestDistControllerElection at " + new Date(System.currentTimeMillis()));
}
Also used : ZkBaseDataAccessor(org.apache.helix.manager.zk.ZkBaseDataAccessor) HelixManager(org.apache.helix.HelixManager) Builder(org.apache.helix.PropertyKey.Builder) PropertyPathBuilder(org.apache.helix.PropertyPathBuilder) DistributedLeaderElection(org.apache.helix.manager.zk.DistributedLeaderElection) Date(java.util.Date) NotificationContext(org.apache.helix.NotificationContext) HelixTimerTask(org.apache.helix.HelixTimerTask) LiveInstance(org.apache.helix.model.LiveInstance) GenericHelixController(org.apache.helix.controller.GenericHelixController) ZKHelixDataAccessor(org.apache.helix.manager.zk.ZKHelixDataAccessor) Test(org.testng.annotations.Test)

Example 18 with LiveInstance

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

the class ClusterAccessor method getClusterInfo.

@GET
@Path("{clusterId}")
public Response getClusterInfo(@PathParam("clusterId") String clusterId) {
    if (!isClusterExist(clusterId)) {
        return notFound();
    }
    HelixDataAccessor dataAccessor = getDataAccssor(clusterId);
    PropertyKey.Builder keyBuilder = dataAccessor.keyBuilder();
    Map<String, Object> clusterInfo = new HashMap<>();
    clusterInfo.put(Properties.id.name(), clusterId);
    LiveInstance controller = dataAccessor.getProperty(keyBuilder.controllerLeader());
    if (controller != null) {
        clusterInfo.put(ClusterProperties.controller.name(), controller.getInstanceName());
    } else {
        clusterInfo.put(ClusterProperties.controller.name(), "No Lead Controller!");
    }
    boolean paused = (dataAccessor.getProperty(keyBuilder.pause()) == null ? false : true);
    clusterInfo.put(ClusterProperties.paused.name(), paused);
    boolean maintenance = (dataAccessor.getProperty(keyBuilder.maintenance()) == null ? false : true);
    clusterInfo.put(ClusterProperties.maintenance.name(), maintenance);
    List<String> idealStates = dataAccessor.getChildNames(keyBuilder.idealStates());
    clusterInfo.put(ClusterProperties.resources.name(), idealStates);
    List<String> instances = dataAccessor.getChildNames(keyBuilder.instanceConfigs());
    clusterInfo.put(ClusterProperties.instances.name(), instances);
    List<String> liveInstances = dataAccessor.getChildNames(keyBuilder.liveInstances());
    clusterInfo.put(ClusterProperties.liveInstances.name(), liveInstances);
    return JSONRepresentation(clusterInfo);
}
Also used : HelixDataAccessor(org.apache.helix.HelixDataAccessor) LiveInstance(org.apache.helix.model.LiveInstance) HashMap(java.util.HashMap) PropertyKey(org.apache.helix.PropertyKey) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET)

Example 19 with LiveInstance

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

the class RoutingTableProvider method refresh.

public void refresh(List<ExternalView> externalViewList, NotificationContext changeContext) {
    HelixDataAccessor accessor = changeContext.getManager().getHelixDataAccessor();
    PropertyKey.Builder keyBuilder = accessor.keyBuilder();
    List<InstanceConfig> configList = accessor.getChildValues(keyBuilder.instanceConfigs());
    List<LiveInstance> liveInstances = accessor.getChildValues(keyBuilder.liveInstances());
    refresh(externalViewList, configList, liveInstances);
}
Also used : HelixDataAccessor(org.apache.helix.HelixDataAccessor) InstanceConfig(org.apache.helix.model.InstanceConfig) LiveInstance(org.apache.helix.model.LiveInstance) PropertyKey(org.apache.helix.PropertyKey)

Example 20 with LiveInstance

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

the class RoutingTableProvider method updateCurrentStatesListeners.

/**
 * Go through all live instances in the cluster, add CurrentStateChange listener to
 * them if they are newly added, and remove CurrentStateChange listener if instance is offline.
 */
private void updateCurrentStatesListeners(List<LiveInstance> liveInstances, NotificationContext changeContext) {
    HelixManager manager = changeContext.getManager();
    PropertyKey.Builder keyBuilder = new PropertyKey.Builder(manager.getClusterName());
    if (changeContext.getType() == NotificationContext.Type.FINALIZE) {
        // on finalize, should remove all current-state listeners
        logger.info("remove current-state listeners. lastSeenSessions: " + _lastSeenSessions);
        liveInstances = Collections.emptyList();
    }
    Map<String, LiveInstance> curSessions = new HashMap<>();
    for (LiveInstance liveInstance : liveInstances) {
        curSessions.put(liveInstance.getSessionId(), liveInstance);
    }
    // Go though the live instance list and update CurrentState listeners
    synchronized (_lastSeenSessions) {
        Map<String, LiveInstance> lastSessions = _lastSeenSessions.get();
        if (lastSessions == null) {
            lastSessions = Collections.emptyMap();
        }
        // add listeners to new live instances
        for (String session : curSessions.keySet()) {
            if (!lastSessions.containsKey(session)) {
                String instanceName = curSessions.get(session).getInstanceName();
                try {
                    // add current-state listeners for new sessions
                    manager.addCurrentStateChangeListener(this, instanceName, session);
                    logger.info(manager.getInstanceName() + " added current-state listener for instance: " + instanceName + ", session: " + session + ", listener: " + this);
                } catch (Exception e) {
                    logger.error("Fail to add current state listener for instance: " + instanceName + " with session: " + session, e);
                }
            }
        }
        // remove current-state listener for expired session
        for (String session : lastSessions.keySet()) {
            if (!curSessions.containsKey(session)) {
                String instanceName = lastSessions.get(session).getInstanceName();
                manager.removeListener(keyBuilder.currentStates(instanceName, session), this);
                logger.info("remove current-state listener for instance:" + instanceName + ", session: " + session);
            }
        }
        // update last-seen
        _lastSeenSessions.set(curSessions);
    }
}
Also used : HelixManager(org.apache.helix.HelixManager) LiveInstance(org.apache.helix.model.LiveInstance) HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) PropertyKey(org.apache.helix.PropertyKey) HelixException(org.apache.helix.HelixException)

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