|
31 | 31 | import io.netty.buffer.Unpooled;
|
32 | 32 | import io.netty.util.ReferenceCountUtil;
|
33 | 33 | import java.io.File;
|
| 34 | +import java.io.FileInputStream; |
34 | 35 | import java.io.IOException;
|
| 36 | +import java.nio.ByteBuffer; |
35 | 37 | import java.util.List;
|
36 | 38 | import org.apache.bookkeeper.bookie.Bookie;
|
37 | 39 | import org.apache.bookkeeper.bookie.Bookie.NoEntryException;
|
38 | 40 | import org.apache.bookkeeper.bookie.BookieException;
|
39 | 41 | import org.apache.bookkeeper.bookie.BookieImpl;
|
| 42 | +import org.apache.bookkeeper.bookie.CheckpointSource; |
| 43 | +import org.apache.bookkeeper.bookie.CheckpointSourceList; |
40 | 44 | import org.apache.bookkeeper.bookie.DefaultEntryLogger;
|
41 | 45 | import org.apache.bookkeeper.bookie.EntryLocation;
|
42 | 46 | import org.apache.bookkeeper.bookie.LedgerDirsManager;
|
43 | 47 | import org.apache.bookkeeper.bookie.LedgerStorage;
|
| 48 | +import org.apache.bookkeeper.bookie.LogMark; |
44 | 49 | import org.apache.bookkeeper.bookie.TestBookieImpl;
|
45 | 50 | import org.apache.bookkeeper.bookie.storage.EntryLogger;
|
46 | 51 | import org.apache.bookkeeper.conf.ServerConfiguration;
|
@@ -639,4 +644,182 @@ public void testStorageStateFlags() throws Exception {
|
639 | 644 |
|
640 | 645 | storage = (DbLedgerStorage) new TestBookieImpl(conf).getLedgerStorage();
|
641 | 646 | }
|
| 647 | + |
| 648 | + @Test |
| 649 | + public void testMultiLedgerDirectoryCheckpoint() throws Exception { |
| 650 | + int gcWaitTime = 1000; |
| 651 | + File firstDir = new File(tmpDir, "dir1"); |
| 652 | + File secondDir = new File(tmpDir, "dir2"); |
| 653 | + ServerConfiguration conf = TestBKConfiguration.newServerConfiguration(); |
| 654 | + conf.setGcWaitTime(gcWaitTime); |
| 655 | + conf.setProperty(DbLedgerStorage.WRITE_CACHE_MAX_SIZE_MB, 4); |
| 656 | + conf.setProperty(DbLedgerStorage.READ_AHEAD_CACHE_MAX_SIZE_MB, 4); |
| 657 | + conf.setLedgerStorageClass(DbLedgerStorage.class.getName()); |
| 658 | + conf.setLedgerDirNames(new String[] { firstDir.getCanonicalPath(), secondDir.getCanonicalPath() }); |
| 659 | + |
| 660 | + BookieImpl bookie = new TestBookieImpl(conf); |
| 661 | + ByteBuf entry1 = Unpooled.buffer(1024); |
| 662 | + entry1.writeLong(1); // ledger id |
| 663 | + entry1.writeLong(2); // entry id |
| 664 | + entry1.writeBytes("entry-1".getBytes()); |
| 665 | + |
| 666 | + bookie.getLedgerStorage().addEntry(entry1); |
| 667 | + // write one entry to first ledger directory and flush with logMark(1, 2), |
| 668 | + // only the first ledger directory should have lastMark |
| 669 | + bookie.getJournals().get(0).getLastLogMark().getCurMark().setLogMark(1, 2); |
| 670 | + ((DbLedgerStorage) bookie.getLedgerStorage()).getLedgerStorageList().get(0).flush(); |
| 671 | + |
| 672 | + File firstDirMark = new File(firstDir + "/current", "lastMark"); |
| 673 | + File secondDirMark = new File(secondDir + "/current", "lastMark"); |
| 674 | + |
| 675 | + // LedgerStorage flush won't trigger lastMark update due to two ledger directories configured |
| 676 | + try { |
| 677 | + readLogMark(firstDirMark); |
| 678 | + readLogMark(secondDirMark); |
| 679 | + fail(); |
| 680 | + } catch (Exception e) { |
| 681 | + // |
| 682 | + } |
| 683 | + |
| 684 | + // write the second entry to second leger directory and flush with log(4, 5), |
| 685 | + // the fist ledger directory's lastMark is (1, 2) and the second ledger directory's lastMark is (4, 5); |
| 686 | + ByteBuf entry2 = Unpooled.buffer(1024); |
| 687 | + entry2.writeLong(2); // ledger id |
| 688 | + entry2.writeLong(1); // entry id |
| 689 | + entry2.writeBytes("entry-2".getBytes()); |
| 690 | + |
| 691 | + bookie.getLedgerStorage().addEntry(entry2); |
| 692 | + // write one entry to first ledger directory and flush with logMark(1, 2), |
| 693 | + // only the first ledger directory should have lastMark |
| 694 | + bookie.getJournals().get(0).getLastLogMark().getCurMark().setLogMark(4, 5); |
| 695 | + ((DbLedgerStorage) bookie.getLedgerStorage()).getLedgerStorageList().get(1).flush(); |
| 696 | + |
| 697 | + // LedgerStorage flush won't trigger lastMark update due to two ledger directories configured |
| 698 | + try { |
| 699 | + readLogMark(firstDirMark); |
| 700 | + readLogMark(secondDirMark); |
| 701 | + fail(); |
| 702 | + } catch (Exception e) { |
| 703 | + // |
| 704 | + } |
| 705 | + |
| 706 | + // The dbLedgerStorage flush also won't trigger lastMark update due to two ledger directories configured. |
| 707 | + bookie.getLedgerStorage().flush(); |
| 708 | + try { |
| 709 | + readLogMark(firstDirMark); |
| 710 | + readLogMark(secondDirMark); |
| 711 | + fail(); |
| 712 | + } catch (Exception e) { |
| 713 | + // |
| 714 | + } |
| 715 | + |
| 716 | + // trigger checkpoint simulate SyncThread do checkpoint. |
| 717 | + CheckpointSource checkpointSource = new CheckpointSourceList(bookie.getJournals()); |
| 718 | + bookie.getJournals().get(0).getLastLogMark().getCurMark().setLogMark(7, 8); |
| 719 | + CheckpointSource.Checkpoint checkpoint = checkpointSource.newCheckpoint(); |
| 720 | + checkpointSource.checkpointComplete(checkpoint, false); |
| 721 | + |
| 722 | + try { |
| 723 | + LogMark firstLogMark = readLogMark(firstDirMark); |
| 724 | + LogMark secondLogMark = readLogMark(secondDirMark); |
| 725 | + assertEquals(7, firstLogMark.getLogFileId()); |
| 726 | + assertEquals(8, firstLogMark.getLogFileOffset()); |
| 727 | + assertEquals(7, secondLogMark.getLogFileId()); |
| 728 | + assertEquals(8, secondLogMark.getLogFileOffset()); |
| 729 | + } catch (Exception e) { |
| 730 | + fail(); |
| 731 | + } |
| 732 | + |
| 733 | + // test replay journal lastMark, to make sure we get the right LastMark position |
| 734 | + bookie.getJournals().get(0).getLastLogMark().readLog(); |
| 735 | + LogMark logMark = bookie.getJournals().get(0).getLastLogMark().getCurMark(); |
| 736 | + assertEquals(7, logMark.getLogFileId()); |
| 737 | + assertEquals(8, logMark.getLogFileOffset()); |
| 738 | + } |
| 739 | + |
| 740 | + private LogMark readLogMark(File file) throws IOException { |
| 741 | + byte[] buff = new byte[16]; |
| 742 | + ByteBuffer bb = ByteBuffer.wrap(buff); |
| 743 | + LogMark mark = new LogMark(); |
| 744 | + try (FileInputStream fis = new FileInputStream(file)) { |
| 745 | + int bytesRead = fis.read(buff); |
| 746 | + if (bytesRead != 16) { |
| 747 | + throw new IOException("Couldn't read enough bytes from lastMark." |
| 748 | + + " Wanted " + 16 + ", got " + bytesRead); |
| 749 | + } |
| 750 | + } |
| 751 | + bb.clear(); |
| 752 | + mark.readLogMark(bb); |
| 753 | + |
| 754 | + return mark; |
| 755 | + } |
| 756 | + |
| 757 | + @Test |
| 758 | + public void testSingleLedgerDirectoryCheckpoint() throws Exception { |
| 759 | + int gcWaitTime = 1000; |
| 760 | + File ledgerDir = new File(tmpDir, "dir"); |
| 761 | + ServerConfiguration conf = TestBKConfiguration.newServerConfiguration(); |
| 762 | + conf.setGcWaitTime(gcWaitTime); |
| 763 | + conf.setProperty(DbLedgerStorage.WRITE_CACHE_MAX_SIZE_MB, 4); |
| 764 | + conf.setProperty(DbLedgerStorage.READ_AHEAD_CACHE_MAX_SIZE_MB, 4); |
| 765 | + conf.setLedgerStorageClass(DbLedgerStorage.class.getName()); |
| 766 | + conf.setLedgerDirNames(new String[] { ledgerDir.getCanonicalPath() }); |
| 767 | + |
| 768 | + BookieImpl bookie = new TestBookieImpl(conf); |
| 769 | + ByteBuf entry1 = Unpooled.buffer(1024); |
| 770 | + entry1.writeLong(1); // ledger id |
| 771 | + entry1.writeLong(2); // entry id |
| 772 | + entry1.writeBytes("entry-1".getBytes()); |
| 773 | + bookie.getLedgerStorage().addEntry(entry1); |
| 774 | + |
| 775 | + bookie.getJournals().get(0).getLastLogMark().getCurMark().setLogMark(1, 2); |
| 776 | + ((DbLedgerStorage) bookie.getLedgerStorage()).getLedgerStorageList().get(0).flush(); |
| 777 | + |
| 778 | + File ledgerDirMark = new File(ledgerDir + "/current", "lastMark"); |
| 779 | + try { |
| 780 | + LogMark logMark = readLogMark(ledgerDirMark); |
| 781 | + assertEquals(1, logMark.getLogFileId()); |
| 782 | + assertEquals(2, logMark.getLogFileOffset()); |
| 783 | + } catch (Exception e) { |
| 784 | + fail(); |
| 785 | + } |
| 786 | + |
| 787 | + ByteBuf entry2 = Unpooled.buffer(1024); |
| 788 | + entry2.writeLong(2); // ledger id |
| 789 | + entry2.writeLong(1); // entry id |
| 790 | + entry2.writeBytes("entry-2".getBytes()); |
| 791 | + |
| 792 | + bookie.getLedgerStorage().addEntry(entry2); |
| 793 | + // write one entry to first ledger directory and flush with logMark(1, 2), |
| 794 | + // only the first ledger directory should have lastMark |
| 795 | + bookie.getJournals().get(0).getLastLogMark().getCurMark().setLogMark(4, 5); |
| 796 | + |
| 797 | + bookie.getLedgerStorage().flush(); |
| 798 | + try { |
| 799 | + LogMark logMark = readLogMark(ledgerDirMark); |
| 800 | + assertEquals(4, logMark.getLogFileId()); |
| 801 | + assertEquals(5, logMark.getLogFileOffset()); |
| 802 | + } catch (Exception e) { |
| 803 | + fail(); |
| 804 | + } |
| 805 | + |
| 806 | + CheckpointSource checkpointSource = new CheckpointSourceList(bookie.getJournals()); |
| 807 | + bookie.getJournals().get(0).getLastLogMark().getCurMark().setLogMark(7, 8); |
| 808 | + CheckpointSource.Checkpoint checkpoint = checkpointSource.newCheckpoint(); |
| 809 | + checkpointSource.checkpointComplete(checkpoint, false); |
| 810 | + |
| 811 | + try { |
| 812 | + LogMark firstLogMark = readLogMark(ledgerDirMark); |
| 813 | + assertEquals(7, firstLogMark.getLogFileId()); |
| 814 | + assertEquals(8, firstLogMark.getLogFileOffset()); |
| 815 | + } catch (Exception e) { |
| 816 | + fail(); |
| 817 | + } |
| 818 | + |
| 819 | + // test replay journal lastMark, to make sure we get the right LastMark position |
| 820 | + bookie.getJournals().get(0).getLastLogMark().readLog(); |
| 821 | + LogMark logMark = bookie.getJournals().get(0).getLastLogMark().getCurMark(); |
| 822 | + assertEquals(7, logMark.getLogFileId()); |
| 823 | + assertEquals(8, logMark.getLogFileOffset()); |
| 824 | + } |
642 | 825 | }
|
0 commit comments