String instanceId = instance.getInstanceName(); String sessionId = instance.getSessionId();
LiveInstance controllerLeaderLiveInstance = zkHelixDataAccessor.getProperty(controllerLeaderKey); ControllerInfo controllerInfo = new ControllerInfo(); controllerInfo.leaderName = controllerLeaderLiveInstance.getId(); clusterInfo.controllerInfo = controllerInfo; for (String server : instancesInCluster) {
Builder keyBuilder = accessor.keyBuilder(); LiveInstance newLeader = new LiveInstance(manager.getInstanceName()); newLeader.setLiveInstance(ManagementFactory.getRuntimeMXBean().getName()); newLeader.setSessionId(manager.getSessionId()); newLeader.setHelixVersion(manager.getVersion()); try { boolean success = accessor.createControllerLeader(newLeader); String currentSession = currentLeader.getSessionId(); LOG.info("Leader exists for cluster: " + manager.getClusterName() + ", currentLeader: " + currentLeader.getInstanceName() + ", leaderSessionId: " + currentSession); if (currentSession != null && currentSession.equals(newLeader.getSessionId())) { return true; } else { LOG.warn("The existing leader has a different session. Expected session Id: " + newLeader .getSessionId());
@Override public void onLiveInstanceChange(List<LiveInstance> liveInstances, NotificationContext changeContext) { for (LiveInstance liveInstance : liveInstances) { log.info("Live Helix participant instance: " + liveInstance.getInstanceName()); } } }
@Override protected CurrentState getState(String resourceName) { PropertyKey.Builder keyBuilder = _helixDataAccessor.keyBuilder(); LiveInstance liveInstance = _helixDataAccessor.getProperty(keyBuilder.liveInstance(_instanceName)); String sessionId = liveInstance.getSessionId(); return _helixDataAccessor.getProperty(keyBuilder.currentState(_instanceName, sessionId, resourceName)); }
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.getAdditionalLiveInstanceInfo(); if (additionalLiveInstanceInfo != null) { additionalLiveInstanceInfo.merge(liveInstance.getRecord()); ZNRecord mergedLiveInstance = new ZNRecord(additionalLiveInstanceInfo, _instanceName); liveInstance = new LiveInstance(mergedLiveInstance); LOG.info("instanceName: " + _instanceName + ", mergedLiveInstance: " + liveInstance); 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 " LiveInstance curLiveInstance = new LiveInstance(record); if (!curLiveInstance.getSessionId().equals(_sessionId)) { + ", old-sessionId: " + curLiveInstance.getSessionId() + ", new-sessionId: " + _sessionId); curLiveInstance.setSessionId(_sessionId); _zkclient.writeData(liveInstancePath, curLiveInstance.getRecord());
@GET @Path("{clusterId}/controller") public Response getClusterController(@PathParam("clusterId") String clusterId) { HelixDataAccessor dataAccessor = getDataAccssor(clusterId); Map<String, Object> controllerInfo = new HashMap<>(); controllerInfo.put(Properties.id.name(), clusterId); LiveInstance leader = dataAccessor.getProperty(dataAccessor.keyBuilder().controllerLeader()); if (leader != null) { controllerInfo.put(ClusterProperties.controller.name(), leader.getInstanceName()); controllerInfo.putAll(leader.getRecord().getSimpleFields()); } else { controllerInfo.put(ClusterProperties.controller.name(), "No Lead Controller!"); } return JSONRepresentation(controllerInfo); }
protected List<String> setupLiveInstances(int numLiveInstances) { List<String> instances = new ArrayList<>(); for (int i = 0; i < numLiveInstances; i++) { LiveInstance liveInstance = new LiveInstance(HOSTNAME_PREFIX + i); liveInstance.setSessionId(SESSION_PREFIX + i); Builder keyBuilder = accessor.keyBuilder(); accessor.setProperty(keyBuilder.liveInstance(HOSTNAME_PREFIX + i), liveInstance); instances.add(liveInstance.getInstanceName()); } return instances; }
Assert.assertTrue(liveInstance.getRecord().getListFields().size() == 0); Assert.assertTrue(liveInstance.getRecord().getMapFields().size() == 0); Assert.assertTrue(liveInstance.getRecord().getSimpleFields().size() == 3); Assert.assertTrue(liveInstance.getRecord().getListFields().size() == 1); Assert.assertTrue(liveInstance.getRecord().getMapFields().size() == 1); Assert.assertTrue(liveInstance.getRecord().getSimpleFields().size() == 4); Assert.assertTrue(liveInstance.getRecord().getListFields().size() == 1); Assert.assertTrue(liveInstance.getRecord().getMapFields().size() == 1); Assert.assertTrue(liveInstance.getRecord().getSimpleFields().size() == 5); Assert.assertFalse(liveInstance.getSessionId().equals("value")); Assert.assertFalse(liveInstance.getLiveInstance().equals("value")); Assert.assertTrue(liveInstance.getRecord().getListFields().size() == 1); Assert.assertTrue(liveInstance.getRecord().getMapFields().size() == 1); Assert.assertTrue(liveInstance.getRecord().getSimpleFields().size() == 5); Assert.assertFalse(liveInstance.getSessionId().equals("value")); Assert.assertFalse(liveInstance.getLiveInstance().equals("value")); String sessionId = liveInstance.getSessionId(); Assert.assertTrue(liveInstance.getRecord().getListFields().size() == 1); Assert.assertTrue(liveInstance.getRecord().getMapFields().size() == 1); Assert.assertTrue(liveInstance.getRecord().getSimpleFields().size() == 5); Assert.assertFalse(liveInstance.getSessionId().equals("value")); Assert.assertFalse(liveInstance.getLiveInstance().equals("value")); Assert.assertFalse(sessionId.equals(liveInstance.getSessionId()));
@Override public void process(ClusterEvent event) throws Exception { HelixManager manager = event.getAttribute(AttributeName.helixmanager.name()); ClusterDataCache cache = event.getAttribute(AttributeName.ClusterDataCache.name()); if (manager == null || cache == null) { throw new StageException("Missing attributes in event:" + event + ". Requires HelixManager | DataCache"); } HelixManagerProperties properties = manager.getProperties(); Map<String, LiveInstance> liveInstanceMap = cache.getLiveInstances(); for (LiveInstance liveInstance : liveInstanceMap.values()) { String participantVersion = liveInstance.getHelixVersion(); if (!properties.isParticipantCompatible(participantVersion)) { String errorMsg = "incompatible participant. pipeline will not continue. " + "controller: " + manager.getInstanceName() + ", controllerVersion: " + properties.getVersion() + ", minimumSupportedParticipantVersion: " + properties.getProperty("miminum_supported_version.participant") + ", participant: " + liveInstance.getInstanceName() + ", participantVersion: " + participantVersion; LogUtil.logError(LOG, event.getEventId(), errorMsg); throw new StageException(errorMsg); } } } }
if (!_instanceConfig.getInstanceName().equals(liveInstance.getInstanceName())) { logger.error( "Cannot update live instance with different instance name. Current: {}; new: {}", _instanceConfig.getInstanceName(), liveInstance.getInstanceName()); } else { if (liveInstance.getResourceCapacityMap() != null && !liveInstance.getResourceCapacityMap() .equals(_liveInstance.getResourceCapacityMap())) { refreshCapacity = true;
protected void setupLiveInstances(String clusterName, int[] liveInstances) { ZKHelixDataAccessor accessor = new ZKHelixDataAccessor(clusterName, new ZkBaseDataAccessor<ZNRecord>(_gZkClient)); Builder keyBuilder = accessor.keyBuilder(); for (int i = 0; i < liveInstances.length; i++) { String instance = "localhost_" + liveInstances[i]; LiveInstance liveInstance = new LiveInstance(instance); liveInstance.setSessionId("session_" + liveInstances[i]); liveInstance.setHelixVersion("0.0.0"); accessor.setProperty(keyBuilder.liveInstance(instance), liveInstance); } }
@Override public boolean isValid() { if (getSessionId() == null) { _logger.error("liveInstance does not have session id. id:" + _record.getId()); return false; } if (getHelixVersion() == null) { _logger.error("liveInstance does not have CLM verion. id:" + _record.getId()); return false; } return true; } }
@Override public boolean verify() { // Newly created node should have a new creating time but with old session. LiveInstance invalidLeaderNode = accessor.getProperty(keyBuilder.controllerLeader()); // node exist if (invalidLeaderNode == null) return false; // node is newly created if (invalidLeaderNode.getStat().getCreationTime() == originalCreationTime) return false; // node has the same session as the old one, so it's invalid if (!invalidLeaderNode.getSessionId().equals(originalSessionId)) return false; return true; } }, 2000));
private List<LiveInstance> deepCopy(List<LiveInstance> instances) { List<LiveInstance> result = new ArrayList<LiveInstance>(); for (LiveInstance instance : instances) { result.add(new LiveInstance(instance.getRecord())); } return result; } }
@Override public boolean createControllerLeader(LiveInstance leader) { return _baseDataAccessor.create(PropertyPathBuilder.controllerLeader(_clusterName), leader.getRecord(), AccessOption.EPHEMERAL); }
private void setLeader(HelixManager manager) throws Exception { System.out.println("Setting controller " + manager.getInstanceName() + " as leader"); HelixDataAccessor accessor = manager.getHelixDataAccessor(); final LiveInstance leader = new LiveInstance(manager.getInstanceName()); leader.setLiveInstance(ManagementFactory.getRuntimeMXBean().getName()); leader.setSessionId(manager.getSessionId()); leader.setHelixVersion(manager.getVersion()); // Delete the current controller leader node so it will trigger leader election while (!manager.isLeader()) { accessor.getBaseDataAccessor() .remove(PropertyPathBuilder.controllerLeader(manager.getClusterName()), AccessOption.EPHEMERAL); Thread.sleep(50); } }
List<LiveInstance> liveInstanceList = new ArrayList<>(); for (String instance : _instanceConfigMap.keySet()) { LiveInstance liveInstance = new LiveInstance(instance); liveInstanceList.add(liveInstance);
accessor.setProperty(keyBuilder.clusterConfig(), clusterConfig); for (String node : _liveNodes) { LiveInstance liveInstance = new LiveInstance(node); liveInstance.setSessionId("testSession"); accessor.setProperty(keyBuilder.liveInstance(node), liveInstance);
/* package */ LiveInstance createLiveInstance(String[] resourceTypes, String[] resourceCapacity, String instancename) { LiveInstance li = new LiveInstance(instancename); if (resourceCapacity != null && resourceTypes != null) { Map<String, String> resMap = new HashMap<>(); for (int i = 0; i < resourceCapacity.length; i++) { resMap.put(resourceTypes[i], resourceCapacity[i]); } li.setResourceCapacityMap(resMap); } return li; }