|
| 1 | +/** |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +package org.apache.hadoop.hdfs.server.blockmanagement; |
| 20 | + |
| 21 | +import org.apache.hadoop.hdfs.protocol.Block; |
| 22 | +import org.junit.Assert; |
| 23 | +import org.junit.Test; |
| 24 | + |
| 25 | +public class TestUnderReplicatedBlockQueues extends Assert { |
| 26 | + |
| 27 | + /** |
| 28 | + * Test that adding blocks with different replication counts puts them |
| 29 | + * into different queues |
| 30 | + * @throws Throwable if something goes wrong |
| 31 | + */ |
| 32 | + @Test |
| 33 | + public void testBlockPriorities() throws Throwable { |
| 34 | + UnderReplicatedBlocks queues = new UnderReplicatedBlocks(); |
| 35 | + Block block1 = new Block(1); |
| 36 | + Block block2 = new Block(2); |
| 37 | + Block block_very_under_replicated = new Block(3); |
| 38 | + Block block_corrupt = new Block(4); |
| 39 | + |
| 40 | + //add a block with a single entry |
| 41 | + assertAdded(queues, block1, 1, 0, 3); |
| 42 | + |
| 43 | + assertEquals(1, queues.getUnderReplicatedBlockCount()); |
| 44 | + assertEquals(1, queues.size()); |
| 45 | + assertInLevel(queues, block1, UnderReplicatedBlocks.QUEUE_HIGHEST_PRIORITY); |
| 46 | + //repeated additions fail |
| 47 | + assertFalse(queues.add(block1, 1, 0, 3)); |
| 48 | + |
| 49 | + //add a second block with two replicas |
| 50 | + assertAdded(queues, block2, 2, 0, 3); |
| 51 | + assertEquals(2, queues.getUnderReplicatedBlockCount()); |
| 52 | + assertEquals(2, queues.size()); |
| 53 | + assertInLevel(queues, block2, UnderReplicatedBlocks.QUEUE_UNDER_REPLICATED); |
| 54 | + //now try to add a block that is corrupt |
| 55 | + assertAdded(queues, block_corrupt, 0, 0, 3); |
| 56 | + assertEquals(3, queues.size()); |
| 57 | + assertEquals(2, queues.getUnderReplicatedBlockCount()); |
| 58 | + assertEquals(1, queues.getCorruptBlockSize()); |
| 59 | + assertInLevel(queues, block_corrupt, |
| 60 | + UnderReplicatedBlocks.QUEUE_WITH_CORRUPT_BLOCKS); |
| 61 | + |
| 62 | + //insert a very under-replicated block |
| 63 | + assertAdded(queues, block_very_under_replicated, 4, 0, 25); |
| 64 | + assertInLevel(queues, block_very_under_replicated, |
| 65 | + UnderReplicatedBlocks.QUEUE_VERY_UNDER_REPLICATED); |
| 66 | + |
| 67 | + } |
| 68 | + |
| 69 | + private void assertAdded(UnderReplicatedBlocks queues, |
| 70 | + Block block, |
| 71 | + int curReplicas, |
| 72 | + int decomissionedReplicas, |
| 73 | + int expectedReplicas) { |
| 74 | + assertTrue("Failed to add " + block, |
| 75 | + queues.add(block, |
| 76 | + curReplicas, |
| 77 | + decomissionedReplicas, |
| 78 | + expectedReplicas)); |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * Determine whether or not a block is in a level without changing the API. |
| 83 | + * Instead get the per-level iterator and run though it looking for a match. |
| 84 | + * If the block is not found, an assertion is thrown. |
| 85 | + * |
| 86 | + * This is inefficient, but this is only a test case. |
| 87 | + * @param queues queues to scan |
| 88 | + * @param block block to look for |
| 89 | + * @param level level to select |
| 90 | + */ |
| 91 | + private void assertInLevel(UnderReplicatedBlocks queues, |
| 92 | + Block block, |
| 93 | + int level) { |
| 94 | + UnderReplicatedBlocks.BlockIterator bi = queues.iterator(level); |
| 95 | + while (bi.hasNext()) { |
| 96 | + Block next = bi.next(); |
| 97 | + if (block.equals(next)) { |
| 98 | + return; |
| 99 | + } |
| 100 | + } |
| 101 | + fail("Block " + block + " not found in level " + level); |
| 102 | + } |
| 103 | +} |
0 commit comments