}
private void createLiveInstance() {
String liveInstancePath = _keyBuilder.liveInstance(_participantId.stringify()).getPath();
String sessionId = _connection.getSessionId().stringify();
LiveInstance liveInstance = new LiveInstance(_participantId.stringify());
liveInstance.setSessionId(sessionId);
liveInstance.setHelixVersion(_connection.getHelixVersion());
liveInstance.setLiveInstance(ManagementFactory.getRuntimeMXBean().getName());
if (_liveInstanceInfoProvider != null) {
LOG.info("Additional live instance information is provided: " + _liveInstanceInfoProvider);
ZNRecord additionalLiveInstanceInfo =
_liveInstanceInfoProvider.getAdditionalLiveInstanceInfo();
if (additionalLiveInstanceInfo != null) {
additionalLiveInstanceInfo.merge(liveInstance.getRecord());
ZNRecord mergedLiveInstance =
new ZNRecord(additionalLiveInstanceInfo, _participantId.stringify());
liveInstance = new LiveInstance(mergedLiveInstance);
LOG.info("Participant: " + _participantId + ", mergedLiveInstance: " + liveInstance);
}
}
boolean retry;
do {
retry = false;
boolean success =
_baseAccessor.create(liveInstancePath, liveInstance.getRecord(), AccessOption.EPHEMERAL);
if (!success) {
LOG.warn("found another participant with same name: " + _participantId + " in cluster "
+ _clusterId);
Stat stat = new Stat();
ZNRecord record = _baseAccessor.get(liveInstancePath, stat, 0);
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);
success =
_baseAccessor.set(liveInstancePath, curLiveInstance.getRecord(),
stat.getVersion(), AccessOption.EPHEMERAL);
if (!success) {
LOG.error("Someone changes sessionId as we update, should not happen");
throw new HelixException("fail to create live-instance for " + _participantId);
}