Repository: helix Updated Branches: refs/heads/helix-0.6.x c8c677405 -> 0737d7acc
[HELIX-651] Add a method in HelixAdmin to set the InstanceConfig of an existing instance - Add a setInstanceConfig() method in HelixAdmin interface - Add an implementation for the same in ZkHelixAdmin - Add a test in TestZkHelixAdmin Project: http://git-wip-us.apache.org/repos/asf/helix/repo Commit: http://git-wip-us.apache.org/repos/asf/helix/commit/0737d7ac Tree: http://git-wip-us.apache.org/repos/asf/helix/tree/0737d7ac Diff: http://git-wip-us.apache.org/repos/asf/helix/diff/0737d7ac Branch: refs/heads/helix-0.6.x Commit: 0737d7accbf70c78bab72be02a39ad8300266fe1 Parents: c8c6774 Author: Priyesh Narayanan <[email protected]> Authored: Mon Jan 9 16:45:46 2017 -0800 Committer: Priyesh Narayanan <[email protected]> Committed: Wed Jan 11 12:08:29 2017 -0800 ---------------------------------------------------------------------- .../main/java/org/apache/helix/HelixAdmin.java | 20 +++++- .../apache/helix/manager/zk/ZKHelixAdmin.java | 27 +++++++- .../helix/manager/zk/TestZkHelixAdmin.java | 66 +++++++++++++++++--- 3 files changed, 102 insertions(+), 11 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/helix/blob/0737d7ac/helix-core/src/main/java/org/apache/helix/HelixAdmin.java ---------------------------------------------------------------------- diff --git a/helix-core/src/main/java/org/apache/helix/HelixAdmin.java b/helix-core/src/main/java/org/apache/helix/HelixAdmin.java index aeacd4b..75cbfcf 100644 --- a/helix-core/src/main/java/org/apache/helix/HelixAdmin.java +++ b/helix-core/src/main/java/org/apache/helix/HelixAdmin.java @@ -22,8 +22,14 @@ package org.apache.helix; import java.io.IOException; import java.util.List; import java.util.Map; -import org.apache.helix.model.*; +import org.apache.helix.model.ClusterConstraints; import org.apache.helix.model.ClusterConstraints.ConstraintType; +import org.apache.helix.model.ConstraintItem; +import org.apache.helix.model.ExternalView; +import org.apache.helix.model.HelixConfigScope; +import org.apache.helix.model.IdealState; +import org.apache.helix.model.InstanceConfig; +import org.apache.helix.model.StateModelDefinition; /* * Helix cluster management @@ -51,6 +57,18 @@ public interface HelixAdmin { InstanceConfig getInstanceConfig(String clusterName, String instanceName); /** + * Set the instance config of an existing instance under the given cluster. + * + * @param clusterName the name of the cluster to which this instance belongs. + * @param instanceName the name of this instance. + * @param instanceConfig the new {@link InstanceConfig} that will replace the current one + * associated with this instance. + * + * @return true if the operation was successful; false otherwise. + */ + boolean setInstanceConfig(String clusterName, String instanceName, InstanceConfig instanceConfig); + + /** * Get a list of resources in a cluster * @param clusterName * @return a list of resource names in the cluster http://git-wip-us.apache.org/repos/asf/helix/blob/0737d7ac/helix-core/src/main/java/org/apache/helix/manager/zk/ZKHelixAdmin.java ---------------------------------------------------------------------- diff --git a/helix-core/src/main/java/org/apache/helix/manager/zk/ZKHelixAdmin.java b/helix-core/src/main/java/org/apache/helix/manager/zk/ZKHelixAdmin.java index c7fa2ae..b58b344 100644 --- a/helix-core/src/main/java/org/apache/helix/manager/zk/ZKHelixAdmin.java +++ b/helix-core/src/main/java/org/apache/helix/manager/zk/ZKHelixAdmin.java @@ -35,7 +35,6 @@ import java.util.Map; import java.util.Set; import java.util.UUID; import java.util.concurrent.TimeUnit; - import org.I0Itec.zkclient.DataUpdater; import org.I0Itec.zkclient.exception.ZkNoNodeException; import org.apache.helix.AccessOption; @@ -162,6 +161,32 @@ public class ZKHelixAdmin implements HelixAdmin { } @Override + public boolean setInstanceConfig(String clusterName, String instanceName, + InstanceConfig newInstanceConfig) { + String instanceConfigPath = PropertyPathConfig + .getPath(PropertyType.CONFIGS, clusterName, ConfigScopeProperty.PARTICIPANT.toString(), + instanceName); + if (!_zkClient.exists(instanceConfigPath)) { + throw new HelixException( + "instance" + instanceName + " does not exist in cluster " + clusterName); + } + + HelixDataAccessor accessor = + new ZKHelixDataAccessor(clusterName, new ZkBaseDataAccessor<ZNRecord>(_zkClient)); + PropertyKey instanceConfigPropertyKey = accessor.keyBuilder().instanceConfig(instanceName); + InstanceConfig currentInstanceConfig = accessor.getProperty(instanceConfigPropertyKey); + if (!newInstanceConfig.getHostName().equals(currentInstanceConfig.getHostName()) + || !newInstanceConfig.getPort().equals(currentInstanceConfig.getPort())) { + throw new HelixException( + "Hostname and port cannot be changed, current hostname: " + currentInstanceConfig + .getHostName() + " and port: " + currentInstanceConfig.getPort() + + " is different from new hostname: " + newInstanceConfig.getHostName() + + "and new port: " + newInstanceConfig.getPort()); + } + return accessor.setProperty(instanceConfigPropertyKey, newInstanceConfig); + } + + @Override public void enableInstance(final String clusterName, final String instanceName, final boolean enabled) { String path = http://git-wip-us.apache.org/repos/asf/helix/blob/0737d7ac/helix-core/src/test/java/org/apache/helix/manager/zk/TestZkHelixAdmin.java ---------------------------------------------------------------------- diff --git a/helix-core/src/test/java/org/apache/helix/manager/zk/TestZkHelixAdmin.java b/helix-core/src/test/java/org/apache/helix/manager/zk/TestZkHelixAdmin.java index 65333d1..c2e53ea 100644 --- a/helix-core/src/test/java/org/apache/helix/manager/zk/TestZkHelixAdmin.java +++ b/helix-core/src/test/java/org/apache/helix/manager/zk/TestZkHelixAdmin.java @@ -24,15 +24,23 @@ import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; - -import org.apache.helix.*; +import org.apache.helix.BaseDataAccessor; +import org.apache.helix.HelixAdmin; +import org.apache.helix.HelixDataAccessor; +import org.apache.helix.HelixException; +import org.apache.helix.PropertyKey; +import org.apache.helix.PropertyPathConfig; +import org.apache.helix.PropertyType; +import org.apache.helix.TestHelper; +import org.apache.helix.ZNRecord; +import org.apache.helix.ZkUnitTestBase; import org.apache.helix.model.ClusterConstraints; import org.apache.helix.model.ClusterConstraints.ConstraintAttribute; import org.apache.helix.model.ClusterConstraints.ConstraintType; -import org.apache.helix.model.HelixConfigScope.ConfigScopeProperty; import org.apache.helix.model.ConstraintItem; import org.apache.helix.model.ExternalView; import org.apache.helix.model.HelixConfigScope; +import org.apache.helix.model.HelixConfigScope.ConfigScopeProperty; import org.apache.helix.model.IdealState; import org.apache.helix.model.InstanceConfig; import org.apache.helix.model.StateModelDefinition; @@ -76,12 +84,19 @@ public class TestZkHelixAdmin extends ZkUnitTestBase { // OK } - InstanceConfig config = new InstanceConfig("host1_9999"); - config.setHostName("host1"); - config.setPort("9999"); + String hostname = "host1"; + String port = "9999"; + String instanceName = hostname + "_" + port; + InstanceConfig config = new InstanceConfig(instanceName); + config.setHostName(hostname); + config.setPort(port); + List<String> dummyList = new ArrayList<String>(); + dummyList.add("foo"); + dummyList.add("bar"); + config.getRecord().setListField("dummy", dummyList); tool.addInstance(clusterName, config); - tool.enableInstance(clusterName, "host1_9999", true); - String path = PropertyPathConfig.getPath(PropertyType.INSTANCES, clusterName, "host1_9999"); + tool.enableInstance(clusterName, instanceName, true); + String path = PropertyPathConfig.getPath(PropertyType.INSTANCES, clusterName, instanceName); AssertJUnit.assertTrue(_gZkClient.exists(path)); try { @@ -90,8 +105,41 @@ public class TestZkHelixAdmin extends ZkUnitTestBase { } catch (HelixException e) { // OK } + config = tool.getInstanceConfig(clusterName, instanceName); + AssertJUnit.assertEquals(config.getId(), instanceName); + + // test setInstanceConfig() + config = tool.getInstanceConfig(clusterName, instanceName); + config.setHostName("host2"); + try { + // different host + tool.setInstanceConfig(clusterName, instanceName, config); + Assert.fail("should fail if hostname is different from the current one"); + } catch (HelixException e) { + // OK + } + + config = tool.getInstanceConfig(clusterName, instanceName); + config.setPort("7777"); + try { + // different port + tool.setInstanceConfig(clusterName, instanceName, config); + Assert.fail("should fail if port is different from the current one"); + } catch (HelixException e) { + // OK + } + + dummyList.remove("bar"); + dummyList.add("baz"); + config = tool.getInstanceConfig(clusterName, instanceName); + config.getRecord().setListField("dummy", dummyList); + AssertJUnit.assertTrue(tool.setInstanceConfig(clusterName, "host1_9999", config)); config = tool.getInstanceConfig(clusterName, "host1_9999"); - AssertJUnit.assertEquals(config.getId(), "host1_9999"); + dummyList = config.getRecord().getListField("dummy"); + AssertJUnit.assertTrue(dummyList.contains("foo")); + AssertJUnit.assertTrue(dummyList.contains("baz")); + AssertJUnit.assertFalse(dummyList.contains("bar")); + AssertJUnit.assertEquals(2, dummyList.size()); tool.dropInstance(clusterName, config); try {
