Search in sources :

Example 1 with ParticipantHistory

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

the class ParticipantManager method disconnect.

public void disconnect() {
    ParticipantHistory history = getHistory();
    history.reportOffline();
    persistHistory(history);
    reset();
}
Also used : ParticipantHistory(org.apache.helix.model.ParticipantHistory)

Example 2 with ParticipantHistory

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

the class ParticipantManager method createLiveInstance.

private void createLiveInstance() {
    String liveInstancePath = _keyBuilder.liveInstance(_instanceName).getPath();
    LiveInstance liveInstance = new LiveInstance(_instanceName);
    liveInstance.setSessionId(_sessionId);
    liveInstance.setHelixVersion(_manager.getVersion());
    liveInstance.setLiveInstance(ManagementFactory.getRuntimeMXBean().getName());
    // LiveInstanceInfoProvider liveInstanceInfoProvider = _manager._liveInstanceInfoProvider;
    if (_liveInstanceInfoProvider != null) {
        LOG.info("invoke liveInstanceInfoProvider");
        ZNRecord additionalLiveInstanceInfo = _liveInstanceInfoProvider.getAdditionalLiveInstanceInfo();
        if (additionalLiveInstanceInfo != null) {
            additionalLiveInstanceInfo.merge(liveInstance.getRecord());
            ZNRecord mergedLiveInstance = new ZNRecord(additionalLiveInstanceInfo, _instanceName);
            liveInstance = new LiveInstance(mergedLiveInstance);
            LOG.info("instanceName: " + _instanceName + ", mergedLiveInstance: " + liveInstance);
        }
    }
    boolean retry;
    do {
        retry = false;
        try {
            _zkclient.createEphemeral(liveInstancePath, liveInstance.getRecord());
            LOG.info("LiveInstance created, path: " + liveInstancePath + ", sessionId: " + liveInstance.getSessionId());
        } catch (ZkNodeExistsException e) {
            LOG.warn("found another instance with same instanceName: " + _instanceName + " in cluster " + _clusterName);
            Stat stat = new Stat();
            ZNRecord record = _zkclient.readData(liveInstancePath, stat, true);
            if (record == null) {
                /**
                 * live-instance is gone as we check it, retry create live-instance
                 */
                retry = true;
            } else {
                String ephemeralOwner = Long.toHexString(stat.getEphemeralOwner());
                if (ephemeralOwner.equals(_sessionId)) {
                    /**
                     * update sessionId field in live-instance if necessary
                     */
                    LiveInstance curLiveInstance = new LiveInstance(record);
                    if (!curLiveInstance.getSessionId().equals(_sessionId)) {
                        /**
                         * in last handle-new-session,
                         * live-instance is created by new zkconnection with stale session-id inside
                         * just update session-id field
                         */
                        LOG.info("overwriting session-id by ephemeralOwner: " + ephemeralOwner + ", old-sessionId: " + curLiveInstance.getSessionId() + ", new-sessionId: " + _sessionId);
                        curLiveInstance.setSessionId(_sessionId);
                        _zkclient.writeData(liveInstancePath, curLiveInstance.getRecord());
                    }
                } else {
                    /**
                     * wait for a while, in case previous helix-participant exits unexpectedly
                     * and its live-instance still hangs around until session timeout
                     */
                    try {
                        TimeUnit.MILLISECONDS.sleep(_sessionTimeout + 5000);
                    } catch (InterruptedException ex) {
                        LOG.warn("Sleep interrupted while waiting for previous live-instance to go away.", ex);
                    }
                    /**
                     * give a last try after exit while loop
                     */
                    retry = true;
                    break;
                }
            }
        }
    } while (retry);
    /**
     * give a last shot
     */
    if (retry) {
        try {
            _zkclient.createEphemeral(liveInstancePath, liveInstance.getRecord());
            LOG.info("LiveInstance created, path: " + liveInstancePath + ", sessionId: " + liveInstance.getSessionId());
        } catch (Exception e) {
            String errorMessage = "instance: " + _instanceName + " already has a live-instance in cluster " + _clusterName;
            LOG.error(errorMessage);
            throw new HelixException(errorMessage);
        }
    }
    ParticipantHistory history = getHistory();
    history.reportOnline(_sessionId, _manager.getVersion());
    persistHistory(history);
}
Also used : HelixException(org.apache.helix.HelixException) ZkNodeExistsException(org.I0Itec.zkclient.exception.ZkNodeExistsException) Stat(org.apache.zookeeper.data.Stat) LiveInstance(org.apache.helix.model.LiveInstance) ZNRecord(org.apache.helix.ZNRecord) HelixException(org.apache.helix.HelixException) ZkNodeExistsException(org.I0Itec.zkclient.exception.ZkNodeExistsException) ParticipantHistory(org.apache.helix.model.ParticipantHistory)

Example 3 with ParticipantHistory

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

the class TestInstanceHistory method testInstanceHistory.

@Test()
public void testInstanceHistory() throws Exception {
    HelixManager manager = HelixManagerFactory.getZKHelixManager(CLUSTER_NAME, "admin", InstanceType.ADMINISTRATOR, ZK_ADDR);
    manager.connect();
    PropertyKey.Builder keyBuilder = new PropertyKey.Builder(CLUSTER_NAME);
    PropertyKey propertyKey = keyBuilder.participantHistory(_participants[0].getInstanceName());
    ParticipantHistory history = manager.getHelixDataAccessor().getProperty(propertyKey);
    Assert.assertNotNull(history);
    List<String> list = history.getRecord().getListField("HISTORY");
    Assert.assertEquals(list.size(), 1);
    for (int i = 0; i <= 22; i++) {
        _participants[0].disconnect();
        _participants[0].connect();
    }
    history = manager.getHelixDataAccessor().getProperty(propertyKey);
    Assert.assertNotNull(history);
    list = history.getRecord().getListField("HISTORY");
    Assert.assertEquals(list.size(), 20);
    list = history.getRecord().getListField("OFFLINE");
    Assert.assertEquals(list.size(), 20);
    manager.disconnect();
}
Also used : HelixManager(org.apache.helix.HelixManager) PropertyKey(org.apache.helix.PropertyKey) ParticipantHistory(org.apache.helix.model.ParticipantHistory) Test(org.testng.annotations.Test)

Example 4 with ParticipantHistory

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

the class TestNodeOfflineTimeStamp method testNodeShutdown.

@Test
public void testNodeShutdown() throws Exception {
    for (MockParticipantManager participant : _participants) {
        ParticipantHistory history = getInstanceHistory(participant.getInstanceName());
        Assert.assertNotNull(history);
        Assert.assertEquals(history.getLastOfflineTime(), ParticipantHistory.ONLINE);
    }
    long shutdownTime = System.currentTimeMillis();
    _participants[0].syncStop();
    ParticipantHistory history = getInstanceHistory(_participants[0].getInstanceName());
    long recordTime = history.getLastOfflineTime();
    Assert.assertTrue(Math.abs(shutdownTime - recordTime) <= 500L);
    _participants[0].reset();
    Thread.sleep(50);
    _participants[0].syncStart();
    Thread.sleep(50);
    history = getInstanceHistory(_participants[0].getInstanceName());
    Assert.assertEquals(history.getLastOfflineTime(), ParticipantHistory.ONLINE);
}
Also used : MockParticipantManager(org.apache.helix.integration.manager.MockParticipantManager) ParticipantHistory(org.apache.helix.model.ParticipantHistory) Test(org.testng.annotations.Test)

Example 5 with ParticipantHistory

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

the class InstanceAccessor method getHistoryOnInstance.

@GET
@Path("{instanceName}/history")
public Response getHistoryOnInstance(@PathParam("clusterId") String clusterId, @PathParam("instanceName") String instanceName) throws IOException {
    HelixDataAccessor accessor = getDataAccssor(clusterId);
    ParticipantHistory history = accessor.getProperty(accessor.keyBuilder().participantHistory(instanceName));
    if (history != null) {
        return JSONRepresentation(history.getRecord());
    }
    return notFound();
}
Also used : HelixDataAccessor(org.apache.helix.HelixDataAccessor) ParticipantHistory(org.apache.helix.model.ParticipantHistory) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET)

Aggregations

ParticipantHistory (org.apache.helix.model.ParticipantHistory)7 PropertyKey (org.apache.helix.PropertyKey)3 Test (org.testng.annotations.Test)2 ArrayList (java.util.ArrayList)1 GET (javax.ws.rs.GET)1 Path (javax.ws.rs.Path)1 ZkNodeExistsException (org.I0Itec.zkclient.exception.ZkNodeExistsException)1 HelixDataAccessor (org.apache.helix.HelixDataAccessor)1 HelixException (org.apache.helix.HelixException)1 HelixManager (org.apache.helix.HelixManager)1 Builder (org.apache.helix.PropertyKey.Builder)1 ZNRecord (org.apache.helix.ZNRecord)1 MockParticipantManager (org.apache.helix.integration.manager.MockParticipantManager)1 LiveInstance (org.apache.helix.model.LiveInstance)1 Stat (org.apache.zookeeper.data.Stat)1