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();
}
}
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;
}
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();
}
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();
}
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
}
}
Aggregations