Search in sources :

Example 31 with InvalidRequestException

use of org.apache.cassandra.exceptions.InvalidRequestException in project cassandra by apache.

the class Attributes method getTimeToLive.

public int getTimeToLive(QueryOptions options, int defaultTimeToLive) throws InvalidRequestException {
    if (timeToLive == null)
        return defaultTimeToLive;
    ByteBuffer tval = timeToLive.bindAndGet(options);
    if (tval == null)
        return 0;
    if (tval == ByteBufferUtil.UNSET_BYTE_BUFFER)
        return defaultTimeToLive;
    try {
        Int32Type.instance.validate(tval);
    } catch (MarshalException e) {
        throw new InvalidRequestException("Invalid timestamp value: " + tval);
    }
    int ttl = Int32Type.instance.compose(tval);
    if (ttl < 0)
        throw new InvalidRequestException("A TTL must be greater or equal to 0, but was " + ttl);
    if (ttl > MAX_TTL)
        throw new InvalidRequestException(String.format("ttl is too large. requested (%d) maximum (%d)", ttl, MAX_TTL));
    if (defaultTimeToLive != LivenessInfo.NO_TTL && ttl == LivenessInfo.NO_TTL)
        return LivenessInfo.NO_TTL;
    return ttl;
}
Also used : MarshalException(org.apache.cassandra.serializers.MarshalException) InvalidRequestException(org.apache.cassandra.exceptions.InvalidRequestException) ByteBuffer(java.nio.ByteBuffer)

Example 32 with InvalidRequestException

use of org.apache.cassandra.exceptions.InvalidRequestException in project cassandra by apache.

the class CQLSSTableWriter method rawAddRow.

/**
     * Adds a new row to the writer given already serialized values.
     * <p>
     * This is a shortcut for {@code rawAddRow(Arrays.asList(values))}.
     *
     * @param values the row values (corresponding to the bind variables of the
     * insertion statement used when creating by this writer) as binary.
     * @return this writer.
     */
public CQLSSTableWriter rawAddRow(List<ByteBuffer> values) throws InvalidRequestException, IOException {
    if (values.size() != boundNames.size())
        throw new InvalidRequestException(String.format("Invalid number of arguments, expecting %d values but got %d", boundNames.size(), values.size()));
    QueryOptions options = QueryOptions.forInternalCalls(null, values);
    List<ByteBuffer> keys = insert.buildPartitionKeyNames(options);
    SortedSet<Clustering> clusterings = insert.createClustering(options);
    long now = System.currentTimeMillis() * 1000;
    // Note that we asks indexes to not validate values (the last 'false' arg below) because that triggers a 'Keyspace.open'
    // and that forces a lot of initialization that we don't want.
    UpdateParameters params = new UpdateParameters(insert.metadata, insert.updatedColumns(), options, insert.getTimestamp(now, options), insert.getTimeToLive(options), Collections.<DecoratedKey, Partition>emptyMap());
    try {
        for (ByteBuffer key : keys) {
            for (Clustering clustering : clusterings) insert.addUpdateForKey(writer.getUpdateFor(key), clustering, params);
        }
        return this;
    } catch (SSTableSimpleUnsortedWriter.SyncException e) {
        // wrapped in a SyncException (see BufferedWriter below). We want to extract that IOE.
        throw (IOException) e.getCause();
    }
}
Also used : InvalidRequestException(org.apache.cassandra.exceptions.InvalidRequestException) UpdateParameters(org.apache.cassandra.cql3.UpdateParameters) Clustering(org.apache.cassandra.db.Clustering) QueryOptions(org.apache.cassandra.cql3.QueryOptions) ByteBuffer(java.nio.ByteBuffer)

Example 33 with InvalidRequestException

use of org.apache.cassandra.exceptions.InvalidRequestException in project cassandra by apache.

the class TriggerExecutor method execute.

/**
     * Takes a collection of mutations and possibly augments it by adding extra mutations
     * generated by configured triggers. If no additional mutations are created
     * this returns null, signalling to the caller that only the initial set of
     * mutations should be applied. If additional mutations <i>are</i> generated,
     * the total set (i.e. the original plus the additional mutations) are applied
     * together in a logged batch. Should this not be possible because the initial
     * mutations contain counter updates, InvalidRequestException is thrown.
     *
     * @param mutations initial collection of mutations
     * @return augmented mutations. Either the union of the initial and additional
     * mutations or null if no additional mutations were generated
     * @throws InvalidRequestException if additional mutations were generated, but
     * the initial mutations contains counter updates
     */
public Collection<Mutation> execute(Collection<? extends IMutation> mutations) throws InvalidRequestException {
    boolean hasCounters = false;
    List<Mutation> augmentedMutations = null;
    for (IMutation mutation : mutations) {
        if (mutation instanceof CounterMutation)
            hasCounters = true;
        for (PartitionUpdate upd : mutation.getPartitionUpdates()) {
            List<Mutation> augmentations = executeInternal(upd);
            if (augmentations == null || augmentations.isEmpty())
                continue;
            validate(augmentations);
            if (augmentedMutations == null)
                augmentedMutations = new LinkedList<>();
            augmentedMutations.addAll(augmentations);
        }
    }
    if (augmentedMutations == null)
        return null;
    if (hasCounters)
        throw new InvalidRequestException("Counter mutations and trigger mutations cannot be applied together atomically.");
    @SuppressWarnings("unchecked") Collection<Mutation> originalMutations = (Collection<Mutation>) mutations;
    return mergeMutations(Iterables.concat(originalMutations, augmentedMutations));
}
Also used : InvalidRequestException(org.apache.cassandra.exceptions.InvalidRequestException) PartitionUpdate(org.apache.cassandra.db.partitions.PartitionUpdate)

Example 34 with InvalidRequestException

use of org.apache.cassandra.exceptions.InvalidRequestException in project cassandra by apache.

the class AggregationTest method testBrokenAggregate.

@Test
public void testBrokenAggregate() throws Throwable {
    createTable("CREATE TABLE %s (key int primary key, val int)");
    execute("INSERT INTO %s (key, val) VALUES (?, ?)", 1, 1);
    String fState = createFunction(KEYSPACE, "int, int", "CREATE FUNCTION %s(a int, b int) " + "CALLED ON NULL INPUT " + "RETURNS int " + "LANGUAGE javascript " + "AS 'a + b;'");
    String a = createAggregate(KEYSPACE, "int", "CREATE AGGREGATE %s(int) " + "SFUNC " + shortFunctionName(fState) + " " + "STYPE int ");
    KeyspaceMetadata ksm = Schema.instance.getKeyspaceMetadata(keyspace());
    UDAggregate f = (UDAggregate) ksm.functions.get(parseFunctionName(a)).iterator().next();
    UDAggregate broken = UDAggregate.createBroken(f.name(), f.argTypes(), f.returnType(), null, new InvalidRequestException("foo bar is broken"));
    Schema.instance.load(ksm.withSwapped(ksm.functions.without(f.name(), f.argTypes()).with(broken)));
    assertInvalidThrowMessage("foo bar is broken", InvalidRequestException.class, "SELECT " + a + "(val) FROM %s");
}
Also used : UDAggregate(org.apache.cassandra.cql3.functions.UDAggregate) InvalidRequestException(org.apache.cassandra.exceptions.InvalidRequestException) KeyspaceMetadata(org.apache.cassandra.schema.KeyspaceMetadata) Test(org.junit.Test)

Aggregations

InvalidRequestException (org.apache.cassandra.exceptions.InvalidRequestException)34 ByteBuffer (java.nio.ByteBuffer)8 TableMetadata (org.apache.cassandra.schema.TableMetadata)8 ColumnMetadata (org.apache.cassandra.schema.ColumnMetadata)5 KeyspaceMetadata (org.apache.cassandra.schema.KeyspaceMetadata)4 ArrayList (java.util.ArrayList)3 QueryOptions (org.apache.cassandra.cql3.QueryOptions)3 AbstractType (org.apache.cassandra.db.marshal.AbstractType)3 PartitionUpdate (org.apache.cassandra.db.partitions.PartitionUpdate)3 ViewMetadata (org.apache.cassandra.schema.ViewMetadata)3 java.util (java.util)2 org.apache.cassandra.config (org.apache.cassandra.config)2 UpdateParameters (org.apache.cassandra.cql3.UpdateParameters)2 org.apache.cassandra.db (org.apache.cassandra.db)2 ConfigurationException (org.apache.cassandra.exceptions.ConfigurationException)2 Index (org.apache.cassandra.index.Index)2 Triggers (org.apache.cassandra.schema.Triggers)2 FBUtilities (org.apache.cassandra.utils.FBUtilities)2 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableMap (com.google.common.collect.ImmutableMap)1