|
| 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; |
| 20 | + |
| 21 | +import java.util.List; |
| 22 | +import java.util.Map; |
| 23 | + |
| 24 | +import com.google.common.collect.ImmutableList; |
| 25 | +import com.google.common.collect.ImmutableMap; |
| 26 | +import org.junit.Assert; |
| 27 | +import org.junit.Test; |
| 28 | + |
| 29 | +import org.apache.cassandra.distributed.Cluster; |
| 30 | +import org.apache.cassandra.distributed.api.ConsistencyLevel; |
| 31 | +import org.apache.cassandra.distributed.api.ICluster; |
| 32 | +import org.apache.cassandra.distributed.api.IInstanceConfig; |
| 33 | +import org.apache.cassandra.distributed.api.IInvokableInstance; |
| 34 | +import org.apache.cassandra.distributed.api.TokenSupplier; |
| 35 | +import org.apache.cassandra.distributed.shared.NetworkTopology; |
| 36 | +import org.apache.cassandra.distributed.shared.WithProperties; |
| 37 | +import org.apache.cassandra.repair.consistent.ConsistentSession; |
| 38 | +import org.apache.cassandra.service.ActiveRepairService; |
| 39 | +import org.apache.cassandra.service.StorageService; |
| 40 | +import org.apache.cassandra.utils.concurrent.Condition; |
| 41 | +import org.apache.cassandra.utils.progress.ProgressEventType; |
| 42 | + |
| 43 | +import static java.util.concurrent.TimeUnit.MILLISECONDS; |
| 44 | +import static java.util.concurrent.TimeUnit.MINUTES; |
| 45 | +import static java.util.concurrent.TimeUnit.SECONDS; |
| 46 | +import static org.apache.cassandra.distributed.api.Feature.GOSSIP; |
| 47 | +import static org.apache.cassandra.distributed.api.Feature.NATIVE_PROTOCOL; |
| 48 | +import static org.apache.cassandra.distributed.api.Feature.NETWORK; |
| 49 | +import static org.apache.cassandra.distributed.test.ExecUtil.rethrow; |
| 50 | +import static org.apache.cassandra.repair.consistent.LocalSessionInfo.STATE; |
| 51 | +import static org.apache.cassandra.repair.messages.RepairOption.INCREMENTAL_KEY; |
| 52 | +import static org.awaitility.Awaitility.await; |
| 53 | +import static org.apache.cassandra.utils.concurrent.Condition.newOneTimeCondition; |
| 54 | +import static org.hamcrest.collection.IsCollectionWithSize.hasSize; |
| 55 | + |
| 56 | +public class IncrementalRepairCleanupAfterNodeAddingTest extends TestBaseImpl |
| 57 | +{ |
| 58 | + @Test |
| 59 | + public void test() throws Exception |
| 60 | + { |
| 61 | + int originalNodeCount = 3; |
| 62 | + try (WithProperties withProperties = new WithProperties()) |
| 63 | + { |
| 64 | + withProperties.setProperty("cassandra.repair_delete_timeout_seconds", "0"); |
| 65 | + try (Cluster cluster = builder().withNodes(originalNodeCount) |
| 66 | + .withTokenSupplier(TokenSupplier.evenlyDistributedTokens(originalNodeCount + 1, 1)) |
| 67 | + .withNodeIdTopology(NetworkTopology.singleDcNetworkTopology(4, "dc0", "rack0")) |
| 68 | + .withConfig(config -> config.with(NETWORK, GOSSIP, NATIVE_PROTOCOL)) |
| 69 | + .start()) |
| 70 | + { |
| 71 | + populate(cluster, 0, 100); |
| 72 | + |
| 73 | + repair(cluster, KEYSPACE, ImmutableMap.of(INCREMENTAL_KEY, "true")); |
| 74 | + |
| 75 | + Thread.sleep(1); // to ensure that we crossed LocalSessions.AUTO_DELETE_TIMEOUT |
| 76 | + |
| 77 | + // to check that the session is still here (it is not superseded yet) |
| 78 | + cluster.get(1).runOnInstance(rethrow(() -> { |
| 79 | + ActiveRepairService.instance.consistent.local.cleanup(); |
| 80 | + List<Map<String, String>> sessions = ActiveRepairService.instance.getSessions(true, null); |
| 81 | + Assert.assertThat(sessions, hasSize(1)); |
| 82 | + })); |
| 83 | + |
| 84 | + addNode(cluster); |
| 85 | + |
| 86 | + repair(cluster, KEYSPACE, ImmutableMap.of(INCREMENTAL_KEY, "true")); |
| 87 | + |
| 88 | + Thread.sleep(1); // to ensure that we crossed LocalSessions.AUTO_DELETE_TIMEOUT |
| 89 | + |
| 90 | + cluster.get(1).runOnInstance(rethrow(() -> { |
| 91 | + ActiveRepairService.instance.consistent.local.cleanup(); |
| 92 | + List<Map<String, String>> sessions = ActiveRepairService.instance.getSessions(true, null); |
| 93 | + Assert.assertThat(sessions, hasSize(1)); |
| 94 | + })); |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + protected void addNode(Cluster cluster) |
| 100 | + { |
| 101 | + IInstanceConfig config = cluster.newInstanceConfig(); |
| 102 | + config.set("auto_bootstrap", true); |
| 103 | + IInvokableInstance newInstance = cluster.bootstrap(config); |
| 104 | + newInstance.startup(cluster); |
| 105 | + } |
| 106 | + |
| 107 | + public static void populate(ICluster cluster, int from, int to) |
| 108 | + { |
| 109 | + populate(cluster, from, to, 1, 3, ConsistencyLevel.QUORUM); |
| 110 | + } |
| 111 | + |
| 112 | + public static void populate(ICluster cluster, int from, int to, int coord, int rf, ConsistencyLevel cl) |
| 113 | + { |
| 114 | + cluster.schemaChange(withKeyspace("CREATE KEYSPACE IF NOT EXISTS %s WITH replication = {'class': 'SimpleStrategy', 'replication_factor': " + rf + "};")); |
| 115 | + cluster.schemaChange(withKeyspace("CREATE TABLE IF NOT EXISTS %s.repair_add_node_test (pk int, ck int, v int, PRIMARY KEY (pk, ck))")); |
| 116 | + for (int i = from; i < to; i++) |
| 117 | + { |
| 118 | + cluster.coordinator(coord).execute(withKeyspace("INSERT INTO %s.repair_add_node_test (pk, ck, v) VALUES (?, ?, ?)"), |
| 119 | + cl, i, i, i); |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + static void repair(ICluster<IInvokableInstance> cluster, String keyspace, Map<String, String> options) |
| 124 | + { |
| 125 | + cluster.get(1).runOnInstance(rethrow(() -> { |
| 126 | + Condition await = newOneTimeCondition(); |
| 127 | + StorageService.instance.repair(keyspace, options, ImmutableList.of((tag, event) -> { |
| 128 | + if (event.getType() == ProgressEventType.COMPLETE) |
| 129 | + await.signalAll(); |
| 130 | + })).right.get(); |
| 131 | + await.await(1L, MINUTES); |
| 132 | + |
| 133 | + // local sessions finalization happens asynchronously |
| 134 | + // so to avoid race condition and flakiness for the test we wait explicitly for local sessions to finalize |
| 135 | + await().pollInterval(10, MILLISECONDS) |
| 136 | + .atMost(60, SECONDS) |
| 137 | + .until(() -> { |
| 138 | + List<Map<String, String>> sessions = ActiveRepairService.instance.getSessions(true, null); |
| 139 | + for (Map<String, String> sessionInfo : sessions) |
| 140 | + if (!sessionInfo.get(STATE).equals(ConsistentSession.State.FINALIZED.toString())) |
| 141 | + return false; |
| 142 | + return true; |
| 143 | + }); |
| 144 | + })); |
| 145 | + } |
| 146 | +} |
0 commit comments