You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by jo...@apache.org on 2021/05/21 13:10:22 UTC

[sling-org-apache-sling-commons-scheduler] 01/01: SLING-10410 add debug logging when registering a job using the default thread pool

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

joerghoh pushed a commit to branch feature/SLING-10410-logging-for-default-threadpool
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-commons-scheduler.git

commit 253df25fb3fb63213df31aa6eb97096b1014bb02
Author: Joerg Hoh <jh...@adobe.com>
AuthorDate: Fri May 21 15:09:16 2021 +0200

    SLING-10410 add debug logging when registering a job using the default thread pool
---
 pom.xml                                            |  6 ++++
 .../scheduler/impl/InternalScheduleOptions.java    |  2 ++
 .../commons/scheduler/impl/QuartzScheduler.java    | 36 ++++++++++++++++++++++
 .../commons/scheduler/impl/WhiteboardHandler.java  |  9 ++++++
 4 files changed, 53 insertions(+)

diff --git a/pom.xml b/pom.xml
index c4a8f44..7c52e2b 100644
--- a/pom.xml
+++ b/pom.xml
@@ -220,5 +220,11 @@
             <version>1.0.10</version>
             <scope>test</scope>
         </dependency>
+        <dependency>
+            <groupId>org.apache.commons</groupId>
+            <artifactId>commons-lang3</artifactId>
+            <version>3.6</version>
+            <scope>provided</scope>
+        </dependency>
     </dependencies>
 </project>
diff --git a/src/main/java/org/apache/sling/commons/scheduler/impl/InternalScheduleOptions.java b/src/main/java/org/apache/sling/commons/scheduler/impl/InternalScheduleOptions.java
index a5e67c9..328f561 100644
--- a/src/main/java/org/apache/sling/commons/scheduler/impl/InternalScheduleOptions.java
+++ b/src/main/java/org/apache/sling/commons/scheduler/impl/InternalScheduleOptions.java
@@ -46,6 +46,8 @@ public class InternalScheduleOptions implements ScheduleOptions {
 
     public String[] runOn;
 
+    public String componenentName;
+
     public InternalScheduleOptions(final TriggerBuilder<? extends Trigger> trigger) {
         this.trigger = trigger;
         this.argumentException = null;
diff --git a/src/main/java/org/apache/sling/commons/scheduler/impl/QuartzScheduler.java b/src/main/java/org/apache/sling/commons/scheduler/impl/QuartzScheduler.java
index 9703a95..4701eca 100644
--- a/src/main/java/org/apache/sling/commons/scheduler/impl/QuartzScheduler.java
+++ b/src/main/java/org/apache/sling/commons/scheduler/impl/QuartzScheduler.java
@@ -25,6 +25,8 @@ import java.util.NoSuchElementException;
 import java.util.Set;
 import java.util.UUID;
 
+import org.apache.commons.lang3.StringUtils;
+import org.apache.commons.lang3.exception.ExceptionUtils;
 import org.apache.sling.commons.scheduler.Job;
 import org.apache.sling.commons.scheduler.ScheduleOptions;
 import org.apache.sling.commons.scheduler.Scheduler;
@@ -106,9 +108,15 @@ public class QuartzScheduler implements BundleListener {
 
     static final String METRICS_NAME_OLDEST_RUNNING_JOB_MILLIS = "commons.scheduler.oldest.running.job.millis";
 
+    // property which contains the component name ( = class names) for service references
+    static final String COMPONENT_NAME = "component.name";
+
     /** Default logger. */
     private final Logger logger = LoggerFactory.getLogger(this.getClass());
 
+    private final Logger defaultThreadPoolLogger = LoggerFactory.getLogger(this.getClass().getName() + ".defaultThreadPool");
+    private static final int STACKTRACE_DEPTH = 10;
+
     @Reference
     private ThreadPoolManager threadPoolManager;
 
@@ -580,6 +588,9 @@ public class QuartzScheduler implements BundleListener {
             // as this method might be called from unbind and during
             // unbind a deactivate could happen, we check the scheduler first
             final String poolName = getThreadPoolName(opts.threadPoolName);
+            if (poolName.equals(this.defaultPoolName) && defaultThreadPoolLogger.isDebugEnabled()) {
+                logUsageOfDefaultThreadPool(opts);
+            }
             SchedulerProxy proxy = null;
             synchronized ( this.schedulers ) {
                 if ( this.active ) {
@@ -625,4 +636,29 @@ public class QuartzScheduler implements BundleListener {
             return new HashMap<>(this.schedulers);
         }
     }
+
+    /**
+     * Log a stacktrace that the default threadpool is used
+     */
+    private void logUsageOfDefaultThreadPool(InternalScheduleOptions options) {
+
+        String location = "";
+        if (!StringUtils.isEmpty(options.componenentName)) {
+             location = "defined by OSGI annotations on component with name " + options.componenentName;
+        } else {
+            try {
+                throw new IllegalArgumentException();
+            } catch (IllegalArgumentException e) {
+                String[] stackFrames = ExceptionUtils.getStackFrames(e);
+                StringBuilder stackExcerpt = new StringBuilder();
+                // skip the first line, it contains the name of the exception
+                for (int i=1;i< STACKTRACE_DEPTH;i++) {
+                    stackExcerpt.append(stackFrames[i]).append("\n");
+                }
+                location = "triggered by this code:\n" + stackExcerpt.toString();
+            }
+        }
+        defaultThreadPoolLogger.debug("Scheduled job using the default threadpool; {}",
+                location);
+    }
 }
diff --git a/src/main/java/org/apache/sling/commons/scheduler/impl/WhiteboardHandler.java b/src/main/java/org/apache/sling/commons/scheduler/impl/WhiteboardHandler.java
index 52c6948..660a66e 100644
--- a/src/main/java/org/apache/sling/commons/scheduler/impl/WhiteboardHandler.java
+++ b/src/main/java/org/apache/sling/commons/scheduler/impl/WhiteboardHandler.java
@@ -21,6 +21,7 @@ import java.util.Date;
 import java.util.Map;
 import java.util.concurrent.ConcurrentHashMap;
 
+import org.apache.commons.lang3.StringUtils;
 import org.apache.sling.commons.scheduler.Job;
 import org.apache.sling.commons.scheduler.ScheduleOptions;
 import org.apache.sling.commons.scheduler.Scheduler;
@@ -265,6 +266,14 @@ public class WhiteboardHandler {
                 .threadPoolName(poolName)
                 .onInstancesOnly(runOnOpts);
         ((InternalScheduleOptions)scheduleOptions).providedName = getStringProperty(ref, Scheduler.PROPERTY_SCHEDULER_NAME);
+        String componentName = getStringProperty(ref, QuartzScheduler.COMPONENT_NAME);
+        if (StringUtils.isEmpty(componentName)) {
+            // if registered is a runnable, this is empty
+            if (job instanceof Runnable) {
+                componentName = "Runnable (implementation class = "+ job.getClass().getName() + ")";
+            }
+        }
+        ((InternalScheduleOptions) scheduleOptions).componenentName = componentName;
 
         final long bundleId = ref.getBundle().getBundleId();
         final Long serviceId = getLongProperty(ref, Constants.SERVICE_ID);