Skip to content

Commit f7418a6

Browse files
committed
Limit calls to Conversions.resolveExecutionProfile
Those repeated calls account for a non-negligible portion of my application CPU (0.6%) and can definitly be a final field so that it gets resolved only once per CqlRequestHandler.
1 parent ef2edba commit f7418a6

File tree

2 files changed

+41
-26
lines changed

2 files changed

+41
-26
lines changed

core/src/main/java/com/datastax/oss/driver/internal/core/cql/Conversions.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -541,6 +541,13 @@ public static boolean resolveIdempotence(Request request, InternalDriverContext
541541
: requestIsIdempotent;
542542
}
543543

544+
public static boolean resolveIdempotence(Request request, DriverExecutionProfile executionProfile) {
545+
Boolean requestIsIdempotent = request.isIdempotent();
546+
return (requestIsIdempotent == null)
547+
? executionProfile.getBoolean(DefaultDriverOption.REQUEST_DEFAULT_IDEMPOTENCE)
548+
: requestIsIdempotent;
549+
}
550+
544551
public static Duration resolveRequestTimeout(Request request, InternalDriverContext context) {
545552
Duration timeout = request.getTimeout();
546553
return timeout != null
@@ -549,14 +556,30 @@ public static Duration resolveRequestTimeout(Request request, InternalDriverCont
549556
.getDuration(DefaultDriverOption.REQUEST_TIMEOUT);
550557
}
551558

559+
public static Duration resolveRequestTimeout(Request request, DriverExecutionProfile executionProfile) {
560+
Duration timeout = request.getTimeout();
561+
return timeout != null
562+
? timeout
563+
: executionProfile.getDuration(DefaultDriverOption.REQUEST_TIMEOUT);
564+
}
565+
552566
public static RetryPolicy resolveRetryPolicy(Request request, InternalDriverContext context) {
553567
DriverExecutionProfile executionProfile = resolveExecutionProfile(request, context);
554568
return context.getRetryPolicy(executionProfile.getName());
555569
}
556570

571+
public static RetryPolicy resolveRetryPolicy(Request request, InternalDriverContext context, DriverExecutionProfile executionProfile) {
572+
return context.getRetryPolicy(executionProfile.getName());
573+
}
574+
557575
public static SpeculativeExecutionPolicy resolveSpeculativeExecutionPolicy(
558576
Request request, InternalDriverContext context) {
559577
DriverExecutionProfile executionProfile = resolveExecutionProfile(request, context);
560578
return context.getSpeculativeExecutionPolicy(executionProfile.getName());
561579
}
580+
581+
public static SpeculativeExecutionPolicy resolveSpeculativeExecutionPolicy(
582+
Request request, InternalDriverContext context, DriverExecutionProfile executionProfile) {
583+
return context.getSpeculativeExecutionPolicy(executionProfile.getName());
584+
}
562585
}

core/src/main/java/com/datastax/oss/driver/internal/core/cql/CqlRequestHandler.java

Lines changed: 18 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ public class CqlRequestHandler implements Throttled {
124124
private final RequestThrottler throttler;
125125
private final RequestTracker requestTracker;
126126
private final SessionMetricUpdater sessionMetricUpdater;
127+
private final DriverExecutionProfile executionProfile;
127128

128129
// The errors on the nodes that were already tried (lazily initialized on the first error).
129130
// We don't use a map because nodes can appear multiple times.
@@ -165,7 +166,8 @@ protected CqlRequestHandler(
165166
this.sessionMetricUpdater = session.getMetricUpdater();
166167

167168
this.timer = context.getNettyOptions().getTimer();
168-
Duration timeout = Conversions.resolveRequestTimeout(statement, context);
169+
this.executionProfile = Conversions.resolveExecutionProfile(initialStatement, context);
170+
Duration timeout = Conversions.resolveRequestTimeout(statement, executionProfile);
169171
this.scheduledTimeout = scheduleTimeout(timeout);
170172

171173
this.throttler = context.getRequestThrottler();
@@ -174,8 +176,6 @@ protected CqlRequestHandler(
174176

175177
@Override
176178
public void onThrottleReady(boolean wasDelayed) {
177-
DriverExecutionProfile executionProfile =
178-
Conversions.resolveExecutionProfile(initialStatement, context);
179179
if (wasDelayed
180180
// avoid call to nanoTime() if metric is disabled:
181181
&& sessionMetricUpdater.isEnabled(
@@ -274,8 +274,6 @@ private void sendRequest(
274274
retryCount,
275275
scheduleNextExecution,
276276
logPrefix);
277-
DriverExecutionProfile executionProfile =
278-
Conversions.resolveExecutionProfile(statement, context);
279277
Message message = Conversions.toMessage(statement, executionProfile, context);
280278
channel
281279
.write(message, statement.isTracing(), statement.getCustomPayload(), nodeResponseCallback)
@@ -336,35 +334,35 @@ private void setFinalResult(
336334
requestTracker.onNodeSuccess(
337335
callback.statement,
338336
nodeLatencyNanos,
339-
callback.executionProfile,
337+
executionProfile,
340338
callback.node,
341339
logPrefix);
342340
requestTracker.onSuccess(
343341
callback.statement,
344342
totalLatencyNanos,
345-
callback.executionProfile,
343+
executionProfile,
346344
callback.node,
347345
logPrefix);
348346
}
349347
if (sessionMetricUpdater.isEnabled(
350-
DefaultSessionMetric.CQL_REQUESTS, callback.executionProfile.getName())) {
348+
DefaultSessionMetric.CQL_REQUESTS, executionProfile.getName())) {
351349
if (completionTimeNanos == NANOTIME_NOT_MEASURED_YET) {
352350
completionTimeNanos = System.nanoTime();
353351
totalLatencyNanos = completionTimeNanos - startTimeNanos;
354352
}
355353
sessionMetricUpdater.updateTimer(
356354
DefaultSessionMetric.CQL_REQUESTS,
357-
callback.executionProfile.getName(),
355+
executionProfile.getName(),
358356
totalLatencyNanos,
359357
TimeUnit.NANOSECONDS);
360358
}
361359
}
362360
// log the warnings if they have NOT been disabled
363361
if (!executionInfo.getWarnings().isEmpty()
364-
&& callback.executionProfile.getBoolean(DefaultDriverOption.REQUEST_LOG_WARNINGS)
362+
&& executionProfile.getBoolean(DefaultDriverOption.REQUEST_LOG_WARNINGS)
365363
&& LOG.isWarnEnabled()) {
366364
logServerWarnings(
367-
callback.statement, callback.executionProfile, executionInfo.getWarnings());
365+
callback.statement, executionProfile, executionInfo.getWarnings());
368366
}
369367
} catch (Throwable error) {
370368
setFinalError(callback.statement, error, callback.node, -1);
@@ -416,21 +414,17 @@ private ExecutionInfo buildExecutionInfo(
416414
schemaInAgreement,
417415
session,
418416
context,
419-
callback.executionProfile);
417+
executionProfile);
420418
}
421419

422420
@Override
423421
public void onThrottleFailure(@NonNull RequestThrottlingException error) {
424-
DriverExecutionProfile executionProfile =
425-
Conversions.resolveExecutionProfile(initialStatement, context);
426422
sessionMetricUpdater.incrementCounter(
427423
DefaultSessionMetric.THROTTLING_ERRORS, executionProfile.getName());
428424
setFinalError(initialStatement, error, null, -1);
429425
}
430426

431427
private void setFinalError(Statement<?> statement, Throwable error, Node node, int execution) {
432-
DriverExecutionProfile executionProfile =
433-
Conversions.resolveExecutionProfile(statement, context);
434428
if (error instanceof DriverException) {
435429
((DriverException) error)
436430
.setExecutionInfo(
@@ -473,7 +467,6 @@ private class NodeResponseCallback
473467

474468
private final long nodeStartTimeNanos = System.nanoTime();
475469
private final Statement<?> statement;
476-
private final DriverExecutionProfile executionProfile;
477470
private final Node node;
478471
private final Queue<Node> queryPlan;
479472
private final DriverChannel channel;
@@ -503,7 +496,6 @@ private NodeResponseCallback(
503496
this.retryCount = retryCount;
504497
this.scheduleNextExecution = scheduleNextExecution;
505498
this.logPrefix = logPrefix + "|" + execution;
506-
this.executionProfile = Conversions.resolveExecutionProfile(statement, context);
507499
}
508500

509501
// this gets invoked once the write completes.
@@ -542,12 +534,12 @@ public void operationComplete(Future<java.lang.Void> future) throws Exception {
542534
cancel();
543535
} else {
544536
inFlightCallbacks.add(this);
545-
if (scheduleNextExecution && Conversions.resolveIdempotence(statement, context)) {
537+
if (scheduleNextExecution && Conversions.resolveIdempotence(statement, executionProfile)) {
546538
int nextExecution = execution + 1;
547539
long nextDelay;
548540
try {
549541
nextDelay =
550-
Conversions.resolveSpeculativeExecutionPolicy(statement, context)
542+
Conversions.resolveSpeculativeExecutionPolicy(statement, context, executionProfile)
551543
.nextExecution(node, keyspace, statement, nextExecution);
552544
} catch (Throwable cause) {
553545
// This is a bug in the policy, but not fatal since we have at least one other
@@ -695,7 +687,7 @@ private void processErrorResponse(Error errorMessage) {
695687
true,
696688
reprepareMessage,
697689
repreparePayload.customPayload,
698-
Conversions.resolveRequestTimeout(statement, context),
690+
Conversions.resolveRequestTimeout(statement, executionProfile),
699691
throttler,
700692
sessionMetricUpdater,
701693
logPrefix);
@@ -765,7 +757,7 @@ private void processErrorResponse(Error errorMessage) {
765757
trackNodeError(node, error, NANOTIME_NOT_MEASURED_YET);
766758
setFinalError(statement, error, node, execution);
767759
} else {
768-
RetryPolicy retryPolicy = Conversions.resolveRetryPolicy(statement, context);
760+
RetryPolicy retryPolicy = Conversions.resolveRetryPolicy(statement, context, executionProfile);
769761
RetryVerdict verdict;
770762
if (error instanceof ReadTimeoutException) {
771763
ReadTimeoutException readTimeout = (ReadTimeoutException) error;
@@ -786,7 +778,7 @@ private void processErrorResponse(Error errorMessage) {
786778
} else if (error instanceof WriteTimeoutException) {
787779
WriteTimeoutException writeTimeout = (WriteTimeoutException) error;
788780
verdict =
789-
Conversions.resolveIdempotence(statement, context)
781+
Conversions.resolveIdempotence(statement, executionProfile)
790782
? retryPolicy.onWriteTimeoutVerdict(
791783
statement,
792784
writeTimeout.getConsistencyLevel(),
@@ -818,7 +810,7 @@ private void processErrorResponse(Error errorMessage) {
818810
DefaultNodeMetric.IGNORES_ON_UNAVAILABLE);
819811
} else {
820812
verdict =
821-
Conversions.resolveIdempotence(statement, context)
813+
Conversions.resolveIdempotence(statement, executionProfile)
822814
? retryPolicy.onErrorResponseVerdict(statement, error, retryCount)
823815
: RetryVerdict.RETHROW;
824816
updateErrorMetrics(
@@ -897,12 +889,12 @@ public void onFailure(Throwable error) {
897889
}
898890
LOG.trace("[{}] Request failure, processing: {}", logPrefix, error);
899891
RetryVerdict verdict;
900-
if (!Conversions.resolveIdempotence(statement, context)
892+
if (!Conversions.resolveIdempotence(statement, executionProfile)
901893
|| error instanceof FrameTooLongException) {
902894
verdict = RetryVerdict.RETHROW;
903895
} else {
904896
try {
905-
RetryPolicy retryPolicy = Conversions.resolveRetryPolicy(statement, context);
897+
RetryPolicy retryPolicy = Conversions.resolveRetryPolicy(statement, context, executionProfile);
906898
verdict = retryPolicy.onRequestAbortedVerdict(statement, error, retryCount);
907899
} catch (Throwable cause) {
908900
setFinalError(

0 commit comments

Comments
 (0)