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 | package org.apache.hadoop.hdfs.server.namenode; |
---|
19 | |
---|
20 | import junit.framework.TestCase; |
---|
21 | import java.lang.System; |
---|
22 | |
---|
23 | import org.apache.hadoop.hdfs.protocol.Block; |
---|
24 | |
---|
25 | /** |
---|
26 | * This class tests the internals of PendingReplicationBlocks.java |
---|
27 | */ |
---|
28 | public class TestPendingReplication extends TestCase { |
---|
29 | public void testPendingReplication() { |
---|
30 | int timeout = 10; // 10 seconds |
---|
31 | PendingReplicationBlocks pendingReplications; |
---|
32 | pendingReplications = new PendingReplicationBlocks(timeout * 1000); |
---|
33 | |
---|
34 | // |
---|
35 | // Add 10 blocks to pendingReplications. |
---|
36 | // |
---|
37 | for (int i = 0; i < 10; i++) { |
---|
38 | Block block = new Block(i, i, 0); |
---|
39 | pendingReplications.add(block, i); |
---|
40 | } |
---|
41 | assertEquals("Size of pendingReplications ", |
---|
42 | 10, pendingReplications.size()); |
---|
43 | |
---|
44 | |
---|
45 | // |
---|
46 | // remove one item and reinsert it |
---|
47 | // |
---|
48 | Block blk = new Block(8, 8, 0); |
---|
49 | pendingReplications.remove(blk); // removes one replica |
---|
50 | assertEquals("pendingReplications.getNumReplicas ", |
---|
51 | 7, pendingReplications.getNumReplicas(blk)); |
---|
52 | |
---|
53 | for (int i = 0; i < 7; i++) { |
---|
54 | pendingReplications.remove(blk); // removes all replicas |
---|
55 | } |
---|
56 | assertTrue(pendingReplications.size() == 9); |
---|
57 | pendingReplications.add(blk, 8); |
---|
58 | assertTrue(pendingReplications.size() == 10); |
---|
59 | |
---|
60 | // |
---|
61 | // verify that the number of replicas returned |
---|
62 | // are sane. |
---|
63 | // |
---|
64 | for (int i = 0; i < 10; i++) { |
---|
65 | Block block = new Block(i, i, 0); |
---|
66 | int numReplicas = pendingReplications.getNumReplicas(block); |
---|
67 | assertTrue(numReplicas == i); |
---|
68 | } |
---|
69 | |
---|
70 | // |
---|
71 | // verify that nothing has timed out so far |
---|
72 | // |
---|
73 | assertTrue(pendingReplications.getTimedOutBlocks() == null); |
---|
74 | |
---|
75 | // |
---|
76 | // Wait for one second and then insert some more items. |
---|
77 | // |
---|
78 | try { |
---|
79 | Thread.sleep(1000); |
---|
80 | } catch (Exception e) { |
---|
81 | } |
---|
82 | |
---|
83 | for (int i = 10; i < 15; i++) { |
---|
84 | Block block = new Block(i, i, 0); |
---|
85 | pendingReplications.add(block, i); |
---|
86 | } |
---|
87 | assertTrue(pendingReplications.size() == 15); |
---|
88 | |
---|
89 | // |
---|
90 | // Wait for everything to timeout. |
---|
91 | // |
---|
92 | int loop = 0; |
---|
93 | while (pendingReplications.size() > 0) { |
---|
94 | try { |
---|
95 | Thread.sleep(1000); |
---|
96 | } catch (Exception e) { |
---|
97 | } |
---|
98 | loop++; |
---|
99 | } |
---|
100 | System.out.println("Had to wait for " + loop + |
---|
101 | " seconds for the lot to timeout"); |
---|
102 | |
---|
103 | // |
---|
104 | // Verify that everything has timed out. |
---|
105 | // |
---|
106 | assertEquals("Size of pendingReplications ", |
---|
107 | 0, pendingReplications.size()); |
---|
108 | Block[] timedOut = pendingReplications.getTimedOutBlocks(); |
---|
109 | assertTrue(timedOut != null && timedOut.length == 15); |
---|
110 | for (int i = 0; i < timedOut.length; i++) { |
---|
111 | assertTrue(timedOut[i].getBlockId() < 15); |
---|
112 | } |
---|
113 | } |
---|
114 | } |
---|