You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@brooklyn.apache.org by tb...@apache.org on 2018/02/15 10:49:29 UTC

[10/13] brooklyn-server git commit: Add Task ID to MDC

Add Task ID to MDC


Project: http://git-wip-us.apache.org/repos/asf/brooklyn-server/repo
Commit: http://git-wip-us.apache.org/repos/asf/brooklyn-server/commit/2d13332f
Tree: http://git-wip-us.apache.org/repos/asf/brooklyn-server/tree/2d13332f
Diff: http://git-wip-us.apache.org/repos/asf/brooklyn-server/diff/2d13332f

Branch: refs/heads/master
Commit: 2d13332f30a739a99737e1adb83773f53678220a
Parents: 8387563
Author: Geoff Macartney <ge...@cloudsoftcorp.com>
Authored: Fri Feb 2 17:46:41 2018 +0000
Committer: Geoff Macartney <ge...@cloudsoftcorp.com>
Committed: Fri Feb 9 21:09:20 2018 +0000

----------------------------------------------------------------------
 .../util/core/task/BasicExecutionContext.java   | 25 +++++++++---------
 .../core/entity/ApplicationLoggingTest.java     | 27 +++++++++++++++-----
 .../brooklyn/logback-appender-stdout.xml        |  2 +-
 3 files changed, 34 insertions(+), 20 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/2d13332f/core/src/main/java/org/apache/brooklyn/util/core/task/BasicExecutionContext.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/brooklyn/util/core/task/BasicExecutionContext.java b/core/src/main/java/org/apache/brooklyn/util/core/task/BasicExecutionContext.java
index 2575464..c32485a 100644
--- a/core/src/main/java/org/apache/brooklyn/util/core/task/BasicExecutionContext.java
+++ b/core/src/main/java/org/apache/brooklyn/util/core/task/BasicExecutionContext.java
@@ -75,6 +75,7 @@ public class BasicExecutionContext extends AbstractExecutionContext {
     
     static final ThreadLocal<BasicExecutionContext> perThreadExecutionContext = new ThreadLocal<BasicExecutionContext>();
     public static final String ENTITY_IDS = "entity.ids";
+    public static final String TASK_ID = "task.id";
 
     public static BasicExecutionContext getCurrentExecutionContext() { return perThreadExecutionContext.get(); }
 
@@ -229,13 +230,15 @@ public class BasicExecutionContext extends AbstractExecutionContext {
         Throwable error = null;
         final Set<Object> taskTags = task.getTags();
         Entity target = BrooklynTaskTags.getWrappedEntityOfType(taskTags, BrooklynTaskTags.TARGET_ENTITY);
-        final AtomicReference<MDC.MDCCloseable> loggingContext = new AtomicReference<>();
+        final AtomicReference<MDC.MDCCloseable> entityMDC = new AtomicReference<>();
+        final AtomicReference<MDC.MDCCloseable> taskMDC = new AtomicReference<>();
 
         try {
             ((BasicExecutionManager)executionManager).afterSubmitRecordFuture(task, future);
             ((BasicExecutionManager)executionManager).beforeStartInSameThreadTask(null, task);
             if (target != null) {
-                loggingContext.set(MDC.putCloseable(ENTITY_IDS, idStack(target).toString()));
+                entityMDC.set(MDC.putCloseable(ENTITY_IDS, idStack(target).toString()));
+                taskMDC.set(MDC.putCloseable(TASK_ID, task.getId()));
             }
             return future.set(job.call());
             
@@ -250,10 +253,8 @@ public class BasicExecutionContext extends AbstractExecutionContext {
             try {
                 ((BasicExecutionManager)executionManager).afterEndInSameThreadTask(null, task, error);
             } finally {
-                final MDC.MDCCloseable context = loggingContext.get();
-                if (context != null) {
-                    context.close();
-                }
+                if (entityMDC.get() != null) entityMDC.get().close();
+                if (taskMDC.get() != null) taskMDC.get().close();
                 BasicExecutionManager.getPerThreadCurrentTask().set(previousTask);
                 perThreadExecutionContext.set(oldExecutionContext);
             }
@@ -376,7 +377,8 @@ public class BasicExecutionContext extends AbstractExecutionContext {
         }
 
         Entity target = BrooklynTaskTags.getWrappedEntityOfType(taskTags, BrooklynTaskTags.TARGET_ENTITY);
-        final AtomicReference<MDC.MDCCloseable> loggingContext = new AtomicReference<>();
+        final AtomicReference<MDC.MDCCloseable> entityMDC = new AtomicReference<>();
+        final AtomicReference<MDC.MDCCloseable> taskMDC = new AtomicReference<>();
 
         final Object startCallback = properties.get("newTaskStartCallback");
         properties.put("newTaskStartCallback", new Function<Task<?>,Void>() {
@@ -384,7 +386,8 @@ public class BasicExecutionContext extends AbstractExecutionContext {
             public Void apply(Task<?> it) {
                 registerPerThreadExecutionContext();
                 if (target != null) {
-                    loggingContext.set(MDC.putCloseable(ENTITY_IDS, idStack(target).toString()));
+                    entityMDC.set(MDC.putCloseable(ENTITY_IDS, idStack(target).toString()));
+                    taskMDC.set(MDC.putCloseable(TASK_ID, it.getId()));
                 }
                 if (startCallback!=null) BasicExecutionManager.invokeCallback(startCallback, it);
                 return null;
@@ -398,10 +401,8 @@ public class BasicExecutionContext extends AbstractExecutionContext {
                     if (endCallback!=null) BasicExecutionManager.invokeCallback(endCallback, it);
                 } finally {
                     clearPerThreadExecutionContext();
-                    final MDC.MDCCloseable context = loggingContext.get();
-                    if (context != null) {
-                        context.close();
-                    }
+                    if (entityMDC.get() != null) entityMDC.get().close();
+                    if (taskMDC.get() != null) taskMDC.get().close();
                 }
                 return null;
             }});

http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/2d13332f/core/src/test/java/org/apache/brooklyn/core/entity/ApplicationLoggingTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/apache/brooklyn/core/entity/ApplicationLoggingTest.java b/core/src/test/java/org/apache/brooklyn/core/entity/ApplicationLoggingTest.java
index 4c075aa..830358f 100644
--- a/core/src/test/java/org/apache/brooklyn/core/entity/ApplicationLoggingTest.java
+++ b/core/src/test/java/org/apache/brooklyn/core/entity/ApplicationLoggingTest.java
@@ -24,13 +24,19 @@ import static org.apache.brooklyn.test.LogWatcher.EventPredicates.matchingRegexe
 import java.util.ArrayDeque;
 import java.util.Collection;
 import java.util.Deque;
+import java.util.List;
 
 import org.apache.brooklyn.api.entity.Entity;
 import org.apache.brooklyn.api.entity.EntitySpec;
 import org.apache.brooklyn.api.entity.ImplementedBy;
 import org.apache.brooklyn.api.location.Location;
+import org.apache.brooklyn.api.mgmt.ExecutionContext;
+import org.apache.brooklyn.api.mgmt.Task;
+import org.apache.brooklyn.api.mgmt.TaskAdaptable;
+import org.apache.brooklyn.core.effector.Effectors;
 import org.apache.brooklyn.core.entity.lifecycle.Lifecycle;
 import org.apache.brooklyn.core.entity.lifecycle.ServiceStateLogic;
+import org.apache.brooklyn.core.entity.trait.Startable;
 import org.apache.brooklyn.core.entity.trait.StartableMethods;
 import org.apache.brooklyn.core.test.BrooklynAppUnitTestSupport;
 import org.apache.brooklyn.core.test.entity.TestApplication;
@@ -39,6 +45,7 @@ import org.apache.brooklyn.core.test.entity.TestEntity;
 import org.apache.brooklyn.core.test.entity.TestEntityImpl;
 import org.apache.brooklyn.test.LogWatcher;
 import org.apache.brooklyn.util.collections.QuorumCheck;
+import org.apache.brooklyn.util.core.task.DynamicTasks;
 import org.apache.brooklyn.util.exceptions.Exceptions;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -46,6 +53,7 @@ import org.testng.annotations.Test;
 
 import com.google.common.base.Predicates;
 import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableMap;
 
 import ch.qos.logback.classic.Level;
 
@@ -150,16 +158,21 @@ public class ApplicationLoggingTest extends BrooklynAppUnitTestSupport {
         try {
             app.start(ImmutableList.of(app.newSimulatedLocation()));
             assertHealthEventually(app, Lifecycle.RUNNING, true);
-            app.stop();
+            final TaskAdaptable<Void> stopTask = Effectors.invocation(app, Startable.STOP, ImmutableMap.of());
+            final String stopId = stopTask.asTask().getId();
+            LOG.info("Stop task id is {}", stopId);
+            final ExecutionContext executionContext = mgmt.getExecutionContext(app);
+            executionContext.submit(stopTask);
             assertHealthEventually(app, Lifecycle.STOPPED, false);
 
             // Look for output like
-            // 2018-01-27 16:25:52,386 INFO  [dwym20xr82, b1babwc34d, se1d2nn62j]     Hello from entity se1d2nn62j
-            // 2018-01-27 16:25:52,388 INFO  [dwym20xr82, b1babwc34d]   Hello from entity b1babwc34d
-            // 2018-01-27 16:25:52,389 INFO  [dwym20xr82] Hello world
-            // 2018-01-27 16:25:52,399 INFO  [dwym20xr82] Goodbye cruel world
-            // 2018-01-27 16:25:52,400 INFO  [dwym20xr82, b1babwc34d]   Goodbye from entity b1babwc34d
-            // 2018-01-27 16:25:52,401 INFO  [dwym20xr82, b1babwc34d, se1d2nn62j]     Goodbye from entity se1d2nn62j
+//          2018-02-02 17:26:20,481 INFO  LxwcJM6p@[i13ysr26ou, bb0ptkb284, xl760xm9dc]     Hello from entity xl760xm9dc
+//          2018-02-02 17:26:20,484 INFO  Wv3r7mmH@[i13ysr26ou, bb0ptkb284]   Hello from entity bb0ptkb284
+//          2018-02-02 17:26:20,485 INFO  P3omDOqq@[i13ysr26ou] Hello world
+//          2018-02-02 17:26:20,497 INFO  KKKPujW4@[i13ysr26ou] Goodbye cruel world
+//          2018-02-02 17:26:20,498 INFO  NR5pk9h4@[i13ysr26ou, bb0ptkb284]   Goodbye from entity bb0ptkb284
+//          2018-02-02 17:26:20,499 INFO  KHMVOAp4@[i13ysr26ou, bb0ptkb284, xl760xm9dc]     Goodbye from entity xl760xm9dc
+            watcher.assertHasEvent(containsMessage(stopId + "@"));
             watcher.assertHasEvent(matchingRegexes(".*" + app.getApplicationId() + ".*Hello world.*"));;
             watcher.assertHasEvent(matchingRegexes(".*" +
                 ImmutableList.of(app.getId(), entity.getId()).toString()

http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/2d13332f/logging/logback-includes/src/main/resources/brooklyn/logback-appender-stdout.xml
----------------------------------------------------------------------
diff --git a/logging/logback-includes/src/main/resources/brooklyn/logback-appender-stdout.xml b/logging/logback-includes/src/main/resources/brooklyn/logback-appender-stdout.xml
index e612042..08b7f9f 100644
--- a/logging/logback-includes/src/main/resources/brooklyn/logback-appender-stdout.xml
+++ b/logging/logback-includes/src/main/resources/brooklyn/logback-appender-stdout.xml
@@ -21,7 +21,7 @@
 
   <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
     <encoder>
-      <pattern>%d %-5level %X{entity.ids} %msg%n%xEx{0}</pattern>
+      <pattern>%d %-5level %X{task.id}@%X{entity.ids} %msg%n%xEx{0}</pattern>
     </encoder>
     <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
         <level>INFO</level>