Search in sources :

Example 6 with LocalLockOwner

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

the class BinaryFileTest method shouldReadBinaryFile.

@Test
public void shouldReadBinaryFile() throws IOException, InterruptedException {
    List<String> failures = new ArrayList<>();
    File binaryFile = temp.newFile("binary-file.bin");
    ReentrantLock lock = new ReentrantLock();
    JoinableFile jf = new RandomAccessJFS().getFile(binaryFile, new LocalLockOwner(binaryFile.getAbsolutePath(), name.getMethodName(), LockLevel.write), null, true, new SignallingLock());
    OutputStream jos = jf.getOutputStream();
    InputStream actual = jf.joinStream();
    ByteArrayOutputStream written = new ByteArrayOutputStream();
    writeBinaryFile(jos, written);
    int pos = 0;
    int exp, act;
    ByteArrayInputStream expected = new ByteArrayInputStream(written.toByteArray());
    while ((exp = expected.read()) != -1) {
        act = actual.read();
        pos++;
        if (act != exp) {
            failures.add(String.format("Failure at position %d. Expected %d, got %d", pos, exp, act));
        }
    }
    actual.close();
    if (!failures.isEmpty()) {
        fail("Failures: " + failures);
    }
}
Also used : ReentrantLock(java.util.concurrent.locks.ReentrantLock) SignallingLock(org.commonjava.cdi.util.weft.SignallingLock) RandomAccessJFS(org.commonjava.util.partyline.impl.local.RandomAccessJFS) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) ByteArrayOutputStream(org.apache.commons.io.output.ByteArrayOutputStream) ArrayList(java.util.ArrayList) ByteArrayOutputStream(org.apache.commons.io.output.ByteArrayOutputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) 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)

Example 7 with LocalLockOwner

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

the class JoinFileWriteJustBeforeFinishedTest method run.

/**
 * Test verifies JoinableFile read could be done before its write stream close
 * with read init 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(1000, -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 8 with LocalLockOwner

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

the class InfinispanJFS method getMetadata.

FileMeta getMetadata(final File target, final LocalLockOwner owner) throws IOException {
    String path = target.getAbsolutePath();
    return lockManager.lockAnd(path, (key, opLock) -> {
        FileMeta meta = null;
        TransactionManager transactionManager = metadataCache.getAdvancedCache().getTransactionManager();
        try {
            transactionManager.begin();
            meta = metadataCache.computeIfAbsent(path, (p) -> new FileMeta(p, target.isDirectory(), this.blockSize));
            LockLevel currentLockLevel = meta.getLockLevel(this.nodeKey);
            // Only update the cache if the lock level changed
            if (currentLockLevel == null || currentLockLevel != owner.getLockLevel()) {
                meta.setLock(this.nodeKey, owner.getLockLevel());
                metadataCache.put(path, meta);
            }
            transactionManager.commit();
        } catch (Exception e) {
            logger.error("Failed to execute in transaction. Rolling back. Path: " + path, e);
            try {
                transactionManager.rollback();
            } catch (SystemException e1) {
                logger.error("SystemException during transaction rollback involving path: " + path, e1);
            }
        }
        return meta;
    });
}
Also used : SignallingLocker(org.commonjava.cdi.util.weft.SignallingLocker) JoinableFilesystem(org.commonjava.util.partyline.spi.JoinableFilesystem) LockLevel(org.commonjava.util.partyline.lock.LockLevel) LoggerFactory(org.slf4j.LoggerFactory) StreamCallbacks(org.commonjava.util.partyline.callback.StreamCallbacks) Cache(org.infinispan.Cache) AtomicReference(java.util.concurrent.atomic.AtomicReference) ArrayList(java.util.ArrayList) FileMeta(org.commonjava.util.partyline.impl.infinispan.model.FileMeta) RollbackException(javax.transaction.RollbackException) LocalLockOwner(org.commonjava.util.partyline.lock.local.LocalLockOwner) CacheEntryModified(org.infinispan.notifications.cachelistener.annotation.CacheEntryModified) JoinableFile(org.commonjava.util.partyline.spi.JoinableFile) UnlockStatus(org.commonjava.util.partyline.lock.UnlockStatus) Listener(org.infinispan.notifications.Listener) Logger(org.slf4j.Logger) HeuristicRollbackException(javax.transaction.HeuristicRollbackException) PartylineException(org.commonjava.util.partyline.PartylineException) IOException(java.io.IOException) File(java.io.File) SignallingLock(org.commonjava.cdi.util.weft.SignallingLock) NotSupportedException(javax.transaction.NotSupportedException) CacheEntryCreated(org.infinispan.notifications.cachelistener.annotation.CacheEntryCreated) List(java.util.List) CacheEntryEvent(org.infinispan.notifications.cachelistener.event.CacheEntryEvent) SystemException(javax.transaction.SystemException) FileBlock(org.commonjava.util.partyline.impl.infinispan.model.FileBlock) HeuristicMixedException(javax.transaction.HeuristicMixedException) TransactionManager(javax.transaction.TransactionManager) Collections(java.util.Collections) SystemException(javax.transaction.SystemException) TransactionManager(javax.transaction.TransactionManager) LockLevel(org.commonjava.util.partyline.lock.LockLevel) FileMeta(org.commonjava.util.partyline.impl.infinispan.model.FileMeta) RollbackException(javax.transaction.RollbackException) HeuristicRollbackException(javax.transaction.HeuristicRollbackException) PartylineException(org.commonjava.util.partyline.PartylineException) IOException(java.io.IOException) NotSupportedException(javax.transaction.NotSupportedException) SystemException(javax.transaction.SystemException) HeuristicMixedException(javax.transaction.HeuristicMixedException)

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