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();
}
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);
}
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();
}
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);
}
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();
}
Aggregations