Search in sources :

Example 1 with InsufficientConsistencyException

use of com.palantir.atlasdb.keyvalue.api.InsufficientConsistencyException in project atlasdb by palantir.

the class CqlKeyValueService method getRangeWithPageCreator.

public <T> ClosableIterator<RowResult<T>> getRangeWithPageCreator(TableReference tableRef, RangeRequest rangeRequest, long timestamp, com.datastax.driver.core.ConsistencyLevel consistency, Supplier<ResultsExtractor<T>> resultsExtractor) {
    if (rangeRequest.isReverse()) {
        throw new UnsupportedOperationException();
    }
    if (rangeRequest.isEmptyRange()) {
        return ClosableIterators.wrap(ImmutableList.<RowResult<T>>of().iterator());
    }
    final int batchHint = rangeRequest.getBatchHint() == null ? 100 : rangeRequest.getBatchHint();
    final ColumnSelection selection = rangeRequest.getColumnNames().isEmpty() ? ColumnSelection.all() : ColumnSelection.create(rangeRequest.getColumnNames());
    final byte[] endExclusive = rangeRequest.getEndExclusive();
    final StringBuilder bindQuery = new StringBuilder();
    bindQuery.append("SELECT * FROM " + getFullTableName(tableRef) + " WHERE token(" + fieldNameProvider.row() + ") >= token(?) ");
    if (endExclusive.length > 0) {
        bindQuery.append("AND token(" + fieldNameProvider.row() + ") < token(?) ");
    }
    bindQuery.append("LIMIT " + batchHint);
    final String getLastRowQuery = "SELECT * FROM " + getFullTableName(tableRef) + " WHERE " + fieldNameProvider.row() + " = ?";
    return ClosableIterators.wrap(new AbstractPagingIterable<RowResult<T>, TokenBackedBasicResultsPage<RowResult<T>, byte[]>>() {

        @Override
        protected TokenBackedBasicResultsPage<RowResult<T>, byte[]> getFirstPage() throws Exception {
            return getPage(rangeRequest.getStartInclusive());
        }

        @Override
        protected TokenBackedBasicResultsPage<RowResult<T>, byte[]> getNextPage(TokenBackedBasicResultsPage<RowResult<T>, byte[]> previous) throws Exception {
            return getPage(previous.getTokenForNextPage());
        }

        TokenBackedBasicResultsPage<RowResult<T>, byte[]> getPage(final byte[] startKey) throws Exception {
            BoundStatement boundStatement = getPreparedStatement(tableRef, bindQuery.toString(), session).setConsistencyLevel(consistency).bind();
            boundStatement.setBytes(0, ByteBuffer.wrap(startKey));
            if (endExclusive.length > 0) {
                boundStatement.setBytes(1, ByteBuffer.wrap(endExclusive));
            }
            ResultSet resultSet = session.execute(boundStatement);
            List<Row> rows = Lists.newArrayList(resultSet.all());
            cqlKeyValueServices.logTracedQuery(bindQuery.toString(), resultSet, session, cqlStatementCache.normalQuery);
            byte[] maxRow = null;
            ResultsExtractor<T> extractor = resultsExtractor.get();
            for (Row row : rows) {
                byte[] rowName = getRowName(row);
                if (maxRow == null) {
                    maxRow = rowName;
                } else {
                    maxRow = PtBytes.BYTES_COMPARATOR.max(maxRow, rowName);
                }
            }
            if (maxRow == null) {
                return new SimpleTokenBackedResultsPage<>(endExclusive, ImmutableList.of(), false);
            }
            // get the rest of the last row
            BoundStatement boundLastRow = getPreparedStatement(tableRef, getLastRowQuery, session).bind();
            boundLastRow.setBytes(fieldNameProvider.row(), ByteBuffer.wrap(maxRow));
            try {
                resultSet = session.execute(boundLastRow);
            } catch (com.datastax.driver.core.exceptions.UnavailableException e) {
                throw new InsufficientConsistencyException("This operation requires all Cassandra" + " nodes to be up and available.", e);
            }
            rows.addAll(resultSet.all());
            cqlKeyValueServices.logTracedQuery(getLastRowQuery, resultSet, session, cqlStatementCache.normalQuery);
            for (Row row : rows) {
                extractor.internalExtractResult(timestamp, selection, getRowName(row), getColName(row), getValue(row), getTs(row));
            }
            SortedMap<byte[], SortedMap<byte[], T>> resultsByRow = Cells.breakCellsUpByRow(extractor.asMap());
            return ResultsExtractor.getRowResults(endExclusive, maxRow, resultsByRow);
        }
    }.iterator());
}
Also used : TokenBackedBasicResultsPage(com.palantir.util.paging.TokenBackedBasicResultsPage) UnavailableException(com.datastax.driver.core.exceptions.UnavailableException) RowResult(com.palantir.atlasdb.keyvalue.api.RowResult) ColumnSelection(com.palantir.atlasdb.keyvalue.api.ColumnSelection) InsufficientConsistencyException(com.palantir.atlasdb.keyvalue.api.InsufficientConsistencyException) SimpleTokenBackedResultsPage(com.palantir.util.paging.SimpleTokenBackedResultsPage) ImmutableSortedMap(com.google.common.collect.ImmutableSortedMap) SortedMap(java.util.SortedMap) ResultSet(com.datastax.driver.core.ResultSet) AbstractPagingIterable(com.palantir.util.paging.AbstractPagingIterable) Row(com.datastax.driver.core.Row) BoundStatement(com.datastax.driver.core.BoundStatement)

Example 2 with InsufficientConsistencyException

use of com.palantir.atlasdb.keyvalue.api.InsufficientConsistencyException in project atlasdb by palantir.

the class CqlKeyValueService method alterTableForCompaction.

private void alterTableForCompaction(TableReference tableRef, int gcGraceSeconds, float tombstoneThreshold) {
    log.trace("Altering table {} to have gc_grace_seconds={} and tombstone_threshold={}", tableRef, gcGraceSeconds, String.format("%.2f", tombstoneThreshold));
    String alterTableQuery = "ALTER TABLE " + getFullTableName(tableRef) + " WITH gc_grace_seconds = " + gcGraceSeconds + " and compaction = {'class':'" + CassandraConstants.LEVELED_COMPACTION_STRATEGY + "', 'tombstone_threshold':" + tombstoneThreshold + "};";
    BoundStatement alterTable = getPreparedStatement(tableRef, alterTableQuery, longRunningQuerySession).setConsistencyLevel(ConsistencyLevel.ALL).bind();
    ResultSet resultSet;
    try {
        resultSet = longRunningQuerySession.execute(alterTable);
    } catch (UnavailableException e) {
        throw new InsufficientConsistencyException("Alter table requires all Cassandra" + " nodes to be up and available.", e);
    } catch (Exception e) {
        throw Throwables.throwUncheckedException(e);
    }
    cqlKeyValueServices.logTracedQuery(alterTableQuery, resultSet, session, cqlStatementCache.normalQuery);
}
Also used : InsufficientConsistencyException(com.palantir.atlasdb.keyvalue.api.InsufficientConsistencyException) ResultSet(com.datastax.driver.core.ResultSet) UnavailableException(com.datastax.driver.core.exceptions.UnavailableException) BoundStatement(com.datastax.driver.core.BoundStatement) NoHostAvailableException(com.datastax.driver.core.exceptions.NoHostAvailableException) InsufficientConsistencyException(com.palantir.atlasdb.keyvalue.api.InsufficientConsistencyException) KeyAlreadyExistsException(com.palantir.atlasdb.keyvalue.api.KeyAlreadyExistsException) ExecutionException(java.util.concurrent.ExecutionException) InvalidQueryException(com.datastax.driver.core.exceptions.InvalidQueryException) UnavailableException(com.datastax.driver.core.exceptions.UnavailableException)

Example 3 with InsufficientConsistencyException

use of com.palantir.atlasdb.keyvalue.api.InsufficientConsistencyException in project atlasdb by palantir.

the class SchemaMutationLock method queryExistingLockColumn.

private Optional<Column> queryExistingLockColumn(CassandraClient client) throws TException {
    TableReference lockTableRef = lockTable.get();
    Column existingColumn = null;
    ConsistencyLevel localQuorum = ConsistencyLevel.LOCAL_QUORUM;
    try {
        ColumnOrSuperColumn result = queryRunner.run(client, lockTableRef, () -> client.get(lockTableRef, getGlobalDdlLockRowName(), getGlobalDdlLockColumnName(), localQuorum));
        existingColumn = result.getColumn();
    } catch (UnavailableException e) {
        throw new InsufficientConsistencyException("Checking the schema lock requires " + localQuorum + " Cassandra nodes to be up and available.", e);
    } catch (NotFoundException e) {
        log.debug("No existing schema lock found in table [{}]", SafeArg.of("tableName", lockTableRef));
    }
    return Optional.ofNullable(existingColumn);
}
Also used : ConsistencyLevel(org.apache.cassandra.thrift.ConsistencyLevel) ColumnOrSuperColumn(org.apache.cassandra.thrift.ColumnOrSuperColumn) InsufficientConsistencyException(com.palantir.atlasdb.keyvalue.api.InsufficientConsistencyException) TableReference(com.palantir.atlasdb.keyvalue.api.TableReference) Column(org.apache.cassandra.thrift.Column) ColumnOrSuperColumn(org.apache.cassandra.thrift.ColumnOrSuperColumn) UnavailableException(org.apache.cassandra.thrift.UnavailableException) NotFoundException(org.apache.cassandra.thrift.NotFoundException)

Example 4 with InsufficientConsistencyException

use of com.palantir.atlasdb.keyvalue.api.InsufficientConsistencyException in project atlasdb by palantir.

the class CassandraKeyValueServiceImpl method executeCheckAndSet.

private CASResult executeCheckAndSet(CassandraClient client, CheckAndSetRequest request) throws TException {
    try {
        TableReference table = request.table();
        Cell cell = request.cell();
        long timestamp = AtlasDbConstants.TRANSACTION_TS;
        ByteBuffer rowName = ByteBuffer.wrap(cell.getRowName());
        byte[] colName = CassandraKeyValueServices.makeCompositeBuffer(cell.getColumnName(), timestamp).array();
        List<Column> oldColumns;
        java.util.Optional<byte[]> oldValue = request.oldValue();
        if (oldValue.isPresent()) {
            oldColumns = ImmutableList.of(makeColumn(colName, oldValue.get(), timestamp));
        } else {
            oldColumns = ImmutableList.of();
        }
        Column newColumn = makeColumn(colName, request.newValue(), timestamp);
        return queryRunner.run(client, table, () -> client.cas(table, rowName, oldColumns, ImmutableList.of(newColumn), ConsistencyLevel.SERIAL, writeConsistency));
    } catch (UnavailableException e) {
        throw new InsufficientConsistencyException("Check-and-set requires " + writeConsistency + " Cassandra nodes to be up and available.", e);
    }
}
Also used : InsufficientConsistencyException(com.palantir.atlasdb.keyvalue.api.InsufficientConsistencyException) TableReference(com.palantir.atlasdb.keyvalue.api.TableReference) Column(org.apache.cassandra.thrift.Column) ColumnOrSuperColumn(org.apache.cassandra.thrift.ColumnOrSuperColumn) UnavailableException(org.apache.cassandra.thrift.UnavailableException) Cell(com.palantir.atlasdb.keyvalue.api.Cell) ByteBuffer(java.nio.ByteBuffer)

Example 5 with InsufficientConsistencyException

use of com.palantir.atlasdb.keyvalue.api.InsufficientConsistencyException in project atlasdb by palantir.

the class CqlKeyValueService method createTables.

@Override
public void createTables(final Map<TableReference, byte[]> tableRefsToTableMetadata) {
    Collection<com.datastax.driver.core.TableMetadata> tables = cluster.getMetadata().getKeyspace(config.getKeyspaceOrThrow()).getTables();
    Set<TableReference> existingTables = Sets.newHashSet(Iterables.transform(tables, input -> TableReference.createUnsafe(input.getName())));
    // ScrubberStore likes to call createTable before our setup gets called...
    if (!existingTables.contains(AtlasDbConstants.DEFAULT_METADATA_TABLE)) {
        cqlKeyValueServices.createTableWithSettings(AtlasDbConstants.DEFAULT_METADATA_TABLE, AtlasDbConstants.EMPTY_TABLE_METADATA, this);
    }
    Set<TableReference> tablesToCreate = Sets.difference(tableRefsToTableMetadata.keySet(), existingTables);
    for (TableReference tableRef : tablesToCreate) {
        try {
            cqlKeyValueServices.createTableWithSettings(tableRef, tableRefsToTableMetadata.get(tableRef), this);
        } catch (com.datastax.driver.core.exceptions.UnavailableException e) {
            throw new InsufficientConsistencyException("Creating tables requires all Cassandra" + " nodes to be up and available.", e);
        }
    }
    if (!tablesToCreate.isEmpty()) {
        CqlKeyValueServices.waitForSchemaVersionsToCoalesce("createTables(" + tableRefsToTableMetadata.size() + " tables)", this);
    }
    internalPutMetadataForTables(tableRefsToTableMetadata, false);
}
Also used : PoolingOptions(com.datastax.driver.core.PoolingOptions) Arrays(java.util.Arrays) SSLContext(javax.net.ssl.SSLContext) Throwables(com.palantir.common.base.Throwables) ResultSetFuture(com.datastax.driver.core.ResultSetFuture) CheckAndSetRequest(com.palantir.atlasdb.keyvalue.api.CheckAndSetRequest) ConsistencyLevel(com.datastax.driver.core.ConsistencyLevel) Future(java.util.concurrent.Future) QueryOptions(com.datastax.driver.core.QueryOptions) Map(java.util.Map) TableReference(com.palantir.atlasdb.keyvalue.api.TableReference) CandidateCellForSweeping(com.palantir.atlasdb.keyvalue.api.CandidateCellForSweeping) AtlasDbConstants(com.palantir.atlasdb.AtlasDbConstants) AnnotationType(com.palantir.atlasdb.util.AnnotationType) Cell(com.palantir.atlasdb.keyvalue.api.Cell) Local(com.palantir.atlasdb.keyvalue.cassandra.CqlKeyValueServices.Local) TransactionType(com.palantir.atlasdb.keyvalue.cassandra.CqlKeyValueServices.TransactionType) Set(java.util.Set) Compression(com.datastax.driver.core.ProtocolOptions.Compression) NoHostAvailableException(com.datastax.driver.core.exceptions.NoHostAvailableException) InsufficientConsistencyException(com.palantir.atlasdb.keyvalue.api.InsufficientConsistencyException) Cluster(com.datastax.driver.core.Cluster) KeyAlreadyExistsException(com.palantir.atlasdb.keyvalue.api.KeyAlreadyExistsException) Cells(com.palantir.atlasdb.keyvalue.impl.Cells) Joiner(com.google.common.base.Joiner) AbstractKeyValueService(com.palantir.atlasdb.keyvalue.impl.AbstractKeyValueService) LoadBalancingPolicy(com.datastax.driver.core.policies.LoadBalancingPolicy) Iterables(com.google.common.collect.Iterables) CassandraKeyValueServiceConfig(com.palantir.atlasdb.cassandra.CassandraKeyValueServiceConfig) Row(com.datastax.driver.core.Row) PtBytes(com.palantir.atlasdb.encoding.PtBytes) Builder(com.google.common.collect.ImmutableMap.Builder) Multimaps(com.google.common.collect.Multimaps) PreparedStatement(com.datastax.driver.core.PreparedStatement) ClusterAvailabilityStatus(com.palantir.atlasdb.keyvalue.api.ClusterAvailabilityStatus) Lists(com.google.common.collect.Lists) SslSocketFactories(com.palantir.remoting3.config.ssl.SslSocketFactories) Predicates(com.google.common.base.Predicates) ImmutableSortedMap(com.google.common.collect.ImmutableSortedMap) Functions(com.google.common.base.Functions) UnsignedBytes(com.google.common.primitives.UnsignedBytes) AnnotatedCallable(com.palantir.atlasdb.util.AnnotatedCallable) ExecutionException(java.util.concurrent.ExecutionException) RowResult(com.palantir.atlasdb.keyvalue.api.RowResult) SSLOptions(com.datastax.driver.core.SSLOptions) GetCandidateCellsForSweepingShim(com.palantir.atlasdb.keyvalue.impl.GetCandidateCellsForSweepingShim) SortedSet(java.util.SortedSet) RoundRobinPolicy(com.datastax.driver.core.policies.RoundRobinPolicy) ClosableIterator(com.palantir.common.base.ClosableIterator) LoggerFactory(org.slf4j.LoggerFactory) ByteBuffer(java.nio.ByteBuffer) KeyValueServices(com.palantir.atlasdb.keyvalue.impl.KeyValueServices) HashMultimap(com.google.common.collect.HashMultimap) TreeMultimap(com.google.common.collect.TreeMultimap) Session(com.datastax.driver.core.Session) TokenAwarePolicy(com.datastax.driver.core.policies.TokenAwarePolicy) BatchStatement(com.datastax.driver.core.BatchStatement) ImmutableSet(com.google.common.collect.ImmutableSet) SortedSetMultimap(com.google.common.collect.SortedSetMultimap) ImmutableMap(com.google.common.collect.ImmutableMap) AbstractPagingIterable(com.palantir.util.paging.AbstractPagingIterable) Collection(java.util.Collection) NavigableMap(java.util.NavigableMap) InetSocketAddress(java.net.InetSocketAddress) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) AllTimestampsCollector(com.palantir.atlasdb.keyvalue.cassandra.CqlKeyValueServices.AllTimestampsCollector) ColumnSelection(com.palantir.atlasdb.keyvalue.api.ColumnSelection) RangeRequest(com.palantir.atlasdb.keyvalue.api.RangeRequest) List(java.util.List) Metadata(com.datastax.driver.core.Metadata) CandidateCellForSweepingRequest(com.palantir.atlasdb.keyvalue.api.CandidateCellForSweepingRequest) Host(com.datastax.driver.core.Host) InvalidQueryException(com.datastax.driver.core.exceptions.InvalidQueryException) Entry(java.util.Map.Entry) HostDistance(com.datastax.driver.core.HostDistance) Peer(com.palantir.atlasdb.keyvalue.cassandra.CqlKeyValueServices.Peer) SortedMap(java.util.SortedMap) Supplier(com.google.common.base.Supplier) Multimap(com.google.common.collect.Multimap) ResultSet(com.datastax.driver.core.ResultSet) BoundStatement(com.datastax.driver.core.BoundStatement) ImmutableList(com.google.common.collect.ImmutableList) ClosableIterators(com.palantir.common.base.ClosableIterators) SocketOptions(com.datastax.driver.core.SocketOptions) UnavailableException(com.datastax.driver.core.exceptions.UnavailableException) Logger(org.slf4j.Logger) Value(com.palantir.atlasdb.keyvalue.api.Value) StartTsResultsCollector(com.palantir.atlasdb.keyvalue.cassandra.CqlKeyValueServices.StartTsResultsCollector) IterablePartitioner(com.palantir.atlasdb.keyvalue.impl.IterablePartitioner) Visitor(com.palantir.common.visitor.Visitor) TokenBackedBasicResultsPage(com.palantir.util.paging.TokenBackedBasicResultsPage) Maps(com.google.common.collect.Maps) SetMultimap(com.google.common.collect.SetMultimap) Ordering(com.google.common.collect.Ordering) SimpleTokenBackedResultsPage(com.palantir.util.paging.SimpleTokenBackedResultsPage) WhiteListPolicy(com.datastax.driver.core.policies.WhiteListPolicy) Idempotent(com.palantir.common.annotation.Idempotent) LatencyAwarePolicy(com.datastax.driver.core.policies.LatencyAwarePolicy) InsufficientConsistencyException(com.palantir.atlasdb.keyvalue.api.InsufficientConsistencyException) TableReference(com.palantir.atlasdb.keyvalue.api.TableReference) UnavailableException(com.datastax.driver.core.exceptions.UnavailableException)

Aggregations

InsufficientConsistencyException (com.palantir.atlasdb.keyvalue.api.InsufficientConsistencyException)7 BoundStatement (com.datastax.driver.core.BoundStatement)5 ResultSet (com.datastax.driver.core.ResultSet)5 UnavailableException (com.datastax.driver.core.exceptions.UnavailableException)5 TableReference (com.palantir.atlasdb.keyvalue.api.TableReference)5 Row (com.datastax.driver.core.Row)2 InvalidQueryException (com.datastax.driver.core.exceptions.InvalidQueryException)2 NoHostAvailableException (com.datastax.driver.core.exceptions.NoHostAvailableException)2 ImmutableSortedMap (com.google.common.collect.ImmutableSortedMap)2 Column (org.apache.cassandra.thrift.Column)2 ColumnOrSuperColumn (org.apache.cassandra.thrift.ColumnOrSuperColumn)2 UnavailableException (org.apache.cassandra.thrift.UnavailableException)2 BatchStatement (com.datastax.driver.core.BatchStatement)1 Cluster (com.datastax.driver.core.Cluster)1 ConsistencyLevel (com.datastax.driver.core.ConsistencyLevel)1 Host (com.datastax.driver.core.Host)1 HostDistance (com.datastax.driver.core.HostDistance)1 Metadata (com.datastax.driver.core.Metadata)1 PoolingOptions (com.datastax.driver.core.PoolingOptions)1 PreparedStatement (com.datastax.driver.core.PreparedStatement)1