use of de.invesdwin.util.concurrent.lock.ILock in project invesdwin-context-persistence by subes.
the class ADelegateTable method getTableWithReadLock.
private Table<H, V> getTableWithReadLock(final boolean forUpdate) {
maybePurgeTable();
// directly return table with read lock if not null
final ILock readLock = getReadLock(forUpdate);
readLock.lock();
if (tableFinalizer.table != null) {
return tableFinalizer.table;
}
readLock.unlock();
if (!initializing.compareAndSet(false, true)) {
while (initializing.get()) {
FTimeUnit.MILLISECONDS.sleepNoInterrupt(1);
}
return getTableWithReadLock(forUpdate);
} else {
try {
return new ARetryCallable<Table<H, V>>(new RetryOriginator(ADelegateTable.class, "initializeTableInitLocked", getName())) {
@Override
protected Table<H, V> callRetry() throws Exception {
return initializeTableInitLocked(readLock);
}
}.call();
} finally {
initializing.set(false);
}
}
}
use of de.invesdwin.util.concurrent.lock.ILock in project invesdwin-context-matlab by subes.
the class JavasciScriptTaskRunnerMatlab method run.
@Override
public <T> T run(final AScriptTaskMatlab<T> scriptTask) {
// get session
final JavasciScriptTaskEngineMatlab engine = new JavasciScriptTaskEngineMatlab(ScilabWrapper.INSTANCE);
final ILock lock = engine.getSharedLock();
lock.lock();
try {
// inputs
scriptTask.populateInputs(engine.getInputs());
// execute
scriptTask.executeScript(engine);
// results
final T result = scriptTask.extractResults(engine.getResults());
engine.close();
// return
return result;
} catch (final Throwable t) {
throw Throwables.propagate(t);
} finally {
lock.unlock();
}
}
use of de.invesdwin.util.concurrent.lock.ILock in project invesdwin-context-persistence by subes.
the class ATimeSeriesUpdater method update.
@Override
public final boolean update() throws IncompleteUpdateFoundException {
final ILock writeLock = table.getTableLock(key).writeLock();
try {
if (!writeLock.tryLock(1, TimeUnit.MINUTES)) {
throw Locks.getLockTrace().handleLockException(writeLock.getName(), new RetryLaterRuntimeException("Write lock could not be acquired for table [" + table.getName() + "] and key [" + key + "]. Please ensure all iterators are closed!"));
}
} catch (final InterruptedException e) {
Thread.currentThread().interrupt();
throw new RuntimeException(e);
}
final File updateLockSyncFile = new File(updateLockFile.getAbsolutePath() + ".sync");
try (FileChannelLock updateLockSyncFileLock = new FileChannelLock(updateLockSyncFile)) {
if (updateLockSyncFile.exists() || !updateLockSyncFileLock.tryLock() || updateLockFile.exists()) {
throw new IncompleteUpdateFoundException("Incomplete update found for table [" + table.getName() + "] and key [" + key + "], need to clean everything up to restore all from scratch.");
}
try {
try {
Files.touch(updateLockFile);
} catch (final IOException e) {
throw new RuntimeException(e);
}
final Instant updateStart = new Instant();
onUpdateStart();
doUpdate();
onUpdateFinished(updateStart);
Assertions.assertThat(updateLockFile.delete()).isTrue();
return true;
} catch (final Throwable t) {
final IncompleteUpdateFoundException incompleteException = Throwables.getCauseByType(t, IncompleteUpdateFoundException.class);
if (incompleteException != null) {
throw incompleteException;
} else {
throw new IncompleteUpdateFoundException("Something unexpected went wrong", t);
}
}
} finally {
writeLock.unlock();
}
}
use of de.invesdwin.util.concurrent.lock.ILock in project invesdwin-context-persistence by subes.
the class ADelegateRangeTable method getTableWithReadLock.
private RangeTable<H, R, V> getTableWithReadLock(final boolean forUpdate) {
maybePurgeTable();
// directly return table with read lock if not null
final ILock readLock = getReadLock(forUpdate);
readLock.lock();
if (tableFinalizer.table != null) {
return tableFinalizer.table;
}
readLock.unlock();
if (!initializing.compareAndSet(false, true)) {
while (initializing.get()) {
FTimeUnit.MILLISECONDS.sleepNoInterrupt(1);
}
return getTableWithReadLock(forUpdate);
} else {
try {
return new ARetryCallable<RangeTable<H, R, V>>(new RetryOriginator(ADelegateRangeTable.class, "initializeTableInitLocked", getName())) {
@Override
protected RangeTable<H, R, V> callRetry() throws Exception {
return initializeTableInitLocked(readLock);
}
}.call();
} finally {
initializing.set(false);
}
}
}
use of de.invesdwin.util.concurrent.lock.ILock in project invesdwin-context-persistence by subes.
the class ASegmentedTimeSeriesStorageCache method maybeInitSegment.
public boolean maybeInitSegment(final SegmentedKey<K> segmentedKey, final Function<SegmentedKey<K>, ICloseableIterable<? extends V>> source) {
if (!assertValidSegment(segmentedKey)) {
return false;
}
// 1. check segment status in series storage
final IReadWriteLock segmentTableLock = segmentedTable.getTableLock(segmentedKey);
/*
* We need this synchronized block so that we don't collide on the write lock not being possible to be acquired
* after 1 minute. The ReadWriteLock object should be safe to lock via synchronized keyword since no internal
* synchronization occurs on that object itself
*/
synchronized (segmentTableLock) {
final SegmentStatus status = getSegmentStatusWithReadLock(segmentedKey, segmentTableLock);
// 2. if not existing or false, set status to false -> start segment update -> after update set status to true
if (status == null || status == SegmentStatus.INITIALIZING) {
final ILock segmentWriteLock = segmentTableLock.writeLock();
try {
if (!segmentWriteLock.tryLock(1, TimeUnit.MINUTES)) {
/*
* should not happen here because segment should not yet exist. Though if it happens we would
* rather like an exception instead of a deadlock!
*/
throw Locks.getLockTrace().handleLockException(segmentWriteLock.getName(), new RetryLaterRuntimeException("Write lock could not be acquired for table [" + segmentedTable.getName() + "] and key [" + segmentedKey + "]. Please ensure all iterators are closed!"));
}
} catch (final InterruptedException e1) {
throw new RuntimeException(e1);
}
try {
// no double checked locking required between read and write lock here because of the outer synchronized block
if (status == SegmentStatus.INITIALIZING) {
// initialization got aborted, retry from a fresh state
segmentedTable.deleteRange(segmentedKey);
storage.getSegmentStatusTable().delete(hashKey, segmentedKey.getSegment());
}
initSegmentWithStatusHandling(segmentedKey, source);
onSegmentCompleted(segmentedKey, readRangeValues(segmentedKey.getSegment().getFrom(), segmentedKey.getSegment().getTo(), DisabledLock.INSTANCE, null));
return true;
} finally {
segmentWriteLock.unlock();
}
}
}
// 3. if true do nothing
return false;
}
Aggregations