Skip to content

Commit

Permalink
[Logging Improvement] Using lambda invocations instead of checking de…
Browse files Browse the repository at this point in the history
…bug/trace isEnabled explicitly

Converted debug/trace/warn/info checks to lambda based logging APIs.

Resolves opensearch-project#8646

Signed-off-by: Abdul Muneer Kolarkunnu <[email protected]>
  • Loading branch information
akolarkunnu committed Jul 19, 2024
1 parent 825529a commit f8790c5
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -210,10 +210,9 @@ public void testAllocationWithDisruption() throws Exception {
logger.info("--> Creating index test{} with primary {} and replica {}", i, shardCount, replicaCount);
createIndex("test" + i, shardCount, replicaCount, i % 2 == 0);
ensureGreen(TimeValue.timeValueSeconds(60));
if (logger.isTraceEnabled()) {
state = client().admin().cluster().prepareState().execute().actionGet().getState();
logger.info(ShardAllocations.printShardDistribution(state));
}
logger.trace(
() -> ShardAllocations.printShardDistribution(client().admin().cluster().prepareState().execute().actionGet().getState())
);
}
state = client().admin().cluster().prepareState().execute().actionGet().getState();
logger.info(ShardAllocations.printShardDistribution(state));
Expand Down Expand Up @@ -271,10 +270,9 @@ public void testAllocationAndRebalanceWithDisruption() throws Exception {
logger.info("--> Creating index test{} with primary {} and replica {}", i, shardCount, replicaCount);
createIndex("test" + i, shardCount, replicaCount, i % 2 == 0);
ensureGreen(TimeValue.timeValueSeconds(60));
if (logger.isTraceEnabled()) {
state = client().admin().cluster().prepareState().execute().actionGet().getState();
logger.info(ShardAllocations.printShardDistribution(state));
}
logger.trace(
() -> ShardAllocations.printShardDistribution(client().admin().cluster().prepareState().execute().actionGet().getState())
);
}
state = client().admin().cluster().prepareState().execute().actionGet().getState();
logger.info(ShardAllocations.printShardDistribution(state));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -331,8 +331,8 @@ private ClusterState applyRequest(
updatedMapping = true;
if (logger.isDebugEnabled()) {
logger.debug("{} create_mapping with source [{}]", index, updatedSource);
} else if (logger.isInfoEnabled()) {
logger.info("{} create_mapping", index);
} else {
logger.info("{} create_mapping", () -> index);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -488,16 +488,8 @@ private void runTask(UpdateTask task) {
warnAboutSlowTaskIfNeeded(executionTime, task.source, stopWatch);
task.listener.onSuccess(task.source);
} else {
if (logger.isTraceEnabled()) {
logger.debug(
"cluster state updated, version [{}], source [{}]\n{}",
newClusterState.version(),
task.source,
newClusterState
);
} else {
logger.debug("cluster state updated, version [{}], source [{}]", newClusterState.version(), task.source);
}
logger.debug("cluster state updated, version [{}], source [{}]", () -> newClusterState.version(), () -> task.source);
logger.trace("{}", () -> newClusterState);
try {
applyChanges(task, previousClusterState, newClusterState, stopWatch);
TimeValue executionTime = TimeValue.timeValueMillis(Math.max(0, currentTimeInMillis() - startTimeMS));
Expand All @@ -512,30 +504,17 @@ private void runTask(UpdateTask task) {
task.listener.onSuccess(task.source);
} catch (Exception e) {
TimeValue executionTime = TimeValue.timeValueMillis(Math.max(0, currentTimeInMillis() - startTimeMS));
if (logger.isTraceEnabled()) {
logger.warn(
new ParameterizedMessage(
"failed to apply updated cluster state in [{}]:\nversion [{}], uuid [{}], source [{}]\n{}",
executionTime,
newClusterState.version(),
newClusterState.stateUUID(),
task.source,
newClusterState
),
e
);
} else {
logger.warn(
new ParameterizedMessage(
"failed to apply updated cluster state in [{}]:\nversion [{}], uuid [{}], source [{}]",
executionTime,
newClusterState.version(),
newClusterState.stateUUID(),
task.source
),
e
);
}
logger.warn(
() -> new ParameterizedMessage(
"failed to apply updated cluster state in [{}]:\nversion [{}], uuid [{}], source [{}]",
executionTime,
newClusterState.version(),
newClusterState.stateUUID(),
task.source
),
e
);
logger.trace("{}", () -> newClusterState);
// failing to apply a cluster state with an exception indicates a bug in validation or in one of the appliers; if we
// continue we will retry with the same cluster state but that might not help.
assert applicationMayFail();
Expand Down

0 comments on commit f8790c5

Please sign in to comment.