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

Commit ba3d316

Browse files
authored
Timelock uses tritium-preprocessor instrumentation (#7132)
Timelock uses tritium-preprocessor instrumentation
1 parent 27a2375 commit ba3d316

File tree

14 files changed

+419
-45
lines changed

14 files changed

+419
-45
lines changed

atlasdb-client/src/main/java/com/palantir/atlasdb/util/AtlasDbMetrics.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import com.palantir.tritium.api.event.InstrumentationFilter;
2424
import com.palantir.tritium.event.InstrumentationFilters;
2525
import com.palantir.tritium.event.InvocationContext;
26+
import com.palantir.tritium.event.InvocationEventHandler;
2627
import com.palantir.tritium.metrics.caffeine.CaffeineCacheStats;
2728
import com.palantir.tritium.metrics.registry.TaggedMetricRegistry;
2829
import com.palantir.tritium.proxy.Instrumentation;
@@ -81,11 +82,18 @@ public static <T, U extends T> T instrumentWithTaggedMetrics(
8182
U service,
8283
Function<InvocationContext, Map<String, String>> tagFunction) {
8384
return Instrumentation.builder(serviceInterface, service)
84-
.withHandler(new TaggedMetricsInvocationEventHandler(
85-
taggedMetrics, MetricRegistry.name(serviceInterface), tagFunction))
85+
.withHandler(taggedMetricsHandler(taggedMetrics, serviceInterface, tagFunction))
8686
.build();
8787
}
8888

89+
public static <T> InvocationEventHandler<InvocationContext> taggedMetricsHandler(
90+
TaggedMetricRegistry taggedMetrics,
91+
Class<T> serviceInterface,
92+
Function<InvocationContext, Map<String, String>> tagFunction) {
93+
return new TaggedMetricsInvocationEventHandler(
94+
taggedMetrics, MetricRegistry.name(serviceInterface), tagFunction);
95+
}
96+
8997
public static void registerCache(MetricRegistry metricRegistry, Cache<?, ?> cache, String metricsPrefix) {
9098
Set<String> existingMetrics = metricRegistry.getMetrics().keySet().stream()
9199
.filter(name -> name.startsWith(metricsPrefix))
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
type: improvement
2+
improvement:
3+
description: Timelock uses tritium-preprocessor instrumentation
4+
links:
5+
- https://github.com/palantir/atlasdb/pull/7132

lock-api/build.gradle

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,15 @@ dependencies {
2727
implementation 'com.palantir.safe-logging:safe-logging'
2828
implementation 'com.palantir.safe-logging:preconditions'
2929
implementation 'com.palantir.refreshable:refreshable'
30+
implementation 'com.palantir.tritium:tritium-api'
31+
implementation 'com.palantir.tritium:tritium-core'
3032
implementation 'com.palantir.tritium:tritium-ids'
3133

3234
implementation 'com.google.guava:guava'
3335
implementation 'com.palantir.common:streams'
3436
implementation 'com.palantir.conjure.java:conjure-lib'
3537
implementation 'com.palantir.tokens:auth-tokens'
38+
implementation 'com.palantir.tritium:tritium-annotations'
3639
implementation 'io.dropwizard.metrics:metrics-core'
3740
implementation project(':commons-annotations')
3841
implementation project(':commons-executors')
@@ -57,6 +60,7 @@ dependencies {
5760
compileOnly project(":atlasdb-processors")
5861
annotationProcessor 'org.immutables:value'
5962
compileOnly 'org.immutables:value::annotations'
63+
annotationProcessor 'com.palantir.tritium:tritium-processor'
6064

6165
testAnnotationProcessor 'org.immutables:value'
6266
testCompileOnly 'org.immutables:value::annotations'

lock-api/src/main/java/com/palantir/lock/LockService.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import com.palantir.common.annotation.NonIdempotent;
2222
import com.palantir.logsafe.Safe;
2323
import com.palantir.processors.AutoDelegate;
24+
import com.palantir.tritium.annotations.Instrument;
2425
import java.math.BigInteger;
2526
import java.util.Set;
2627
import javax.annotation.Nullable;
@@ -39,6 +40,7 @@
3940
@Path("/lock")
4041
@AutoDelegate
4142
@Beta
43+
@Instrument
4244
public interface LockService extends RemoteLockService {
4345
/**
4446
* Attempts to acquire the requested set of locks. The locks are

timelock-agent/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ dependencies {
2626
implementation 'com.palantir.safe-logging:safe-logging'
2727
implementation 'com.palantir.sls.versions:sls-versions'
2828
implementation 'com.palantir.tokens:auth-tokens'
29+
implementation 'com.palantir.tritium:tritium-annotations'
2930
implementation 'com.palantir.tritium:tritium-api'
3031
implementation 'com.palantir.tritium:tritium-registry'
3132
implementation 'com.zaxxer:HikariCP'

timelock-agent/src/main/java/com/palantir/atlasdb/timelock/paxos/LeadershipComponents.java

Lines changed: 46 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,30 +18,41 @@
1818

1919
import com.google.common.collect.ImmutableList;
2020
import com.google.common.io.Closer;
21+
import com.palantir.atlasdb.timelock.AsyncTimelockService;
22+
import com.palantir.atlasdb.timelock.InstrumentedAsyncTimelockService;
23+
import com.palantir.atlasdb.timelock.TimelockNamespaces;
24+
import com.palantir.atlasdb.util.AtlasDbMetrics;
25+
import com.palantir.common.concurrent.ConcurrentMaps;
2126
import com.palantir.leader.LeaderElectionService;
2227
import com.palantir.leader.NotCurrentLeaderException;
2328
import com.palantir.leader.proxy.AwaitingLeadershipProxy;
2429
import com.palantir.leader.proxy.LeadershipCoordinator;
30+
import com.palantir.lock.InstrumentedLockService;
31+
import com.palantir.lock.LockService;
2532
import com.palantir.logsafe.logger.SafeLogger;
2633
import com.palantir.logsafe.logger.SafeLoggerFactory;
2734
import com.palantir.paxos.Client;
2835
import com.palantir.timelock.paxos.HealthCheckPinger;
2936
import com.palantir.timelock.paxos.LeaderPingHealthCheck;
3037
import com.palantir.timelock.paxos.NamespaceTracker;
38+
import com.palantir.tritium.event.InvocationContext;
39+
import com.palantir.tritium.metrics.registry.TaggedMetricRegistry;
3140
import java.io.Closeable;
3241
import java.io.IOException;
3342
import java.util.Collection;
3443
import java.util.List;
35-
import java.util.concurrent.ConcurrentHashMap;
44+
import java.util.Map;
3645
import java.util.concurrent.ConcurrentMap;
46+
import java.util.function.Function;
3747
import java.util.function.Supplier;
3848
import org.immutables.value.Value;
3949

4050
public class LeadershipComponents {
4151

4252
private static final SafeLogger log = SafeLoggerFactory.get(LeadershipComponents.class);
4353

44-
private final ConcurrentMap<Client, LeadershipContext> leadershipContextByClient = new ConcurrentHashMap<>();
54+
private final ConcurrentMap<Client, LeadershipContext> leadershipContextByClient =
55+
ConcurrentMaps.newWithExpectedEntries(TimelockNamespaces.estimatedClients());
4556
private final ShutdownAwareCloser closer = new ShutdownAwareCloser();
4657

4758
private final NetworkClientFactories.Factory<LeadershipContext> leadershipContextFactory;
@@ -54,15 +65,42 @@ public class LeadershipComponents {
5465
this.healthCheckPingers = healthCheckPingers;
5566
}
5667

57-
public <T> T wrapInLeadershipProxy(Client client, Class<T> clazz, Supplier<T> delegateSupplier) {
68+
public LeadershipProxies createServices(
69+
Client client,
70+
Supplier<AsyncTimelockService> asyncTimelockServiceSupplier,
71+
Supplier<LockService> lockServiceSupplier) {
5872
LeadershipContext context = getOrCreateNewLeadershipContext(client);
59-
T instance = AwaitingLeadershipProxy.newProxyInstance(clazz, delegateSupplier, context.leadershipCoordinator());
73+
TaggedMetricRegistry metrics = context.leadershipMetrics().taggedMetrics();
74+
Function<InvocationContext, Map<String, String>> tagger =
75+
context.leadershipMetrics().suspectedLeaderTag();
76+
return ImmutableLeadershipProxies.of(
77+
InstrumentedAsyncTimelockService.builder(wrapInLeadershipProxy(
78+
AsyncTimelockService.class,
79+
asyncTimelockServiceSupplier,
80+
context.leadershipCoordinator()))
81+
.withHandler(AtlasDbMetrics.taggedMetricsHandler(metrics, AsyncTimelockService.class, tagger))
82+
.build(),
83+
InstrumentedLockService.builder(wrapInLeadershipProxy(
84+
LockService.class, lockServiceSupplier, context.leadershipCoordinator()))
85+
.withHandler(AtlasDbMetrics.taggedMetricsHandler(metrics, LockService.class, tagger))
86+
.build());
87+
}
6088

89+
private <T> T wrapInLeadershipProxy(
90+
Class<T> clazz, Supplier<T> delegateSupplier, LeadershipCoordinator leadershipCoordinator) {
91+
T instance = AwaitingLeadershipProxy.newProxyInstance(clazz, delegateSupplier, leadershipCoordinator);
6192
// this is acceptable since the proxy returned implements Closeable and needs to be closed
62-
Closeable closeableInstance = (Closeable) instance;
63-
closer.register(closeableInstance);
93+
closer.register((Closeable) instance);
94+
return instance;
95+
}
96+
97+
@Value.Immutable(builder = false)
98+
public interface LeadershipProxies {
99+
@Value.Parameter
100+
AsyncTimelockService asyncTimelockService();
64101

65-
return context.leadershipMetrics().instrument(clazz, instance);
102+
@Value.Parameter
103+
LockService lockService();
66104
}
67105

68106
public void shutdown() {
@@ -143,7 +181,7 @@ synchronized void shutdown() {
143181
}
144182

145183
@Value.Immutable
146-
abstract static class LeadershipContext {
184+
public abstract static class LeadershipContext {
147185
abstract LeaderElectionService leaderElectionService();
148186

149187
abstract LeadershipCoordinator leadershipCoordinator();

timelock-agent/src/main/java/com/palantir/atlasdb/timelock/paxos/LeadershipContextFactory.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,15 +139,16 @@ public LeadershipContext create(Client client) {
139139
BatchingLeaderElectionService leaderElectionService =
140140
leaderElectionServiceFactory().create(clientAwareComponents);
141141

142-
LeadershipCoordinator leadershipCoordinator =
143-
leadershipCoordinatorFactory().create(leaderElectionService);
142+
LeadershipCoordinatorFactory coordinatorFactory = leadershipCoordinatorFactory();
143+
LeadershipCoordinator leadershipCoordinator = coordinatorFactory.create(leaderElectionService);
144144

145145
return ImmutableLeadershipContext.builder()
146146
.leadershipMetrics(clientAwareComponents.leadershipMetrics())
147147
.leaderElectionService(leaderElectionService)
148148
.addCloseables(leaderElectionService)
149149
.leadershipCoordinator(leadershipCoordinator)
150-
.addCloseables(leadershipCoordinatorFactory())
150+
.addCloseables(leadershipCoordinator)
151+
.addCloseables(coordinatorFactory)
151152
.addAllCloseables(leaderPingerFactory().closeables())
152153
.build();
153154
}

timelock-agent/src/main/java/com/palantir/atlasdb/timelock/paxos/TimelockLeadershipMetrics.java

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,22 +21,25 @@
2121
import com.google.common.collect.SetMultimap;
2222
import com.palantir.atlasdb.AtlasDbMetricNames;
2323
import com.palantir.atlasdb.timelock.paxos.AutobatchingLeadershipObserverFactory.LeadershipEvent;
24-
import com.palantir.atlasdb.util.AtlasDbMetrics;
2524
import com.palantir.leader.LeadershipObserver;
2625
import com.palantir.leader.PaxosLeadershipEventRecorder;
2726
import com.palantir.logsafe.SafeArg;
2827
import com.palantir.logsafe.exceptions.SafeIllegalArgumentException;
2928
import com.palantir.paxos.Client;
29+
import com.palantir.tritium.event.InvocationContext;
3030
import com.palantir.tritium.metrics.registry.MetricName;
31+
import com.palantir.tritium.metrics.registry.TaggedMetricRegistry;
3132
import java.util.List;
33+
import java.util.Map;
3234
import java.util.Optional;
3335
import java.util.function.Consumer;
36+
import java.util.function.Function;
3437
import java.util.function.Predicate;
3538
import org.immutables.value.Value;
3639

3740
@Value.Immutable
3841
public abstract class TimelockLeadershipMetrics implements Dependencies.LeadershipMetrics {
39-
@Value.Derived
42+
@Value.Lazy
4043
List<SafeArg<String>> namespaceAsLoggingArgs() {
4144
return ImmutableList.of(
4245
SafeArg.of(
@@ -45,28 +48,30 @@ List<SafeArg<String>> namespaceAsLoggingArgs() {
4548
SafeArg.of(AtlasDbMetricNames.TAG_CLIENT, proxyClient().value()));
4649
}
4750

48-
@Value.Derived
51+
@Value.Lazy
4952
public PaxosLeadershipEventRecorder eventRecorder() {
5053
return PaxosLeadershipEventRecorder.create(
51-
metrics().clientScopedMetrics().metricRegistryForClient(proxyClient()), // metrics for client etc.
54+
taggedMetrics(), // metrics for client etc.
5255
leaderUuid().toString(),
5356
leadershipObserver(),
5457
namespaceAsLoggingArgs());
5558
}
5659

57-
@Value.Derived
60+
@Value.Lazy
5861
LeadershipObserver leadershipObserver() {
5962
return leadershipObserverFactory().create(proxyClient());
6063
}
6164

62-
public <T> T instrument(Class<T> clazz, T instance) {
63-
return AtlasDbMetrics.instrumentWithTaggedMetrics(
64-
metrics().clientScopedMetrics().metricRegistryForClient(proxyClient()),
65-
clazz,
66-
instance,
67-
_context -> ImmutableMap.of(
68-
AtlasDbMetricNames.TAG_CURRENT_SUSPECTED_LEADER,
69-
String.valueOf(localPingableLeader().ping())));
65+
@Value.Lazy
66+
public Function<InvocationContext, Map<String, String>> suspectedLeaderTag() {
67+
return _context -> ImmutableMap.of(
68+
AtlasDbMetricNames.TAG_CURRENT_SUSPECTED_LEADER,
69+
String.valueOf(localPingableLeader().ping()));
70+
}
71+
72+
@Value.Lazy
73+
public TaggedMetricRegistry taggedMetrics() {
74+
return metrics().clientScopedMetrics().metricRegistryForClient(proxyClient());
7075
}
7176

7277
public static AutobatchingLeadershipObserverFactory createFactory(TimelockPaxosMetrics metrics) {

timelock-agent/src/main/java/com/palantir/timelock/paxos/AsyncTimeLockServicesCreator.java

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import com.palantir.atlasdb.timelock.lockwatches.BufferMetrics;
2828
import com.palantir.atlasdb.timelock.lockwatches.RequestMetrics;
2929
import com.palantir.atlasdb.timelock.paxos.LeadershipComponents;
30+
import com.palantir.atlasdb.timelock.paxos.LeadershipComponents.LeadershipProxies;
3031
import com.palantir.atlasdb.util.MetricsManager;
3132
import com.palantir.common.concurrent.NamedThreadFactory;
3233
import com.palantir.common.concurrent.PTExecutors;
@@ -68,17 +69,12 @@ public TimeLockServices createTimeLockServices(
6869
Supplier<LockService> rawLockServiceSupplier) {
6970
log.info("Creating async timelock services for client {}", SafeArg.of("client", client));
7071
LockLog maybeEnhancedLockLog = maybeEnhancedLockLog(client);
71-
72-
AsyncTimelockService asyncTimelockService = leadershipComponents.wrapInLeadershipProxy(
73-
client,
74-
AsyncTimelockService.class,
75-
() -> createRawAsyncTimelockService(client, rawTimestampServiceSupplier, maybeEnhancedLockLog));
76-
77-
LockService lockService = leadershipComponents.wrapInLeadershipProxy(
72+
LeadershipProxies leadershipProxies = leadershipComponents.createServices(
7873
client,
79-
LockService.class,
74+
() -> createRawAsyncTimelockService(client, rawTimestampServiceSupplier, maybeEnhancedLockLog),
8075
Suppliers.compose(NonTransactionalLockService::new, rawLockServiceSupplier::get));
81-
76+
AsyncTimelockService asyncTimelockService = leadershipProxies.asyncTimelockService();
77+
LockService lockService = leadershipProxies.lockService();
8278
return TimeLockServices.create(
8379
asyncTimelockService, lockService, asyncTimelockService, asyncTimelockService, maybeEnhancedLockLog);
8480
}

0 commit comments

Comments
 (0)