Search in sources :

Example 1 with ForceWriteRequest

use of org.apache.bookkeeper.bookie.Journal.ForceWriteRequest in project bookkeeper by apache.

the class BookieJournalForceTest method testAckAfterSync.

@Test
public void testAckAfterSync() throws Exception {
    File journalDir = tempDir.newFolder();
    Bookie.checkDirectoryStructure(Bookie.getCurrentDirectory(journalDir));
    ServerConfiguration conf = TestBKConfiguration.newServerConfiguration();
    conf.setJournalDirName(journalDir.getPath()).setZkServers(null);
    JournalChannel jc = spy(new JournalChannel(journalDir, 1));
    whenNew(JournalChannel.class).withAnyArguments().thenReturn(jc);
    LedgerDirsManager ledgerDirsManager = mock(LedgerDirsManager.class);
    Journal journal = new Journal(0, journalDir, conf, ledgerDirsManager);
    // machinery to suspend ForceWriteThread
    CountDownLatch forceWriteThreadSuspendedLatch = new CountDownLatch(1);
    LinkedBlockingQueue<ForceWriteRequest> supportQueue = enableForceWriteThreadSuspension(forceWriteThreadSuspendedLatch, journal);
    journal.start();
    LogMark lastLogMarkBeforeWrite = journal.getLastLogMark().markLog().getCurMark();
    CountDownLatch latch = new CountDownLatch(1);
    long ledgerId = 1;
    long entryId = 0;
    journal.logAddEntry(ledgerId, entryId, DATA, false, /* ackBeforeSync */
    new WriteCallback() {

        @Override
        public void writeComplete(int rc, long ledgerId, long entryId, BookieSocketAddress addr, Object ctx) {
            latch.countDown();
        }
    }, null);
    // wait that an entry is written to the ForceWriteThread queue
    while (supportQueue.isEmpty()) {
        Thread.sleep(100);
    }
    assertEquals(1, latch.getCount());
    assertEquals(1, supportQueue.size());
    // in constructor of JournalChannel we are calling forceWrite(true) but it is not tracked by PowerMock
    // because the 'spy' is applied only on return from the constructor
    verify(jc, times(0)).forceWrite(true);
    // let ForceWriteThread work
    forceWriteThreadSuspendedLatch.countDown();
    // callback should complete now
    assertTrue(latch.await(20, TimeUnit.SECONDS));
    verify(jc, atLeast(1)).forceWrite(false);
    assertEquals(0, supportQueue.size());
    // verify that log marker advanced
    LastLogMark lastLogMarkAfterForceWrite = journal.getLastLogMark();
    assertTrue(lastLogMarkAfterForceWrite.getCurMark().compare(lastLogMarkBeforeWrite) > 0);
    journal.shutdown();
}
Also used : LastLogMark(org.apache.bookkeeper.bookie.Journal.LastLogMark) ServerConfiguration(org.apache.bookkeeper.conf.ServerConfiguration) WriteCallback(org.apache.bookkeeper.proto.BookkeeperInternalCallbacks.WriteCallback) LastLogMark(org.apache.bookkeeper.bookie.Journal.LastLogMark) CountDownLatch(java.util.concurrent.CountDownLatch) ForceWriteRequest(org.apache.bookkeeper.bookie.Journal.ForceWriteRequest) BookieSocketAddress(org.apache.bookkeeper.net.BookieSocketAddress) File(java.io.File) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

File (java.io.File)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 ForceWriteRequest (org.apache.bookkeeper.bookie.Journal.ForceWriteRequest)1 LastLogMark (org.apache.bookkeeper.bookie.Journal.LastLogMark)1 ServerConfiguration (org.apache.bookkeeper.conf.ServerConfiguration)1 BookieSocketAddress (org.apache.bookkeeper.net.BookieSocketAddress)1 WriteCallback (org.apache.bookkeeper.proto.BookkeeperInternalCallbacks.WriteCallback)1 Test (org.junit.Test)1 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)1