dasahcc commented on a change in pull request #727: Add 
MetadataStoreRoutingDataWriter with DistributedLeaderElection
URL: https://github.com/apache/helix/pull/727#discussion_r377321200
 
 

 ##########
 File path: 
helix-rest/src/main/java/org/apache/helix/rest/metadatastore/concurrency/ZkDistributedLeaderElection.java
 ##########
 @@ -0,0 +1,142 @@
+package org.apache.helix.rest.metadatastore.concurrency;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import java.util.Collections;
+import java.util.List;
+
+import org.I0Itec.zkclient.IZkDataListener;
+import org.I0Itec.zkclient.exception.ZkNodeExistsException;
+import org.apache.helix.ZNRecord;
+import org.apache.helix.manager.zk.ZNRecordSerializer;
+import org.apache.helix.manager.zk.client.HelixZkClient;
+import org.apache.helix.manager.zk.zookeeper.IZkStateListener;
+import org.apache.zookeeper.CreateMode;
+import org.apache.zookeeper.Watcher;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+
+public class ZkDistributedLeaderElection implements IZkDataListener, 
IZkStateListener {
+  private static final Logger LOG = 
LoggerFactory.getLogger(ZkDistributedLeaderElection.class);
+  private static final String PREFIX = "MSDS_SERVER_";
+
+  private final HelixZkClient _zkClient;
+  private final String _basePath;
+  private final ZNRecord _participantInfo;
+  private ZNRecord _currentLeaderInfo;
+
+  private String _myEphemeralSequentialPath;
+  private volatile boolean _isLeader;
+
+  public ZkDistributedLeaderElection(HelixZkClient zkClient, String basePath,
+      ZNRecord participantInfo) {
+    synchronized (this) {
+      if (zkClient == null || zkClient.isClosed()) {
+        throw new IllegalArgumentException("ZkClient cannot be null or 
closed!");
+      }
+      _zkClient = zkClient;
+      _zkClient.setZkSerializer(new ZNRecordSerializer());
+      if (basePath == null || basePath.isEmpty()) {
+        throw new IllegalArgumentException("lockBasePath cannot be null or 
empty!");
+      }
+      _basePath = basePath;
+      _participantInfo = participantInfo;
+      _isLeader = false;
+    }
+    init();
+  }
+
+  /**
+   * Create the base path if it doesn't exist and create an ephemeral 
sequential ZNode.
+   */
+  private void init() {
+    try {
+      _zkClient.createPersistent(_basePath, true);
+    } catch (ZkNodeExistsException e) {
+      // Okay if it exists already
+    }
+
+    // Create my ephemeral sequential node with my information
+    _myEphemeralSequentialPath = _zkClient
 
 Review comment:
   Shall we create this ephemeral node first instead of creating storing 
structure?

----------------------------------------------------------------
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:
us...@infra.apache.org


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscr...@helix.apache.org
For additional commands, e-mail: reviews-h...@helix.apache.org

Reply via email to