|
| 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.db; |
| 20 | + |
| 21 | +import java.net.UnknownHostException; |
| 22 | +import java.util.Collections; |
| 23 | +import java.util.HashMap; |
| 24 | +import java.util.Map; |
| 25 | + |
| 26 | +import com.google.common.collect.HashMultimap; |
| 27 | +import com.google.common.collect.Multimap; |
| 28 | +import org.junit.After; |
| 29 | +import org.junit.AfterClass; |
| 30 | +import org.junit.BeforeClass; |
| 31 | +import org.junit.Test; |
| 32 | + |
| 33 | +import org.apache.cassandra.config.DatabaseDescriptor; |
| 34 | +import org.apache.cassandra.dht.OrderPreservingPartitioner; |
| 35 | +import org.apache.cassandra.dht.Token; |
| 36 | +import org.apache.cassandra.locator.AbstractNetworkTopologySnitch; |
| 37 | +import org.apache.cassandra.locator.AbstractReplicationStrategy; |
| 38 | +import org.apache.cassandra.locator.EverywhereStrategy; |
| 39 | +import org.apache.cassandra.locator.IEndpointSnitch; |
| 40 | +import org.apache.cassandra.locator.InetAddressAndPort; |
| 41 | +import org.apache.cassandra.locator.LocalStrategy; |
| 42 | +import org.apache.cassandra.locator.NetworkTopologyStrategy; |
| 43 | +import org.apache.cassandra.locator.SimpleStrategy; |
| 44 | +import org.apache.cassandra.locator.TokenMetadata; |
| 45 | +import org.apache.cassandra.utils.Pair; |
| 46 | + |
| 47 | +import static org.junit.Assert.*; |
| 48 | + |
| 49 | +public class ConsistencyLevelTest |
| 50 | +{ |
| 51 | + private static final String KS = "test"; |
| 52 | + private static final Map<String, String> RACK = new HashMap<>(), DATACENTER = new HashMap<>(); |
| 53 | + private static final IEndpointSnitch SNITCH = new AbstractNetworkTopologySnitch() |
| 54 | + { |
| 55 | + @Override |
| 56 | + public String getRack(InetAddressAndPort endpoint) |
| 57 | + { |
| 58 | + return RACK.getOrDefault(endpoint.getHostAddress(false), "RC1"); |
| 59 | + } |
| 60 | + |
| 61 | + @Override |
| 62 | + public String getDatacenter(InetAddressAndPort endpoint) |
| 63 | + { |
| 64 | + return DATACENTER.getOrDefault(endpoint.getHostAddress(false), "DC1"); |
| 65 | + } |
| 66 | + }; |
| 67 | + |
| 68 | + @BeforeClass |
| 69 | + public static void setSnitch() |
| 70 | + { |
| 71 | + DatabaseDescriptor.daemonInitialization(); |
| 72 | + DatabaseDescriptor.setEndpointSnitch(SNITCH); |
| 73 | + } |
| 74 | + |
| 75 | + @AfterClass |
| 76 | + public static void resetSnitch() |
| 77 | + { |
| 78 | + DatabaseDescriptor.setEndpointSnitch(null); |
| 79 | + } |
| 80 | + |
| 81 | + @After |
| 82 | + public void resetSnitchState() |
| 83 | + { |
| 84 | + RACK.clear(); |
| 85 | + DATACENTER.clear(); |
| 86 | + } |
| 87 | + |
| 88 | + @Test |
| 89 | + public void allButOne_shouldBe_2_forReplicationFactor_3() |
| 90 | + { |
| 91 | + testAllButOne(simpleStrategy(3), 2); |
| 92 | + } |
| 93 | + |
| 94 | + @Test |
| 95 | + public void allButOne_shouldBe_1_forReplicationFactor_2() |
| 96 | + { |
| 97 | + testAllButOne(simpleStrategy(2), 1); |
| 98 | + } |
| 99 | + |
| 100 | + @Test |
| 101 | + public void allButOne_shouldBe_1_forReplicationFactor_1() |
| 102 | + { |
| 103 | + testAllButOne(simpleStrategy(1), 1); |
| 104 | + } |
| 105 | + |
| 106 | + @Test |
| 107 | + public void allButOne_shouldBe_1_forLocalStrategy() |
| 108 | + { |
| 109 | + testAllButOne(localStrategy(), 1); |
| 110 | + } |
| 111 | + |
| 112 | + @Test |
| 113 | + public void allButOne_shouldBe_8_forReplicationFactor_3_3_3() |
| 114 | + { |
| 115 | + testAllButOne(networkTopologyStrategy(3, 3, 3), 8); |
| 116 | + } |
| 117 | + |
| 118 | + @Test |
| 119 | + public void allButOne_shouldBe_11_forEverywhereStrategyOnClusterOf_12() throws Exception |
| 120 | + { |
| 121 | + testAllButOne(everywhereStrategy( |
| 122 | + dc(1, Pair.create("192.168.0.1", "A"), Pair.create("192.168.0.2", "E"), Pair.create("192.168.0.3", "H"), |
| 123 | + Pair.create("192.168.0.4", "C"), Pair.create("192.168.0.5", "I"), Pair.create("192.168.0.6", "J")), |
| 124 | + dc(2, Pair.create("192.168.1.1", "B"), Pair.create("192.168.1.2", "G"), Pair.create("192.168.1.3", "L"), |
| 125 | + Pair.create("192.168.1.4", "D"), Pair.create("192.168.1.5", "F"), Pair.create("192.168.1.6", "K"))), |
| 126 | + 11); |
| 127 | + } |
| 128 | + |
| 129 | + private void testAllButOne(AbstractReplicationStrategy replicationStrategy, int expected) |
| 130 | + { |
| 131 | + // when |
| 132 | + int blockFor = ConsistencyLevel.allButOneFor(replicationStrategy); |
| 133 | + |
| 134 | + // then |
| 135 | + assertEquals("number of nodes to block for", expected, blockFor); |
| 136 | + } |
| 137 | + |
| 138 | + private static NetworkTopologyStrategy networkTopologyStrategy(int... dc) |
| 139 | + { |
| 140 | + Map<String, String> config = new HashMap<>(); |
| 141 | + for (int i = 0; i < dc.length; i++) |
| 142 | + { |
| 143 | + config.put("DC" + i, Integer.toString(dc[i])); |
| 144 | + } |
| 145 | + return new NetworkTopologyStrategy(KS, new TokenMetadata(), SNITCH, config); |
| 146 | + } |
| 147 | + |
| 148 | + private static AbstractReplicationStrategy simpleStrategy(int replicationFactory) |
| 149 | + { |
| 150 | + Map<String, String> config = Collections.singletonMap("replication_factor", Integer.toString(replicationFactory)); |
| 151 | + return new SimpleStrategy(KS, new TokenMetadata(), SNITCH, config); |
| 152 | + } |
| 153 | + |
| 154 | + @SafeVarargs |
| 155 | + private static AbstractReplicationStrategy everywhereStrategy(Multimap<InetAddressAndPort, Token>... dcs) |
| 156 | + { |
| 157 | + TokenMetadata metadata = new TokenMetadata(); |
| 158 | + for (Multimap<InetAddressAndPort, Token> dc : dcs) |
| 159 | + { |
| 160 | + metadata.updateNormalTokens(dc); |
| 161 | + } |
| 162 | + return new EverywhereStrategy(KS, metadata, SNITCH, Collections.emptyMap()); |
| 163 | + } |
| 164 | + |
| 165 | + private static AbstractReplicationStrategy localStrategy() |
| 166 | + { |
| 167 | + return new LocalStrategy(KS, new TokenMetadata(), SNITCH, Collections.emptyMap()); |
| 168 | + } |
| 169 | + |
| 170 | + private static Multimap<InetAddressAndPort, Token> dc(int id, Pair<String, String>... addressToken) throws UnknownHostException |
| 171 | + { |
| 172 | + Multimap<InetAddressAndPort, Token> dc = HashMultimap.create(); |
| 173 | + for (Pair<String, String> pair : addressToken) |
| 174 | + { |
| 175 | + DATACENTER.put(pair.left, "DC" + id); |
| 176 | + dc.put(InetAddressAndPort.getByName(pair.left), new OrderPreservingPartitioner.StringToken(pair.right)); |
| 177 | + } |
| 178 | + return dc; |
| 179 | + } |
| 180 | +} |
0 commit comments