narendly commented on a change in pull request #846: Make ZKHelixAdmin and 
ZKHelixManager Realm-aware
URL: https://github.com/apache/helix/pull/846#discussion_r389205431
 
 

 ##########
 File path: 
helix-core/src/main/java/org/apache/helix/manager/zk/ZKHelixAdmin.java
 ##########
 @@ -89,33 +91,88 @@
 
 
 public class ZKHelixAdmin implements HelixAdmin {
+  private static final Logger LOG = 
LoggerFactory.getLogger(ZKHelixAdmin.class);
+
+
   public static final String CONNECTION_TIMEOUT = "helixAdmin.timeOutInSec";
   private static final String MAINTENANCE_ZNODE_ID = "maintenance";
   private static final int DEFAULT_SUPERCLUSTER_REPLICA = 3;
 
-  private final HelixZkClient _zkClient;
+  private final RealmAwareZkClient _zkClient;
   private final ConfigAccessor _configAccessor;
-  // true if ZKHelixAdmin was instantiated with a HelixZkClient, false 
otherwise
+  // true if ZKHelixAdmin was instantiated with a RealmAwareZkClient, false 
otherwise
   // This is used for close() to determine how ZKHelixAdmin should close the 
underlying ZkClient
   private final boolean _usesExternalZkClient;
 
   private static Logger logger = LoggerFactory.getLogger(ZKHelixAdmin.class);
 
+  /**
+   * @deprecated it is recommended to use the other constructors instead to 
avoid having to manually
+   * create and maintain a RealmAwareZkClient outside of ZkBaseDataAccessor.
+   *
+   * @param zkClient A created RealmAwareZkClient
+   */
   @Deprecated
-  public ZKHelixAdmin(HelixZkClient zkClient) {
+  public ZKHelixAdmin(RealmAwareZkClient zkClient) {
     _zkClient = zkClient;
     _configAccessor = new ConfigAccessor(zkClient);
     _usesExternalZkClient = true;
   }
 
   public ZKHelixAdmin(String zkAddress) {
     int timeOutInSec = Integer.parseInt(System.getProperty(CONNECTION_TIMEOUT, 
"30"));
-    HelixZkClient.ZkClientConfig clientConfig = new 
HelixZkClient.ZkClientConfig();
-    clientConfig.setZkSerializer(new ZNRecordSerializer())
-        .setConnectInitTimeout(timeOutInSec * 1000);
-    _zkClient = SharedZkClientFactory.getInstance()
-        .buildZkClient(new HelixZkClient.ZkConnectionConfig(zkAddress), 
clientConfig);
-    _zkClient.waitUntilConnected(timeOutInSec, TimeUnit.SECONDS);
+    RealmAwareZkClient.RealmAwareZkClientConfig clientConfig =
+        new RealmAwareZkClient.RealmAwareZkClientConfig()
+            .setConnectInitTimeout(timeOutInSec * 1000L)
+            .setZkSerializer(new ZNRecordSerializer());
+
+    RealmAwareZkClient zkClient;
+    try {
+      zkClient = new FederatedZkClient(
+          new 
RealmAwareZkClient.RealmAwareZkConnectionConfig.Builder().build(), 
clientConfig);
+    } catch (IllegalStateException | IOException | InvalidRoutingDataException 
e) {
+      LOG.info("Not able to connect on multi-realm mode. "
+          + "Connecting on single-realm mode to ZK: {}", zkAddress);
+
+      zkClient = SharedZkClientFactory.getInstance()
+          .buildZkClient(new HelixZkClient.ZkConnectionConfig(zkAddress),
+              clientConfig.createHelixZkClientConfig());
+      zkClient.waitUntilConnected(timeOutInSec, TimeUnit.SECONDS);
+    }
+
+    _zkClient = zkClient;
+    _configAccessor = new ConfigAccessor(_zkClient);
+    _usesExternalZkClient = false;
+  }
+
+  private ZKHelixAdmin(Builder builder) {
+    RealmAwareZkClient zkClient;
+    switch (builder.realmMode) {
+      case MULTI_REALM:
+        try {
+          zkClient = new 
FederatedZkClient(builder.realmAwareZkConnectionConfig,
+              builder.realmAwareZkClientConfig);
+          break;
+        } catch (IOException | InvalidRoutingDataException | 
IllegalStateException e) {
+          if (builder.zkAddress == null || builder.zkAddress.isEmpty()) {
+            throw new IllegalStateException("Not able to connect on 
multi-realm mode.", e);
+          }
+          LOG.info("Not able to connect on multi-realm mode. "
+              + "Connecting on single-realm mode to ZK: {}", 
builder.zkAddress);
+          builder.setRealmMode(RealmAwareZkClient.RealmMode.SINGLE_REALM);
 
 Review comment:
   @pkuwm 
   Please throw a HelixException here and propagate the exception. 
   Also note that per our offline discussion, there will no longer be a 
fallback to single realm mode. If multi-realm fails, we fail the creation.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to