Example usage for org.apache.cassandra.dht IPartitioner getTokenValidator

List of usage examples for org.apache.cassandra.dht IPartitioner getTokenValidator

Introduction

In this page you can find the example usage for org.apache.cassandra.dht IPartitioner getTokenValidator.

Prototype

public AbstractType<?> getTokenValidator();

Source Link

Usage

From source file:com.stratio.deep.cassandra.cql.RangeUtils.java

License:Apache License

/**
 * Gets the list of token for each cluster machine.<br/>
 * The concrete class of the token depends on the partitioner used.<br/>
 *
 * @param query           the query to execute against the given session to obtain the list of tokens.
 * @param sessionWithHost the pair object containing both the session and the name of the machine to which we're connected to.
 * @param partitioner     the partitioner used in the cluster.
 * @return a map containing, for each cluster machine, the list of tokens. Tokens are not returned in any particular
 * order./*from www  .  ja  v  a 2  s  .c o m*/
 */
static Map<String, Iterable<Comparable>> fetchTokens(String query, final Pair<Session, String> sessionWithHost,
        IPartitioner partitioner) {

    ResultSet rSet = sessionWithHost.left.execute(query);

    final AbstractType tkValidator = partitioner.getTokenValidator();
    final Map<String, Iterable<Comparable>> tokens = Maps.newHashMap();

    Iterable<Pair<String, Iterable<Comparable>>> pairs = transform(rSet.all(),
            new FetchTokensRowPairFunction(sessionWithHost, tkValidator));

    for (Pair<String, Iterable<Comparable>> pair : pairs) {
        tokens.put(pair.left, pair.right);
    }

    return tokens;
}

From source file:com.stratio.deep.cassandra.cql.RangeUtils.java

License:Apache License

/**
 * Given a token, fetches the list of replica machines holding that token.
 *
 * @param token       the token whose replicas we want to fetch.
 * @param session     the connection to the cluster.
 * @param partitioner the partitioner used in the cluster.
 * @return the list of replica machines holding that token.
 *///from w  ww . j av  a 2 s.com
private static List<String> initReplicas(final Comparable token, final Session session,
        final IPartitioner partitioner) {
    final AbstractType tkValidator = partitioner.getTokenValidator();
    final Metadata metadata = session.getCluster().getMetadata();

    @SuppressWarnings("unchecked")
    Set<Host> replicas = metadata.getReplicas(quote(session.getLoggedKeyspace()),
            ByteBuffer.wrap(token.toString().getBytes()));

    return Lists.newArrayList(Iterables.transform(replicas, new Function<Host, String>() {
        @Nullable
        @Override
        public String apply(@Nullable Host input) {
            assert input != null;
            return input.getAddress().getHostName();
        }
    }));
}

From source file:com.stratio.deep.cassandra.cql.RangeUtils.java

License:Apache License

/**
 * Recursive function that splits a given token range to a given number of token ranges.
 *
 * @param range        the token range to be splitted.
 * @param partitioner  the cassandra partitioner.
 * @param bisectFactor the actual number of pieces the original token range will be splitted to.
 * @param accumulator  a token range accumulator (ne
 *///from ww  w  .  j a  v a  2  s.c  om
private static void bisectTokeRange(DeepTokenRange range, final IPartitioner partitioner,
        final int bisectFactor, final List<DeepTokenRange> accumulator) {

    final AbstractType tkValidator = partitioner.getTokenValidator();

    Token leftToken = partitioner.getTokenFactory().fromByteArray(tkValidator.decompose(range.getStartToken()));
    Token rightToken = partitioner.getTokenFactory().fromByteArray(tkValidator.decompose(range.getEndToken()));
    Token midToken = partitioner.midpoint(leftToken, rightToken);

    Comparable midpoint = (Comparable) tkValidator.compose(tkValidator.fromString(midToken.toString()));

    DeepTokenRange left = new DeepTokenRange(range.getStartToken(), midpoint, range.getReplicas());
    DeepTokenRange right = new DeepTokenRange(midpoint, range.getEndToken(), range.getReplicas());

    if (bisectFactor / 2 <= 1) {
        accumulator.add(left);
        accumulator.add(right);
    } else {
        bisectTokeRange(left, partitioner, bisectFactor / 2, accumulator);
        bisectTokeRange(right, partitioner, bisectFactor / 2, accumulator);
    }
}

From source file:com.stratio.deep.cassandra.cql.RangeUtils.java

License:Apache License

/**
 * Returns the token ranges that will be mapped to Spark partitions.
 *
 * @param config the Deep configuration object.
 * @return the list of computed token ranges.
 *///from  ww w  .j a  va  2s. co m
public static List<DeepTokenRange> getSplitsBySize(CassandraDeepJobConfig config) {

    IPartitioner p = getPartitioner(config);
    AbstractType tokenValidator = p.getTokenValidator();

    Pair<Session, String> sessionWithHost = CassandraClientProvider.getSession(config.getHost(), config, false);

    String query = new StringBuilder("CALCULATE SPLITS FROM ").append(config.getKeyspace()).append(".")
            .append(config.getTable()).append(" ESTIMATING ").append(config.getSplitSize()).toString();
    ResultSet rSet = sessionWithHost.left.execute(query);

    List<DeepTokenRange> tokens = new ArrayList<>();

    for (Row row : rSet.all()) {
        Comparable startToken = (Comparable) tokenValidator.compose(row.getBytesUnsafe("start_token"));
        Comparable endToken = (Comparable) tokenValidator.compose(row.getBytesUnsafe("end_token"));
        List<String> replicas = new ArrayList<>();
        for (InetAddress addres : row.getList("preferred_locations", InetAddress.class)) {
            replicas.add(addres.getHostName());
        }
        tokens.add(new DeepTokenRange(startToken, endToken, replicas));
    }
    return tokens;
}

From source file:com.stratio.deep.cassandra.thrift.ThriftRangeUtils.java

License:Apache License

/**
 * Builds a new {@link ThriftRangeUtils}.
 *
 * @param partitioner  the partitioner./*from  w w  w .j av  a 2 s .  c  o m*/
 * @param host         the host address.
 * @param rpcPort      the host RPC port.
 * @param keyspace     the keyspace name.
 * @param columnFamily the column family name.
 * @param splitSize    the number of rows per split.
 */
public ThriftRangeUtils(IPartitioner partitioner, String host, int rpcPort, String keyspace,
        String columnFamily, int splitSize) {
    this.host = host;
    this.rpcPort = rpcPort;
    this.splitSize = splitSize;
    this.keyspace = keyspace;
    this.columnFamily = columnFamily;
    tokenType = partitioner.getTokenValidator();
    tokenFactory = partitioner.getTokenFactory();
    minToken = (Comparable) partitioner.getMinimumToken().token;
}

From source file:kina.cql.RangeUtils.java

License:Apache License

/**
 * Given a token, fetches the list of replica machines holding that token.
 *
 * @param token       the token whose replicas we want to fetch.
 * @param session     the connection to the cluster.
 * @param partitioner the partitioner used in the cluster.
 * @return the list of replica machines holding that token.
 *///ww  w  .j av a  2s  .  co  m
private static List<String> initReplicas(final Comparable token, final Session session,
        final IPartitioner partitioner) {
    final AbstractType tkValidator = partitioner.getTokenValidator();
    final Metadata metadata = session.getCluster().getMetadata();

    @SuppressWarnings("unchecked")

    /* dynamically call getTokenReplicas code */
    Set<Host> replicas = Utils.callGetTokenReplicas(metadata, quote(session.getLoggedKeyspace()),
            token.toString());

    return Lists.newArrayList(Iterables.transform(replicas, new Function<Host, String>() {
        @Nullable
        @Override
        public String apply(@Nullable Host input) {
            assert input != null;
            return input.getAddress().getHostName();
        }
    }));
}

From source file:kina.cql.RangeUtils.java

License:Apache License

/**
 * Recursive function that splits a given token range to a given number of tolen ranges.
 *
 * @param range the token range to be splitted.
 * @param partitioner the cassandra partitioner.
 * @param bisectFactor the actual number of pieces the original token range will be splitted to.
 * @param accumulator a token range accumulator (ne
 *//*  w  w w.  j  a  v  a2 s  .  c o m*/
private static void bisectTokeRange(Range range, final IPartitioner partitioner, final int bisectFactor,
        final List<Range> accumulator) {

    final AbstractType tkValidator = partitioner.getTokenValidator();

    Token leftToken = partitioner.getTokenFactory().fromByteArray(tkValidator.decompose(range.getStartToken()));
    Token rightToken = partitioner.getTokenFactory().fromByteArray(tkValidator.decompose(range.getEndToken()));
    Token midToken = partitioner.midpoint(leftToken, rightToken);

    Comparable midpoint = (Comparable) tkValidator.compose(tkValidator.fromString(midToken.toString()));

    Range left = new Range(range.getStartToken(), midpoint, range.getReplicas());
    Range right = new Range(midpoint, range.getEndToken(), range.getReplicas());

    if (bisectFactor / 2 <= 1) {
        accumulator.add(left);
        accumulator.add(right);
    } else {
        bisectTokeRange(left, partitioner, bisectFactor / 2, accumulator);
        bisectTokeRange(right, partitioner, bisectFactor / 2, accumulator);
    }
}