|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +package org.apache.cassandra.distributed.test.tcm; |
| 20 | + |
| 21 | +import java.io.IOException; |
| 22 | +import java.util.concurrent.Callable; |
| 23 | +import java.util.concurrent.TimeUnit; |
| 24 | +import java.util.concurrent.atomic.AtomicBoolean; |
| 25 | +import java.util.concurrent.atomic.AtomicInteger; |
| 26 | + |
| 27 | +import com.google.common.util.concurrent.Uninterruptibles; |
| 28 | + |
| 29 | +import net.bytebuddy.ByteBuddy; |
| 30 | +import net.bytebuddy.dynamic.loading.ClassLoadingStrategy; |
| 31 | +import net.bytebuddy.implementation.MethodDelegation; |
| 32 | +import net.bytebuddy.implementation.bind.annotation.SuperCall; |
| 33 | + |
| 34 | +import org.awaitility.Awaitility; |
| 35 | +import org.junit.Test; |
| 36 | + |
| 37 | +import org.apache.cassandra.db.DecoratedKey; |
| 38 | +import org.apache.cassandra.db.ReadCommand; |
| 39 | +import org.apache.cassandra.db.ReadExecutionController; |
| 40 | +import org.apache.cassandra.db.rows.UnfilteredRowIterator; |
| 41 | +import org.apache.cassandra.distributed.Cluster; |
| 42 | +import org.apache.cassandra.distributed.api.ConsistencyLevel; |
| 43 | +import org.apache.cassandra.distributed.shared.ClusterUtils; |
| 44 | +import org.apache.cassandra.distributed.test.TestBaseImpl; |
| 45 | +import org.apache.cassandra.index.internal.CassandraIndexSearcher; |
| 46 | +import org.apache.cassandra.net.Verb; |
| 47 | +import org.apache.cassandra.tcm.Epoch; |
| 48 | + |
| 49 | +import static net.bytebuddy.matcher.ElementMatchers.named; |
| 50 | +import static org.apache.cassandra.distributed.api.Feature.GOSSIP; |
| 51 | +import static org.apache.cassandra.distributed.api.Feature.NETWORK; |
| 52 | +import static org.junit.Assert.assertEquals; |
| 53 | + |
| 54 | +public class IndexTablePartitionerAfterForceSnapshotTest extends TestBaseImpl |
| 55 | +{ |
| 56 | + @Test |
| 57 | + public void indexQueryAfterSnapshotTest() throws IOException, InterruptedException |
| 58 | + { |
| 59 | + try (Cluster cluster = Cluster.build(3) |
| 60 | + .withInstanceInitializer(BBInstaller::install) |
| 61 | + .withConfig(config -> config.with(GOSSIP, NETWORK)) |
| 62 | + .start()) |
| 63 | + { |
| 64 | + init(cluster); |
| 65 | + cluster.schemaChange("CREATE TABLE " + KEYSPACE + ".tbl (pk int, ck int, v int, PRIMARY KEY (pk, ck))"); |
| 66 | + cluster.schemaChange(withKeyspace("create index iii2 on %s.tbl (v)")); |
| 67 | + for (int i = 0; i < 10000; i++) |
| 68 | + cluster.coordinator(1).execute(withKeyspace("insert into %s.tbl (pk, ck, v) values (?, ?, ?)"), ConsistencyLevel.ALL, 1, i, i % 5); |
| 69 | + |
| 70 | + // drop metadata change replication messages from node 1 to nodes 2 & 3 |
| 71 | + cluster.filters().verbs(Verb.TCM_REPLICATION.id, |
| 72 | + Verb.TCM_FETCH_CMS_LOG_RSP.id, |
| 73 | + Verb.TCM_FETCH_PEER_LOG_RSP.id, |
| 74 | + Verb.TCM_CURRENT_EPOCH_REQ.id) |
| 75 | + .from(1) |
| 76 | + .drop() |
| 77 | + .on(); |
| 78 | + |
| 79 | + // node1 makes some metadata changes interspersed with snapshots. When the message filters are dropped, this |
| 80 | + // will cause nodes 2 & 3 to try and catchup. We want multiple snapshots to be taken to ensure that the |
| 81 | + // catchup responses contain one, rather than just a list of entries. |
| 82 | + cluster.get(1).nodetoolResult("cms", "snapshot").asserts().success(); |
| 83 | + cluster.get(1).nodetoolResult("cms", "snapshot").asserts().success(); |
| 84 | + final Epoch epoch = ClusterUtils.getCurrentEpoch(cluster.get(1)); |
| 85 | + // start executing |
| 86 | + AtomicBoolean stop = new AtomicBoolean(); |
| 87 | + AtomicInteger queryFailures = new AtomicInteger(); |
| 88 | + Thread t = new Thread(() -> { |
| 89 | + while (!stop.get()) |
| 90 | + { |
| 91 | + try |
| 92 | + { |
| 93 | + cluster.coordinator(1).execute(withKeyspace("select * from %s.tbl where pk=1 and v=4"), ConsistencyLevel.ALL); |
| 94 | + } |
| 95 | + catch (Throwable e) |
| 96 | + { |
| 97 | + queryFailures.incrementAndGet(); |
| 98 | + } |
| 99 | + } |
| 100 | + }); |
| 101 | + t.start(); |
| 102 | + TimeUnit.SECONDS.sleep(1); |
| 103 | + // drop the filters and wait for nodes 2 & 3 to catch up |
| 104 | + cluster.filters().reset(); |
| 105 | + Awaitility.waitAtMost(30, TimeUnit.SECONDS) |
| 106 | + .pollInterval(1, TimeUnit.SECONDS) |
| 107 | + .until(() -> ClusterUtils.getCurrentEpoch(cluster.get(2)).isEqualOrAfter(epoch) && |
| 108 | + ClusterUtils.getCurrentEpoch(cluster.get(3)).isEqualOrAfter(epoch)); |
| 109 | + |
| 110 | + // give it another second of querying |
| 111 | + TimeUnit.SECONDS.sleep(1); |
| 112 | + stop.set(true); |
| 113 | + t.join(); |
| 114 | + assertEquals(0, queryFailures.get()); |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + public static class BBInstaller |
| 119 | + { |
| 120 | + public static void install(ClassLoader classLoader, int inst) |
| 121 | + { |
| 122 | + if (inst == 1) |
| 123 | + return; |
| 124 | + new ByteBuddy().rebase(CassandraIndexSearcher.class) |
| 125 | + .method(named("queryIndex")) |
| 126 | + .intercept(MethodDelegation.to(BBInterceptor.class)) |
| 127 | + .make() |
| 128 | + .load(classLoader, ClassLoadingStrategy.Default.INJECTION); |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + public static class BBInterceptor |
| 133 | + { |
| 134 | + public static UnfilteredRowIterator queryIndex(DecoratedKey indexKey, ReadCommand command, ReadExecutionController executionController, @SuperCall Callable<UnfilteredRowIterator> zuper) throws Exception |
| 135 | + { |
| 136 | + // this makes it more likely that we decorate the key with one partitioner and execute the SinglePartitionReadCommand with another |
| 137 | + Uninterruptibles.sleepUninterruptibly(100, TimeUnit.MILLISECONDS); |
| 138 | + return zuper.call(); |
| 139 | + } |
| 140 | + } |
| 141 | +} |
0 commit comments