Search in sources :

Example 1 with FileLock

use of java.nio.channels.FileLock in project kafka by apache.

the class StateDirectory method lock.

/**
     * Get the lock for the {@link TaskId}s directory if it is available
     * @param taskId
     * @param retry
     * @return true if successful
     * @throws IOException
     */
boolean lock(final TaskId taskId, int retry) throws IOException {
    // we already have the lock so bail out here
    if (locks.containsKey(taskId)) {
        return true;
    }
    final File lockFile = new File(directoryForTask(taskId), LOCK_FILE_NAME);
    final FileChannel channel;
    try {
        channel = getOrCreateFileChannel(taskId, lockFile.toPath());
    } catch (NoSuchFileException e) {
        // file, in this case we will return immediately indicating locking failed.
        return false;
    }
    final FileLock lock = tryLock(retry, channel);
    if (lock != null) {
        locks.put(taskId, lock);
    }
    return lock != null;
}
Also used : FileChannel(java.nio.channels.FileChannel) NoSuchFileException(java.nio.file.NoSuchFileException) FileLock(java.nio.channels.FileLock) File(java.io.File)

Example 2 with FileLock

use of java.nio.channels.FileLock in project kafka by apache.

the class StateDirectory method tryLock.

private FileLock tryLock(int retry, final FileChannel channel) throws IOException {
    FileLock lock = tryAcquireLock(channel);
    while (lock == null && retry > 0) {
        try {
            Thread.sleep(200);
        } catch (Exception ex) {
        // do nothing
        }
        retry--;
        lock = tryAcquireLock(channel);
    }
    return lock;
}
Also used : FileLock(java.nio.channels.FileLock) OverlappingFileLockException(java.nio.channels.OverlappingFileLockException) NoSuchFileException(java.nio.file.NoSuchFileException) ProcessorStateException(org.apache.kafka.streams.errors.ProcessorStateException) IOException(java.io.IOException)

Example 3 with FileLock

use of java.nio.channels.FileLock in project android_frameworks_base by ParanoidAndroid.

the class MiniThumbFile method saveMiniThumbToFile.

public synchronized void saveMiniThumbToFile(byte[] data, long id, long magic) throws IOException {
    RandomAccessFile r = miniThumbDataFile();
    if (r == null)
        return;
    long pos = id * BYTES_PER_MINTHUMB;
    FileLock lock = null;
    try {
        if (data != null) {
            if (data.length > BYTES_PER_MINTHUMB - HEADER_SIZE) {
                // not enough space to store it.
                return;
            }
            mBuffer.clear();
            mBuffer.put((byte) 1);
            mBuffer.putLong(magic);
            mBuffer.putInt(data.length);
            mBuffer.put(data);
            mBuffer.flip();
            lock = mChannel.lock(pos, BYTES_PER_MINTHUMB, false);
            mChannel.write(mBuffer, pos);
        }
    } catch (IOException ex) {
        Log.e(TAG, "couldn't save mini thumbnail data for " + id + "; ", ex);
        throw ex;
    } catch (RuntimeException ex) {
        // Other NIO related exception like disk full, read only channel..etc
        Log.e(TAG, "couldn't save mini thumbnail data for " + id + "; disk full or mount read-only? " + ex.getClass());
    } finally {
        try {
            if (lock != null)
                lock.release();
        } catch (IOException ex) {
        // ignore it.
        }
    }
}
Also used : RandomAccessFile(java.io.RandomAccessFile) FileLock(java.nio.channels.FileLock) IOException(java.io.IOException)

Example 4 with FileLock

use of java.nio.channels.FileLock in project XobotOS by xamarin.

the class FileChannelImpl method addLock.

/**
     * Add a new pending lock to the manager. Throws an exception if the lock
     * would overlap an existing lock. Once the lock is acquired it remains in
     * this set as an acquired lock.
     */
private synchronized void addLock(FileLock lock) throws OverlappingFileLockException {
    long lockEnd = lock.position() + lock.size();
    for (FileLock existingLock : locks) {
        if (existingLock.position() > lockEnd) {
            // cannot overlap).
            break;
        }
        if (existingLock.overlaps(lock.position(), lock.size())) {
            throw new OverlappingFileLockException();
        }
    }
    locks.add(lock);
}
Also used : FileLock(java.nio.channels.FileLock) OverlappingFileLockException(java.nio.channels.OverlappingFileLockException)

Example 5 with FileLock

use of java.nio.channels.FileLock in project XobotOS by xamarin.

the class FileChannelImpl method basicLock.

private FileLock basicLock(long position, long size, boolean shared, boolean wait) throws IOException {
    int accessMode = (mode & O_ACCMODE);
    if (accessMode == O_RDONLY) {
        if (!shared) {
            throw new NonWritableChannelException();
        }
    } else if (accessMode == O_WRONLY) {
        if (shared) {
            throw new NonReadableChannelException();
        }
    }
    if (position < 0 || size < 0) {
        throw new IllegalArgumentException("position=" + position + " size=" + size);
    }
    FileLock pendingLock = new FileLockImpl(this, position, size, shared);
    addLock(pendingLock);
    StructFlock flock = new StructFlock();
    flock.l_type = (short) (shared ? F_RDLCK : F_WRLCK);
    flock.l_whence = (short) SEEK_SET;
    flock.l_start = position;
    flock.l_len = translateLockLength(size);
    boolean success = false;
    try {
        success = (Libcore.os.fcntlFlock(fd, wait ? F_SETLKW64 : F_SETLK64, flock) != -1);
    } catch (ErrnoException errnoException) {
        throw errnoException.rethrowAsIOException();
    } finally {
        if (!success) {
            removeLock(pendingLock);
        }
    }
    return success ? pendingLock : null;
}
Also used : NonReadableChannelException(java.nio.channels.NonReadableChannelException) ErrnoException(libcore.io.ErrnoException) StructFlock(libcore.io.StructFlock) FileLock(java.nio.channels.FileLock) NonWritableChannelException(java.nio.channels.NonWritableChannelException)

Aggregations

FileLock (java.nio.channels.FileLock)253 IOException (java.io.IOException)133 RandomAccessFile (java.io.RandomAccessFile)103 FileChannel (java.nio.channels.FileChannel)87 File (java.io.File)81 OverlappingFileLockException (java.nio.channels.OverlappingFileLockException)42 FileOutputStream (java.io.FileOutputStream)30 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 ByteArrayInputStream (java.io.ByteArrayInputStream)3