diff --git a/helix-core/src/main/java/org/apache/helix/HelixManagerProperty.java b/helix-core/src/main/java/org/apache/helix/HelixManagerProperty.java index 00c065d20a..8f8b6c6227 100644 --- a/helix-core/src/main/java/org/apache/helix/HelixManagerProperty.java +++ b/helix-core/src/main/java/org/apache/helix/HelixManagerProperty.java @@ -22,6 +22,7 @@ import java.util.Properties; import org.apache.helix.model.CloudConfig; +import org.apache.helix.model.InstanceConfig; import org.apache.helix.zookeeper.api.client.RealmAwareZkClient; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -35,6 +36,7 @@ public class HelixManagerProperty { private String _version; private long _healthReportLatency; private HelixCloudProperty _helixCloudProperty; + private InstanceConfig.Builder _defaultInstanceConfigBuilder; private RealmAwareZkClient.RealmAwareZkConnectionConfig _zkConnectionConfig; private RealmAwareZkClient.RealmAwareZkClientConfig _zkClientConfig; @@ -55,12 +57,13 @@ public HelixManagerProperty(Properties helixManagerProperties, CloudConfig cloud } private HelixManagerProperty(String version, long healthReportLatency, - HelixCloudProperty helixCloudProperty, + HelixCloudProperty helixCloudProperty, InstanceConfig.Builder defaultInstanceConfig, RealmAwareZkClient.RealmAwareZkConnectionConfig zkConnectionConfig, RealmAwareZkClient.RealmAwareZkClientConfig zkClientConfig) { _version = version; _healthReportLatency = healthReportLatency; _helixCloudProperty = helixCloudProperty; + _defaultInstanceConfigBuilder = defaultInstanceConfig; _zkConnectionConfig = zkConnectionConfig; _zkClientConfig = zkClientConfig; } @@ -72,6 +75,13 @@ public HelixCloudProperty getHelixCloudProperty() { return _helixCloudProperty; } + public InstanceConfig.Builder getDefaultInstanceConfigBuilder() { + if (_defaultInstanceConfigBuilder == null) { + _defaultInstanceConfigBuilder = new InstanceConfig.Builder(); + } + return _defaultInstanceConfigBuilder; + } + public String getVersion() { return _version; } @@ -92,6 +102,7 @@ public static class Builder { private String _version; private long _healthReportLatency; private HelixCloudProperty _helixCloudProperty; + private InstanceConfig.Builder _defaultInstanceConfigBuilder; private RealmAwareZkClient.RealmAwareZkConnectionConfig _zkConnectionConfig; private RealmAwareZkClient.RealmAwareZkClientConfig _zkClientConfig; @@ -100,7 +111,7 @@ public Builder() { public HelixManagerProperty build() { return new HelixManagerProperty(_version, _healthReportLatency, _helixCloudProperty, - _zkConnectionConfig, _zkClientConfig); + _defaultInstanceConfigBuilder, _zkConnectionConfig, _zkClientConfig); } public Builder setVersion(String version) { @@ -118,6 +129,12 @@ public Builder setHelixCloudProperty(HelixCloudProperty helixCloudProperty) { return this; } + public Builder setDefaultInstanceConfigBuilder( + InstanceConfig.Builder defaultInstanceConfigBuilder) { + _defaultInstanceConfigBuilder = defaultInstanceConfigBuilder; + return this; + } + public Builder setRealmAWareZkConnectionConfig( RealmAwareZkClient.RealmAwareZkConnectionConfig zkConnectionConfig) { _zkConnectionConfig = zkConnectionConfig; diff --git a/helix-core/src/main/java/org/apache/helix/manager/zk/ParticipantManager.java b/helix-core/src/main/java/org/apache/helix/manager/zk/ParticipantManager.java index f6fffd640b..abc288ec1e 100644 --- a/helix-core/src/main/java/org/apache/helix/manager/zk/ParticipantManager.java +++ b/helix-core/src/main/java/org/apache/helix/manager/zk/ParticipantManager.java @@ -58,7 +58,6 @@ import org.apache.helix.participant.statemachine.StateModelFactory; import org.apache.helix.task.TaskConstants; import org.apache.helix.task.TaskUtil; -import org.apache.helix.util.HelixUtil; import org.apache.helix.zookeeper.api.client.RealmAwareZkClient; import org.apache.helix.zookeeper.datamodel.ZNRecord; import org.apache.helix.zookeeper.datamodel.ZNRecordBucketizer; @@ -199,8 +198,7 @@ private void joinCluster() { // Difference between auto join and auto registration is that the latter will also populate the // domain information in instance config try { - autoRegistration = - Boolean.valueOf(_helixManagerProperty.getHelixCloudProperty().getCloudEnabled()); + autoRegistration = _helixManagerProperty.getHelixCloudProperty().getCloudEnabled(); LOG.info("instance: " + _instanceName + " auto-registering " + _clusterName + " is " + autoRegistration); } catch (Exception e) { @@ -215,13 +213,15 @@ private void joinCluster() { } if (!autoRegistration) { LOG.info(_instanceName + " is auto-joining cluster: " + _clusterName); - instanceConfig = HelixUtil.composeInstanceConfig(_instanceName); + instanceConfig = + _helixManagerProperty.getDefaultInstanceConfigBuilder().build(_instanceName); } else { LOG.info(_instanceName + " is auto-registering cluster: " + _clusterName); CloudInstanceInformation cloudInstanceInformation = getCloudInstanceInformation(); - String domain = cloudInstanceInformation - .get(CloudInstanceInformation.CloudInstanceField.FAULT_DOMAIN.name()) + _instanceName; - instanceConfig = HelixUtil.composeInstanceConfig(_instanceName); + String domain = cloudInstanceInformation.get( + CloudInstanceInformation.CloudInstanceField.FAULT_DOMAIN.name()) + _instanceName; + instanceConfig = + _helixManagerProperty.getDefaultInstanceConfigBuilder().build(_instanceName); instanceConfig.setDomain(domain); } instanceConfig diff --git a/helix-core/src/main/java/org/apache/helix/model/InstanceConfig.java b/helix-core/src/main/java/org/apache/helix/model/InstanceConfig.java index 45e0476ba0..252f8254d1 100644 --- a/helix-core/src/main/java/org/apache/helix/model/InstanceConfig.java +++ b/helix-core/src/main/java/org/apache/helix/model/InstanceConfig.java @@ -29,11 +29,11 @@ import java.util.Set; import java.util.stream.Collectors; -import org.apache.helix.util.ConfigStringUtil; import org.apache.helix.HelixException; import org.apache.helix.HelixProperty; import org.apache.helix.constants.InstanceConstants; import org.apache.helix.controller.rebalancer.topology.Topology; +import org.apache.helix.util.ConfigStringUtil; import org.apache.helix.util.HelixUtil; import org.apache.helix.zookeeper.datamodel.ZNRecord; import org.slf4j.Logger; @@ -68,6 +68,7 @@ public enum InstanceConfigProperty { public static final int WEIGHT_NOT_SET = -1; public static final int MAX_CONCURRENT_TASK_NOT_SET = -1; private static final int TARGET_TASK_THREAD_POOL_SIZE_NOT_SET = -1; + private static final boolean HELIX_ENABLED_DEFAULT_VALUE = true; private static final Logger _logger = LoggerFactory.getLogger(InstanceConfig.class.getName()); @@ -92,7 +93,7 @@ public InstanceConfig(ZNRecord record) { * @return the host name */ public String getHostName() { - return _record.getSimpleField(InstanceConfigProperty.HELIX_HOST.toString()); + return _record.getSimpleField(InstanceConfigProperty.HELIX_HOST.name()); } /** @@ -100,7 +101,7 @@ public String getHostName() { * @param hostName the host name */ public void setHostName(String hostName) { - _record.setSimpleField(InstanceConfigProperty.HELIX_HOST.toString(), hostName); + _record.setSimpleField(InstanceConfigProperty.HELIX_HOST.name(), hostName); } /** @@ -108,7 +109,7 @@ public void setHostName(String hostName) { * @return the port */ public String getPort() { - return _record.getSimpleField(InstanceConfigProperty.HELIX_PORT.toString()); + return _record.getSimpleField(InstanceConfigProperty.HELIX_PORT.name()); } /** @@ -116,7 +117,7 @@ public String getPort() { * @param port the port */ public void setPort(String port) { - _record.setSimpleField(InstanceConfigProperty.HELIX_PORT.toString(), port); + _record.setSimpleField(InstanceConfigProperty.HELIX_PORT.name(), port); } /** @@ -200,7 +201,7 @@ public void setWeight(int weight) { * @return a list of tags */ public List getTags() { - List tags = getRecord().getListField(InstanceConfigProperty.TAG_LIST.toString()); + List tags = getRecord().getListField(InstanceConfigProperty.TAG_LIST.name()); if (tags == null) { tags = new ArrayList(0); } @@ -212,14 +213,14 @@ public List getTags() { * @param tag an arbitrary property of the instance */ public void addTag(String tag) { - List tags = getRecord().getListField(InstanceConfigProperty.TAG_LIST.toString()); + List tags = getRecord().getListField(InstanceConfigProperty.TAG_LIST.name()); if (tags == null) { tags = new ArrayList(0); } if (!tags.contains(tag)) { tags.add(tag); } - getRecord().setListField(InstanceConfigProperty.TAG_LIST.toString(), tags); + getRecord().setListField(InstanceConfigProperty.TAG_LIST.name(), tags); } /** @@ -227,7 +228,7 @@ public void addTag(String tag) { * @param tag a property of this instance */ public void removeTag(String tag) { - List tags = getRecord().getListField(InstanceConfigProperty.TAG_LIST.toString()); + List tags = getRecord().getListField(InstanceConfigProperty.TAG_LIST.name()); if (tags == null) { return; } @@ -242,7 +243,7 @@ public void removeTag(String tag) { * @return true if the instance contains the tag, false otherwise */ public boolean containsTag(String tag) { - List tags = getRecord().getListField(InstanceConfigProperty.TAG_LIST.toString()); + List tags = getRecord().getListField(InstanceConfigProperty.TAG_LIST.name()); if (tags == null) { return false; } @@ -254,7 +255,8 @@ public boolean containsTag(String tag) { * @return true if enabled, false if disabled */ public boolean getInstanceEnabled() { - return _record.getBooleanField(InstanceConfigProperty.HELIX_ENABLED.toString(), true); + return _record.getBooleanField(InstanceConfigProperty.HELIX_ENABLED.name(), + HELIX_ENABLED_DEFAULT_VALUE); } /** @@ -280,8 +282,8 @@ private void setInstanceEnabledHelper(boolean enabled) { * Removes HELIX_DISABLED_REASON and HELIX_DISABLED_TYPE entry from simple field. */ public void resetInstanceDisabledTypeAndReason() { - _record.getSimpleFields().remove(InstanceConfigProperty.HELIX_DISABLED_REASON.toString()); - _record.getSimpleFields().remove(InstanceConfigProperty.HELIX_DISABLED_TYPE.toString()); + _record.getSimpleFields().remove(InstanceConfigProperty.HELIX_DISABLED_REASON.name()); + _record.getSimpleFields().remove(InstanceConfigProperty.HELIX_DISABLED_TYPE.name()); } /** @@ -290,7 +292,7 @@ public void resetInstanceDisabledTypeAndReason() { */ public void setInstanceDisabledReason(String disabledReason) { if (!getInstanceEnabled()) { - _record.setSimpleField(InstanceConfigProperty.HELIX_DISABLED_REASON.toString(), disabledReason); + _record.setSimpleField(InstanceConfigProperty.HELIX_DISABLED_REASON.name(), disabledReason); } } @@ -300,8 +302,8 @@ public void setInstanceDisabledReason(String disabledReason) { */ public void setInstanceDisabledType(InstanceConstants.InstanceDisabledType disabledType) { if (!getInstanceEnabled()) { - _record.setSimpleField(InstanceConfigProperty.HELIX_DISABLED_TYPE.toString(), - disabledType.toString()); + _record.setSimpleField(InstanceConfigProperty.HELIX_DISABLED_TYPE.name(), + disabledType.name()); } } @@ -309,7 +311,7 @@ public void setInstanceDisabledType(InstanceConstants.InstanceDisabledType disab * @return Return instance disabled reason. Default is am empty string. */ public String getInstanceDisabledReason() { - return _record.getStringField(InstanceConfigProperty.HELIX_DISABLED_REASON.toString(), ""); + return _record.getStringField(InstanceConfigProperty.HELIX_DISABLED_REASON.name(), ""); } /** @@ -321,8 +323,8 @@ public String getInstanceDisabledType() { if (getInstanceEnabled()) { return InstanceConstants.INSTANCE_NOT_DISABLED; } - return _record.getStringField(InstanceConfigProperty.HELIX_DISABLED_TYPE.toString(), - InstanceConstants.InstanceDisabledType.DEFAULT_INSTANCE_DISABLE_TYPE.toString()); + return _record.getStringField(InstanceConfigProperty.HELIX_DISABLED_TYPE.name(), + InstanceConstants.InstanceDisabledType.DEFAULT_INSTANCE_DISABLE_TYPE.name()); } /** @@ -468,7 +470,7 @@ public Map> getDisabledPartitionsMap() { @Deprecated public void setInstanceEnabledForPartition(String partitionName, boolean enabled) { List list = - _record.getListField(InstanceConfigProperty.HELIX_DISABLED_PARTITION.toString()); + _record.getListField(InstanceConfigProperty.HELIX_DISABLED_PARTITION.name()); Set disabledPartitions = new HashSet(); if (list != null) { disabledPartitions.addAll(list); @@ -482,7 +484,7 @@ public void setInstanceEnabledForPartition(String partitionName, boolean enabled list = new ArrayList(disabledPartitions); Collections.sort(list); - _record.setListField(InstanceConfigProperty.HELIX_DISABLED_PARTITION.toString(), list); + _record.setListField(InstanceConfigProperty.HELIX_DISABLED_PARTITION.name(), list); } public void setInstanceEnabledForPartition(String resourceName, String partitionName, @@ -718,7 +720,6 @@ public static InstanceConfig toInstanceConfig(String instanceId) { } - config.setInstanceEnabled(true); if (config.getHostName() == null) { config.setHostName(instanceId); } @@ -739,4 +740,135 @@ public boolean validateTopologySettingInInstanceConfig(ClusterConfig clusterConf false /*earlyQuitForFaultZone*/); return true; } + + public static class Builder { + private String _hostName; + private String _port; + private String _domain; + private int _weight = WEIGHT_NOT_SET; + private List _tags = new ArrayList<>(); + private boolean _instanceEnabled = HELIX_ENABLED_DEFAULT_VALUE; + private Map _instanceCapacityMap; + + /** + * Build a new InstanceConfig with given instanceId + * @param instanceId A unique ID for this instance + * @return InstanceConfig + */ + public InstanceConfig build(String instanceId) { + InstanceConfig instanceConfig = new InstanceConfig(instanceId); + + String proposedHostName = instanceId; + String proposedPort = ""; + int lastPos = instanceId.lastIndexOf("_"); + if (lastPos > 0) { + proposedHostName = instanceId.substring(0, lastPos); + proposedPort = instanceId.substring(lastPos + 1); + } + + if (_hostName != null) { + instanceConfig.setHostName(_hostName); + } else { + instanceConfig.setHostName(proposedHostName); + } + + if (_port != null) { + instanceConfig.setPort(_port); + } else { + instanceConfig.setPort(proposedPort); + } + + if (_domain != null) { + instanceConfig.setDomain(_domain); + } + + if (_weight != InstanceConfig.WEIGHT_NOT_SET) { + instanceConfig.setWeight(_weight); + } + + for (String tag : _tags) { + instanceConfig.addTag(tag); + } + + if (_instanceEnabled != HELIX_ENABLED_DEFAULT_VALUE) { + instanceConfig.setInstanceEnabled(_instanceEnabled); + } + + if (_instanceCapacityMap != null) { + instanceConfig.setInstanceCapacityMap(_instanceCapacityMap); + } + + return instanceConfig; + } + + /** + * Set the host name for this instance + * @param hostName the host name + * @return InstanceConfig.Builder + */ + public Builder setHostName(String hostName) { + _hostName = hostName; + return this; + } + + /** + * Set the port for this instance + * @param port the Helix port + * @return InstanceConfig.Builder + */ + public Builder setPort(String port) { + _port = port; + return this; + } + + /** + * Set the domain for this instance + * @param domain the domain + * @return InstanceConfig.Builder + */ + public Builder setDomain(String domain) { + _domain = domain; + return this; + } + + /** + * Set the weight for this instance + * @param weight the weight + * @return InstanceConfig.Builder + */ + public Builder setWeight(int weight) { + _weight = weight; + return this; + } + + /** + * Add a tag for this instance + * @param tag the tag + * @return InstanceConfig.Builder + */ + public Builder addTag(String tag) { + _tags.add(tag); + return this; + } + + /** + * Set the enabled status for this instance + * @param instanceEnabled true if enabled, false otherwise + * @return InstanceConfig.Builder + */ + public Builder setInstanceEnabled(boolean instanceEnabled) { + _instanceEnabled = instanceEnabled; + return this; + } + + /** + * Set the capacity map for this instance + * @param instanceCapacityMap the capacity map + * @return InstanceConfig.Builder + */ + public Builder setInstanceCapacityMap(Map instanceCapacityMap) { + _instanceCapacityMap = instanceCapacityMap; + return this; + } + } } diff --git a/helix-core/src/main/java/org/apache/helix/util/HelixUtil.java b/helix-core/src/main/java/org/apache/helix/util/HelixUtil.java index ad4b6931d4..88c33f608e 100644 --- a/helix-core/src/main/java/org/apache/helix/util/HelixUtil.java +++ b/helix-core/src/main/java/org/apache/helix/util/HelixUtil.java @@ -533,22 +533,11 @@ public static long getSystemPropertyAsLong(String propertyKey, long propertyDefa /** * Compose the config for an instance - * @param instanceName + * @param instanceName the unique name of the instance * @return InstanceConfig */ public static InstanceConfig composeInstanceConfig(String instanceName) { - InstanceConfig instanceConfig = new InstanceConfig(instanceName); - String hostName = instanceName; - String port = ""; - int lastPos = instanceName.lastIndexOf("_"); - if (lastPos > 0) { - hostName = instanceName.substring(0, lastPos); - port = instanceName.substring(lastPos + 1); - } - instanceConfig.setHostName(hostName); - instanceConfig.setPort(port); - instanceConfig.setInstanceEnabled(true); - return instanceConfig; + return new InstanceConfig.Builder().build(instanceName); } /** diff --git a/helix-core/src/test/java/org/apache/helix/TestConfigAccessor.java b/helix-core/src/test/java/org/apache/helix/TestConfigAccessor.java index d65f3f9eff..a4c6862574 100644 --- a/helix-core/src/test/java/org/apache/helix/TestConfigAccessor.java +++ b/helix-core/src/test/java/org/apache/helix/TestConfigAccessor.java @@ -116,12 +116,11 @@ public void testBasic() throws Exception { keys = configAccessor.getKeys(ConfigScopeProperty.PARTICIPANT, clusterName, "localhost_12918"); System.out.println((keys)); - Assert.assertEquals(keys.size(), 5, - "should be [HELIX_ENABLED, HELIX_ENABLED_TIMESTAMP, HELIX_HOST, HELIX_PORT, participantConfigKey]"); - Assert.assertEquals(keys.get(4), "participantConfigKey"); + Assert.assertEquals(keys.size(), 3, "should be [HELIX_HOST, HELIX_PORT, participantConfigKey]"); + Assert.assertEquals(keys.get(2), "participantConfigKey"); - keys = configAccessor - .getKeys(ConfigScopeProperty.PARTITION, clusterName, "testResource", "testPartition"); + keys = configAccessor.getKeys(ConfigScopeProperty.PARTITION, clusterName, "testResource", + "testPartition"); Assert.assertEquals(keys.size(), 1, "should be [partitionConfigKey]"); Assert.assertEquals(keys.get(0), "partitionConfigKey"); diff --git a/helix-core/src/test/java/org/apache/helix/TestHelixConfigAccessor.java b/helix-core/src/test/java/org/apache/helix/TestHelixConfigAccessor.java index d7dbae9f49..6e57913140 100644 --- a/helix-core/src/test/java/org/apache/helix/TestHelixConfigAccessor.java +++ b/helix-core/src/test/java/org/apache/helix/TestHelixConfigAccessor.java @@ -111,23 +111,20 @@ public void testBasic() throws Exception { Assert.assertEquals(keys.size(), 1, "should be [resourceConfigKey]"); Assert.assertTrue(keys.contains("resourceConfigKey")); - keys = - configAccessor.getKeys(new HelixConfigScopeBuilder(ConfigScopeProperty.CLUSTER).forCluster( - clusterName).build()); + keys = configAccessor.getKeys( + new HelixConfigScopeBuilder(ConfigScopeProperty.CLUSTER).forCluster(clusterName).build()); Assert.assertEquals(keys.size(), 1, "should be [clusterConfigKey]"); Assert.assertTrue(keys.contains("clusterConfigKey")); - keys = - configAccessor.getKeys(new HelixConfigScopeBuilder(ConfigScopeProperty.PARTICIPANT) - .forCluster(clusterName).forParticipant("localhost_12918").build()); - Assert.assertEquals(keys.size(), 5, - "should be [HELIX_ENABLED, HELIX_ENABLED_TIMESTAMP, HELIX_PORT, HELIX_HOST, participantConfigKey]"); + keys = configAccessor.getKeys( + new HelixConfigScopeBuilder(ConfigScopeProperty.PARTICIPANT).forCluster(clusterName) + .forParticipant("localhost_12918").build()); + Assert.assertEquals(keys.size(), 3, "should be [HELIX_PORT, HELIX_HOST, participantConfigKey]"); Assert.assertTrue(keys.contains("participantConfigKey")); - keys = - configAccessor.getKeys(new HelixConfigScopeBuilder(ConfigScopeProperty.PARTITION) - .forCluster(clusterName).forResource("testResource").forPartition("testPartition") - .build()); + keys = configAccessor.getKeys( + new HelixConfigScopeBuilder(ConfigScopeProperty.PARTITION).forCluster(clusterName) + .forResource("testResource").forPartition("testPartition").build()); Assert.assertEquals(keys.size(), 1, "should be [partitionConfigKey]"); Assert.assertEquals(keys.get(0), "partitionConfigKey"); diff --git a/helix-core/src/test/java/org/apache/helix/integration/manager/MockParticipantManager.java b/helix-core/src/test/java/org/apache/helix/integration/manager/MockParticipantManager.java index c6e7427a40..fc90d88b0c 100644 --- a/helix-core/src/test/java/org/apache/helix/integration/manager/MockParticipantManager.java +++ b/helix-core/src/test/java/org/apache/helix/integration/manager/MockParticipantManager.java @@ -24,6 +24,7 @@ import org.apache.helix.HelixCloudProperty; import org.apache.helix.HelixManagerProperty; +import org.apache.helix.HelixPropertyFactory; import org.apache.helix.InstanceType; import org.apache.helix.manager.zk.CallbackHandler; import org.apache.helix.mock.participant.DummyProcess.DummyLeaderStandbyStateModelFactory; @@ -58,7 +59,14 @@ public MockParticipantManager(String zkAddr, String clusterName, String instance public MockParticipantManager(String zkAddr, String clusterName, String instanceName, int transDelay, HelixCloudProperty helixCloudProperty) { - super(zkAddr, clusterName, instanceName, InstanceType.PARTICIPANT); + this(zkAddr, clusterName, instanceName, transDelay, helixCloudProperty, + HelixPropertyFactory.getInstance().getHelixManagerProperty(zkAddr, clusterName)); + } + + public MockParticipantManager(String zkAddr, String clusterName, String instanceName, + int transDelay, HelixCloudProperty helixCloudProperty, + HelixManagerProperty helixManagerProperty) { + super(clusterName, instanceName, InstanceType.PARTICIPANT, zkAddr, null, helixManagerProperty); _transDelay = transDelay; _msModelFactory = new MockMSModelFactory(null); _lsModelFactory = new DummyLeaderStandbyStateModelFactory(_transDelay); @@ -67,7 +75,8 @@ public MockParticipantManager(String zkAddr, String clusterName, String instance } public MockParticipantManager(String clusterName, String instanceName, - HelixManagerProperty helixManagerProperty, int transDelay, HelixCloudProperty helixCloudProperty) { + HelixManagerProperty helixManagerProperty, int transDelay, + HelixCloudProperty helixCloudProperty) { super(clusterName, instanceName, InstanceType.PARTICIPANT, null, null, helixManagerProperty); _transDelay = transDelay; _msModelFactory = new MockMSModelFactory(null); diff --git a/helix-core/src/test/java/org/apache/helix/integration/paticipant/TestInstanceAutoJoin.java b/helix-core/src/test/java/org/apache/helix/integration/paticipant/TestInstanceAutoJoin.java index 5d424e2277..f12cec6ba8 100644 --- a/helix-core/src/test/java/org/apache/helix/integration/paticipant/TestInstanceAutoJoin.java +++ b/helix-core/src/test/java/org/apache/helix/integration/paticipant/TestInstanceAutoJoin.java @@ -5,6 +5,7 @@ import org.apache.helix.HelixDataAccessor; import org.apache.helix.HelixException; import org.apache.helix.HelixManager; +import org.apache.helix.HelixManagerProperty; import org.apache.helix.PropertyKey; import org.apache.helix.TestHelper; import org.apache.helix.cloud.constants.CloudProvider; @@ -16,6 +17,7 @@ import org.apache.helix.model.ConfigScope; import org.apache.helix.model.HelixConfigScope; import org.apache.helix.model.IdealState.RebalanceMode; +import org.apache.helix.model.InstanceConfig; import org.apache.helix.model.builder.ConfigScopeBuilder; import org.apache.helix.model.builder.HelixConfigScopeBuilder; import org.testng.Assert; @@ -76,14 +78,56 @@ public void testInstanceAutoJoin() throws Exception { if (null == manager.getHelixDataAccessor() .getProperty(accessor.keyBuilder().liveInstance(instance2))) { Thread.sleep(100); - } else + } else { break; + } } - Assert.assertNotNull(manager.getHelixDataAccessor().getProperty(accessor.keyBuilder().liveInstance(instance2))); + Assert.assertNotNull( + manager.getHelixDataAccessor().getProperty(accessor.keyBuilder().liveInstance(instance2))); newParticipant.syncStop(); } + /** + * Test auto join with a defaultInstanceConfig. + * @throws Exception + */ + @Test(dependsOnMethods = "testInstanceAutoJoin") + public void testAutoJoinWithDefaultInstanceConfig() throws Exception { + HelixManager manager = _participants[0]; + HelixDataAccessor accessor = manager.getHelixDataAccessor(); + String instance3 = "localhost_279700"; + + // Enable cluster auto join. + HelixConfigScope scope = + new HelixConfigScopeBuilder(HelixConfigScope.ConfigScopeProperty.CLUSTER).forCluster( + CLUSTER_NAME).build(); + manager.getConfigAccessor().set(scope, ZKHelixManager.ALLOW_PARTICIPANT_AUTO_JOIN, "true"); + + // Create and start a new participant with default instance config. + InstanceConfig.Builder defaultInstanceConfig = + new InstanceConfig.Builder().setInstanceEnabled(false).addTag("foo"); + MockParticipantManager autoParticipant = + new MockParticipantManager(ZK_ADDR, CLUSTER_NAME, instance3, 10, null, + new HelixManagerProperty.Builder().setDefaultInstanceConfigBuilder( + defaultInstanceConfig).build()); + autoParticipant.syncStart(); + + Assert.assertTrue(TestHelper.verify(() -> { + // Check that live instance is added and instance config is populated with correct fields. + if (manager.getHelixDataAccessor().getProperty(accessor.keyBuilder().liveInstance(instance3)) + == null) { + return false; + } + InstanceConfig composedInstanceConfig = + manager.getConfigAccessor().getInstanceConfig(CLUSTER_NAME, instance3); + return !composedInstanceConfig.getInstanceEnabled() && composedInstanceConfig.getTags() + .contains("foo"); + }, 2000)); + + autoParticipant.syncStop(); + } + @Test(dependsOnMethods = "testInstanceAutoJoin") public void testAutoRegistration() throws Exception { // Create CloudConfig object and add to config @@ -97,7 +141,7 @@ public void testAutoRegistration() throws Exception { _gSetupTool.addResourceToCluster(CLUSTER_NAME, db3, 60, "OnlineOffline", RebalanceMode.FULL_AUTO.name(), CrushEdRebalanceStrategy.class.getName()); _gSetupTool.rebalanceStorageCluster(CLUSTER_NAME, db3, 1); - String instance3 = "localhost_279700"; + String instance4 = "localhost_279701"; ConfigScope scope = new ConfigScopeBuilder().forCluster(CLUSTER_NAME).build(); @@ -107,24 +151,24 @@ public void testAutoRegistration() throws Exception { accessor.setProperty(keyBuilder.cloudConfig(), cloudConfig); MockParticipantManager autoParticipant = - new MockParticipantManager(ZK_ADDR, CLUSTER_NAME, instance3); + new MockParticipantManager(ZK_ADDR, CLUSTER_NAME, instance4); autoParticipant.syncStart(); // if the test is run in cloud environment, auto registration will succeed and live instance // will be added, otherwise, auto registration will fail and instance config will not be // populated. An exception will be thrown. try { - manager.getConfigAccessor().getInstanceConfig(CLUSTER_NAME, instance3); + manager.getConfigAccessor().getInstanceConfig(CLUSTER_NAME, instance4); Assert.assertTrue(TestHelper.verify(() -> { if (null == manager.getHelixDataAccessor() - .getProperty(accessor.keyBuilder().liveInstance(instance3))) { + .getProperty(accessor.keyBuilder().liveInstance(instance4))) { return false; } return true; }, 2000)); } catch (HelixException e) { Assert.assertNull(manager.getHelixDataAccessor() - .getProperty(accessor.keyBuilder().liveInstance(instance3))); + .getProperty(accessor.keyBuilder().liveInstance(instance4))); } autoParticipant.syncStop(); @@ -135,11 +179,11 @@ public void testAutoRegistration() throws Exception { * class name. * @throws Exception */ - @Test + @Test(dependsOnMethods = "testAutoRegistration") public void testAutoRegistrationCustomizedFullyQualifiedInfoProcessorPath() throws Exception { HelixManager manager = _participants[0]; HelixDataAccessor accessor = manager.getHelixDataAccessor(); - String instance4 = "localhost_279707"; + String instance5 = "localhost_279702"; // Enable cluster auto join. HelixConfigScope scope = @@ -160,15 +204,15 @@ public void testAutoRegistrationCustomizedFullyQualifiedInfoProcessorPath() thro // Create and start a new participant. MockParticipantManager autoParticipant = - new MockParticipantManager(ZK_ADDR, CLUSTER_NAME, instance4); + new MockParticipantManager(ZK_ADDR, CLUSTER_NAME, instance5); autoParticipant.syncStart(); Assert.assertTrue(TestHelper.verify(() -> { // Check that live instance is added and instance config is populated with correct domain. return null != manager.getHelixDataAccessor() - .getProperty(accessor.keyBuilder().liveInstance(instance4)) && manager.getConfigAccessor() - .getInstanceConfig(CLUSTER_NAME, instance4).getDomainAsString() - .equals("rack=A:123, host=" + instance4); + .getProperty(accessor.keyBuilder().liveInstance(instance5)) && manager.getConfigAccessor() + .getInstanceConfig(CLUSTER_NAME, instance5).getDomainAsString() + .equals("rack=A:123, host=" + instance5); }, 2000)); autoParticipant.syncStop(); diff --git a/helix-core/src/test/java/org/apache/helix/model/TestInstanceConfig.java b/helix-core/src/test/java/org/apache/helix/model/TestInstanceConfig.java index 96f50e4916..2fee5ae3cd 100644 --- a/helix-core/src/test/java/org/apache/helix/model/TestInstanceConfig.java +++ b/helix-core/src/test/java/org/apache/helix/model/TestInstanceConfig.java @@ -160,8 +160,9 @@ public void testSetTargetTaskThreadPoolSize() { InstanceConfig testConfig = new InstanceConfig("testConfig"); testConfig.setTargetTaskThreadPoolSize(100); - Assert.assertEquals(testConfig.getRecord().getIntField( - InstanceConfig.InstanceConfigProperty.TARGET_TASK_THREAD_POOL_SIZE.name(), -1), 100); + Assert.assertEquals(testConfig.getRecord() + .getIntField(InstanceConfig.InstanceConfigProperty.TARGET_TASK_THREAD_POOL_SIZE.name(), -1), + 100); } @Test(expectedExceptions = IllegalArgumentException.class) @@ -169,4 +170,23 @@ public void testSetTargetTaskThreadPoolSizeIllegalArgument() { InstanceConfig testConfig = new InstanceConfig("testConfig"); testConfig.setTargetTaskThreadPoolSize(-1); } + + @Test + public void testInstanceConfigBuilder() { + Map capacityDataMap = ImmutableMap.of("weight1", 1); + InstanceConfig instanceConfig = + new InstanceConfig.Builder().setHostName("testHost").setPort("1234").setDomain("foo=bar") + .setWeight(100).setInstanceEnabled(true).addTag("tag1").addTag("tag2") + .setInstanceEnabled(false).setInstanceCapacityMap(capacityDataMap).build("instance1"); + + Assert.assertEquals(instanceConfig.getId(), "instance1"); + Assert.assertEquals(instanceConfig.getHostName(), "testHost"); + Assert.assertEquals(instanceConfig.getPort(), "1234"); + Assert.assertEquals(instanceConfig.getDomainAsString(), "foo=bar"); + Assert.assertEquals(instanceConfig.getWeight(), 100); + Assert.assertTrue(instanceConfig.getTags().contains("tag1")); + Assert.assertTrue(instanceConfig.getTags().contains("tag2")); + Assert.assertFalse(instanceConfig.getInstanceEnabled()); + Assert.assertEquals(instanceConfig.getInstanceCapacityMap().get("weight1"), Integer.valueOf(1)); + } }