You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@brooklyn.apache.org by he...@apache.org on 2021/09/14 21:56:29 UTC

[brooklyn-server] 10/27: improve logging for tasks

This is an automated email from the ASF dual-hosted git repository.

heneveld pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/brooklyn-server.git

commit bcf0fc6aae0fd97d50b57b23e0aff8c49891a891
Author: Alex Heneveld <al...@cloudsoftcorp.com>
AuthorDate: Tue Sep 14 09:13:17 2021 +0100

    improve logging for tasks
---
 .../apache/brooklyn/core/entity/AbstractEntity.java  |  2 +-
 .../core/mgmt/internal/BrooklynGarbageCollector.java | 20 ++++++++++++++------
 .../util/core/task/BasicExecutionManager.java        | 14 +++++++++++---
 .../brooklyn/util/core/task/ScheduledTask.java       |  3 +++
 4 files changed, 29 insertions(+), 10 deletions(-)

diff --git a/core/src/main/java/org/apache/brooklyn/core/entity/AbstractEntity.java b/core/src/main/java/org/apache/brooklyn/core/entity/AbstractEntity.java
index 7c9607e..b796f61 100644
--- a/core/src/main/java/org/apache/brooklyn/core/entity/AbstractEntity.java
+++ b/core/src/main/java/org/apache/brooklyn/core/entity/AbstractEntity.java
@@ -838,7 +838,7 @@ public abstract class AbstractEntity extends AbstractBrooklynObject implements E
         for (Location loc : newLocations) {
             NamedStringTag ownerEntityTag = BrooklynTags.findFirstNamedStringTag(BrooklynTags.OWNER_ENTITY_ID, loc.tags().getTags());
             if (ownerEntityTag != null) {
-                if (!getId().equals(ownerEntityTag.getContents())) {
+                if (!getId().equals(ownerEntityTag.getContents()) && !Entities.isReadOnly(this)) {
                     // A location is "owned" if it was created as part of the EntitySpec of an entity (by Brooklyn).
                     // To share a location between entities create it yourself and pass it to any entities that needs it.
                     LOG.info("Adding location {} to entity {}, which is already owned by another entity {}. " +
diff --git a/core/src/main/java/org/apache/brooklyn/core/mgmt/internal/BrooklynGarbageCollector.java b/core/src/main/java/org/apache/brooklyn/core/mgmt/internal/BrooklynGarbageCollector.java
index ca1e075..b1bc870 100644
--- a/core/src/main/java/org/apache/brooklyn/core/mgmt/internal/BrooklynGarbageCollector.java
+++ b/core/src/main/java/org/apache/brooklyn/core/mgmt/internal/BrooklynGarbageCollector.java
@@ -611,17 +611,25 @@ public class BrooklynGarbageCollector {
             LOG.debug("Got CME inspecting tasks, with "+tasksToConsiderDeleting.size()+" found for deletion: "+e);
         }
 
+        if (tasksToConsiderDeleting.isEmpty()) {
+            return 0;
+        }
+
         Collections.sort(tasksToConsiderDeleting, TASKS_OLDEST_FIRST_COMPARATOR);
 
         if (LOG.isDebugEnabled()) {
-            MutableList<Task<?>> tasksToConsiderDeletingNewest = MutableList.copyOf(tasksToConsiderDeleting);
-            Collections.sort(tasksToConsiderDeletingNewest, TASKS_NEWEST_FIRST_COMPARATOR);
+            List<Object> tasksToLog = MutableList.copyOf(tasksToConsiderDeleting);
+            if (tasksToConsiderDeleting.size()>10) {
+                tasksToConsiderDeleting.stream().limit(5).forEach(tasksToLog::add);
+                tasksToLog.add("...");
+                tasksToConsiderDeleting.stream().skip(tasksToConsiderDeleting.size()-5).forEach(tasksToLog::add);
+            } else {
+                tasksToLog.addAll(tasksToConsiderDeleting);
+            }
             LOG.debug("brooklyn-gc detected " + taskTagsInCategoryOverCapacity.size() + " " + category + " "
-                    + "tags over capacity, expiring old tasks; "
+                    + "tag(s) over capacity, expiring old tasks; "
                     + tasksToConsiderDeleting.size() + " tasks under consideration; categories are: "
-                    + taskTagsInCategoryOverCapacity + "; including "
-                    + tasksToConsiderDeleting.stream().limit(5).collect(Collectors.toList()) + " ... "
-                    + tasksToConsiderDeletingNewest.stream().limit(5).collect(Collectors.toList()) );
+                    + taskTagsInCategoryOverCapacity + "; including " + tasksToConsiderDeleting);
         }
 
         // now try deleting tasks which are overcapacity for each (non-entity) tag
diff --git a/core/src/main/java/org/apache/brooklyn/util/core/task/BasicExecutionManager.java b/core/src/main/java/org/apache/brooklyn/util/core/task/BasicExecutionManager.java
index 793c5ca..b7e5adf 100644
--- a/core/src/main/java/org/apache/brooklyn/util/core/task/BasicExecutionManager.java
+++ b/core/src/main/java/org/apache/brooklyn/util/core/task/BasicExecutionManager.java
@@ -206,10 +206,12 @@ public class BasicExecutionManager implements ExecutionManager {
             if (entityMdc != null) {
                 entityMdc.close();
                 if (prevEntityMdc!=null) MDC.put(LOGGING_MDC_KEY_ENTITY_IDS, prevEntityMdc);
+                prevEntityMdc = null;
             }
             if (taskMdc != null) {
                 taskMdc.close();
                 if (prevTaskMdc!=null) MDC.put(LOGGING_MDC_KEY_TASK_ID, prevTaskMdc);
+                prevTaskMdc = null;
             }
         }
 
@@ -967,7 +969,9 @@ public class BasicExecutionManager implements ExecutionManager {
     }
 
     /** normally (if not interrupted) called once for each call to {@link #beforeSubmitScheduledTaskAllIterations(Map, Task)} */
-    protected void beforeStartScheduledTaskAllIterations(Map<?,?> flags, Task<?> taskDoingTheInitialSchedule) {
+    protected void beforeStartScheduledTaskAllIterations(Map<?,?> flags, ScheduledTask taskDoingTheInitialSchedule) {
+        taskDoingTheInitialSchedule.mdc = BrooklynTaskLoggingMdc.create(taskDoingTheInitialSchedule).start();
+
         internalBeforeStart(flags, taskDoingTheInitialSchedule, !SCHEDULED_TASKS_COUNT_AS_ACTIVE, true, true);
     }
     protected void beforeStartScheduledTaskSubmissionIteration(Map<?,?> flags, Task<?> taskDoingTheScheduling, Task<?> taskIteration) {
@@ -1049,12 +1053,16 @@ public class BasicExecutionManager implements ExecutionManager {
     }
     private static boolean loggedClosureDeprecatedInInvokeCallback;
     
-    /** normally (if not interrupted) called once for each call to {@link #beforeStartScheduledTaskAllIterations(Map, Task)}  */
-    protected void afterEndScheduledTaskAllIterations(Map<?,?> flags, Task<?> taskDoingTheInitialSchedule, Throwable error) {
+    /** normally (if not interrupted) called once for each call to {@link #beforeStartScheduledTaskAllIterations(Map, ScheduledTask)}  */
+    protected void afterEndScheduledTaskAllIterations(Map<?,?> flags, ScheduledTask taskDoingTheInitialSchedule, Throwable error) {
         boolean taskWasSubmittedAndNotYetEnded = true;
         try {
             taskWasSubmittedAndNotYetEnded = internalAfterEnd(flags, taskDoingTheInitialSchedule, !SCHEDULED_TASKS_COUNT_AS_ACTIVE, false, error);
         } finally {
+            if (taskDoingTheInitialSchedule.mdc!=null) {
+                taskDoingTheInitialSchedule.mdc.close();
+                taskDoingTheInitialSchedule.mdc = null;
+            }
             synchronized (taskDoingTheInitialSchedule) { taskDoingTheInitialSchedule.notifyAll(); }
             if (taskWasSubmittedAndNotYetEnded) {
                 // prevent from running twice on cancellation after start
diff --git a/core/src/main/java/org/apache/brooklyn/util/core/task/ScheduledTask.java b/core/src/main/java/org/apache/brooklyn/util/core/task/ScheduledTask.java
index 8851971..4422209 100644
--- a/core/src/main/java/org/apache/brooklyn/util/core/task/ScheduledTask.java
+++ b/core/src/main/java/org/apache/brooklyn/util/core/task/ScheduledTask.java
@@ -34,6 +34,7 @@ import org.apache.brooklyn.api.mgmt.Task;
 import org.apache.brooklyn.core.mgmt.BrooklynTaskTags;
 import org.apache.brooklyn.util.collections.MutableList;
 import org.apache.brooklyn.util.collections.MutableMap;
+import org.apache.brooklyn.util.core.task.BasicExecutionManager.BrooklynTaskLoggingMdc;
 import org.apache.brooklyn.util.exceptions.Exceptions;
 import org.apache.brooklyn.util.time.Duration;
 
@@ -52,6 +53,8 @@ public class ScheduledTask extends BasicTask<Object> {
     
     final Callable<Task<?>> taskFactory;
 
+    protected BrooklynTaskLoggingMdc mdc;
+
     /**
      * Initial delay before running, set as flag in constructor; defaults to 0
      */