Search in sources :

Example 1 with MarshalException

use of org.apache.cassandra.db.marshal.MarshalException in project eiger by wlloyd.

the class ThriftValidation method validateCfDef.

public static void validateCfDef(CfDef cf_def, CFMetaData old) throws InvalidRequestException {
    try {
        if (cf_def.name.length() > 32)
            throw new InvalidRequestException(String.format("Column family names shouldn't be more than 32 character long (got \"%s\")", cf_def.name));
        if (cf_def.key_alias != null) {
            if (!cf_def.key_alias.hasRemaining())
                throw new InvalidRequestException("key_alias may not be empty");
            try {
                // it's hard to use a key in a select statement if we can't type it.
                // for now let's keep it simple and require ascii.
                AsciiType.instance.validate(cf_def.key_alias);
            } catch (MarshalException e) {
                throw new InvalidRequestException("Key aliases must be ascii");
            }
        }
        ColumnFamilyType cfType = ColumnFamilyType.create(cf_def.column_type);
        if (cfType == null)
            throw new InvalidRequestException("invalid column type " + cf_def.column_type);
        TypeParser.parse(cf_def.key_validation_class);
        TypeParser.parse(cf_def.comparator_type);
        TypeParser.parse(cf_def.subcomparator_type);
        TypeParser.parse(cf_def.default_validation_class);
        if (cfType != ColumnFamilyType.Super && cf_def.subcomparator_type != null)
            throw new InvalidRequestException("subcomparator_type is invalid for standard columns");
        if (cf_def.column_metadata == null)
            return;
        AbstractType comparator = cfType == ColumnFamilyType.Standard ? TypeParser.parse(cf_def.comparator_type) : TypeParser.parse(cf_def.subcomparator_type);
        if (cf_def.key_alias != null) {
            // check if any of the columns has name equal to the cf.key_alias
            for (ColumnDef columnDef : cf_def.column_metadata) {
                if (cf_def.key_alias.equals(columnDef.name))
                    throw new InvalidRequestException("Invalid column name: " + AsciiType.instance.compose(cf_def.key_alias) + ", because it equals the key_alias");
            }
        }
        // initialize a set of names NOT in the CF under consideration
        Set<String> indexNames = new HashSet<String>();
        for (ColumnFamilyStore cfs : ColumnFamilyStore.all()) {
            if (!cfs.getColumnFamilyName().equals(cf_def.name))
                for (ColumnDefinition cd : cfs.metadata.getColumn_metadata().values()) indexNames.add(cd.getIndexName());
        }
        for (ColumnDef c : cf_def.column_metadata) {
            TypeParser.parse(c.validation_class);
            try {
                comparator.validate(c.name);
            } catch (MarshalException e) {
                throw new InvalidRequestException(String.format("Column name %s is not valid for comparator %s", ByteBufferUtil.bytesToHex(c.name), cf_def.comparator_type));
            }
            if (c.index_type == null) {
                if (c.index_name != null)
                    throw new ConfigurationException("index_name cannot be set without index_type");
            } else {
                if (cfType == ColumnFamilyType.Super)
                    throw new InvalidRequestException("Secondary indexes are not supported on supercolumns");
                // should have a default set by now if none was provided
                assert c.index_name != null;
                if (!Migration.isLegalName(c.index_name))
                    throw new InvalidRequestException("Illegal index name " + c.index_name);
                // check index names against this CF _and_ globally
                if (indexNames.contains(c.index_name))
                    throw new InvalidRequestException("Duplicate index name " + c.index_name);
                indexNames.add(c.index_name);
                ColumnDefinition oldCd = old == null ? null : old.getColumnDefinition(c.name);
                if (oldCd != null && oldCd.getIndexType() != null) {
                    assert oldCd.getIndexName() != null;
                    if (!oldCd.getIndexName().equals(c.index_name))
                        throw new InvalidRequestException("Cannot modify index name");
                }
                if (c.index_type == IndexType.CUSTOM) {
                    if (c.index_options == null || !c.index_options.containsKey(SecondaryIndex.CUSTOM_INDEX_OPTION_NAME))
                        throw new InvalidRequestException("Required index option missing: " + SecondaryIndex.CUSTOM_INDEX_OPTION_NAME);
                }
                // Create the index type and validate the options
                ColumnDefinition cdef = ColumnDefinition.fromThrift(c);
                // This method validates the column metadata but does not intialize the index
                SecondaryIndex.createInstance(null, cdef);
            }
        }
        validateMinMaxCompactionThresholds(cf_def);
        // validates compression parameters
        CompressionParameters.create(cf_def.compression_options);
    } catch (ConfigurationException e) {
        throw new InvalidRequestException(e.getMessage());
    }
}
Also used : MarshalException(org.apache.cassandra.db.marshal.MarshalException) AbstractType(org.apache.cassandra.db.marshal.AbstractType)

Example 2 with MarshalException

use of org.apache.cassandra.db.marshal.MarshalException in project eiger by wlloyd.

the class ThriftValidation method validateRange.

public static void validateRange(CFMetaData metadata, ColumnParent column_parent, SliceRange range) throws InvalidRequestException {
    AbstractType comparator = metadata.getComparatorFor(column_parent.super_column);
    try {
        comparator.validate(range.start);
        comparator.validate(range.finish);
    } catch (MarshalException e) {
        throw new InvalidRequestException(e.getMessage());
    }
    if (range.count < 0)
        throw new InvalidRequestException("get_slice requires non-negative count");
    Comparator<ByteBuffer> orderedComparator = range.isReversed() ? comparator.reverseComparator : comparator;
    if (range.start.remaining() > 0 && range.finish.remaining() > 0 && orderedComparator.compare(range.start, range.finish) > 0) {
        throw new InvalidRequestException("range finish must come after start in the order of traversal");
    }
}
Also used : MarshalException(org.apache.cassandra.db.marshal.MarshalException) AbstractType(org.apache.cassandra.db.marshal.AbstractType) ByteBuffer(java.nio.ByteBuffer)

Example 3 with MarshalException

use of org.apache.cassandra.db.marshal.MarshalException in project eiger by wlloyd.

the class ThriftValidation method validateColumnData.

/**
 * Validates the data part of the column (everything in the Column object but the name, which is assumed to be valid)
 */
public static void validateColumnData(CFMetaData metadata, Column column, boolean isSubColumn) throws InvalidRequestException {
    validateTtl(column);
    if (!column.isSetValue())
        throw new InvalidRequestException("Column value is required");
    if (!column.isSetTimestamp())
        throw new InvalidRequestException("Column timestamp is required");
    ColumnDefinition columnDef = metadata.getColumnDefinition(column.name);
    try {
        AbstractType validator = metadata.getValueValidator(columnDef);
        if (validator != null)
            validator.validate(column.value);
    } catch (MarshalException me) {
        if (logger.isDebugEnabled())
            logger.debug("rejecting invalid value " + ByteBufferUtil.bytesToHex(summarize(column.value)));
        throw new InvalidRequestException(String.format("(%s) [%s][%s][%s] failed validation", me.getMessage(), metadata.ksName, metadata.cfName, (isSubColumn ? metadata.subcolumnComparator : metadata.comparator).getString(column.name)));
    }
    // Indexed column values cannot be larger than 64K.  See CASSANDRA-3057 for more details
    if (columnDef != null && columnDef.getIndexType() != null && column.value.remaining() > FBUtilities.MAX_UNSIGNED_SHORT)
        throw new InvalidRequestException(String.format("Can't index column value of size %d for index %s in CF %s of KS %s", column.value.remaining(), columnDef.getIndexName(), metadata.cfName, metadata.ksName));
}
Also used : MarshalException(org.apache.cassandra.db.marshal.MarshalException) AbstractType(org.apache.cassandra.db.marshal.AbstractType)

Example 4 with MarshalException

use of org.apache.cassandra.db.marshal.MarshalException in project eiger by wlloyd.

the class CassandraServer method add.

// counter methods
@Override
public WriteResult add(ByteBuffer key, ColumnParent column_parent, CounterColumn column, ConsistencyLevel consistency_level, Set<Dep> deps, long lts) throws InvalidRequestException, UnavailableException, TimedOutException, TException {
    LamportClock.updateTime(lts);
    logger.debug("add");
    state().hasColumnFamilyAccess(column_parent.column_family, Permission.WRITE);
    String keyspace = state().getKeyspace();
    CFMetaData metadata = ThriftValidation.validateColumnFamily(keyspace, column_parent.column_family, true);
    ThriftValidation.validateKey(metadata, key);
    ThriftValidation.validateCommutativeForWrite(metadata, consistency_level);
    ThriftValidation.validateColumnParent(metadata, column_parent);
    // SuperColumn field is usually optional, but not when we're adding
    if (metadata.cfType == ColumnFamilyType.Super && column_parent.super_column == null) {
        throw new InvalidRequestException("missing mandatory super column name for super CF " + column_parent.column_family);
    }
    ThriftValidation.validateColumnNames(metadata, column_parent, Arrays.asList(column.name));
    Set<Dependency> dependencies = new HashSet<Dependency>();
    for (Dep dep : deps) {
        dependencies.add(new Dependency(dep));
    }
    // add operations also need a dependency on the previous value for this datacenter
    try {
        ColumnOrSuperColumn cosc = this.internal_get(key, new ColumnPath(column_parent.column_family).setSuper_column(column_parent.super_column).setColumn(column.name), ConsistencyLevel.ONE);
        if (cosc.isSetColumn()) {
            // on that delete because clients have to wait until it reaches all nodes before resurrecting it
            assert cosc.column.isSetDeleted_time();
        } else {
            ClientContext tmpContext = new ClientContext();
            tmpContext.addDep(key, cosc);
            if (tmpContext.getDeps().size() > 0) {
                Dependency newDep = new Dependency(tmpContext.getDeps().iterator().next());
                dependencies.add(newDep);
                logger.debug("Adding a dependency on the previous value from this dc: " + newDep);
            }
        }
    } catch (NotFoundException e1) {
    // this is fine, it's the first add for this datacenter, no dep needed
    }
    RowMutation rm = new RowMutation(keyspace, key, dependencies);
    long timestamp = LamportClock.getVersion();
    try {
        rm.addCounter(new QueryPath(column_parent.column_family, column_parent.super_column, column.name), column.value, timestamp, timestamp, null);
    } catch (MarshalException e) {
        throw new InvalidRequestException(e.getMessage());
    }
    doInsert(consistency_level, Arrays.asList(new CounterMutation(rm, consistency_level)));
    if (logger.isTraceEnabled()) {
        logger.trace("add({}, {}, {}, {}, {}, {}) = {}", new Object[] { ByteBufferUtil.bytesToHex(key), column_parent, column, consistency_level, deps, lts, timestamp });
    }
    return new WriteResult(timestamp, LamportClock.sendTimestamp());
}
Also used : MarshalException(org.apache.cassandra.db.marshal.MarshalException) ClientContext(org.apache.cassandra.client.ClientContext) QueryPath(org.apache.cassandra.db.filter.QueryPath)

Example 5 with MarshalException

use of org.apache.cassandra.db.marshal.MarshalException in project eiger by wlloyd.

the class CassandraServer method internal_insert.

private void internal_insert(ByteBuffer key, ColumnParent column_parent, Column column, ConsistencyLevel consistency_level, Set<Dep> deps) throws InvalidRequestException, UnavailableException, TimedOutException {
    state().hasColumnFamilyAccess(column_parent.column_family, Permission.WRITE);
    CFMetaData metadata = ThriftValidation.validateColumnFamily(state().getKeyspace(), column_parent.column_family, false);
    ThriftValidation.validateKey(metadata, key);
    ThriftValidation.validateColumnParent(metadata, column_parent);
    // SuperColumn field is usually optional, but not when we're inserting
    if (metadata.cfType == ColumnFamilyType.Super && column_parent.super_column == null) {
        throw new InvalidRequestException("missing mandatory super column name for super CF " + column_parent.column_family);
    }
    ThriftValidation.validateColumnNames(metadata, column_parent, Arrays.asList(column.name));
    ThriftValidation.validateColumnData(metadata, column, column_parent.super_column != null);
    // be 0 when sent to us, and we'll set it here.
    if (column.timestamp == 0) {
        column.timestamp = LamportClock.getVersion();
        logger.debug("Setting timestamp to {}", column.timestamp);
    }
    Set<Dependency> dependencies = new HashSet<Dependency>();
    for (Dep dep : deps) {
        dependencies.add(new Dependency(dep));
    }
    RowMutation rm = new RowMutation(state().getKeyspace(), key, dependencies);
    try {
        rm.add(new QueryPath(column_parent.column_family, column_parent.super_column, column.name), column.value, column.timestamp, column.ttl);
    } catch (MarshalException e) {
        throw new InvalidRequestException(e.getMessage());
    }
    doInsert(consistency_level, Arrays.asList(rm));
}
Also used : QueryPath(org.apache.cassandra.db.filter.QueryPath) MarshalException(org.apache.cassandra.db.marshal.MarshalException)

Aggregations

MarshalException (org.apache.cassandra.db.marshal.MarshalException)6 AbstractType (org.apache.cassandra.db.marshal.AbstractType)4 ByteBuffer (java.nio.ByteBuffer)2 QueryPath (org.apache.cassandra.db.filter.QueryPath)2 ClientContext (org.apache.cassandra.client.ClientContext)1