Search in sources :

Example 1 with LocalLockOwner

use of org.commonjava.util.partyline.lock.local.LocalLockOwner in project partyline by Commonjava.

the class JoinFileWriteAndCloseBeforeFinishedTest method run.

/**
 * Test verifies JoinableFile read could be done before its write stream close
 * with read close delay, this setup an script of events for one single file, where:
 * <ol>
 *     <li>Simulate JoinableFile write process </li>
 *     <li>Read should be proceeded before write stream close</li>
 * </ol>
 * @throws Exception
 */
@BMRules(rules = { // wait for read call to exit
@BMRule(name = "write close", targetClass = "RandomAccessJF", targetMethod = "close", targetLocation = "ENTRY", condition = "incrementCounter($0)==1", action = "debug(\">>>wait for service enter read.\");" + "waitFor(\"read\");" + "debug(\"<<<proceed with write close.\")"), // setup the trigger to signal write close when the read exits
@BMRule(name = "read", targetClass = "RandomAccessJF", targetMethod = "joinStream", targetLocation = "EXIT", action = "debug(\"<<<signalling write close.\"); " + "signalWake(\"read\", true);" + "debug(\"<<<signalled write close.\")") })
@Test
@BMUnitConfig(debug = true)
public void run() throws Exception {
    final ExecutorService execs = Executors.newFixedThreadPool(2);
    final CountDownLatch latch = new CountDownLatch(2);
    final File file = temp.newFile();
    String threadName = "writer" + writers++;
    final JoinableFile stream = new RandomAccessJFS().getFile(file, new LocalLockOwner(file.getAbsolutePath(), name.getMethodName(), LockLevel.write), null, true, new SignallingLock());
    execs.execute(() -> {
        Thread.currentThread().setName(threadName);
        new TimedFileWriter(stream, 1, latch).run();
    });
    execs.execute(() -> {
        Thread.currentThread().setName("reader" + readers++);
        new AsyncFileReader(0, -1, 6000, stream, latch).run();
    });
    System.out.println("Waiting for " + name.getMethodName() + " threads to complete.");
    latch.await();
}
Also used : SignallingLock(org.commonjava.cdi.util.weft.SignallingLock) RandomAccessJFS(org.commonjava.util.partyline.impl.local.RandomAccessJFS) TimedFileWriter(org.commonjava.util.partyline.fixture.TimedFileWriter) ExecutorService(java.util.concurrent.ExecutorService) JoinableFile(org.commonjava.util.partyline.spi.JoinableFile) CountDownLatch(java.util.concurrent.CountDownLatch) LocalLockOwner(org.commonjava.util.partyline.lock.local.LocalLockOwner) File(java.io.File) JoinableFile(org.commonjava.util.partyline.spi.JoinableFile) BMUnitConfig(org.jboss.byteman.contrib.bmunit.BMUnitConfig) Test(org.junit.Test) BMRules(org.jboss.byteman.contrib.bmunit.BMRules)

Example 2 with LocalLockOwner

use of org.commonjava.util.partyline.lock.local.LocalLockOwner in project partyline by Commonjava.

the class JoinFileWriteTwiceWithDelayTest method run.

/**
 * Test verifies JoinableFile simultaneous reads could be done before its write stream close,
 * this setup an script of events for one single file, where:
 * <ol>
 *     <li>Simulate JoinableFile write process </li>
 *     <li>Two simultaneous reads with init delay should be proceeded before write stream close</li>
 * </ol>
 * @throws Exception
 */
@BMRules(rules = { // setup the rendezvous for all threads, which will mean suspending everything until all threads are started.
@BMRule(name = "init rendezvous", targetClass = "RandomAccessJF", targetMethod = "<init>", targetLocation = "ENTRY", action = "createRendezvous(\"begin\", 3);" + "debug(\"<<<init rendezvous for begin.\")"), // setup the rendezvous to wait for all threads to be ready before proceeding.
@BMRule(name = "write close", targetClass = "RandomAccessJF", targetMethod = "close", targetLocation = "ENTRY", condition = "incrementCounter($0)==1", action = "debug(\">>>Waiting for ALL to start.\");" + "rendezvous(\"begin\");" + "debug(\"<<<\"+Thread.currentThread().getName() + \": write thread proceeding.\" )"), @BMRule(name = "read", targetClass = "RandomAccessJF", targetMethod = "joinStream", targetLocation = "EXIT", action = "debug(\">>>Waiting for ALL to start.\");" + "rendezvous(\"begin\");" + "debug(\"<<<\"+Thread.currentThread().getName() + \": read thread proceeding.\" )") })
@Test
@BMUnitConfig(debug = true)
public void run() throws Exception {
    final ExecutorService execs = Executors.newFixedThreadPool(3);
    final CountDownLatch latch = new CountDownLatch(3);
    final File file = temp.newFile();
    String threadName = "writer" + writers++;
    final JoinableFile stream = new RandomAccessJFS().getFile(file, new LocalLockOwner(file.getAbsolutePath(), name.getMethodName(), LockLevel.write), null, true, new SignallingLock());
    execs.execute(() -> {
        Thread.currentThread().setName(threadName);
        new TimedFileWriter(stream, 1, latch).run();
    });
    execs.execute(() -> {
        Thread.currentThread().setName("reader" + readers++);
        new AsyncFileReader(0, -1, -1, stream, latch).run();
    });
    execs.execute(() -> {
        Thread.currentThread().setName("reader" + readers++);
        new AsyncFileReader(500, -1, -1, stream, latch).run();
    });
    System.out.println("Waiting for " + name.getMethodName() + " threads to complete.");
    latch.await();
}
Also used : SignallingLock(org.commonjava.cdi.util.weft.SignallingLock) RandomAccessJFS(org.commonjava.util.partyline.impl.local.RandomAccessJFS) TimedFileWriter(org.commonjava.util.partyline.fixture.TimedFileWriter) ExecutorService(java.util.concurrent.ExecutorService) JoinableFile(org.commonjava.util.partyline.spi.JoinableFile) CountDownLatch(java.util.concurrent.CountDownLatch) LocalLockOwner(org.commonjava.util.partyline.lock.local.LocalLockOwner) File(java.io.File) JoinableFile(org.commonjava.util.partyline.spi.JoinableFile) BMUnitConfig(org.jboss.byteman.contrib.bmunit.BMUnitConfig) Test(org.junit.Test) BMRules(org.jboss.byteman.contrib.bmunit.BMRules)

Example 3 with LocalLockOwner

use of org.commonjava.util.partyline.lock.local.LocalLockOwner in project partyline by Commonjava.

the class LockOwnerTest method lockClearAndLockAgainWithDifferentLevel.

@Test
public void lockClearAndLockAgainWithDifferentLevel() {
    String label = "testing";
    LocalLockOwner owner = new LocalLockOwner("/path/to/nowhere", label, LockLevel.write);
    assertThat(owner.isLockedByCurrentThread(), equalTo(true));
    UnlockStatus unlocked = owner.unlock(label);
    assertThat(unlocked, notNullValue());
    assertThat(unlocked.isUnlocked(), equalTo(true));
    assertThat(owner.isLockedByCurrentThread(), equalTo(false));
    boolean locked = owner.lock("relocking", LockLevel.delete);
    assertThat(locked, equalTo(true));
}
Also used : LocalLockOwner(org.commonjava.util.partyline.lock.local.LocalLockOwner) UnlockStatus(org.commonjava.util.partyline.lock.UnlockStatus) Test(org.junit.Test)

Example 4 with LocalLockOwner

use of org.commonjava.util.partyline.lock.local.LocalLockOwner in project partyline by Commonjava.

the class JoinFileWriteTest method run.

/**
 * Test verifies JoinableFile read could be done before its write stream close
 * without any delay, this setup an script of events for one single file, where:
 * <ol>
 *     <li>Simulate JoinableFile write process </li>
 *     <li>Read should be proceeded before write stream close</li>
 * </ol>
 * @throws Exception
 */
@BMRules(rules = { // wait for read call to exit
@BMRule(name = "write close", targetClass = "RandomAccessJF", targetMethod = "close", targetLocation = "ENTRY", condition = "incrementCounter($0)==1", action = "debug(\">>>wait for service enter read.\");" + "waitFor(\"read\");" + "debug(\"<<<proceed with write close.\")"), // setup the trigger to signal write close when the read exits
@BMRule(name = "read", targetClass = "RandomAccessJF", targetMethod = "joinStream", targetLocation = "EXIT", action = "debug(\"<<<signalling write close.\"); " + "signalWake(\"read\", true);" + "debug(\"<<<signalled write close.\")") })
@Test
@BMUnitConfig(debug = true)
public void run() throws Exception {
    final ExecutorService execs = Executors.newFixedThreadPool(2);
    final CountDownLatch latch = new CountDownLatch(2);
    final File file = temp.newFile();
    String threadName = "writer" + writers++;
    final JoinableFile stream = new RandomAccessJFS().getFile(file, new LocalLockOwner(file.getAbsolutePath(), name.getMethodName(), LockLevel.write), null, true, new SignallingLock());
    execs.execute(() -> {
        Thread.currentThread().setName(threadName);
        new TimedFileWriter(stream, 0, latch).run();
    });
    execs.execute(() -> {
        Thread.currentThread().setName("reader" + readers++);
        new AsyncFileReader(0, -1, -1, stream, latch).run();
    });
    System.out.println("Waiting for " + name.getMethodName() + " threads to complete.");
    latch.await();
}
Also used : SignallingLock(org.commonjava.cdi.util.weft.SignallingLock) RandomAccessJFS(org.commonjava.util.partyline.impl.local.RandomAccessJFS) TimedFileWriter(org.commonjava.util.partyline.fixture.TimedFileWriter) ExecutorService(java.util.concurrent.ExecutorService) JoinableFile(org.commonjava.util.partyline.spi.JoinableFile) CountDownLatch(java.util.concurrent.CountDownLatch) LocalLockOwner(org.commonjava.util.partyline.lock.local.LocalLockOwner) File(java.io.File) JoinableFile(org.commonjava.util.partyline.spi.JoinableFile) BMUnitConfig(org.jboss.byteman.contrib.bmunit.BMUnitConfig) Test(org.junit.Test) BMRules(org.jboss.byteman.contrib.bmunit.BMRules)

Example 5 with LocalLockOwner

use of org.commonjava.util.partyline.lock.local.LocalLockOwner in project partyline by Commonjava.

the class JoinableFileManagerPerformanceTest method bigFileWritePerformanceTest.

@Test
public void bigFileWritePerformanceTest() throws Exception {
    final File f = temp.newFile("bigfile.txt");
    final File fm = temp.newFile("bigfileJFM.txt");
    final File jft = temp.newFile("bigfileJF.txt");
    String content = createBigFileContent();
    JoinableFile jf = new RandomAccessJFS().getFile(jft, new LocalLockOwner(jft.getPath(), "write test", LockLevel.write), null, true, new SignallingLock());
    long start = System.currentTimeMillis();
    System.out.println("Opening output...");
    try (OutputStream out = jf.getOutputStream()) {
        System.out.println("Writing content");
        out.write(content.getBytes());
        System.out.println("closing stream");
    }
    long end = System.currentTimeMillis();
    System.out.println("closing file");
    jf.close();
    System.out.println("done");
    long jfTime = end - start;
    // JFM file io
    start = System.currentTimeMillis();
    try (OutputStream out = mgr.openOutputStream(fm)) {
        out.write(content.getBytes());
    }
    end = System.currentTimeMillis();
    final long jfmFileWritingTime = end - start;
    // Normal java file io
    start = System.currentTimeMillis();
    try (FileOutputStream stream = new FileOutputStream(f)) {
        stream.write(content.getBytes());
    }
    end = System.currentTimeMillis();
    final long javaFileWritingTime = end - start;
    System.out.println(String.format("File size: JFM file: %dM; JF file: %dM; Java FO file: %dM", fm.length() / 1024 / 1024, jft.length() / 1024 / 1024, f.length() / 1024 / 1024));
    System.out.printf("File IO time comparison: JFM writing: %dms; JF writing: %dms; Java FO writing: %dms. Ratio JFM/FO: %d, JF/FO: %d\n", jfmFileWritingTime, jfTime, javaFileWritingTime, (jfmFileWritingTime / javaFileWritingTime), (jfTime / javaFileWritingTime));
    // TODO: Seems current JFM writing performance is slower about 3 times than normal java File IO when writing a 50m file
    final int times = 10;
    if (jfmFileWritingTime / javaFileWritingTime > times) {
        fail(String.format("JFM writing performance is %d times slower than normal Java FO writing against a %dm file", (jfmFileWritingTime / javaFileWritingTime), f.length() / 1024 / 1024));
    }
}
Also used : SignallingLock(org.commonjava.cdi.util.weft.SignallingLock) RandomAccessJFS(org.commonjava.util.partyline.impl.local.RandomAccessJFS) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) JoinableFile(org.commonjava.util.partyline.spi.JoinableFile) LocalLockOwner(org.commonjava.util.partyline.lock.local.LocalLockOwner) File(java.io.File) JoinableFile(org.commonjava.util.partyline.spi.JoinableFile) Test(org.junit.Test)

Aggregations

LocalLockOwner (org.commonjava.util.partyline.lock.local.LocalLockOwner)8 File (java.io.File)7 SignallingLock (org.commonjava.cdi.util.weft.SignallingLock)7 JoinableFile (org.commonjava.util.partyline.spi.JoinableFile)7 Test (org.junit.Test)7 RandomAccessJFS (org.commonjava.util.partyline.impl.local.RandomAccessJFS)6 CountDownLatch (java.util.concurrent.CountDownLatch)4 ExecutorService (java.util.concurrent.ExecutorService)4 TimedFileWriter (org.commonjava.util.partyline.fixture.TimedFileWriter)4 BMRules (org.jboss.byteman.contrib.bmunit.BMRules)4 BMUnitConfig (org.jboss.byteman.contrib.bmunit.BMUnitConfig)4 OutputStream (java.io.OutputStream)2 ArrayList (java.util.ArrayList)2 UnlockStatus (org.commonjava.util.partyline.lock.UnlockStatus)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 Collections (java.util.Collections)1 List (java.util.List)1