Java Code Examples for org.apache.cassandra.dht.Token#TokenFactory

The following examples show how to use org.apache.cassandra.dht.Token#TokenFactory . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ThriftRangeUtilsTest.java    From deep-spark with Apache License 2.0 6 votes vote down vote up
private static <K extends Comparable, T extends Token<K>> void testDeepTokenRanges(IPartitioner<T> partitioner,
                                                                                   K startToken,
                                                                                   K endToken,
                                                                                   List<String> endpoints,
                                                                                   List<DeepTokenRange> expectedRanges) {

    ThriftRangeUtils utils = new ThriftRangeUtils(partitioner, "", 0, "", "", 0);

    Token.TokenFactory tokenFactory = partitioner.getTokenFactory();
    AbstractType tokenType = partitioner.getTokenValidator();
    String start = tokenFactory.toString(tokenFactory.fromByteArray(tokenType.decompose(startToken)));
    String end = tokenFactory.toString(tokenFactory.fromByteArray(tokenType.decompose(endToken)));
    CfSplit thriftSplit = new CfSplit(start, end, 0);
    List<DeepTokenRange> actualRanges = utils.deepTokenRanges(Arrays.asList(thriftSplit), endpoints);
    assertEquals(actualRanges, expectedRanges);
}
 
Example 2
Source File: CassandraThriftStoreManager.java    From titan1withtp3.1 with Apache License 2.0 5 votes vote down vote up
@Override
public List<KeyRange> getLocalKeyPartition() throws BackendException {
    CTConnection conn = null;
    IPartitioner partitioner = getCassandraPartitioner();

    // DAVID CASSANDRA
    // if (!(partitioner instanceof AbstractByteOrderedPartitioner))
    if (!(partitioner instanceof ByteOrderedPartitioner))
        throw new UnsupportedOperationException("getLocalKeyPartition() only supported by byte ordered partitioner.");

    Token.TokenFactory tokenFactory = partitioner.getTokenFactory();

    try {
        // Resist the temptation to describe SYSTEM_KS.  It has no ring.
        // Instead, we'll create our own keyspace (or check that it exists), then describe it.
        ensureKeyspaceExists(keySpaceName);

        conn = pool.borrowObject(keySpaceName);
        List<TokenRange> ranges  = conn.getClient().describe_ring(keySpaceName);
        List<KeyRange> keyRanges = new ArrayList<KeyRange>(ranges.size());

        for (TokenRange range : ranges) {
            if (!NetworkUtil.hasLocalAddress(range.endpoints))
                continue;

            keyRanges.add(CassandraHelper.transformRange(tokenFactory.fromString(range.start_token), tokenFactory.fromString(range.end_token)));
        }

        return keyRanges;
    } catch (Exception e) {
        throw CassandraThriftKeyColumnValueStore.convertException(e);
    } finally {
        pool.returnObjectUnsafe(keySpaceName, conn);
    }
}
 
Example 3
Source File: SystemKeyspace.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
private static Set<String> tokensAsSet(Collection<Token> tokens)
{
    Token.TokenFactory factory = StorageService.getPartitioner().getTokenFactory();
    Set<String> s = new HashSet<>(tokens.size());
    for (Token tk : tokens)
        s.add(factory.toString(tk));
    return s;
}
 
Example 4
Source File: SystemKeyspace.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
private static Collection<Token> deserializeTokens(Collection<String> tokensStrings)
{
    Token.TokenFactory factory = StorageService.getPartitioner().getTokenFactory();
    List<Token> tokens = new ArrayList<Token>(tokensStrings.size());
    for (String tk : tokensStrings)
        tokens.add(factory.fromString(tk));
    return tokens;
}
 
Example 5
Source File: HintedHandOffManager.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
public List<String> listEndpointsPendingHints()
{
    Token.TokenFactory tokenFactory = StorageService.getPartitioner().getTokenFactory();

    // Extract the keys as strings to be reported.
    LinkedList<String> result = new LinkedList<>();
    for (Row row : getHintsSlice(1))
    {
        if (row.cf != null) //ignore removed rows
            result.addFirst(tokenFactory.toString(row.key.getToken()));
    }
    return result;
}
 
Example 6
Source File: StorageService.java    From stratio-cassandra with Apache License 2.0 4 votes vote down vote up
private List<TokenRange> describeRing(String keyspace, boolean includeOnlyLocalDC) throws InvalidRequestException
{
    if (!Schema.instance.getKeyspaces().contains(keyspace))
        throw new InvalidRequestException("No such keyspace: " + keyspace);

    if (keyspace == null || Keyspace.open(keyspace).getReplicationStrategy() instanceof LocalStrategy)
        throw new InvalidRequestException("There is no ring for the keyspace: " + keyspace);

    List<TokenRange> ranges = new ArrayList<>();
    Token.TokenFactory tf = getPartitioner().getTokenFactory();

    Map<Range<Token>, List<InetAddress>> rangeToAddressMap =
            includeOnlyLocalDC
                    ? getRangeToAddressMapInLocalDC(keyspace)
                    : getRangeToAddressMap(keyspace);

    for (Map.Entry<Range<Token>, List<InetAddress>> entry : rangeToAddressMap.entrySet())
    {
        Range range = entry.getKey();
        List<InetAddress> addresses = entry.getValue();
        List<String> endpoints = new ArrayList<>(addresses.size());
        List<String> rpc_endpoints = new ArrayList<>(addresses.size());
        List<EndpointDetails> epDetails = new ArrayList<>(addresses.size());

        for (InetAddress endpoint : addresses)
        {
            EndpointDetails details = new EndpointDetails();
            details.host = endpoint.getHostAddress();
            details.datacenter = DatabaseDescriptor.getEndpointSnitch().getDatacenter(endpoint);
            details.rack = DatabaseDescriptor.getEndpointSnitch().getRack(endpoint);

            endpoints.add(details.host);
            rpc_endpoints.add(getRpcaddress(endpoint));

            epDetails.add(details);
        }

        TokenRange tr = new TokenRange(tf.toString(range.left.getToken()), tf.toString(range.right.getToken()), endpoints)
                                .setEndpoint_details(epDetails)
                                .setRpc_endpoints(rpc_endpoints);

        ranges.add(tr);
    }

    return ranges;
}
 
Example 7
Source File: BulkLoader.java    From stratio-cassandra with Apache License 2.0 4 votes vote down vote up
@Override
public void init(String keyspace)
{
    Iterator<InetAddress> hostiter = hosts.iterator();
    while (hostiter.hasNext())
    {
        try
        {
            // Query endpoint to ranges map and schemas from thrift
            InetAddress host = hostiter.next();
            Cassandra.Client client = createThriftClient(host.getHostAddress(), rpcPort, this.user, this.passwd, this.transportFactory);

            setPartitioner(client.describe_partitioner());
            Token.TokenFactory tkFactory = getPartitioner().getTokenFactory();

            for (TokenRange tr : client.describe_ring(keyspace))
            {
                Range<Token> range = new Range<>(tkFactory.fromString(tr.start_token), tkFactory.fromString(tr.end_token), getPartitioner());
                for (String ep : tr.endpoints)
                {
                    addRangeForEndpoint(range, InetAddress.getByName(ep));
                }
            }

            String cfQuery = String.format("SELECT * FROM %s.%s WHERE keyspace_name = '%s'",
                                         Keyspace.SYSTEM_KS,
                                         SystemKeyspace.SCHEMA_COLUMNFAMILIES_CF,
                                         keyspace);
            CqlResult cfRes = client.execute_cql3_query(ByteBufferUtil.bytes(cfQuery), Compression.NONE, ConsistencyLevel.ONE);


            for (CqlRow row : cfRes.rows)
            {
                String columnFamily = UTF8Type.instance.getString(row.columns.get(1).bufferForName());
                String columnsQuery = String.format("SELECT * FROM %s.%s WHERE keyspace_name = '%s' AND columnfamily_name = '%s'",
                                                    Keyspace.SYSTEM_KS,
                                                    SystemKeyspace.SCHEMA_COLUMNS_CF,
                                                    keyspace,
                                                    columnFamily);
                CqlResult columnsRes = client.execute_cql3_query(ByteBufferUtil.bytes(columnsQuery), Compression.NONE, ConsistencyLevel.ONE);

                CFMetaData metadata = CFMetaData.fromThriftCqlRow(row, columnsRes);
                knownCfs.put(metadata.cfName, metadata);
            }
            break;
        }
        catch (Exception e)
        {
            if (!hostiter.hasNext())
                throw new RuntimeException("Could not retrieve endpoint ranges: ", e);
        }
    }
}