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