Search in sources :

Example 1 with RemoteLockServiceAdapter

use of com.palantir.lock.client.RemoteLockServiceAdapter in project atlasdb by palantir.

the class DefaultLockAndTimestampServiceFactory method createRawLeaderServices.

private static LockAndTimestampServices createRawLeaderServices(MetricsManager metricsManager, LeaderConfig leaderConfig, Consumer<Object> env, Supplier<LockService> lock, Supplier<ManagedTimestampService> time, UserAgent userAgent) {
    // Create local services, that may or may not end up being registered in an Consumer<Object>.
    LocalPaxosServices localPaxosServices = Leaders.createAndRegisterLocalServices(metricsManager, env, leaderConfig, userAgent);
    LeadershipCoordinator leadershipCoordinator = localPaxosServices.leadershipCoordinator();
    LockService localLock = AwaitingLeadershipProxy.newProxyInstance(LockService.class, lock, leadershipCoordinator);
    ManagedTimestampService managedTimestampProxy = AwaitingLeadershipProxy.newProxyInstance(ManagedTimestampService.class, time, leadershipCoordinator);
    // These facades are necessary because of the semantics of the JAX-RS algorithm (in particular, accepting
    // just the managed timestamp service will *not* work).
    TimestampService localTime = getTimestampFacade(managedTimestampProxy);
    TimestampManagementService localManagement = getTimestampManagementFacade(managedTimestampProxy);
    env.accept(localLock);
    env.accept(localTime);
    env.accept(localManagement);
    // Create remote services, that may end up calling our own local services.
    ImmutableServerListConfig serverListConfig = ImmutableServerListConfig.builder().servers(leaderConfig.leaders()).sslConfiguration(leaderConfig.sslConfiguration()).build();
    ServiceCreator creator = ServiceCreator.noPayloadLimiter(metricsManager, Refreshable.only(serverListConfig), userAgent, () -> RemotingClientConfigs.DEFAULT);
    LockService remoteLock = new RemoteLockServiceAdapter(creator.createService(NamespaceAgnosticLockRpcClient.class));
    TimestampService remoteTime = creator.createService(TimestampService.class);
    TimestampManagementService remoteManagement = creator.createService(TimestampManagementService.class);
    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.localPingableLeader();
        String localServerId = localPingableLeader.getUUID();
        PingableLeader remotePingableLeader = AtlasDbHttpClients.createProxy(ServiceCreator.createTrustContext(leaderConfig.sslConfiguration()), Iterables.getOnlyElement(leaderConfig.leaders()), PingableLeader.class, AuxiliaryRemotingParameters.builder().userAgent(userAgent).shouldRetry(true).shouldLimitPayload(true).shouldUseExtendedTimeout(false).build());
        // Determine asynchronously whether the remote services are talking to our local services.
        CompletableFuture<Boolean> useLocalServicesFuture = new CompletableFuture<>();
        TransactionManagers.runAsync.accept(() -> {
            int attemptsLeftBeforeLog = ATTEMPTS_BEFORE_LOGGING_FAILURE_TO_READ_REMOTE_TIMESTAMP_SERVER_ID;
            while (true) {
                try {
                    String remoteServerId = remotePingableLeader.getUUID();
                    useLocalServicesFuture.complete(localServerId.equals(remoteServerId));
                    return;
                } catch (ClientErrorException e) {
                    useLocalServicesFuture.complete(false);
                    return;
                } catch (UnknownRemoteException e) {
                    // manifest as ClientErrorExceptions.
                    if (400 <= e.getStatus() && e.getStatus() <= 499) {
                        useLocalServicesFuture.complete(false);
                        return;
                    }
                    attemptsLeftBeforeLog = logFailureToReadRemoteTimestampServerId(attemptsLeftBeforeLog, e);
                } catch (Throwable e) {
                    attemptsLeftBeforeLog = logFailureToReadRemoteTimestampServerId(attemptsLeftBeforeLog, e);
                }
                Uninterruptibles.sleepUninterruptibly(Duration.ofSeconds(1));
            }
        });
        // 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);
        // Use managedTimestampProxy here to avoid local calls going through indirection.
        TimestampService dynamicTimeService = LocalOrRemoteProxy.newProxyInstance(TimestampService.class, managedTimestampProxy, remoteTime, useLocalServicesFuture);
        TimestampManagementService dynamicManagementService = LocalOrRemoteProxy.newProxyInstance(TimestampManagementService.class, managedTimestampProxy, remoteManagement, useLocalServicesFuture);
        return ImmutableLockAndTimestampServices.builder().lock(dynamicLockService).timestamp(dynamicTimeService).timestampManagement(dynamicManagementService).timelock(new LegacyTimelockService(dynamicTimeService, dynamicLockService, TransactionManagers.LOCK_CLIENT)).build();
    } else {
        return ImmutableLockAndTimestampServices.builder().lock(remoteLock).timestamp(remoteTime).timestampManagement(remoteManagement).timelock(new LegacyTimelockService(remoteTime, remoteLock, TransactionManagers.LOCK_CLIENT)).build();
    }
}
Also used : LegacyTimelockService(com.palantir.lock.impl.LegacyTimelockService) ImmutableServerListConfig(com.palantir.atlasdb.config.ImmutableServerListConfig) RemoteLockServiceAdapter(com.palantir.lock.client.RemoteLockServiceAdapter) LockRefreshingLockService(com.palantir.lock.client.LockRefreshingLockService) LockService(com.palantir.lock.LockService) ManagedTimestampService(com.palantir.timestamp.ManagedTimestampService) UnknownRemoteException(com.palantir.conjure.java.api.errors.UnknownRemoteException) LeadershipCoordinator(com.palantir.leader.proxy.LeadershipCoordinator) LocalPaxosServices(com.palantir.atlasdb.factory.Leaders.LocalPaxosServices) TimestampManagementService(com.palantir.timestamp.TimestampManagementService) CompletableFuture(java.util.concurrent.CompletableFuture) NamespaceAgnosticLockRpcClient(com.palantir.lock.NamespaceAgnosticLockRpcClient) PingableLeader(com.palantir.leader.PingableLeader) ClientErrorException(javax.ws.rs.ClientErrorException) TimestampService(com.palantir.timestamp.TimestampService) ManagedTimestampService(com.palantir.timestamp.ManagedTimestampService)

Example 2 with RemoteLockServiceAdapter

use of com.palantir.lock.client.RemoteLockServiceAdapter in project atlasdb by palantir.

the class DefaultLockAndTimestampServiceFactory method createRawRemoteServices.

private static LockAndTimestampServices createRawRemoteServices(MetricsManager metricsManager, AtlasDbConfig config, Supplier<AtlasDbRuntimeConfig> runtimeConfigSupplier, UserAgent userAgent) {
    ServiceCreator creator = ServiceCreator.noPayloadLimiter(metricsManager, Refreshable.only(config.lock().orElseThrow(() -> new SafeIllegalArgumentException("lock server list config absent"))), userAgent, () -> runtimeConfigSupplier.get().remotingClient());
    LockService lockService = new RemoteLockServiceAdapter(creator.createService(NamespaceAgnosticLockRpcClient.class));
    TimestampService timeService = creator.createService(TimestampService.class);
    TimestampManagementService timestampManagementService = creator.createService(TimestampManagementService.class);
    return ImmutableLockAndTimestampServices.builder().lock(lockService).timestamp(timeService).timestampManagement(timestampManagementService).timelock(new LegacyTimelockService(timeService, lockService, TransactionManagers.LOCK_CLIENT)).build();
}
Also used : LegacyTimelockService(com.palantir.lock.impl.LegacyTimelockService) TimestampManagementService(com.palantir.timestamp.TimestampManagementService) RemoteLockServiceAdapter(com.palantir.lock.client.RemoteLockServiceAdapter) LockRefreshingLockService(com.palantir.lock.client.LockRefreshingLockService) LockService(com.palantir.lock.LockService) NamespaceAgnosticLockRpcClient(com.palantir.lock.NamespaceAgnosticLockRpcClient) SafeIllegalArgumentException(com.palantir.logsafe.exceptions.SafeIllegalArgumentException) TimestampService(com.palantir.timestamp.TimestampService) ManagedTimestampService(com.palantir.timestamp.ManagedTimestampService)

Aggregations

LockService (com.palantir.lock.LockService)2 NamespaceAgnosticLockRpcClient (com.palantir.lock.NamespaceAgnosticLockRpcClient)2 LockRefreshingLockService (com.palantir.lock.client.LockRefreshingLockService)2 RemoteLockServiceAdapter (com.palantir.lock.client.RemoteLockServiceAdapter)2 LegacyTimelockService (com.palantir.lock.impl.LegacyTimelockService)2 ManagedTimestampService (com.palantir.timestamp.ManagedTimestampService)2 TimestampManagementService (com.palantir.timestamp.TimestampManagementService)2 TimestampService (com.palantir.timestamp.TimestampService)2 ImmutableServerListConfig (com.palantir.atlasdb.config.ImmutableServerListConfig)1 LocalPaxosServices (com.palantir.atlasdb.factory.Leaders.LocalPaxosServices)1 UnknownRemoteException (com.palantir.conjure.java.api.errors.UnknownRemoteException)1 PingableLeader (com.palantir.leader.PingableLeader)1 LeadershipCoordinator (com.palantir.leader.proxy.LeadershipCoordinator)1 SafeIllegalArgumentException (com.palantir.logsafe.exceptions.SafeIllegalArgumentException)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 ClientErrorException (javax.ws.rs.ClientErrorException)1