Search in sources :

Example 26 with Multimap

use of com.google.common.collect.Multimap in project hbase by apache.

the class LoadIncrementalHFiles method groupOrSplitPhase.

/**
   * @param table the table to load into
   * @param pool the ExecutorService
   * @param queue the queue for LoadQueueItem
   * @param startEndKeys start and end keys
   * @return A map that groups LQI by likely bulk load region targets and Set of missing hfiles.
   */
private Pair<Multimap<ByteBuffer, LoadQueueItem>, Set<String>> groupOrSplitPhase(final Table table, ExecutorService pool, Deque<LoadQueueItem> queue, final Pair<byte[][], byte[][]> startEndKeys) throws IOException {
    // <region start key, LQI> need synchronized only within this scope of this
    // phase because of the puts that happen in futures.
    Multimap<ByteBuffer, LoadQueueItem> rgs = HashMultimap.create();
    final Multimap<ByteBuffer, LoadQueueItem> regionGroups = Multimaps.synchronizedMultimap(rgs);
    Set<String> missingHFiles = new HashSet<>();
    Pair<Multimap<ByteBuffer, LoadQueueItem>, Set<String>> pair = new Pair<>(regionGroups, missingHFiles);
    // drain LQIs and figure out bulk load groups
    Set<Future<Pair<List<LoadQueueItem>, String>>> splittingFutures = new HashSet<>();
    while (!queue.isEmpty()) {
        final LoadQueueItem item = queue.remove();
        final Callable<Pair<List<LoadQueueItem>, String>> call = new Callable<Pair<List<LoadQueueItem>, String>>() {

            @Override
            public Pair<List<LoadQueueItem>, String> call() throws Exception {
                Pair<List<LoadQueueItem>, String> splits = groupOrSplit(regionGroups, item, table, startEndKeys);
                return splits;
            }
        };
        splittingFutures.add(pool.submit(call));
    }
    // we can attempt the atomic loads.
    for (Future<Pair<List<LoadQueueItem>, String>> lqis : splittingFutures) {
        try {
            Pair<List<LoadQueueItem>, String> splits = lqis.get();
            if (splits != null) {
                if (splits.getFirst() != null) {
                    queue.addAll(splits.getFirst());
                } else {
                    missingHFiles.add(splits.getSecond());
                }
            }
        } catch (ExecutionException e1) {
            Throwable t = e1.getCause();
            if (t instanceof IOException) {
                LOG.error("IOException during splitting", e1);
                // would have been thrown if not parallelized,
                throw (IOException) t;
            }
            LOG.error("Unexpected execution exception during splitting", e1);
            throw new IllegalStateException(t);
        } catch (InterruptedException e1) {
            LOG.error("Unexpected interrupted exception during splitting", e1);
            throw (InterruptedIOException) new InterruptedIOException().initCause(e1);
        }
    }
    return pair;
}
Also used : InterruptedIOException(java.io.InterruptedIOException) Set(java.util.Set) HashSet(java.util.HashSet) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer) ClientServiceCallable(org.apache.hadoop.hbase.client.ClientServiceCallable) Callable(java.util.concurrent.Callable) HashMultimap(com.google.common.collect.HashMultimap) Multimap(com.google.common.collect.Multimap) Future(java.util.concurrent.Future) List(java.util.List) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) ExecutionException(java.util.concurrent.ExecutionException) HashSet(java.util.HashSet) Pair(org.apache.hadoop.hbase.util.Pair)

Example 27 with Multimap

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

the class MoveTest method testSimultaneousMove.

/*
     * Test 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();
    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[] 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");
    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))));
    PendingRangeCalculatorService.instance.blockUntilFinished();
    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))));
    PendingRangeCalculatorService.instance.blockUntilFinished();
    // 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("MoveTestKeyspace" + i, getStrategy("MoveTestKeyspace" + i, tmd));
    }
    /**
        *  Keyspace1 & Keyspace2 RF=1
        *  {
        *      /127.0.0.1=[(97,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,67]],
        *      /127.0.0.8=[(67,70]],
        *      /127.0.0.9=[(70,87]],
        *      /127.0.0.10=[(87,97]]
        *  }
        */
    Multimap<InetAddress, Range<Token>> keyspace1ranges = keyspaceStrategyMap.get(Simple_RF1_KeyspaceName).getAddressRanges();
    Collection<Range<Token>> ranges1 = keyspace1ranges.get(InetAddress.getByName("127.0.0.1"));
    assertEquals(1, collectionSize(ranges1));
    assertEquals(generateRange(97, 0), ranges1.iterator().next());
    Collection<Range<Token>> ranges2 = keyspace1ranges.get(InetAddress.getByName("127.0.0.2"));
    assertEquals(1, collectionSize(ranges2));
    assertEquals(generateRange(0, 10), ranges2.iterator().next());
    Collection<Range<Token>> ranges3 = keyspace1ranges.get(InetAddress.getByName("127.0.0.3"));
    assertEquals(1, collectionSize(ranges3));
    assertEquals(generateRange(10, 20), ranges3.iterator().next());
    Collection<Range<Token>> ranges4 = keyspace1ranges.get(InetAddress.getByName("127.0.0.4"));
    assertEquals(1, collectionSize(ranges4));
    assertEquals(generateRange(20, 30), ranges4.iterator().next());
    Collection<Range<Token>> ranges5 = keyspace1ranges.get(InetAddress.getByName("127.0.0.5"));
    assertEquals(1, collectionSize(ranges5));
    assertEquals(generateRange(30, 40), ranges5.iterator().next());
    Collection<Range<Token>> ranges6 = keyspace1ranges.get(InetAddress.getByName("127.0.0.6"));
    assertEquals(1, collectionSize(ranges6));
    assertEquals(generateRange(40, 50), ranges6.iterator().next());
    Collection<Range<Token>> ranges7 = keyspace1ranges.get(InetAddress.getByName("127.0.0.7"));
    assertEquals(1, collectionSize(ranges7));
    assertEquals(generateRange(50, 67), ranges7.iterator().next());
    Collection<Range<Token>> ranges8 = keyspace1ranges.get(InetAddress.getByName("127.0.0.8"));
    assertEquals(1, collectionSize(ranges8));
    assertEquals(generateRange(67, 70), ranges8.iterator().next());
    Collection<Range<Token>> ranges9 = keyspace1ranges.get(InetAddress.getByName("127.0.0.9"));
    assertEquals(1, collectionSize(ranges9));
    assertEquals(generateRange(70, 87), ranges9.iterator().next());
    Collection<Range<Token>> ranges10 = keyspace1ranges.get(InetAddress.getByName("127.0.0.10"));
    assertEquals(1, collectionSize(ranges10));
    assertEquals(generateRange(87, 97), ranges10.iterator().next());
    /**
        * Keyspace3 RF=5
        * {
        *      /127.0.0.1=[(97,0], (70,87], (50,67], (87,97], (67,70]],
        *      /127.0.0.2=[(97,0], (70,87], (87,97], (0,10], (67,70]],
        *      /127.0.0.3=[(97,0], (70,87], (87,97], (0,10], (10,20]],
        *      /127.0.0.4=[(97,0], (20,30], (87,97], (0,10], (10,20]],
        *      /127.0.0.5=[(97,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,67], (20,30], (10,20]],
        *      /127.0.0.8=[(40,50], (30,40], (50,67], (20,30], (67,70]],
        *      /127.0.0.9=[(40,50], (70,87], (30,40], (50,67], (67,70]],
        *      /127.0.0.10=[(40,50], (70,87], (50,67], (87,97], (67,70]]
        * }
        */
    Multimap<InetAddress, Range<Token>> keyspace3ranges = keyspaceStrategyMap.get(KEYSPACE3).getAddressRanges();
    ranges1 = keyspace3ranges.get(InetAddress.getByName("127.0.0.1"));
    assertEquals(collectionSize(ranges1), 5);
    assertTrue(ranges1.equals(generateRanges(97, 0, 70, 87, 50, 67, 87, 97, 67, 70)));
    ranges2 = keyspace3ranges.get(InetAddress.getByName("127.0.0.2"));
    assertEquals(collectionSize(ranges2), 5);
    assertTrue(ranges2.equals(generateRanges(97, 0, 70, 87, 87, 97, 0, 10, 67, 70)));
    ranges3 = keyspace3ranges.get(InetAddress.getByName("127.0.0.3"));
    assertEquals(collectionSize(ranges3), 5);
    assertTrue(ranges3.equals(generateRanges(97, 0, 70, 87, 87, 97, 0, 10, 10, 20)));
    ranges4 = keyspace3ranges.get(InetAddress.getByName("127.0.0.4"));
    assertEquals(collectionSize(ranges4), 5);
    assertTrue(ranges4.equals(generateRanges(97, 0, 20, 30, 87, 97, 0, 10, 10, 20)));
    ranges5 = keyspace3ranges.get(InetAddress.getByName("127.0.0.5"));
    assertEquals(collectionSize(ranges5), 5);
    assertTrue(ranges5.equals(generateRanges(97, 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, 67, 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, 67, 20, 30, 67, 70)));
    ranges9 = keyspace3ranges.get(InetAddress.getByName("127.0.0.9"));
    assertEquals(collectionSize(ranges9), 5);
    assertTrue(ranges9.equals(generateRanges(40, 50, 70, 87, 30, 40, 50, 67, 67, 70)));
    ranges10 = keyspace3ranges.get(InetAddress.getByName("127.0.0.10"));
    assertEquals(collectionSize(ranges10), 5);
    assertTrue(ranges10.equals(generateRanges(40, 50, 70, 87, 50, 67, 87, 97, 67, 70)));
    /**
         * Keyspace4 RF=3
         * {
         *      /127.0.0.1=[(97,0], (70,87], (87,97]],
         *      /127.0.0.2=[(97,0], (87,97], (0,10]],
         *      /127.0.0.3=[(97,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,67]],
         *      /127.0.0.8=[(40,50], (50,67], (67,70]],
         *      /127.0.0.9=[(70,87], (50,67], (67,70]],
         *      /127.0.0.10=[(70,87], (87,97], (67,70]]
         *  }
         */
    Multimap<InetAddress, Range<Token>> keyspace4ranges = keyspaceStrategyMap.get(Simple_RF3_KeyspaceName).getAddressRanges();
    ranges1 = keyspace4ranges.get(InetAddress.getByName("127.0.0.1"));
    assertEquals(collectionSize(ranges1), 3);
    assertTrue(ranges1.equals(generateRanges(97, 0, 70, 87, 87, 97)));
    ranges2 = keyspace4ranges.get(InetAddress.getByName("127.0.0.2"));
    assertEquals(collectionSize(ranges2), 3);
    assertTrue(ranges2.equals(generateRanges(97, 0, 87, 97, 0, 10)));
    ranges3 = keyspace4ranges.get(InetAddress.getByName("127.0.0.3"));
    assertEquals(collectionSize(ranges3), 3);
    assertTrue(ranges3.equals(generateRanges(97, 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, 67)));
    ranges8 = keyspace4ranges.get(InetAddress.getByName("127.0.0.8"));
    assertEquals(collectionSize(ranges8), 3);
    assertTrue(ranges8.equals(generateRanges(40, 50, 50, 67, 67, 70)));
    ranges9 = keyspace4ranges.get(InetAddress.getByName("127.0.0.9"));
    assertEquals(collectionSize(ranges9), 3);
    assertTrue(ranges9.equals(generateRanges(70, 87, 50, 67, 67, 70)));
    ranges10 = keyspace4ranges.get(InetAddress.getByName("127.0.0.10"));
    assertEquals(collectionSize(ranges10), 3);
    assertTrue(ranges10.equals(generateRanges(70, 87, 87, 97, 67, 70)));
    // pre-calculate the results.
    Map<String, Multimap<Token, InetAddress>> expectedEndpoints = new HashMap<String, Multimap<Token, InetAddress>>();
    expectedEndpoints.put(Simple_RF1_KeyspaceName, HashMultimap.<Token, InetAddress>create());
    expectedEndpoints.get(Simple_RF1_KeyspaceName).putAll(new BigIntegerToken("5"), makeAddrs("127.0.0.2"));
    expectedEndpoints.get(Simple_RF1_KeyspaceName).putAll(new BigIntegerToken("15"), makeAddrs("127.0.0.3"));
    expectedEndpoints.get(Simple_RF1_KeyspaceName).putAll(new BigIntegerToken("25"), makeAddrs("127.0.0.4"));
    expectedEndpoints.get(Simple_RF1_KeyspaceName).putAll(new BigIntegerToken("35"), makeAddrs("127.0.0.5"));
    expectedEndpoints.get(Simple_RF1_KeyspaceName).putAll(new BigIntegerToken("45"), makeAddrs("127.0.0.6"));
    expectedEndpoints.get(Simple_RF1_KeyspaceName).putAll(new BigIntegerToken("55"), makeAddrs("127.0.0.7", "127.0.1.1"));
    expectedEndpoints.get(Simple_RF1_KeyspaceName).putAll(new BigIntegerToken("65"), makeAddrs("127.0.0.7"));
    expectedEndpoints.get(Simple_RF1_KeyspaceName).putAll(new BigIntegerToken("75"), makeAddrs("127.0.0.9", "127.0.1.2"));
    expectedEndpoints.get(Simple_RF1_KeyspaceName).putAll(new BigIntegerToken("85"), makeAddrs("127.0.0.9"));
    expectedEndpoints.get(Simple_RF1_KeyspaceName).putAll(new BigIntegerToken("95"), makeAddrs("127.0.0.10"));
    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.7"));
    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.9"));
    expectedEndpoints.get(KEYSPACE2).putAll(new BigIntegerToken("95"), makeAddrs("127.0.0.10"));
    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.7", "127.0.0.8", "127.0.0.9", "127.0.0.10", "127.0.0.1", "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.9", "127.0.0.10", "127.0.0.1", "127.0.0.2", "127.0.0.3"));
    expectedEndpoints.get(KEYSPACE3).putAll(new BigIntegerToken("95"), makeAddrs("127.0.0.10", "127.0.0.1", "127.0.0.2", "127.0.0.3", "127.0.0.4"));
    expectedEndpoints.put(Simple_RF3_KeyspaceName, HashMultimap.<Token, InetAddress>create());
    expectedEndpoints.get(Simple_RF3_KeyspaceName).putAll(new BigIntegerToken("5"), makeAddrs("127.0.0.2", "127.0.0.3", "127.0.0.4"));
    expectedEndpoints.get(Simple_RF3_KeyspaceName).putAll(new BigIntegerToken("15"), makeAddrs("127.0.0.3", "127.0.0.4", "127.0.0.5"));
    expectedEndpoints.get(Simple_RF3_KeyspaceName).putAll(new BigIntegerToken("25"), makeAddrs("127.0.0.4", "127.0.0.5", "127.0.0.6"));
    expectedEndpoints.get(Simple_RF3_KeyspaceName).putAll(new BigIntegerToken("35"), makeAddrs("127.0.0.5", "127.0.0.6", "127.0.0.7", "127.0.1.1"));
    expectedEndpoints.get(Simple_RF3_KeyspaceName).putAll(new BigIntegerToken("45"), makeAddrs("127.0.0.6", "127.0.0.7", "127.0.0.8", "127.0.1.1"));
    expectedEndpoints.get(Simple_RF3_KeyspaceName).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(Simple_RF3_KeyspaceName).putAll(new BigIntegerToken("65"), makeAddrs("127.0.0.7", "127.0.0.8", "127.0.0.9", "127.0.1.2"));
    expectedEndpoints.get(Simple_RF3_KeyspaceName).putAll(new BigIntegerToken("75"), makeAddrs("127.0.0.9", "127.0.0.10", "127.0.0.1", "127.0.1.2"));
    expectedEndpoints.get(Simple_RF3_KeyspaceName).putAll(new BigIntegerToken("85"), makeAddrs("127.0.0.9", "127.0.0.10", "127.0.0.1"));
    expectedEndpoints.get(Simple_RF3_KeyspaceName).putAll(new BigIntegerToken("95"), makeAddrs("127.0.0.10", "127.0.0.1", "127.0.0.2"));
    for (Map.Entry<String, AbstractReplicationStrategy> keyspaceStrategy : keyspaceStrategyMap.entrySet()) {
        String keyspaceName = keyspaceStrategy.getKey();
        AbstractReplicationStrategy strategy = keyspaceStrategy.getValue();
        for (Token token : keyTokens) {
            endpoints = tmd.getWriteEndpoints(token, keyspaceName, strategy.getNaturalEndpoints(token));
            assertEquals(expectedEndpoints.get(keyspaceName).get(token).size(), endpoints.size());
            assertTrue(expectedEndpoints.get(keyspaceName).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), 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 and boot1
        endpoints = tmd.getWriteEndpoints(keyTokens.get(3), keyspaceName, strategy.getNaturalEndpoints(keyTokens.get(3)));
        assertEquals(4, endpoints.size());
        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), keyspaceName, strategy.getNaturalEndpoints(keyTokens.get(4)));
        assertEquals(4, endpoints.size());
        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), keyspaceName, strategy.getNaturalEndpoints(keyTokens.get(5)));
        assertEquals(5, endpoints.size());
        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 6, 7, 8 and boot2
        endpoints = tmd.getWriteEndpoints(keyTokens.get(6), keyspaceName, strategy.getNaturalEndpoints(keyTokens.get(6)));
        assertEquals(4, endpoints.size());
        assertTrue(endpoints.contains(hosts.get(6)));
        assertTrue(endpoints.contains(hosts.get(7)));
        assertTrue(endpoints.contains(hosts.get(8)));
        assertTrue(endpoints.contains(boot2));
        // token 75 should to go nodes 8, 9, 0 and boot2
        endpoints = tmd.getWriteEndpoints(keyTokens.get(7), keyspaceName, strategy.getNaturalEndpoints(keyTokens.get(7)));
        assertEquals(4, endpoints.size());
        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 8, 9 and 0
        endpoints = tmd.getWriteEndpoints(keyTokens.get(8), keyspaceName, strategy.getNaturalEndpoints(keyTokens.get(8)));
        assertEquals(3, endpoints.size());
        assertTrue(endpoints.contains(hosts.get(8)));
        assertTrue(endpoints.contains(hosts.get(9)));
        assertTrue(endpoints.contains(hosts.get(0)));
        // token 95 should go to nodes 9, 0 and 1
        endpoints = tmd.getWriteEndpoints(keyTokens.get(9), keyspaceName, strategy.getNaturalEndpoints(keyTokens.get(9)));
        assertEquals(3, endpoints.size());
        assertTrue(endpoints.contains(hosts.get(9)));
        assertTrue(endpoints.contains(hosts.get(0)));
        assertTrue(endpoints.contains(hosts.get(1)));
    }
    // all moving nodes are back to the normal state
    for (Integer movingIndex : MOVING) {
        ss.onChange(hosts.get(movingIndex), ApplicationState.STATUS, valueFactory.normal(Collections.singleton(newTokens.get(movingIndex))));
    }
}
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) Range(org.apache.cassandra.dht.Range) 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 28 with Multimap

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

the class NetworkTopologyStrategy method calculateNaturalEndpoints.

/**
     * calculate endpoints in one pass through the tokens by tracking our progress in each DC.
     */
public List<InetAddress> calculateNaturalEndpoints(Token searchToken, TokenMetadata tokenMetadata) {
    // we want to preserve insertion order so that the first added endpoint becomes primary
    Set<InetAddress> replicas = new LinkedHashSet<>();
    Set<Pair<String, String>> seenRacks = new HashSet<>();
    Topology topology = tokenMetadata.getTopology();
    // all endpoints in each DC, so we can check when we have exhausted all the members of a DC
    Multimap<String, InetAddress> allEndpoints = topology.getDatacenterEndpoints();
    // all racks in a DC so we can check when we have exhausted all racks in a DC
    Map<String, Multimap<String, InetAddress>> racks = topology.getDatacenterRacks();
    assert !allEndpoints.isEmpty() && !racks.isEmpty() : "not aware of any cluster members";
    int dcsToFill = 0;
    Map<String, DatacenterEndpoints> dcs = new HashMap<>(datacenters.size() * 2);
    // Create a DatacenterEndpoints object for each non-empty DC.
    for (Map.Entry<String, Integer> en : datacenters.entrySet()) {
        String dc = en.getKey();
        int rf = en.getValue();
        int nodeCount = sizeOrZero(allEndpoints.get(dc));
        if (rf <= 0 || nodeCount <= 0)
            continue;
        DatacenterEndpoints dcEndpoints = new DatacenterEndpoints(rf, sizeOrZero(racks.get(dc)), nodeCount, replicas, seenRacks);
        dcs.put(dc, dcEndpoints);
        ++dcsToFill;
    }
    Iterator<Token> tokenIter = TokenMetadata.ringIterator(tokenMetadata.sortedTokens(), searchToken, false);
    while (dcsToFill > 0 && tokenIter.hasNext()) {
        Token next = tokenIter.next();
        InetAddress ep = tokenMetadata.getEndpoint(next);
        Pair<String, String> location = topology.getLocation(ep);
        DatacenterEndpoints dcEndpoints = dcs.get(location.left);
        if (dcEndpoints != null && dcEndpoints.addEndpointAndCheckIfDone(ep, location))
            --dcsToFill;
    }
    return new ArrayList<>(replicas);
}
Also used : Token(org.apache.cassandra.dht.Token) Topology(org.apache.cassandra.locator.TokenMetadata.Topology) Multimap(com.google.common.collect.Multimap) InetAddress(java.net.InetAddress) Pair(org.apache.cassandra.utils.Pair)

Example 29 with Multimap

use of com.google.common.collect.Multimap in project crate by crate.

the class SQLTransportIntegrationTest method assertNoJobExecutionContextAreLeftOpen.

@After
public void assertNoJobExecutionContextAreLeftOpen() throws Exception {
    final Field activeContexts = JobContextService.class.getDeclaredField("activeContexts");
    final Field activeOperationsSb = TransportShardAction.class.getDeclaredField("activeOperations");
    activeContexts.setAccessible(true);
    activeOperationsSb.setAccessible(true);
    try {
        assertBusy(new Runnable() {

            @Override
            public void run() {
                for (JobContextService jobContextService : internalCluster().getInstances(JobContextService.class)) {
                    try {
                        //noinspection unchecked
                        Map<UUID, JobExecutionContext> contexts = (Map<UUID, JobExecutionContext>) activeContexts.get(jobContextService);
                        assertThat(contexts.size(), is(0));
                    } catch (IllegalAccessException e) {
                        throw Throwables.propagate(e);
                    }
                }
                for (TransportShardUpsertAction action : internalCluster().getInstances(TransportShardUpsertAction.class)) {
                    try {
                        Multimap<UUID, KillableCallable> operations = (Multimap<UUID, KillableCallable>) activeOperationsSb.get(action);
                        assertThat(operations.size(), is(0));
                    } catch (IllegalAccessException e) {
                        throw Throwables.propagate(e);
                    }
                }
                for (TransportShardDeleteAction action : internalCluster().getInstances(TransportShardDeleteAction.class)) {
                    try {
                        Multimap<UUID, KillableCallable> operations = (Multimap<UUID, KillableCallable>) activeOperationsSb.get(action);
                        assertThat(operations.size(), is(0));
                    } catch (IllegalAccessException e) {
                        throw Throwables.propagate(e);
                    }
                }
            }
        }, 10L, TimeUnit.SECONDS);
    } catch (AssertionError e) {
        StringBuilder errorMessageBuilder = new StringBuilder();
        String[] nodeNames = internalCluster().getNodeNames();
        for (String nodeName : nodeNames) {
            JobContextService jobContextService = internalCluster().getInstance(JobContextService.class, nodeName);
            try {
                //noinspection unchecked
                Map<UUID, JobExecutionContext> contexts = (Map<UUID, JobExecutionContext>) activeContexts.get(jobContextService);
                String contextsString = contexts.toString();
                if (!"{}".equals(contextsString)) {
                    errorMessageBuilder.append("## node: ");
                    errorMessageBuilder.append(nodeName);
                    errorMessageBuilder.append("\n");
                    errorMessageBuilder.append(contextsString);
                    errorMessageBuilder.append("\n");
                }
                contexts.clear();
            } catch (IllegalAccessException ex) {
                throw Throwables.propagate(e);
            }
        }
        throw new AssertionError(errorMessageBuilder.toString(), e);
    }
}
Also used : TransportShardUpsertAction(io.crate.executor.transport.TransportShardUpsertAction) TransportShardDeleteAction(io.crate.executor.transport.TransportShardDeleteAction) KillableCallable(io.crate.executor.transport.kill.KillableCallable) Field(java.lang.reflect.Field) Multimap(com.google.common.collect.Multimap) JobExecutionContext(io.crate.jobs.JobExecutionContext) JobContextService(io.crate.jobs.JobContextService) After(org.junit.After)

Example 30 with Multimap

use of com.google.common.collect.Multimap in project crate by crate.

the class PlannerTest method testAllocateRouting.

@Test
public void testAllocateRouting() throws Exception {
    TableIdent custom = new TableIdent("custom", "t1");
    TableInfo tableInfo1 = TestingTableInfo.builder(custom, shardRouting("t1")).add("id", DataTypes.INTEGER, null).build();
    TableInfo tableInfo2 = TestingTableInfo.builder(custom, shardRoutingForReplicas("t1")).add("id", DataTypes.INTEGER, null).build();
    Planner.Context plannerContext = new Planner.Context(e.planner, clusterService, UUID.randomUUID(), null, normalizer, new TransactionContext(SessionContext.SYSTEM_SESSION), 0, 0);
    WhereClause whereClause = new WhereClause(new Function(new FunctionInfo(new FunctionIdent(EqOperator.NAME, Arrays.<DataType>asList(DataTypes.INTEGER, DataTypes.INTEGER)), DataTypes.BOOLEAN), Arrays.asList(tableInfo1.getReference(new ColumnIdent("id")), Literal.of(2))));
    plannerContext.allocateRouting(tableInfo1, WhereClause.MATCH_ALL, null);
    plannerContext.allocateRouting(tableInfo2, whereClause, null);
    // 2 routing allocations with different where clause must result in 2 allocated routings
    java.lang.reflect.Field tableRoutings = Planner.Context.class.getDeclaredField("tableRoutings");
    tableRoutings.setAccessible(true);
    Multimap<TableIdent, Planner.TableRouting> routing = (Multimap<TableIdent, Planner.TableRouting>) tableRoutings.get(plannerContext);
    assertThat(routing.size(), is(2));
    // The routings must be the same after merging the locations
    Iterator<Planner.TableRouting> iterator = routing.values().iterator();
    Routing routing1 = iterator.next().routing;
    Routing routing2 = iterator.next().routing;
    assertThat(routing1, is(routing2));
}
Also used : SessionContext(io.crate.action.sql.SessionContext) WhereClause(io.crate.analyze.WhereClause) TableDefinitions.shardRouting(io.crate.analyze.TableDefinitions.shardRouting) Function(io.crate.analyze.symbol.Function) Multimap(com.google.common.collect.Multimap) DataType(io.crate.types.DataType) TableInfo(io.crate.metadata.table.TableInfo) TestingTableInfo(io.crate.metadata.table.TestingTableInfo) Test(org.junit.Test) CrateUnitTest(io.crate.test.integration.CrateUnitTest)

Aggregations

Multimap (com.google.common.collect.Multimap)47 HashMultimap (com.google.common.collect.HashMultimap)16 Test (org.junit.Test)15 List (java.util.List)13 InetAddress (java.net.InetAddress)11 Map (java.util.Map)9 IOException (java.io.IOException)8 ImmutableList (com.google.common.collect.ImmutableList)7 Collection (java.util.Collection)7 ArrayListMultimap (com.google.common.collect.ArrayListMultimap)6 ImmutableMap (com.google.common.collect.ImmutableMap)6 ImmutableMultimap (com.google.common.collect.ImmutableMultimap)6 ArrayList (java.util.ArrayList)6 Set (java.util.Set)6 ImmutableSet (com.google.common.collect.ImmutableSet)5 LinkedListMultimap (com.google.common.collect.LinkedListMultimap)5 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)5 Collectors (java.util.stream.Collectors)5 Token (org.apache.cassandra.dht.Token)4 HashMap (java.util.HashMap)3