@Override public boolean createControllerLeader(LiveInstance leader) { return _baseDataAccessor.create(PropertyPathBuilder.controllerLeader(_clusterName), leader.getRecord(), AccessOption.EPHEMERAL); }
@Override public boolean createControllerLeader(LiveInstance leader) { return _baseDataAccessor.create(PropertyPathBuilder.controllerLeader(_clusterName), leader.getRecord(), AccessOption.EPHEMERAL); }
if (leader != null) { String leaderName = leader.getInstanceName(); String sessionId = Long.toHexString(leader.getRecord().getEphemeralOwner()); if (leaderName != null && leaderName.equals(_instanceName) && sessionId .equals(_sessionId)) {
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; } }
@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); }
private void refreshCache(List<LiveInstance> liveInstances) { List<ServiceMetadata> services = new ArrayList<ServiceMetadata>(); for (LiveInstance liveInstance : liveInstances) { ServiceMetadata metadata = new ServiceMetadata(); ZNRecord rec = liveInstance.getRecord(); metadata.setPort(Integer.parseInt(rec.getSimpleField("PORT"))); metadata.setHost(rec.getSimpleField("HOST")); metadata.setServiceName(rec.getSimpleField("SERVICE_NAME")); services.add(metadata); } // protect against multiple threads updating this synchronized (this) { cache = services; } }
@GET @Path("{instanceName}") public Response getInstance(@PathParam("clusterId") String clusterId, @PathParam("instanceName") String instanceName) throws IOException { HelixDataAccessor accessor = getDataAccssor(clusterId); Map<String, Object> instanceMap = new HashMap<>(); instanceMap.put(Properties.id.name(), JsonNodeFactory.instance.textNode(instanceName)); instanceMap.put(InstanceProperties.liveInstance.name(), null); InstanceConfig instanceConfig = accessor.getProperty(accessor.keyBuilder().instanceConfig(instanceName)); LiveInstance liveInstance = accessor.getProperty(accessor.keyBuilder().liveInstance(instanceName)); if (instanceConfig != null) { instanceMap.put(InstanceProperties.config.name(), instanceConfig.getRecord()); } else { return notFound(); } if (liveInstance != null) { instanceMap.put(InstanceProperties.liveInstance.name(), liveInstance.getRecord()); } return JSONRepresentation(instanceMap); }
_liveInstanceInfoProvider.getAdditionalLiveInstanceInfo(); if (additionalLiveInstanceInfo != null) { additionalLiveInstanceInfo.merge(liveInstance.getRecord()); ZNRecord mergedLiveInstance = new ZNRecord(additionalLiveInstanceInfo, _instanceName); liveInstance = new LiveInstance(mergedLiveInstance); retry = false; try { _zkclient.createEphemeral(liveInstancePath, liveInstance.getRecord()); LOG.info("LiveInstance created, path: " + liveInstancePath + ", sessionId: " + liveInstance.getSessionId()); } catch (ZkNodeExistsException e) { _zkclient.writeData(liveInstancePath, curLiveInstance.getRecord()); _zkclient.createEphemeral(liveInstancePath, liveInstance.getRecord()); LOG.info("LiveInstance created, path: " + liveInstancePath + ", sessionId: " + liveInstance.getSessionId()); } catch (Exception e) {
_liveInstanceInfoProvider.getAdditionalLiveInstanceInfo(); if (additionalLiveInstanceInfo != null) { additionalLiveInstanceInfo.merge(liveInstance.getRecord()); ZNRecord mergedLiveInstance = new ZNRecord(additionalLiveInstanceInfo, _instanceName); liveInstance = new LiveInstance(mergedLiveInstance); retry = false; try { _zkclient.createEphemeral(liveInstancePath, liveInstance.getRecord()); LOG.info("LiveInstance created, path: " + liveInstancePath + ", sessionId: " + liveInstance.getSessionId()); } catch (ZkNodeExistsException e) { _zkclient.writeData(liveInstancePath, curLiveInstance.getRecord()); _zkclient.createEphemeral(liveInstancePath, liveInstance.getRecord()); LOG.info("LiveInstance created, path: " + liveInstancePath + ", sessionId: " + liveInstance.getSessionId()); } catch (Exception e) {
StringRepresentation getControllerRepresentation(String clusterName) throws JsonGenerationException, JsonMappingException, IOException { Builder keyBuilder = new PropertyKey.Builder(clusterName); ZkClient zkClient = ResourceUtil.getAttributeFromCtx(getContext(), ResourceUtil.ContextKey.ZKCLIENT); ZKHelixDataAccessor accessor = new ZKHelixDataAccessor(clusterName, new ZkBaseDataAccessor<ZNRecord>(zkClient)); ZNRecord record = null; LiveInstance leader = accessor.getProperty(keyBuilder.controllerLeader()); if (leader != null) { record = leader.getRecord(); } else { record = new ZNRecord(""); DateFormat formatter = new SimpleDateFormat("yyyyMMdd-HHmmss.SSSSSS"); String time = formatter.format(new Date()); Map<String, String> contentMap = new TreeMap<String, String>(); contentMap.put("AdditionalInfo", "No leader exists"); record.setMapField(Level.HELIX_INFO + "-" + time, contentMap); } boolean paused = (accessor.getProperty(keyBuilder.pause()) == null ? false : true); record.setSimpleField(PropertyType.PAUSE.toString(), "" + paused); String retVal = ClusterRepresentationUtil.ZNRecordToJson(record); StringRepresentation representation = new StringRepresentation(retVal, MediaType.APPLICATION_JSON); return representation; }
instance.getRecord().setMapField("test", map); Assert.assertTrue(helixDataAccessor.updateProperty(propertyKey, instance), "Failed to update live instance node"); Assert.assertEquals(instances.get(0).getRecord().getMapField("test"), map, "Wrong map data."); manager.disconnect(); Thread.sleep(1000); // wait for callback finish
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")); 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"));