Skip to content

Commit 80d7c61

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 db4c807 commit 80d7c61

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
@@ -543,21 +543,44 @@ public static boolean resolveIdempotence(Request request, InternalDriverContext
543543
: requestIsIdempotent;
544544
}
545545

546+
public static boolean resolveIdempotence(Request request, DriverExecutionProfile executionProfile) {
547+
Boolean requestIsIdempotent = request.isIdempotent();
548+
return (requestIsIdempotent == null)
549+
? executionProfile.getBoolean(DefaultDriverOption.REQUEST_DEFAULT_IDEMPOTENCE)
550+
: requestIsIdempotent;
551+
}
552+
546553
public static Duration resolveRequestTimeout(Request request, InternalDriverContext context) {
547554
DriverExecutionProfile executionProfile = resolveExecutionProfile(request, context);
548555
return request.getTimeout() != null
549556
? request.getTimeout()
550557
: executionProfile.getDuration(DefaultDriverOption.REQUEST_TIMEOUT);
551558
}
552559

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

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

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
@@ -126,6 +126,7 @@ public class CqlRequestHandler implements Throttled {
126126
private final RequestThrottler throttler;
127127
private final RequestTracker requestTracker;
128128
private final SessionMetricUpdater sessionMetricUpdater;
129+
private final DriverExecutionProfile executionProfile;
129130

130131
// The errors on the nodes that were already tried (lazily initialized on the first error).
131132
// We don't use a map because nodes can appear multiple times.
@@ -167,7 +168,8 @@ protected CqlRequestHandler(
167168
this.sessionMetricUpdater = session.getMetricUpdater();
168169

169170
this.timer = context.getNettyOptions().getTimer();
170-
Duration timeout = Conversions.resolveRequestTimeout(statement, context);
171+
this.executionProfile = Conversions.resolveExecutionProfile(initialStatement, context);
172+
Duration timeout = Conversions.resolveRequestTimeout(statement, executionProfile);
171173
this.scheduledTimeout = scheduleTimeout(timeout);
172174

173175
this.throttler = context.getRequestThrottler();
@@ -176,8 +178,6 @@ protected CqlRequestHandler(
176178

177179
@Override
178180
public void onThrottleReady(boolean wasDelayed) {
179-
DriverExecutionProfile executionProfile =
180-
Conversions.resolveExecutionProfile(initialStatement, context);
181181
if (wasDelayed
182182
// avoid call to nanoTime() if metric is disabled:
183183
&& sessionMetricUpdater.isEnabled(
@@ -276,8 +276,6 @@ private void sendRequest(
276276
retryCount,
277277
scheduleNextExecution,
278278
logPrefix);
279-
DriverExecutionProfile executionProfile =
280-
Conversions.resolveExecutionProfile(statement, context);
281279
Message message = Conversions.toMessage(statement, executionProfile, context);
282280
channel
283281
.write(message, statement.isTracing(), statement.getCustomPayload(), nodeResponseCallback)
@@ -338,35 +336,35 @@ private void setFinalResult(
338336
requestTracker.onNodeSuccess(
339337
callback.statement,
340338
nodeLatencyNanos,
341-
callback.executionProfile,
339+
executionProfile,
342340
callback.node,
343341
logPrefix);
344342
requestTracker.onSuccess(
345343
callback.statement,
346344
totalLatencyNanos,
347-
callback.executionProfile,
345+
executionProfile,
348346
callback.node,
349347
logPrefix);
350348
}
351349
if (sessionMetricUpdater.isEnabled(
352-
DefaultSessionMetric.CQL_REQUESTS, callback.executionProfile.getName())) {
350+
DefaultSessionMetric.CQL_REQUESTS, executionProfile.getName())) {
353351
if (completionTimeNanos == NANOTIME_NOT_MEASURED_YET) {
354352
completionTimeNanos = System.nanoTime();
355353
totalLatencyNanos = completionTimeNanos - startTimeNanos;
356354
}
357355
sessionMetricUpdater.updateTimer(
358356
DefaultSessionMetric.CQL_REQUESTS,
359-
callback.executionProfile.getName(),
357+
executionProfile.getName(),
360358
totalLatencyNanos,
361359
TimeUnit.NANOSECONDS);
362360
}
363361
}
364362
// log the warnings if they have NOT been disabled
365363
if (!executionInfo.getWarnings().isEmpty()
366-
&& callback.executionProfile.getBoolean(DefaultDriverOption.REQUEST_LOG_WARNINGS)
364+
&& executionProfile.getBoolean(DefaultDriverOption.REQUEST_LOG_WARNINGS)
367365
&& LOG.isWarnEnabled()) {
368366
logServerWarnings(
369-
callback.statement, callback.executionProfile, executionInfo.getWarnings());
367+
callback.statement, executionProfile, executionInfo.getWarnings());
370368
}
371369
} catch (Throwable error) {
372370
setFinalError(callback.statement, error, callback.node, -1);
@@ -418,21 +416,17 @@ private ExecutionInfo buildExecutionInfo(
418416
schemaInAgreement,
419417
session,
420418
context,
421-
callback.executionProfile);
419+
executionProfile);
422420
}
423421

424422
@Override
425423
public void onThrottleFailure(@NonNull RequestThrottlingException error) {
426-
DriverExecutionProfile executionProfile =
427-
Conversions.resolveExecutionProfile(initialStatement, context);
428424
sessionMetricUpdater.incrementCounter(
429425
DefaultSessionMetric.THROTTLING_ERRORS, executionProfile.getName());
430426
setFinalError(initialStatement, error, null, -1);
431427
}
432428

433429
private void setFinalError(Statement<?> statement, Throwable error, Node node, int execution) {
434-
DriverExecutionProfile executionProfile =
435-
Conversions.resolveExecutionProfile(statement, context);
436430
if (error instanceof DriverException) {
437431
((DriverException) error)
438432
.setExecutionInfo(
@@ -475,7 +469,6 @@ private class NodeResponseCallback
475469

476470
private final long nodeStartTimeNanos = System.nanoTime();
477471
private final Statement<?> statement;
478-
private final DriverExecutionProfile executionProfile;
479472
private final Node node;
480473
private final Queue<Node> queryPlan;
481474
private final DriverChannel channel;
@@ -505,7 +498,6 @@ private NodeResponseCallback(
505498
this.retryCount = retryCount;
506499
this.scheduleNextExecution = scheduleNextExecution;
507500
this.logPrefix = logPrefix + "|" + execution;
508-
this.executionProfile = Conversions.resolveExecutionProfile(statement, context);
509501
}
510502

511503
// this gets invoked once the write completes.
@@ -544,12 +536,12 @@ public void operationComplete(Future<java.lang.Void> future) throws Exception {
544536
cancel();
545537
} else {
546538
inFlightCallbacks.add(this);
547-
if (scheduleNextExecution && Conversions.resolveIdempotence(statement, context)) {
539+
if (scheduleNextExecution && Conversions.resolveIdempotence(statement, executionProfile)) {
548540
int nextExecution = execution + 1;
549541
long nextDelay;
550542
try {
551543
nextDelay =
552-
Conversions.resolveSpeculativeExecutionPolicy(statement, context)
544+
Conversions.resolveSpeculativeExecutionPolicy(statement, context, executionProfile)
553545
.nextExecution(node, keyspace, statement, nextExecution);
554546
} catch (Throwable cause) {
555547
// This is a bug in the policy, but not fatal since we have at least one other
@@ -697,7 +689,7 @@ private void processErrorResponse(Error errorMessage) {
697689
true,
698690
reprepareMessage,
699691
repreparePayload.customPayload,
700-
Conversions.resolveRequestTimeout(statement, context),
692+
Conversions.resolveRequestTimeout(statement, executionProfile),
701693
throttler,
702694
sessionMetricUpdater,
703695
logPrefix);
@@ -767,7 +759,7 @@ private void processErrorResponse(Error errorMessage) {
767759
trackNodeError(node, error, NANOTIME_NOT_MEASURED_YET);
768760
setFinalError(statement, error, node, execution);
769761
} else {
770-
RetryPolicy retryPolicy = Conversions.resolveRetryPolicy(statement, context);
762+
RetryPolicy retryPolicy = Conversions.resolveRetryPolicy(statement, context, executionProfile);
771763
RetryVerdict verdict;
772764
if (error instanceof ReadTimeoutException) {
773765
ReadTimeoutException readTimeout = (ReadTimeoutException) error;
@@ -788,7 +780,7 @@ private void processErrorResponse(Error errorMessage) {
788780
} else if (error instanceof WriteTimeoutException) {
789781
WriteTimeoutException writeTimeout = (WriteTimeoutException) error;
790782
verdict =
791-
Conversions.resolveIdempotence(statement, context)
783+
Conversions.resolveIdempotence(statement, executionProfile)
792784
? retryPolicy.onWriteTimeoutVerdict(
793785
statement,
794786
writeTimeout.getConsistencyLevel(),
@@ -820,7 +812,7 @@ private void processErrorResponse(Error errorMessage) {
820812
DefaultNodeMetric.IGNORES_ON_UNAVAILABLE);
821813
} else {
822814
verdict =
823-
Conversions.resolveIdempotence(statement, context)
815+
Conversions.resolveIdempotence(statement, executionProfile)
824816
? retryPolicy.onErrorResponseVerdict(statement, error, retryCount)
825817
: RetryVerdict.RETHROW;
826818
updateErrorMetrics(
@@ -899,12 +891,12 @@ public void onFailure(Throwable error) {
899891
}
900892
LOG.trace("[{}] Request failure, processing: {}", logPrefix, error);
901893
RetryVerdict verdict;
902-
if (!Conversions.resolveIdempotence(statement, context)
894+
if (!Conversions.resolveIdempotence(statement, executionProfile)
903895
|| error instanceof FrameTooLongException) {
904896
verdict = RetryVerdict.RETHROW;
905897
} else {
906898
try {
907-
RetryPolicy retryPolicy = Conversions.resolveRetryPolicy(statement, context);
899+
RetryPolicy retryPolicy = Conversions.resolveRetryPolicy(statement, context, executionProfile);
908900
verdict = retryPolicy.onRequestAbortedVerdict(statement, error, retryCount);
909901
} catch (Throwable cause) {
910902
setFinalError(

0 commit comments

Comments
 (0)