org.apache.cassandra.service.StorageProxy Java Examples

The following examples show how to use org.apache.cassandra.service.StorageProxy. 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: CassandraEmbeddedStoreManager.java    From titan1withtp3.1 with Apache License 2.0 6 votes vote down vote up
private void mutate(List<org.apache.cassandra.db.Mutation> cmds, org.apache.cassandra.db.ConsistencyLevel clvl) throws BackendException {
    try {
        schedule(DatabaseDescriptor.getRpcTimeout());
        try {
            if (atomicBatch) {
                StorageProxy.mutateAtomically(cmds, clvl);
            } else {
                StorageProxy.mutate(cmds, clvl);
            }
        } catch (RequestExecutionException e) {
            throw new TemporaryBackendException(e);
        } finally {
            release();
        }
    } catch (TimeoutException ex) {
        log.debug("Cassandra TimeoutException", ex);
        throw new TemporaryBackendException(ex);
    }
}
 
Example #2
Source File: CassandraServer.java    From stratio-cassandra with Apache License 2.0 6 votes vote down vote up
private void doInsert(ConsistencyLevel consistency_level, List<? extends IMutation> mutations, boolean mutateAtomically)
throws UnavailableException, TimedOutException, org.apache.cassandra.exceptions.InvalidRequestException
{
    org.apache.cassandra.db.ConsistencyLevel consistencyLevel = ThriftConversion.fromThrift(consistency_level);
    consistencyLevel.validateForWrite(state().getKeyspace());
    if (mutations.isEmpty())
        return;

    long timeout = Long.MAX_VALUE;
    for (IMutation m : mutations)
        timeout = Longs.min(timeout, m.getTimeout());

    schedule(timeout);
    try
    {
        StorageProxy.mutateWithTriggers(mutations, consistencyLevel, mutateAtomically);
    }
    catch (RequestExecutionException e)
    {
        ThriftConversion.rethrow(e);
    }
    finally
    {
        release();
    }
}
 
Example #3
Source File: BatchlogManager.java    From stratio-cassandra with Apache License 2.0 6 votes vote down vote up
private void writeHintsForUndeliveredEndpoints(int startFrom)
{
    try
    {
        // Here we deserialize mutations 2nd time from byte buffer.
        // but this is ok, because timeout on batch direct delivery is rare
        // (it can happen only several seconds until node is marked dead)
        // so trading some cpu to keep less objects
        List<Mutation> replayingMutations = replayingMutations();
        for (int i = startFrom; i < replayHandlers.size(); i++)
        {
            Mutation undeliveredMutation = replayingMutations.get(i);
            int ttl = calculateHintTTL(replayingMutations);
            ReplayWriteResponseHandler handler = replayHandlers.get(i);

            if (ttl > 0 && handler != null)
                for (InetAddress endpoint : handler.undelivered)
                    StorageProxy.writeHintForMutation(undeliveredMutation, writtenAt, ttl, endpoint);
        }
    }
    catch (IOException e)
    {
        logger.error("Cannot schedule hints for undelivered batch", e);
    }
}
 
Example #4
Source File: SliceQueryPager.java    From stratio-cassandra with Apache License 2.0 6 votes vote down vote up
protected List<Row> queryNextPage(int pageSize, ConsistencyLevel consistencyLevel, boolean localQuery)
throws RequestValidationException, RequestExecutionException
{
    // For some queries, such as a DISTINCT query on static columns, the limit for slice queries will be lower
    // than the page size (in the static example, it will be 1).  We use the min here to ensure we don't fetch
    // more rows than we're supposed to.  See CASSANDRA-8108 for more details.
    SliceQueryFilter filter = command.filter.withUpdatedCount(Math.min(command.filter.count, pageSize));
    if (lastReturned != null)
        filter = filter.withUpdatedStart(lastReturned, cfm.comparator);

    logger.debug("Querying next page of slice query; new filter: {}", filter);
    ReadCommand pageCmd = command.withUpdatedFilter(filter);
    return localQuery
         ? Collections.singletonList(pageCmd.getRow(Keyspace.open(command.ksName)))
         : StorageProxy.read(Collections.singletonList(pageCmd), consistencyLevel, cstate);
}
 
Example #5
Source File: RangeSliceQueryPager.java    From stratio-cassandra with Apache License 2.0 6 votes vote down vote up
protected List<Row> queryNextPage(int pageSize, ConsistencyLevel consistencyLevel, boolean localQuery)
throws RequestExecutionException
{
    SliceQueryFilter sf = (SliceQueryFilter)columnFilter;
    AbstractBounds<RowPosition> keyRange = lastReturnedKey == null ? command.keyRange : makeIncludingKeyBounds(lastReturnedKey);
    Composite start = lastReturnedName == null ? sf.start() : lastReturnedName;
    PagedRangeCommand pageCmd = new PagedRangeCommand(command.keyspace,
                                                      command.columnFamily,
                                                      command.timestamp,
                                                      keyRange,
                                                      sf,
                                                      start,
                                                      sf.finish(),
                                                      command.rowFilter,
                                                      pageSize,
                                                      command.countCQL3Rows);

    return localQuery
         ? pageCmd.executeLocally()
         : StorageProxy.getRangeSlice(pageCmd, consistencyLevel);
}
 
Example #6
Source File: SelectStatement.java    From stratio-cassandra with Apache License 2.0 6 votes vote down vote up
private ResultMessage.Rows execute(Pageable command, QueryOptions options, int limit, long now, QueryState state) throws RequestValidationException, RequestExecutionException
{
    List<Row> rows;
    if (command == null)
    {
        rows = Collections.<Row>emptyList();
    }
    else
    {
        rows = command instanceof Pageable.ReadCommands
             ? StorageProxy.read(((Pageable.ReadCommands)command).commands, options.getConsistency(), state.getClientState())
             : StorageProxy.getRangeSlice((RangeSliceCommand)command, options.getConsistency());
    }

    return processResults(rows, options, limit, now);
}
 
Example #7
Source File: ModificationStatement.java    From stratio-cassandra with Apache License 2.0 6 votes vote down vote up
public ResultMessage executeWithCondition(QueryState queryState, QueryOptions options)
throws RequestExecutionException, RequestValidationException
{
    List<ByteBuffer> keys = buildPartitionKeyNames(options);
    // We don't support IN for CAS operation so far
    if (keys.size() > 1)
        throw new InvalidRequestException("IN on the partition key is not supported with conditional updates");

    ByteBuffer key = keys.get(0);
    long now = options.getTimestamp(queryState);
    Composite prefix = createClusteringPrefix(options);

    CQL3CasRequest request = new CQL3CasRequest(cfm, key, false);
    addConditions(prefix, request, options);
    request.addRowUpdate(prefix, this, options, now);

    ColumnFamily result = StorageProxy.cas(keyspace(),
                                           columnFamily(),
                                           key,
                                           request,
                                           options.getSerialConsistency(),
                                           options.getConsistency(),
                                           queryState.getClientState());
    return new ResultMessage.Rows(buildCasResultSet(key, result, options));
}
 
Example #8
Source File: BatchStatement.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
private void executeWithoutConditions(Collection<? extends IMutation> mutations, ConsistencyLevel cl) throws RequestExecutionException, RequestValidationException
{
    // Extract each collection of cfs from it's IMutation and then lazily concatenate all of them into a single Iterable.
    Iterable<ColumnFamily> cfs = Iterables.concat(Iterables.transform(mutations, new Function<IMutation, Collection<ColumnFamily>>()
    {
        public Collection<ColumnFamily> apply(IMutation im)
        {
            return im.getColumnFamilies();
        }
    }));
    verifyBatchSize(cfs);

    boolean mutateAtomic = (type == Type.LOGGED && mutations.size() > 1);
    StorageProxy.mutateWithTriggers(mutations, cl, mutateAtomic);
}
 
Example #9
Source File: ModificationStatement.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
private ResultMessage executeWithoutCondition(QueryState queryState, QueryOptions options)
throws RequestExecutionException, RequestValidationException
{
    ConsistencyLevel cl = options.getConsistency();
    if (isCounter())
        cl.validateCounterForWrite(cfm);
    else
        cl.validateForWrite(cfm.ksName);

    Collection<? extends IMutation> mutations = getMutations(options, false, options.getTimestamp(queryState));
    if (!mutations.isEmpty())
        StorageProxy.mutateWithTriggers(mutations, cl, false);

    return null;
}
 
Example #10
Source File: RangeNamesQueryPager.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
protected List<Row> queryNextPage(int pageSize, ConsistencyLevel consistencyLevel, boolean localQuery)
throws RequestExecutionException
{
    AbstractRangeCommand pageCmd = command.withUpdatedLimit(pageSize);
    if (lastReturnedKey != null)
        pageCmd = pageCmd.forSubRange(makeExcludingKeyBounds(lastReturnedKey));

    return localQuery
         ? pageCmd.executeLocally()
         : StorageProxy.getRangeSlice(pageCmd, consistencyLevel);
}
 
Example #11
Source File: CassandraEmbeddedStoreManager.java    From titan1withtp3.1 with Apache License 2.0 5 votes vote down vote up
private void retryDummyRead(String ks, String cf) throws PermanentBackendException {

        final long limit = System.currentTimeMillis() + (60L * 1000L);

        while (System.currentTimeMillis() < limit) {
            try {
                SortedSet<CellName> names = new TreeSet<>(new Comparator<CellName>() {
                    // This is a singleton set.  We need to define a comparator because SimpleDenseCellName is not
                    // comparable, but it doesn't have to be a useful comparator
                    @Override
                    public int compare(CellName o1, CellName o2)
                    {
                        return 0;
                    }
                });
                names.add(CellNames.simpleDense(ByteBufferUtil.zeroByteBuffer(1)));
                NamesQueryFilter nqf = new NamesQueryFilter(names);
                SliceByNamesReadCommand cmd = new SliceByNamesReadCommand(ks, ByteBufferUtil.zeroByteBuffer(1), cf, 1L, nqf);
                StorageProxy.read(ImmutableList.<ReadCommand> of(cmd), ConsistencyLevel.QUORUM);
                log.info("Read on CF {} in KS {} succeeded", cf, ks);
                return;
            } catch (Throwable t) {
                log.warn("Failed to read CF {} in KS {} following creation", cf, ks, t);
            }

            try {
                Thread.sleep(1000L);
            } catch (InterruptedException e) {
                throw new PermanentBackendException(e);
            }
        }

        throw new PermanentBackendException("Timed out while attempting to read CF " + cf + " in KS " + ks + " following creation");
    }
 
Example #12
Source File: CounterMutationVerbHandler.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
public void doVerb(final MessageIn<CounterMutation> message, final int id)
{
    try
    {
        final CounterMutation cm = message.payload;
        logger.debug("Applying forwarded {}", cm);

        String localDataCenter = DatabaseDescriptor.getEndpointSnitch().getDatacenter(FBUtilities.getBroadcastAddress());
        // We should not wait for the result of the write in this thread,
        // otherwise we could have a distributed deadlock between replicas
        // running this VerbHandler (see #4578).
        // Instead, we use a callback to send the response. Note that the callback
        // will not be called if the request timeout, but this is ok
        // because the coordinator of the counter mutation will timeout on
        // it's own in that case.
        StorageProxy.applyCounterMutationOnLeader(cm, localDataCenter, new Runnable()
        {
            public void run()
            {
                MessagingService.instance().sendReply(new WriteResponse().createMessage(), id, message.from);
            }
        });
    }
    catch (RequestExecutionException e)
    {
        // The coordinator will timeout on it's own so ignore
        logger.debug("counter error", e);
    }
}
 
Example #13
Source File: CassandraServer.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
protected Map<DecoratedKey, ColumnFamily> readColumnFamily(List<ReadCommand> commands, org.apache.cassandra.db.ConsistencyLevel consistency_level, ClientState cState)
throws org.apache.cassandra.exceptions.InvalidRequestException, UnavailableException, TimedOutException
{
    // TODO - Support multiple column families per row, right now row only contains 1 column family
    Map<DecoratedKey, ColumnFamily> columnFamilyKeyMap = new HashMap<DecoratedKey, ColumnFamily>();

    List<Row> rows = null;
    try
    {
        schedule(DatabaseDescriptor.getReadRpcTimeout());
        try
        {
            rows = StorageProxy.read(commands, consistency_level, cState);
        }
        finally
        {
            release();
        }
    }
    catch (RequestExecutionException e)
    {
        ThriftConversion.rethrow(e);
    }

    for (Row row: rows)
    {
        columnFamilyKeyMap.put(row.key, row.cf);
    }
    return columnFamilyKeyMap;
}
 
Example #14
Source File: WriteCallbackInfo.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
public boolean shouldHint()
{
    return allowHints
        && sentMessage.verb != MessagingService.Verb.COUNTER_MUTATION
        && consistencyLevel != ConsistencyLevel.ANY
        && StorageProxy.shouldHint(target);
}
 
Example #15
Source File: CassandraRunner.java    From staash with Apache License 2.0 5 votes vote down vote up
private void maybeTruncateSafely(RequiresColumnFamily rcf) {
  if ( rcf != null && rcf.truncateExisting() ) {
    try {
      StorageProxy.truncateBlocking(rcf.ksName(), rcf.cfName());
    } catch (Exception ex) {
      throw new RuntimeException("Could not truncate column family: " + rcf.cfName(),ex);
    }
  }
}
 
Example #16
Source File: BatchStatement.java    From stratio-cassandra with Apache License 2.0 4 votes vote down vote up
private ResultMessage executeWithConditions(BatchQueryOptions options, QueryState state)
throws RequestExecutionException, RequestValidationException
{
    long now = state.getTimestamp();
    ByteBuffer key = null;
    String ksName = null;
    String cfName = null;
    CQL3CasRequest casRequest = null;
    Set<ColumnDefinition> columnsWithConditions = new LinkedHashSet<>();

    for (int i = 0; i < statements.size(); i++)
    {
        ModificationStatement statement = statements.get(i);
        QueryOptions statementOptions = options.forStatement(i);
        long timestamp = attrs.getTimestamp(now, statementOptions);
        List<ByteBuffer> pks = statement.buildPartitionKeyNames(statementOptions);
        if (pks.size() > 1)
            throw new IllegalArgumentException("Batch with conditions cannot span multiple partitions (you cannot use IN on the partition key)");
        if (key == null)
        {
            key = pks.get(0);
            ksName = statement.cfm.ksName;
            cfName = statement.cfm.cfName;
            casRequest = new CQL3CasRequest(statement.cfm, key, true);
        }
        else if (!key.equals(pks.get(0)))
        {
            throw new InvalidRequestException("Batch with conditions cannot span multiple partitions");
        }

        Composite clusteringPrefix = statement.createClusteringPrefix(statementOptions);
        if (statement.hasConditions())
        {
            statement.addConditions(clusteringPrefix, casRequest, statementOptions);
            // As soon as we have a ifNotExists, we set columnsWithConditions to null so that everything is in the resultSet
            if (statement.hasIfNotExistCondition() || statement.hasIfExistCondition())
                columnsWithConditions = null;
            else if (columnsWithConditions != null)
                Iterables.addAll(columnsWithConditions, statement.getColumnsWithConditions());
        }
        casRequest.addRowUpdate(clusteringPrefix, statement, statementOptions, timestamp);
    }

    ColumnFamily result = StorageProxy.cas(ksName, cfName, key, casRequest, options.getSerialConsistency(), options.getConsistency(), state.getClientState());

    return new ResultMessage.Rows(ModificationStatement.buildCasResultSet(ksName, key, cfName, result, columnsWithConditions, true, options.forStatement(0)));
}
 
Example #17
Source File: QueryProcessor.java    From stratio-cassandra with Apache License 2.0 4 votes vote down vote up
private static List<org.apache.cassandra.db.Row> multiRangeSlice(CFMetaData metadata, SelectStatement select, List<ByteBuffer> variables, long now)
throws ReadTimeoutException, UnavailableException, InvalidRequestException
{
    IPartitioner p = StorageService.getPartitioner();

    AbstractType<?> keyType = Schema.instance.getCFMetaData(metadata.ksName, select.getColumnFamily()).getKeyValidator();

    ByteBuffer startKeyBytes = (select.getKeyStart() != null)
                               ? select.getKeyStart().getByteBuffer(keyType,variables)
                               : null;

    ByteBuffer finishKeyBytes = (select.getKeyFinish() != null)
                                ? select.getKeyFinish().getByteBuffer(keyType,variables)
                                : null;

    RowPosition startKey = RowPosition.ForKey.get(startKeyBytes, p), finishKey = RowPosition.ForKey.get(finishKeyBytes, p);
    if (startKey.compareTo(finishKey) > 0 && !finishKey.isMinimum(p))
    {
        if (p instanceof RandomPartitioner)
            throw new InvalidRequestException("Start key sorts after end key. This is not allowed; you probably should not specify end key at all, under RandomPartitioner");
        else
            throw new InvalidRequestException("Start key must sort before (or equal to) finish key in your partitioner!");
    }
    AbstractBounds<RowPosition> bounds = new Bounds<RowPosition>(startKey, finishKey);

    IDiskAtomFilter columnFilter = filterFromSelect(select, metadata, variables);
    validateFilter(metadata, columnFilter);

    List<Relation> columnRelations = select.getColumnRelations();
    List<IndexExpression> expressions = new ArrayList<IndexExpression>(columnRelations.size());
    for (Relation columnRelation : columnRelations)
    {
        // Left and right side of relational expression encoded according to comparator/validator.
        ByteBuffer entity = columnRelation.getEntity().getByteBuffer(metadata.comparator.asAbstractType(), variables);
        ByteBuffer value = columnRelation.getValue().getByteBuffer(metadata.getValueValidator(metadata.comparator.cellFromByteBuffer(entity)), variables);

        expressions.add(new IndexExpression(entity,
                                            Operator.valueOf(columnRelation.operator().name()),
                                            value));
    }

    int limit = select.isKeyRange() && select.getKeyStart() != null
              ? select.getNumRecords() + 1
              : select.getNumRecords();

    List<org.apache.cassandra.db.Row> rows = StorageProxy.getRangeSlice(new RangeSliceCommand(metadata.ksName,
                                                                                              select.getColumnFamily(),
                                                                                              now,
                                                                                              columnFilter,
                                                                                              bounds,
                                                                                              expressions,
                                                                                              limit),
                                                                        select.getConsistencyLevel());

    // if start key was set and relation was "greater than"
    if (select.getKeyStart() != null && !select.includeStartKey() && !rows.isEmpty())
    {
        if (rows.get(0).key.getKey().equals(startKeyBytes))
            rows.remove(0);
    }

    // if finish key was set and relation was "less than"
    if (select.getKeyFinish() != null && !select.includeFinishKey() && !rows.isEmpty())
    {
        int lastIndex = rows.size() - 1;
        if (rows.get(lastIndex).key.getKey().equals(finishKeyBytes))
            rows.remove(lastIndex);
    }

    return rows.subList(0, select.getNumRecords() < rows.size() ? select.getNumRecords() : rows.size());
}
 
Example #18
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;
}
 
Example #19
Source File: CassandraServer.java    From stratio-cassandra with Apache License 2.0 4 votes vote down vote up
public Map<String, List<String>> describe_schema_versions() throws TException, InvalidRequestException
{
    logger.debug("checking schema agreement");
    return StorageProxy.describeSchemaVersions();
}