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();
}
}
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();
}
Aggregations