~pbeaman/akiban-persistit/fix_1005206_infinite_loop

« back to all changes in this revision

Viewing changes to core/src/test/java/com/persistit/JournalManagerTest.java

  • Committer: build-akiban
  • Date: 2012-05-15 21:24:26 UTC
  • mfrom: (292.3.38 fix_959456)
  • Revision ID: build-akiban-20120515212426-6lp526nql1wu790g
merge pbeaman: This branch fixes several problems related to https://bugs.launchpad.net/akiban-persistit/+bug/959456 Journal copier falls behind in 4-hour Persistit TPCC test.

https://code.launchpad.net/~pbeaman/akiban-persistit/fix_959456/+merge/105529

Show diffs side-by-side

added added

removed removed

Lines of Context:
28
28
 
29
29
import static org.junit.Assert.assertEquals;
30
30
import static org.junit.Assert.assertTrue;
 
31
import static org.junit.Assert.fail;
31
32
 
 
33
import java.util.ArrayList;
32
34
import java.util.HashMap;
33
35
import java.util.HashSet;
 
36
import java.util.Iterator;
 
37
import java.util.List;
34
38
import java.util.Map;
35
39
import java.util.Properties;
 
40
import java.util.Random;
36
41
import java.util.Set;
37
42
 
38
43
import org.junit.Test;
74
79
            if ((i % 400) == 0) {
75
80
                jman.rollover();
76
81
            }
 
82
            buffer.setDirtyAtTimestamp(_persistit.getTimestampAllocator().updateTimestamp());
77
83
            jman.writePageToJournal(buffer);
 
84
            buffer.clearDirty();
78
85
            buffer.releaseTouched();
79
86
        }
80
87
 
298
305
        }
299
306
        assertEquals(50000, countKeys(false));
300
307
        assertEquals(0, countKeys(true));
301
 
        _persistit.getJournalManager().pruneObsoleteTransactions(Long.MAX_VALUE, true);
 
308
        _persistit.getJournalManager().pruneObsoleteTransactions(true);
302
309
        assertTrue(countKeys(false) < 50000);
303
310
        CleanupManager cm = _persistit.getCleanupManager();
304
311
        assertTrue(cm.getAcceptedCount() > 0);
305
 
        while (cm.getEnqueuedCount() > 0) {
 
312
        long start = System.currentTimeMillis();
 
313
        while (cm.getPerformedCount() < cm.getAcceptedCount()) {
 
314
            if (System.currentTimeMillis() > start + 30000) {
 
315
                fail("Pruning not done in 30 seconds");
 
316
            }
306
317
            Util.sleep(100);
307
318
        }
308
319
        assertEquals(0, countKeys(false));
343
354
            txn.rollback();
344
355
            txn.end();
345
356
        }
346
 
        _persistit.getJournalManager().pruneObsoleteTransactions(Long.MAX_VALUE, true);
 
357
        _persistit.getJournalManager().pruneObsoleteTransactions(true);
347
358
        assertEquals(0, countKeys(false));
348
359
        IntegrityCheck icheck = new IntegrityCheck(_persistit);
349
360
        icheck.checkVolume(volume);
385
396
    }
386
397
 
387
398
    @Test
 
399
    public void testCleanupPageList() throws Exception {
 
400
        /*
 
401
         * Remove from the left end
 
402
         */
 
403
        {
 
404
            final List<PageNode> source =testCleanupPageListSource(10);
 
405
            for (int i = 0; i < 4; i++) {
 
406
                source.get(i).invalidate();
 
407
            }
 
408
            testCleanupPageListHelper(source);
 
409
        }
 
410
 
 
411
        /*
 
412
         * Remove from the right end
 
413
         */
 
414
        {
 
415
            final List<PageNode> source =testCleanupPageListSource(10);
 
416
            for (int i = 10; --i >= 7;) {
 
417
                source.get(i).invalidate();
 
418
            }
 
419
            testCleanupPageListHelper(source);
 
420
        }
 
421
 
 
422
        /*
 
423
         * Remove from the middle
 
424
         */
 
425
        {
 
426
            final List<PageNode> source =testCleanupPageListSource(10);
 
427
            for (int i = 2; i < 8; i++) {
 
428
                source.get(i).invalidate();
 
429
            }
 
430
            testCleanupPageListHelper(source);
 
431
        }
 
432
 
 
433
        /*
 
434
         * Randomly invalidated PageNodes
 
435
         */
 
436
        {
 
437
            final int SIZE = 5000;
 
438
            final Random random = new Random(1);
 
439
            final List<PageNode> source = testCleanupPageListSource(SIZE);
 
440
            int next = -1;
 
441
            for (int index = 0; index < SIZE; index++) {
 
442
                if (index < next) {
 
443
                    source.get(index).invalidate();
 
444
                } else {
 
445
                    index += random.nextInt(50);
 
446
                    next = random.nextInt(50) + index;
 
447
                }
 
448
            }
 
449
            testCleanupPageListHelper(source);
 
450
        }
 
451
    }
 
452
 
 
453
    private List<PageNode> testCleanupPageListSource(final int size) {
 
454
        final List<PageNode> source = new ArrayList<PageNode>(size);
 
455
        for (int index = 0; index < 1000000; index++) {
 
456
            source.add(new PageNode(0, index, index * 10, index));
 
457
        }
 
458
        return source;
 
459
    }
 
460
    
 
461
    
 
462
    private void testCleanupPageListHelper(final List<PageNode> source) throws Exception {
 
463
        final List<PageNode> cleaned = new ArrayList<PageNode>(source);
 
464
        for (Iterator<PageNode> iterator = cleaned.iterator(); iterator.hasNext();) {
 
465
            if (iterator.next().isInvalid()) {
 
466
                iterator.remove();
 
467
            }
 
468
        }
 
469
        JournalManager jman = new JournalManager(_persistit);
 
470
        jman.unitTestInjectPageList(source);
 
471
        int removed = jman.cleanupPageList();
 
472
        assertTrue(jman.unitTestPageListEquals(cleaned));
 
473
        assertEquals("Removed count is wrong", source.size() - cleaned.size(), removed);
 
474
        assertTrue("Invalidated no page nodes", source.size() > cleaned.size());
 
475
    }
 
476
 
 
477
    @Test
388
478
    public void copyBackPagesLeavesOneJournal() throws Exception {
389
479
        final int BATCH_SIZE = 1000;
390
480
        JournalManager jman = _persistit.getJournalManager();
391
481
 
392
482
        int total = 0;
393
 
        for(long curSize = 0; curSize < JournalManager.ROLLOVER_THRESHOLD; ) {
 
483
        for (long curSize = 0; curSize < JournalManager.ROLLOVER_THRESHOLD;) {
394
484
            Exchange ex = _persistit.getExchange(UnitTestProperties.VOLUME_NAME, "JournalManagerTest", true);
395
485
            Transaction txn = _persistit.getTransaction();
396
486
            Accumulator accum = ex.getTree().getAccumulator(Accumulator.Type.SUM, 0);
397
487
            txn.begin();
398
 
            for(int j = 0; j < BATCH_SIZE; ++j) {
 
488
            for (int j = 0; j < BATCH_SIZE; ++j) {
399
489
                ex.clear().append(total + j);
400
490
                ex.getValue().put(j);
401
491
                ex.store();