use of com.palantir.timestamp.ManagedTimestampService 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.timestamp.ManagedTimestampService in project atlasdb by palantir.
the class DefaultLockAndTimestampServiceFactory method createRawEmbeddedServices.
private static LockAndTimestampServices createRawEmbeddedServices(MetricsManager metricsManager, Consumer<Object> env, Supplier<LockService> lock, Supplier<ManagedTimestampService> managedTimestampServiceSupplier) {
LockService lockService = ServiceCreator.instrumentService(metricsManager.getRegistry(), lock.get(), LockService.class);
ManagedTimestampService managedTimestampService = managedTimestampServiceSupplier.get();
TimestampService timeService = ServiceCreator.instrumentService(metricsManager.getRegistry(), managedTimestampService, TimestampService.class);
TimestampManagementService timestampManagementService = ServiceCreator.instrumentService(metricsManager.getRegistry(), managedTimestampService, TimestampManagementService.class);
env.accept(lockService);
env.accept(timeService);
env.accept(timestampManagementService);
return ImmutableLockAndTimestampServices.builder().lock(lockService).timestamp(timeService).timestampManagement(timestampManagementService).timelock(new LegacyTimelockService(timeService, lockService, TransactionManagers.LOCK_CLIENT)).build();
}
use of com.palantir.timestamp.ManagedTimestampService in project atlasdb by palantir.
the class LockAndTimestampModule method provideLockAndTimestampServices.
@Provides
@Singleton
public LockAndTimestampServices provideLockAndTimestampServices(MetricsManager metricsManager, ServicesConfig config) {
ServiceDiscoveringAtlasSupplier atlasSupplier = config.atlasDbSupplier(metricsManager);
Supplier<ManagedTimestampService> managedTimestampService = atlasSupplier::getManagedTimestampService;
return TransactionManagers.createLockAndTimestampServicesForCli(metricsManager, config.atlasDbConfig(), Refreshable.only(config.atlasDbRuntimeConfig()), resource -> {
}, LockServiceImpl::create, managedTimestampService, config.atlasDbSupplier(metricsManager).getTimestampStoreInvalidator(), "cli");
}
use of com.palantir.timestamp.ManagedTimestampService in project atlasdb by palantir.
the class ServiceDiscoveringAtlasSupplierTest method delegateToFactoriesAnnotatedWithAutoService.
@Test
public void delegateToFactoriesAnnotatedWithAutoService() {
ServiceDiscoveringAtlasSupplier atlasSupplier = createAtlasSupplier(kvsConfig);
assertThat(atlasSupplier.getKeyValueService()).as("delegates createRawKeyValueService").isEqualTo(delegate.createRawKeyValueService(metrics, kvsConfig, Refreshable.only(Optional.empty()), leaderConfig, Optional.empty(), AtlasDbFactory.THROWING_FRESH_TIMESTAMP_SOURCE, AtlasDbFactory.DEFAULT_INITIALIZE_ASYNC));
ManagedTimestampService timestampService = mock(ManagedTimestampService.class);
AutoServiceAnnotatedAtlasDbFactory.nextTimestampServiceToReturn(timestampService);
assertThat(atlasSupplier.getManagedTimestampService()).as("delegates getManagedTimestampService").isEqualTo(timestampService);
}
use of com.palantir.timestamp.ManagedTimestampService in project atlasdb by palantir.
the class KeyValueServiceMigratorsTest method createMock.
private static AtlasDbServices createMock(KeyValueService kvs, InMemoryTimeLockRule timeLock) {
ManagedTimestampService timestampService = timeLock.getManagedTimestampService();
TransactionTables.createTables(kvs);
TransactionService transactionService = spy(TransactionServices.createRaw(kvs, timestampService, false));
AtlasDbServices mockServices = mock(AtlasDbServices.class);
when(mockServices.getManagedTimestampService()).thenReturn(timestampService);
when(mockServices.getTransactionService()).thenReturn(transactionService);
when(mockServices.getKeyValueService()).thenReturn(kvs);
TargetedSweeper sweeper = TargetedSweeper.createUninitializedForTest(() -> 1);
SerializableTransactionManager txManager = SerializableTransactionManager.createForTest(MetricsManagers.createForTests(), kvs, timeLock.getLegacyTimelockService(), timestampService, timeLock.getLockService(), timeLock.getLockWatchManager(), transactionService, () -> AtlasDbConstraintCheckingMode.NO_CONSTRAINT_CHECKING, ConflictDetectionManagers.createWithoutWarmingCache(kvs), SweepStrategyManagers.createDefault(kvs), new NoOpCleaner(), 16, 4, sweeper);
sweeper.initialize(txManager);
when(mockServices.getTransactionManager()).thenReturn(txManager);
return mockServices;
}
Aggregations