|
| 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.util; |
| 19 | + |
| 20 | +import java.util.ArrayList; |
| 21 | +import java.util.Comparator; |
| 22 | +import java.util.Iterator; |
| 23 | +import java.util.Random; |
| 24 | +import java.util.concurrent.locks.ReentrantReadWriteLock; |
| 25 | +import org.apache.hadoop.util.LightWeightGSet.LinkedElement; |
| 26 | +import org.junit.Assert; |
| 27 | +import org.junit.Test; |
| 28 | +import org.slf4j.Logger; |
| 29 | +import org.slf4j.LoggerFactory; |
| 30 | + |
| 31 | +/** Testing {@link PartitionedGSet} */ |
| 32 | +public class TestPartitionedGSet { |
| 33 | + public static final Logger LOG = |
| 34 | + LoggerFactory.getLogger(TestPartitionedGSet.class); |
| 35 | + private static final int ELEMENT_NUM = 100; |
| 36 | + |
| 37 | + /** |
| 38 | + * Generate positive random numbers for testing. We want to use only positive |
| 39 | + * numbers because the smallest partition used in testing is 0. |
| 40 | + * |
| 41 | + * @param length |
| 42 | + * number of random numbers to be generated. |
| 43 | + * |
| 44 | + * @param randomSeed |
| 45 | + * seed to be used for random number generator. |
| 46 | + * |
| 47 | + * @return |
| 48 | + * An array of Integers |
| 49 | + */ |
| 50 | + private static ArrayList<Integer> getRandomList(int length, int randomSeed) { |
| 51 | + Random random = new Random(randomSeed); |
| 52 | + ArrayList<Integer> list = new ArrayList<Integer>(length); |
| 53 | + for (int i = 0; i < length; i++) { |
| 54 | + list.add(random.nextInt(Integer.MAX_VALUE)); |
| 55 | + } |
| 56 | + return list; |
| 57 | + } |
| 58 | + |
| 59 | + private static class TestElement implements LinkedElement { |
| 60 | + private final int val; |
| 61 | + private LinkedElement next; |
| 62 | + |
| 63 | + TestElement(int val) { |
| 64 | + this.val = val; |
| 65 | + this.next = null; |
| 66 | + } |
| 67 | + |
| 68 | + public int getVal() { |
| 69 | + return val; |
| 70 | + } |
| 71 | + |
| 72 | + @Override |
| 73 | + public void setNext(LinkedElement next) { |
| 74 | + this.next = next; |
| 75 | + } |
| 76 | + |
| 77 | + @Override |
| 78 | + public LinkedElement getNext() { |
| 79 | + return next; |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + private static class TestElementComparator implements Comparator<TestElement> |
| 84 | + { |
| 85 | + @Override |
| 86 | + public int compare(TestElement e1, TestElement e2) { |
| 87 | + if (e1 == null || e2 == null) { |
| 88 | + throw new NullPointerException("Cannot compare null elements"); |
| 89 | + } |
| 90 | + |
| 91 | + return e1.getVal() - e2.getVal(); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + protected ReentrantReadWriteLock topLock = |
| 96 | + new ReentrantReadWriteLock(false); |
| 97 | + /** |
| 98 | + * We are NOT testing any concurrent access to a PartitionedGSet here. |
| 99 | + */ |
| 100 | + private class NoOpLock extends LatchLock<ReentrantReadWriteLock> { |
| 101 | + private ReentrantReadWriteLock childLock; |
| 102 | + |
| 103 | + public NoOpLock() { |
| 104 | + childLock = new ReentrantReadWriteLock(false); |
| 105 | + } |
| 106 | + |
| 107 | + @Override |
| 108 | + protected boolean isReadTopLocked() { |
| 109 | + return topLock.getReadLockCount() > 0 || isWriteTopLocked(); |
| 110 | + } |
| 111 | + |
| 112 | + @Override |
| 113 | + protected boolean isWriteTopLocked() { |
| 114 | + return topLock.isWriteLocked(); |
| 115 | + } |
| 116 | + |
| 117 | + @Override |
| 118 | + protected void readTopUnlock() { |
| 119 | + topLock.readLock().unlock(); |
| 120 | + } |
| 121 | + |
| 122 | + @Override |
| 123 | + protected void writeTopUnlock() { |
| 124 | + topLock.writeLock().unlock(); |
| 125 | + } |
| 126 | + |
| 127 | + @Override |
| 128 | + protected boolean hasReadChildLock() { |
| 129 | + return childLock.getReadLockCount() > 0 || hasWriteChildLock(); |
| 130 | + } |
| 131 | + |
| 132 | + @Override |
| 133 | + protected void readChildLock() { |
| 134 | + childLock.readLock().lock(); |
| 135 | + } |
| 136 | + |
| 137 | + @Override |
| 138 | + protected void readChildUnlock() { |
| 139 | + childLock.readLock().unlock(); |
| 140 | + } |
| 141 | + |
| 142 | + @Override |
| 143 | + protected boolean hasWriteChildLock() { |
| 144 | + return childLock.isWriteLockedByCurrentThread(); |
| 145 | + } |
| 146 | + |
| 147 | + @Override |
| 148 | + protected void writeChildLock() { |
| 149 | + childLock.writeLock().lock(); |
| 150 | + } |
| 151 | + |
| 152 | + @Override |
| 153 | + protected void writeChildUnlock() { |
| 154 | + childLock.writeLock().unlock(); |
| 155 | + } |
| 156 | + |
| 157 | + @Override |
| 158 | + protected LatchLock<ReentrantReadWriteLock> clone() { |
| 159 | + return new NoOpLock(); |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + /** |
| 164 | + * Test iterator for a PartitionedGSet with no partitions. |
| 165 | + */ |
| 166 | + @Test(timeout=60000) |
| 167 | + public void testIteratorForNoPartition() { |
| 168 | + PartitionedGSet<TestElement, TestElement> set = |
| 169 | + new PartitionedGSet<TestElement, TestElement>( |
| 170 | + 16, new TestElementComparator(), new NoOpLock()); |
| 171 | + |
| 172 | + topLock.readLock().lock(); |
| 173 | + int count = 0; |
| 174 | + Iterator<TestElement> iter = set.iterator(); |
| 175 | + while( iter.hasNext() ) { |
| 176 | + iter.next(); |
| 177 | + count ++; |
| 178 | + } |
| 179 | + topLock.readLock().unlock(); |
| 180 | + Assert.assertEquals(0, count); |
| 181 | + } |
| 182 | + |
| 183 | + /** |
| 184 | + * Test iterator for a PartitionedGSet with empty partitions. |
| 185 | + */ |
| 186 | + @Test(timeout=60000) |
| 187 | + public void testIteratorForEmptyPartitions() { |
| 188 | + PartitionedGSet<TestElement, TestElement> set = |
| 189 | + new PartitionedGSet<TestElement, TestElement>( |
| 190 | + 16, new TestElementComparator(), new NoOpLock()); |
| 191 | + |
| 192 | + set.addNewPartition(new TestElement(0)); |
| 193 | + set.addNewPartition(new TestElement(1000)); |
| 194 | + set.addNewPartition(new TestElement(2000)); |
| 195 | + |
| 196 | + topLock.readLock().lock(); |
| 197 | + int count = 0; |
| 198 | + Iterator<TestElement> iter = set.iterator(); |
| 199 | + while( iter.hasNext() ) { |
| 200 | + iter.next(); |
| 201 | + count ++; |
| 202 | + } |
| 203 | + topLock.readLock().unlock(); |
| 204 | + Assert.assertEquals(0, count); |
| 205 | + } |
| 206 | + |
| 207 | + /** |
| 208 | + * Test whether the iterator can return the same number of elements as stored |
| 209 | + * into the PartitionedGSet. |
| 210 | + */ |
| 211 | + @Test(timeout=60000) |
| 212 | + public void testIteratorCountElements() { |
| 213 | + ArrayList<Integer> list = getRandomList(ELEMENT_NUM, 123); |
| 214 | + PartitionedGSet<TestElement, TestElement> set = |
| 215 | + new PartitionedGSet<TestElement, TestElement>( |
| 216 | + 16, new TestElementComparator(), new NoOpLock()); |
| 217 | + |
| 218 | + set.addNewPartition(new TestElement(0)); |
| 219 | + set.addNewPartition(new TestElement(1000)); |
| 220 | + set.addNewPartition(new TestElement(2000)); |
| 221 | + |
| 222 | + topLock.writeLock().lock(); |
| 223 | + for (Integer i : list) { |
| 224 | + set.put(new TestElement(i)); |
| 225 | + } |
| 226 | + topLock.writeLock().unlock(); |
| 227 | + |
| 228 | + topLock.readLock().lock(); |
| 229 | + int count = 0; |
| 230 | + Iterator<TestElement> iter = set.iterator(); |
| 231 | + while( iter.hasNext() ) { |
| 232 | + iter.next(); |
| 233 | + count ++; |
| 234 | + } |
| 235 | + topLock.readLock().unlock(); |
| 236 | + Assert.assertEquals(ELEMENT_NUM, count); |
| 237 | + } |
| 238 | + |
| 239 | + /** |
| 240 | + * Test iterator when it is created before partitions/elements are |
| 241 | + * added to the PartitionedGSet. |
| 242 | + */ |
| 243 | + @Test(timeout=60000) |
| 244 | + public void testIteratorAddElementsAfterIteratorCreation() { |
| 245 | + PartitionedGSet<TestElement, TestElement> set = |
| 246 | + new PartitionedGSet<TestElement, TestElement>( |
| 247 | + 16, new TestElementComparator(), new NoOpLock()); |
| 248 | + |
| 249 | + // Create the iterator before partitions are added. |
| 250 | + Iterator<TestElement> iter = set.iterator(); |
| 251 | + |
| 252 | + set.addNewPartition(new TestElement(0)); |
| 253 | + set.addNewPartition(new TestElement(1000)); |
| 254 | + set.addNewPartition(new TestElement(2000)); |
| 255 | + |
| 256 | + // Added one element |
| 257 | + topLock.writeLock().lock(); |
| 258 | + set.put(new TestElement(2500)); |
| 259 | + topLock.writeLock().unlock(); |
| 260 | + |
| 261 | + topLock.readLock().lock(); |
| 262 | + int count = 0; |
| 263 | + while( iter.hasNext() ) { |
| 264 | + iter.next(); |
| 265 | + count ++; |
| 266 | + } |
| 267 | + topLock.readLock().unlock(); |
| 268 | + Assert.assertEquals(1, count); |
| 269 | + } |
| 270 | +} |
0 commit comments