Search in sources :

Example 6 with LockRequest

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

Example 7 with LockRequest

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

the class SynchronousLockClientTest method lockRequestIsWrite.

@Test
public void lockRequestIsWrite() throws InterruptedException {
    when(LOCK_CLIENT.lock(CLIENT, LOCK_NAME)).thenAnswer((invocation) -> {
        LockRequest request = (LockRequest) invocation.getArguments()[1];
        assertThat(request.getLocks(), contains(hasProperty("lockMode", is(LockMode.WRITE))));
        return null;
    });
    LOCK_CLIENT.lock(CLIENT, LOCK_NAME);
}
Also used : LockRequest(com.palantir.lock.LockRequest) Test(org.junit.Test)

Example 8 with LockRequest

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

the class SynchronousLockClientTest method lockRequestIsNonBlocking.

@Test
public void lockRequestIsNonBlocking() throws InterruptedException {
    when(LOCK_CLIENT.lock(CLIENT, LOCK_NAME)).thenAnswer((invocation) -> {
        LockRequest request = (LockRequest) invocation.getArguments()[1];
        assertThat(request.getBlockingMode(), is(BlockingMode.DO_NOT_BLOCK));
        return null;
    });
    LOCK_CLIENT.lock(CLIENT, LOCK_NAME);
}
Also used : LockRequest(com.palantir.lock.LockRequest) Test(org.junit.Test)

Example 9 with LockRequest

use of com.palantir.lock.LockRequest 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 10 with LockRequest

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

the class LockServiceStateLoggerTest method setUp.

@BeforeClass
public static void setUp() throws Exception {
    LockServiceTestUtils.cleanUpLogStateDir();
    LockClient clientA = LockClient.of("Client A");
    LockClient clientB = LockClient.of("Client B");
    LockDescriptor descriptor1 = StringLockDescriptor.of("logger-lock");
    LockDescriptor descriptor2 = StringLockDescriptor.of("logger-AAA");
    LockRequest request1 = LockRequest.builder(LockCollections.of(ImmutableSortedMap.of(descriptor1, LockMode.WRITE))).blockForAtMost(SimpleTimeDuration.of(1000, TimeUnit.MILLISECONDS)).build();
    LockRequest request2 = LockRequest.builder(LockCollections.of(ImmutableSortedMap.of(descriptor2, LockMode.WRITE))).blockForAtMost(SimpleTimeDuration.of(1000, TimeUnit.MILLISECONDS)).build();
    outstandingLockRequestMultimap.put(clientA, request1);
    outstandingLockRequestMultimap.put(clientB, request2);
    outstandingLockRequestMultimap.put(clientA, request2);
    HeldLocksToken token = LockServiceTestUtils.getFakeHeldLocksToken("client A", "Fake thread", new BigInteger("1"), "held-lock-1", "logger-lock");
    HeldLocksToken token2 = LockServiceTestUtils.getFakeHeldLocksToken("client B", "Fake thread 2", new BigInteger("2"), "held-lock-2", "held-lock-3");
    heldLocksTokenMap.putIfAbsent(token, LockServiceImpl.HeldLocks.of(token, LockCollections.of()));
    heldLocksTokenMap.putIfAbsent(token2, LockServiceImpl.HeldLocks.of(token2, LockCollections.of()));
    LockServiceStateLogger logger = new LockServiceStateLogger(heldLocksTokenMap, outstandingLockRequestMultimap, LockServiceTestUtils.TEST_LOG_STATE_DIR);
    logger.logLocks();
}
Also used : LockDescriptor(com.palantir.lock.LockDescriptor) StringLockDescriptor(com.palantir.lock.StringLockDescriptor) HeldLocksToken(com.palantir.lock.HeldLocksToken) LockClient(com.palantir.lock.LockClient) BigInteger(java.math.BigInteger) LockRequest(com.palantir.lock.LockRequest) BeforeClass(org.junit.BeforeClass)

Aggregations

LockRequest (com.palantir.lock.LockRequest)10 Test (org.junit.Test)5 LockDescriptor (com.palantir.lock.LockDescriptor)4 LockRefreshToken (com.palantir.lock.LockRefreshToken)4 StringLockDescriptor (com.palantir.lock.StringLockDescriptor)3 HeldLocksToken (com.palantir.lock.HeldLocksToken)2 LockClient (com.palantir.lock.LockClient)2 LockService (com.palantir.lock.LockService)2 MetricRegistry (com.codahale.metrics.MetricRegistry)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 GlobalClock (com.palantir.atlasdb.cleaner.GlobalClock)1 SingleBackendCliTestRunner (com.palantir.atlasdb.cli.runner.SingleBackendCliTestRunner)1 AtlasDbConfig (com.palantir.atlasdb.config.AtlasDbConfig)1 ImmutableAtlasDbConfig (com.palantir.atlasdb.config.ImmutableAtlasDbConfig)1 Cell (com.palantir.atlasdb.keyvalue.api.Cell)1 InMemoryAtlasDbConfig (com.palantir.atlasdb.memory.InMemoryAtlasDbConfig)1 DaggerTestAtlasDbServices (com.palantir.atlasdb.services.test.DaggerTestAtlasDbServices)1 TestAtlasDbServices (com.palantir.atlasdb.services.test.TestAtlasDbServices)1 Clock (com.palantir.common.time.Clock)1 AtlasRowLockDescriptor (com.palantir.lock.AtlasRowLockDescriptor)1