blerer commented on code in PR #2807: URL: https://github.com/apache/cassandra/pull/2807#discussion_r1381691188
########## src/java/org/apache/cassandra/tcm/Discovery.java: ########## @@ -0,0 +1,249 @@ +/* + * 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; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashSet; +import java.util.Set; +import java.util.concurrent.ConcurrentSkipListSet; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicReference; + +import com.google.common.collect.Sets; +import com.google.common.util.concurrent.Uninterruptibles; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import org.apache.cassandra.config.DatabaseDescriptor; +import org.apache.cassandra.db.TypeSizes; +import org.apache.cassandra.io.IVersionedSerializer; +import org.apache.cassandra.io.util.DataInputPlus; +import org.apache.cassandra.io.util.DataOutputPlus; +import org.apache.cassandra.locator.InetAddressAndPort; +import org.apache.cassandra.net.IVerbHandler; +import org.apache.cassandra.net.Message; +import org.apache.cassandra.net.MessagingService; +import org.apache.cassandra.net.NoPayload; +import org.apache.cassandra.net.Verb; +import org.apache.cassandra.tcm.migration.Election; +import org.apache.cassandra.utils.Clock; +import org.apache.cassandra.utils.FBUtilities; +import org.apache.cassandra.utils.Pair; + +/** + * Service in charge of discovering nodes + */ +public class Discovery +{ + private static final Logger logger = LoggerFactory.getLogger(Discovery.class); + + public static final Discovery instance = new Discovery(); + public static final Serializer serializer = new Serializer(); + + public final DiscoveryRequestHandler requestHandler = new DiscoveryRequestHandler(); + private final Set<InetAddressAndPort> discovered = new ConcurrentSkipListSet<>(); + private final AtomicReference<State> state = new AtomicReference<>(State.NOT_STARTED); + + public DiscoveredNodes discover() + { + return discover(5); + } + + /** + * The discovery process starts with the configured seeds - a message is sent to them asking for the list of the + * known CMS members. If a seed node knows about CMS members, it responds with a collection of only those nodes. + * Otherwise, the seed node responds with a collection of all known peers. They are accumulated as new target + * nodes to query for CMS members in the next round. The process completes when either: + * - a response containing the CMS members is received + * - a deadline or a specified maximum number of rounds is reached + */ + public DiscoveredNodes discover(int rounds) + { + boolean res = state.compareAndSet(State.NOT_STARTED, State.IN_PROGRESS); + assert res : String.format("Can not start discovery as it is in state %s", state.get()); Review Comment: The code relies on this assertion to behave correctly. Should it not be a real check? -- 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]

