Search in sources :

Example 11 with FileChannel

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

the class Utils method readFileAsString.

/**
     * Attempt to read a file as a string
     * @throws IOException
     */
public static String readFileAsString(String path, Charset charset) throws IOException {
    if (charset == null)
        charset = Charset.defaultCharset();
    try (FileInputStream stream = new FileInputStream(new File(path))) {
        FileChannel fc = stream.getChannel();
        MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());
        return charset.decode(bb).toString();
    }
}
Also used : MappedByteBuffer(java.nio.MappedByteBuffer) FileChannel(java.nio.channels.FileChannel) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 12 with FileChannel

use of java.nio.channels.FileChannel 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 13 with FileChannel

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

the class StateDirectoryTest method shouldUnlockGlobalStateDirectory.

@Test
public void shouldUnlockGlobalStateDirectory() throws Exception {
    final FileChannel channel = FileChannel.open(new File(directory.globalStateDir(), StateDirectory.LOCK_FILE_NAME).toPath(), StandardOpenOption.CREATE, StandardOpenOption.WRITE);
    directory.lockGlobalState(1);
    directory.unlockGlobalState();
    // should lock without any exceptions
    channel.lock();
}
Also used : FileChannel(java.nio.channels.FileChannel) File(java.io.File) Test(org.junit.Test)

Example 14 with FileChannel

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

the class StateDirectoryTest method shouldReleaseTaskStateDirectoryLock.

@Test
public void shouldReleaseTaskStateDirectoryLock() throws Exception {
    final TaskId taskId = new TaskId(0, 0);
    final File taskDirectory = directory.directoryForTask(taskId);
    directory.lock(taskId, 1);
    directory.unlock(taskId);
    final FileChannel channel = FileChannel.open(new File(taskDirectory, StateDirectory.LOCK_FILE_NAME).toPath(), StandardOpenOption.CREATE, StandardOpenOption.WRITE);
    channel.tryLock();
}
Also used : TaskId(org.apache.kafka.streams.processor.TaskId) FileChannel(java.nio.channels.FileChannel) File(java.io.File) Test(org.junit.Test)

Example 15 with FileChannel

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

the class StateDirectoryTest method shouldLockMulitpleTaskDirectories.

@Test
public void shouldLockMulitpleTaskDirectories() throws Exception {
    final TaskId taskId = new TaskId(0, 0);
    final File task1Dir = directory.directoryForTask(taskId);
    final TaskId taskId2 = new TaskId(1, 0);
    final File task2Dir = directory.directoryForTask(taskId2);
    directory.lock(taskId, 0);
    directory.lock(taskId2, 0);
    final FileChannel channel1 = FileChannel.open(new File(task1Dir, StateDirectory.LOCK_FILE_NAME).toPath(), StandardOpenOption.CREATE, StandardOpenOption.WRITE);
    final FileChannel channel2 = FileChannel.open(new File(task2Dir, StateDirectory.LOCK_FILE_NAME).toPath(), StandardOpenOption.CREATE, StandardOpenOption.WRITE);
    try {
        channel1.tryLock();
        channel2.tryLock();
        fail("shouldn't be able to lock already locked directory");
    } catch (OverlappingFileLockException e) {
    // pass
    }
}
Also used : TaskId(org.apache.kafka.streams.processor.TaskId) FileChannel(java.nio.channels.FileChannel) File(java.io.File) OverlappingFileLockException(java.nio.channels.OverlappingFileLockException) Test(org.junit.Test)

Aggregations

FileChannel (java.nio.channels.FileChannel)629 IOException (java.io.IOException)227 ByteBuffer (java.nio.ByteBuffer)205 File (java.io.File)185 FileInputStream (java.io.FileInputStream)164 FileOutputStream (java.io.FileOutputStream)147 RandomAccessFile (java.io.RandomAccessFile)144 Test (org.junit.Test)95 MappedByteBuffer (java.nio.MappedByteBuffer)78 Path (java.nio.file.Path)37 FileLock (java.nio.channels.FileLock)32 FileNotFoundException (java.io.FileNotFoundException)29 Random (java.util.Random)12 OutputStream (java.io.OutputStream)11 ArrayList (java.util.ArrayList)11 AsynchronousFileChannel (java.nio.channels.AsynchronousFileChannel)10 OverlappingFileLockException (java.nio.channels.OverlappingFileLockException)10 LinkedList (java.util.LinkedList)10 ProjectWorkspace (com.facebook.buck.testutil.integration.ProjectWorkspace)9 BufferedReader (java.io.BufferedReader)9