Search in sources :

Example 11 with LockRefreshToken

use of com.palantir.lock.LockRefreshToken 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);
}
Also used : AtlasRowLockDescriptor(com.palantir.lock.AtlasRowLockDescriptor) LockDescriptor(com.palantir.lock.LockDescriptor) LockMode(com.palantir.lock.LockMode) Map(java.util.Map) SortedMap(java.util.SortedMap) Cell(com.palantir.atlasdb.keyvalue.api.Cell) LockRequest(com.palantir.lock.LockRequest) LockRefreshToken(com.palantir.lock.LockRefreshToken)

Example 12 with LockRefreshToken

use of com.palantir.lock.LockRefreshToken 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);
    }
}
Also used : AtlasTimestampLockDescriptor(com.palantir.lock.AtlasTimestampLockDescriptor) LockDescriptor(com.palantir.lock.LockDescriptor) LockRefreshToken(com.palantir.lock.LockRefreshToken)

Example 13 with LockRefreshToken

use of com.palantir.lock.LockRefreshToken in project atlasdb by palantir.

the class LockServiceImpl method refreshLockRefreshTokens.

@Override
public Set<LockRefreshToken> refreshLockRefreshTokens(Iterable<LockRefreshToken> tokens) {
    List<HeldLocksToken> fakeTokens = Lists.newArrayList();
    LockDescriptor fakeLockDesc = StringLockDescriptor.of("refreshLockRefreshTokens");
    SortedLockCollection<LockDescriptor> fakeLockSet = LockCollections.of(ImmutableSortedMap.of(fakeLockDesc, LockMode.READ));
    for (LockRefreshToken token : tokens) {
        fakeTokens.add(new HeldLocksToken(token.getTokenId(), LockClient.ANONYMOUS, 0L, 0L, fakeLockSet, maxAllowedLockTimeout, 0L, "UnknownThread-refreshLockRefreshTokens"));
    }
    return ImmutableSet.copyOf(Iterables.transform(refreshTokens(fakeTokens), HeldLocksTokens.getRefreshTokenFun()));
}
Also used : LockDescriptor(com.palantir.lock.LockDescriptor) StringLockDescriptor(com.palantir.lock.StringLockDescriptor) HeldLocksToken(com.palantir.lock.HeldLocksToken) SimpleHeldLocksToken(com.palantir.lock.SimpleHeldLocksToken) LockRefreshToken(com.palantir.lock.LockRefreshToken)

Example 14 with LockRefreshToken

use of com.palantir.lock.LockRefreshToken in project atlasdb by palantir.

the class LegacyTimelockServiceTest method mockImmutableTsLockResponse.

private LockRefreshToken mockImmutableTsLockResponse() throws InterruptedException {
    LockDescriptor descriptor = AtlasTimestampLockDescriptor.of(FRESH_TIMESTAMP);
    com.palantir.lock.LockRequest expectedRequest = com.palantir.lock.LockRequest.builder(ImmutableSortedMap.of(descriptor, LockMode.READ)).withLockedInVersionId(FRESH_TIMESTAMP).build();
    LockRefreshToken expectedToken = new LockRefreshToken(BigInteger.ONE, 123L);
    when(lockService.lock(LOCK_CLIENT.getClientId(), expectedRequest)).thenReturn(expectedToken);
    return expectedToken;
}
Also used : AtlasTimestampLockDescriptor(com.palantir.lock.AtlasTimestampLockDescriptor) LockDescriptor(com.palantir.lock.LockDescriptor) StringLockDescriptor(com.palantir.lock.StringLockDescriptor) LockRefreshToken(com.palantir.lock.LockRefreshToken)

Example 15 with LockRefreshToken

use of com.palantir.lock.LockRefreshToken in project atlasdb by palantir.

the class LockTokenConverterTest method assertConversionFromAndToLegacyPreservesId.

private void assertConversionFromAndToLegacyPreservesId(LockRefreshToken lockRefreshToken) {
    LockToken tokenV2 = LockTokenConverter.toTokenV2(lockRefreshToken);
    LockRefreshToken reconverted = LockTokenConverter.toLegacyToken(tokenV2);
    assertThat(reconverted).isEqualTo(lockRefreshToken);
}
Also used : LockToken(com.palantir.lock.v2.LockToken) LockRefreshToken(com.palantir.lock.LockRefreshToken)

Aggregations

LockRefreshToken (com.palantir.lock.LockRefreshToken)22 Test (org.junit.Test)12 LockService (com.palantir.lock.LockService)6 LockDescriptor (com.palantir.lock.LockDescriptor)5 HeldLocksToken (com.palantir.lock.HeldLocksToken)4 LockRequest (com.palantir.lock.LockRequest)4 SimpleHeldLocksToken (com.palantir.lock.SimpleHeldLocksToken)3 StringLockDescriptor (com.palantir.lock.StringLockDescriptor)3 BigInteger (java.math.BigInteger)3 AtlasTimestampLockDescriptor (com.palantir.lock.AtlasTimestampLockDescriptor)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 GlobalClock (com.palantir.atlasdb.cleaner.GlobalClock)1 SingleBackendCliTestRunner (com.palantir.atlasdb.cli.runner.SingleBackendCliTestRunner)1 Cell (com.palantir.atlasdb.keyvalue.api.Cell)1 DaggerTestAtlasDbServices (com.palantir.atlasdb.services.test.DaggerTestAtlasDbServices)1 TestAtlasDbServices (com.palantir.atlasdb.services.test.TestAtlasDbServices)1 ExceptionMatchers (com.palantir.atlasdb.timelock.util.ExceptionMatchers)1 Clock (com.palantir.common.time.Clock)1 AtlasRowLockDescriptor (com.palantir.lock.AtlasRowLockDescriptor)1 LockClient (com.palantir.lock.LockClient)1