Search in sources :

Example 1 with FileMeta

use of org.commonjava.util.partyline.impl.infinispan.model.FileMeta in project partyline by Commonjava.

the class Main method main.

public static void main(String[] args) {
    if (args.length < 2) {
        System.out.println("Usage: $0 <input-dir> <output-dir>");
        System.exit(1);
    }
    File indir = new File(args[0]);
    File outdir = new File(args[1]);
    // Configure the block size for FileBlocks
    int blockSize = 1024;
    Logger logger = LoggerFactory.getLogger(Main.class);
    logger.info("Copying files from: " + indir + " to: " + outdir);
    DefaultCacheManager cacheManager = new DefaultCacheManager(true);
    ConfigurationBuilder builder = new ConfigurationBuilder();
    // Pick a config - persistence or no persistence
    Configuration persistence = builder.persistence().addSingleFileStore().transaction().transactionMode(TransactionMode.TRANSACTIONAL).build();
    // Configuration noPersist = builder.transaction().transactionMode( TransactionMode.TRANSACTIONAL ).build();
    cacheManager.defineConfiguration("blocks", persistence);
    Cache<String, FileBlock> blocks = cacheManager.getCache("blocks", true);
    cacheManager.defineConfiguration("files", persistence);
    Cache<String, FileMeta> files = cacheManager.getCache("files", true);
    Partyline partyline = new Partyline(new InfinispanJFS("single-node", files, blocks, blockSize));
    AtomicInteger inCounter = new AtomicInteger();
    File[] dirFiles = indir.listFiles();
    // copy from input dir to ISPN
    Stream.of(dirFiles).parallel().forEach(dirFile -> {
        if (!dirFile.isDirectory()) {
            logger.info("Copying from input: {}", dirFile);
            try (InputStream in = new FileInputStream(dirFile);
                OutputStream out = partyline.openOutputStream(dirFile)) {
                int b = in.read();
                out.write(b);
                while (b != -1) {
                    b = in.read();
                    out.write(b);
                }
            } catch (InterruptedException e) {
                logger.error("Input copy interrupted: " + dirFile.getName(), e);
            } catch (IOException e) {
                logger.error("Failed to copy to input: " + dirFile.getName(), e);
            } finally {
                inCounter.incrementAndGet();
            }
        }
    });
    AtomicInteger outCounter = new AtomicInteger();
    // pull the same files from ISPN and copy to output dir
    Stream.of(dirFiles).parallel().forEach(dirFile -> {
        if (!dirFile.isDirectory()) {
            logger.info("Copying to output: {}", dirFile);
            try (InputStream in = partyline.openInputStream(dirFile);
                OutputStream out = new FileOutputStream(new File(outdir, dirFile.getName()))) {
                IOUtils.copy(in, out);
            } catch (InterruptedException e) {
                logger.error("Output copy interrupted: " + dirFile.getName(), e);
            } catch (IOException e) {
                logger.error("Failed to copy to output: " + dirFile.getName(), e);
            } finally {
                outCounter.incrementAndGet();
            }
        }
    });
    System.out.println("Copied " + inCounter.get() + " files into Infinispan, and " + outCounter + " back out to destination directory.");
}
Also used : DefaultCacheManager(org.infinispan.manager.DefaultCacheManager) ConfigurationBuilder(org.infinispan.configuration.cache.ConfigurationBuilder) InfinispanJFS(org.commonjava.util.partyline.impl.infinispan.model.InfinispanJFS) Configuration(org.infinispan.configuration.cache.Configuration) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) Logger(org.slf4j.Logger) FileInputStream(java.io.FileInputStream) FileBlock(org.commonjava.util.partyline.impl.infinispan.model.FileBlock) Partyline(org.commonjava.util.partyline.Partyline) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) FileOutputStream(java.io.FileOutputStream) File(java.io.File) FileMeta(org.commonjava.util.partyline.impl.infinispan.model.FileMeta)

Example 2 with FileMeta

use of org.commonjava.util.partyline.impl.infinispan.model.FileMeta 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)

Example 3 with FileMeta

use of org.commonjava.util.partyline.impl.infinispan.model.FileMeta in project partyline by Commonjava.

the class InfinispanJFS method updateDominantLocks.

@Override
public void updateDominantLocks(String path, UnlockStatus unlockStatus) {
    // Do nothing if dominance did not change
    if (!unlockStatus.isDominanceChanged()) {
        return;
    }
    TransactionManager transactionManager = metadataCache.getAdvancedCache().getTransactionManager();
    try {
        transactionManager.begin();
        FileMeta meta = metadataCache.get(path);
        if (unlockStatus.getDominantLockLevel() == null) {
            meta.removeLock(this.nodeKey);
        } else {
            meta.setLock(this.nodeKey, unlockStatus.getDominantLockLevel());
        }
        metadataCache.put(path, meta);
    } catch (NotSupportedException | SystemException e) {
        try {
            transactionManager.rollback();
        } catch (SystemException e1) {
            LoggerFactory.getLogger(getClass().getName()).error("System Exception during transaction rollback involving path: " + path, e1);
        }
    } finally {
        try {
            transactionManager.commit();
        } catch (RollbackException | HeuristicMixedException | HeuristicRollbackException | SystemException e) {
            LoggerFactory.getLogger(getClass().getName()).error("Exception during transaction commit involving path: " + path, e);
        }
    }
}
Also used : HeuristicRollbackException(javax.transaction.HeuristicRollbackException) SystemException(javax.transaction.SystemException) TransactionManager(javax.transaction.TransactionManager) HeuristicMixedException(javax.transaction.HeuristicMixedException) NotSupportedException(javax.transaction.NotSupportedException) RollbackException(javax.transaction.RollbackException) HeuristicRollbackException(javax.transaction.HeuristicRollbackException) FileMeta(org.commonjava.util.partyline.impl.infinispan.model.FileMeta)

Aggregations

FileMeta (org.commonjava.util.partyline.impl.infinispan.model.FileMeta)3 File (java.io.File)2 IOException (java.io.IOException)2 HeuristicMixedException (javax.transaction.HeuristicMixedException)2 HeuristicRollbackException (javax.transaction.HeuristicRollbackException)2 NotSupportedException (javax.transaction.NotSupportedException)2 RollbackException (javax.transaction.RollbackException)2 SystemException (javax.transaction.SystemException)2 TransactionManager (javax.transaction.TransactionManager)2 FileBlock (org.commonjava.util.partyline.impl.infinispan.model.FileBlock)2 Logger (org.slf4j.Logger)2 FileInputStream (java.io.FileInputStream)1 FileOutputStream (java.io.FileOutputStream)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 ArrayList (java.util.ArrayList)1 Collections (java.util.Collections)1 List (java.util.List)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1