Search in sources :

Example 6 with Keyspace

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

the class BootStrapper method allocateTokens.

static Collection<Token> allocateTokens(final TokenMetadata metadata, InetAddress address, String allocationKeyspace, int numTokens, int schemaWaitDelay) {
    StorageService.instance.waitForSchema(schemaWaitDelay);
    if (!FBUtilities.getBroadcastAddress().equals(InetAddress.getLoopbackAddress()))
        Gossiper.waitToSettle();
    Keyspace ks = Keyspace.open(allocationKeyspace);
    if (ks == null)
        throw new ConfigurationException("Problem opening token allocation keyspace " + allocationKeyspace);
    AbstractReplicationStrategy rs = ks.getReplicationStrategy();
    return TokenAllocation.allocateTokens(metadata, rs, address, numTokens);
}
Also used : ConfigurationException(org.apache.cassandra.exceptions.ConfigurationException) Keyspace(org.apache.cassandra.db.Keyspace) AbstractReplicationStrategy(org.apache.cassandra.locator.AbstractReplicationStrategy)

Example 7 with Keyspace

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

the class IndexSummaryManager method getCompactingAndNonCompactingSSTables.

/**
     * Returns a Pair of all compacting and non-compacting sstables.  Non-compacting sstables will be marked as
     * compacting.
     */
@SuppressWarnings("resource")
private Pair<List<SSTableReader>, Map<TableId, LifecycleTransaction>> getCompactingAndNonCompactingSSTables() {
    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 = null;
            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.UNKNOWN)));
            allNonCompacting.put(cfStore.metadata.id, txn);
            allCompacting.addAll(Sets.difference(allSSTables, nonCompacting));
        }
    }
    return Pair.create(allCompacting, allNonCompacting);
}
Also used : TableId(org.apache.cassandra.schema.TableId) SSTableReader(org.apache.cassandra.io.sstable.format.SSTableReader) Keyspace(org.apache.cassandra.db.Keyspace) LifecycleTransaction(org.apache.cassandra.db.lifecycle.LifecycleTransaction) ColumnFamilyStore(org.apache.cassandra.db.ColumnFamilyStore) View(org.apache.cassandra.db.lifecycle.View)

Example 8 with Keyspace

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

the class CASTest method consistencyAfterWriteTimeoutTest.

/**
 * Base test to ensure that if a write times out but with a proposal accepted by some nodes (less then quorum), and
 * a following SERIAL operation does not observe that write (the node having accepted it do not participate in that
 * following operation), then that write is never applied, even when the nodes having accepted the original proposal
 * participate.
 *
 * <p>In other words, if an operation timeout, it may or may not be applied, but that "fate" is persistently decided
 * by the very SERIAL operation that "succeed" (in the sense of 'not timing out or throwing some other exception').
 *
 * @param postTimeoutOperation1 a SERIAL operation executed after an initial write that inserts the row [0, 0] times
 *                              out. It is executed with a QUORUM of nodes that have _not_ see the timed out
 *                              proposal, and so that operation should expect that the [0, 0] write has not taken
 *                              place.
 * @param postTimeoutOperation2 a 2nd SERIAL operation executed _after_ {@code postTimeoutOperation1}, with no
 *                              write executed between the 2 operation. Contrarily to the 1st operation, the QORUM
 *                              for this operation _will_ include the node that got the proposal for the [0, 0]
 *                              insert but didn't participated to {@code postTimeoutOperation1}}. That operation
 *                              should also no witness that [0, 0] write (since {@code postTimeoutOperation1}
 *                              didn't).
 * @param loseCommitOfOperation1 if {@code true}, the test will also drop the "commits" messages for
 *                               {@code postTimeoutOperation1}. In general, the test should behave the same with or
 *                               without that flag since a value is decided as soon as it has been "accepted by
 *                               quorum" and the commits should always be properly replayed.
 */
private void consistencyAfterWriteTimeoutTest(BiConsumer<String, ICoordinator> postTimeoutOperation1, BiConsumer<String, ICoordinator> postTimeoutOperation2, boolean loseCommitOfOperation1) throws IOException {
    // not about performance, this is probably ok, even if we ideally should dug into the underlying reason.
    try (Cluster cluster = init(Cluster.create(3, config -> config.set("write_request_timeout", "4000ms").set("cas_contention_timeout", CONTENTION_TIMEOUT)))) {
        String table = KEYSPACE + ".t";
        cluster.schemaChange("CREATE TABLE " + table + " (k int PRIMARY KEY, v int)");
        // We do a CAS insertion, but have with the PROPOSE message dropped on node 1 and 2. The CAS will not get
        // through and should timeout. Importantly, node 3 does receive and answer the PROPOSE.
        IMessageFilters.Filter dropProposeFilter = cluster.filters().inbound().verbs(PAXOS_PROPOSE_REQ.id).from(3).to(1, 2).drop();
        try {
            // NOTE: the consistency below is the "commit" one, so it doesn't matter at all here.
            // NOTE 2: we use node 3 as coordinator because message filters don't currently work for locally
            // delivered messages and as we want to drop messages to 1 and 2, we can't use them.
            cluster.coordinator(3).execute("INSERT INTO " + table + "(k, v) VALUES (0, 0) IF NOT EXISTS", ConsistencyLevel.ONE);
            fail("The insertion should have timed-out");
        } catch (Exception e) {
            // be improved at the dtest API level.
            if (!e.getClass().getSimpleName().equals("CasWriteTimeoutException"))
                throw e;
        } finally {
            dropProposeFilter.off();
        }
        // Isolates node 3 and executes the SERIAL operation. As neither node 1 or 2 got the initial insert proposal,
        // there is nothing to "replay" and the operation should assert the table is still empty.
        IMessageFilters.Filter ignoreNode3Filter = cluster.filters().verbs(paxosAndReadVerbs()).to(3).drop();
        IMessageFilters.Filter dropCommitFilter = null;
        if (loseCommitOfOperation1) {
            dropCommitFilter = cluster.filters().verbs(PAXOS_COMMIT_REQ.id).to(1, 2).drop();
        }
        try {
            postTimeoutOperation1.accept(table, cluster.coordinator(1));
        } finally {
            ignoreNode3Filter.off();
            if (dropCommitFilter != null)
                dropCommitFilter.off();
        }
        // Node 3 is now back and we isolate node 2 to ensure the next read hits node 1 and 3.
        // What we want to ensure is that despite node 3 having the initial insert in its paxos state in a position of
        // being replayed, that insert is _not_ replayed (it would contradict serializability since the previous
        // operation asserted nothing was inserted). It is this execution that failed before CASSANDRA-12126.
        IMessageFilters.Filter ignoreNode2Filter = cluster.filters().verbs(paxosAndReadVerbs()).to(2).drop();
        try {
            postTimeoutOperation2.accept(table, cluster.coordinator(1));
        } finally {
            ignoreNode2Filter.off();
        }
    }
}
Also used : IMessageFilters(org.apache.cassandra.distributed.api.IMessageFilters) TableId(org.apache.cassandra.schema.TableId) IInstance(org.apache.cassandra.distributed.api.IInstance) PAXOS_PREPARE_REQ(org.apache.cassandra.net.Verb.PAXOS_PREPARE_REQ) PAXOS_PROPOSE_REQ(org.apache.cassandra.net.Verb.PAXOS_PROPOSE_REQ) Int32Type(org.apache.cassandra.db.marshal.Int32Type) Token(org.apache.cassandra.dht.Token) UnsafeGossipHelper(org.apache.cassandra.distributed.impl.UnsafeGossipHelper) READ_REQ(org.apache.cassandra.net.Verb.READ_REQ) ICoordinator(org.apache.cassandra.distributed.api.ICoordinator) BiConsumer(java.util.function.BiConsumer) Murmur3Partitioner(org.apache.cassandra.dht.Murmur3Partitioner) AssertUtils.fail(org.apache.cassandra.distributed.shared.AssertUtils.fail) Keyspace(org.apache.cassandra.db.Keyspace) AssertUtils.row(org.apache.cassandra.distributed.shared.AssertUtils.row) StorageService(org.apache.cassandra.service.StorageService) Assert.assertTrue(org.junit.Assert.assertTrue) IOException(java.io.IOException) Test(org.junit.Test) UUID(java.util.UUID) ConsistencyLevel(org.apache.cassandra.distributed.api.ConsistencyLevel) UUIDGen(org.apache.cassandra.utils.UUIDGen) AssertUtils.assertRows(org.apache.cassandra.distributed.shared.AssertUtils.assertRows) Ignore(org.junit.Ignore) Assert.assertFalse(org.junit.Assert.assertFalse) Cluster(org.apache.cassandra.distributed.Cluster) Assert(org.junit.Assert) PAXOS_COMMIT_REQ(org.apache.cassandra.net.Verb.PAXOS_COMMIT_REQ) IMessageFilters(org.apache.cassandra.distributed.api.IMessageFilters) Cluster(org.apache.cassandra.distributed.Cluster) IOException(java.io.IOException)

Example 9 with Keyspace

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

the class CASTest method incompletePropose.

@Test
public void incompletePropose() throws Throwable {
    try (Cluster cluster = init(Cluster.create(3, config -> config.set("write_request_timeout", REQUEST_TIMEOUT).set("cas_contention_timeout", CONTENTION_TIMEOUT)))) {
        cluster.schemaChange("CREATE TABLE " + KEYSPACE + ".tbl (pk int, ck int, v int, PRIMARY KEY (pk, ck))");
        IMessageFilters.Filter drop1 = cluster.filters().verbs(PAXOS_PROPOSE_REQ.id).from(1).to(2, 3).drop();
        try {
            cluster.coordinator(1).execute("INSERT INTO " + KEYSPACE + ".tbl (pk, ck, v) VALUES (1, 1, 1) IF NOT EXISTS", ConsistencyLevel.QUORUM);
            Assert.fail();
        } catch (RuntimeException e) {
            Assert.assertEquals("CAS operation timed out - encountered contentions: 0", e.getMessage());
        }
        drop1.off();
        // make sure we encounter one of the in-progress proposals so we complete it
        cluster.filters().verbs(PAXOS_PREPARE_REQ.id).from(1).to(2).drop();
        cluster.coordinator(1).execute("UPDATE " + KEYSPACE + ".tbl SET v = 2 WHERE pk = 1 and ck = 1 IF v = 1", ConsistencyLevel.QUORUM);
        assertRows(cluster.coordinator(1).execute("SELECT * FROM " + KEYSPACE + ".tbl WHERE pk = 1", ConsistencyLevel.SERIAL), row(1, 1, 2));
    }
}
Also used : IMessageFilters(org.apache.cassandra.distributed.api.IMessageFilters) TableId(org.apache.cassandra.schema.TableId) IInstance(org.apache.cassandra.distributed.api.IInstance) PAXOS_PREPARE_REQ(org.apache.cassandra.net.Verb.PAXOS_PREPARE_REQ) PAXOS_PROPOSE_REQ(org.apache.cassandra.net.Verb.PAXOS_PROPOSE_REQ) Int32Type(org.apache.cassandra.db.marshal.Int32Type) Token(org.apache.cassandra.dht.Token) UnsafeGossipHelper(org.apache.cassandra.distributed.impl.UnsafeGossipHelper) READ_REQ(org.apache.cassandra.net.Verb.READ_REQ) ICoordinator(org.apache.cassandra.distributed.api.ICoordinator) BiConsumer(java.util.function.BiConsumer) Murmur3Partitioner(org.apache.cassandra.dht.Murmur3Partitioner) AssertUtils.fail(org.apache.cassandra.distributed.shared.AssertUtils.fail) Keyspace(org.apache.cassandra.db.Keyspace) AssertUtils.row(org.apache.cassandra.distributed.shared.AssertUtils.row) StorageService(org.apache.cassandra.service.StorageService) Assert.assertTrue(org.junit.Assert.assertTrue) IOException(java.io.IOException) Test(org.junit.Test) UUID(java.util.UUID) ConsistencyLevel(org.apache.cassandra.distributed.api.ConsistencyLevel) UUIDGen(org.apache.cassandra.utils.UUIDGen) AssertUtils.assertRows(org.apache.cassandra.distributed.shared.AssertUtils.assertRows) Ignore(org.junit.Ignore) Assert.assertFalse(org.junit.Assert.assertFalse) Cluster(org.apache.cassandra.distributed.Cluster) Assert(org.junit.Assert) PAXOS_COMMIT_REQ(org.apache.cassandra.net.Verb.PAXOS_COMMIT_REQ) IMessageFilters(org.apache.cassandra.distributed.api.IMessageFilters) Cluster(org.apache.cassandra.distributed.Cluster) Test(org.junit.Test)

Example 10 with Keyspace

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

the class CASTest method incompletePrepare.

@Test
public void incompletePrepare() throws Throwable {
    try (Cluster cluster = init(Cluster.create(3, config -> config.set("write_request_timeout", REQUEST_TIMEOUT).set("cas_contention_timeout", CONTENTION_TIMEOUT)))) {
        cluster.schemaChange("CREATE TABLE " + KEYSPACE + ".tbl (pk int, ck int, v int, PRIMARY KEY (pk, ck))");
        IMessageFilters.Filter drop = cluster.filters().verbs(PAXOS_PREPARE_REQ.id).from(1).to(2, 3).drop();
        try {
            cluster.coordinator(1).execute("INSERT INTO " + KEYSPACE + ".tbl (pk, ck, v) VALUES (1, 1, 1) IF NOT EXISTS", ConsistencyLevel.QUORUM);
            Assert.fail();
        } catch (RuntimeException e) {
            Assert.assertEquals("CAS operation timed out - encountered contentions: 0", e.getMessage());
        }
        drop.off();
        cluster.coordinator(1).execute("UPDATE " + KEYSPACE + ".tbl SET v = 2 WHERE pk = 1 and ck = 1 IF v = 1", ConsistencyLevel.QUORUM);
        assertRows(cluster.coordinator(1).execute("SELECT * FROM " + KEYSPACE + ".tbl WHERE pk = 1", ConsistencyLevel.SERIAL));
    }
}
Also used : IMessageFilters(org.apache.cassandra.distributed.api.IMessageFilters) TableId(org.apache.cassandra.schema.TableId) IInstance(org.apache.cassandra.distributed.api.IInstance) PAXOS_PREPARE_REQ(org.apache.cassandra.net.Verb.PAXOS_PREPARE_REQ) PAXOS_PROPOSE_REQ(org.apache.cassandra.net.Verb.PAXOS_PROPOSE_REQ) Int32Type(org.apache.cassandra.db.marshal.Int32Type) Token(org.apache.cassandra.dht.Token) UnsafeGossipHelper(org.apache.cassandra.distributed.impl.UnsafeGossipHelper) READ_REQ(org.apache.cassandra.net.Verb.READ_REQ) ICoordinator(org.apache.cassandra.distributed.api.ICoordinator) BiConsumer(java.util.function.BiConsumer) Murmur3Partitioner(org.apache.cassandra.dht.Murmur3Partitioner) AssertUtils.fail(org.apache.cassandra.distributed.shared.AssertUtils.fail) Keyspace(org.apache.cassandra.db.Keyspace) AssertUtils.row(org.apache.cassandra.distributed.shared.AssertUtils.row) StorageService(org.apache.cassandra.service.StorageService) Assert.assertTrue(org.junit.Assert.assertTrue) IOException(java.io.IOException) Test(org.junit.Test) UUID(java.util.UUID) ConsistencyLevel(org.apache.cassandra.distributed.api.ConsistencyLevel) UUIDGen(org.apache.cassandra.utils.UUIDGen) AssertUtils.assertRows(org.apache.cassandra.distributed.shared.AssertUtils.assertRows) Ignore(org.junit.Ignore) Assert.assertFalse(org.junit.Assert.assertFalse) Cluster(org.apache.cassandra.distributed.Cluster) Assert(org.junit.Assert) PAXOS_COMMIT_REQ(org.apache.cassandra.net.Verb.PAXOS_COMMIT_REQ) IMessageFilters(org.apache.cassandra.distributed.api.IMessageFilters) Cluster(org.apache.cassandra.distributed.Cluster) Test(org.junit.Test)

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