Search in sources :

Example 51 with FileLock

use of java.nio.channels.FileLock in project Chronicle-Queue by OpenHFT.

the class SingleTableStore method doWithExclusiveLock.

/**
 * {@inheritDoc}
 */
@Override
public <R> R doWithExclusiveLock(Function<TableStore, ? extends R> code) {
    final long timeoutAt = System.currentTimeMillis() + 2 * timeoutMS;
    boolean warnedOnFailure = false;
    try (final FileChannel channel = FileChannel.open(file().toPath(), StandardOpenOption.WRITE)) {
        while (System.currentTimeMillis() < timeoutAt) {
            try {
                FileLock fileLock = channel.tryLock();
                if (fileLock != null) {
                    return code.apply(this);
                }
            } catch (IOException | OverlappingFileLockException e) {
                // failed to acquire the lock, wait until other operation completes
                if (!warnedOnFailure) {
                    Jvm.debug().on(getClass(), "Failed to acquire a lock on the table store file. Retrying");
                    warnedOnFailure = true;
                }
            }
            Jvm.pause(50L);
        }
    } catch (IOException e) {
        throw new IllegalStateException("Couldn't open table store file for writing", e);
    }
    throw new IllegalStateException("Unable to claim exclusive lock on file " + file());
}
Also used : FileChannel(java.nio.channels.FileChannel) FileLock(java.nio.channels.FileLock) IOException(java.io.IOException) OverlappingFileLockException(java.nio.channels.OverlappingFileLockException)

Example 52 with FileLock

use of java.nio.channels.FileLock in project bnd by bndtools.

the class CAFS method write.

/**
	 * Store an input stream in the CAFS while calculating and returning the
	 * SHA-1 code.
	 * 
	 * @param in The input stream to store.
	 * @return The SHA-1 code.
	 * @throws Exception if anything goes wrong
	 */
public SHA1 write(InputStream in) throws Exception {
    Deflater deflater = new Deflater();
    MessageDigest md = MessageDigest.getInstance(ALGORITHM);
    DigestInputStream din = new DigestInputStream(in, md);
    ByteArrayOutputStream bout = new ByteArrayOutputStream();
    DeflaterOutputStream dout = new DeflaterOutputStream(bout, deflater);
    copy(din, dout);
    synchronized (store) {
        // First check if it already exists
        SHA1 sha1 = new SHA1(md.digest());
        long search = index.search(sha1.digest());
        if (search > 0)
            return sha1;
        byte[] compressed = bout.toByteArray();
        // we need to append this file to our store,
        // which requires a lock. However, we are in a race
        // so others can get the lock between us getting
        // the length and someone else getting the lock.
        // So we must verify after we get the lock that the
        // length was unchanged.
        FileLock lock = null;
        try {
            long insertPoint;
            int recordLength = compressed.length + HEADERLENGTH;
            while (true) {
                insertPoint = store.length();
                lock = channel.lock(insertPoint, recordLength, false);
                if (store.length() == insertPoint)
                    break;
                // We got the wrong lock, someone else
                // got in between reading the length
                // and locking
                lock.release();
            }
            int totalLength = deflater.getTotalIn();
            store.seek(insertPoint);
            update(sha1.digest(), compressed, totalLength);
            index.insert(sha1.digest(), insertPoint);
            return sha1;
        } finally {
            if (lock != null)
                lock.release();
        }
    }
}
Also used : SHA1(aQute.libg.cryptography.SHA1) Deflater(java.util.zip.Deflater) DigestInputStream(java.security.DigestInputStream) DeflaterOutputStream(java.util.zip.DeflaterOutputStream) FileLock(java.nio.channels.FileLock) ByteArrayOutputStream(java.io.ByteArrayOutputStream) MessageDigest(java.security.MessageDigest)

Example 53 with FileLock

use of java.nio.channels.FileLock in project bnd by bndtools.

the class PersistentMap method put.

public V put(String key, V value) {
    init();
    try {
        V old = null;
        SoftReference<V> ref = cache.get(key);
        if (ref != null)
            old = ref.get();
        FileLock lock = lock();
        try {
            File file = new File(data, key);
            codec.enc().to(file).put(value);
            cache.put(key, new SoftReference<V>(value));
            return old;
        } finally {
            unlock(lock);
        }
    } catch (RuntimeException e) {
        throw e;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : FileLock(java.nio.channels.FileLock) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) OverlappingFileLockException(java.nio.channels.OverlappingFileLockException) IOException(java.io.IOException)

Example 54 with FileLock

use of java.nio.channels.FileLock in project bnd by bndtools.

the class PersistentMap method clear.

public void clear() {
    init();
    try {
        FileLock lock = lock();
        try {
            IO.deleteWithException(data);
            cache.clear();
            IO.mkdirs(data);
        } finally {
            unlock(lock);
        }
    } catch (RuntimeException e) {
        throw e;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : FileLock(java.nio.channels.FileLock) OverlappingFileLockException(java.nio.channels.OverlappingFileLockException) IOException(java.io.IOException)

Example 55 with FileLock

use of java.nio.channels.FileLock in project PocketHub by pockethub.

the class RequestWriter method write.

/**
 * Write request to file
 *
 * @param request
 * @return request
 */
public <V> V write(V request) {
    RandomAccessFile dir = null;
    FileLock lock = null;
    ObjectOutputStream output = null;
    try {
        createDirectory(handle.getParentFile());
        dir = new RandomAccessFile(handle, "rw");
        lock = dir.getChannel().lock();
        output = new ObjectOutputStream(new GZIPOutputStream(new FileOutputStream(dir.getFD()), 8192));
        output.writeInt(version);
        output.writeObject(request);
    } catch (IOException e) {
        Log.d(TAG, "Exception writing cache " + handle.getName(), e);
        return null;
    } finally {
        if (output != null) {
            try {
                output.close();
            } catch (IOException e) {
                Log.d(TAG, "Exception closing stream", e);
            }
        }
        if (lock != null) {
            try {
                lock.release();
            } catch (IOException e) {
                Log.d(TAG, "Exception unlocking file", e);
            }
        }
        if (dir != null) {
            try {
                dir.close();
            } catch (IOException e) {
                Log.d(TAG, "Exception closing file", e);
            }
        }
    }
    return request;
}
Also used : RandomAccessFile(java.io.RandomAccessFile) GZIPOutputStream(java.util.zip.GZIPOutputStream) FileOutputStream(java.io.FileOutputStream) FileLock(java.nio.channels.FileLock) IOException(java.io.IOException) ObjectOutputStream(java.io.ObjectOutputStream)

Aggregations

FileLock (java.nio.channels.FileLock)246 IOException (java.io.IOException)127 RandomAccessFile (java.io.RandomAccessFile)99 FileChannel (java.nio.channels.FileChannel)83 File (java.io.File)77 OverlappingFileLockException (java.nio.channels.OverlappingFileLockException)37 FileOutputStream (java.io.FileOutputStream)29 Test (org.junit.Test)19 Path (java.nio.file.Path)16 FileInputStream (java.io.FileInputStream)13 FileNotFoundException (java.io.FileNotFoundException)12 ByteBuffer (java.nio.ByteBuffer)10 InputStream (java.io.InputStream)5 Properties (java.util.Properties)5 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 NonWritableChannelException (java.nio.channels.NonWritableChannelException)4 NoSuchFileException (java.nio.file.NoSuchFileException)4 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)4 Map (java.util.Map)4 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)3