|
19 | 19 |
|
20 | 20 | package org.elasticsearch.index.engine; |
21 | 21 |
|
| 22 | +import org.apache.lucene.util.BytesRef; |
22 | 23 | import org.apache.lucene.util.BytesRefBuilder; |
23 | 24 | import org.apache.lucene.util.RamUsageTester; |
24 | 25 | import org.apache.lucene.util.TestUtil; |
| 26 | +import org.elasticsearch.Assertions; |
25 | 27 | import org.elasticsearch.bootstrap.JavaVersion; |
| 28 | +import org.elasticsearch.common.lease.Releasable; |
| 29 | +import org.elasticsearch.common.util.concurrent.KeyedLock; |
26 | 30 | import org.elasticsearch.test.ESTestCase; |
27 | 31 |
|
| 32 | +import java.io.IOException; |
| 33 | +import java.util.ArrayList; |
| 34 | +import java.util.HashMap; |
| 35 | +import java.util.HashSet; |
| 36 | +import java.util.List; |
| 37 | +import java.util.Map; |
| 38 | +import java.util.concurrent.ConcurrentHashMap; |
| 39 | +import java.util.concurrent.CountDownLatch; |
| 40 | + |
28 | 41 | public class LiveVersionMapTests extends ESTestCase { |
29 | 42 |
|
30 | 43 | public void testRamBytesUsed() throws Exception { |
@@ -57,4 +70,151 @@ public void testRamBytesUsed() throws Exception { |
57 | 70 | assertEquals(actualRamBytesUsed, estimatedRamBytesUsed, actualRamBytesUsed / 4); |
58 | 71 | } |
59 | 72 |
|
| 73 | + private BytesRef uid(String string) { |
| 74 | + BytesRefBuilder builder = new BytesRefBuilder(); |
| 75 | + builder.copyChars(string); |
| 76 | + // length of the array must be the same as the len of the ref... there is an assertion in LiveVersionMap#putUnderLock |
| 77 | + return BytesRef.deepCopyOf(builder.get()); |
| 78 | + } |
| 79 | + |
| 80 | + public void testBasics() throws IOException { |
| 81 | + LiveVersionMap map = new LiveVersionMap(); |
| 82 | + map.putUnderLock(uid("test"), new VersionValue(1,1,1)); |
| 83 | + assertEquals(new VersionValue(1,1,1), map.getUnderLock(uid("test"))); |
| 84 | + map.beforeRefresh(); |
| 85 | + assertEquals(new VersionValue(1,1,1), map.getUnderLock(uid("test"))); |
| 86 | + map.afterRefresh(randomBoolean()); |
| 87 | + assertNull(map.getUnderLock(uid("test"))); |
| 88 | + |
| 89 | + |
| 90 | + map.putUnderLock(uid("test"), new DeleteVersionValue(1,1,1, Long.MAX_VALUE)); |
| 91 | + assertEquals(new DeleteVersionValue(1,1,1, Long.MAX_VALUE), map.getUnderLock(uid("test"))); |
| 92 | + map.beforeRefresh(); |
| 93 | + assertEquals(new DeleteVersionValue(1,1,1, Long.MAX_VALUE), map.getUnderLock(uid("test"))); |
| 94 | + map.afterRefresh(randomBoolean()); |
| 95 | + assertEquals(new DeleteVersionValue(1,1,1, Long.MAX_VALUE), map.getUnderLock(uid("test"))); |
| 96 | + map.removeTombstoneUnderLock(uid("test")); |
| 97 | + assertNull(map.getUnderLock(uid("test"))); |
| 98 | + } |
| 99 | + |
| 100 | + |
| 101 | + public void testAdjustMapSizeUnderLock() throws IOException { |
| 102 | + LiveVersionMap map = new LiveVersionMap(); |
| 103 | + map.putUnderLock(uid("test"), new VersionValue(1,1,1)); |
| 104 | + boolean withinRefresh = randomBoolean(); |
| 105 | + if (withinRefresh) { |
| 106 | + map.beforeRefresh(); |
| 107 | + } |
| 108 | + assertEquals(new VersionValue(1,1,1), map.getUnderLock(uid("test"))); |
| 109 | + final String msg; |
| 110 | + if (Assertions.ENABLED) { |
| 111 | + msg = expectThrows(AssertionError.class, map::adjustMapSizeUnderLock).getMessage(); |
| 112 | + } else { |
| 113 | + msg = expectThrows(IllegalStateException.class, map::adjustMapSizeUnderLock).getMessage(); |
| 114 | + } |
| 115 | + assertEquals("map must be empty", msg); |
| 116 | + assertEquals(new VersionValue(1,1,1), map.getUnderLock(uid("test"))); |
| 117 | + if (withinRefresh == false) { |
| 118 | + map.beforeRefresh(); |
| 119 | + } |
| 120 | + map.afterRefresh(randomBoolean()); |
| 121 | + Map<BytesRef, VersionValue> allCurrent = map.getAllCurrent(); |
| 122 | + map.adjustMapSizeUnderLock(); |
| 123 | + assertNotSame(allCurrent, map.getAllCurrent()); |
| 124 | + } |
| 125 | + |
| 126 | + public void testConcurrently() throws IOException, InterruptedException { |
| 127 | + HashSet<BytesRef> keySet = new HashSet<>(); |
| 128 | + int numKeys = randomIntBetween(50, 200); |
| 129 | + for (int i = 0; i < numKeys; i++) { |
| 130 | + keySet.add(uid(TestUtil.randomSimpleString(random(), 10, 20))); |
| 131 | + } |
| 132 | + List<BytesRef> keyList = new ArrayList<>(keySet); |
| 133 | + ConcurrentHashMap<BytesRef, VersionValue> values = new ConcurrentHashMap<>(); |
| 134 | + KeyedLock<BytesRef> keyedLock = new KeyedLock<>(); |
| 135 | + LiveVersionMap map = new LiveVersionMap(); |
| 136 | + int numThreads = randomIntBetween(2, 5); |
| 137 | + |
| 138 | + Thread[] threads = new Thread[numThreads]; |
| 139 | + CountDownLatch startGun = new CountDownLatch(numThreads); |
| 140 | + CountDownLatch done = new CountDownLatch(numThreads); |
| 141 | + int randomValuesPerThread = randomIntBetween(5000, 20000); |
| 142 | + for (int j = 0; j < threads.length; j++) { |
| 143 | + threads[j] = new Thread(() -> { |
| 144 | + startGun.countDown(); |
| 145 | + try { |
| 146 | + startGun.await(); |
| 147 | + } catch (InterruptedException e) { |
| 148 | + done.countDown(); |
| 149 | + throw new AssertionError(e); |
| 150 | + } |
| 151 | + try { |
| 152 | + for (int i = 0; i < randomValuesPerThread; ++i) { |
| 153 | + BytesRef bytesRef = randomFrom(random(), keyList); |
| 154 | + try (Releasable r = keyedLock.acquire(bytesRef)) { |
| 155 | + VersionValue versionValue = values.computeIfAbsent(bytesRef, |
| 156 | + v -> new VersionValue(randomLong(), randomLong(), randomLong())); |
| 157 | + boolean isDelete = versionValue instanceof DeleteVersionValue; |
| 158 | + if (isDelete) { |
| 159 | + map.removeTombstoneUnderLock(bytesRef); |
| 160 | + } |
| 161 | + if (isDelete == false && rarely()) { |
| 162 | + versionValue = new DeleteVersionValue(versionValue.version + 1, versionValue.seqNo + 1, |
| 163 | + versionValue.term, Long.MAX_VALUE); |
| 164 | + } else { |
| 165 | + versionValue = new VersionValue(versionValue.version + 1, versionValue.seqNo + 1, versionValue.term); |
| 166 | + } |
| 167 | + values.put(bytesRef, versionValue); |
| 168 | + map.putUnderLock(bytesRef, versionValue); |
| 169 | + } |
| 170 | + } |
| 171 | + } finally { |
| 172 | + done.countDown(); |
| 173 | + } |
| 174 | + }); |
| 175 | + threads[j].start(); |
| 176 | + |
| 177 | + |
| 178 | + } |
| 179 | + do { |
| 180 | + Map<BytesRef, VersionValue> valueMap = new HashMap<>(map.getAllCurrent()); |
| 181 | + map.beforeRefresh(); |
| 182 | + valueMap.forEach((k, v) -> { |
| 183 | + VersionValue actualValue = map.getUnderLock(k); |
| 184 | + assertNotNull(actualValue); |
| 185 | + assertTrue(v.version <= actualValue.version); |
| 186 | + }); |
| 187 | + map.afterRefresh(randomBoolean()); |
| 188 | + valueMap.forEach((k, v) -> { |
| 189 | + VersionValue actualValue = map.getUnderLock(k); |
| 190 | + if (actualValue != null) { |
| 191 | + if (actualValue instanceof DeleteVersionValue) { |
| 192 | + assertTrue(v.version <= actualValue.version); // deletes can be the same version |
| 193 | + } else { |
| 194 | + assertTrue(v.version < actualValue.version); |
| 195 | + } |
| 196 | + |
| 197 | + } |
| 198 | + }); |
| 199 | + if (randomBoolean()) { |
| 200 | + Thread.yield(); |
| 201 | + } |
| 202 | + } while (done.getCount() != 0); |
| 203 | + |
| 204 | + for (int j = 0; j < threads.length; j++) { |
| 205 | + threads[j].join(); |
| 206 | + } |
| 207 | + map.getAllCurrent().forEach((k, v) -> { |
| 208 | + VersionValue versionValue = values.get(k); |
| 209 | + assertNotNull(versionValue); |
| 210 | + assertEquals(v, versionValue); |
| 211 | + }); |
| 212 | + |
| 213 | + map.getAllTombstones().forEach(e -> { |
| 214 | + VersionValue versionValue = values.get(e.getKey()); |
| 215 | + assertNotNull(versionValue); |
| 216 | + assertEquals(e.getValue(), versionValue); |
| 217 | + assertTrue(versionValue instanceof DeleteVersionValue); |
| 218 | + }); |
| 219 | + } |
60 | 220 | } |
0 commit comments