org.apache.cassandra.dht.tokenallocator.NoReplicationTokenAllocator Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of cassandra-all Show documentation
Show all versions of cassandra-all Show documentation
The Apache Cassandra Project develops a highly scalable second-generation distributed database, bringing together Dynamo's fully distributed design and Bigtable's ColumnFamily-based data model.
/*
* 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.dht.tokenallocator;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.NavigableMap;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Set;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Queues;
import org.apache.cassandra.dht.IPartitioner;
import org.apache.cassandra.dht.Token;
public class NoReplicationTokenAllocator extends TokenAllocatorBase
{
PriorityQueue> sortedUnits = Queues.newPriorityQueue();
Map>> tokensInUnits = Maps.newHashMap();
private static final double MAX_TAKEOVER_RATIO = 0.90;
private static final double MIN_TAKEOVER_RATIO = 1.0 - MAX_TAKEOVER_RATIO;
public NoReplicationTokenAllocator(NavigableMap sortedTokens,
ReplicationStrategy strategy,
IPartitioner partitioner)
{
super(sortedTokens, strategy, partitioner);
}
/**
* Construct the token ring as a CircularList of TokenInfo,
* and populate the ownership of the UnitInfo's provided
*/
private TokenInfo createTokenInfos(Map> units)
{
if (units.isEmpty())
return null;
// build the circular list
TokenInfo prev = null;
TokenInfo first = null;
for (Map.Entry en : sortedTokens.entrySet())
{
Token t = en.getKey();
UnitInfo ni = units.get(en.getValue());
TokenInfo ti = new TokenInfo<>(t, ni);
first = ti.insertAfter(first, prev);
prev = ti;
}
TokenInfo curr = first;
tokensInUnits.clear();
sortedUnits.clear();
do
{
populateTokenInfoAndAdjustUnit(curr);
curr = curr.next;
} while (curr != first);
for (UnitInfo unitInfo : units.values())
{
sortedUnits.add(new Weighted(unitInfo.ownership, unitInfo));
}
return first;
}
/**
* Used in tests.
*/
protected void createTokenInfos()
{
createTokenInfos(createUnitInfos(Maps.newHashMap()));
}
private void populateTokenInfoAndAdjustUnit(TokenInfo token)
{
token.replicationStart = token.prevInRing().token;
token.replicationThreshold = token.token;
token.replicatedOwnership = token.replicationStart.size(token.token);
token.owningUnit.ownership += token.replicatedOwnership;
PriorityQueue> unitTokens = tokensInUnits.get(token.owningUnit.unit);
if (unitTokens == null)
{
unitTokens = Queues.newPriorityQueue();
tokensInUnits.put(token.owningUnit.unit, unitTokens);
}
unitTokens.add(new Weighted(token.replicatedOwnership, token));
}
private Collection generateRandomTokens(UnitInfo newUnit, int numTokens, Map> unitInfos)
{
Set tokens = new HashSet<>(numTokens);
while (tokens.size() < numTokens)
{
Token token = partitioner.getRandomToken();
if (!sortedTokens.containsKey(token))
{
tokens.add(token);
sortedTokens.put(token, newUnit.unit);
}
}
unitInfos.put(newUnit.unit, newUnit);
createTokenInfos(unitInfos);
return tokens;
}
public Collection addUnit(Unit newUnit, int numTokens)
{
assert !tokensInUnits.containsKey(newUnit);
Map