| /* |
| * Licensed to the Apache Software Foundation (ASF) under one |
| * or more contributor license agreements. See the NOTICE file |
| * distributed with this work for additional information |
| * regarding copyright ownership. The ASF licenses this file |
| * to you under the Apache License, Version 2.0 (the |
| * "License"); you may not use this file except in compliance |
| * with the License. You may obtain a copy of the License at |
| * |
| * http://www.apache.org/licenses/LICENSE-2.0 |
| * |
| * Unless required by applicable law or agreed to in writing, software |
| * distributed under the License is distributed on an "AS IS" BASIS, |
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| * See the License for the specific language governing permissions and |
| * limitations under the License. |
| */ |
| |
| package org.apache.cassandra.sidecar.routes.tokenrange; |
| |
| import java.math.BigInteger; |
| import java.util.Arrays; |
| import java.util.Collection; |
| import java.util.HashMap; |
| import java.util.List; |
| import java.util.Map; |
| import java.util.concurrent.Callable; |
| import java.util.concurrent.CountDownLatch; |
| |
| import com.google.common.collect.Range; |
| import com.google.common.util.concurrent.Uninterruptibles; |
| import org.junit.jupiter.api.extension.ExtendWith; |
| |
| import io.vertx.junit5.VertxExtension; |
| import io.vertx.junit5.VertxTestContext; |
| import net.bytebuddy.ByteBuddy; |
| import net.bytebuddy.description.type.TypeDescription; |
| import net.bytebuddy.dynamic.ClassFileLocator; |
| import net.bytebuddy.dynamic.TypeResolutionStrategy; |
| import net.bytebuddy.dynamic.loading.ClassLoadingStrategy; |
| import net.bytebuddy.implementation.MethodDelegation; |
| import net.bytebuddy.implementation.bind.annotation.SuperCall; |
| import net.bytebuddy.pool.TypePool; |
| import org.apache.cassandra.distributed.UpgradeableCluster; |
| import org.apache.cassandra.testing.CassandraIntegrationTest; |
| import org.apache.cassandra.testing.ConfigurableCassandraTestContext; |
| import org.apache.cassandra.utils.Shared; |
| |
| import static net.bytebuddy.matcher.ElementMatchers.named; |
| import static net.bytebuddy.matcher.ElementMatchers.takesArguments; |
| |
| /** |
| * Multi-DC Cluster expansion scenarios integration tests for token range replica mapping endpoint with the in-jvm |
| * dtest framework. |
| * |
| * Note: Some related test classes are broken down to have a single test case to parallelize test execution and |
| * therefore limit the instance size required to run the tests from CircleCI as the in-jvm-dtests tests are memory bound |
| */ |
| @ExtendWith(VertxExtension.class) |
| public class JoiningTestMultiDCSingleReplicated extends JoiningBaseTest |
| { |
| @CassandraIntegrationTest( |
| nodesPerDc = 5, newNodesPerDc = 1, numDcs = 2, network = true, gossip = true, buildCluster = false) |
| void retrieveMappingsSingleDCReplicatedKeyspace(VertxTestContext context, |
| ConfigurableCassandraTestContext cassandraTestContext) |
| throws Exception |
| { |
| BBHelperMultiDC.reset(); |
| UpgradeableCluster cluster = getMultiDCCluster(BBHelperMultiDC::install, cassandraTestContext); |
| |
| runJoiningTestScenario(context, |
| BBHelperMultiDC.transientStateStart, |
| BBHelperMultiDC.transientStateEnd, |
| cluster, |
| generateExpectedRanges(false), |
| generateExpectedRangeMappingOneof2DCs(), |
| false); |
| } |
| |
| /** |
| * Generates expected token range and replica mappings specific to the test case involving a 5 node cluster |
| * with the additional node joining the cluster |
| * <p> |
| * Expected ranges are generated by adding RF replicas per range in increasing order. The replica-sets in subsequent |
| * ranges cascade with the next range excluding the first replica, and including the next replica from the nodes. |
| * eg. |
| * Range 1 - A, B, C |
| * Range 2 - B, C, D |
| * <p> |
| * Ranges that include the joining node will have [RF + no. joining nodes in replica-set] replicas with |
| * the replicas being the existing nodes in ring-order. |
| * eg. |
| * Range 1 - A, B, C |
| * Range 2 - B, C, D (with E being the joining node) |
| * Expected Range 2 - B, C, D, E |
| */ |
| private HashMap<String, Map<Range<BigInteger>, List<String>>> generateExpectedRangeMappingOneof2DCs() |
| { |
| |
| /* |
| * Initial Ranges: |
| * [-9223372036854775808, -5869418568907584607]=[127.0.0.1:52914, 127.0.0.3:52916, 127.0.0.5:52918] |
| * [-5869418568907584607, -5869418568907584606]=[127.0.0.3:52916, 127.0.0.5:52918, 127.0.0.7:52920] |
| * [-5869418568907584606, -2515465100960393407]=[127.0.0.3:52918, 127.0.0.5:52920, 127.0.0.7:52922] |
| * [-2515465100960393407, -2515465100960393406]=[127.0.0.5:52918, 127.0.0.7:52920, 127.0.0.9:52922] |
| * [-2515465100960393406, 838488366986797793]=[127.0.0.5:52918, 127.0.0.7:52920, 127.0.0.9:52922] |
| * [838488366986797793, 838488366986797794]=[127.0.0.7:52920, 127.0.0.9:52922, 127.0.0.1:52914] |
| * [838488366986797794, 4192441834933988993]=[127.0.0.7:52920, 127.0.0.9:52922, 127.0.0.1:52914] |
| * [4192441834933988993, 4192441834933988994]=[127.0.0.9:52922, 127.0.0.1:52914, 127.0.0.3:52916] |
| * [4192441834933988994, 7546395302881180193]=[127.0.0.9:52922, 127.0.0.1:52914, 127.0.0.3:52916] |
| * [7546395302881180193, 7546395302881180194]=[127.0.0.1:52914, 127.0.0.3:52916, 127.0.0.5:52918] |
| * [7546395302881180194, 9223372036854775807]=[127.0.0.1:52914, 127.0.0.3:52916, 127.0.0.5:52918] |
| * |
| * Pending Ranges: |
| * [-5869418568907584607, -5869418568907584606]=[127.0.0.11:58400] |
| * [-5869418568907584606, -4192441834933989006]=[127.0.0.11:58400] |
| * [4192441834933988993, 4192441834933988994]=[127.0.0.11:58400] |
| * [7546395302881180194, -5869418568907584607]=[127.0.0.11:58400] (wrap-around) |
| * [7546395302881180193, 7546395302881180194]=[127.0.0.11:58400] |
| * [4192441834933988994, 7546395302881180193]=[127.0.0.11:58400] |
| * |
| */ |
| List<Range<BigInteger>> expectedRanges = generateExpectedRanges(false); |
| Map<Range<BigInteger>, List<String>> mapping = new HashMap<>(); |
| // [-9223372036854775808, -5869418568907584607] |
| mapping.put(expectedRanges.get(0), Arrays.asList("127.0.0.1", "127.0.0.3", "127.0.0.5", "127.0.0.11")); |
| // [-5869418568907584607, -5869418568907584606] |
| mapping.put(expectedRanges.get(1), Arrays.asList("127.0.0.3", "127.0.0.5", "127.0.0.7", "127.0.0.11")); |
| // [-5869418568907584606, -4192441834933989006] |
| mapping.put(expectedRanges.get(2), Arrays.asList("127.0.0.3", "127.0.0.5", "127.0.0.7", "127.0.0.11")); |
| // [-4192441834933989006, -2515465100960393407] |
| mapping.put(expectedRanges.get(3), Arrays.asList("127.0.0.3", "127.0.0.5", "127.0.0.7")); |
| // [-2515465100960393407, -2515465100960393406] |
| mapping.put(expectedRanges.get(4), Arrays.asList("127.0.0.5", "127.0.0.7", "127.0.0.9")); |
| // [-2515465100960393406, 838488366986797793] |
| mapping.put(expectedRanges.get(5), Arrays.asList("127.0.0.5", "127.0.0.7", "127.0.0.9")); |
| // [838488366986797793, 838488366986797794] |
| mapping.put(expectedRanges.get(6), Arrays.asList("127.0.0.7", "127.0.0.9", "127.0.0.1")); |
| // [838488366986797794, 4192441834933988993] |
| mapping.put(expectedRanges.get(7), Arrays.asList("127.0.0.7", "127.0.0.9", "127.0.0.1")); |
| // [4192441834933988993, 4192441834933988994] |
| mapping.put(expectedRanges.get(8), Arrays.asList("127.0.0.9", "127.0.0.1", "127.0.0.3", "127.0.0.11")); |
| // [4192441834933988994, 7546395302881180193] |
| mapping.put(expectedRanges.get(9), Arrays.asList("127.0.0.9", "127.0.0.1", "127.0.0.3", "127.0.0.11")); |
| // [7546395302881180193, 7546395302881180194] |
| mapping.put(expectedRanges.get(10), Arrays.asList("127.0.0.1", "127.0.0.3", "127.0.0.5", "127.0.0.11")); |
| // [7546395302881180194, 9223372036854775807] |
| mapping.put(expectedRanges.get(11), Arrays.asList("127.0.0.1", "127.0.0.3", "127.0.0.5", "127.0.0.11")); |
| |
| return new HashMap<String, Map<Range<BigInteger>, List<String>>>() |
| { |
| { |
| put("datacenter1", mapping); |
| } |
| }; |
| } |
| |
| /** |
| * ByteBuddy helper for multiple joining nodes |
| */ |
| @Shared |
| public static class BBHelperMultiDC |
| { |
| static CountDownLatch transientStateStart = new CountDownLatch(2); |
| static CountDownLatch transientStateEnd = new CountDownLatch(2); |
| |
| public static void install(ClassLoader cl, Integer nodeNumber) |
| { |
| // Test case involves adding 2 nodes to a 10 node cluster (5 per DC) |
| // We intercept the bootstrap of nodes (11,12) to validate token ranges |
| if (nodeNumber > 10) |
| { |
| TypePool typePool = TypePool.Default.of(cl); |
| TypeDescription description = typePool.describe("org.apache.cassandra.service.StorageService") |
| .resolve(); |
| new ByteBuddy().rebase(description, ClassFileLocator.ForClassLoader.of(cl)) |
| .method(named("bootstrap").and(takesArguments(2))) |
| .intercept(MethodDelegation.to(BBHelperMultiDC.class)) |
| // Defer class loading until all dependencies are loaded |
| .make(TypeResolutionStrategy.Lazy.INSTANCE, typePool) |
| .load(cl, ClassLoadingStrategy.Default.INJECTION); |
| } |
| } |
| |
| public static boolean bootstrap(Collection<?> tokens, |
| long bootstrapTimeoutMillis, |
| @SuperCall Callable<Boolean> orig) throws Exception |
| { |
| boolean result = orig.call(); |
| // trigger bootstrap start and wait until bootstrap is ready from test |
| transientStateStart.countDown(); |
| Uninterruptibles.awaitUninterruptibly(transientStateEnd); |
| return result; |
| } |
| |
| public static void reset() |
| { |
| transientStateStart = new CountDownLatch(2); |
| transientStateEnd = new CountDownLatch(2); |
| } |
| } |
| } |