mattcasters commented on code in PR #9481:
URL: https://github.com/apache/hop/pull/9481#discussion_r4583023754


##########
engine/src/main/java/org/apache/hop/execution/lifecycle/ExecutionLifecycle.java:
##########
@@ -0,0 +1,343 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *       http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.hop.execution.lifecycle;
+
+import java.lang.ref.ReferenceQueue;
+import java.lang.ref.WeakReference;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.IdentityHashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Objects;
+import java.util.concurrent.locks.ReentrantLock;
+
+/**
+ * Optional, process-wide execution observation. Subjects are always compared 
by identity. A begin
+ * does not activate: callers explicitly activate on each executing thread and 
finish exactly once
+ * when execution ends. Activation scopes must close on the opening thread in 
LIFO order, including
+ * across nested executions; they restore the previous current handle even if 
either handle
+ * finished.
+ *
+ * <p>Start/finish callbacks are serialized under a reentrant lock to preserve 
ordering. Observers
+ * must not block waiting for another thread's lifecycle operations. 
Activation and scope closure
+ * run outside that lock on the calling thread. Registration changes affect 
future executions only:
+ * successfully started observers remain paired with their execution until 
finish. With no
+ * registered observers begin, activate and associate are allocation-free 
no-ops.
+ */
+public final class ExecutionLifecycle {
+  private static final ExecutionScope NOOP = () -> {};
+  private static final ReentrantLock LOCK = new ReentrantLock();
+  private static final List<Registration> OBSERVERS = new ArrayList<>();
+  private static final Map<Object, Active> ACTIVE = new IdentityHashMap<>();
+  private static final ReferenceQueue<Object> PENDING_QUEUE = new 
ReferenceQueue<>();
+  private static final Map<WeakIdentityKey, ExecutionHandle> PENDING = new 
HashMap<>();
+  private static final ThreadLocal<ExecutionHandle> CURRENT = new 
ThreadLocal<>();
+  private static volatile boolean enabled;
+  private static volatile int activeCount;
+
+  private ExecutionLifecycle() {}
+
+  /** Registers an observer; closing the returned scope removes only this 
registration. */
+  public static ExecutionScope addObserver(ExecutionObserver observer) {
+    Objects.requireNonNull(observer, "observer");
+    LOCK.lock();
+    try {
+      Registration registration = new Registration(observer);
+      OBSERVERS.add(registration);
+      enabled = true;
+      return registration;
+    } finally {
+      LOCK.unlock();
+    }
+  }
+
+  /**
+   * Starts once per active subject identity, or returns null when observation 
is disabled. Parent
+   * precedence is captured association (even a captured null), explicit 
active parent, then
+   * current. Names never participate in parent resolution. A finished subject 
can begin a new,
+   * distinct run.
+   */
+  public static ExecutionHandle begin(
+      Object subject, Object parentSubject, ExecutionKind kind, String name, 
int copy) {
+    if (!enabled) {
+      return null;
+    }
+    LOCK.lock();
+    try {
+      if (!enabled) {
+        return null;
+      }
+      Objects.requireNonNull(subject, "subject");
+      Objects.requireNonNull(kind, "kind");
+      purgePending();
+      Active existing = ACTIVE.get(subject);
+      if (existing != null) {
+        return existing.handle;
+      }
+      WeakIdentityKey key = new WeakIdentityKey(subject, null);
+      ExecutionHandle parent;
+      if (PENDING.containsKey(key)) {
+        parent = PENDING.remove(key);
+      } else {
+        Active explicit = ACTIVE.get(parentSubject);
+        parent = explicit == null ? CURRENT.get() : explicit.handle;
+      }
+      Active active = new Active(new ExecutionHandle(parent, subject, kind, 
name, copy));
+      ACTIVE.put(subject, active);
+      activeCount = ACTIVE.size();
+      List<Registration> registrations = new ArrayList<>(OBSERVERS);
+      for (Registration registration : registrations) {
+        try {
+          registration.observer.started(active.handle);

Review Comment:
   `started()` here, and `finished()` through `notifyFinished()`, run while 
holding the global `LOCK`. An observer that does I/O or blocks would serialize 
every pipeline, workflow, transform and action start and finish in the JVM (Hop 
Server, executors with many child runs). An observer that waits on another 
thread that calls into `ExecutionLifecycle` would deadlock. Could you collect 
what you need under the lock and call the observers outside it, like 
`activate()` already does?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to