ekaterinadimitrova2 commented on code in PR #2807:
URL: https://github.com/apache/cassandra/pull/2807#discussion_r1366290625


##########
src/java/org/apache/cassandra/tcm/transformations/Register.java:
##########
@@ -0,0 +1,222 @@
+/*
+ * 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.cassandra.tcm.transformations;
+
+import java.io.IOException;
+import java.util.Map;
+import java.util.UUID;
+
+import com.google.common.annotations.VisibleForTesting;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import org.apache.cassandra.config.DatabaseDescriptor;
+import org.apache.cassandra.db.SystemKeyspace;
+import org.apache.cassandra.io.util.DataInputPlus;
+import org.apache.cassandra.io.util.DataOutputPlus;
+import org.apache.cassandra.locator.IEndpointSnitch;
+import org.apache.cassandra.tcm.ClusterMetadata;
+import org.apache.cassandra.tcm.ClusterMetadataService;
+import org.apache.cassandra.tcm.Transformation;
+import org.apache.cassandra.tcm.membership.Directory;
+import org.apache.cassandra.tcm.membership.Location;
+import org.apache.cassandra.tcm.membership.NodeAddresses;
+import org.apache.cassandra.tcm.membership.NodeId;
+import org.apache.cassandra.tcm.membership.NodeState;
+import org.apache.cassandra.tcm.membership.NodeVersion;
+import org.apache.cassandra.tcm.sequences.LockedRanges;
+import org.apache.cassandra.tcm.serialization.AsymmetricMetadataSerializer;
+import org.apache.cassandra.tcm.serialization.Version;
+import org.apache.cassandra.utils.FBUtilities;
+
+import static org.apache.cassandra.exceptions.ExceptionCode.INVALID;
+
+public class Register implements Transformation
+{
+    private static final Logger logger = 
LoggerFactory.getLogger(Register.class);
+    public static final Serializer serializer = new Serializer();
+
+    private final NodeAddresses addresses;
+    private final Location location;
+    private final NodeVersion version;
+
+    public Register(NodeAddresses addresses, Location location, NodeVersion 
version)
+    {
+        this.location = location;
+        this.version = version;
+        this.addresses = addresses;
+    }
+
+    @Override
+    public Kind kind()
+    {
+        return Kind.REGISTER;
+    }
+
+    @Override
+    public Result execute(ClusterMetadata prev)
+    {
+        for (Map.Entry<NodeId, NodeAddresses> entry : 
prev.directory.addresses.entrySet())
+        {
+            NodeAddresses existingAddresses = entry.getValue();
+            if (addresses.conflictsWith(existingAddresses))
+                return new Rejected(INVALID, String.format("New addresses %s 
conflicts with existing node %s with addresses %s", addresses, entry.getKey(), 
existingAddresses));
+        }
+
+        ClusterMetadata.Transformer next = prev.transformer()
+                                               .register(addresses, location, 
version);
+        return success(next, LockedRanges.AffectedRanges.EMPTY);
+    }
+
+    public static NodeId maybeRegister()
+    {
+        return register(false);
+    }
+
+    @VisibleForTesting
+    public static NodeId register(NodeAddresses nodeAddresses)
+    {
+        return register(nodeAddresses, NodeVersion.CURRENT);
+    }
+
+    @VisibleForTesting
+    public static NodeId register(NodeAddresses nodeAddresses, NodeVersion 
nodeVersion)
+    {
+        IEndpointSnitch snitch = DatabaseDescriptor.getEndpointSnitch();
+        Location location = new Location(snitch.getLocalDatacenter(), 
snitch.getLocalRack());
+
+        ClusterMetadata metadata = ClusterMetadata.current();
+        NodeId nodeId = 
metadata.directory.peerId(nodeAddresses.broadcastAddress);
+        if (nodeId == null || metadata.directory.peerState(nodeId) == 
NodeState.LEFT)
+        {
+            if (nodeId != null)
+                ClusterMetadataService.instance().commit(new 
Unregister(nodeId));
+            nodeId = ClusterMetadataService.instance()
+                                           .commit(new Register(nodeAddresses, 
location, nodeVersion),
+                                                   (metadata_) -> 
metadata_.directory.peerId(nodeAddresses.broadcastAddress),
+                                                   (metadata_, code, reason) 
-> {
+                                                       NodeId registered = 
metadata_.directory.peerId(nodeAddresses.broadcastAddress);
+                                                       // Check if this 
registration happened within the lifetime of this node and was done by it
+                                                       if (registered != null 
&& 
nodeAddresses.identityMatches(metadata_.directory.getNodeAddresses(registered)))
+                                                           return registered;
+
+                                                       throw new 
IllegalStateException("Can't register node: " + reason);
+                                                   });
+        }
+        else
+        {
+            throw new IllegalStateException(String.format("A node with address 
%s already exists, cancelling join. Use cassandra.replace_address if you want 
to replace this node.", nodeAddresses.broadcastAddress));
+        }
+
+        logger.info("Registering with endpoint {}", 
nodeAddresses.broadcastAddress);
+        return nodeId;
+    }
+
+    private static NodeId register(boolean force)

Review Comment:
   `force` is always `false`?



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


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to