Search in sources :

Example 16 with HelixManager

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

the class TestDistControllerElection method testParticipant.

@Test()
public void testParticipant() throws Exception {
    String className = getShortClassName();
    LOG.info("RUN " + className + " at " + new Date(System.currentTimeMillis()));
    final String clusterName = CLUSTER_PREFIX + "_" + className + "_" + "testParticipant";
    String path = "/" + clusterName;
    if (_gZkClient.exists(path)) {
        _gZkClient.deleteRecursively(path);
    }
    TestHelper.setupEmptyCluster(_gZkClient, clusterName);
    final String controllerName = "participant_0";
    HelixManager manager = new MockZKHelixManager(clusterName, controllerName, InstanceType.PARTICIPANT, _gZkClient);
    GenericHelixController participant0 = new GenericHelixController();
    List<HelixTimerTask> timerTasks = Collections.emptyList();
    DistributedLeaderElection election = new DistributedLeaderElection(manager, participant0, timerTasks);
    NotificationContext context = new NotificationContext(manager);
    context.setType(NotificationContext.Type.INIT);
    election.onControllerChange(context);
    path = PropertyPathBuilder.controllerLeader(clusterName);
    ZNRecord leaderRecord = _gZkClient.<ZNRecord>readData(path, true);
    AssertJUnit.assertNull(leaderRecord);
// AssertJUnit.assertNull(election.getController());
// AssertJUnit.assertNull(election.getLeader());
}
Also used : NotificationContext(org.apache.helix.NotificationContext) HelixTimerTask(org.apache.helix.HelixTimerTask) HelixManager(org.apache.helix.HelixManager) GenericHelixController(org.apache.helix.controller.GenericHelixController) DistributedLeaderElection(org.apache.helix.manager.zk.DistributedLeaderElection) Date(java.util.Date) ZNRecord(org.apache.helix.ZNRecord) Test(org.testng.annotations.Test)

Example 17 with HelixManager

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

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

the class CustomCodeInvoker method callParticipantCode.

private void callParticipantCode(NotificationContext context) {
    // therefore, double check to make sure only one participant invokes the code
    if (context.getType() == Type.CALLBACK) {
        HelixManager manager = context.getManager();
        // DataAccessor accessor = manager.getDataAccessor();
        HelixDataAccessor accessor = manager.getHelixDataAccessor();
        Builder keyBuilder = accessor.keyBuilder();
        String instance = manager.getInstanceName();
        String sessionId = manager.getSessionId();
        // get resource name from partition key: "PARTICIPANT_LEADER_XXX_0"
        String resourceName = _partitionKey.substring(0, _partitionKey.lastIndexOf('_'));
        CurrentState curState = accessor.getProperty(keyBuilder.currentState(instance, sessionId, resourceName));
        if (curState == null) {
            return;
        }
        String state = curState.getState(_partitionKey);
        if (state == null || !state.equalsIgnoreCase("LEADER")) {
            return;
        }
    }
    try {
        _callback.onCallback(context);
    } catch (Exception e) {
        LOG.error("Error invoking callback:" + _callback, e);
    }
}
Also used : HelixDataAccessor(org.apache.helix.HelixDataAccessor) HelixManager(org.apache.helix.HelixManager) Builder(org.apache.helix.PropertyKey.Builder) CurrentState(org.apache.helix.model.CurrentState)

Example 19 with HelixManager

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

the class GenericLeaderStandbyModel method onBecomeLeaderFromStandby.

@Transition(to = "LEADER", from = "STANDBY")
public void onBecomeLeaderFromStandby(Message message, NotificationContext context) throws Exception {
    LOG.info("Become LEADER from STANDBY");
    HelixManager manager = context.getManager();
    if (manager == null) {
        throw new IllegalArgumentException("Require HelixManager in notification conext");
    }
    for (ChangeType notificationType : _notificationTypes) {
        if (notificationType == ChangeType.LIVE_INSTANCE) {
            manager.addLiveInstanceChangeListener(_particHolder);
        } else if (notificationType == ChangeType.CONFIG) {
            manager.addConfigChangeListener(_particHolder);
        } else if (notificationType == ChangeType.EXTERNAL_VIEW) {
            manager.addExternalViewChangeListener(_particHolder);
        } else {
            LOG.error("Unsupport notificationType:" + notificationType.toString());
        }
    }
}
Also used : HelixManager(org.apache.helix.HelixManager) ChangeType(org.apache.helix.HelixConstants.ChangeType) Transition(org.apache.helix.participant.statemachine.Transition)

Example 20 with HelixManager

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

HelixManager (org.apache.helix.HelixManager)115 Test (org.testng.annotations.Test)49 HelixDataAccessor (org.apache.helix.HelixDataAccessor)35 ZNRecord (org.apache.helix.ZNRecord)28 Message (org.apache.helix.model.Message)23 PropertyKey (org.apache.helix.PropertyKey)20 Date (java.util.Date)18 ZKHelixDataAccessor (org.apache.helix.manager.zk.ZKHelixDataAccessor)17 Builder (org.apache.helix.PropertyKey.Builder)16 ArrayList (java.util.ArrayList)14 HashMap (java.util.HashMap)12 HelixException (org.apache.helix.HelixException)11 ExternalView (org.apache.helix.model.ExternalView)11 NotificationContext (org.apache.helix.NotificationContext)10 LiveInstance (org.apache.helix.model.LiveInstance)10 IdealState (org.apache.helix.model.IdealState)9 Criteria (org.apache.helix.Criteria)8 HelixAdmin (org.apache.helix.HelixAdmin)8 ZKHelixManager (org.apache.helix.manager.zk.ZKHelixManager)8 StringWriter (java.io.StringWriter)7