Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#2965 Atomic ZNode creation on node registration #2978

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
* under the License.
*/

import com.google.common.collect.ImmutableMap;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
Expand Down Expand Up @@ -107,9 +106,11 @@
import org.apache.helix.zookeeper.zkclient.NetworkUtil;
import org.apache.helix.zookeeper.zkclient.exception.ZkException;
import org.apache.helix.zookeeper.zkclient.exception.ZkNoNodeException;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.Op;
import org.apache.zookeeper.OpResult;
import org.apache.zookeeper.ZooDefs;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -238,19 +239,32 @@ public void addInstance(String clusterName, InstanceConfig instanceConfig) {

ZKUtil.createChildren(_zkClient, instanceConfigsPath, instanceConfig.getRecord());

_zkClient.createPersistent(PropertyPathBuilder.instanceMessage(clusterName, nodeId), true);
_zkClient.createPersistent(PropertyPathBuilder.instanceCurrentState(clusterName, nodeId), true);
_zkClient
.createPersistent(PropertyPathBuilder.instanceTaskCurrentState(clusterName, nodeId), true);
_zkClient.createPersistent(PropertyPathBuilder.instanceCustomizedState(clusterName, nodeId), true);
_zkClient.createPersistent(PropertyPathBuilder.instanceError(clusterName, nodeId), true);
_zkClient.createPersistent(PropertyPathBuilder.instanceStatusUpdate(clusterName, nodeId), true);
_zkClient.createPersistent(PropertyPathBuilder.instanceHistory(clusterName, nodeId), true);
// if those nodes are not created atomically, then having two nodes registering almost exactly
// at the same time could be problematic, because one node would not able to check for already
// existing instance with matching logical ID (see
// InstanceUtil.findInstancesWithMatchingLogicalId call above, where finding an incomplete
// "INSTANCE" node is a killer)
final List<Op> ops = Arrays.asList(
createPersistentNodeOp(PropertyPathBuilder.instance(clusterName, nodeId)),
createPersistentNodeOp(PropertyPathBuilder.instanceMessage(clusterName, nodeId)),
createPersistentNodeOp(PropertyPathBuilder.instanceCurrentState(clusterName, nodeId)),
createPersistentNodeOp(PropertyPathBuilder.instanceTaskCurrentState(clusterName, nodeId)),
createPersistentNodeOp(PropertyPathBuilder.instanceCustomizedState(clusterName, nodeId)),
createPersistentNodeOp(PropertyPathBuilder.instanceError(clusterName, nodeId)),
createPersistentNodeOp(PropertyPathBuilder.instanceStatusUpdate(clusterName, nodeId)),
createPersistentNodeOp(PropertyPathBuilder.instanceHistory(clusterName, nodeId))
);
_zkClient.multi(ops);

HelixDataAccessor accessor = new ZKHelixDataAccessor(clusterName, _baseDataAccessor);
PropertyKey.Builder keyBuilder = accessor.keyBuilder();
accessor.setProperty(keyBuilder.participantHistory(nodeId), new ParticipantHistory(nodeId));
}

private static Op createPersistentNodeOp(String path) {
return Op.create(path, null, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
}

@Override
public void dropInstance(String clusterName, InstanceConfig instanceConfig) {
logger.info("Drop instance {} from cluster {}.", instanceConfig.getInstanceName(), clusterName);
Expand Down
Loading