|
30 | 30 | import static org.hamcrest.Matchers.empty;
|
31 | 31 | import static org.hamcrest.Matchers.equalTo;
|
32 | 32 | import static org.hamcrest.Matchers.greaterThan;
|
| 33 | +import static org.junit.Assert.assertEquals; |
33 | 34 | import static org.junit.Assert.assertFalse;
|
34 | 35 | import static org.junit.Assert.assertTrue;
|
35 | 36 | import static org.mockito.Mockito.any;
|
@@ -238,4 +239,138 @@ private void testExtractMetaFromEntryLogs(EntryLogger entryLogger, File ledgerDi
|
238 | 239 | assertTrue(entryLogMetaMap.isEmpty());
|
239 | 240 | assertFalse(entryLogger.logExists(logId3));
|
240 | 241 | }
|
| 242 | + |
| 243 | + @Test |
| 244 | + public void testCompactionWithFileSizeCheck() throws Exception { |
| 245 | + File ledgerDir = tmpDirs.createNew("testFileSize", "ledgers"); |
| 246 | + EntryLogger entryLogger = newLegacyEntryLogger(20000, ledgerDir); |
| 247 | + |
| 248 | + MockLedgerStorage storage = new MockLedgerStorage(); |
| 249 | + MockLedgerManager lm = new MockLedgerManager(); |
| 250 | + |
| 251 | + GarbageCollectorThread gcThread = new GarbageCollectorThread( |
| 252 | + TestBKConfiguration.newServerConfiguration().setUseTargetEntryLogSizeForGc(true), lm, |
| 253 | + newDirsManager(ledgerDir), |
| 254 | + storage, entryLogger, NullStatsLogger.INSTANCE); |
| 255 | + |
| 256 | + // Add entries. |
| 257 | + // Ledger 1 is on first entry log |
| 258 | + // Ledger 2 spans first, second and third entry log |
| 259 | + // Ledger 3 is on the third entry log (which is still active when extract meta) |
| 260 | + long loc1 = entryLogger.addEntry(1L, makeEntry(1L, 1L, 5000)); |
| 261 | + long loc2 = entryLogger.addEntry(2L, makeEntry(2L, 1L, 5000)); |
| 262 | + assertThat(logIdFromLocation(loc2), equalTo(logIdFromLocation(loc1))); |
| 263 | + long loc3 = entryLogger.addEntry(2L, makeEntry(2L, 2L, 15000)); |
| 264 | + assertThat(logIdFromLocation(loc3), greaterThan(logIdFromLocation(loc2))); |
| 265 | + long loc4 = entryLogger.addEntry(2L, makeEntry(2L, 3L, 15000)); |
| 266 | + assertThat(logIdFromLocation(loc4), greaterThan(logIdFromLocation(loc3))); |
| 267 | + long loc5 = entryLogger.addEntry(3L, makeEntry(3L, 1L, 1000)); |
| 268 | + assertThat(logIdFromLocation(loc5), equalTo(logIdFromLocation(loc4))); |
| 269 | + long loc6 = entryLogger.addEntry(3L, makeEntry(3L, 2L, 5000)); |
| 270 | + |
| 271 | + long logId1 = logIdFromLocation(loc2); |
| 272 | + long logId2 = logIdFromLocation(loc3); |
| 273 | + long logId3 = logIdFromLocation(loc5); |
| 274 | + long logId4 = logIdFromLocation(loc6); |
| 275 | + entryLogger.flush(); |
| 276 | + |
| 277 | + storage.setMasterKey(1L, new byte[0]); |
| 278 | + storage.setMasterKey(2L, new byte[0]); |
| 279 | + storage.setMasterKey(3L, new byte[0]); |
| 280 | + |
| 281 | + assertThat(entryLogger.getFlushedLogIds(), containsInAnyOrder(logId1, logId2, logId3)); |
| 282 | + assertTrue(entryLogger.logExists(logId1)); |
| 283 | + assertTrue(entryLogger.logExists(logId2)); |
| 284 | + assertTrue(entryLogger.logExists(logId3)); |
| 285 | + assertTrue(entryLogger.logExists(logId4)); |
| 286 | + |
| 287 | + // all ledgers exist, nothing should disappear |
| 288 | + final EntryLogMetadataMap entryLogMetaMap = gcThread.getEntryLogMetaMap(); |
| 289 | + gcThread.extractMetaFromEntryLogs(); |
| 290 | + |
| 291 | + assertThat(entryLogger.getFlushedLogIds(), containsInAnyOrder(logId1, logId2, logId3)); |
| 292 | + assertTrue(entryLogMetaMap.containsKey(logId1)); |
| 293 | + assertTrue(entryLogMetaMap.containsKey(logId2)); |
| 294 | + assertTrue(entryLogger.logExists(logId3)); |
| 295 | + |
| 296 | + storage.deleteLedger(1); |
| 297 | + // only logId 1 will be compacted. |
| 298 | + gcThread.runWithFlags(true, true, false); |
| 299 | + |
| 300 | + // logId1 and logId2 should be compacted |
| 301 | + assertFalse(entryLogger.logExists(logId1)); |
| 302 | + assertTrue(entryLogger.logExists(logId2)); |
| 303 | + assertTrue(entryLogger.logExists(logId3)); |
| 304 | + assertFalse(entryLogMetaMap.containsKey(logId1)); |
| 305 | + assertTrue(entryLogMetaMap.containsKey(logId2)); |
| 306 | + |
| 307 | + assertEquals(1, storage.getUpdatedLocations().size()); |
| 308 | + |
| 309 | + EntryLocation location2 = storage.getUpdatedLocations().get(0); |
| 310 | + assertEquals(2, location2.getLedger()); |
| 311 | + assertEquals(1, location2.getEntry()); |
| 312 | + assertEquals(logIdFromLocation(location2.getLocation()), logId4); |
| 313 | + } |
| 314 | + |
| 315 | + @Test |
| 316 | + public void testCompactionWithoutFileSizeCheck() throws Exception { |
| 317 | + File ledgerDir = tmpDirs.createNew("testFileSize", "ledgers"); |
| 318 | + EntryLogger entryLogger = newLegacyEntryLogger(20000, ledgerDir); |
| 319 | + |
| 320 | + MockLedgerStorage storage = new MockLedgerStorage(); |
| 321 | + MockLedgerManager lm = new MockLedgerManager(); |
| 322 | + |
| 323 | + GarbageCollectorThread gcThread = new GarbageCollectorThread( |
| 324 | + TestBKConfiguration.newServerConfiguration(), lm, |
| 325 | + newDirsManager(ledgerDir), |
| 326 | + storage, entryLogger, NullStatsLogger.INSTANCE); |
| 327 | + |
| 328 | + // Add entries. |
| 329 | + // Ledger 1 is on first entry log |
| 330 | + // Ledger 2 spans first, second and third entry log |
| 331 | + // Ledger 3 is on the third entry log (which is still active when extract meta) |
| 332 | + long loc1 = entryLogger.addEntry(1L, makeEntry(1L, 1L, 5000)); |
| 333 | + long loc2 = entryLogger.addEntry(2L, makeEntry(2L, 1L, 5000)); |
| 334 | + assertThat(logIdFromLocation(loc2), equalTo(logIdFromLocation(loc1))); |
| 335 | + long loc3 = entryLogger.addEntry(2L, makeEntry(2L, 2L, 15000)); |
| 336 | + assertThat(logIdFromLocation(loc3), greaterThan(logIdFromLocation(loc2))); |
| 337 | + long loc4 = entryLogger.addEntry(2L, makeEntry(2L, 3L, 15000)); |
| 338 | + assertThat(logIdFromLocation(loc4), greaterThan(logIdFromLocation(loc3))); |
| 339 | + long loc5 = entryLogger.addEntry(3L, makeEntry(3L, 1L, 1000)); |
| 340 | + assertThat(logIdFromLocation(loc5), equalTo(logIdFromLocation(loc4))); |
| 341 | + |
| 342 | + long logId1 = logIdFromLocation(loc2); |
| 343 | + long logId2 = logIdFromLocation(loc3); |
| 344 | + long logId3 = logIdFromLocation(loc5); |
| 345 | + entryLogger.flush(); |
| 346 | + |
| 347 | + storage.setMasterKey(1L, new byte[0]); |
| 348 | + storage.setMasterKey(2L, new byte[0]); |
| 349 | + storage.setMasterKey(3L, new byte[0]); |
| 350 | + |
| 351 | + assertThat(entryLogger.getFlushedLogIds(), containsInAnyOrder(logId1, logId2)); |
| 352 | + assertTrue(entryLogger.logExists(logId1)); |
| 353 | + assertTrue(entryLogger.logExists(logId2)); |
| 354 | + assertTrue(entryLogger.logExists(logId3)); |
| 355 | + |
| 356 | + // all ledgers exist, nothing should disappear |
| 357 | + final EntryLogMetadataMap entryLogMetaMap = gcThread.getEntryLogMetaMap(); |
| 358 | + gcThread.extractMetaFromEntryLogs(); |
| 359 | + |
| 360 | + assertThat(entryLogger.getFlushedLogIds(), containsInAnyOrder(logId1, logId2)); |
| 361 | + assertTrue(entryLogMetaMap.containsKey(logId1)); |
| 362 | + assertTrue(entryLogMetaMap.containsKey(logId2)); |
| 363 | + assertTrue(entryLogger.logExists(logId3)); |
| 364 | + |
| 365 | + gcThread.runWithFlags(true, true, false); |
| 366 | + |
| 367 | + assertTrue(entryLogger.logExists(logId1)); |
| 368 | + assertTrue(entryLogger.logExists(logId2)); |
| 369 | + assertTrue(entryLogger.logExists(logId3)); |
| 370 | + assertTrue(entryLogMetaMap.containsKey(logId1)); |
| 371 | + assertTrue(entryLogMetaMap.containsKey(logId2)); |
| 372 | + |
| 373 | + assertEquals(0, storage.getUpdatedLocations().size()); |
| 374 | + } |
| 375 | + |
241 | 376 | }
|
0 commit comments