8484import java .util .Objects ;
8585import java .util .Optional ;
8686import java .util .concurrent .TimeUnit ;
87+ import java .util .function .Function ;
8788import java .util .function .Supplier ;
8889import java .util .stream .Collectors ;
8990
@@ -221,10 +222,10 @@ protected void onTimeout(List<? extends BatchedTask> tasks, TimeValue timeout) {
221222 }
222223
223224 @ Override
224- protected void run (Object batchingKey , List <? extends BatchedTask > tasks , String tasksSummary ) {
225+ protected void run (Object batchingKey , List <? extends BatchedTask > tasks , Function < Boolean , String > taskSummaryGenerator ) {
225226 ClusterStateTaskExecutor <Object > taskExecutor = (ClusterStateTaskExecutor <Object >) batchingKey ;
226227 List <UpdateTask > updateTasks = (List <UpdateTask >) tasks ;
227- runTasks (new TaskInputs (taskExecutor , updateTasks , tasksSummary ));
228+ runTasks (new TaskInputs (taskExecutor , updateTasks , taskSummaryGenerator ));
228229 }
229230
230231 class UpdateTask extends BatchedTask {
@@ -297,26 +298,33 @@ public static boolean assertNotMasterUpdateThread(String reason) {
297298 }
298299
299300 private void runTasks (TaskInputs taskInputs ) {
300- final String summary = taskInputs .summary ;
301+ final String longSummary = logger .isTraceEnabled () ? taskInputs .taskSummaryGenerator .apply (true ) : "" ;
302+ final String shortSummary = taskInputs .taskSummaryGenerator .apply (false );
303+
301304 if (!lifecycle .started ()) {
302- logger .debug ("processing [{}]: ignoring, cluster-manager service not started" , summary );
305+ logger .debug ("processing [{}]: ignoring, cluster-manager service not started" , shortSummary );
303306 return ;
304307 }
305308
306- logger .debug ("executing cluster state update for [{}]" , summary );
309+ if (logger .isTraceEnabled ()) {
310+ logger .trace ("executing cluster state update for [{}]" , longSummary );
311+ } else {
312+ logger .debug ("executing cluster state update for [{}]" , shortSummary );
313+ }
314+
307315 final ClusterState previousClusterState = state ();
308316
309317 if (!previousClusterState .nodes ().isLocalNodeElectedClusterManager () && taskInputs .runOnlyWhenClusterManager ()) {
310- logger .debug ("failing [{}]: local node is no longer cluster-manager" , summary );
318+ logger .debug ("failing [{}]: local node is no longer cluster-manager" , shortSummary );
311319 taskInputs .onNoLongerClusterManager ();
312320 return ;
313321 }
314322
315323 final long computationStartTime = threadPool .preciseRelativeTimeInNanos ();
316- final TaskOutputs taskOutputs = calculateTaskOutputs (taskInputs , previousClusterState );
324+ final TaskOutputs taskOutputs = calculateTaskOutputs (taskInputs , previousClusterState , shortSummary );
317325 taskOutputs .notifyFailedTasks ();
318326 final TimeValue computationTime = getTimeSince (computationStartTime );
319- logExecutionTime (computationTime , "compute cluster state update" , summary );
327+ logExecutionTime (computationTime , "compute cluster state update" , shortSummary );
320328
321329 clusterManagerMetrics .recordLatency (
322330 clusterManagerMetrics .clusterStateComputeHistogram ,
@@ -328,25 +336,25 @@ private void runTasks(TaskInputs taskInputs) {
328336 final long notificationStartTime = threadPool .preciseRelativeTimeInNanos ();
329337 taskOutputs .notifySuccessfulTasksOnUnchangedClusterState ();
330338 final TimeValue executionTime = getTimeSince (notificationStartTime );
331- logExecutionTime (executionTime , "notify listeners on unchanged cluster state" , summary );
339+ logExecutionTime (executionTime , "notify listeners on unchanged cluster state" , shortSummary );
332340 } else {
333341 final ClusterState newClusterState = taskOutputs .newClusterState ;
334342 if (logger .isTraceEnabled ()) {
335- logger .trace ("cluster state updated, source [{}]\n {}" , summary , newClusterState );
343+ logger .trace ("cluster state updated, source [{}]\n {}" , longSummary , newClusterState );
336344 } else {
337- logger .debug ("cluster state updated, version [{}], source [{}]" , newClusterState .version (), summary );
345+ logger .debug ("cluster state updated, version [{}], source [{}]" , newClusterState .version (), shortSummary );
338346 }
339347 final long publicationStartTime = threadPool .preciseRelativeTimeInNanos ();
340348 try {
341- ClusterChangedEvent clusterChangedEvent = new ClusterChangedEvent (summary , newClusterState , previousClusterState );
349+ ClusterChangedEvent clusterChangedEvent = new ClusterChangedEvent (shortSummary , newClusterState , previousClusterState );
342350 // new cluster state, notify all listeners
343351 final DiscoveryNodes .Delta nodesDelta = clusterChangedEvent .nodesDelta ();
344352 if (nodesDelta .hasChanges () && logger .isInfoEnabled ()) {
345353 String nodesDeltaSummary = nodesDelta .shortSummary ();
346354 if (nodesDeltaSummary .length () > 0 ) {
347355 logger .info (
348356 "{}, term: {}, version: {}, delta: {}" ,
349- summary ,
357+ shortSummary ,
350358 newClusterState .term (),
351359 newClusterState .version (),
352360 nodesDeltaSummary
@@ -357,7 +365,7 @@ private void runTasks(TaskInputs taskInputs) {
357365 logger .debug ("publishing cluster state version [{}]" , newClusterState .version ());
358366 publish (clusterChangedEvent , taskOutputs , publicationStartTime );
359367 } catch (Exception e ) {
360- handleException (summary , publicationStartTime , newClusterState , e );
368+ handleException (shortSummary , publicationStartTime , newClusterState , e );
361369 }
362370 }
363371 }
@@ -452,8 +460,8 @@ private void handleException(String summary, long startTimeMillis, ClusterState
452460 // TODO: do we want to call updateTask.onFailure here?
453461 }
454462
455- private TaskOutputs calculateTaskOutputs (TaskInputs taskInputs , ClusterState previousClusterState ) {
456- ClusterTasksResult <Object > clusterTasksResult = executeTasks (taskInputs , previousClusterState );
463+ private TaskOutputs calculateTaskOutputs (TaskInputs taskInputs , ClusterState previousClusterState , String taskSummary ) {
464+ ClusterTasksResult <Object > clusterTasksResult = executeTasks (taskInputs , previousClusterState , taskSummary );
457465 ClusterState newClusterState = patchVersions (previousClusterState , clusterTasksResult );
458466 return new TaskOutputs (
459467 taskInputs ,
@@ -897,7 +905,7 @@ public void onTimeout() {
897905 }
898906 }
899907
900- private ClusterTasksResult <Object > executeTasks (TaskInputs taskInputs , ClusterState previousClusterState ) {
908+ private ClusterTasksResult <Object > executeTasks (TaskInputs taskInputs , ClusterState previousClusterState , String taskSummary ) {
901909 ClusterTasksResult <Object > clusterTasksResult ;
902910 try {
903911 List <Object > inputs = taskInputs .updateTasks .stream ().map (tUpdateTask -> tUpdateTask .task ).collect (Collectors .toList ());
@@ -913,7 +921,7 @@ private ClusterTasksResult<Object> executeTasks(TaskInputs taskInputs, ClusterSt
913921 "failed to execute cluster state update (on version: [{}], uuid: [{}]) for [{}]\n {}{}{}" ,
914922 previousClusterState .version (),
915923 previousClusterState .stateUUID (),
916- taskInputs . summary ,
924+ taskSummary ,
917925 previousClusterState .nodes (),
918926 previousClusterState .routingTable (),
919927 previousClusterState .getRoutingNodes ()
@@ -955,14 +963,19 @@ private List<Batcher.UpdateTask> getNonFailedTasks(TaskInputs taskInputs, Cluste
955963 * Represents a set of tasks to be processed together with their executor
956964 */
957965 private class TaskInputs {
958- final String summary ;
966+
959967 final List <Batcher .UpdateTask > updateTasks ;
960968 final ClusterStateTaskExecutor <Object > executor ;
969+ final Function <Boolean , String > taskSummaryGenerator ;
961970
962- TaskInputs (ClusterStateTaskExecutor <Object > executor , List <Batcher .UpdateTask > updateTasks , String summary ) {
963- this .summary = summary ;
971+ TaskInputs (
972+ ClusterStateTaskExecutor <Object > executor ,
973+ List <Batcher .UpdateTask > updateTasks ,
974+ final Function <Boolean , String > taskSummaryGenerator
975+ ) {
964976 this .executor = executor ;
965977 this .updateTasks = updateTasks ;
978+ this .taskSummaryGenerator = taskSummaryGenerator ;
966979 }
967980
968981 boolean runOnlyWhenClusterManager () {
0 commit comments