Search in sources :

Example 1 with HashMultimap

use of com.google.common.collect.HashMultimap in project buck by facebook.

the class JavaDepsFinder method findDepsForBuildFiles.

private DepsForBuildFiles findDepsForBuildFiles(final TargetGraph graph, final DependencyInfo dependencyInfo, final Console console) {
    // For the rules that expect to have their deps generated, look through all of their required
    // symbols and try to find the build rule that provides each symbols. Store these build rules in
    // the depsForBuildFiles data structure.
    //
    // Currently, we process each rule with autodeps=True on a single thread. See the class overview
    // for DepsForBuildFiles about what it would take to do this work in a multi-threaded way.
    DepsForBuildFiles depsForBuildFiles = new DepsForBuildFiles();
    for (final TargetNode<?, ?> rule : dependencyInfo.rulesWithAutodeps) {
        final Set<BuildTarget> providedDeps = dependencyInfo.rulesWithAutodepsToProvidedDeps.get(rule);
        final Predicate<TargetNode<?, ?>> isVisibleDepNotAlreadyInProvidedDeps = provider -> provider.isVisibleTo(graph, rule) && !providedDeps.contains(provider.getBuildTarget());
        final boolean isJavaTestRule = rule.getDescription() instanceof JavaTestDescription;
        for (DependencyType type : DependencyType.values()) {
            HashMultimap<TargetNode<?, ?>, String> ruleToSymbolsMap;
            switch(type) {
                case DEPS:
                    ruleToSymbolsMap = dependencyInfo.ruleToRequiredSymbols;
                    break;
                case EXPORTED_DEPS:
                    ruleToSymbolsMap = dependencyInfo.ruleToExportedSymbols;
                    break;
                default:
                    throw new IllegalStateException("Unrecognized type: " + type);
            }
            final DependencyType typeOfDepToAdd;
            if (isJavaTestRule) {
                // java_test rules do not honor exported_deps: add all dependencies to the ordinary deps.
                typeOfDepToAdd = DependencyType.DEPS;
            } else {
                typeOfDepToAdd = type;
            }
            for (String requiredSymbol : ruleToSymbolsMap.get(rule)) {
                BuildTarget provider = findProviderForSymbolFromBuckConfig(requiredSymbol);
                if (provider != null) {
                    depsForBuildFiles.addDep(rule.getBuildTarget(), provider, typeOfDepToAdd);
                    continue;
                }
                Set<TargetNode<?, ?>> providers = dependencyInfo.symbolToProviders.get(requiredSymbol);
                SortedSet<TargetNode<?, ?>> candidateProviders = providers.stream().filter(isVisibleDepNotAlreadyInProvidedDeps).collect(MoreCollectors.toImmutableSortedSet(Comparator.<TargetNode<?, ?>>naturalOrder()));
                int numCandidates = candidateProviders.size();
                if (numCandidates == 1) {
                    depsForBuildFiles.addDep(rule.getBuildTarget(), Iterables.getOnlyElement(candidateProviders).getBuildTarget(), typeOfDepToAdd);
                } else if (numCandidates > 1) {
                    // Warn the user that there is an ambiguity. This could be very common with macros that
                    // generate multiple versions of a java_library() with the same sources.
                    // If numProviders is 0, then hopefully the dep is provided by something the user
                    // hardcoded in the BUCK file.
                    console.printErrorText(String.format("WARNING: Multiple providers for %s: %s. " + "Consider adding entry to .buckconfig to eliminate ambiguity:\n" + "[autodeps]\n" + "java-package-mappings = %s => %s", requiredSymbol, Joiner.on(", ").join(candidateProviders), requiredSymbol, Iterables.getFirst(candidateProviders, null)));
                } else {
                    // If there aren't any candidates, then see if there is a visible rule that can provide
                    // the symbol via its exported_deps. We make this a secondary check because we prefer to
                    // depend on the rule that defines the symbol directly rather than one of possibly many
                    // rules that provides it via its exported_deps.
                    ImmutableSortedSet<TargetNode<?, ?>> newCandidates = providers.stream().flatMap(candidate -> dependencyInfo.ruleToRulesThatExportIt.get(candidate).stream()).filter(ruleThatExportsCandidate -> ruleThatExportsCandidate.isVisibleTo(graph, rule)).collect(MoreCollectors.toImmutableSortedSet(Comparator.<TargetNode<?, ?>>naturalOrder()));
                    int numNewCandidates = newCandidates.size();
                    if (numNewCandidates == 1) {
                        depsForBuildFiles.addDep(rule.getBuildTarget(), Iterables.getOnlyElement(newCandidates).getBuildTarget(), typeOfDepToAdd);
                    } else if (numNewCandidates > 1) {
                        console.printErrorText(String.format("WARNING: No providers found for '%s' for build rule %s, " + "but there are multiple rules that export a rule to provide %s: %s", requiredSymbol, rule.getBuildTarget(), requiredSymbol, Joiner.on(", ").join(newCandidates)));
                    }
                // In the case that numNewCandidates is 0, we assume that the user is taking
                // responsibility for declaring a provider for the symbol by hardcoding it in the deps.
                }
            }
        }
    }
    return depsForBuildFiles;
}
Also used : Iterables(com.google.common.collect.Iterables) BuildRuleType(com.facebook.buck.rules.BuildRuleType) CellPathResolver(com.facebook.buck.rules.CellPathResolver) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) SortedSet(java.util.SortedSet) JavaBuckConfig(com.facebook.buck.jvm.java.JavaBuckConfig) ExecutionContext(com.facebook.buck.step.ExecutionContext) HashSet(java.util.HashSet) AndroidLibraryDescription(com.facebook.buck.android.AndroidLibraryDescription) BuckConfig(com.facebook.buck.cli.BuckConfig) HashMultimap(com.google.common.collect.HashMultimap) PrebuiltJarDescription(com.facebook.buck.jvm.java.PrebuiltJarDescription) JavaFileParser(com.facebook.buck.jvm.java.JavaFileParser) BuildTargetPatternParser(com.facebook.buck.parser.BuildTargetPatternParser) BuildTargetParser(com.facebook.buck.parser.BuildTargetParser) Map(java.util.Map) JavaLibraryDescription(com.facebook.buck.jvm.java.JavaLibraryDescription) Splitter(com.google.common.base.Splitter) ImmutableSortedMap(com.google.common.collect.ImmutableSortedMap) Nullable(javax.annotation.Nullable) MoreCollectors(com.facebook.buck.util.MoreCollectors) ImmutableSortedSet(com.google.common.collect.ImmutableSortedSet) ImmutableSet(com.google.common.collect.ImmutableSet) DepsForBuildFiles(com.facebook.buck.autodeps.DepsForBuildFiles) Predicate(java.util.function.Predicate) TargetGraph(com.facebook.buck.rules.TargetGraph) TargetNode(com.facebook.buck.rules.TargetNode) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) CharMatcher(com.google.common.base.CharMatcher) Set(java.util.Set) JavacOptions(com.facebook.buck.jvm.java.JavacOptions) Console(com.facebook.buck.util.Console) BuildTarget(com.facebook.buck.model.BuildTarget) Maps(com.google.common.collect.Maps) JavaTestDescription(com.facebook.buck.jvm.java.JavaTestDescription) BuildResult(com.facebook.buck.rules.BuildResult) Futures(com.google.common.util.concurrent.Futures) BuildEngineBuildContext(com.facebook.buck.rules.BuildEngineBuildContext) Stream(java.util.stream.Stream) DependencyType(com.facebook.buck.autodeps.DepsForBuildFiles.DependencyType) BuildEngine(com.facebook.buck.rules.BuildEngine) Optional(java.util.Optional) Preconditions(com.google.common.base.Preconditions) Comparator(java.util.Comparator) Description(com.facebook.buck.rules.Description) Joiner(com.google.common.base.Joiner) TargetNode(com.facebook.buck.rules.TargetNode) DepsForBuildFiles(com.facebook.buck.autodeps.DepsForBuildFiles) BuildTarget(com.facebook.buck.model.BuildTarget) ImmutableSortedSet(com.google.common.collect.ImmutableSortedSet) DependencyType(com.facebook.buck.autodeps.DepsForBuildFiles.DependencyType) JavaTestDescription(com.facebook.buck.jvm.java.JavaTestDescription)

Example 2 with HashMultimap

use of com.google.common.collect.HashMultimap in project fastjson by alibaba.

the class HashMultimapTest method test_for_multimap.

public void test_for_multimap() throws Exception {
    HashMultimap map = HashMultimap.create();
    map.put("name", "a");
    map.put("name", "b");
    String json = JSON.toJSONString(map);
    assertEquals("{\"name\":[\"a\",\"b\"]}", json);
}
Also used : HashMultimap(com.google.common.collect.HashMultimap)

Example 3 with HashMultimap

use of com.google.common.collect.HashMultimap in project cassandra by apache.

the class LeaveAndBootstrapTest method testSimultaneousMove.

/**
     * Test pending ranges and write endpoints when multiple nodes are on the move
     * simultaneously
     */
@Test
public void testSimultaneousMove() throws UnknownHostException {
    StorageService ss = StorageService.instance;
    final int RING_SIZE = 10;
    TokenMetadata tmd = ss.getTokenMetadata();
    tmd.clearUnsafe();
    IPartitioner partitioner = RandomPartitioner.instance;
    VersionedValue.VersionedValueFactory valueFactory = new VersionedValue.VersionedValueFactory(partitioner);
    ArrayList<Token> endpointTokens = new ArrayList<Token>();
    ArrayList<Token> keyTokens = new ArrayList<Token>();
    List<InetAddress> hosts = new ArrayList<InetAddress>();
    List<UUID> hostIds = new ArrayList<UUID>();
    // create a ring or 10 nodes
    Util.createInitialRing(ss, partitioner, endpointTokens, keyTokens, hosts, hostIds, RING_SIZE);
    // nodes 6, 8 and 9 leave
    final int[] LEAVING = new int[] { 6, 8, 9 };
    for (int leaving : LEAVING) ss.onChange(hosts.get(leaving), ApplicationState.STATUS, valueFactory.leaving(Collections.singleton(endpointTokens.get(leaving))));
    // boot two new nodes with keyTokens.get(5) and keyTokens.get(7)
    InetAddress boot1 = InetAddress.getByName("127.0.1.1");
    Gossiper.instance.initializeNodeUnsafe(boot1, UUID.randomUUID(), 1);
    Gossiper.instance.injectApplicationState(boot1, ApplicationState.TOKENS, valueFactory.tokens(Collections.singleton(keyTokens.get(5))));
    ss.onChange(boot1, ApplicationState.STATUS, valueFactory.bootstrapping(Collections.<Token>singleton(keyTokens.get(5))));
    InetAddress boot2 = InetAddress.getByName("127.0.1.2");
    Gossiper.instance.initializeNodeUnsafe(boot2, UUID.randomUUID(), 1);
    Gossiper.instance.injectApplicationState(boot2, ApplicationState.TOKENS, valueFactory.tokens(Collections.singleton(keyTokens.get(7))));
    ss.onChange(boot2, ApplicationState.STATUS, valueFactory.bootstrapping(Collections.<Token>singleton(keyTokens.get(7))));
    Collection<InetAddress> endpoints = null;
    /* don't require test update every time a new keyspace is added to test/conf/cassandra.yaml */
    Map<String, AbstractReplicationStrategy> keyspaceStrategyMap = new HashMap<String, AbstractReplicationStrategy>();
    for (int i = 1; i <= 4; i++) {
        keyspaceStrategyMap.put("LeaveAndBootstrapTestKeyspace" + i, getStrategy("LeaveAndBootstrapTestKeyspace" + i, tmd));
    }
    // pre-calculate the results.
    Map<String, Multimap<Token, InetAddress>> expectedEndpoints = new HashMap<String, Multimap<Token, InetAddress>>();
    expectedEndpoints.put(KEYSPACE1, HashMultimap.<Token, InetAddress>create());
    expectedEndpoints.get(KEYSPACE1).putAll(new BigIntegerToken("5"), makeAddrs("127.0.0.2"));
    expectedEndpoints.get(KEYSPACE1).putAll(new BigIntegerToken("15"), makeAddrs("127.0.0.3"));
    expectedEndpoints.get(KEYSPACE1).putAll(new BigIntegerToken("25"), makeAddrs("127.0.0.4"));
    expectedEndpoints.get(KEYSPACE1).putAll(new BigIntegerToken("35"), makeAddrs("127.0.0.5"));
    expectedEndpoints.get(KEYSPACE1).putAll(new BigIntegerToken("45"), makeAddrs("127.0.0.6"));
    expectedEndpoints.get(KEYSPACE1).putAll(new BigIntegerToken("55"), makeAddrs("127.0.0.7", "127.0.0.8", "127.0.1.1"));
    expectedEndpoints.get(KEYSPACE1).putAll(new BigIntegerToken("65"), makeAddrs("127.0.0.8"));
    expectedEndpoints.get(KEYSPACE1).putAll(new BigIntegerToken("75"), makeAddrs("127.0.0.9", "127.0.1.2", "127.0.0.1"));
    expectedEndpoints.get(KEYSPACE1).putAll(new BigIntegerToken("85"), makeAddrs("127.0.0.10", "127.0.0.1"));
    expectedEndpoints.get(KEYSPACE1).putAll(new BigIntegerToken("95"), makeAddrs("127.0.0.1"));
    expectedEndpoints.put(KEYSPACE2, HashMultimap.<Token, InetAddress>create());
    expectedEndpoints.get(KEYSPACE2).putAll(new BigIntegerToken("5"), makeAddrs("127.0.0.2"));
    expectedEndpoints.get(KEYSPACE2).putAll(new BigIntegerToken("15"), makeAddrs("127.0.0.3"));
    expectedEndpoints.get(KEYSPACE2).putAll(new BigIntegerToken("25"), makeAddrs("127.0.0.4"));
    expectedEndpoints.get(KEYSPACE2).putAll(new BigIntegerToken("35"), makeAddrs("127.0.0.5"));
    expectedEndpoints.get(KEYSPACE2).putAll(new BigIntegerToken("45"), makeAddrs("127.0.0.6"));
    expectedEndpoints.get(KEYSPACE2).putAll(new BigIntegerToken("55"), makeAddrs("127.0.0.7", "127.0.0.8", "127.0.1.1"));
    expectedEndpoints.get(KEYSPACE2).putAll(new BigIntegerToken("65"), makeAddrs("127.0.0.8"));
    expectedEndpoints.get(KEYSPACE2).putAll(new BigIntegerToken("75"), makeAddrs("127.0.0.9", "127.0.1.2", "127.0.0.1"));
    expectedEndpoints.get(KEYSPACE2).putAll(new BigIntegerToken("85"), makeAddrs("127.0.0.10", "127.0.0.1"));
    expectedEndpoints.get(KEYSPACE2).putAll(new BigIntegerToken("95"), makeAddrs("127.0.0.1"));
    expectedEndpoints.put(KEYSPACE3, HashMultimap.<Token, InetAddress>create());
    expectedEndpoints.get(KEYSPACE3).putAll(new BigIntegerToken("5"), makeAddrs("127.0.0.2", "127.0.0.3", "127.0.0.4", "127.0.0.5", "127.0.0.6"));
    expectedEndpoints.get(KEYSPACE3).putAll(new BigIntegerToken("15"), makeAddrs("127.0.0.3", "127.0.0.4", "127.0.0.5", "127.0.0.6", "127.0.0.7", "127.0.1.1", "127.0.0.8"));
    expectedEndpoints.get(KEYSPACE3).putAll(new BigIntegerToken("25"), makeAddrs("127.0.0.4", "127.0.0.5", "127.0.0.6", "127.0.0.7", "127.0.0.8", "127.0.1.2", "127.0.0.1", "127.0.1.1"));
    expectedEndpoints.get(KEYSPACE3).putAll(new BigIntegerToken("35"), makeAddrs("127.0.0.5", "127.0.0.6", "127.0.0.7", "127.0.0.8", "127.0.0.9", "127.0.1.2", "127.0.0.1", "127.0.0.2", "127.0.1.1"));
    expectedEndpoints.get(KEYSPACE3).putAll(new BigIntegerToken("45"), makeAddrs("127.0.0.6", "127.0.0.7", "127.0.0.8", "127.0.0.9", "127.0.0.10", "127.0.1.2", "127.0.0.1", "127.0.0.2", "127.0.1.1", "127.0.0.3"));
    expectedEndpoints.get(KEYSPACE3).putAll(new BigIntegerToken("55"), makeAddrs("127.0.0.7", "127.0.0.8", "127.0.0.9", "127.0.0.10", "127.0.0.1", "127.0.0.2", "127.0.0.3", "127.0.0.4", "127.0.1.1", "127.0.1.2"));
    expectedEndpoints.get(KEYSPACE3).putAll(new BigIntegerToken("65"), makeAddrs("127.0.0.8", "127.0.0.9", "127.0.0.10", "127.0.0.1", "127.0.0.2", "127.0.1.2", "127.0.0.3", "127.0.0.4"));
    expectedEndpoints.get(KEYSPACE3).putAll(new BigIntegerToken("75"), makeAddrs("127.0.0.9", "127.0.0.10", "127.0.0.1", "127.0.0.2", "127.0.0.3", "127.0.1.2", "127.0.0.4", "127.0.0.5"));
    expectedEndpoints.get(KEYSPACE3).putAll(new BigIntegerToken("85"), makeAddrs("127.0.0.10", "127.0.0.1", "127.0.0.2", "127.0.0.3", "127.0.0.4", "127.0.0.5"));
    expectedEndpoints.get(KEYSPACE3).putAll(new BigIntegerToken("95"), makeAddrs("127.0.0.1", "127.0.0.2", "127.0.0.3", "127.0.0.4", "127.0.0.5"));
    expectedEndpoints.put(KEYSPACE4, HashMultimap.<Token, InetAddress>create());
    expectedEndpoints.get(KEYSPACE4).putAll(new BigIntegerToken("5"), makeAddrs("127.0.0.2", "127.0.0.3", "127.0.0.4"));
    expectedEndpoints.get(KEYSPACE4).putAll(new BigIntegerToken("15"), makeAddrs("127.0.0.3", "127.0.0.4", "127.0.0.5"));
    expectedEndpoints.get(KEYSPACE4).putAll(new BigIntegerToken("25"), makeAddrs("127.0.0.4", "127.0.0.5", "127.0.0.6"));
    expectedEndpoints.get(KEYSPACE4).putAll(new BigIntegerToken("35"), makeAddrs("127.0.0.5", "127.0.0.6", "127.0.0.7", "127.0.1.1", "127.0.0.8"));
    expectedEndpoints.get(KEYSPACE4).putAll(new BigIntegerToken("45"), makeAddrs("127.0.0.6", "127.0.0.7", "127.0.0.8", "127.0.1.2", "127.0.0.1", "127.0.1.1"));
    expectedEndpoints.get(KEYSPACE4).putAll(new BigIntegerToken("55"), makeAddrs("127.0.0.7", "127.0.0.8", "127.0.0.9", "127.0.0.1", "127.0.0.2", "127.0.1.1", "127.0.1.2"));
    expectedEndpoints.get(KEYSPACE4).putAll(new BigIntegerToken("65"), makeAddrs("127.0.0.8", "127.0.0.9", "127.0.0.10", "127.0.1.2", "127.0.0.1", "127.0.0.2"));
    expectedEndpoints.get(KEYSPACE4).putAll(new BigIntegerToken("75"), makeAddrs("127.0.0.9", "127.0.0.10", "127.0.0.1", "127.0.1.2", "127.0.0.2", "127.0.0.3"));
    expectedEndpoints.get(KEYSPACE4).putAll(new BigIntegerToken("85"), makeAddrs("127.0.0.10", "127.0.0.1", "127.0.0.2", "127.0.0.3"));
    expectedEndpoints.get(KEYSPACE4).putAll(new BigIntegerToken("95"), makeAddrs("127.0.0.1", "127.0.0.2", "127.0.0.3"));
    PendingRangeCalculatorService.instance.blockUntilFinished();
    for (Map.Entry<String, AbstractReplicationStrategy> keyspaceStrategy : keyspaceStrategyMap.entrySet()) {
        String keyspaceName = keyspaceStrategy.getKey();
        AbstractReplicationStrategy strategy = keyspaceStrategy.getValue();
        for (int i = 0; i < keyTokens.size(); i++) {
            endpoints = tmd.getWriteEndpoints(keyTokens.get(i), keyspaceName, strategy.getNaturalEndpoints(keyTokens.get(i)));
            assertEquals(expectedEndpoints.get(keyspaceName).get(keyTokens.get(i)).size(), endpoints.size());
            assertTrue(expectedEndpoints.get(keyspaceName).get(keyTokens.get(i)).containsAll(endpoints));
        }
        // just to be sure that things still work according to the old tests, run them:
        if (strategy.getReplicationFactor() != 3)
            continue;
        // tokens 5, 15 and 25 should go three nodes
        for (int i = 0; i < 3; ++i) {
            endpoints = tmd.getWriteEndpoints(keyTokens.get(i), keyspaceName, strategy.getNaturalEndpoints(keyTokens.get(i)));
            assertEquals(3, endpoints.size());
            assertTrue(endpoints.contains(hosts.get(i + 1)));
            assertTrue(endpoints.contains(hosts.get(i + 2)));
            assertTrue(endpoints.contains(hosts.get(i + 3)));
        }
        // token 35 should go to nodes 4, 5, 6, 7 and boot1
        endpoints = tmd.getWriteEndpoints(keyTokens.get(3), keyspaceName, strategy.getNaturalEndpoints(keyTokens.get(3)));
        assertEquals(5, endpoints.size());
        assertTrue(endpoints.contains(hosts.get(4)));
        assertTrue(endpoints.contains(hosts.get(5)));
        assertTrue(endpoints.contains(hosts.get(6)));
        assertTrue(endpoints.contains(hosts.get(7)));
        assertTrue(endpoints.contains(boot1));
        // token 45 should go to nodes 5, 6, 7, 0, boot1 and boot2
        endpoints = tmd.getWriteEndpoints(keyTokens.get(4), keyspaceName, strategy.getNaturalEndpoints(keyTokens.get(4)));
        assertEquals(6, endpoints.size());
        assertTrue(endpoints.contains(hosts.get(5)));
        assertTrue(endpoints.contains(hosts.get(6)));
        assertTrue(endpoints.contains(hosts.get(7)));
        assertTrue(endpoints.contains(hosts.get(0)));
        assertTrue(endpoints.contains(boot1));
        assertTrue(endpoints.contains(boot2));
        // token 55 should go to nodes 6, 7, 8, 0, 1, boot1 and boot2
        endpoints = tmd.getWriteEndpoints(keyTokens.get(5), keyspaceName, strategy.getNaturalEndpoints(keyTokens.get(5)));
        assertEquals(7, endpoints.size());
        assertTrue(endpoints.contains(hosts.get(6)));
        assertTrue(endpoints.contains(hosts.get(7)));
        assertTrue(endpoints.contains(hosts.get(8)));
        assertTrue(endpoints.contains(hosts.get(0)));
        assertTrue(endpoints.contains(hosts.get(1)));
        assertTrue(endpoints.contains(boot1));
        assertTrue(endpoints.contains(boot2));
        // token 65 should go to nodes 7, 8, 9, 0, 1 and boot2
        endpoints = tmd.getWriteEndpoints(keyTokens.get(6), keyspaceName, strategy.getNaturalEndpoints(keyTokens.get(6)));
        assertEquals(6, endpoints.size());
        assertTrue(endpoints.contains(hosts.get(7)));
        assertTrue(endpoints.contains(hosts.get(8)));
        assertTrue(endpoints.contains(hosts.get(9)));
        assertTrue(endpoints.contains(hosts.get(0)));
        assertTrue(endpoints.contains(hosts.get(1)));
        assertTrue(endpoints.contains(boot2));
        // token 75 should to go nodes 8, 9, 0, 1, 2 and boot2
        endpoints = tmd.getWriteEndpoints(keyTokens.get(7), keyspaceName, strategy.getNaturalEndpoints(keyTokens.get(7)));
        assertEquals(6, endpoints.size());
        assertTrue(endpoints.contains(hosts.get(8)));
        assertTrue(endpoints.contains(hosts.get(9)));
        assertTrue(endpoints.contains(hosts.get(0)));
        assertTrue(endpoints.contains(hosts.get(1)));
        assertTrue(endpoints.contains(hosts.get(2)));
        assertTrue(endpoints.contains(boot2));
        // token 85 should go to nodes 9, 0, 1 and 2
        endpoints = tmd.getWriteEndpoints(keyTokens.get(8), keyspaceName, strategy.getNaturalEndpoints(keyTokens.get(8)));
        assertEquals(4, endpoints.size());
        assertTrue(endpoints.contains(hosts.get(9)));
        assertTrue(endpoints.contains(hosts.get(0)));
        assertTrue(endpoints.contains(hosts.get(1)));
        assertTrue(endpoints.contains(hosts.get(2)));
        // token 95 should go to nodes 0, 1 and 2
        endpoints = tmd.getWriteEndpoints(keyTokens.get(9), keyspaceName, strategy.getNaturalEndpoints(keyTokens.get(9)));
        assertEquals(3, endpoints.size());
        assertTrue(endpoints.contains(hosts.get(0)));
        assertTrue(endpoints.contains(hosts.get(1)));
        assertTrue(endpoints.contains(hosts.get(2)));
    }
    // Now finish node 6 and node 9 leaving, as well as boot1 (after this node 8 is still
    // leaving and boot2 in progress
    ss.onChange(hosts.get(LEAVING[0]), ApplicationState.STATUS, valueFactory.left(Collections.singleton(endpointTokens.get(LEAVING[0])), Gossiper.computeExpireTime()));
    ss.onChange(hosts.get(LEAVING[2]), ApplicationState.STATUS, valueFactory.left(Collections.singleton(endpointTokens.get(LEAVING[2])), Gossiper.computeExpireTime()));
    ss.onChange(boot1, ApplicationState.STATUS, valueFactory.normal(Collections.singleton(keyTokens.get(5))));
    // adjust precalcuated results.  this changes what the epected endpoints are.
    expectedEndpoints.get(KEYSPACE1).get(new BigIntegerToken("55")).removeAll(makeAddrs("127.0.0.7", "127.0.0.8"));
    expectedEndpoints.get(KEYSPACE1).get(new BigIntegerToken("85")).removeAll(makeAddrs("127.0.0.10"));
    expectedEndpoints.get(KEYSPACE2).get(new BigIntegerToken("55")).removeAll(makeAddrs("127.0.0.7", "127.0.0.8"));
    expectedEndpoints.get(KEYSPACE2).get(new BigIntegerToken("85")).removeAll(makeAddrs("127.0.0.10"));
    expectedEndpoints.get(KEYSPACE3).get(new BigIntegerToken("15")).removeAll(makeAddrs("127.0.0.7", "127.0.0.8"));
    expectedEndpoints.get(KEYSPACE3).get(new BigIntegerToken("25")).removeAll(makeAddrs("127.0.0.7", "127.0.1.2", "127.0.0.1"));
    expectedEndpoints.get(KEYSPACE3).get(new BigIntegerToken("35")).removeAll(makeAddrs("127.0.0.7", "127.0.0.2"));
    expectedEndpoints.get(KEYSPACE3).get(new BigIntegerToken("45")).removeAll(makeAddrs("127.0.0.7", "127.0.0.10", "127.0.0.3"));
    expectedEndpoints.get(KEYSPACE3).get(new BigIntegerToken("55")).removeAll(makeAddrs("127.0.0.7", "127.0.0.10", "127.0.0.4"));
    expectedEndpoints.get(KEYSPACE3).get(new BigIntegerToken("65")).removeAll(makeAddrs("127.0.0.10"));
    expectedEndpoints.get(KEYSPACE3).get(new BigIntegerToken("75")).removeAll(makeAddrs("127.0.0.10"));
    expectedEndpoints.get(KEYSPACE3).get(new BigIntegerToken("85")).removeAll(makeAddrs("127.0.0.10"));
    expectedEndpoints.get(KEYSPACE4).get(new BigIntegerToken("35")).removeAll(makeAddrs("127.0.0.7", "127.0.0.8"));
    expectedEndpoints.get(KEYSPACE4).get(new BigIntegerToken("45")).removeAll(makeAddrs("127.0.0.7", "127.0.1.2", "127.0.0.1"));
    expectedEndpoints.get(KEYSPACE4).get(new BigIntegerToken("55")).removeAll(makeAddrs("127.0.0.2", "127.0.0.7"));
    expectedEndpoints.get(KEYSPACE4).get(new BigIntegerToken("65")).removeAll(makeAddrs("127.0.0.10"));
    expectedEndpoints.get(KEYSPACE4).get(new BigIntegerToken("75")).removeAll(makeAddrs("127.0.0.10"));
    expectedEndpoints.get(KEYSPACE4).get(new BigIntegerToken("85")).removeAll(makeAddrs("127.0.0.10"));
    PendingRangeCalculatorService.instance.blockUntilFinished();
    for (Map.Entry<String, AbstractReplicationStrategy> keyspaceStrategy : keyspaceStrategyMap.entrySet()) {
        String keyspaceName = keyspaceStrategy.getKey();
        AbstractReplicationStrategy strategy = keyspaceStrategy.getValue();
        for (int i = 0; i < keyTokens.size(); i++) {
            endpoints = tmd.getWriteEndpoints(keyTokens.get(i), keyspaceName, strategy.getNaturalEndpoints(keyTokens.get(i)));
            assertEquals(expectedEndpoints.get(keyspaceName).get(keyTokens.get(i)).size(), endpoints.size());
            assertTrue(expectedEndpoints.get(keyspaceName).get(keyTokens.get(i)).containsAll(endpoints));
        }
        if (strategy.getReplicationFactor() != 3)
            continue;
        // tokens 5, 15 and 25 should go three nodes
        for (int i = 0; i < 3; ++i) {
            endpoints = tmd.getWriteEndpoints(keyTokens.get(i), keyspaceName, strategy.getNaturalEndpoints(keyTokens.get(i)));
            assertEquals(3, endpoints.size());
            assertTrue(endpoints.contains(hosts.get(i + 1)));
            assertTrue(endpoints.contains(hosts.get(i + 2)));
            assertTrue(endpoints.contains(hosts.get(i + 3)));
        }
        // token 35 goes to nodes 4, 5 and boot1
        endpoints = tmd.getWriteEndpoints(keyTokens.get(3), keyspaceName, strategy.getNaturalEndpoints(keyTokens.get(3)));
        assertEquals(3, endpoints.size());
        assertTrue(endpoints.contains(hosts.get(4)));
        assertTrue(endpoints.contains(hosts.get(5)));
        assertTrue(endpoints.contains(boot1));
        // token 45 goes to nodes 5, boot1 and node7
        endpoints = tmd.getWriteEndpoints(keyTokens.get(4), keyspaceName, strategy.getNaturalEndpoints(keyTokens.get(4)));
        assertEquals(3, endpoints.size());
        assertTrue(endpoints.contains(hosts.get(5)));
        assertTrue(endpoints.contains(boot1));
        assertTrue(endpoints.contains(hosts.get(7)));
        // token 55 goes to boot1, 7, boot2, 8 and 0
        endpoints = tmd.getWriteEndpoints(keyTokens.get(5), keyspaceName, strategy.getNaturalEndpoints(keyTokens.get(5)));
        assertEquals(5, endpoints.size());
        assertTrue(endpoints.contains(boot1));
        assertTrue(endpoints.contains(hosts.get(7)));
        assertTrue(endpoints.contains(boot2));
        assertTrue(endpoints.contains(hosts.get(8)));
        assertTrue(endpoints.contains(hosts.get(0)));
        // token 65 goes to nodes 7, boot2, 8, 0 and 1
        endpoints = tmd.getWriteEndpoints(keyTokens.get(6), keyspaceName, strategy.getNaturalEndpoints(keyTokens.get(6)));
        assertEquals(5, endpoints.size());
        assertTrue(endpoints.contains(hosts.get(7)));
        assertTrue(endpoints.contains(boot2));
        assertTrue(endpoints.contains(hosts.get(8)));
        assertTrue(endpoints.contains(hosts.get(0)));
        assertTrue(endpoints.contains(hosts.get(1)));
        // token 75 goes to nodes boot2, 8, 0, 1 and 2
        endpoints = tmd.getWriteEndpoints(keyTokens.get(7), keyspaceName, strategy.getNaturalEndpoints(keyTokens.get(7)));
        assertEquals(5, endpoints.size());
        assertTrue(endpoints.contains(boot2));
        assertTrue(endpoints.contains(hosts.get(8)));
        assertTrue(endpoints.contains(hosts.get(0)));
        assertTrue(endpoints.contains(hosts.get(1)));
        assertTrue(endpoints.contains(hosts.get(2)));
        // token 85 goes to nodes 0, 1 and 2
        endpoints = tmd.getWriteEndpoints(keyTokens.get(8), keyspaceName, strategy.getNaturalEndpoints(keyTokens.get(8)));
        assertEquals(3, endpoints.size());
        assertTrue(endpoints.contains(hosts.get(0)));
        assertTrue(endpoints.contains(hosts.get(1)));
        assertTrue(endpoints.contains(hosts.get(2)));
        // token 95 goes to nodes 0, 1 and 2
        endpoints = tmd.getWriteEndpoints(keyTokens.get(9), keyspaceName, strategy.getNaturalEndpoints(keyTokens.get(9)));
        assertEquals(3, endpoints.size());
        assertTrue(endpoints.contains(hosts.get(0)));
        assertTrue(endpoints.contains(hosts.get(1)));
        assertTrue(endpoints.contains(hosts.get(2)));
    }
}
Also used : BigIntegerToken(org.apache.cassandra.dht.RandomPartitioner.BigIntegerToken) Token(org.apache.cassandra.dht.Token) IPartitioner(org.apache.cassandra.dht.IPartitioner) VersionedValue(org.apache.cassandra.gms.VersionedValue) BigIntegerToken(org.apache.cassandra.dht.RandomPartitioner.BigIntegerToken) TokenMetadata(org.apache.cassandra.locator.TokenMetadata) Multimap(com.google.common.collect.Multimap) HashMultimap(com.google.common.collect.HashMultimap) AbstractReplicationStrategy(org.apache.cassandra.locator.AbstractReplicationStrategy) InetAddress(java.net.InetAddress) Test(org.junit.Test)

Example 4 with HashMultimap

use of com.google.common.collect.HashMultimap in project eiger by wlloyd.

the class MoveTest method testSimultaneousMove.

/*
     * Test ranges and write endpoints when multiple nodes are on the move simultaneously
     */
@Test
public void testSimultaneousMove() throws UnknownHostException, ConfigurationException {
    StorageService ss = StorageService.instance;
    final int RING_SIZE = 10;
    TokenMetadata tmd = ss.getTokenMetadata();
    tmd.clearUnsafe();
    IPartitioner partitioner = new RandomPartitioner();
    VersionedValue.VersionedValueFactory valueFactory = new VersionedValue.VersionedValueFactory(partitioner);
    IPartitioner oldPartitioner = ss.setPartitionerUnsafe(partitioner);
    ArrayList<Token> endpointTokens = new ArrayList<Token>();
    ArrayList<Token> keyTokens = new ArrayList<Token>();
    List<InetAddress> hosts = new ArrayList<InetAddress>();
    // create a ring or 10 nodes
    Util.createInitialRing(ss, partitioner, endpointTokens, keyTokens, hosts, RING_SIZE);
    // nodes 6, 8 and 9 leave
    final int[] MOVING = new int[] { 6, 8, 9 };
    Map<Integer, Token> newTokens = new HashMap<Integer, Token>();
    for (int movingIndex : MOVING) {
        Token newToken = positionToken(movingIndex);
        ss.onChange(hosts.get(movingIndex), ApplicationState.STATUS, valueFactory.moving(newToken));
        // storing token associated with a node index
        newTokens.put(movingIndex, newToken);
    }
    Collection<InetAddress> endpoints;
    tmd = tmd.cloneAfterAllSettled();
    ss.setTokenMetadataUnsafe(tmd);
    // boot two new nodes with keyTokens.get(5) and keyTokens.get(7)
    InetAddress boot1 = InetAddress.getByName("127.0.1.1");
    ss.onChange(boot1, ApplicationState.STATUS, valueFactory.bootstrapping(keyTokens.get(5)));
    InetAddress boot2 = InetAddress.getByName("127.0.1.2");
    ss.onChange(boot2, ApplicationState.STATUS, valueFactory.bootstrapping(keyTokens.get(7)));
    // don't require test update every time a new keyspace is added to test/conf/cassandra.yaml
    Map<String, AbstractReplicationStrategy> tableStrategyMap = new HashMap<String, AbstractReplicationStrategy>();
    for (int i = 1; i <= 4; i++) {
        tableStrategyMap.put("Keyspace" + i, getStrategy("Keyspace" + i, tmd));
    }
    /**
        *  Keyspace1 & Keyspace2 RF=1
        *  {
        *      /127.0.0.1=[(92,0]],
        *      /127.0.0.2=[(0,10]],
        *      /127.0.0.3=[(10,20]],
        *      /127.0.0.4=[(20,30]],
        *      /127.0.0.5=[(30,40]],
        *      /127.0.0.6=[(40,50]],
        *      /127.0.0.7=[(50,62]],
        *      /127.0.0.8=[(62,70]],
        *      /127.0.0.9=[(70,82]],
        *      /127.0.0.10=[(82,92]]
        *  }
        */
    Multimap<InetAddress, Range<Token>> keyspace1ranges = tableStrategyMap.get("Keyspace1").getAddressRanges();
    Collection<Range<Token>> ranges1 = keyspace1ranges.get(InetAddress.getByName("127.0.0.1"));
    assertEquals(collectionSize(ranges1), 1);
    assertTrue(ranges1.iterator().next().equals(generateRange(92, 0)));
    Collection<Range<Token>> ranges2 = keyspace1ranges.get(InetAddress.getByName("127.0.0.2"));
    assertEquals(collectionSize(ranges2), 1);
    assertTrue(ranges2.iterator().next().equals(generateRange(0, 10)));
    Collection<Range<Token>> ranges3 = keyspace1ranges.get(InetAddress.getByName("127.0.0.3"));
    assertEquals(collectionSize(ranges3), 1);
    assertTrue(ranges3.iterator().next().equals(generateRange(10, 20)));
    Collection<Range<Token>> ranges4 = keyspace1ranges.get(InetAddress.getByName("127.0.0.4"));
    assertEquals(collectionSize(ranges4), 1);
    assertTrue(ranges4.iterator().next().equals(generateRange(20, 30)));
    Collection<Range<Token>> ranges5 = keyspace1ranges.get(InetAddress.getByName("127.0.0.5"));
    assertEquals(collectionSize(ranges5), 1);
    assertTrue(ranges5.iterator().next().equals(generateRange(30, 40)));
    Collection<Range<Token>> ranges6 = keyspace1ranges.get(InetAddress.getByName("127.0.0.6"));
    assertEquals(collectionSize(ranges6), 1);
    assertTrue(ranges6.iterator().next().equals(generateRange(40, 50)));
    Collection<Range<Token>> ranges7 = keyspace1ranges.get(InetAddress.getByName("127.0.0.7"));
    assertEquals(collectionSize(ranges7), 1);
    assertTrue(ranges7.iterator().next().equals(generateRange(50, 62)));
    Collection<Range<Token>> ranges8 = keyspace1ranges.get(InetAddress.getByName("127.0.0.8"));
    assertEquals(collectionSize(ranges8), 1);
    assertTrue(ranges8.iterator().next().equals(generateRange(62, 70)));
    Collection<Range<Token>> ranges9 = keyspace1ranges.get(InetAddress.getByName("127.0.0.9"));
    assertEquals(collectionSize(ranges9), 1);
    assertTrue(ranges9.iterator().next().equals(generateRange(70, 82)));
    Collection<Range<Token>> ranges10 = keyspace1ranges.get(InetAddress.getByName("127.0.0.10"));
    assertEquals(collectionSize(ranges10), 1);
    assertTrue(ranges10.iterator().next().equals(generateRange(82, 92)));
    /**
        * Keyspace3 RF=5
        * {
        *      /127.0.0.1=[(92,0], (70,82], (50,62], (82,92], (62,70]],
        *      /127.0.0.2=[(92,0], (70,82], (82,92], (0,10], (62,70]],
        *      /127.0.0.3=[(92,0], (70,82], (82,92], (0,10], (10,20]],
        *      /127.0.0.4=[(92,0], (20,30], (82,92], (0,10], (10,20]],
        *      /127.0.0.5=[(92,0], (30,40], (20,30], (0,10], (10,20]],
        *      /127.0.0.6=[(40,50], (30,40], (20,30], (0,10], (10,20]],
        *      /127.0.0.7=[(40,50], (30,40], (50,62], (20,30], (10,20]],
        *      /127.0.0.8=[(40,50], (30,40], (50,62], (20,30], (62,70]],
        *      /127.0.0.9=[(40,50], (70,82], (30,40], (50,62], (62,70]],
        *      /127.0.0.10=[(40,50], (70,82], (50,62], (82,92], (62,70]]
        * }
        */
    Multimap<InetAddress, Range<Token>> keyspace3ranges = tableStrategyMap.get("Keyspace3").getAddressRanges();
    ranges1 = keyspace3ranges.get(InetAddress.getByName("127.0.0.1"));
    assertEquals(collectionSize(ranges1), 5);
    assertTrue(ranges1.equals(generateRanges(92, 0, 70, 82, 50, 62, 82, 92, 62, 70)));
    ranges2 = keyspace3ranges.get(InetAddress.getByName("127.0.0.2"));
    assertEquals(collectionSize(ranges2), 5);
    assertTrue(ranges2.equals(generateRanges(92, 0, 70, 82, 82, 92, 0, 10, 62, 70)));
    ranges3 = keyspace3ranges.get(InetAddress.getByName("127.0.0.3"));
    assertEquals(collectionSize(ranges3), 5);
    assertTrue(ranges3.equals(generateRanges(92, 0, 70, 82, 82, 92, 0, 10, 10, 20)));
    ranges4 = keyspace3ranges.get(InetAddress.getByName("127.0.0.4"));
    assertEquals(collectionSize(ranges4), 5);
    assertTrue(ranges4.equals(generateRanges(92, 0, 20, 30, 82, 92, 0, 10, 10, 20)));
    ranges5 = keyspace3ranges.get(InetAddress.getByName("127.0.0.5"));
    assertEquals(collectionSize(ranges5), 5);
    assertTrue(ranges5.equals(generateRanges(92, 0, 30, 40, 20, 30, 0, 10, 10, 20)));
    ranges6 = keyspace3ranges.get(InetAddress.getByName("127.0.0.6"));
    assertEquals(collectionSize(ranges6), 5);
    assertTrue(ranges6.equals(generateRanges(40, 50, 30, 40, 20, 30, 0, 10, 10, 20)));
    ranges7 = keyspace3ranges.get(InetAddress.getByName("127.0.0.7"));
    assertEquals(collectionSize(ranges7), 5);
    assertTrue(ranges7.equals(generateRanges(40, 50, 30, 40, 50, 62, 20, 30, 10, 20)));
    ranges8 = keyspace3ranges.get(InetAddress.getByName("127.0.0.8"));
    assertEquals(collectionSize(ranges8), 5);
    assertTrue(ranges8.equals(generateRanges(40, 50, 30, 40, 50, 62, 20, 30, 62, 70)));
    ranges9 = keyspace3ranges.get(InetAddress.getByName("127.0.0.9"));
    assertEquals(collectionSize(ranges9), 5);
    assertTrue(ranges9.equals(generateRanges(40, 50, 70, 82, 30, 40, 50, 62, 62, 70)));
    ranges10 = keyspace3ranges.get(InetAddress.getByName("127.0.0.10"));
    assertEquals(collectionSize(ranges10), 5);
    assertTrue(ranges10.equals(generateRanges(40, 50, 70, 82, 50, 62, 82, 92, 62, 70)));
    /**
         * Keyspace4 RF=3
         * {
         *      /127.0.0.1=[(92,0], (70,82], (82,92]],
         *      /127.0.0.2=[(92,0], (82,92], (0,10]],
         *      /127.0.0.3=[(92,0], (0,10], (10,20]],
         *      /127.0.0.4=[(20,30], (0,10], (10,20]],
         *      /127.0.0.5=[(30,40], (20,30], (10,20]],
         *      /127.0.0.6=[(40,50], (30,40], (20,30]],
         *      /127.0.0.7=[(40,50], (30,40], (50,62]],
         *      /127.0.0.8=[(40,50], (50,62], (62,70]],
         *      /127.0.0.9=[(70,82], (50,62], (62,70]],
         *      /127.0.0.10=[(70,82], (82,92], (62,70]]
         *  }
         */
    Multimap<InetAddress, Range<Token>> keyspace4ranges = tableStrategyMap.get("Keyspace4").getAddressRanges();
    ranges1 = keyspace4ranges.get(InetAddress.getByName("127.0.0.1"));
    assertEquals(collectionSize(ranges1), 3);
    assertTrue(ranges1.equals(generateRanges(92, 0, 70, 82, 82, 92)));
    ranges2 = keyspace4ranges.get(InetAddress.getByName("127.0.0.2"));
    assertEquals(collectionSize(ranges2), 3);
    assertTrue(ranges2.equals(generateRanges(92, 0, 82, 92, 0, 10)));
    ranges3 = keyspace4ranges.get(InetAddress.getByName("127.0.0.3"));
    assertEquals(collectionSize(ranges3), 3);
    assertTrue(ranges3.equals(generateRanges(92, 0, 0, 10, 10, 20)));
    ranges4 = keyspace4ranges.get(InetAddress.getByName("127.0.0.4"));
    assertEquals(collectionSize(ranges4), 3);
    assertTrue(ranges4.equals(generateRanges(20, 30, 0, 10, 10, 20)));
    ranges5 = keyspace4ranges.get(InetAddress.getByName("127.0.0.5"));
    assertEquals(collectionSize(ranges5), 3);
    assertTrue(ranges5.equals(generateRanges(30, 40, 20, 30, 10, 20)));
    ranges6 = keyspace4ranges.get(InetAddress.getByName("127.0.0.6"));
    assertEquals(collectionSize(ranges6), 3);
    assertTrue(ranges6.equals(generateRanges(40, 50, 30, 40, 20, 30)));
    ranges7 = keyspace4ranges.get(InetAddress.getByName("127.0.0.7"));
    assertEquals(collectionSize(ranges7), 3);
    assertTrue(ranges7.equals(generateRanges(40, 50, 30, 40, 50, 62)));
    ranges8 = keyspace4ranges.get(InetAddress.getByName("127.0.0.8"));
    assertEquals(collectionSize(ranges8), 3);
    assertTrue(ranges8.equals(generateRanges(40, 50, 50, 62, 62, 70)));
    ranges9 = keyspace4ranges.get(InetAddress.getByName("127.0.0.9"));
    assertEquals(collectionSize(ranges9), 3);
    assertTrue(ranges9.equals(generateRanges(70, 82, 50, 62, 62, 70)));
    ranges10 = keyspace4ranges.get(InetAddress.getByName("127.0.0.10"));
    assertEquals(collectionSize(ranges10), 3);
    assertTrue(ranges10.equals(generateRanges(70, 82, 82, 92, 62, 70)));
    // pre-calculate the results.
    Map<String, Multimap<Token, InetAddress>> expectedEndpoints = new HashMap<String, Multimap<Token, InetAddress>>();
    expectedEndpoints.put("Keyspace1", HashMultimap.<Token, InetAddress>create());
    expectedEndpoints.get("Keyspace1").putAll(new BigIntegerToken("5"), makeAddrs("127.0.0.2"));
    expectedEndpoints.get("Keyspace1").putAll(new BigIntegerToken("15"), makeAddrs("127.0.0.3"));
    expectedEndpoints.get("Keyspace1").putAll(new BigIntegerToken("25"), makeAddrs("127.0.0.4"));
    expectedEndpoints.get("Keyspace1").putAll(new BigIntegerToken("35"), makeAddrs("127.0.0.5"));
    expectedEndpoints.get("Keyspace1").putAll(new BigIntegerToken("45"), makeAddrs("127.0.0.6"));
    expectedEndpoints.get("Keyspace1").putAll(new BigIntegerToken("55"), makeAddrs("127.0.0.7", "127.0.1.1"));
    expectedEndpoints.get("Keyspace1").putAll(new BigIntegerToken("65"), makeAddrs("127.0.0.8"));
    expectedEndpoints.get("Keyspace1").putAll(new BigIntegerToken("75"), makeAddrs("127.0.0.9", "127.0.1.2"));
    expectedEndpoints.get("Keyspace1").putAll(new BigIntegerToken("85"), makeAddrs("127.0.0.10"));
    expectedEndpoints.get("Keyspace1").putAll(new BigIntegerToken("95"), makeAddrs("127.0.0.1"));
    expectedEndpoints.put("Keyspace2", HashMultimap.<Token, InetAddress>create());
    expectedEndpoints.get("Keyspace2").putAll(new BigIntegerToken("5"), makeAddrs("127.0.0.2"));
    expectedEndpoints.get("Keyspace2").putAll(new BigIntegerToken("15"), makeAddrs("127.0.0.3"));
    expectedEndpoints.get("Keyspace2").putAll(new BigIntegerToken("25"), makeAddrs("127.0.0.4"));
    expectedEndpoints.get("Keyspace2").putAll(new BigIntegerToken("35"), makeAddrs("127.0.0.5"));
    expectedEndpoints.get("Keyspace2").putAll(new BigIntegerToken("45"), makeAddrs("127.0.0.6"));
    expectedEndpoints.get("Keyspace2").putAll(new BigIntegerToken("55"), makeAddrs("127.0.0.7", "127.0.1.1"));
    expectedEndpoints.get("Keyspace2").putAll(new BigIntegerToken("65"), makeAddrs("127.0.0.8"));
    expectedEndpoints.get("Keyspace2").putAll(new BigIntegerToken("75"), makeAddrs("127.0.0.9", "127.0.1.2"));
    expectedEndpoints.get("Keyspace2").putAll(new BigIntegerToken("85"), makeAddrs("127.0.0.10"));
    expectedEndpoints.get("Keyspace2").putAll(new BigIntegerToken("95"), makeAddrs("127.0.0.1"));
    expectedEndpoints.put("Keyspace3", HashMultimap.<Token, InetAddress>create());
    expectedEndpoints.get("Keyspace3").putAll(new BigIntegerToken("5"), makeAddrs("127.0.0.2", "127.0.0.3", "127.0.0.4", "127.0.0.5", "127.0.0.6"));
    expectedEndpoints.get("Keyspace3").putAll(new BigIntegerToken("15"), makeAddrs("127.0.0.3", "127.0.0.4", "127.0.0.5", "127.0.0.6", "127.0.0.7", "127.0.1.1"));
    expectedEndpoints.get("Keyspace3").putAll(new BigIntegerToken("25"), makeAddrs("127.0.0.4", "127.0.0.5", "127.0.0.6", "127.0.0.7", "127.0.0.8", "127.0.1.1"));
    expectedEndpoints.get("Keyspace3").putAll(new BigIntegerToken("35"), makeAddrs("127.0.0.5", "127.0.0.6", "127.0.0.7", "127.0.0.8", "127.0.0.9", "127.0.1.1", "127.0.1.2"));
    expectedEndpoints.get("Keyspace3").putAll(new BigIntegerToken("45"), makeAddrs("127.0.0.6", "127.0.0.7", "127.0.0.8", "127.0.0.9", "127.0.0.10", "127.0.1.1", "127.0.1.2"));
    expectedEndpoints.get("Keyspace3").putAll(new BigIntegerToken("55"), makeAddrs("127.0.0.7", "127.0.0.8", "127.0.0.9", "127.0.0.10", "127.0.0.1", "127.0.1.1", "127.0.1.2"));
    expectedEndpoints.get("Keyspace3").putAll(new BigIntegerToken("65"), makeAddrs("127.0.0.8", "127.0.0.9", "127.0.0.10", "127.0.0.1", "127.0.0.2", "127.0.1.2"));
    expectedEndpoints.get("Keyspace3").putAll(new BigIntegerToken("75"), makeAddrs("127.0.0.9", "127.0.0.10", "127.0.0.1", "127.0.0.2", "127.0.0.3", "127.0.1.2"));
    expectedEndpoints.get("Keyspace3").putAll(new BigIntegerToken("85"), makeAddrs("127.0.0.10", "127.0.0.1", "127.0.0.2", "127.0.0.3", "127.0.0.4"));
    expectedEndpoints.get("Keyspace3").putAll(new BigIntegerToken("95"), makeAddrs("127.0.0.1", "127.0.0.2", "127.0.0.3", "127.0.0.4", "127.0.0.5"));
    expectedEndpoints.put("Keyspace4", HashMultimap.<Token, InetAddress>create());
    expectedEndpoints.get("Keyspace4").putAll(new BigIntegerToken("5"), makeAddrs("127.0.0.2", "127.0.0.3", "127.0.0.4"));
    expectedEndpoints.get("Keyspace4").putAll(new BigIntegerToken("15"), makeAddrs("127.0.0.3", "127.0.0.4", "127.0.0.5"));
    expectedEndpoints.get("Keyspace4").putAll(new BigIntegerToken("25"), makeAddrs("127.0.0.4", "127.0.0.5", "127.0.0.6"));
    expectedEndpoints.get("Keyspace4").putAll(new BigIntegerToken("35"), makeAddrs("127.0.0.5", "127.0.0.6", "127.0.0.7", "127.0.1.1"));
    expectedEndpoints.get("Keyspace4").putAll(new BigIntegerToken("45"), makeAddrs("127.0.0.6", "127.0.0.7", "127.0.0.8", "127.0.1.1"));
    expectedEndpoints.get("Keyspace4").putAll(new BigIntegerToken("55"), makeAddrs("127.0.0.7", "127.0.0.8", "127.0.0.9", "127.0.1.1", "127.0.1.2"));
    expectedEndpoints.get("Keyspace4").putAll(new BigIntegerToken("65"), makeAddrs("127.0.0.8", "127.0.0.9", "127.0.0.10", "127.0.1.2"));
    expectedEndpoints.get("Keyspace4").putAll(new BigIntegerToken("75"), makeAddrs("127.0.0.9", "127.0.0.10", "127.0.0.1", "127.0.1.2"));
    expectedEndpoints.get("Keyspace4").putAll(new BigIntegerToken("85"), makeAddrs("127.0.0.10", "127.0.0.1", "127.0.0.2"));
    expectedEndpoints.get("Keyspace4").putAll(new BigIntegerToken("95"), makeAddrs("127.0.0.1", "127.0.0.2", "127.0.0.3"));
    for (Map.Entry<String, AbstractReplicationStrategy> tableStrategy : tableStrategyMap.entrySet()) {
        String table = tableStrategy.getKey();
        AbstractReplicationStrategy strategy = tableStrategy.getValue();
        for (Token token : keyTokens) {
            endpoints = tmd.getWriteEndpoints(token, table, strategy.getNaturalEndpoints(token));
            assertTrue(expectedEndpoints.get(table).get(token).size() == endpoints.size());
            assertTrue(expectedEndpoints.get(table).get(token).containsAll(endpoints));
        }
        // just to be sure that things still work according to the old tests, run them:
        if (strategy.getReplicationFactor() != 3)
            continue;
        // tokens 5, 15 and 25 should go three nodes
        for (int i = 0; i < 3; i++) {
            endpoints = tmd.getWriteEndpoints(keyTokens.get(i), table, strategy.getNaturalEndpoints(keyTokens.get(i)));
            assertTrue(endpoints.size() == 3);
            assertTrue(endpoints.contains(hosts.get(i + 1)));
            assertTrue(endpoints.contains(hosts.get(i + 2)));
            assertTrue(endpoints.contains(hosts.get(i + 3)));
        }
        // token 35 should go to nodes 4, 5, 6 and boot1
        endpoints = tmd.getWriteEndpoints(keyTokens.get(3), table, strategy.getNaturalEndpoints(keyTokens.get(3)));
        assertTrue(endpoints.size() == 4);
        assertTrue(endpoints.contains(hosts.get(4)));
        assertTrue(endpoints.contains(hosts.get(5)));
        assertTrue(endpoints.contains(hosts.get(6)));
        assertTrue(endpoints.contains(boot1));
        // token 45 should go to nodes 5, 6, 7 boot1
        endpoints = tmd.getWriteEndpoints(keyTokens.get(4), table, strategy.getNaturalEndpoints(keyTokens.get(4)));
        assertTrue(endpoints.size() == 4);
        assertTrue(endpoints.contains(hosts.get(5)));
        assertTrue(endpoints.contains(hosts.get(6)));
        assertTrue(endpoints.contains(hosts.get(7)));
        assertTrue(endpoints.contains(boot1));
        // token 55 should go to nodes 6, 7, 8 boot1 and boot2
        endpoints = tmd.getWriteEndpoints(keyTokens.get(5), table, strategy.getNaturalEndpoints(keyTokens.get(5)));
        assertTrue(endpoints.size() == 5);
        assertTrue(endpoints.contains(hosts.get(6)));
        assertTrue(endpoints.contains(hosts.get(7)));
        assertTrue(endpoints.contains(hosts.get(8)));
        assertTrue(endpoints.contains(boot1));
        assertTrue(endpoints.contains(boot2));
        // token 65 should go to nodes 7, 8, 9 and boot2
        endpoints = tmd.getWriteEndpoints(keyTokens.get(6), table, strategy.getNaturalEndpoints(keyTokens.get(6)));
        assertTrue(endpoints.size() == 4);
        assertTrue(endpoints.contains(hosts.get(7)));
        assertTrue(endpoints.contains(hosts.get(8)));
        assertTrue(endpoints.contains(hosts.get(9)));
        assertTrue(endpoints.contains(boot2));
        // token 75 should to go nodes 8, 9, 0 and boot2
        endpoints = tmd.getWriteEndpoints(keyTokens.get(7), table, strategy.getNaturalEndpoints(keyTokens.get(7)));
        assertTrue(endpoints.size() == 4);
        assertTrue(endpoints.contains(hosts.get(8)));
        assertTrue(endpoints.contains(hosts.get(9)));
        assertTrue(endpoints.contains(hosts.get(0)));
        assertTrue(endpoints.contains(boot2));
        // token 85 should go to nodes 9, 0, 1
        endpoints = tmd.getWriteEndpoints(keyTokens.get(8), table, strategy.getNaturalEndpoints(keyTokens.get(8)));
        assertTrue(endpoints.size() == 3);
        assertTrue(endpoints.contains(hosts.get(9)));
        assertTrue(endpoints.contains(hosts.get(0)));
        assertTrue(endpoints.contains(hosts.get(1)));
        // token 95 should go to nodes 0, 1 and 2
        endpoints = tmd.getWriteEndpoints(keyTokens.get(9), table, strategy.getNaturalEndpoints(keyTokens.get(9)));
        assertTrue(endpoints.size() == 3);
        assertTrue(endpoints.contains(hosts.get(0)));
        assertTrue(endpoints.contains(hosts.get(1)));
        assertTrue(endpoints.contains(hosts.get(2)));
    }
    // all moving nodes are back to the normal state
    for (Integer movingIndex : MOVING) {
        ss.onChange(hosts.get(movingIndex), ApplicationState.STATUS, valueFactory.normal(newTokens.get(movingIndex)));
    }
    ss.setPartitionerUnsafe(oldPartitioner);
}
Also used : VersionedValue(org.apache.cassandra.gms.VersionedValue) TokenMetadata(org.apache.cassandra.locator.TokenMetadata) Multimap(com.google.common.collect.Multimap) HashMultimap(com.google.common.collect.HashMultimap) AbstractReplicationStrategy(org.apache.cassandra.locator.AbstractReplicationStrategy) InetAddress(java.net.InetAddress) Test(org.junit.Test)

Example 5 with HashMultimap

use of com.google.common.collect.HashMultimap in project eiger by wlloyd.

the class LeaveAndBootstrapTest method testSimultaneousMove.

/**
     * Test pending ranges and write endpoints when multiple nodes are on the move
     * simultaneously
     */
@Test
public void testSimultaneousMove() throws UnknownHostException, ConfigurationException {
    StorageService ss = StorageService.instance;
    final int RING_SIZE = 10;
    TokenMetadata tmd = ss.getTokenMetadata();
    tmd.clearUnsafe();
    IPartitioner partitioner = new RandomPartitioner();
    VersionedValue.VersionedValueFactory valueFactory = new VersionedValue.VersionedValueFactory(partitioner);
    IPartitioner oldPartitioner = ss.setPartitionerUnsafe(partitioner);
    ArrayList<Token> endpointTokens = new ArrayList<Token>();
    ArrayList<Token> keyTokens = new ArrayList<Token>();
    List<InetAddress> hosts = new ArrayList<InetAddress>();
    // create a ring or 10 nodes
    Util.createInitialRing(ss, partitioner, endpointTokens, keyTokens, hosts, RING_SIZE);
    // nodes 6, 8 and 9 leave
    final int[] LEAVING = new int[] { 6, 8, 9 };
    for (int leaving : LEAVING) ss.onChange(hosts.get(leaving), ApplicationState.STATUS, valueFactory.leaving(endpointTokens.get(leaving)));
    // boot two new nodes with keyTokens.get(5) and keyTokens.get(7)
    InetAddress boot1 = InetAddress.getByName("127.0.1.1");
    ss.onChange(boot1, ApplicationState.STATUS, valueFactory.bootstrapping(keyTokens.get(5)));
    InetAddress boot2 = InetAddress.getByName("127.0.1.2");
    ss.onChange(boot2, ApplicationState.STATUS, valueFactory.bootstrapping(keyTokens.get(7)));
    Collection<InetAddress> endpoints = null;
    /* don't require test update every time a new keyspace is added to test/conf/cassandra.yaml */
    Map<String, AbstractReplicationStrategy> tableStrategyMap = new HashMap<String, AbstractReplicationStrategy>();
    for (int i = 1; i <= 4; i++) {
        tableStrategyMap.put("Keyspace" + i, getStrategy("Keyspace" + i, tmd));
    }
    // pre-calculate the results.
    Map<String, Multimap<Token, InetAddress>> expectedEndpoints = new HashMap<String, Multimap<Token, InetAddress>>();
    expectedEndpoints.put("Keyspace1", HashMultimap.<Token, InetAddress>create());
    expectedEndpoints.get("Keyspace1").putAll(new BigIntegerToken("5"), makeAddrs("127.0.0.2"));
    expectedEndpoints.get("Keyspace1").putAll(new BigIntegerToken("15"), makeAddrs("127.0.0.3"));
    expectedEndpoints.get("Keyspace1").putAll(new BigIntegerToken("25"), makeAddrs("127.0.0.4"));
    expectedEndpoints.get("Keyspace1").putAll(new BigIntegerToken("35"), makeAddrs("127.0.0.5"));
    expectedEndpoints.get("Keyspace1").putAll(new BigIntegerToken("45"), makeAddrs("127.0.0.6"));
    expectedEndpoints.get("Keyspace1").putAll(new BigIntegerToken("55"), makeAddrs("127.0.0.7", "127.0.0.8", "127.0.1.1"));
    expectedEndpoints.get("Keyspace1").putAll(new BigIntegerToken("65"), makeAddrs("127.0.0.8"));
    expectedEndpoints.get("Keyspace1").putAll(new BigIntegerToken("75"), makeAddrs("127.0.0.9", "127.0.1.2", "127.0.0.1"));
    expectedEndpoints.get("Keyspace1").putAll(new BigIntegerToken("85"), makeAddrs("127.0.0.10", "127.0.0.1"));
    expectedEndpoints.get("Keyspace1").putAll(new BigIntegerToken("95"), makeAddrs("127.0.0.1"));
    expectedEndpoints.put("Keyspace2", HashMultimap.<Token, InetAddress>create());
    expectedEndpoints.get("Keyspace2").putAll(new BigIntegerToken("5"), makeAddrs("127.0.0.2"));
    expectedEndpoints.get("Keyspace2").putAll(new BigIntegerToken("15"), makeAddrs("127.0.0.3"));
    expectedEndpoints.get("Keyspace2").putAll(new BigIntegerToken("25"), makeAddrs("127.0.0.4"));
    expectedEndpoints.get("Keyspace2").putAll(new BigIntegerToken("35"), makeAddrs("127.0.0.5"));
    expectedEndpoints.get("Keyspace2").putAll(new BigIntegerToken("45"), makeAddrs("127.0.0.6"));
    expectedEndpoints.get("Keyspace2").putAll(new BigIntegerToken("55"), makeAddrs("127.0.0.7", "127.0.0.8", "127.0.1.1"));
    expectedEndpoints.get("Keyspace2").putAll(new BigIntegerToken("65"), makeAddrs("127.0.0.8"));
    expectedEndpoints.get("Keyspace2").putAll(new BigIntegerToken("75"), makeAddrs("127.0.0.9", "127.0.1.2", "127.0.0.1"));
    expectedEndpoints.get("Keyspace2").putAll(new BigIntegerToken("85"), makeAddrs("127.0.0.10", "127.0.0.1"));
    expectedEndpoints.get("Keyspace2").putAll(new BigIntegerToken("95"), makeAddrs("127.0.0.1"));
    expectedEndpoints.put("Keyspace3", HashMultimap.<Token, InetAddress>create());
    expectedEndpoints.get("Keyspace3").putAll(new BigIntegerToken("5"), makeAddrs("127.0.0.2", "127.0.0.3", "127.0.0.4", "127.0.0.5", "127.0.0.6"));
    expectedEndpoints.get("Keyspace3").putAll(new BigIntegerToken("15"), makeAddrs("127.0.0.3", "127.0.0.4", "127.0.0.5", "127.0.0.6", "127.0.0.7", "127.0.1.1", "127.0.0.8"));
    expectedEndpoints.get("Keyspace3").putAll(new BigIntegerToken("25"), makeAddrs("127.0.0.4", "127.0.0.5", "127.0.0.6", "127.0.0.7", "127.0.0.8", "127.0.1.2", "127.0.0.1", "127.0.1.1"));
    expectedEndpoints.get("Keyspace3").putAll(new BigIntegerToken("35"), makeAddrs("127.0.0.5", "127.0.0.6", "127.0.0.7", "127.0.0.8", "127.0.0.9", "127.0.1.2", "127.0.0.1", "127.0.0.2", "127.0.1.1"));
    expectedEndpoints.get("Keyspace3").putAll(new BigIntegerToken("45"), makeAddrs("127.0.0.6", "127.0.0.7", "127.0.0.8", "127.0.0.9", "127.0.0.10", "127.0.1.2", "127.0.0.1", "127.0.0.2", "127.0.1.1", "127.0.0.3"));
    expectedEndpoints.get("Keyspace3").putAll(new BigIntegerToken("55"), makeAddrs("127.0.0.7", "127.0.0.8", "127.0.0.9", "127.0.0.10", "127.0.0.1", "127.0.0.2", "127.0.0.3", "127.0.0.4", "127.0.1.1", "127.0.1.2"));
    expectedEndpoints.get("Keyspace3").putAll(new BigIntegerToken("65"), makeAddrs("127.0.0.8", "127.0.0.9", "127.0.0.10", "127.0.0.1", "127.0.0.2", "127.0.1.2", "127.0.0.3", "127.0.0.4"));
    expectedEndpoints.get("Keyspace3").putAll(new BigIntegerToken("75"), makeAddrs("127.0.0.9", "127.0.0.10", "127.0.0.1", "127.0.0.2", "127.0.0.3", "127.0.1.2", "127.0.0.4", "127.0.0.5"));
    expectedEndpoints.get("Keyspace3").putAll(new BigIntegerToken("85"), makeAddrs("127.0.0.10", "127.0.0.1", "127.0.0.2", "127.0.0.3", "127.0.0.4", "127.0.0.5"));
    expectedEndpoints.get("Keyspace3").putAll(new BigIntegerToken("95"), makeAddrs("127.0.0.1", "127.0.0.2", "127.0.0.3", "127.0.0.4", "127.0.0.5"));
    expectedEndpoints.put("Keyspace4", HashMultimap.<Token, InetAddress>create());
    expectedEndpoints.get("Keyspace4").putAll(new BigIntegerToken("5"), makeAddrs("127.0.0.2", "127.0.0.3", "127.0.0.4"));
    expectedEndpoints.get("Keyspace4").putAll(new BigIntegerToken("15"), makeAddrs("127.0.0.3", "127.0.0.4", "127.0.0.5"));
    expectedEndpoints.get("Keyspace4").putAll(new BigIntegerToken("25"), makeAddrs("127.0.0.4", "127.0.0.5", "127.0.0.6"));
    expectedEndpoints.get("Keyspace4").putAll(new BigIntegerToken("35"), makeAddrs("127.0.0.5", "127.0.0.6", "127.0.0.7", "127.0.1.1", "127.0.0.8"));
    expectedEndpoints.get("Keyspace4").putAll(new BigIntegerToken("45"), makeAddrs("127.0.0.6", "127.0.0.7", "127.0.0.8", "127.0.1.2", "127.0.0.1", "127.0.1.1"));
    expectedEndpoints.get("Keyspace4").putAll(new BigIntegerToken("55"), makeAddrs("127.0.0.7", "127.0.0.8", "127.0.0.9", "127.0.0.1", "127.0.0.2", "127.0.1.1", "127.0.1.2"));
    expectedEndpoints.get("Keyspace4").putAll(new BigIntegerToken("65"), makeAddrs("127.0.0.8", "127.0.0.9", "127.0.0.10", "127.0.1.2", "127.0.0.1", "127.0.0.2"));
    expectedEndpoints.get("Keyspace4").putAll(new BigIntegerToken("75"), makeAddrs("127.0.0.9", "127.0.0.10", "127.0.0.1", "127.0.1.2", "127.0.0.2", "127.0.0.3"));
    expectedEndpoints.get("Keyspace4").putAll(new BigIntegerToken("85"), makeAddrs("127.0.0.10", "127.0.0.1", "127.0.0.2", "127.0.0.3"));
    expectedEndpoints.get("Keyspace4").putAll(new BigIntegerToken("95"), makeAddrs("127.0.0.1", "127.0.0.2", "127.0.0.3"));
    for (Map.Entry<String, AbstractReplicationStrategy> tableStrategy : tableStrategyMap.entrySet()) {
        String table = tableStrategy.getKey();
        AbstractReplicationStrategy strategy = tableStrategy.getValue();
        for (int i = 0; i < keyTokens.size(); i++) {
            endpoints = tmd.getWriteEndpoints(keyTokens.get(i), table, strategy.getNaturalEndpoints(keyTokens.get(i)));
            assertTrue(expectedEndpoints.get(table).get(keyTokens.get(i)).size() == endpoints.size());
            assertTrue(expectedEndpoints.get(table).get(keyTokens.get(i)).containsAll(endpoints));
        }
        // just to be sure that things still work according to the old tests, run them:
        if (strategy.getReplicationFactor() != 3)
            continue;
        // tokens 5, 15 and 25 should go three nodes
        for (int i = 0; i < 3; ++i) {
            endpoints = tmd.getWriteEndpoints(keyTokens.get(i), table, strategy.getNaturalEndpoints(keyTokens.get(i)));
            assertTrue(endpoints.size() == 3);
            assertTrue(endpoints.contains(hosts.get(i + 1)));
            assertTrue(endpoints.contains(hosts.get(i + 2)));
            assertTrue(endpoints.contains(hosts.get(i + 3)));
        }
        // token 35 should go to nodes 4, 5, 6, 7 and boot1
        endpoints = tmd.getWriteEndpoints(keyTokens.get(3), table, strategy.getNaturalEndpoints(keyTokens.get(3)));
        assertTrue(endpoints.size() == 5);
        assertTrue(endpoints.contains(hosts.get(4)));
        assertTrue(endpoints.contains(hosts.get(5)));
        assertTrue(endpoints.contains(hosts.get(6)));
        assertTrue(endpoints.contains(hosts.get(7)));
        assertTrue(endpoints.contains(boot1));
        // token 45 should go to nodes 5, 6, 7, 0, boot1 and boot2
        endpoints = tmd.getWriteEndpoints(keyTokens.get(4), table, strategy.getNaturalEndpoints(keyTokens.get(4)));
        assertTrue(endpoints.size() == 6);
        assertTrue(endpoints.contains(hosts.get(5)));
        assertTrue(endpoints.contains(hosts.get(6)));
        assertTrue(endpoints.contains(hosts.get(7)));
        assertTrue(endpoints.contains(hosts.get(0)));
        assertTrue(endpoints.contains(boot1));
        assertTrue(endpoints.contains(boot2));
        // token 55 should go to nodes 6, 7, 8, 0, 1, boot1 and boot2
        endpoints = tmd.getWriteEndpoints(keyTokens.get(5), table, strategy.getNaturalEndpoints(keyTokens.get(5)));
        assertTrue(endpoints.size() == 7);
        assertTrue(endpoints.contains(hosts.get(6)));
        assertTrue(endpoints.contains(hosts.get(7)));
        assertTrue(endpoints.contains(hosts.get(8)));
        assertTrue(endpoints.contains(hosts.get(0)));
        assertTrue(endpoints.contains(hosts.get(1)));
        assertTrue(endpoints.contains(boot1));
        assertTrue(endpoints.contains(boot2));
        // token 65 should go to nodes 7, 8, 9, 0, 1 and boot2
        endpoints = tmd.getWriteEndpoints(keyTokens.get(6), table, strategy.getNaturalEndpoints(keyTokens.get(6)));
        assertTrue(endpoints.size() == 6);
        assertTrue(endpoints.contains(hosts.get(7)));
        assertTrue(endpoints.contains(hosts.get(8)));
        assertTrue(endpoints.contains(hosts.get(9)));
        assertTrue(endpoints.contains(hosts.get(0)));
        assertTrue(endpoints.contains(hosts.get(1)));
        assertTrue(endpoints.contains(boot2));
        // token 75 should to go nodes 8, 9, 0, 1, 2 and boot2
        endpoints = tmd.getWriteEndpoints(keyTokens.get(7), table, strategy.getNaturalEndpoints(keyTokens.get(7)));
        assertTrue(endpoints.size() == 6);
        assertTrue(endpoints.contains(hosts.get(8)));
        assertTrue(endpoints.contains(hosts.get(9)));
        assertTrue(endpoints.contains(hosts.get(0)));
        assertTrue(endpoints.contains(hosts.get(1)));
        assertTrue(endpoints.contains(hosts.get(2)));
        assertTrue(endpoints.contains(boot2));
        // token 85 should go to nodes 9, 0, 1 and 2
        endpoints = tmd.getWriteEndpoints(keyTokens.get(8), table, strategy.getNaturalEndpoints(keyTokens.get(8)));
        assertTrue(endpoints.size() == 4);
        assertTrue(endpoints.contains(hosts.get(9)));
        assertTrue(endpoints.contains(hosts.get(0)));
        assertTrue(endpoints.contains(hosts.get(1)));
        assertTrue(endpoints.contains(hosts.get(2)));
        // token 95 should go to nodes 0, 1 and 2
        endpoints = tmd.getWriteEndpoints(keyTokens.get(9), table, strategy.getNaturalEndpoints(keyTokens.get(9)));
        assertTrue(endpoints.size() == 3);
        assertTrue(endpoints.contains(hosts.get(0)));
        assertTrue(endpoints.contains(hosts.get(1)));
        assertTrue(endpoints.contains(hosts.get(2)));
    }
    // Now finish node 6 and node 9 leaving, as well as boot1 (after this node 8 is still
    // leaving and boot2 in progress
    ss.onChange(hosts.get(LEAVING[0]), ApplicationState.STATUS, valueFactory.left(endpointTokens.get(LEAVING[0]), Gossiper.computeExpireTime()));
    ss.onChange(hosts.get(LEAVING[2]), ApplicationState.STATUS, valueFactory.left(endpointTokens.get(LEAVING[2]), Gossiper.computeExpireTime()));
    ss.onChange(boot1, ApplicationState.STATUS, valueFactory.normal(keyTokens.get(5)));
    // adjust precalcuated results.  this changes what the epected endpoints are.
    expectedEndpoints.get("Keyspace1").get(new BigIntegerToken("55")).removeAll(makeAddrs("127.0.0.7", "127.0.0.8"));
    expectedEndpoints.get("Keyspace1").get(new BigIntegerToken("85")).removeAll(makeAddrs("127.0.0.10"));
    expectedEndpoints.get("Keyspace2").get(new BigIntegerToken("55")).removeAll(makeAddrs("127.0.0.7", "127.0.0.8"));
    expectedEndpoints.get("Keyspace2").get(new BigIntegerToken("85")).removeAll(makeAddrs("127.0.0.10"));
    expectedEndpoints.get("Keyspace3").get(new BigIntegerToken("15")).removeAll(makeAddrs("127.0.0.7", "127.0.0.8"));
    expectedEndpoints.get("Keyspace3").get(new BigIntegerToken("25")).removeAll(makeAddrs("127.0.0.7", "127.0.1.2", "127.0.0.1"));
    expectedEndpoints.get("Keyspace3").get(new BigIntegerToken("35")).removeAll(makeAddrs("127.0.0.7", "127.0.0.2"));
    expectedEndpoints.get("Keyspace3").get(new BigIntegerToken("45")).removeAll(makeAddrs("127.0.0.7", "127.0.0.10", "127.0.0.3"));
    expectedEndpoints.get("Keyspace3").get(new BigIntegerToken("55")).removeAll(makeAddrs("127.0.0.7", "127.0.0.10", "127.0.0.4"));
    expectedEndpoints.get("Keyspace3").get(new BigIntegerToken("65")).removeAll(makeAddrs("127.0.0.10"));
    expectedEndpoints.get("Keyspace3").get(new BigIntegerToken("75")).removeAll(makeAddrs("127.0.0.10"));
    expectedEndpoints.get("Keyspace3").get(new BigIntegerToken("85")).removeAll(makeAddrs("127.0.0.10"));
    expectedEndpoints.get("Keyspace4").get(new BigIntegerToken("35")).removeAll(makeAddrs("127.0.0.7", "127.0.0.8"));
    expectedEndpoints.get("Keyspace4").get(new BigIntegerToken("45")).removeAll(makeAddrs("127.0.0.7", "127.0.1.2", "127.0.0.1"));
    expectedEndpoints.get("Keyspace4").get(new BigIntegerToken("55")).removeAll(makeAddrs("127.0.0.2", "127.0.0.7"));
    expectedEndpoints.get("Keyspace4").get(new BigIntegerToken("65")).removeAll(makeAddrs("127.0.0.10"));
    expectedEndpoints.get("Keyspace4").get(new BigIntegerToken("75")).removeAll(makeAddrs("127.0.0.10"));
    expectedEndpoints.get("Keyspace4").get(new BigIntegerToken("85")).removeAll(makeAddrs("127.0.0.10"));
    for (Map.Entry<String, AbstractReplicationStrategy> tableStrategy : tableStrategyMap.entrySet()) {
        String table = tableStrategy.getKey();
        AbstractReplicationStrategy strategy = tableStrategy.getValue();
        for (int i = 0; i < keyTokens.size(); i++) {
            endpoints = tmd.getWriteEndpoints(keyTokens.get(i), table, strategy.getNaturalEndpoints(keyTokens.get(i)));
            assertTrue(expectedEndpoints.get(table).get(keyTokens.get(i)).size() == endpoints.size());
            assertTrue(expectedEndpoints.get(table).get(keyTokens.get(i)).containsAll(endpoints));
        }
        if (strategy.getReplicationFactor() != 3)
            continue;
        // tokens 5, 15 and 25 should go three nodes
        for (int i = 0; i < 3; ++i) {
            endpoints = tmd.getWriteEndpoints(keyTokens.get(i), table, strategy.getNaturalEndpoints(keyTokens.get(i)));
            assertTrue(endpoints.size() == 3);
            assertTrue(endpoints.contains(hosts.get(i + 1)));
            assertTrue(endpoints.contains(hosts.get(i + 2)));
            assertTrue(endpoints.contains(hosts.get(i + 3)));
        }
        // token 35 goes to nodes 4, 5 and boot1
        endpoints = tmd.getWriteEndpoints(keyTokens.get(3), table, strategy.getNaturalEndpoints(keyTokens.get(3)));
        assertTrue(endpoints.size() == 3);
        assertTrue(endpoints.contains(hosts.get(4)));
        assertTrue(endpoints.contains(hosts.get(5)));
        assertTrue(endpoints.contains(boot1));
        // token 45 goes to nodes 5, boot1 and node7
        endpoints = tmd.getWriteEndpoints(keyTokens.get(4), table, strategy.getNaturalEndpoints(keyTokens.get(4)));
        assertTrue(endpoints.size() == 3);
        assertTrue(endpoints.contains(hosts.get(5)));
        assertTrue(endpoints.contains(boot1));
        assertTrue(endpoints.contains(hosts.get(7)));
        // token 55 goes to boot1, 7, boot2, 8 and 0
        endpoints = tmd.getWriteEndpoints(keyTokens.get(5), table, strategy.getNaturalEndpoints(keyTokens.get(5)));
        assertTrue(endpoints.size() == 5);
        assertTrue(endpoints.contains(boot1));
        assertTrue(endpoints.contains(hosts.get(7)));
        assertTrue(endpoints.contains(boot2));
        assertTrue(endpoints.contains(hosts.get(8)));
        assertTrue(endpoints.contains(hosts.get(0)));
        // token 65 goes to nodes 7, boot2, 8, 0 and 1
        endpoints = tmd.getWriteEndpoints(keyTokens.get(6), table, strategy.getNaturalEndpoints(keyTokens.get(6)));
        assertTrue(endpoints.size() == 5);
        assertTrue(endpoints.contains(hosts.get(7)));
        assertTrue(endpoints.contains(boot2));
        assertTrue(endpoints.contains(hosts.get(8)));
        assertTrue(endpoints.contains(hosts.get(0)));
        assertTrue(endpoints.contains(hosts.get(1)));
        // token 75 goes to nodes boot2, 8, 0, 1 and 2
        endpoints = tmd.getWriteEndpoints(keyTokens.get(7), table, strategy.getNaturalEndpoints(keyTokens.get(7)));
        assertTrue(endpoints.size() == 5);
        assertTrue(endpoints.contains(boot2));
        assertTrue(endpoints.contains(hosts.get(8)));
        assertTrue(endpoints.contains(hosts.get(0)));
        assertTrue(endpoints.contains(hosts.get(1)));
        assertTrue(endpoints.contains(hosts.get(2)));
        // token 85 goes to nodes 0, 1 and 2
        endpoints = tmd.getWriteEndpoints(keyTokens.get(8), table, strategy.getNaturalEndpoints(keyTokens.get(8)));
        assertTrue(endpoints.size() == 3);
        assertTrue(endpoints.contains(hosts.get(0)));
        assertTrue(endpoints.contains(hosts.get(1)));
        assertTrue(endpoints.contains(hosts.get(2)));
        // token 95 goes to nodes 0, 1 and 2
        endpoints = tmd.getWriteEndpoints(keyTokens.get(9), table, strategy.getNaturalEndpoints(keyTokens.get(9)));
        assertTrue(endpoints.size() == 3);
        assertTrue(endpoints.contains(hosts.get(0)));
        assertTrue(endpoints.contains(hosts.get(1)));
        assertTrue(endpoints.contains(hosts.get(2)));
    }
    ss.setPartitionerUnsafe(oldPartitioner);
}
Also used : VersionedValue(org.apache.cassandra.gms.VersionedValue) TokenMetadata(org.apache.cassandra.locator.TokenMetadata) Multimap(com.google.common.collect.Multimap) HashMultimap(com.google.common.collect.HashMultimap) AbstractReplicationStrategy(org.apache.cassandra.locator.AbstractReplicationStrategy) InetAddress(java.net.InetAddress) Test(org.junit.Test)

Aggregations

HashMultimap (com.google.common.collect.HashMultimap)7 Multimap (com.google.common.collect.Multimap)4 InetAddress (java.net.InetAddress)4 VersionedValue (org.apache.cassandra.gms.VersionedValue)4 AbstractReplicationStrategy (org.apache.cassandra.locator.AbstractReplicationStrategy)4 TokenMetadata (org.apache.cassandra.locator.TokenMetadata)4 Test (org.junit.Test)4 AndroidLibrary (com.android.builder.model.AndroidLibrary)1 AndroidLibraryDescription (com.facebook.buck.android.AndroidLibraryDescription)1 DepsForBuildFiles (com.facebook.buck.autodeps.DepsForBuildFiles)1 DependencyType (com.facebook.buck.autodeps.DepsForBuildFiles.DependencyType)1 BuckConfig (com.facebook.buck.cli.BuckConfig)1 JavaBuckConfig (com.facebook.buck.jvm.java.JavaBuckConfig)1 JavaFileParser (com.facebook.buck.jvm.java.JavaFileParser)1 JavaLibraryDescription (com.facebook.buck.jvm.java.JavaLibraryDescription)1 JavaTestDescription (com.facebook.buck.jvm.java.JavaTestDescription)1 JavacOptions (com.facebook.buck.jvm.java.JavacOptions)1 PrebuiltJarDescription (com.facebook.buck.jvm.java.PrebuiltJarDescription)1 BuildTarget (com.facebook.buck.model.BuildTarget)1 BuildTargetParser (com.facebook.buck.parser.BuildTargetParser)1