All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.cassandra.locator.NetworkTopologyStrategy Maven / Gradle / Ivy

There is a newer version: 3.11.12.3
Show newest version
/*
 * 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.locator;

import java.net.InetAddress;
import java.util.*;
import java.util.Map.Entry;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.apache.cassandra.exceptions.ConfigurationException;
import org.apache.cassandra.dht.Token;
import org.apache.cassandra.locator.TokenMetadata.Topology;
import org.apache.cassandra.utils.FBUtilities;

import com.google.common.collect.Multimap;

/**
 * 

* This Replication Strategy takes a property file that gives the intended * replication factor in each datacenter. The sum total of the datacenter * replication factor values should be equal to the keyspace replication * factor. *

*

* So for example, if the keyspace replication factor is 6, the * datacenter replication factors could be 3, 2, and 1 - so 3 replicas in * one datacenter, 2 in another, and 1 in another - totalling 6. *

* This class also caches the Endpoints and invalidates the cache if there is a * change in the number of tokens. */ public class NetworkTopologyStrategy extends AbstractReplicationStrategy { private final IEndpointSnitch snitch; private final Map datacenters; private static final Logger logger = LoggerFactory.getLogger(NetworkTopologyStrategy.class); public NetworkTopologyStrategy(String keyspaceName, TokenMetadata tokenMetadata, IEndpointSnitch snitch, Map configOptions) throws ConfigurationException { super(keyspaceName, tokenMetadata, snitch, configOptions); this.snitch = snitch; Map newDatacenters = new HashMap(); if (configOptions != null) { for (Entry entry : configOptions.entrySet()) { String dc = entry.getKey(); if (dc.equalsIgnoreCase("replication_factor")) throw new ConfigurationException("replication_factor is an option for SimpleStrategy, not NetworkTopologyStrategy"); Integer replicas = Integer.valueOf(entry.getValue()); newDatacenters.put(dc, replicas); } } datacenters = Collections.unmodifiableMap(newDatacenters); logger.trace("Configured datacenter replicas are {}", FBUtilities.toString(datacenters)); } /** * calculate endpoints in one pass through the tokens by tracking our progress in each DC, rack etc. */ @SuppressWarnings("serial") public List calculateNaturalEndpoints(Token searchToken, TokenMetadata tokenMetadata) { // we want to preserve insertion order so that the first added endpoint becomes primary Set replicas = new LinkedHashSet<>(); // replicas we have found in each DC Map> dcReplicas = new HashMap<>(datacenters.size()); for (Map.Entry dc : datacenters.entrySet()) dcReplicas.put(dc.getKey(), new HashSet(dc.getValue())); Topology topology = tokenMetadata.getTopology(); // all endpoints in each DC, so we can check when we have exhausted all the members of a DC Multimap allEndpoints = topology.getDatacenterEndpoints(); // all racks in a DC so we can check when we have exhausted all racks in a DC Map> racks = topology.getDatacenterRacks(); assert !allEndpoints.isEmpty() && !racks.isEmpty() : "not aware of any cluster members"; // tracks the racks we have already placed replicas in Map> seenRacks = new HashMap<>(datacenters.size()); for (Map.Entry dc : datacenters.entrySet()) seenRacks.put(dc.getKey(), new HashSet()); // tracks the endpoints that we skipped over while looking for unique racks // when we relax the rack uniqueness we can append this to the current result so we don't have to wind back the iterator Map> skippedDcEndpoints = new HashMap<>(datacenters.size()); for (Map.Entry dc : datacenters.entrySet()) skippedDcEndpoints.put(dc.getKey(), new LinkedHashSet()); Iterator tokenIter = TokenMetadata.ringIterator(tokenMetadata.sortedTokens(), searchToken, false); while (tokenIter.hasNext() && !hasSufficientReplicas(dcReplicas, allEndpoints)) { Token next = tokenIter.next(); InetAddress ep = tokenMetadata.getEndpoint(next); String dc = snitch.getDatacenter(ep); // have we already found all replicas for this dc? if (!datacenters.containsKey(dc) || hasSufficientReplicas(dc, dcReplicas, allEndpoints)) continue; // can we skip checking the rack? if (seenRacks.get(dc).size() == racks.get(dc).keySet().size()) { dcReplicas.get(dc).add(ep); replicas.add(ep); } else { String rack = snitch.getRack(ep); // is this a new rack? if (seenRacks.get(dc).contains(rack)) { skippedDcEndpoints.get(dc).add(ep); } else { dcReplicas.get(dc).add(ep); replicas.add(ep); seenRacks.get(dc).add(rack); // if we've run out of distinct racks, add the hosts we skipped past already (up to RF) if (seenRacks.get(dc).size() == racks.get(dc).keySet().size()) { Iterator skippedIt = skippedDcEndpoints.get(dc).iterator(); while (skippedIt.hasNext() && !hasSufficientReplicas(dc, dcReplicas, allEndpoints)) { InetAddress nextSkipped = skippedIt.next(); dcReplicas.get(dc).add(nextSkipped); replicas.add(nextSkipped); } } } } } return new ArrayList(replicas); } private boolean hasSufficientReplicas(String dc, Map> dcReplicas, Multimap allEndpoints) { return dcReplicas.get(dc).size() >= Math.min(allEndpoints.get(dc).size(), getReplicationFactor(dc)); } private boolean hasSufficientReplicas(Map> dcReplicas, Multimap allEndpoints) { for (String dc : datacenters.keySet()) if (!hasSufficientReplicas(dc, dcReplicas, allEndpoints)) return false; return true; } public int getReplicationFactor() { int total = 0; for (int repFactor : datacenters.values()) total += repFactor; return total; } public int getReplicationFactor(String dc) { Integer replicas = datacenters.get(dc); return replicas == null ? 0 : replicas; } public Set getDatacenters() { return datacenters.keySet(); } public void validateOptions() throws ConfigurationException { for (Entry e : this.configOptions.entrySet()) { if (e.getKey().equalsIgnoreCase("replication_factor")) throw new ConfigurationException("replication_factor is an option for SimpleStrategy, not NetworkTopologyStrategy"); validateReplicationFactor(e.getValue()); } } public Collection recognizedOptions() { // We explicitely allow all options return null; } @Override public boolean hasSameSettings(AbstractReplicationStrategy other) { return super.hasSameSettings(other) && ((NetworkTopologyStrategy) other).datacenters.equals(datacenters); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy