use of com.palantir.lock.LockDescriptor in project atlasdb by palantir.
the class SnapshotTransaction method getLocksForWrites.
protected Set<LockDescriptor> getLocksForWrites() {
Set<LockDescriptor> result = Sets.newHashSet();
Iterable<TableReference> allTables = IterableUtils.append(writesByTable.keySet(), TransactionConstants.TRANSACTION_TABLE);
for (TableReference tableRef : allTables) {
if (tableRef.equals(TransactionConstants.TRANSACTION_TABLE)) {
result.add(AtlasRowLockDescriptor.of(TransactionConstants.TRANSACTION_TABLE.getQualifiedName(), TransactionConstants.getValueForTimestamp(getStartTimestamp())));
continue;
}
ConflictHandler conflictHandler = getConflictHandlerForTable(tableRef);
if (conflictHandler == ConflictHandler.RETRY_ON_WRITE_WRITE_CELL) {
for (Cell cell : getLocalWrites(tableRef).keySet()) {
result.add(AtlasCellLockDescriptor.of(tableRef.getQualifiedName(), cell.getRowName(), cell.getColumnName()));
}
} else if (conflictHandler != ConflictHandler.IGNORE_ALL) {
Cell lastCell = null;
for (Cell cell : getLocalWrites(tableRef).keySet()) {
if (lastCell == null || !Arrays.equals(lastCell.getRowName(), cell.getRowName())) {
result.add(AtlasRowLockDescriptor.of(tableRef.getQualifiedName(), cell.getRowName()));
}
lastCell = cell;
}
}
}
return result;
}
use of com.palantir.lock.LockDescriptor in project atlasdb by palantir.
the class SynchronousLockClient method lock.
@Override
public LockRefreshToken lock(String client, String lockName) throws InterruptedException {
LockDescriptor descriptor = StringLockDescriptor.of(lockName);
LockRequest request = LockRequest.builder(ImmutableSortedMap.of(descriptor, LockMode.WRITE)).doNotBlock().build();
return lockService.lock(client, request);
}
use of com.palantir.lock.LockDescriptor in project atlasdb by palantir.
the class AssertLockedKeyValueService method put.
@Override
public void put(TableReference tableRef, Map<Cell, byte[]> values, long timestamp) {
if (tableRef.equals(TransactionConstants.TRANSACTION_TABLE)) {
SortedMap<LockDescriptor, LockMode> mapToAssertLockHeld = Maps.newTreeMap();
SortedMap<LockDescriptor, LockMode> mapToAssertLockNotHeld = Maps.newTreeMap();
for (Map.Entry<Cell, byte[]> e : values.entrySet()) {
LockDescriptor descriptor = AtlasRowLockDescriptor.of(tableRef.getQualifiedName(), e.getKey().getRowName());
if (Arrays.equals(e.getValue(), TransactionConstants.getValueForTimestamp(TransactionConstants.FAILED_COMMIT_TS))) {
mapToAssertLockNotHeld.put(descriptor, LockMode.READ);
} else {
mapToAssertLockHeld.put(descriptor, LockMode.READ);
}
}
try {
if (!mapToAssertLockHeld.isEmpty()) {
LockRequest request = LockRequest.builder(mapToAssertLockHeld).doNotBlock().lockAsManyAsPossible().build();
LockRefreshToken lock = lockService.lock(LockClient.ANONYMOUS.getClientId(), request);
Validate.isTrue(lock == null, "these should already be held");
}
if (!mapToAssertLockNotHeld.isEmpty()) {
LockRequest request = LockRequest.builder(mapToAssertLockNotHeld).doNotBlock().build();
LockRefreshToken lock = lockService.lock(LockClient.ANONYMOUS.getClientId(), request);
Validate.isTrue(lock != null, "these should already be waited for");
}
} catch (InterruptedException e) {
throw Throwables.throwUncheckedException(e);
}
}
super.put(tableRef, values, timestamp);
}
use of com.palantir.lock.LockDescriptor in project atlasdb by palantir.
the class LegacyTimelockService method lockImmutableTimestamp.
@Override
public LockImmutableTimestampResponse lockImmutableTimestamp(LockImmutableTimestampRequest request) {
long immutableLockTs = timestampService.getFreshTimestamp();
LockDescriptor lockDesc = AtlasTimestampLockDescriptor.of(immutableLockTs);
com.palantir.lock.LockRequest lockRequest = com.palantir.lock.LockRequest.builder(ImmutableSortedMap.of(lockDesc, LockMode.READ)).withLockedInVersionId(immutableLockTs).build();
LockRefreshToken lock;
try {
lock = lockService.lock(immutableTsLockClient.getClientId(), lockRequest);
} catch (InterruptedException e) {
throw Throwables.throwUncheckedException(e);
}
try {
return LockImmutableTimestampResponse.of(getImmutableTimestampInternal(immutableLockTs), LockTokenConverter.toTokenV2(lock));
} catch (Throwable e) {
if (lock != null) {
lockService.unlock(lock);
}
throw Throwables.rewrapAndThrowUncheckedException(e);
}
}
use of com.palantir.lock.LockDescriptor in project atlasdb by palantir.
the class LockServiceImpl method lockWithFullLockResponse.
@Override
public // We're concerned about sanitizing logs at the info level and above. This method just logs at debug and info.
LockResponse lockWithFullLockResponse(LockClient client, LockRequest request) throws InterruptedException {
Preconditions.checkNotNull(client);
Preconditions.checkArgument(!client.equals(INTERNAL_LOCK_GRANT_CLIENT));
Preconditions.checkArgument(request.getLockTimeout().compareTo(maxAllowedLockTimeout) <= 0, "Requested lock timeout (%s) is greater than maximum allowed lock timeout (%s)", request.getLockTimeout(), maxAllowedLockTimeout);
long startTime = System.currentTimeMillis();
if (requestLogger.isDebugEnabled()) {
requestLogger.debug("LockServiceImpl processing lock request {} for requesting thread {}", UnsafeArg.of("lockRequest", request), SafeArg.of("requestingThread", request.getCreatingThreadName()));
}
Map<ClientAwareReadWriteLock, LockMode> locks = Maps.newLinkedHashMap();
if (isShutDown.get()) {
throw new ServiceNotAvailableException("This lock server is shut down.");
}
try {
boolean indefinitelyBlocking = isIndefinitelyBlocking(request.getBlockingMode());
if (indefinitelyBlocking) {
indefinitelyBlockingThreads.add(Thread.currentThread());
}
outstandingLockRequestMultimap.put(client, request);
Map<LockDescriptor, LockClient> failedLocks = Maps.newHashMap();
@Nullable Long deadline = (request.getBlockingDuration() == null) ? null : System.nanoTime() + request.getBlockingDuration().toNanos();
if (request.getBlockingMode() == BLOCK_UNTIL_TIMEOUT) {
if (request.getLockGroupBehavior() == LOCK_AS_MANY_AS_POSSIBLE) {
tryLocks(client, request, DO_NOT_BLOCK, null, LOCK_AS_MANY_AS_POSSIBLE, locks, failedLocks);
}
}
tryLocks(client, request, request.getBlockingMode(), deadline, request.getLockGroupBehavior(), locks, failedLocks);
if (request.getBlockingMode() == BlockingMode.BLOCK_INDEFINITELY_THEN_RELEASE) {
if (log.isTraceEnabled()) {
logNullResponse(client, request, null);
}
if (requestLogger.isDebugEnabled()) {
requestLogger.debug("Timed out requesting {} for requesting thread {} after {} ms", UnsafeArg.of("request", request), SafeArg.of("threadName", request.getCreatingThreadName()), SafeArg.of("timeoutMillis", System.currentTimeMillis() - startTime));
}
return new LockResponse(failedLocks);
}
if (locks.isEmpty() || ((request.getLockGroupBehavior() == LOCK_ALL_OR_NONE) && (locks.size() < request.getLockDescriptors().size()))) {
if (log.isTraceEnabled()) {
logNullResponse(client, request, null);
}
if (requestLogger.isDebugEnabled()) {
requestLogger.debug("Failed to acquire all locks for {} for requesting thread {} after {} ms", UnsafeArg.of("request", request), SafeArg.of("threadName", request.getCreatingThreadName()), SafeArg.of("waitMillis", System.currentTimeMillis() - startTime));
}
if (requestLogger.isTraceEnabled()) {
logLockAcquisitionFailure(failedLocks);
}
return new LockResponse(null, failedLocks);
}
Builder<LockDescriptor, LockMode> lockDescriptorMap = ImmutableSortedMap.naturalOrder();
for (Entry<ClientAwareReadWriteLock, LockMode> entry : locks.entrySet()) {
lockDescriptorMap.put(entry.getKey().getDescriptor(), entry.getValue());
}
if (request.getVersionId() != null) {
versionIdMap.put(client, request.getVersionId());
}
HeldLocksToken token = createHeldLocksToken(client, LockCollections.of(lockDescriptorMap.build()), LockCollections.of(locks), request.getLockTimeout(), request.getVersionId(), request.getCreatingThreadName());
locks.clear();
if (log.isTraceEnabled()) {
logNullResponse(client, request, token);
}
if (Thread.interrupted()) {
throw new InterruptedException("Interrupted while locking.");
}
if (requestLogger.isDebugEnabled()) {
requestLogger.debug("Successfully acquired locks {} for requesting thread {} after {} ms", UnsafeArg.of("request", request), SafeArg.of("threadName", request.getCreatingThreadName()), SafeArg.of("waitMillis", System.currentTimeMillis() - startTime));
}
return new LockResponse(token, failedLocks);
} finally {
outstandingLockRequestMultimap.remove(client, request);
indefinitelyBlockingThreads.remove(Thread.currentThread());
try {
for (Entry<ClientAwareReadWriteLock, LockMode> entry : locks.entrySet()) {
entry.getKey().get(client, entry.getValue()).unlock();
}
} catch (Throwable e) {
// (authorized)
log.error("Internal lock server error: state has been corrupted!!", UnsafeArg.of("exception", e), SafeArg.of("stacktrace", e.getStackTrace()));
throw Throwables.throwUncheckedException(e);
}
}
}
Aggregations