Java Code Examples for org.apache.cassandra.service.StorageService#getPartitioner()

The following examples show how to use org.apache.cassandra.service.StorageService#getPartitioner() . 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: SSTableAttachedSecondaryIndexTest.java    From sasi with Apache License 2.0 6 votes vote down vote up
private static List<Row> getIndexed(ColumnFamilyStore store, IDiskAtomFilter columnFilter, DecoratedKey startKey, int maxResults, IndexExpression... expressions)
{
    IPartitioner p = StorageService.getPartitioner();
    AbstractBounds<RowPosition> bounds;

    if (startKey == null)
    {
        bounds = new Range<>(p.getMinimumToken(), p.getMinimumToken()).toRowBounds();
    }
    else
    {
        bounds = new Bounds<>(startKey, p.getMinimumToken().maxKeyBound(p));
    }

    return store.indexManager.search(ExtendedFilter.create(store,
                                     new DataRange(bounds, columnFilter),
                                     Arrays.asList(expressions),
                                     maxResults,
                                     false,
                                     System.currentTimeMillis()));
}
 
Example 2
Source File: OperationTest.java    From sasi with Apache License 2.0 6 votes vote down vote up
@Test
public void testSatisfiedByWithMultipleTerms()
{
    final ByteBuffer comment = UTF8Type.instance.decompose("comment");
    final ColumnFamilyStore store = Keyspace.open("sasecondaryindex").getColumnFamilyStore("saindexed1");
    final IPartitioner<?> partitioner = StorageService.getPartitioner();

    ColumnFamily cf = ArrayBackedSortedColumns.factory.create(store.metadata);
    cf.addColumn(new Column(comment, UTF8Type.instance.decompose("software engineer is working on a project"), System.currentTimeMillis()));

    Operation.Builder builder = new Operation.Builder(OperationType.AND, UTF8Type.instance, controller,
                                        new IndexExpression(comment, IndexOperator.EQ, UTF8Type.instance.decompose("eng is a work")));
    Operation op = builder.complete();

    Assert.assertTrue(op.satisfiedBy(new Row(partitioner.decorateKey(UTF8Type.instance.decompose("key1")), cf), null, false));

    builder = new Operation.Builder(OperationType.AND, UTF8Type.instance, controller,
                                        new IndexExpression(comment, IndexOperator.EQ, UTF8Type.instance.decompose("soft works fine")));
    op = builder.complete();

    Assert.assertTrue(op.satisfiedBy(new Row(partitioner.decorateKey(UTF8Type.instance.decompose("key1")), cf), null, false));
}
 
Example 3
Source File: AntiCompactionTest.java    From stratio-cassandra with Apache License 2.0 6 votes vote down vote up
private SSTableReader writeFile(ColumnFamilyStore cfs, int count)
{
    ArrayBackedSortedColumns cf = ArrayBackedSortedColumns.factory.create(cfs.metadata);
    for (int i = 0; i < count; i++)
        cf.addColumn(Util.column(String.valueOf(i), "a", 1));
    File dir = cfs.directories.getDirectoryForNewSSTables();
    String filename = cfs.getTempSSTablePath(dir);

    SSTableWriter writer = new SSTableWriter(filename,
            0,
            0,
            cfs.metadata,
            StorageService.getPartitioner(),
            new MetadataCollector(cfs.metadata.comparator));

    for (int i = 0; i < count * 5; i++)
        writer.append(StorageService.getPartitioner().decorateKey(ByteBufferUtil.bytes(i)), cf);
    return writer.closeAndOpenReader();
}
 
Example 4
Source File: SSTableReader.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
public static SSTableReader open(Descriptor desc, CFMetaData metadata) throws IOException
{
    IPartitioner p = desc.cfname.contains(SECONDARY_INDEX_NAME_SEPARATOR)
                   ? new LocalPartitioner(metadata.getKeyValidator())
                   : StorageService.getPartitioner();
    return open(desc, componentsFor(desc), metadata, p);
}
 
Example 5
Source File: CassandraEmbeddedKeyColumnValueStore.java    From titan1withtp3.1 with Apache License 2.0 5 votes vote down vote up
private static Token getMinimumToken() throws PermanentBackendException {
    IPartitioner partitioner = StorageService.getPartitioner();

    if (partitioner instanceof RandomPartitioner) {
        return ((RandomPartitioner) partitioner).getMinimumToken();
    } else if (partitioner instanceof Murmur3Partitioner) {
        return ((Murmur3Partitioner) partitioner).getMinimumToken();
    } else if (partitioner instanceof ByteOrderedPartitioner) {
        //TODO: This makes the assumption that its an EdgeStore (i.e. 8 byte keys)
        return new BytesToken(com.thinkaurelius.titan.diskstorage.util.ByteBufferUtil.zeroByteBuffer(8));
    } else {
        throw new PermanentBackendException("Unsupported partitioner: " + partitioner);
    }
}
 
Example 6
Source File: BootStrapperTest.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
private void generateFakeEndpoints(int numOldNodes) throws UnknownHostException
{
    TokenMetadata tmd = StorageService.instance.getTokenMetadata();
    tmd.clearUnsafe();
    IPartitioner p = StorageService.getPartitioner();

    for (int i = 1; i <= numOldNodes; i++)
    {
        // leave .1 for myEndpoint
        tmd.updateNormalToken(p.getRandomToken(), InetAddress.getByName("127.0.0." + (i + 1)));
    }
}
 
Example 7
Source File: SSTableRewriterTest.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
private SSTableWriter getWriter(ColumnFamilyStore cfs, File directory)
{
    String filename = cfs.getTempSSTablePath(directory);
    return new SSTableWriter(filename,
                             0,
                             0,
                             cfs.metadata,
                             StorageService.getPartitioner(),
                             new MetadataCollector(cfs.metadata.comparator));
}
 
Example 8
Source File: SSTableAttachedSecondaryIndex.java    From sasi with Apache License 2.0 5 votes vote down vote up
public void init()
{
    if (!(StorageService.getPartitioner() instanceof Murmur3Partitioner))
        throw new UnsupportedOperationException("SASI supported only with Murmur3Partitioner.");

    isInitialized = true;

    metrics = new IndexMetrics(baseCfs);

    // init() is called by SIM only on the instance that it will keep around, but will call addColumnDef on any instance
    // that it happens to create (and subsequently/immediately throw away)
    track(columnDefs);

    baseCfs.getDataTracker().subscribe(this);
}
 
Example 9
Source File: SSTableWriter.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
public SSTableWriter(String filename, long keyCount, long repairedAt)
{
    this(filename,
         keyCount,
         repairedAt,
         Schema.instance.getCFMetaData(Descriptor.fromFilename(filename)),
         StorageService.getPartitioner(),
         new MetadataCollector(Schema.instance.getCFMetaData(Descriptor.fromFilename(filename)).comparator));
}
 
Example 10
Source File: StreamingTransferTest.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
private void transferSSTables(SSTableReader sstable) throws Exception
{
    IPartitioner p = StorageService.getPartitioner();
    List<Range<Token>> ranges = new ArrayList<>();
    ranges.add(new Range<>(p.getMinimumToken(), p.getToken(ByteBufferUtil.bytes("key1"))));
    ranges.add(new Range<>(p.getToken(ByteBufferUtil.bytes("key2")), p.getMinimumToken()));
    transfer(sstable, ranges);
}
 
Example 11
Source File: CassandraEmbeddedKeyColumnValueStore.java    From titan1withtp3.1 with Apache License 2.0 5 votes vote down vote up
@Override
public KeyIterator getKeys(KeyRangeQuery keyRangeQuery, StoreTransaction txh) throws BackendException {
    IPartitioner partitioner = StorageService.getPartitioner();

    // see rant about this in Astyanax implementation
    if (partitioner instanceof RandomPartitioner || partitioner instanceof Murmur3Partitioner)
        throw new PermanentBackendException("This operation is only supported when byte-ordered partitioner is used.");

    return new RowIterator(keyRangeQuery, storeManager.getPageSize(), txh);
}
 
Example 12
Source File: SerializationsTest.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
private void testRangeSliceCommandWrite() throws IOException
{
    IPartitioner part = StorageService.getPartitioner();
    AbstractBounds<RowPosition> bounds = new Range<Token>(part.getRandomToken(), part.getRandomToken()).toRowBounds();

    RangeSliceCommand namesCmd = new RangeSliceCommand(statics.KS, "Standard1", statics.readTs, namesPred, bounds, 100);
    MessageOut<RangeSliceCommand> namesCmdMsg = namesCmd.createMessage();
    RangeSliceCommand emptyRangeCmd = new RangeSliceCommand(statics.KS, "Standard1", statics.readTs, emptyRangePred, bounds, 100);
    MessageOut<RangeSliceCommand> emptyRangeCmdMsg = emptyRangeCmd.createMessage();
    RangeSliceCommand regRangeCmd = new RangeSliceCommand(statics.KS, "Standard1", statics.readTs, nonEmptyRangePred, bounds, 100);
    MessageOut<RangeSliceCommand> regRangeCmdMsg = regRangeCmd.createMessage();
    RangeSliceCommand namesCmdSup = new RangeSliceCommand(statics.KS, "Super1", statics.readTs, namesSCPred, bounds, 100);
    MessageOut<RangeSliceCommand> namesCmdSupMsg = namesCmdSup.createMessage();
    RangeSliceCommand emptyRangeCmdSup = new RangeSliceCommand(statics.KS, "Super1", statics.readTs, emptyRangePred, bounds, 100);
    MessageOut<RangeSliceCommand> emptyRangeCmdSupMsg = emptyRangeCmdSup.createMessage();
    RangeSliceCommand regRangeCmdSup = new RangeSliceCommand(statics.KS, "Super1", statics.readTs, nonEmptyRangeSCPred, bounds, 100);
    MessageOut<RangeSliceCommand> regRangeCmdSupMsg = regRangeCmdSup.createMessage();

    DataOutputStreamAndChannel out = getOutput("db.RangeSliceCommand.bin");
    namesCmdMsg.serialize(out, getVersion());
    emptyRangeCmdMsg.serialize(out, getVersion());
    regRangeCmdMsg.serialize(out, getVersion());
    namesCmdSupMsg.serialize(out, getVersion());
    emptyRangeCmdSupMsg.serialize(out, getVersion());
    regRangeCmdSupMsg.serialize(out, getVersion());
    out.close();

    // test serializedSize
    testSerializedSize(namesCmd, RangeSliceCommand.serializer);
    testSerializedSize(emptyRangeCmd, RangeSliceCommand.serializer);
    testSerializedSize(regRangeCmd, RangeSliceCommand.serializer);
    testSerializedSize(namesCmdSup, RangeSliceCommand.serializer);
    testSerializedSize(emptyRangeCmdSup, RangeSliceCommand.serializer);
    testSerializedSize(regRangeCmdSup, RangeSliceCommand.serializer);
}
 
Example 13
Source File: StreamingTransferTest.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
@Test
public void testRandomSSTableTransfer() throws Exception
{
    final Keyspace keyspace = Keyspace.open("Keyspace1");
    final ColumnFamilyStore cfs = keyspace.getColumnFamilyStore("Standard1");
    Mutator mutator = new Mutator()
    {
        public void mutate(String key, String colName, long timestamp) throws Exception
        {
            ColumnFamily cf = ArrayBackedSortedColumns.factory.create(keyspace.getName(), cfs.name);
            cf.addColumn(column(colName, "value", timestamp));
            cf.addColumn(new BufferCell(cellname("birthdate"), ByteBufferUtil.bytes(new Date(timestamp).toString()), timestamp));
            Mutation rm = new Mutation("Keyspace1", ByteBufferUtil.bytes(key), cf);
            logger.debug("Applying row to transfer " + rm);
            rm.apply();
        }
    };
    // write a lot more data so the data is spread in more than 1 chunk.
    for (int i = 1; i <= 6000; i++)
        mutator.mutate("key" + i, "col" + i, System.currentTimeMillis());
    cfs.forceBlockingFlush();
    Util.compactAll(cfs, Integer.MAX_VALUE).get();
    SSTableReader sstable = cfs.getSSTables().iterator().next();
    cfs.clearUnsafe();

    IPartitioner p = StorageService.getPartitioner();
    List<Range<Token>> ranges = new ArrayList<>();
    ranges.add(new Range<>(p.getToken(ByteBufferUtil.bytes("key1")), p.getToken(ByteBufferUtil.bytes("key1000"))));
    ranges.add(new Range<>(p.getToken(ByteBufferUtil.bytes("key5")), p.getToken(ByteBufferUtil.bytes("key500"))));
    ranges.add(new Range<>(p.getToken(ByteBufferUtil.bytes("key9")), p.getToken(ByteBufferUtil.bytes("key900"))));
    transfer(sstable, ranges);
    assertEquals(1, cfs.getSSTables().size());
    assertEquals(7, Util.getRangeSlice(cfs).size());
}
 
Example 14
Source File: Range.java    From stratio-cassandra with Apache License 2.0 4 votes vote down vote up
public Range(T left, T right)
{
    this(left, right, StorageService.getPartitioner());
}
 
Example 15
Source File: ColumnFamilyStoreTest.java    From stratio-cassandra with Apache License 2.0 4 votes vote down vote up
@Test
public void testRemoveUnfinishedCompactionLeftovers() throws Throwable
{
    String ks = "Keyspace1";
    String cf = "Standard3"; // should be empty

    final CFMetaData cfmeta = Schema.instance.getCFMetaData(ks, cf);
    Directories dir = new Directories(cfmeta);
    ByteBuffer key = bytes("key");

    // 1st sstable
    SSTableSimpleWriter writer = new SSTableSimpleWriter(dir.getDirectoryForNewSSTables(), cfmeta, StorageService.getPartitioner());
    writer.newRow(key);
    writer.addColumn(bytes("col"), bytes("val"), 1);
    writer.close();

    Map<Descriptor, Set<Component>> sstables = dir.sstableLister().list();
    assertEquals(1, sstables.size());

    Map.Entry<Descriptor, Set<Component>> sstableToOpen = sstables.entrySet().iterator().next();
    final SSTableReader sstable1 = SSTableReader.open(sstableToOpen.getKey());

    // simulate incomplete compaction
    writer = new SSTableSimpleWriter(dir.getDirectoryForNewSSTables(),
                                     cfmeta, StorageService.getPartitioner())
    {
        protected SSTableWriter getWriter()
        {
            MetadataCollector collector = new MetadataCollector(cfmeta.comparator);
            collector.addAncestor(sstable1.descriptor.generation); // add ancestor from previously written sstable
            return new SSTableWriter(makeFilename(directory, metadata.ksName, metadata.cfName),
                                     0,
                                     ActiveRepairService.UNREPAIRED_SSTABLE,
                                     metadata,
                                     StorageService.getPartitioner(),
                                     collector);
        }
    };
    writer.newRow(key);
    writer.addColumn(bytes("col"), bytes("val"), 1);
    writer.close();

    // should have 2 sstables now
    sstables = dir.sstableLister().list();
    assertEquals(2, sstables.size());

    SSTableReader sstable2 = SSTableReader.open(sstable1.descriptor);
    UUID compactionTaskID = SystemKeyspace.startCompaction(
            Keyspace.open(ks).getColumnFamilyStore(cf),
            Collections.singleton(sstable2));

    Map<Integer, UUID> unfinishedCompaction = new HashMap<>();
    unfinishedCompaction.put(sstable1.descriptor.generation, compactionTaskID);
    ColumnFamilyStore.removeUnfinishedCompactionLeftovers(cfmeta, unfinishedCompaction);

    // 2nd sstable should be removed (only 1st sstable exists in set of size 1)
    sstables = dir.sstableLister().list();
    assertEquals(1, sstables.size());
    assertTrue(sstables.containsKey(sstable1.descriptor));

    Map<Pair<String, String>, Map<Integer, UUID>> unfinished = SystemKeyspace.getUnfinishedCompactions();
    assertTrue(unfinished.isEmpty());
    sstable1.selfRef().release();
    sstable2.selfRef().release();
}
 
Example 16
Source File: Token.java    From stratio-cassandra with Apache License 2.0 4 votes vote down vote up
public void serialize(Token token, DataOutputPlus out) throws IOException
{
    IPartitioner p = StorageService.getPartitioner();
    ByteBuffer b = p.getTokenFactory().toByteArray(token);
    ByteBufferUtil.writeWithLength(b, out);
}
 
Example 17
Source File: ExcludingBounds.java    From stratio-cassandra with Apache License 2.0 4 votes vote down vote up
public ExcludingBounds(T left, T right)
{
    this(left, right, StorageService.getPartitioner());
}
 
Example 18
Source File: Bounds.java    From stratio-cassandra with Apache License 2.0 4 votes vote down vote up
public Bounds(T left, T right)
{
    this(left, right, StorageService.getPartitioner());
}
 
Example 19
Source File: SelectStatement.java    From stratio-cassandra with Apache License 2.0 4 votes vote down vote up
private AbstractBounds<RowPosition> getKeyBounds(QueryOptions options) throws InvalidRequestException
{
    IPartitioner p = StorageService.getPartitioner();

    if (onToken)
    {
        Token startToken = getTokenBound(Bound.START, options, p);
        Token endToken = getTokenBound(Bound.END, options, p);

        boolean includeStart = includeKeyBound(Bound.START);
        boolean includeEnd = includeKeyBound(Bound.END);

        /*
         * If we ask SP.getRangeSlice() for (token(200), token(200)], it will happily return the whole ring.
         * However, wrapping range doesn't really make sense for CQL, and we want to return an empty result
         * in that case (CASSANDRA-5573). So special case to create a range that is guaranteed to be empty.
         *
         * In practice, we want to return an empty result set if either startToken > endToken, or both are
         * equal but one of the bound is excluded (since [a, a] can contains something, but not (a, a], [a, a)
         * or (a, a)). Note though that in the case where startToken or endToken is the minimum token, then
         * this special case rule should not apply.
         */
        int cmp = startToken.compareTo(endToken);
        if (!startToken.isMinimum() && !endToken.isMinimum() && (cmp > 0 || (cmp == 0 && (!includeStart || !includeEnd))))
            return null;

        RowPosition start = includeStart ? startToken.minKeyBound() : startToken.maxKeyBound();
        RowPosition end = includeEnd ? endToken.maxKeyBound() : endToken.minKeyBound();

        return new Range<RowPosition>(start, end);
    }
    else
    {
        ByteBuffer startKeyBytes = getKeyBound(Bound.START, options);
        ByteBuffer finishKeyBytes = getKeyBound(Bound.END, options);

        RowPosition startKey = RowPosition.ForKey.get(startKeyBytes, p);
        RowPosition finishKey = RowPosition.ForKey.get(finishKeyBytes, p);

        if (startKey.compareTo(finishKey) > 0 && !finishKey.isMinimum(p))
            return null;

        if (includeKeyBound(Bound.START))
        {
            return includeKeyBound(Bound.END)
                 ? new Bounds<RowPosition>(startKey, finishKey)
                 : new IncludingExcludingBounds<RowPosition>(startKey, finishKey);
        }
        else
        {
            return includeKeyBound(Bound.END)
                 ? new Range<RowPosition>(startKey, finishKey)
                 : new ExcludingBounds<RowPosition>(startKey, finishKey);
        }
    }
}
 
Example 20
Source File: CassandraEmbeddedKeyColumnValueStore.java    From titan1withtp3.1 with Apache License 2.0 4 votes vote down vote up
/**
 * Create a RangeSliceCommand and run it against the StorageProxy.
 * <p>
 * To match the behavior of the standard Cassandra thrift API endpoint, the
 * {@code nowMillis} argument should be the number of milliseconds since the
 * UNIX Epoch (e.g. System.currentTimeMillis() or equivalent obtained
 * through a {@link TimestampProvider}). This is per
 * {@link org.apache.cassandra.thrift.CassandraServer#get_range_slices(ColumnParent, SlicePredicate, KeyRange, ConsistencyLevel)},
 * which passes the server's System.currentTimeMillis() to the
 * {@code RangeSliceCommand} constructor.
 */
private List<Row> getKeySlice(Token start,
                              Token end,
                              @Nullable SliceQuery sliceQuery,
                              int pageSize,
                              long nowMillis) throws BackendException {
    IPartitioner partitioner = StorageService.getPartitioner();

    SliceRange columnSlice = new SliceRange();
    if (sliceQuery == null) {
        columnSlice.setStart(ArrayUtils.EMPTY_BYTE_ARRAY)
                .setFinish(ArrayUtils.EMPTY_BYTE_ARRAY)
                .setCount(5);
    } else {
        columnSlice.setStart(sliceQuery.getSliceStart().asByteBuffer())
                .setFinish(sliceQuery.getSliceEnd().asByteBuffer())
                .setCount(sliceQuery.hasLimit() ? sliceQuery.getLimit() : Integer.MAX_VALUE);
    }
    /* Note: we need to fetch columns for each row as well to remove "range ghosts" */
    SlicePredicate predicate = new SlicePredicate().setSlice_range(columnSlice);

    // DAVID CASSANDRA
    // Old cassandra code did not use partitioner anyway in this call...so new code removed it as a parmaeter
    // RowPosition startPosition = start.minKeyBound(partitioner);
    RowPosition startPosition = start.minKeyBound();
    // DAVID CASSANDRA
    // RowPosition endPosition = end.minKeyBound(partitioner);
    RowPosition endPosition = end.minKeyBound();

    List<Row> rows;

    try {
        CFMetaData cfm = Schema.instance.getCFMetaData(keyspace, columnFamily);
        IDiskAtomFilter filter = ThriftValidation.asIFilter(predicate, cfm, null);

        RangeSliceCommand cmd = new RangeSliceCommand(keyspace, columnFamily, nowMillis, filter, new Bounds<RowPosition>(startPosition, endPosition), pageSize);

        rows = StorageProxy.getRangeSlice(cmd, ConsistencyLevel.QUORUM);
    } catch (Exception e) {
        throw new PermanentBackendException(e);
    }

    return rows;
}