Search in sources :

Example 11 with Keyspace

use of org.apache.cassandra.db.Keyspace in project cassandra by apache.

the class CompactionsTest method testNeedsCleanup.

@Test
@Ignore("making ranges based on the keys, not on the tokens")
public void testNeedsCleanup() {
    Keyspace keyspace = Keyspace.open(KEYSPACE1);
    ColumnFamilyStore store = keyspace.getColumnFamilyStore("CF_STANDARD1");
    store.clearUnsafe();
    // disable compaction while flushing
    store.disableAutoCompaction();
    // 201, 202, ... 208, 209
    for (int i = 1; i < 10; i++) {
        insertRowWithKey(i);
        insertRowWithKey(i + 100);
        insertRowWithKey(i + 200);
    }
    store.forceBlockingFlush();
    assertEquals(1, store.getLiveSSTables().size());
    SSTableReader sstable = store.getLiveSSTables().iterator().next();
    // contiguous range spans all data
    assertFalse(CompactionManager.needsCleanup(sstable, makeRanges(0, 209)));
    assertFalse(CompactionManager.needsCleanup(sstable, makeRanges(0, 210)));
    // separate ranges span all data
    assertFalse(CompactionManager.needsCleanup(sstable, makeRanges(0, 9, 100, 109, 200, 209)));
    assertFalse(CompactionManager.needsCleanup(sstable, makeRanges(0, 109, 200, 210)));
    assertFalse(CompactionManager.needsCleanup(sstable, makeRanges(0, 9, 100, 210)));
    // one range is missing completely
    assertTrue(CompactionManager.needsCleanup(sstable, makeRanges(100, 109, 200, 209)));
    assertTrue(CompactionManager.needsCleanup(sstable, makeRanges(0, 9, 200, 209)));
    assertTrue(CompactionManager.needsCleanup(sstable, makeRanges(0, 9, 100, 109)));
    // the beginning of one range is missing
    assertTrue(CompactionManager.needsCleanup(sstable, makeRanges(1, 9, 100, 109, 200, 209)));
    assertTrue(CompactionManager.needsCleanup(sstable, makeRanges(0, 9, 101, 109, 200, 209)));
    assertTrue(CompactionManager.needsCleanup(sstable, makeRanges(0, 9, 100, 109, 201, 209)));
    // the end of one range is missing
    assertTrue(CompactionManager.needsCleanup(sstable, makeRanges(0, 8, 100, 109, 200, 209)));
    assertTrue(CompactionManager.needsCleanup(sstable, makeRanges(0, 9, 100, 108, 200, 209)));
    assertTrue(CompactionManager.needsCleanup(sstable, makeRanges(0, 9, 100, 109, 200, 208)));
    // some ranges don't contain any data
    assertFalse(CompactionManager.needsCleanup(sstable, makeRanges(0, 0, 0, 9, 50, 51, 100, 109, 150, 199, 200, 209, 300, 301)));
    // same case, but with a middle range not covering some of the existing data
    assertFalse(CompactionManager.needsCleanup(sstable, makeRanges(0, 0, 0, 9, 50, 51, 100, 103, 150, 199, 200, 209, 300, 301)));
}
Also used : SSTableReader(org.apache.cassandra.io.sstable.format.SSTableReader) Keyspace(org.apache.cassandra.db.Keyspace) ColumnFamilyStore(org.apache.cassandra.db.ColumnFamilyStore) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 12 with Keyspace

use of org.apache.cassandra.db.Keyspace in project cassandra by apache.

the class CommitLogReplayer method blockForWrites.

/**
 * Flushes all keyspaces associated with this replayer in parallel, blocking until their flushes are complete.
 * @return the number of mutations replayed
 */
public int blockForWrites() {
    for (Map.Entry<TableId, AtomicInteger> entry : commitLogReader.getInvalidMutations()) logger.warn("Skipped {} mutations from unknown (probably removed) CF with id {}", entry.getValue(), entry.getKey());
    // wait for all the writes to finish on the mutation stage
    FBUtilities.waitOnFutures(futures);
    logger.trace("Finished waiting on mutations from recovery");
    // flush replayed keyspaces
    futures.clear();
    boolean flushingSystem = false;
    List<Future<?>> futures = new ArrayList<Future<?>>();
    for (Keyspace keyspace : keyspacesReplayed) {
        if (keyspace.getName().equals(SchemaConstants.SYSTEM_KEYSPACE_NAME))
            flushingSystem = true;
        futures.addAll(keyspace.flush());
    }
    // also flush batchlog incase of any MV updates
    if (!flushingSystem)
        futures.add(Keyspace.open(SchemaConstants.SYSTEM_KEYSPACE_NAME).getColumnFamilyStore(SystemKeyspace.BATCHES).forceFlush());
    FBUtilities.waitOnFutures(futures);
    return replayedCount.get();
}
Also used : TableId(org.apache.cassandra.schema.TableId) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) SystemKeyspace(org.apache.cassandra.db.SystemKeyspace) Keyspace(org.apache.cassandra.db.Keyspace) Future(org.apache.cassandra.utils.concurrent.Future)

Example 13 with Keyspace

use of org.apache.cassandra.db.Keyspace in project cassandra by apache.

the class IndexSummaryManager method getRestributionTransactions.

/**
 * Marks the non-compacting sstables as compacting for index summary redistribution for all keyspaces/tables.
 *
 * @return Pair containing:
 *          left: total size of the off heap index summaries for the sstables we were unable to mark compacting (they were involved in other compactions)
 *          right: the transactions, keyed by table id.
 */
@SuppressWarnings("resource")
private Pair<Long, Map<TableId, LifecycleTransaction>> getRestributionTransactions() {
    List<SSTableReader> allCompacting = new ArrayList<>();
    Map<TableId, LifecycleTransaction> allNonCompacting = new HashMap<>();
    for (Keyspace ks : Keyspace.all()) {
        for (ColumnFamilyStore cfStore : ks.getColumnFamilyStores()) {
            Set<SSTableReader> nonCompacting, allSSTables;
            LifecycleTransaction txn;
            do {
                View view = cfStore.getTracker().getView();
                allSSTables = ImmutableSet.copyOf(view.select(SSTableSet.CANONICAL));
                nonCompacting = ImmutableSet.copyOf(view.getUncompacting(allSSTables));
            } while (null == (txn = cfStore.getTracker().tryModify(nonCompacting, OperationType.INDEX_SUMMARY)));
            allNonCompacting.put(cfStore.metadata.id, txn);
            allCompacting.addAll(Sets.difference(allSSTables, nonCompacting));
        }
    }
    long nonRedistributingOffHeapSize = allCompacting.stream().mapToLong(SSTableReader::getIndexSummaryOffHeapSize).sum();
    return Pair.create(nonRedistributingOffHeapSize, allNonCompacting);
}
Also used : TableId(org.apache.cassandra.schema.TableId) SSTableReader(org.apache.cassandra.io.sstable.format.SSTableReader) HashMap(java.util.HashMap) Keyspace(org.apache.cassandra.db.Keyspace) ArrayList(java.util.ArrayList) LifecycleTransaction(org.apache.cassandra.db.lifecycle.LifecycleTransaction) ColumnFamilyStore(org.apache.cassandra.db.ColumnFamilyStore) View(org.apache.cassandra.db.lifecycle.View)

Example 14 with Keyspace

use of org.apache.cassandra.db.Keyspace in project cassandra by apache.

the class StorageProxy method doPaxos.

/**
 * Performs the Paxos rounds for a given proposal, retrying when preempted until the timeout.
 *
 * <p>The main 'configurable' of this method is the {@code createUpdateProposal} method: it is called by the method
 * once a ballot has been successfully 'prepared' to generate the update to 'propose' (and commit if the proposal is
 * successful). That method also generates the result that the whole method will return. Note that due to retrying,
 * this method may be called multiple times and does not have to return the same results.
 *
 * @param metadata the table to update with Paxos.
 * @param key the partition updated.
 * @param consistencyForPaxos the serial consistency of the operation (either {@link ConsistencyLevel#SERIAL} or
 *     {@link ConsistencyLevel#LOCAL_SERIAL}).
 * @param consistencyForReplayCommits the consistency for the commit phase of "replayed" in-progress operations.
 * @param consistencyForCommit the consistency for the commit phase of _this_ operation update.
 * @param queryStartNanoTime the nano time for the start of the query this is part of. This is the base time for
 *     timeouts.
 * @param casMetrics the metrics to update for this operation.
 * @param createUpdateProposal method called after a successful 'prepare' phase to obtain 1) the actual update of
 *     this operation and 2) the result that the whole method should return. This can return {@code null} in the
 *     special where, after having "prepared" (and thus potentially replayed in-progress upgdates), we don't want
 *     to propose anything (the whole method then return {@code null}).
 * @return the second element of the pair returned by {@code createUpdateProposal} (for the last call of that method
 *     if that method is called multiple times due to retries).
 */
private static RowIterator doPaxos(TableMetadata metadata, DecoratedKey key, ConsistencyLevel consistencyForPaxos, ConsistencyLevel consistencyForReplayCommits, ConsistencyLevel consistencyForCommit, long queryStartNanoTime, CASClientRequestMetrics casMetrics, Supplier<Pair<PartitionUpdate, RowIterator>> createUpdateProposal) throws UnavailableException, IsBootstrappingException, RequestFailureException, RequestTimeoutException, InvalidRequestException {
    int contentions = 0;
    Keyspace keyspace = Keyspace.open(metadata.keyspace);
    AbstractReplicationStrategy latestRs = keyspace.getReplicationStrategy();
    try {
        consistencyForPaxos.validateForCas();
        consistencyForReplayCommits.validateForCasCommit(latestRs);
        consistencyForCommit.validateForCasCommit(latestRs);
        long timeoutNanos = DatabaseDescriptor.getCasContentionTimeout(NANOSECONDS);
        while (nanoTime() - queryStartNanoTime < timeoutNanos) {
            // for simplicity, we'll do a single liveness check at the start of each attempt
            ReplicaPlan.ForPaxosWrite replicaPlan = ReplicaPlans.forPaxos(keyspace, key, consistencyForPaxos);
            latestRs = replicaPlan.replicationStrategy();
            PaxosBallotAndContention pair = beginAndRepairPaxos(queryStartNanoTime, key, metadata, replicaPlan, consistencyForPaxos, consistencyForReplayCommits, casMetrics);
            final UUID ballot = pair.ballot;
            contentions += pair.contentions;
            Pair<PartitionUpdate, RowIterator> proposalPair = createUpdateProposal.get();
            // See method javadoc: null here is code for "stop here and return null".
            if (proposalPair == null)
                return null;
            Commit proposal = Commit.newProposal(ballot, proposalPair.left);
            Tracing.trace("CAS precondition is met; proposing client-requested updates for {}", ballot);
            if (proposePaxos(proposal, replicaPlan, true, queryStartNanoTime)) {
                // them), this is worth bothering.
                if (!proposal.update.isEmpty())
                    commitPaxos(proposal, consistencyForCommit, true, queryStartNanoTime);
                RowIterator result = proposalPair.right;
                if (result != null)
                    Tracing.trace("CAS did not apply");
                else
                    Tracing.trace("CAS applied successfully");
                return result;
            }
            Tracing.trace("Paxos proposal not accepted (pre-empted by a higher ballot)");
            contentions++;
            Uninterruptibles.sleepUninterruptibly(ThreadLocalRandom.current().nextInt(100), TimeUnit.MILLISECONDS);
        // continue to retry
        }
    } catch (CasWriteTimeoutException e) {
        // Might be thrown by beginRepairAndPaxos. In that case, any contention that happened within the method and
        // led up to the timeout was not accounted in our local 'contentions' variable and we add it now so it the
        // contention recorded in the finally is correct.
        contentions += e.contentions;
        throw e;
    } catch (WriteTimeoutException e) {
        // Might be thrown by proposePaxos or commitPaxos
        throw new CasWriteTimeoutException(e.writeType, e.consistency, e.received, e.blockFor, contentions);
    } finally {
        recordCasContention(metadata, key, casMetrics, contentions);
    }
    throw new CasWriteTimeoutException(WriteType.CAS, consistencyForPaxos, 0, consistencyForPaxos.blockFor(latestRs), contentions);
}
Also used : ReplicaPlan(org.apache.cassandra.locator.ReplicaPlan) Hint(org.apache.cassandra.hints.Hint) CasWriteTimeoutException(org.apache.cassandra.exceptions.CasWriteTimeoutException) WriteTimeoutException(org.apache.cassandra.exceptions.WriteTimeoutException) Keyspace(org.apache.cassandra.db.Keyspace) RowIterator(org.apache.cassandra.db.rows.RowIterator) AbstractReplicationStrategy(org.apache.cassandra.locator.AbstractReplicationStrategy) UUID(java.util.UUID) CasWriteTimeoutException(org.apache.cassandra.exceptions.CasWriteTimeoutException) PartitionUpdate(org.apache.cassandra.db.partitions.PartitionUpdate)

Example 15 with Keyspace

use of org.apache.cassandra.db.Keyspace in project cassandra by apache.

the class StorageProxy method wrapBatchResponseHandler.

// same as performWrites except does not initiate writes (but does perform availability checks).
private static WriteResponseHandlerWrapper wrapBatchResponseHandler(Mutation mutation, ConsistencyLevel consistencyLevel, ConsistencyLevel batchConsistencyLevel, WriteType writeType, BatchlogResponseHandler.BatchlogCleanup cleanup, long queryStartNanoTime) {
    Keyspace keyspace = Keyspace.open(mutation.getKeyspaceName());
    Token tk = mutation.key().getToken();
    ReplicaPlan.ForTokenWrite replicaPlan = ReplicaPlans.forWrite(keyspace, consistencyLevel, tk, ReplicaPlans.writeNormal);
    AbstractReplicationStrategy rs = replicaPlan.replicationStrategy();
    AbstractWriteResponseHandler<IMutation> writeHandler = rs.getWriteResponseHandler(replicaPlan, null, writeType, queryStartNanoTime);
    BatchlogResponseHandler<IMutation> batchHandler = new BatchlogResponseHandler<>(writeHandler, batchConsistencyLevel.blockFor(rs), cleanup, queryStartNanoTime);
    return new WriteResponseHandlerWrapper(batchHandler, mutation);
}
Also used : ReplicaPlan(org.apache.cassandra.locator.ReplicaPlan) IMutation(org.apache.cassandra.db.IMutation) Keyspace(org.apache.cassandra.db.Keyspace) EndpointsForToken(org.apache.cassandra.locator.EndpointsForToken) Token(org.apache.cassandra.dht.Token) AbstractReplicationStrategy(org.apache.cassandra.locator.AbstractReplicationStrategy)

Aggregations

Keyspace (org.apache.cassandra.db.Keyspace)163 ColumnFamilyStore (org.apache.cassandra.db.ColumnFamilyStore)100 Test (org.junit.Test)73 SSTableReader (org.apache.cassandra.io.sstable.format.SSTableReader)66 RowUpdateBuilder (org.apache.cassandra.db.RowUpdateBuilder)29 Token (org.apache.cassandra.dht.Token)28 LifecycleTransaction (org.apache.cassandra.db.lifecycle.LifecycleTransaction)27 ArrayList (java.util.ArrayList)18 DecoratedKey (org.apache.cassandra.db.DecoratedKey)17 ByteBuffer (java.nio.ByteBuffer)13 SystemKeyspace (org.apache.cassandra.db.SystemKeyspace)13 CompactionController (org.apache.cassandra.db.compaction.CompactionController)13 TableMetadata (org.apache.cassandra.schema.TableMetadata)13 CompactionIterator (org.apache.cassandra.db.compaction.CompactionIterator)12 Range (org.apache.cassandra.dht.Range)12 AbstractReplicationStrategy (org.apache.cassandra.locator.AbstractReplicationStrategy)12 InetAddressAndPort (org.apache.cassandra.locator.InetAddressAndPort)12 IOException (java.io.IOException)11 List (java.util.List)11 Map (java.util.Map)11