List of usage examples for org.apache.cassandra.dht RandomPartitioner MAXIMUM
BigInteger MAXIMUM
To view the source code for org.apache.cassandra.dht RandomPartitioner MAXIMUM.
Click Source Link
From source file:com.spotify.cassandra.opstools.autobalance.Main.java
License:Apache License
private void run(CommandLine cmd) throws IOException, InterruptedException { boolean dryrun = cmd.hasOption("d"); boolean force = cmd.hasOption("f"); boolean noresolve = cmd.hasOption("r"); int port = cmd.hasOption("p") ? Integer.parseInt(cmd.getOptionValue("p")) : 7199; String nodehost = cmd.hasOption("h") ? cmd.getOptionValue("h") : "localhost"; System.out.println("Collecting information about the cluster..."); NodeProbe nodeProbe = new NodeProbe(nodehost, port); if (nodeProbe.getTokens().size() != 1) { System.err.println("Cluster is using vnodes and should already be automatically balanced!"); System.exit(1);//from ww w.jav a2 s . c om } boolean hasData = false; if (!dryrun) { Map<String, String> loadMap = nodeProbe.getLoadMap(); for (String s : loadMap.values()) { if (s.contains("KB")) continue; if (s.contains("MB") || s.contains("GB") || s.contains("TB")) { hasData = true; continue; } throw new RuntimeException("Unknown suffix in load map; don't dare to continue"); } } String partitioner = nodeProbe.getPartitioner(); BigInteger minToken, maxToken; if (partitioner.equals(RandomPartitioner.class.getName())) { minToken = RandomPartitioner.ZERO; maxToken = RandomPartitioner.MAXIMUM; } else if (partitioner.equals(Murmur3Partitioner.class.getName())) { minToken = BigInteger.valueOf(Murmur3Partitioner.MINIMUM.token); maxToken = BigInteger.valueOf(Murmur3Partitioner.MAXIMUM); } else { throw new RuntimeException("Unsupported partitioner: " + partitioner); } // Get current mapping of all live nodes List<String> liveNodes = nodeProbe.getLiveNodes(); Map<String, BigInteger> hostTokenMap = new HashMap<String, BigInteger>(); Map<String, String> hostDcMap = new HashMap<String, String>(); for (String host : liveNodes) { String dc = nodeProbe.getEndpointSnitchInfoProxy().getDatacenter(host); String decoratedHost = host; if (!noresolve) { // Prefix host with canonical host name. // This makes things prettier and also causes tokens to be assigned in logical order. decoratedHost = InetAddress.getByName(host).getCanonicalHostName() + "/" + host; } else { decoratedHost = "/" + host; } hostDcMap.put(decoratedHost, dc); List<String> tokens = nodeProbe.getTokens(host); if (tokens.size() > 1) { throw new RuntimeException("vnodes not supported"); } if (tokens.size() == 0) { throw new RuntimeException("No token for " + host + "; aborting"); } hostTokenMap.put(decoratedHost, new BigInteger(tokens.get(0))); } Balancer balancer = new Balancer(hostTokenMap, hostDcMap, minToken, maxToken); Map<String, BigInteger> newMap = balancer.balance(); List<Operation> operations = new ArrayList<Operation>(); boolean movesNeeded = false; for (Map.Entry<String, BigInteger> entry : hostTokenMap.entrySet()) { String host = entry.getKey(); BigInteger oldToken = entry.getValue(); BigInteger newToken = newMap.get(host); if (!oldToken.equals(newToken)) { movesNeeded = true; } operations.add(new Operation(host, hostDcMap.get(host), oldToken, newToken)); } if (movesNeeded && hasData && !dryrun && !force) { dryrun = true; System.out.println( "The cluster is unbalanced but has data, so no operations will actually be carried out. Use --force if you want the cluster to balance anyway."); } Collections.sort(operations); boolean unbalanced = false, moved = false; for (Operation op : operations) { if (op.oldToken.equals(op.newToken)) { System.out.println(op.host + ": Stays on token " + op.oldToken); } else { System.out.println(op.host + ": Moving from token " + op.oldToken + " to token " + op.newToken); if (!dryrun) { String ip = op.host.substring(op.host.lastIndexOf("/") + 1); NodeProbe np = new NodeProbe(ip, 7199); np.move(op.newToken.toString()); moved = true; } else { unbalanced = true; } } } if (!unbalanced && moved) { System.out.println("The cluster is now balanced!"); } }
From source file:com.spotify.hdfs2cass.CassandraPartitionerTest.java
License:Apache License
@Test public void testGetPartition() throws Exception { final int maxNodes = 5; final List<String> tokenRanges = new ArrayList<String>(); BigInteger start = BigInteger.ZERO; BigInteger step = RandomPartitioner.MAXIMUM.divide(BigInteger.valueOf(maxNodes)); for (int i = 0; i < maxNodes - 1; i++) { BigInteger end = start.add(step); tokenRanges.add(String.format("%d:%d", start, end)); start = end.add(BigInteger.ONE); }/*from www . j a v a2s .c o m*/ tokenRanges.add(String.format("%d:0", start)); final JobConf conf = new JobConf(); conf.set(ClusterInfo.SPOTIFY_CASSANDRA_TOKENS_PARAM, StringUtils.join(tokenRanges, ",")); conf.set(ClusterInfo.SPOTIFY_CASSANDRA_PARTITIONER_PARAM, "org.apache.cassandra.dht.RandomPartitioner"); CassandraPartitioner instance = new CassandraPartitioner(); instance.configure(conf); Text key = new Text("foobar"); assertEquals(2, instance.getPartition(key, null, 5)); key = new Text("someotherkey"); assertEquals(1, instance.getPartition(key, null, 5)); key = new Text("1ce5cf4b861941f4aa799ae39ac9daa4"); assertEquals(4, instance.getPartition(key, null, 5)); }
From source file:com.spotify.scio.cassandra.CompatUtil.java
License:Apache License
public static BigInteger maxToken(String partitioner) { switch (partitioner) { case "org.apache.cassandra.dht.RandomPartitioner": return RandomPartitioner.MAXIMUM.subtract(BigInteger.ONE); case "org.apache.cassandra.dht.Murmur3Partitioner": return BigInteger.valueOf(Murmur3Partitioner.MAXIMUM); default:/*from w ww.j a v a2s .co m*/ throw new IllegalArgumentException("Unsupported partitioner " + partitioner); } }
From source file:org.janusgraph.diskstorage.cassandra.embedded.CassandraEmbeddedKeyColumnValueStore.java
License:Apache License
private static Token getMaximumToken() throws PermanentBackendException { IPartitioner partitioner = StorageService.getPartitioner(); if (partitioner instanceof RandomPartitioner) { return new BigIntegerToken(RandomPartitioner.MAXIMUM); } else if (partitioner instanceof Murmur3Partitioner) { return new LongToken(Murmur3Partitioner.MAXIMUM); } else if (partitioner instanceof ByteOrderedPartitioner) { //TODO: This makes the assumption that its an EdgeStore (i.e. 8 byte keys) return new BytesToken(org.janusgraph.diskstorage.util.ByteBufferUtil.oneByteBuffer(8)); } else {/* w w w. j a v a 2 s . c o m*/ throw new PermanentBackendException("Unsupported partitioner: " + partitioner); } }