use of java.nio.channels.FileLock in project Chronicle-Queue by OpenHFT.
the class SingleTableStore method doWithExclusiveLock.
/**
* {@inheritDoc}
*/
@Override
public <R> R doWithExclusiveLock(Function<TableStore, ? extends R> code) {
final long timeoutAt = System.currentTimeMillis() + 2 * timeoutMS;
boolean warnedOnFailure = false;
try (final FileChannel channel = FileChannel.open(file().toPath(), StandardOpenOption.WRITE)) {
while (System.currentTimeMillis() < timeoutAt) {
try {
FileLock fileLock = channel.tryLock();
if (fileLock != null) {
return code.apply(this);
}
} catch (IOException | OverlappingFileLockException e) {
// failed to acquire the lock, wait until other operation completes
if (!warnedOnFailure) {
Jvm.debug().on(getClass(), "Failed to acquire a lock on the table store file. Retrying");
warnedOnFailure = true;
}
}
Jvm.pause(50L);
}
} catch (IOException e) {
throw new IllegalStateException("Couldn't open table store file for writing", e);
}
throw new IllegalStateException("Unable to claim exclusive lock on file " + file());
}
use of java.nio.channels.FileLock in project bnd by bndtools.
the class CAFS method write.
/**
* Store an input stream in the CAFS while calculating and returning the
* SHA-1 code.
*
* @param in The input stream to store.
* @return The SHA-1 code.
* @throws Exception if anything goes wrong
*/
public SHA1 write(InputStream in) throws Exception {
Deflater deflater = new Deflater();
MessageDigest md = MessageDigest.getInstance(ALGORITHM);
DigestInputStream din = new DigestInputStream(in, md);
ByteArrayOutputStream bout = new ByteArrayOutputStream();
DeflaterOutputStream dout = new DeflaterOutputStream(bout, deflater);
copy(din, dout);
synchronized (store) {
// First check if it already exists
SHA1 sha1 = new SHA1(md.digest());
long search = index.search(sha1.digest());
if (search > 0)
return sha1;
byte[] compressed = bout.toByteArray();
// we need to append this file to our store,
// which requires a lock. However, we are in a race
// so others can get the lock between us getting
// the length and someone else getting the lock.
// So we must verify after we get the lock that the
// length was unchanged.
FileLock lock = null;
try {
long insertPoint;
int recordLength = compressed.length + HEADERLENGTH;
while (true) {
insertPoint = store.length();
lock = channel.lock(insertPoint, recordLength, false);
if (store.length() == insertPoint)
break;
// We got the wrong lock, someone else
// got in between reading the length
// and locking
lock.release();
}
int totalLength = deflater.getTotalIn();
store.seek(insertPoint);
update(sha1.digest(), compressed, totalLength);
index.insert(sha1.digest(), insertPoint);
return sha1;
} finally {
if (lock != null)
lock.release();
}
}
}
use of java.nio.channels.FileLock in project bnd by bndtools.
the class PersistentMap method put.
public V put(String key, V value) {
init();
try {
V old = null;
SoftReference<V> ref = cache.get(key);
if (ref != null)
old = ref.get();
FileLock lock = lock();
try {
File file = new File(data, key);
codec.enc().to(file).put(value);
cache.put(key, new SoftReference<V>(value));
return old;
} finally {
unlock(lock);
}
} catch (RuntimeException e) {
throw e;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
use of java.nio.channels.FileLock in project bnd by bndtools.
the class PersistentMap method clear.
public void clear() {
init();
try {
FileLock lock = lock();
try {
IO.deleteWithException(data);
cache.clear();
IO.mkdirs(data);
} finally {
unlock(lock);
}
} catch (RuntimeException e) {
throw e;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
use of java.nio.channels.FileLock in project PocketHub by pockethub.
the class RequestWriter method write.
/**
* Write request to file
*
* @param request
* @return request
*/
public <V> V write(V request) {
RandomAccessFile dir = null;
FileLock lock = null;
ObjectOutputStream output = null;
try {
createDirectory(handle.getParentFile());
dir = new RandomAccessFile(handle, "rw");
lock = dir.getChannel().lock();
output = new ObjectOutputStream(new GZIPOutputStream(new FileOutputStream(dir.getFD()), 8192));
output.writeInt(version);
output.writeObject(request);
} catch (IOException e) {
Log.d(TAG, "Exception writing cache " + handle.getName(), e);
return null;
} finally {
if (output != null) {
try {
output.close();
} catch (IOException e) {
Log.d(TAG, "Exception closing stream", e);
}
}
if (lock != null) {
try {
lock.release();
} catch (IOException e) {
Log.d(TAG, "Exception unlocking file", e);
}
}
if (dir != null) {
try {
dir.close();
} catch (IOException e) {
Log.d(TAG, "Exception closing file", e);
}
}
}
return request;
}
Aggregations