EdColeman commented on code in PR #2569: URL: https://github.com/apache/accumulo/pull/2569#discussion_r862989046
########## 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; Review Comment: Addressed in 8a2d2cb0dc - moved the haveToken set to be in the constructor. The changes also clarify token ownership rather than lock that remained from earlier changes to remove possible confusion with lock semantics and the function of the class to token. -- 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]
