Example usage for org.apache.cassandra.dht Murmur3Partitioner MAXIMUM

List of usage examples for org.apache.cassandra.dht Murmur3Partitioner MAXIMUM

Introduction

In this page you can find the example usage for org.apache.cassandra.dht Murmur3Partitioner MAXIMUM.

Prototype

long MAXIMUM

To view the source code for org.apache.cassandra.dht Murmur3Partitioner MAXIMUM.

Click Source Link

Usage

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 w  ww. j av  a2 s  .  co  m*/
    }

    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.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  www .  ja  va  2  s. c  o  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 {//from  w  w w . ja v  a  2 s .c  o  m
        throw new PermanentBackendException("Unsupported partitioner: " + partitioner);
    }
}