Search in sources :

Example 1 with KeyspaceMetadata

use of org.apache.cassandra.schema.KeyspaceMetadata in project cassandra by apache.

the class StorageService method maybeAddOrUpdateKeyspace.

/**
     * Ensure the schema of a pseudo-system keyspace (a distributed system keyspace: traces, auth and the so-called distributedKeyspace),
     * is up to date with what we expected (creating it if it doesn't exist and updating tables that may have been upgraded).
     */
private void maybeAddOrUpdateKeyspace(KeyspaceMetadata expected) {
    // Note that want to deal with the keyspace and its table a bit differently: for the keyspace definition
    // itself, we want to create it if it doesn't exist yet, but if it does exist, we don't want to modify it,
    // because user can modify the definition to change the replication factor (#6016) and we don't want to
    // override it. For the tables however, we have to deal with the fact that new version can add new columns
    // (#8162 being an example), so even if the table definition exists, we still need to force the "current"
    // version of the schema, the one the node will be expecting.
    KeyspaceMetadata defined = Schema.instance.getKeyspaceMetadata(expected.name);
    // If the keyspace doesn't exist, create it
    if (defined == null) {
        maybeAddKeyspace(expected);
        defined = Schema.instance.getKeyspaceMetadata(expected.name);
    }
    // all the expected tables are present
    for (TableMetadata expectedTable : expected.tables) {
        TableMetadata definedTable = defined.tables.get(expectedTable.name).orElse(null);
        if (definedTable == null || !definedTable.equals(expectedTable))
            MigrationManager.forceAnnounceNewTable(expectedTable);
    }
}
Also used : TableMetadata(org.apache.cassandra.schema.TableMetadata) KeyspaceMetadata(org.apache.cassandra.schema.KeyspaceMetadata)

Example 2 with KeyspaceMetadata

use of org.apache.cassandra.schema.KeyspaceMetadata in project cassandra by apache.

the class AlterTypeStatement method announceMigration.

public Event.SchemaChange announceMigration(QueryState queryState, boolean isLocalOnly) throws InvalidRequestException, ConfigurationException {
    KeyspaceMetadata ksm = Schema.instance.getKeyspaceMetadata(name.getKeyspace());
    if (ksm == null)
        throw new InvalidRequestException(String.format("Cannot alter type in unknown keyspace %s", name.getKeyspace()));
    UserType toUpdate = ksm.types.get(name.getUserTypeName()).orElseThrow(() -> new InvalidRequestException(String.format("No user type named %s exists.", name)));
    UserType updated = makeUpdatedType(toUpdate, ksm);
    // Now, we need to announce the type update to basically change it for new tables using this type,
    // but we also need to find all existing user types and CF using it and change them.
    MigrationManager.announceTypeUpdate(updated, isLocalOnly);
    return new Event.SchemaChange(Event.SchemaChange.Change.UPDATED, Event.SchemaChange.Target.TYPE, keyspace(), name.getStringTypeName());
}
Also used : KeyspaceMetadata(org.apache.cassandra.schema.KeyspaceMetadata)

Example 3 with KeyspaceMetadata

use of org.apache.cassandra.schema.KeyspaceMetadata in project cassandra by apache.

the class DropFunctionStatement method announceMigration.

public Event.SchemaChange announceMigration(QueryState queryState, boolean isLocalOnly) throws RequestValidationException {
    Function old = findFunction();
    if (old == null) {
        if (ifExists)
            return null;
        else
            throw new InvalidRequestException(getMissingFunctionError());
    }
    KeyspaceMetadata ksm = Schema.instance.getKeyspaceMetadata(old.name().keyspace);
    Collection<UDAggregate> referrers = ksm.functions.aggregatesUsingFunction(old);
    if (!referrers.isEmpty())
        throw new InvalidRequestException(String.format("Function '%s' still referenced by %s", old, referrers));
    MigrationManager.announceFunctionDrop((UDFunction) old, isLocalOnly);
    return new Event.SchemaChange(Event.SchemaChange.Change.DROPPED, Event.SchemaChange.Target.FUNCTION, old.name().keyspace, old.name().name, AbstractType.asCQLTypeStringList(old.argTypes()));
}
Also used : InvalidRequestException(org.apache.cassandra.exceptions.InvalidRequestException) KeyspaceMetadata(org.apache.cassandra.schema.KeyspaceMetadata)

Example 4 with KeyspaceMetadata

use of org.apache.cassandra.schema.KeyspaceMetadata in project cassandra by apache.

the class DropTableStatement method announceMigration.

public Event.SchemaChange announceMigration(QueryState queryState, boolean isLocalOnly) throws ConfigurationException {
    try {
        KeyspaceMetadata ksm = Schema.instance.getKeyspaceMetadata(keyspace());
        if (ksm == null)
            throw new ConfigurationException(String.format("Cannot drop table in unknown keyspace '%s'", keyspace()));
        TableMetadata metadata = ksm.getTableOrViewNullable(columnFamily());
        if (metadata != null) {
            if (metadata.isView())
                throw new InvalidRequestException("Cannot use DROP TABLE on Materialized View");
            boolean rejectDrop = false;
            StringBuilder messageBuilder = new StringBuilder();
            for (ViewMetadata def : ksm.views) {
                if (def.baseTableId.equals(metadata.id)) {
                    if (rejectDrop)
                        messageBuilder.append(',');
                    rejectDrop = true;
                    messageBuilder.append(def.name);
                }
            }
            if (rejectDrop) {
                throw new InvalidRequestException(String.format("Cannot drop table when materialized views still depend on it (%s.{%s})", keyspace(), messageBuilder.toString()));
            }
        }
        MigrationManager.announceTableDrop(keyspace(), columnFamily(), isLocalOnly);
        return new Event.SchemaChange(Event.SchemaChange.Change.DROPPED, Event.SchemaChange.Target.TABLE, keyspace(), columnFamily());
    } catch (ConfigurationException e) {
        if (ifExists)
            return null;
        throw e;
    }
}
Also used : TableMetadata(org.apache.cassandra.schema.TableMetadata) ConfigurationException(org.apache.cassandra.exceptions.ConfigurationException) InvalidRequestException(org.apache.cassandra.exceptions.InvalidRequestException) KeyspaceMetadata(org.apache.cassandra.schema.KeyspaceMetadata) ViewMetadata(org.apache.cassandra.schema.ViewMetadata)

Example 5 with KeyspaceMetadata

use of org.apache.cassandra.schema.KeyspaceMetadata in project cassandra by apache.

the class CQLSSTableWriter method getUDType.

/**
     * Returns the User Defined type, used in this SSTable Writer, that can
     * be used to create UDTValue instances.
     *
     * @param dataType name of the User Defined type
     * @return user defined type
     */
public com.datastax.driver.core.UserType getUDType(String dataType) {
    KeyspaceMetadata ksm = Schema.instance.getKeyspaceMetadata(insert.keyspace());
    UserType userType = ksm.types.getNullable(ByteBufferUtil.bytes(dataType));
    return (com.datastax.driver.core.UserType) UDHelper.driverType(userType);
}
Also used : KeyspaceMetadata(org.apache.cassandra.schema.KeyspaceMetadata) UserType(org.apache.cassandra.db.marshal.UserType)

Aggregations

KeyspaceMetadata (org.apache.cassandra.schema.KeyspaceMetadata)30 Test (org.junit.Test)17 TokenMetadata (org.apache.cassandra.locator.TokenMetadata)12 StringToken (org.apache.cassandra.dht.OrderPreservingPartitioner.StringToken)11 Range (org.apache.cassandra.dht.Range)8 TableMetadata (org.apache.cassandra.schema.TableMetadata)8 InetAddress (java.net.InetAddress)6 LongToken (org.apache.cassandra.dht.Murmur3Partitioner.LongToken)6 Token (org.apache.cassandra.dht.Token)6 InvalidRequestException (org.apache.cassandra.exceptions.InvalidRequestException)4 HashMap (java.util.HashMap)3 UserType (org.apache.cassandra.db.marshal.UserType)3 NetworkTopologyStrategy (org.apache.cassandra.locator.NetworkTopologyStrategy)3 TriggerMetadata (org.apache.cassandra.schema.TriggerMetadata)3 JavaBasedUDFunction (org.apache.cassandra.cql3.functions.JavaBasedUDFunction)1 UDAggregate (org.apache.cassandra.cql3.functions.UDAggregate)1 UDFunction (org.apache.cassandra.cql3.functions.UDFunction)1 ConfigurationException (org.apache.cassandra.exceptions.ConfigurationException)1 DataInputBuffer (org.apache.cassandra.io.util.DataInputBuffer)1 DataOutputBufferFixed (org.apache.cassandra.io.util.DataOutputBufferFixed)1