abstractdog commented on code in PR #427:
URL: https://github.com/apache/tez/pull/427#discussion_r2565261793


##########
tez-dag/src/main/java/org/apache/tez/dag/api/client/registry/zookeeper/ZkAMRegistry.java:
##########
@@ -0,0 +1,175 @@
+/**
+ * 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
+ * <p>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p>
+ * 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.
+ */
+
+package org.apache.tez.dag.api.client.registry.zookeeper;
+
+import java.nio.charset.StandardCharsets;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+
+import org.apache.curator.RetryLoop;
+import org.apache.curator.RetryPolicy;
+import org.apache.curator.framework.CuratorFramework;
+import org.apache.hadoop.classification.InterfaceAudience;
+import org.apache.hadoop.conf.Configuration;
+import org.apache.hadoop.registry.client.binding.RegistryUtils;
+import org.apache.hadoop.yarn.api.records.ApplicationId;
+import org.apache.tez.client.registry.AMRecord;
+import org.apache.tez.client.registry.AMRegistry;
+import org.apache.tez.client.registry.zookeeper.ZkConfig;
+import org.apache.zookeeper.CreateMode;
+import org.apache.zookeeper.KeeperException;
+import org.apache.zookeeper.data.Stat;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Curator/Zookeeper impl of AMRegistry (for internal use only)
+ * Clients should use 
org.apache.tez.dag.api.client.registry.zookeeper.ZkAMRegistryClient instead.
+ */
[email protected]
+public class ZkAMRegistry extends AMRegistry {
+
+  private static final Logger LOG = 
LoggerFactory.getLogger(ZkAMRegistry.class);
+
+  private final List<AMRecord> amRecords = new ArrayList<>();
+  private final String externalId;
+
+  private CuratorFramework client = null;
+  private String namespace = null;
+  private ZkConfig zkConfig = null;
+  private boolean started = false;
+
+  public ZkAMRegistry(String externalId) {
+    super("ZkAMRegistry");
+    this.externalId = externalId;
+  }
+
+  @Override
+  public void serviceInit(Configuration conf) {
+    if (zkConfig == null) {
+      zkConfig = new ZkConfig(conf);
+      this.client = zkConfig.createCuratorFramework();
+      this.namespace = zkConfig.getZkNamespace();
+      LOG.info("AMRegistryZkImpl initialized");
+    }
+  }
+
+  @Override
+  public void serviceStart() throws Exception {
+    if (!started) {
+      client.start();
+      started = true;
+      LOG.info("AMRegistryZkImpl started");
+    }
+  }
+
+  //Deletes from Zookeeper AMRecords that were added by this instance
+  @Override
+  public void serviceStop() throws Exception {
+    List<AMRecord> records = new ArrayList<>(amRecords);
+    for (AMRecord amRecord : records) {
+      remove(amRecord);
+    }
+    client.close();
+    LOG.info("AMRegistryZkImpl shutdown");
+  }
+
+  //Serialize AMRecord to ServiceRecord and deliver the JSON bytes to
+  //zkNode at the path:  <TEZ_AM_REGISTRY_NAMESPACE>/<appId>
+  @Override
+  public void add(AMRecord server) throws Exception {
+    RegistryUtils.ServiceRecordMarshal marshal = new 
RegistryUtils.ServiceRecordMarshal();
+    String json = marshal.toJson(server.toServiceRecord());
+    try {
+      final String path = namespace + "/" + 
server.getApplicationId().toString();
+      client.setData().forPath(path, json.getBytes(StandardCharsets.UTF_8));
+      LOG.info("Added AMRecord to zkpath {}", path);
+    } catch (KeeperException.NoNodeException nne) {
+      
client.create().creatingParentContainersIfNeeded().withMode(CreateMode.EPHEMERAL)
+          .forPath(namespace + "/" + server.getApplicationId().toString(), 
json.getBytes(StandardCharsets.UTF_8));
+    }
+    amRecords.add(server);
+  }
+
+  @Override
+  public void remove(AMRecord server) throws Exception {
+    amRecords.remove(server);
+    final String path = namespace + "/" + server.getApplicationId().toString();
+    client.delete().forPath(path);
+    LOG.info("Deleted AMRecord from zkpath {}", path);
+  }
+
+  @Override
+  public ApplicationId generateNewId() throws Exception {
+    createNamespaceIfNotExists();
+    long namespaceCreationTime = getNamespaceCreationTime();
+
+    boolean success = false;
+    long startTime = System.currentTimeMillis();
+    RetryPolicy retryPolicy = zkConfig.getRetryPolicy();
+    int tryId = 0;
+    for (int i = 0; (i < zkConfig.getCuratorMaxRetries()) && !success; i++) {
+      List<String> children = client.getChildren().forPath(namespace);
+      if (children != null && !children.isEmpty()) {
+        children.sort(Collections.reverseOrder());
+        String last = children.getFirst();
+        ApplicationId lastAppId = ApplicationId.fromString(last);
+        tryId = lastAppId.getId() + 1;

Review Comment:
   thread safety is indeed concerning, I was able to reproduce the problem with 
a unit test case



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

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to