public static boolean isInstanceSetup(ZkClient zkclient, String clusterName, String instanceName, InstanceType type) { if (type == InstanceType.PARTICIPANT || type == InstanceType.CONTROLLER_PARTICIPANT) { ArrayList<String> requiredPaths = new ArrayList<>(); requiredPaths.add(PropertyPathBuilder.instanceConfig(clusterName, instanceName)); requiredPaths.add(PropertyPathBuilder.instanceMessage(clusterName, instanceName)); requiredPaths.add(PropertyPathBuilder.instanceCurrentState(clusterName, instanceName)); requiredPaths.add(PropertyPathBuilder.instanceStatusUpdate(clusterName, instanceName)); requiredPaths.add(PropertyPathBuilder.instanceError(clusterName, instanceName)); boolean isValid = true; for (String path : requiredPaths) { if (!zkclient.exists(path)) { isValid = false; logger.error("Invalid instance setup, missing znode path: {}", path); } } if (isValid) { // Create the instance history node if it does not exist. // This is for back-compatibility. String historyPath = PropertyPathBuilder.instanceHistory(clusterName, instanceName); if (!zkclient.exists(historyPath)) { zkclient.createPersistent(historyPath, true); } } return isValid; } return true; }
public static boolean isInstanceSetup(HelixZkClient zkclient, String clusterName, String instanceName, InstanceType type) { if (type == InstanceType.PARTICIPANT || type == InstanceType.CONTROLLER_PARTICIPANT) { ArrayList<String> requiredPaths = new ArrayList<>(); requiredPaths.add(PropertyPathBuilder.instanceConfig(clusterName, instanceName)); requiredPaths.add(PropertyPathBuilder.instanceMessage(clusterName, instanceName)); requiredPaths.add(PropertyPathBuilder.instanceCurrentState(clusterName, instanceName)); requiredPaths.add(PropertyPathBuilder.instanceStatusUpdate(clusterName, instanceName)); requiredPaths.add(PropertyPathBuilder.instanceError(clusterName, instanceName)); boolean isValid = true; for (String path : requiredPaths) { if (!zkclient.exists(path)) { isValid = false; logger.error("Invalid instance setup, missing znode path: {}", path); } } if (isValid) { // Create the instance history node if it does not exist. // This is for back-compatibility. String historyPath = PropertyPathBuilder.instanceHistory(clusterName, instanceName); if (!zkclient.exists(historyPath)) { zkclient.createPersistent(historyPath, true); } } return isValid; } return true; }
for (int i = 0; i < 10; i++) { String msgId = "msg_" + i; paths.add(PropertyPathBuilder.instanceMessage(root, "host_1", msgId)); records.add(new ZNRecord(msgId)); String path = PropertyPathBuilder.instanceMessage(root, "host_1", msgId); ZNRecord record = _gZkClient.readData(path); Assert.assertEquals(record.getId(), msgId, "Should get what we created"); for (int i = 0; i < 10; i++) { String msgId = "msg_" + i; paths.add(PropertyPathBuilder.instanceMessage(root, "host_1", msgId)); ZNRecord newRecord = new ZNRecord(msgId); newRecord.setSimpleField("key1", "value1"); String path = PropertyPathBuilder.instanceMessage(root, "host_1", msgId); ZNRecord record = _gZkClient.readData(path); Assert.assertEquals(record.getSimpleFields().size(), 1, "Should have 1 simple field set"); for (int i = 0; i < 10; i++) { String msgId = "msg_" + i; paths.add(PropertyPathBuilder.instanceMessage(root, "host_1", msgId)); ZNRecord newRecord = new ZNRecord(msgId); newRecord.setSimpleField("key2", "value2"); String path = PropertyPathBuilder.instanceMessage(root, "host_1", msgId); ZNRecord record = _gZkClient.readData(path); Assert.assertEquals(record.getSimpleFields().size(), 2, "Should have 2 simple fields set");
ZK_ADDR, clusterName)); Assert.assertTrue(result); String msgPath = PropertyPathBuilder.instanceMessage(clusterName, "localhost_12918"); result = checkHandlers(controller.getHandlers(), msgPath); Assert.assertTrue(result); ZK_ADDR, clusterName)); Assert.assertTrue(result); msgPath = PropertyPathBuilder.instanceMessage(clusterName, "localhost_12922"); result = checkHandlers(controller.getHandlers(), msgPath); Assert.assertTrue(result);
public void post(String zkServer, Message message, String clusterName, String instanceName) { ZkClient client = new ZkClient(zkServer); client.setZkSerializer(new ZNRecordSerializer()); String path = PropertyPathBuilder.instanceMessage(clusterName, instanceName, message.getId()); client.delete(path); ZNRecord record = client.readData(PropertyPathBuilder.liveInstance(clusterName, instanceName)); message.setTgtSessionId(record.getSimpleField(LiveInstanceProperty.SESSION_ID.toString())); message.setTgtName(record.getId()); // System.out.println(message); client.createPersistent(path, message.getRecord()); }
void sendMessage(String msgId, String instanceName, String fromState, String toState, String partitionKey, int partitionId) throws InterruptedException, JsonGenerationException, JsonMappingException, IOException { Message message = new Message(MessageType.STATE_TRANSITION, msgId); message.setMsgId(msgId); message.setSrcName(srcName); message.setTgtName(instanceName); message.setMsgState(MessageState.NEW); message.setFromState(fromState); message.setToState(toState); // message.setPartitionId(partitionId); message.setPartitionName(partitionKey); String path = PropertyPathBuilder.instanceMessage(clusterName, instanceName, message.getId()); ObjectMapper mapper = new ObjectMapper(); StringWriter sw = new StringWriter(); mapper.writeValueUsingView(sw, message, Message.class); System.out.println(sw.toString()); client.delete(path); Thread.sleep(10000); ZNRecord record = client.readData(PropertyPathBuilder.liveInstance(clusterName, instanceName)); message.setTgtSessionId(record.getSimpleField(LiveInstanceProperty.SESSION_ID.toString()) .toString()); client.createPersistent(path, message); }
@Override public void addInstance(String clusterName, InstanceConfig instanceConfig) { logger.info("Add instance {} to cluster {}.", instanceConfig.getInstanceName(), clusterName); if (!ZKUtil.isClusterSetup(clusterName, _zkClient)) { throw new HelixException("cluster " + clusterName + " is not setup yet"); } String instanceConfigsPath = PropertyPathBuilder.instanceConfig(clusterName); String nodeId = instanceConfig.getId(); String instanceConfigPath = instanceConfigsPath + "/" + nodeId; if (_zkClient.exists(instanceConfigPath)) { throw new HelixException("Node " + nodeId + " already exists in cluster " + clusterName); } ZKUtil.createChildren(_zkClient, instanceConfigsPath, instanceConfig.getRecord()); _zkClient.createPersistent(PropertyPathBuilder.instanceMessage(clusterName, nodeId), true); _zkClient.createPersistent(PropertyPathBuilder.instanceCurrentState(clusterName, nodeId), true); _zkClient.createPersistent(PropertyPathBuilder.instanceError(clusterName, nodeId), true); _zkClient.createPersistent(PropertyPathBuilder.instanceStatusUpdate(clusterName, nodeId), true); _zkClient.createPersistent(PropertyPathBuilder.instanceHistory(clusterName, nodeId), true); }
@Override public void addInstance(String clusterName, InstanceConfig instanceConfig) { logger.info("Add instance {} to cluster {}.", instanceConfig.getInstanceName(), clusterName); if (!ZKUtil.isClusterSetup(clusterName, _zkClient)) { throw new HelixException("cluster " + clusterName + " is not setup yet"); } String instanceConfigsPath = PropertyPathBuilder.instanceConfig(clusterName); String nodeId = instanceConfig.getId(); String instanceConfigPath = instanceConfigsPath + "/" + nodeId; if (_zkClient.exists(instanceConfigPath)) { throw new HelixException("Node " + nodeId + " already exists in cluster " + clusterName); } ZKUtil.createChildren(_zkClient, instanceConfigsPath, instanceConfig.getRecord()); _zkClient.createPersistent(PropertyPathBuilder.instanceMessage(clusterName, nodeId), true); _zkClient.createPersistent(PropertyPathBuilder.instanceCurrentState(clusterName, nodeId), true); _zkClient.createPersistent(PropertyPathBuilder.instanceError(clusterName, nodeId), true); _zkClient.createPersistent(PropertyPathBuilder.instanceStatusUpdate(clusterName, nodeId), true); _zkClient.createPersistent(PropertyPathBuilder.instanceHistory(clusterName, nodeId), true); }
public void post(String zkServer, Message message, String clusterName, String instanceName) { HelixZkClient client = SharedZkClientFactory.getInstance().buildZkClient(new HelixZkClient.ZkConnectionConfig( zkServer)); client.setZkSerializer(new ZNRecordSerializer()); String path = PropertyPathBuilder.instanceMessage(clusterName, instanceName, message.getId()); client.delete(path); ZNRecord record = client.readData(PropertyPathBuilder.liveInstance(clusterName, instanceName)); message.setTgtSessionId(record.getSimpleField(LiveInstanceProperty.SESSION_ID.toString())); message.setTgtName(record.getId()); // System.out.println(message); client.createPersistent(path, message.getRecord()); }
String msgPath = PropertyPathBuilder.instanceMessage(clusterName, "localhost_12918"); int numberOfListeners = ZkTestHelper.numberOfListeners(ZK_ADDR, msgPath); Assert.assertTrue(result); msgPath = PropertyPathBuilder.instanceMessage(clusterName, "localhost_12919"); numberOfListeners = ZkTestHelper.numberOfListeners(ZK_ADDR, msgPath);
for (int i = 0; i < 5; i++) { String instanceName = "localhost_" + (12918 + i); String msgPath = PropertyPathBuilder.instanceMessage(clusterName, instanceName);
@Override public void addInstance(String clusterName, InstanceConfig instanceConfig) { String instanceConfigsPath = PropertyPathBuilder.instanceConfig(clusterName); String nodeId = instanceConfig.getId(); if (!_baseDataAccessor.exists(instanceConfigsPath, 0)) { _baseDataAccessor.create(instanceConfigsPath, new ZNRecord(nodeId), 0); } String instanceConfigPath = instanceConfigsPath + "/" + nodeId; _baseDataAccessor.create(instanceConfigPath, instanceConfig.getRecord(), 0); _baseDataAccessor .set(PropertyPathBuilder.instanceMessage(clusterName, nodeId), new ZNRecord(nodeId), 0); _baseDataAccessor .set(PropertyPathBuilder.instanceCurrentState(clusterName, nodeId), new ZNRecord(nodeId), 0); _baseDataAccessor .set(PropertyPathBuilder.instanceError(clusterName, nodeId), new ZNRecord(nodeId), 0); _baseDataAccessor .set(PropertyPathBuilder.instanceStatusUpdate(clusterName, nodeId), new ZNRecord(nodeId), 0); _baseDataAccessor .set(PropertyPathBuilder.instanceHistory(clusterName, nodeId), new ZNRecord(nodeId), 0); }