Search in sources :

Example 16 with ResultMessage

use of org.apache.cassandra.transport.messages.ResultMessage in project cassandra by apache.

the class Coordinator method executeInternal.

private SimpleQueryResult executeInternal(String query, ConsistencyLevel consistencyLevelOrigin, Object[] boundValues) {
    ClientState clientState = makeFakeClientState();
    CQLStatement prepared = QueryProcessor.getStatement(query, clientState);
    List<ByteBuffer> boundBBValues = new ArrayList<>();
    ConsistencyLevel consistencyLevel = ConsistencyLevel.valueOf(consistencyLevelOrigin.name());
    for (Object boundValue : boundValues) boundBBValues.add(ByteBufferUtil.objectToBytes(boundValue));
    prepared.validate(QueryState.forInternalCalls().getClientState());
    // Start capturing warnings on this thread. Note that this will implicitly clear out any previous
    // warnings as it sets a new State instance on the ThreadLocal.
    ClientWarn.instance.captureWarnings();
    CoordinatorWarnings.init();
    try {
        ResultMessage res = prepared.execute(QueryState.forInternalCalls(), QueryOptions.create(toCassandraCL(consistencyLevel), boundBBValues, false, Integer.MAX_VALUE, null, null, ProtocolVersion.CURRENT, null), nanoTime());
        // Collect warnings reported during the query.
        CoordinatorWarnings.done();
        if (res != null)
            res.setWarnings(ClientWarn.instance.getWarnings());
        return RowUtil.toQueryResult(res);
    } catch (Exception | Error e) {
        CoordinatorWarnings.done();
        throw e;
    } finally {
        CoordinatorWarnings.reset();
        ClientWarn.instance.resetWarnings();
    }
}
Also used : ClientState(org.apache.cassandra.service.ClientState) CQLStatement(org.apache.cassandra.cql3.CQLStatement) ConsistencyLevel(org.apache.cassandra.distributed.api.ConsistencyLevel) ArrayList(java.util.ArrayList) ResultMessage(org.apache.cassandra.transport.messages.ResultMessage) ByteBuffer(java.nio.ByteBuffer)

Example 17 with ResultMessage

use of org.apache.cassandra.transport.messages.ResultMessage in project cassandra by apache.

the class Instance method unsafeExecuteInternalWithResult.

public static SimpleQueryResult unsafeExecuteInternalWithResult(String query, Object... args) {
    ClientWarn.instance.captureWarnings();
    CoordinatorWarnings.init();
    try {
        QueryHandler.Prepared prepared = QueryProcessor.prepareInternal(query);
        ResultMessage result = prepared.statement.executeLocally(QueryProcessor.internalQueryState(), QueryProcessor.makeInternalOptions(prepared.statement, args));
        CoordinatorWarnings.done();
        if (result != null)
            result.setWarnings(ClientWarn.instance.getWarnings());
        return RowUtil.toQueryResult(result);
    } catch (Exception | Error e) {
        CoordinatorWarnings.done();
        throw e;
    } finally {
        CoordinatorWarnings.reset();
        ClientWarn.instance.resetWarnings();
    }
}
Also used : QueryHandler(org.apache.cassandra.cql3.QueryHandler) ResultMessage(org.apache.cassandra.transport.messages.ResultMessage) ListenerNotFoundException(javax.management.ListenerNotFoundException) IOException(java.io.IOException) TimeoutException(java.util.concurrent.TimeoutException) BindException(java.net.BindException) SystemExitException(org.apache.cassandra.tools.SystemExitException)

Example 18 with ResultMessage

use of org.apache.cassandra.transport.messages.ResultMessage in project cassandra by apache.

the class Query method call.

public Object[][] call() {
    ConsistencyLevel commitConsistency = toCassandraCL(commitConsistencyOrigin);
    ConsistencyLevel serialConsistency = serialConsistencyOrigin == null ? null : toCassandraCL(serialConsistencyOrigin);
    ClientState clientState = Coordinator.makeFakeClientState();
    CQLStatement prepared = QueryProcessor.getStatement(query, clientState);
    List<ByteBuffer> boundBBValues = new ArrayList<>();
    for (Object boundValue : boundValues) boundBBValues.add(ByteBufferUtil.objectToBytes(boundValue));
    prepared.validate(QueryState.forInternalCalls().getClientState());
    // Start capturing warnings on this thread. Note that this will implicitly clear out any previous
    // warnings as it sets a new State instance on the ThreadLocal.
    ClientWarn.instance.captureWarnings();
    ResultMessage res = prepared.execute(QueryState.forInternalCalls(), QueryOptions.create(commitConsistency, boundBBValues, false, Integer.MAX_VALUE, null, serialConsistency, ProtocolVersion.V4, null, timestamp, FBUtilities.nowInSeconds()), nanoTime());
    // Collect warnings reported during the query.
    if (res != null)
        res.setWarnings(ClientWarn.instance.getWarnings());
    return RowUtil.toQueryResult(res).toObjectArrays();
}
Also used : ConsistencyLevel(org.apache.cassandra.db.ConsistencyLevel) ClientState(org.apache.cassandra.service.ClientState) CQLStatement(org.apache.cassandra.cql3.CQLStatement) ArrayList(java.util.ArrayList) ResultMessage(org.apache.cassandra.transport.messages.ResultMessage) ByteBuffer(java.nio.ByteBuffer)

Example 19 with ResultMessage

use of org.apache.cassandra.transport.messages.ResultMessage in project cassandra by apache.

the class AlterSchemaStatement method execute.

public ResultMessage execute(QueryState state, boolean locally) {
    if (SchemaConstants.isLocalSystemKeyspace(keyspaceName))
        throw ire("System keyspace '%s' is not user-modifiable", keyspaceName);
    KeyspaceMetadata keyspace = Schema.instance.getKeyspaceMetadata(keyspaceName);
    if (null != keyspace && keyspace.isVirtual())
        throw ire("Virtual keyspace '%s' is not user-modifiable", keyspaceName);
    validateKeyspaceName();
    KeyspacesDiff diff = MigrationManager.announce(this, locally);
    clientWarnings(diff).forEach(ClientWarn.instance::warn);
    if (diff.isEmpty())
        return new ResultMessage.Void();
    /*
         * When a schema alteration results in a new db object being created, we grant permissions on the new
         * object to the user performing the request if:
         * - the user is not anonymous
         * - the configured IAuthorizer supports granting of permissions (not all do, AllowAllAuthorizer doesn't and
         *   custom external implementations may not)
         */
    AuthenticatedUser user = state.getClientState().getUser();
    if (null != user && !user.isAnonymous())
        createdResources(diff).forEach(r -> grantPermissionsOnResource(r, user));
    return new ResultMessage.SchemaChange(schemaChangeEvent(diff));
}
Also used : ResultMessage(org.apache.cassandra.transport.messages.ResultMessage) ImmutableSet(com.google.common.collect.ImmutableSet) QueryState(org.apache.cassandra.service.QueryState) ClientWarn(org.apache.cassandra.service.ClientWarn) SchemaChange(org.apache.cassandra.transport.Event.SchemaChange) ClientState(org.apache.cassandra.service.ClientState) Set(java.util.Set) AuthenticatedUser(org.apache.cassandra.auth.AuthenticatedUser) CQLStatement(org.apache.cassandra.cql3.CQLStatement) IResource(org.apache.cassandra.auth.IResource) KeyspacesDiff(org.apache.cassandra.schema.Keyspaces.KeyspacesDiff) DatabaseDescriptor(org.apache.cassandra.config.DatabaseDescriptor) QueryOptions(org.apache.cassandra.cql3.QueryOptions) InvalidRequestException(org.apache.cassandra.exceptions.InvalidRequestException) org.apache.cassandra.schema(org.apache.cassandra.schema) KeyspacesDiff(org.apache.cassandra.schema.Keyspaces.KeyspacesDiff) SchemaChange(org.apache.cassandra.transport.Event.SchemaChange) ResultMessage(org.apache.cassandra.transport.messages.ResultMessage) AuthenticatedUser(org.apache.cassandra.auth.AuthenticatedUser)

Example 20 with ResultMessage

use of org.apache.cassandra.transport.messages.ResultMessage in project cassandra by apache.

the class DescribeStatement method executeLocally.

@Override
public ResultMessage executeLocally(QueryState state, QueryOptions options) {
    Keyspaces keyspaces = Schema.instance.snapshot();
    UUID schemaVersion = Schema.instance.getVersion();
    keyspaces = Keyspaces.builder().add(keyspaces).add(VirtualKeyspaceRegistry.instance.virtualKeyspacesMetadata()).build();
    PagingState pagingState = options.getPagingState();
    // The paging implemented here uses some arbitray row number as the partition-key for paging,
    // which is used to skip/limit the result from the Java Stream. This works good enough for
    // reasonably sized schemas. Even a 'DESCRIBE SCHEMA' for an abnormally schema with 10000 tables
    // completes within a few seconds. This seems good enough for now. Once Cassandra actually supports
    // more than a few hundred tables, the implementation here should be reconsidered.
    // 
    // Paging is only supported on row-level.
    // 
    // The "partition key" in the paging-state contains a serialized object:
    // (short) version, currently 0x0001
    // (long) row offset
    // (vint bytes) serialized schema hash (currently the result of Keyspaces.hashCode())
    // 
    long offset = getOffset(pagingState, schemaVersion);
    int pageSize = options.getPageSize();
    Stream<? extends T> stream = describe(state.getClientState(), keyspaces);
    if (offset > 0L)
        stream = stream.skip(offset);
    if (pageSize > 0)
        stream = stream.limit(pageSize);
    List<List<ByteBuffer>> rows = stream.map(e -> toRow(e, includeInternalDetails)).collect(Collectors.toList());
    ResultSet.ResultMetadata resultMetadata = new ResultSet.ResultMetadata(metadata(state.getClientState()));
    ResultSet result = new ResultSet(resultMetadata, rows);
    if (pageSize > 0 && rows.size() == pageSize) {
        result.metadata.setHasMorePages(getPagingState(offset + pageSize, schemaVersion));
    }
    return new ResultMessage.Rows(result);
}
Also used : AuditLogContext(org.apache.cassandra.audit.AuditLogContext) java.util(java.util) RequestValidations.invalidRequest(org.apache.cassandra.cql3.statements.RequestValidations.invalidRequest) DataOutputBuffer(org.apache.cassandra.io.util.DataOutputBuffer) RequestValidations.checkNotEmpty(org.apache.cassandra.cql3.statements.RequestValidations.checkNotEmpty) BiFunction(java.util.function.BiFunction) ByteBufferUtil.bytes(org.apache.cassandra.utils.ByteBufferUtil.bytes) ByteBuffer(java.nio.ByteBuffer) org.apache.cassandra.cql3(org.apache.cassandra.cql3) RequestExecutionException(org.apache.cassandra.exceptions.RequestExecutionException) ListType(org.apache.cassandra.db.marshal.ListType) UTF8Type(org.apache.cassandra.db.marshal.UTF8Type) ImmutableList(com.google.common.collect.ImmutableList) KeyspaceNotDefinedException(org.apache.cassandra.db.KeyspaceNotDefinedException) FunctionName(org.apache.cassandra.cql3.functions.FunctionName) RequestValidations.checkNotNull(org.apache.cassandra.cql3.statements.RequestValidations.checkNotNull) DatabaseDescriptor(org.apache.cassandra.config.DatabaseDescriptor) InvalidRequestException(org.apache.cassandra.exceptions.InvalidRequestException) RequestValidationException(org.apache.cassandra.exceptions.RequestValidationException) ResultMessage(org.apache.cassandra.transport.messages.ResultMessage) FBUtilities(org.apache.cassandra.utils.FBUtilities) QueryState(org.apache.cassandra.service.QueryState) ByteBufferUtil(org.apache.cassandra.utils.ByteBufferUtil) RequestValidations.checkTrue(org.apache.cassandra.cql3.statements.RequestValidations.checkTrue) ClientState(org.apache.cassandra.service.ClientState) StorageService(org.apache.cassandra.service.StorageService) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) UUIDGen(org.apache.cassandra.utils.UUIDGen) String.format(java.lang.String.format) AuditLogEntryType(org.apache.cassandra.audit.AuditLogEntryType) MapType(org.apache.cassandra.db.marshal.MapType) Stream(java.util.stream.Stream) PagingState(org.apache.cassandra.service.pager.PagingState) VirtualKeyspaceRegistry(org.apache.cassandra.db.virtual.VirtualKeyspaceRegistry) org.apache.cassandra.schema(org.apache.cassandra.schema) DataInputBuffer(org.apache.cassandra.io.util.DataInputBuffer) PagingState(org.apache.cassandra.service.pager.PagingState) ImmutableList(com.google.common.collect.ImmutableList)

Aggregations

ResultMessage (org.apache.cassandra.transport.messages.ResultMessage)20 ClientState (org.apache.cassandra.service.ClientState)4 ByteBuffer (java.nio.ByteBuffer)3 CQLStatement (org.apache.cassandra.cql3.CQLStatement)3 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 AuthenticatedUser (org.apache.cassandra.auth.AuthenticatedUser)2 DatabaseDescriptor (org.apache.cassandra.config.DatabaseDescriptor)2 InvalidRequestException (org.apache.cassandra.exceptions.InvalidRequestException)2 org.apache.cassandra.schema (org.apache.cassandra.schema)2 QueryState (org.apache.cassandra.service.QueryState)2 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableSet (com.google.common.collect.ImmutableSet)1 String.format (java.lang.String.format)1 BindException (java.net.BindException)1 java.util (java.util)1 Set (java.util.Set)1 TimeoutException (java.util.concurrent.TimeoutException)1 BiFunction (java.util.function.BiFunction)1 Collectors (java.util.stream.Collectors)1