This is an automated email from the ASF dual-hosted git repository.

xyuanlu pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/helix.git


The following commit(s) were added to refs/heads/master by this push:
     new 0922e1f2f Add support for providing default Instance Config to be used 
with auto-join and auto-reg (#2609)
0922e1f2f is described below

commit 0922e1f2f67c48abb3dc6adf86bfd91813bb62cd
Author: Zachary Pinto <[email protected]>
AuthorDate: Wed Sep 20 00:11:47 2023 -0700

    Add support for providing default Instance Config to be used with auto-join 
and auto-reg (#2609)
    
    Add support for setting a default InstanceConfig through 
HelixManagerProperty. This will allow InstanceConfig fields be able to have 
defaults while also allowing users to leverage autoJoin and autoRegistration.
---
 .../org/apache/helix/HelixManagerProperty.java     |  21 ++-
 .../helix/manager/zk/ParticipantManager.java       |  14 +-
 .../org/apache/helix/model/InstanceConfig.java     | 176 ++++++++++++++++++---
 .../main/java/org/apache/helix/util/HelixUtil.java |  15 +-
 .../java/org/apache/helix/TestConfigAccessor.java  |   9 +-
 .../org/apache/helix/TestHelixConfigAccessor.java  |  21 ++-
 .../manager/MockParticipantManager.java            |  13 +-
 .../paticipant/TestInstanceAutoJoin.java           |  70 ++++++--
 .../org/apache/helix/model/TestInstanceConfig.java |  24 ++-
 9 files changed, 285 insertions(+), 78 deletions(-)

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 00c065d20..8f8b6c622 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 @@ package org.apache.helix;
 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 class HelixManagerProperty {
   }
 
   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 class HelixManagerProperty {
     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 class HelixManagerProperty {
     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 class HelixManagerProperty {
 
     public HelixManagerProperty build() {
       return new HelixManagerProperty(_version, _healthReportLatency, 
_helixCloudProperty,
-          _zkConnectionConfig, _zkClientConfig);
+          _defaultInstanceConfigBuilder, _zkConnectionConfig, _zkClientConfig);
     }
 
     public Builder setVersion(String version) {
@@ -118,6 +129,12 @@ public class HelixManagerProperty {
       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 f6fffd640..abc288ec1 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.StateModel;
 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 @@ public class ParticipantManager {
     // 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 @@ public class ParticipantManager {
       }
       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 45e0476ba..252f8254d 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.Map;
 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 class InstanceConfig extends HelixProperty {
   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 class InstanceConfig extends HelixProperty {
    * @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 class InstanceConfig extends HelixProperty {
    * @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 class InstanceConfig extends HelixProperty {
    * @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 class InstanceConfig extends HelixProperty {
    * @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 class InstanceConfig extends HelixProperty {
    * @return a list of tags
    */
   public List<String> getTags() {
-    List<String> tags = 
getRecord().getListField(InstanceConfigProperty.TAG_LIST.toString());
+    List<String> tags = 
getRecord().getListField(InstanceConfigProperty.TAG_LIST.name());
     if (tags == null) {
       tags = new ArrayList<String>(0);
     }
@@ -212,14 +213,14 @@ public class InstanceConfig extends HelixProperty {
    * @param tag an arbitrary property of the instance
    */
   public void addTag(String tag) {
-    List<String> tags = 
getRecord().getListField(InstanceConfigProperty.TAG_LIST.toString());
+    List<String> tags = 
getRecord().getListField(InstanceConfigProperty.TAG_LIST.name());
     if (tags == null) {
       tags = new ArrayList<String>(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 class InstanceConfig extends HelixProperty {
    * @param tag a property of this instance
    */
   public void removeTag(String tag) {
-    List<String> tags = 
getRecord().getListField(InstanceConfigProperty.TAG_LIST.toString());
+    List<String> tags = 
getRecord().getListField(InstanceConfigProperty.TAG_LIST.name());
     if (tags == null) {
       return;
     }
@@ -242,7 +243,7 @@ public class InstanceConfig extends HelixProperty {
    * @return true if the instance contains the tag, false otherwise
    */
   public boolean containsTag(String tag) {
-    List<String> tags = 
getRecord().getListField(InstanceConfigProperty.TAG_LIST.toString());
+    List<String> tags = 
getRecord().getListField(InstanceConfigProperty.TAG_LIST.name());
     if (tags == null) {
       return false;
     }
@@ -254,7 +255,8 @@ public class InstanceConfig extends HelixProperty {
    * @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 @@ public class InstanceConfig extends HelixProperty {
    * 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 class InstanceConfig extends HelixProperty {
    */
   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 class InstanceConfig extends HelixProperty {
    */
   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 class InstanceConfig extends HelixProperty {
    * @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 class InstanceConfig extends HelixProperty {
     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 class InstanceConfig extends HelixProperty {
   @Deprecated
   public void setInstanceEnabledForPartition(String partitionName, boolean 
enabled) {
     List<String> list =
-        
_record.getListField(InstanceConfigProperty.HELIX_DISABLED_PARTITION.toString());
+        
_record.getListField(InstanceConfigProperty.HELIX_DISABLED_PARTITION.name());
     Set<String> disabledPartitions = new HashSet<String>();
     if (list != null) {
       disabledPartitions.addAll(list);
@@ -482,7 +484,7 @@ public class InstanceConfig extends HelixProperty {
 
     list = new ArrayList<String>(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 class InstanceConfig extends HelixProperty {
 
     }
 
-    config.setInstanceEnabled(true);
     if (config.getHostName() == null) {
       config.setHostName(instanceId);
     }
@@ -739,4 +740,135 @@ public class InstanceConfig extends HelixProperty {
         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<String> _tags = new ArrayList<>();
+    private boolean _instanceEnabled = HELIX_ENABLED_DEFAULT_VALUE;
+    private Map<String, Integer> _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<String, Integer> 
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 ad4b6931d..88c33f608 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 final class HelixUtil {
 
   /**
    * 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 d65f3f9ef..a4c686257 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 class TestConfigAccessor extends ZkUnitTestBase {
 
     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 d7dbae9f4..6e5791314 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 class TestHelixConfigAccessor extends 
ZkUnitTestBase {
     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 c6e7427a4..fc90d88b0 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 java.util.concurrent.CountDownLatch;
 
 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 class MockParticipantManager extends ClusterManager {
 
   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 class MockParticipantManager extends ClusterManager {
   }
 
   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 5d424e227..f12cec6ba 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 java.util.Collections;
 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.CloudConfig;
 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 class TestInstanceAutoJoin extends 
ZkStandAloneCMTestBase {
       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 class TestInstanceAutoJoin extends 
ZkStandAloneCMTestBase {
 
     _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 class TestInstanceAutoJoin extends 
ZkStandAloneCMTestBase {
     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 class TestInstanceAutoJoin extends 
ZkStandAloneCMTestBase {
    * 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 class TestInstanceAutoJoin extends 
ZkStandAloneCMTestBase {
 
     // 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 96f50e491..2fee5ae3c 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 class TestInstanceConfig {
     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 class TestInstanceConfig {
     InstanceConfig testConfig = new InstanceConfig("testConfig");
     testConfig.setTargetTaskThreadPoolSize(-1);
   }
+
+  @Test
+  public void testInstanceConfigBuilder() {
+    Map<String, Integer> 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));
+  }
 }


Reply via email to