Skip to content
This repository has been archived by the owner on Nov 14, 2024. It is now read-only.

Commit

Permalink
move to builder
Browse files Browse the repository at this point in the history
  • Loading branch information
tpetracca committed Oct 10, 2017
1 parent 458705d commit b899cca
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 107 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package com.palantir.atlasdb.factory;

import java.util.Optional;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
Expand All @@ -27,6 +28,7 @@
import org.slf4j.LoggerFactory;

import com.codahale.metrics.MetricRegistry;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Preconditions;
import com.google.common.base.Supplier;
Expand Down Expand Up @@ -92,6 +94,7 @@
import com.palantir.leader.proxy.AwaitingLeadershipProxy;
import com.palantir.lock.LockClient;
import com.palantir.lock.LockRequest;
import com.palantir.lock.LockServerOptions;
import com.palantir.lock.LockService;
import com.palantir.lock.SimpleTimeDuration;
import com.palantir.lock.client.LockRefreshingLockService;
Expand All @@ -103,7 +106,55 @@
import com.palantir.timestamp.TimestampStoreInvalidator;
import com.palantir.util.OptionalResolver;

public final class TransactionManagers {
@Value.Immutable
public abstract class TransactionManagers {
abstract AtlasDbConfig config();

@Value.Default
Supplier<Optional<AtlasDbRuntimeConfig>> runtimeConfigSupplier() {
return Optional::empty;
}

abstract Set<Schema> schemas();

@Value.Default
Consumer<Object> env() {
return resource -> { };
}

@Value.Default
LockServerOptions lockServerOptions() {
return LockServerOptions.DEFAULT;
}

@Value.Default
boolean allowHiddenTableAccess() {
return false;
}

abstract Optional<Class<?>> callingClass();

abstract Optional<String> userAgent();

// directly specified -> inferred from caller -> default
@Value.Derived
String derivedUserAgent() {
return userAgent().orElse(callingClass().map(UserAgents::fromClass).orElse(UserAgents.DEFAULT_USER_AGENT));
}

public static Builder builder() {
return new Builder();
}

public static class Builder extends ImmutableTransactionManagers.Builder {
public SerializableTransactionManager buildSerializable() {
return build().serializable();
}
}

// ==========================================================================================
// ==========================================================================================
// ==========================================================================================

private static final int LOGGING_INTERVAL = 60;
private static final Logger log = LoggerFactory.getLogger(TransactionManagers.class);
Expand All @@ -116,10 +167,6 @@ public final class TransactionManagers {
thread.start();
};

private TransactionManagers() {
// Utility class
}

/**
* Accepts a single {@link Schema}.
* @see TransactionManagers#createInMemory(Set)
Expand All @@ -133,21 +180,20 @@ public static SerializableTransactionManager createInMemory(Schema schema) {
* {@link com.palantir.atlasdb.keyvalue.impl.InMemoryKeyValueService}. This should be used for testing
* purposes only.
*/
public static SerializableTransactionManager createInMemory(Set<Schema> schemas) {
static SerializableTransactionManager createInMemory(Set<Schema> schemas) {
AtlasDbConfig config = ImmutableAtlasDbConfig.builder().keyValueService(new InMemoryAtlasDbConfig()).build();
return create(TransactionManagerOptions.builder()
.config(config)
.schemas(schemas)
.build());
return builder().config(config).schemas(schemas).buildSerializable();
}

public static SerializableTransactionManager create(TransactionManagerOptions options) {
final AtlasDbConfig config = options.config();
@JsonIgnore
@Value.Derived
SerializableTransactionManager serializable() {
final AtlasDbConfig config = config();
checkInstallConfig(config);

AtlasDbRuntimeConfig defaultRuntime = AtlasDbRuntimeConfig.defaultRuntimeConfig();
java.util.function.Supplier<AtlasDbRuntimeConfig> runtimeConfigSupplier =
() -> options.runtimeConfigSupplier().get().orElse(defaultRuntime);
() -> runtimeConfigSupplier().get().orElse(defaultRuntime);

ServiceDiscoveringAtlasSupplier atlasFactory =
new ServiceDiscoveringAtlasSupplier(config.keyValueService(), config.leader(), config.namespace(),
Expand All @@ -159,27 +205,27 @@ public static SerializableTransactionManager create(TransactionManagerOptions op
LockAndTimestampServices lockAndTimestampServices = createLockAndTimestampServices(
config,
() -> runtimeConfigSupplier.get().timestampClient(),
options.env(),
() -> LockServiceImpl.create(options.lockServerOptions()),
env(),
() -> LockServiceImpl.create(lockServerOptions()),
atlasFactory::getTimestampService,
atlasFactory.getTimestampStoreInvalidator(),
options.derivedUserAgent());
derivedUserAgent());
KeyValueService kvs = ProfilingKeyValueService.create(rawKvs, config.getKvsSlowLogThresholdMillis());
kvs = SweepStatsKeyValueService.create(kvs,
new TimelockTimestampServiceAdapter(lockAndTimestampServices.timelock()));
kvs = TracingKeyValueService.create(kvs);
kvs = AtlasDbMetrics.instrument(KeyValueService.class, kvs, MetricRegistry.name(KeyValueService.class));
kvs = ValidatingQueryRewritingKeyValueService.create(kvs);

TransactionManagersInitializer.createInitialTables(kvs, options.schemas(), config.initializeAsync());
PersistentLockService persistentLockService = createAndRegisterPersistentLockService(kvs, options.env(),
TransactionManagersInitializer.createInitialTables(kvs, schemas(), config.initializeAsync());
PersistentLockService persistentLockService = createAndRegisterPersistentLockService(kvs, env(),
config.initializeAsync());

TransactionService transactionService = TransactionServices.createTransactionService(kvs);
ConflictDetectionManager conflictManager = ConflictDetectionManagers.create(kvs);
SweepStrategyManager sweepStrategyManager = SweepStrategyManagers.createDefault(kvs);

CleanupFollower follower = CleanupFollower.create(options.schemas());
CleanupFollower follower = CleanupFollower.create(schemas());

Cleaner cleaner = new DefaultCleanerBuilder(
kvs,
Expand All @@ -204,7 +250,7 @@ public static SerializableTransactionManager create(TransactionManagerOptions op
conflictManager,
sweepStrategyManager,
cleaner,
options.allowHiddenTableAccess(),
allowHiddenTableAccess(),
() -> runtimeConfigSupplier.get().transaction().getLockAcquireTimeoutMillis(),
config.keyValueService().concurrentGetRangesThreadPoolSize(),
config.initializeAsync());
Expand All @@ -213,7 +259,7 @@ public static SerializableTransactionManager create(TransactionManagerOptions op
persistentLockService,
config.getSweepPersistentLockWaitMillis());
initializeSweepEndpointAndBackgroundProcess(runtimeConfigSupplier,
options.env(),
env(),
kvs,
transactionService,
sweepStrategyManager,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -259,11 +259,10 @@ public void setsGlobalDefaultLockTimeout() {
.keyValueService(new InMemoryAtlasDbConfig())
.defaultLockTimeoutSeconds((int) expectedTimeout.getTime())
.build();
TransactionManagerOptions options = TransactionManagerOptions.builder()
TransactionManagers.builder()
.config(realConfig)
.env(environment)
.build();
TransactionManagers.create(options);
.buildSerializable();

assertEquals(expectedTimeout, LockRequest.getDefaultLockTimeout());

Expand Down Expand Up @@ -299,14 +298,13 @@ public void runsClosingCallbackOnShutdown() throws Exception {
.keyValueService(new InMemoryAtlasDbConfig())
.defaultLockTimeoutSeconds(120)
.build();
TransactionManagerOptions options = TransactionManagerOptions.builder()
.config(realConfig)
.env(environment)
.build();

Runnable callback = mock(Runnable.class);

SerializableTransactionManager manager = TransactionManagers.create(options);
SerializableTransactionManager manager = TransactionManagers.builder()
.config(realConfig)
.env(environment)
.buildSerializable();
manager.registerClosingCallback(callback);
manager.close();
verify(callback, times(1)).run();
Expand All @@ -318,11 +316,10 @@ public void keyValueServiceMetricsDoNotContainUserAgent() {
.keyValueService(new InMemoryAtlasDbConfig())
.build();

TransactionManagerOptions options = TransactionManagerOptions.builder()
TransactionManagers.builder()
.config(realConfig)
.env(environment)
.build();
TransactionManagers.create(options);
.buildSerializable();
assertThat(metricsRule.metrics().getNames().stream()
.anyMatch(metricName -> metricName.contains(USER_AGENT_NAME)), is(false));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import com.palantir.atlasdb.console.AtlasConsoleServiceImpl
import com.palantir.atlasdb.console.AtlasConsoleServiceWrapper
import com.palantir.atlasdb.console.exceptions.InvalidTableException
import com.palantir.atlasdb.factory.TransactionManagers
import com.palantir.atlasdb.factory.TransactionManagerOptions
import com.palantir.atlasdb.impl.AtlasDbServiceImpl
import com.palantir.atlasdb.impl.TableMetadataCache
import com.palantir.atlasdb.jackson.AtlasJacksonModule
Expand Down Expand Up @@ -215,11 +214,10 @@ class AtlasCoreModule implements AtlasConsoleModule {
}

private setupConnection(AtlasDbConfig config) {
TransactionManagerOptions options = TransactionManagerOptions.builder()
SerializableTransactionManager tm = TransactionManagers.builder()
.config(config)
.allowHiddenTableAccess(true)
.build();
SerializableTransactionManager tm = TransactionManagers.create(options);
.buildSerializable();
TableMetadataCache cache = new TableMetadataCache(tm.getKeyValueService())
AtlasDbService service = new AtlasDbServiceImpl(tm.getKeyValueService(), tm, cache)
ObjectMapper serviceMapper = new ObjectMapper()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import com.palantir.atlasdb.cas.SimpleCheckAndSetResource;
import com.palantir.atlasdb.config.AtlasDbConfig;
import com.palantir.atlasdb.dropwizard.AtlasDbBundle;
import com.palantir.atlasdb.factory.TransactionManagerOptions;
import com.palantir.atlasdb.factory.TransactionManagers;
import com.palantir.atlasdb.http.NotInitializedExceptionMapper;
import com.palantir.atlasdb.table.description.Schema;
Expand Down Expand Up @@ -99,12 +98,11 @@ private TransactionManager createTransactionManagerWithRetry(AtlasDbConfig confi
}

private TransactionManager createTransactionManager(AtlasDbConfig config, Environment environment) {
TransactionManagerOptions options = TransactionManagerOptions.builder()
return TransactionManagers.builder()
.config(config)
.schemas(ETE_SCHEMAS)
.env(environment.jersey()::register)
.build();
return TransactionManagers.create(options);
.buildSerializable();
}

private void enableEnvironmentVariablesInConfig(Bootstrap<AtlasDbEteConfiguration> bootstrap) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
*/
package com.palantir.atlasdb.server;

import com.palantir.atlasdb.factory.TransactionManagerOptions;
import com.palantir.atlasdb.factory.TransactionManagers;
import com.palantir.atlasdb.impl.AtlasDbServiceImpl;
import com.palantir.atlasdb.impl.TableMetadataCache;
Expand Down Expand Up @@ -44,11 +43,10 @@ public void initialize(Bootstrap<AtlasDbServiceServerConfiguration> bootstrap) {
public void run(AtlasDbServiceServerConfiguration config, final Environment environment) throws Exception {
AtlasDbMetrics.setMetricRegistry(environment.metrics());

TransactionManagerOptions options = TransactionManagerOptions.builder()
SerializableTransactionManager tm = TransactionManagers.builder()
.config(config.getConfig())
.env(environment.jersey()::register)
.build();
SerializableTransactionManager tm = TransactionManagers.create(options);
.buildSerializable();

TableMetadataCache cache = new TableMetadataCache(tm.getKeyValueService());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@
import com.palantir.atlasdb.config.ImmutableAtlasDbConfig;
import com.palantir.atlasdb.config.ImmutableServerListConfig;
import com.palantir.atlasdb.config.ImmutableTimeLockClientConfig;
import com.palantir.atlasdb.factory.TransactionManagerOptions;
import com.palantir.atlasdb.factory.TransactionManagers;
import com.palantir.atlasdb.keyvalue.api.Cell;
import com.palantir.atlasdb.keyvalue.api.Namespace;
Expand Down Expand Up @@ -87,7 +86,7 @@ public AsyncTimelockServiceTransactionIntegrationTest(TestableTimelockCluster cl
.build())
.build())
.build();
txnManager = TransactionManagers.create(TransactionManagerOptions.builder().config(config).build());
txnManager = TransactionManagers.builder().config(config).buildSerializable();
txnManager.getKeyValueService().createTable(TABLE, AtlasDbConstants.GENERIC_TABLE_METADATA);
}

Expand Down

0 comments on commit b899cca

Please sign in to comment.