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

 ##########
 File path: 
helix-core/src/main/java/org/apache/helix/manager/zk/ZKHelixManager.java
 ##########
 @@ -1285,4 +1264,87 @@ public void handleSessionEstablishmentError(Throwable 
error) throws Exception {
   public Long getSessionStartTime() {
     return _sessionStartTime;
   }
+
+  private RealmAwareZkClient buildRealmAwareZkClient() {
+    final String shardingKey = buildShardingKey();
+    PathBasedZkSerializer zkSerializer =
+        ChainedPathZkSerializer.builder(new ZNRecordSerializer()).build();
+
+    RealmAwareZkClient.RealmAwareZkConnectionConfig connectionConfig =
+        new RealmAwareZkClient.RealmAwareZkConnectionConfig.Builder()
+            .setRealmMode(RealmAwareZkClient.RealmMode.SINGLE_REALM)
+            .setZkRealmShardingKey(shardingKey)
+            .setSessionTimeout(_sessionTimeout).build();
+
+    RealmAwareZkClient.RealmAwareZkClientConfig clientConfig =
+        new RealmAwareZkClient.RealmAwareZkClientConfig();
+
+    clientConfig.setZkSerializer(zkSerializer)
+        .setConnectInitTimeout(_connectionInitTimeout)
+        .setMonitorType(_instanceType.name())
+        .setMonitorKey(_clusterName)
+        .setMonitorInstanceName(_instanceName)
+        .setMonitorRootPathOnly(isMonitorRootPathOnly());
+
+    RealmAwareZkClient newClient;
+    if (_instanceType == InstanceType.ADMINISTRATOR) {
+      newClient = buildSharedZkClient(connectionConfig, clientConfig);
+    } else {
+      newClient = buildDedicatedZkClient(connectionConfig, clientConfig);
+    }
+
+    return newClient;
+  }
+
+  private RealmAwareZkClient buildSharedZkClient(
+      RealmAwareZkClient.RealmAwareZkConnectionConfig connectionConfig,
+      RealmAwareZkClient.RealmAwareZkClientConfig clientConfig) {
+    RealmAwareZkClient zkClient;
+    // TODO: change exception type
+    try {
+      zkClient =
+          SharedZkClientFactory.getInstance().buildZkClient(connectionConfig, 
clientConfig);
+    } catch (IllegalArgumentException e) {
+      LOG.warn("Not able to build realm-aware shared ZK client for sharding 
key: {}, caused by: {}"
+              + " Fallback to HelixZkClient.", 
connectionConfig.getZkRealmShardingKey(),
+          e.getMessage());
+
+      // Fallback to HelixZkClient
+      HelixZkClient.ZkConnectionConfig helixZkConnectionConfig =
+          new HelixZkClient.ZkConnectionConfig(_zkAddress);
+      
helixZkConnectionConfig.setSessionTimeout(connectionConfig.getSessionTimeout());
+      zkClient = SharedZkClientFactory.getInstance()
+          .buildZkClient(helixZkConnectionConfig, 
clientConfig.createHelixZkClientConfig());
+    }
+
+    return zkClient;
+  }
+
+  private RealmAwareZkClient buildDedicatedZkClient(
+      RealmAwareZkClient.RealmAwareZkConnectionConfig connectionConfig,
+      RealmAwareZkClient.RealmAwareZkClientConfig clientConfig) {
+    RealmAwareZkClient zkClient;
+    // TODO: change exception type
+    try {
+      zkClient =
+          
DedicatedZkClientFactory.getInstance().buildZkClient(connectionConfig, 
clientConfig);
+    } catch (IllegalArgumentException e) {
+      LOG.warn("Not able to build realm-aware Dedicated ZK client for sharding 
key: {}, caused by: {}"
+              + " Fallback to HelixZkClient.", 
connectionConfig.getZkRealmShardingKey(),
+          e.getMessage());
+
+      // Fallback to HelixZkClient
+      HelixZkClient.ZkConnectionConfig helixZkConnectionConfig =
+          new HelixZkClient.ZkConnectionConfig(_zkAddress);
+      
helixZkConnectionConfig.setSessionTimeout(connectionConfig.getSessionTimeout());
+      zkClient = DedicatedZkClientFactory.getInstance()
+          .buildZkClient(helixZkConnectionConfig, 
clientConfig.createHelixZkClientConfig());
+    }
 
 Review comment:
   I tried. We have 2 options:
   1. As you said, we can pass in the factory as a parameter. But we have to 
make `HelixZkClientFactory` public. Shall we do this?
   `public abstract class HelixZkClientFactory implements 
RealmAwareZkClientFactory`
   
   2. It is the implemented as I do. Keep`HelixZkClientFactory` as it is. I 
prefer this considering that we don't want HelixZkClientFactory public.

----------------------------------------------------------------
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