Search in sources :

Example 61 with FileLock

use of java.nio.channels.FileLock in project spf4j by zolyfarkas.

the class TimeSeriesDatabase method reReadTableInfos.

public void reReadTableInfos() throws IOException {
    synchronized (path) {
        FileLock lock = ch.lock(0, Long.MAX_VALUE, true);
        try {
            // reread toc
            toc = new TableOfContents(file, toc.getLocation());
            readTableInfos();
        } catch (IOException | RuntimeException e) {
            try {
                lock.release();
                throw e;
            } catch (IOException ex) {
                ex.addSuppressed(e);
                throw ex;
            }
        }
        lock.release();
    }
}
Also used : FileLock(java.nio.channels.FileLock) IOException(java.io.IOException)

Example 62 with FileLock

use of java.nio.channels.FileLock in project spf4j by zolyfarkas.

the class TimeSeriesDatabase method addTSTable.

public void addTSTable(final String tableName, final byte[] tableMetaData, final int sampleTime, final String[] columnNames, final byte[][] columnMetaData) throws IOException {
    synchronized (path) {
        if (hasTSTable(tableName)) {
            throw new IllegalArgumentException("group already exists " + tableName);
        }
        flush();
        FileLock lock = ch.lock();
        TSTable colInfo;
        try {
            readLastTableInfo();
            // write column information at the end of the file.
            file.seek(file.length());
            colInfo = new TSTable(tableName, tableMetaData, columnNames, columnMetaData, sampleTime, file.getFilePointer());
            colInfo.writeTo(file);
            // update refferences to this new TableInfo.
            if (lastTableInfo != null) {
                lastTableInfo.setNextColumnInfo(colInfo.getLocation(), file);
            } else {
                toc.setFirstTableInfo(colInfo.getLocation(), file);
            }
            toc.setLastTableInfo(colInfo.getLocation(), file);
        } catch (IOException | RuntimeException e) {
            try {
                lock.release();
                throw e;
            } catch (IOException ex) {
                ex.addSuppressed(e);
                throw ex;
            }
        }
        lock.release();
        lastTableInfo = colInfo;
        tables.put(tableName, colInfo);
    }
}
Also used : FileLock(java.nio.channels.FileLock) IOException(java.io.IOException)

Example 63 with FileLock

use of java.nio.channels.FileLock in project twister2 by DSC-SPIDAL.

the class KubernetesWorker method updateCompletions.

/**
 * update the count in the shared file with a lock
 * to let other workers in this pod to know that a worker has finished
 * @return
 */
public static int updateCompletions() {
    String completionsFile = POD_SHARED_VOLUME + "/" + COMPLETIONS_FILE_NAME;
    try {
        Path path = Paths.get(completionsFile);
        FileChannel fileChannel = FileChannel.open(path, StandardOpenOption.WRITE, StandardOpenOption.READ);
        LOG.info("Opened File channel. Acquiring lock ...");
        // exclusive lock
        FileLock lock = fileChannel.lock();
        LOG.info("Acquired the file lock. Validity of the lock: " + lock.isValid());
        // read the counter from the file
        ByteBuffer buffer = ByteBuffer.allocate(20);
        int noOfBytesRead = fileChannel.read(buffer);
        byte[] readByteArray = buffer.array();
        String inStr = new String(readByteArray, 0, noOfBytesRead, StandardCharsets.UTF_8);
        int count = Integer.parseInt(inStr);
        // update the counter and write back to the file
        count++;
        String outStr = Integer.toString(count);
        byte[] outByteArray = outStr.getBytes(StandardCharsets.UTF_8);
        ByteBuffer outBuffer = ByteBuffer.wrap(outByteArray);
        fileChannel.write(outBuffer, 0);
        LOG.info("Counter in file [" + completionsFile + "] updated to: " + count);
        // close the file channel and release the lock
        // also releases the lock
        fileChannel.close();
        return count;
    } catch (IOException e) {
        LOG.log(Level.SEVERE, "Exception when updating the counter in file: " + completionsFile, e);
        return -1;
    }
}
Also used : Path(java.nio.file.Path) FileChannel(java.nio.channels.FileChannel) FileLock(java.nio.channels.FileLock) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer)

Example 64 with FileLock

use of java.nio.channels.FileLock in project apache-kafka-on-k8s by banzaicloud.

the class StateDirectory method lockGlobalState.

synchronized boolean lockGlobalState() throws IOException {
    if (globalStateLock != null) {
        log.trace("{} Found cached state dir lock for the global task", logPrefix());
        return true;
    }
    final File lockFile = new File(globalStateDir(), LOCK_FILE_NAME);
    final FileChannel channel;
    try {
        channel = FileChannel.open(lockFile.toPath(), StandardOpenOption.CREATE, StandardOpenOption.WRITE);
    } catch (NoSuchFileException e) {
        // file, in this case we will return immediately indicating locking failed.
        return false;
    }
    final FileLock fileLock = tryLock(channel);
    if (fileLock == null) {
        channel.close();
        return false;
    }
    globalStateChannel = channel;
    globalStateLock = fileLock;
    log.debug("{} Acquired global state dir lock", logPrefix());
    return true;
}
Also used : FileChannel(java.nio.channels.FileChannel) NoSuchFileException(java.nio.file.NoSuchFileException) FileLock(java.nio.channels.FileLock) File(java.io.File)

Example 65 with FileLock

use of java.nio.channels.FileLock in project apache-kafka-on-k8s by banzaicloud.

the class StateDirectory method lock.

/**
 * Get the lock for the {@link TaskId}s directory if it is available
 * @param taskId
 * @return true if successful
 * @throws IOException
 */
synchronized boolean lock(final TaskId taskId) throws IOException {
    final File lockFile;
    // we already have the lock so bail out here
    final LockAndOwner lockAndOwner = locks.get(taskId);
    if (lockAndOwner != null && lockAndOwner.owningThread.equals(Thread.currentThread().getName())) {
        log.trace("{} Found cached state dir lock for task {}", logPrefix(), taskId);
        return true;
    } else if (lockAndOwner != null) {
        // another thread owns the lock
        return false;
    }
    try {
        lockFile = new File(directoryForTask(taskId), LOCK_FILE_NAME);
    } catch (ProcessorStateException e) {
        // has concurrently deleted the directory
        return false;
    }
    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(channel);
    if (lock != null) {
        locks.put(taskId, new LockAndOwner(Thread.currentThread().getName(), lock));
        log.debug("{} Acquired state dir lock for task {}", logPrefix(), taskId);
    }
    return lock != null;
}
Also used : FileChannel(java.nio.channels.FileChannel) NoSuchFileException(java.nio.file.NoSuchFileException) FileLock(java.nio.channels.FileLock) File(java.io.File) ProcessorStateException(org.apache.kafka.streams.errors.ProcessorStateException)

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