Skip to content

Commit 92ca970

Browse files
alex268polRk
authored andcommitted
Use AtomicHistogram instead of syncronized
1 parent da1d24f commit 92ca970

3 files changed

Lines changed: 29 additions & 15 deletions

File tree

‎slo/src/main/java/tech/ydb/slo/Config.java‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,22 @@
1515
*/
1616
public final class Config {
1717
private final String connectionString;
18+
private final String token;
1819
private final String ref;
1920
private final String workloadName;
2021
private final int durationSeconds;
2122
private final String otlpEndpoint;
2223

2324
private Config(
2425
String connectionString,
26+
String token,
2527
String ref,
2628
String workloadName,
2729
int durationSeconds,
2830
String otlpEndpoint
2931
) {
3032
this.connectionString = connectionString;
33+
this.token = token;
3134
this.ref = ref;
3235
this.workloadName = workloadName;
3336
this.durationSeconds = durationSeconds;
@@ -38,6 +41,10 @@ public String connectionString() {
3841
return connectionString;
3942
}
4043

44+
public String token() {
45+
return token;
46+
}
47+
4148
public String ref() {
4249
return ref;
4350
}
@@ -57,6 +64,7 @@ public String otlpEndpoint() {
5764
/**
5865
* Loads configuration from environment variables.
5966
*
67+
* @return configuration instance
6068
* @throws IllegalStateException if required variables are missing or invalid
6169
*/
6270
public static Config fromEnv() {
@@ -67,12 +75,13 @@ public static Config fromEnv() {
6775
);
6876
}
6977

78+
String token = envOrDefault("YDB_TOKEN", "");
7079
String ref = envOrDefault("WORKLOAD_REF", "unknown");
7180
String workloadName = envOrDefault("WORKLOAD_NAME", "java-slo-workload");
7281
int durationSeconds = parseInt(envOrDefault("WORKLOAD_DURATION", "600"), 600);
7382
String otlpEndpoint = envOrDefault("OTEL_EXPORTER_OTLP_ENDPOINT", "");
7483

75-
return new Config(connectionString, ref, workloadName, durationSeconds, otlpEndpoint);
84+
return new Config(connectionString, token, ref, workloadName, durationSeconds, otlpEndpoint);
7685
}
7786

7887
private static String resolveConnectionString() {

‎slo/src/main/java/tech/ydb/slo/Main.java‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
import org.slf4j.Logger;
66
import org.slf4j.LoggerFactory;
77

8+
import tech.ydb.auth.AuthProvider;
9+
import tech.ydb.auth.NopAuthProvider;
10+
import tech.ydb.auth.TokenAuthProvider;
811
import tech.ydb.core.grpc.GrpcTransport;
912
import tech.ydb.query.QueryClient;
1013
import tech.ydb.slo.kv.KvWorkload;
@@ -78,7 +81,12 @@ public static void main(String[] args) {
7881

7982
int exitCode = 0;
8083
Metrics metrics = Metrics.create(config);
84+
AuthProvider provider = NopAuthProvider.INSTANCE;
85+
if (config.token() != null && !config.token().isEmpty()) {
86+
provider = new TokenAuthProvider(config.token());
87+
}
8188
GrpcTransport transport = GrpcTransport.forConnectionString(config.connectionString())
89+
.withAuthProvider(provider)
8290
.build();
8391
QueryClient queryClient = QueryClient.newClient(transport).build();
8492

‎slo/src/main/java/tech/ydb/slo/Metrics.java‎

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import io.opentelemetry.sdk.metrics.SdkMeterProviderBuilder;
1717
import io.opentelemetry.sdk.metrics.export.PeriodicMetricReader;
1818
import io.opentelemetry.sdk.resources.Resource;
19+
import org.HdrHistogram.AtomicHistogram;
1920
import org.HdrHistogram.Histogram;
2021

2122
/**
@@ -112,7 +113,7 @@ private Metrics(
112113
this.pendingOperations = pendingOperations;
113114
}
114115

115-
/**
116+
/*
116117
* Builds a {@code Metrics} instance configured to push OTLP metrics every
117118
* second to the endpoint from {@code config.otlpEndpoint()}. If the
118119
* endpoint is empty, all metrics are still observable in-process but never
@@ -232,7 +233,7 @@ private static String metricsEndpoint(String otlpEndpoint) {
232233
return trimmed + "/v1/metrics";
233234
}
234235

235-
/**
236+
/*
236237
* Records a started operation and returns a span used to record the
237238
* outcome.
238239
*/
@@ -286,9 +287,7 @@ private void recordOutcome(
286287
if (status == OperationStatus.SUCCESS) {
287288
Histogram histogram = histograms.computeIfAbsent(type, k -> newHistogram());
288289
long clamped = Math.max(HDR_MIN_MICROS, Math.min(HDR_MAX_MICROS, latencyMicros));
289-
synchronized (histogram) {
290-
histogram.recordValue(clamped);
291-
}
290+
histogram.recordValue(clamped);
292291
} else {
293292
errorsTotal.add(1, Attributes.of(
294293
ATTR_REF, ref,
@@ -319,15 +318,13 @@ private static void observeAndResetPercentiles(
319318
long p50Micros;
320319
long p95Micros;
321320
long p99Micros;
322-
synchronized (histogram) {
323-
if (histogram.getTotalCount() == 0) {
324-
continue;
325-
}
326-
p50Micros = histogram.getValueAtPercentile(50.0);
327-
p95Micros = histogram.getValueAtPercentile(95.0);
328-
p99Micros = histogram.getValueAtPercentile(99.0);
329-
histogram.reset();
321+
if (histogram.getTotalCount() == 0) {
322+
continue;
330323
}
324+
p50Micros = histogram.getValueAtPercentile(50.0);
325+
p95Micros = histogram.getValueAtPercentile(95.0);
326+
p99Micros = histogram.getValueAtPercentile(99.0);
327+
histogram.reset();
331328

332329
// Percentile gauges are always tagged with operation_status="success"
333330
// because we only record successful samples (see recordOutcome).
@@ -345,7 +342,7 @@ private static void observeAndResetPercentiles(
345342
}
346343

347344
private static Histogram newHistogram() {
348-
return new Histogram(HDR_MIN_MICROS, HDR_MAX_MICROS, HDR_SIGNIFICANT_DIGITS);
345+
return new AtomicHistogram(HDR_MIN_MICROS, HDR_MAX_MICROS, HDR_SIGNIFICANT_DIGITS);
349346
}
350347

351348
/**

0 commit comments

Comments
 (0)