Search in sources :

Example 11 with LockService

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

the class AsyncTimeLockServicesCreator method createTimeLockServices.

@Override
public TimeLockServices createTimeLockServices(String client, Supplier<ManagedTimestampService> rawTimestampServiceSupplier, Supplier<LockService> rawLockServiceSupplier) {
    log.info("Creating async timelock services for client {}", SafeArg.of("client", client));
    AsyncOrLegacyTimelockService asyncOrLegacyTimelockService;
    AsyncTimelockService asyncTimelockService = instrumentInLeadershipProxy(AsyncTimelockService.class, () -> AsyncTimeLockServicesCreator.createRawAsyncTimelockService(client, rawTimestampServiceSupplier), client);
    asyncOrLegacyTimelockService = AsyncOrLegacyTimelockService.createFromAsyncTimelock(new AsyncTimelockResource(asyncTimelockService));
    LockService lockService = instrumentInLeadershipProxy(LockService.class, asyncLockConfiguration.disableLegacySafetyChecksWarningPotentialDataCorruption() ? rawLockServiceSupplier : JavaSuppliers.compose(NonTransactionalLockService::new, rawLockServiceSupplier), client);
    return TimeLockServices.create(asyncTimelockService, lockService, asyncOrLegacyTimelockService, asyncTimelockService);
}
Also used : AsyncTimelockResource(com.palantir.atlasdb.timelock.AsyncTimelockResource) NonTransactionalLockService(com.palantir.atlasdb.timelock.lock.NonTransactionalLockService) NonTransactionalLockService(com.palantir.atlasdb.timelock.lock.NonTransactionalLockService) LockService(com.palantir.lock.LockService) AsyncLockService(com.palantir.atlasdb.timelock.lock.AsyncLockService) AsyncOrLegacyTimelockService(com.palantir.atlasdb.timelock.util.AsyncOrLegacyTimelockService) AsyncTimelockService(com.palantir.atlasdb.timelock.AsyncTimelockService)

Example 12 with LockService

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

the class TransactionManagers method getLockAndTimestampServices.

private static LockAndTimestampServices getLockAndTimestampServices(Supplier<ServerListConfig> timelockServerListConfig, String userAgent) {
    LockService lockService = new ServiceCreator<>(LockService.class, userAgent).applyDynamic(timelockServerListConfig);
    TimelockService timelockService = new ServiceCreator<>(TimelockService.class, userAgent).applyDynamic(timelockServerListConfig);
    return ImmutableLockAndTimestampServices.builder().lock(lockService).timestamp(new TimelockTimestampServiceAdapter(timelockService)).timelock(timelockService).build();
}
Also used : TimelockTimestampServiceAdapter(com.palantir.atlasdb.transaction.impl.TimelockTimestampServiceAdapter) NoOpPersistentLockService(com.palantir.atlasdb.persistentlock.NoOpPersistentLockService) KvsBackedPersistentLockService(com.palantir.atlasdb.persistentlock.KvsBackedPersistentLockService) LockService(com.palantir.lock.LockService) LockRefreshingLockService(com.palantir.lock.client.LockRefreshingLockService) PersistentLockService(com.palantir.atlasdb.persistentlock.PersistentLockService) LegacyTimelockService(com.palantir.lock.impl.LegacyTimelockService) TimelockService(com.palantir.lock.v2.TimelockService) InstrumentedTimelockService(com.palantir.atlasdb.transaction.impl.InstrumentedTimelockService)

Example 13 with LockService

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

the class TransactionManagers method createRawLeaderServices.

private static LockAndTimestampServices createRawLeaderServices(LeaderConfig leaderConfig, Consumer<Object> env, com.google.common.base.Supplier<LockService> lock, com.google.common.base.Supplier<TimestampService> time, String userAgent) {
    // Create local services, that may or may not end up being registered in an Consumer<Object>.
    LeaderRuntimeConfig defaultRuntime = ImmutableLeaderRuntimeConfig.builder().build();
    LocalPaxosServices localPaxosServices = Leaders.createAndRegisterLocalServices(env, leaderConfig, () -> defaultRuntime, userAgent);
    LeaderElectionService leader = localPaxosServices.leaderElectionService();
    LockService localLock = ServiceCreator.createInstrumentedService(AwaitingLeadershipProxy.newProxyInstance(LockService.class, lock, leader), LockService.class);
    TimestampService localTime = ServiceCreator.createInstrumentedService(AwaitingLeadershipProxy.newProxyInstance(TimestampService.class, time, leader), TimestampService.class);
    env.accept(localLock);
    env.accept(localTime);
    // Create remote services, that may end up calling our own local services.
    ImmutableServerListConfig serverListConfig = ImmutableServerListConfig.builder().servers(leaderConfig.leaders()).sslConfiguration(leaderConfig.sslConfiguration()).build();
    LockService remoteLock = new ServiceCreator<>(LockService.class, userAgent).apply(serverListConfig);
    TimestampService remoteTime = new ServiceCreator<>(TimestampService.class, userAgent).apply(serverListConfig);
    if (leaderConfig.leaders().size() == 1) {
        // Attempting to connect to ourself while processing a request can lead to deadlock if incoming request
        // volume is high, as all Jetty threads end up waiting for the timestamp server, and no threads remain to
        // actually handle the timestamp server requests. If we are the only single leader, we can avoid the
        // deadlock entirely; so use PingableLeader's getUUID() to detect this situation and eliminate the redundant
        // call.
        PingableLeader localPingableLeader = localPaxosServices.pingableLeader();
        String localServerId = localPingableLeader.getUUID();
        PingableLeader remotePingableLeader = AtlasDbFeignTargetFactory.createRsProxy(ServiceCreator.createSslSocketFactory(leaderConfig.sslConfiguration()), Iterables.getOnlyElement(leaderConfig.leaders()), PingableLeader.class, userAgent);
        // Determine asynchronously whether the remote services are talking to our local services.
        CompletableFuture<Boolean> useLocalServicesFuture = new CompletableFuture<>();
        runAsync.accept(() -> {
            int logAfter = LOGGING_INTERVAL;
            while (true) {
                try {
                    String remoteServerId = remotePingableLeader.getUUID();
                    useLocalServicesFuture.complete(localServerId.equals(remoteServerId));
                    return;
                } catch (ClientErrorException e) {
                    useLocalServicesFuture.complete(false);
                    return;
                } catch (Throwable e) {
                    if (--logAfter == 0) {
                        log.warn("Failed to read remote timestamp server ID", e);
                        logAfter = LOGGING_INTERVAL;
                    }
                }
                Uninterruptibles.sleepUninterruptibly(1, TimeUnit.SECONDS);
            }
        });
        // Create dynamic service proxies, that switch to talking directly to our local services if it turns out our
        // remote services are pointed at them anyway.
        LockService dynamicLockService = LocalOrRemoteProxy.newProxyInstance(LockService.class, localLock, remoteLock, useLocalServicesFuture);
        TimestampService dynamicTimeService = LocalOrRemoteProxy.newProxyInstance(TimestampService.class, localTime, remoteTime, useLocalServicesFuture);
        return ImmutableLockAndTimestampServices.builder().lock(dynamicLockService).timestamp(dynamicTimeService).timelock(new LegacyTimelockService(dynamicTimeService, dynamicLockService, LOCK_CLIENT)).build();
    } else {
        return ImmutableLockAndTimestampServices.builder().lock(remoteLock).timestamp(remoteTime).timelock(new LegacyTimelockService(remoteTime, remoteLock, LOCK_CLIENT)).build();
    }
}
Also used : LegacyTimelockService(com.palantir.lock.impl.LegacyTimelockService) ImmutableServerListConfig(com.palantir.atlasdb.config.ImmutableServerListConfig) NoOpPersistentLockService(com.palantir.atlasdb.persistentlock.NoOpPersistentLockService) KvsBackedPersistentLockService(com.palantir.atlasdb.persistentlock.KvsBackedPersistentLockService) LockService(com.palantir.lock.LockService) LockRefreshingLockService(com.palantir.lock.client.LockRefreshingLockService) PersistentLockService(com.palantir.atlasdb.persistentlock.PersistentLockService) LocalPaxosServices(com.palantir.atlasdb.factory.Leaders.LocalPaxosServices) CompletableFuture(java.util.concurrent.CompletableFuture) PingableLeader(com.palantir.leader.PingableLeader) LeaderElectionService(com.palantir.leader.LeaderElectionService) ClientErrorException(javax.ws.rs.ClientErrorException) LeaderRuntimeConfig(com.palantir.atlasdb.config.LeaderRuntimeConfig) ImmutableLeaderRuntimeConfig(com.palantir.atlasdb.config.ImmutableLeaderRuntimeConfig) TimestampService(com.palantir.timestamp.TimestampService)

Example 14 with LockService

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

the class InMemoryAtlasDbFactory method createInMemoryTransactionManagerInternal.

private static SerializableTransactionManager createInMemoryTransactionManagerInternal(Set<Schema> schemas) {
    TimestampService ts = new InMemoryTimestampService();
    KeyValueService keyValueService = new InMemoryKeyValueService(false);
    schemas.forEach(s -> Schemas.createTablesAndIndexes(s, keyValueService));
    TransactionTables.createTables(keyValueService);
    TransactionService transactionService = TransactionServices.createTransactionService(keyValueService);
    LockService lock = LockRefreshingLockService.create(LockServiceImpl.create(LockServerOptions.builder().isStandaloneServer(false).build()));
    LockClient client = LockClient.of("in memory atlasdb instance");
    ConflictDetectionManager conflictManager = ConflictDetectionManagers.createWithoutWarmingCache(keyValueService);
    SweepStrategyManager sweepStrategyManager = SweepStrategyManagers.createDefault(keyValueService);
    CleanupFollower follower = CleanupFollower.create(schemas);
    Cleaner cleaner = new DefaultCleanerBuilder(keyValueService, lock, ts, client, ImmutableList.of(follower), transactionService).buildCleaner();
    SerializableTransactionManager ret = SerializableTransactionManager.createForTest(keyValueService, ts, client, lock, transactionService, Suppliers.ofInstance(AtlasDbConstraintCheckingMode.FULL_CONSTRAINT_CHECKING_THROWS_EXCEPTIONS), conflictManager, sweepStrategyManager, cleaner, DEFAULT_MAX_CONCURRENT_RANGES, DEFAULT_GET_RANGES_CONCURRENCY, () -> DEFAULT_TIMESTAMP_CACHE_SIZE, MultiTableSweepQueueWriter.NO_OP);
    cleaner.start(ret);
    return ret;
}
Also used : SweepStrategyManager(com.palantir.atlasdb.transaction.impl.SweepStrategyManager) InMemoryKeyValueService(com.palantir.atlasdb.keyvalue.impl.InMemoryKeyValueService) KeyValueService(com.palantir.atlasdb.keyvalue.api.KeyValueService) TransactionService(com.palantir.atlasdb.transaction.service.TransactionService) LockRefreshingLockService(com.palantir.lock.client.LockRefreshingLockService) LockService(com.palantir.lock.LockService) InMemoryKeyValueService(com.palantir.atlasdb.keyvalue.impl.InMemoryKeyValueService) SerializableTransactionManager(com.palantir.atlasdb.transaction.impl.SerializableTransactionManager) LockClient(com.palantir.lock.LockClient) DefaultCleanerBuilder(com.palantir.atlasdb.cleaner.DefaultCleanerBuilder) ConflictDetectionManager(com.palantir.atlasdb.transaction.impl.ConflictDetectionManager) InMemoryTimestampService(com.palantir.timestamp.InMemoryTimestampService) InMemoryTimestampService(com.palantir.timestamp.InMemoryTimestampService) TimestampService(com.palantir.timestamp.TimestampService) CleanupFollower(com.palantir.atlasdb.cleaner.CleanupFollower) Cleaner(com.palantir.atlasdb.cleaner.Cleaner)

Example 15 with LockService

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

the class SnapshotTransactionTest method testLockAfterGet.

// If lock happens concurrent with get, we aren't sure that we can rollback the transaction
@Test
public void testLockAfterGet() throws Exception {
    byte[] rowName = PtBytes.toBytes("1");
    Mockery m = new Mockery();
    final KeyValueService kvMock = m.mock(KeyValueService.class);
    final LockService lockMock = m.mock(LockService.class);
    LockService lock = MultiDelegateProxy.newProxyInstance(LockService.class, lockService, lockMock);
    final Cell cell = Cell.create(rowName, rowName);
    timestampService.getFreshTimestamp();
    final long startTs = timestampService.getFreshTimestamp();
    final long transactionTs = timestampService.getFreshTimestamp();
    keyValueService.put(TABLE, ImmutableMap.of(cell, PtBytes.EMPTY_BYTE_ARRAY), startTs);
    m.checking(new Expectations() {

        {
            oneOf(kvMock).get(TABLE, ImmutableMap.of(cell, transactionTs));
            will(throwException(new RuntimeException()));
            never(lockMock).lockWithFullLockResponse(with(LockClient.ANONYMOUS), with(any(LockRequest.class)));
        }
    });
    SnapshotTransaction snapshot = new SnapshotTransaction(kvMock, new LegacyTimelockService(timestampService, lock, lockClient), transactionService, NoOpCleaner.INSTANCE, transactionTs, TestConflictDetectionManagers.createWithStaticConflictDetection(ImmutableMap.of(TABLE, ConflictHandler.RETRY_ON_WRITE_WRITE)), AtlasDbConstraintCheckingMode.NO_CONSTRAINT_CHECKING, TransactionReadSentinelBehavior.THROW_EXCEPTION, timestampCache, getRangesExecutor, defaultGetRangesConcurrency, sweepQueue);
    try {
        snapshot.get(TABLE, ImmutableSet.of(cell));
        fail();
    } catch (RuntimeException e) {
    // expected
    }
    m.assertIsSatisfied();
}
Also used : Expectations(org.jmock.Expectations) LegacyTimelockService(com.palantir.lock.impl.LegacyTimelockService) TrackingKeyValueService(com.palantir.atlasdb.keyvalue.impl.TrackingKeyValueService) ForwardingKeyValueService(com.palantir.atlasdb.keyvalue.impl.ForwardingKeyValueService) KeyValueService(com.palantir.atlasdb.keyvalue.api.KeyValueService) LockService(com.palantir.lock.LockService) Mockery(org.jmock.Mockery) Cell(com.palantir.atlasdb.keyvalue.api.Cell) LockRequest(com.palantir.lock.LockRequest) Test(org.junit.Test)

Aggregations

LockService (com.palantir.lock.LockService)22 Test (org.junit.Test)11 LockRefreshToken (com.palantir.lock.LockRefreshToken)7 LegacyTimelockService (com.palantir.lock.impl.LegacyTimelockService)7 TimestampService (com.palantir.timestamp.TimestampService)6 KvsBackedPersistentLockService (com.palantir.atlasdb.persistentlock.KvsBackedPersistentLockService)5 NoOpPersistentLockService (com.palantir.atlasdb.persistentlock.NoOpPersistentLockService)5 PersistentLockService (com.palantir.atlasdb.persistentlock.PersistentLockService)5 LockRefreshingLockService (com.palantir.lock.client.LockRefreshingLockService)5 TimelockService (com.palantir.lock.v2.TimelockService)5 LockClient (com.palantir.lock.LockClient)4 LockRequest (com.palantir.lock.LockRequest)4 KeyValueService (com.palantir.atlasdb.keyvalue.api.KeyValueService)3 TransactionManager (com.palantir.atlasdb.transaction.api.TransactionManager)3 Cleaner (com.palantir.atlasdb.cleaner.Cleaner)2 NoOpCleaner (com.palantir.atlasdb.cleaner.NoOpCleaner)2 Cell (com.palantir.atlasdb.keyvalue.api.Cell)2 ForwardingKeyValueService (com.palantir.atlasdb.keyvalue.impl.ForwardingKeyValueService)2 TrackingKeyValueService (com.palantir.atlasdb.keyvalue.impl.TrackingKeyValueService)2 ManagedTimestampService (com.palantir.atlasdb.timelock.paxos.ManagedTimestampService)2