EdColeman commented on code in PR #2569:
URL: https://github.com/apache/accumulo/pull/2569#discussion_r858064724


##########
server/base/src/main/java/org/apache/accumulo/server/conf/util/TransformToken.java:
##########
@@ -0,0 +1,198 @@
+/*
+ * 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.
+ */
+package org.apache.accumulo.server.conf.util;
+
+import static java.nio.charset.StandardCharsets.UTF_8;
+
+import java.util.Arrays;
+import java.util.Objects;
+import java.util.UUID;
+
+import org.apache.accumulo.fate.zookeeper.ZooReaderWriter;
+import org.apache.accumulo.server.conf.store.PropCacheKey;
+import org.apache.accumulo.server.conf.store.PropStoreException;
+import org.apache.zookeeper.KeeperException;
+import org.apache.zookeeper.data.Stat;
+import org.checkerframework.checker.nullness.qual.NonNull;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Provides a token used in property conversion. The token is used to limit 
the number of processes
+ * that try to create a property node and transform the legacy property format 
to the 2.1 encoded
+ * properties. Processes do not queue for a token (using a sequential node) - 
processes should look
+ * for the token to exist, and if present wait and then periodically re-check 
for the property node
+ * to be created by the process that created / has the token.
+ * <p>
+ * Features
+ * <ul>
+ * <li>Uses ephemeral node that will be removed if transform process 
terminates without
+ * completing</li>
+ * <li>Watcher not necessary - the existence of the node and uuid in data 
sufficient to detect
+ * changes.</li>
+ * </ul>
+ */
+public class TransformToken {
+  public static final String TRANSFORM_TOKEN = "/transform_token";
+  private static final Logger log = 
LoggerFactory.getLogger(TransformToken.class);
+  private final TokenUUID tokenUUID = new TokenUUID();
+  private final String path;
+  private final ZooReaderWriter zrw;
+  private boolean haveToken = false;
+
+  private TransformToken(final @NonNull PropCacheKey key, final 
ZooReaderWriter zrw) {
+    path = key.getBasePath() + TRANSFORM_TOKEN;
+    this.zrw = zrw;
+  }
+
+  /**
+   * Create a lock node in ZooKeeper using an ephemeral node. Will not throw 
and exception except on
+   * an interrupt. If the lock node is created, the returned lock will be 
locked. If another lock
+   * already exists, the lock is unlocked and the caller can decide to either 
wait for the resource
+   * to be created by the thread that created the lock, or try calling to 
{@code lock} to succeed
+   *
+   * @param key
+   *          a PropCacheKey that defines the storage location of the created 
lock and the
+   *          associated property nodes.
+   * @param zrw
+   *          a ZooReaderWriter
+   * @return an TransformLock instance.
+   * @throws PropStoreException
+   *           is the lock creation fails due to an underlying ZooKeeper 
exception.
+   */
+  public static TransformToken createToken(final @NonNull PropCacheKey key,
+      final ZooReaderWriter zrw) {
+    TransformToken token = new TransformToken(key, zrw);
+    token.haveToken = token.holdToken();
+    return token;
+  }
+
+  public boolean holdToken() {
+    if (haveToken) {
+      return true;
+    }
+    try {
+      // existence check should be lighter-weight than failing on NODE_EXISTS 
exception
+      if (zrw.exists(path)) {
+        return false;
+      }
+      // if this completes this thread has created the lock
+      zrw.putEphemeralData(path, tokenUUID.asBytes());
+      log.trace("wrote property upgrade lock: {} - {}", path, tokenUUID);
+      return true;
+    } catch (KeeperException ex) {
+      log.debug(
+          "Failed to write transform lock for " + path + " another process may 
have created one",
+          ex);
+      return false;
+    } catch (InterruptedException ex) {
+      Thread.currentThread().interrupt();
+      throw new PropStoreException("Interrupted getting transform lock", ex);
+    }
+  }
+
+  /**
+   * Return the token status
+   *
+   * @return true if this instance has created the token, false otherwise.
+   */
+  public boolean haveToken() {
+    return haveToken;
+  }
+
+  /**
+   * Verify lock is still present and valid while keeping the lock.
+   *
+   * @return true if lock is valid, false otherwise
+   */
+  public boolean validateToken() {
+    try {
+      byte[] readId = zrw.getData(path);
+      log.trace("validate token: read: {} - expected: {}", readId, tokenUUID);
+      return Arrays.equals(readId, tokenUUID.asBytes());
+    } catch (KeeperException ex) {
+      throw new PropStoreException("Failed to validate lock", ex);
+    } catch (InterruptedException ex) {
+      Thread.currentThread().interrupt();
+      throw new PropStoreException("Interrupted while validating lock", ex);
+    }
+  }
+
+  /**
+   * If the lock was created by this instance the uuid created nad the uuid 
stored in the ZooKeeper
+   * data will match.
+   */
+  public void releaseToken() {
+    try {
+      log.trace("releaseToken called - {} - exists in ZooKeeper: {}", path, 
zrw.exists(path));

Review Comment:
   Resolved with e8cbf539bd



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