vlsi commented on code in PR #6884:
URL: https://github.com/apache/jmeter/pull/6884#discussion_r4736324365


##########
xdocs/changes.xml:
##########
@@ -150,6 +150,7 @@ Summary
   <ch_section>Bug fixes</ch_section>
   <h3>General</h3>
   <ul>
+    <li><pr>6884</pr><issue>6625</issue>Do not report a Transaction Controller 
sample when the thread stops at a timer before any sampler in the transaction 
runs, for example because the delay would end after the scheduled end of the 
test. Such samples took close to 0 ms and distorted the minimum response 
time.</li>

Review Comment:
   **F4 minor** The changes.xml entry limits the fix to a stop at a timer, but 
the code also drops the transaction when the thread is stopped from outside 
(Stop or Shutdown) while it waits in a timer.
   
   Proposed solution: Needs F1. Once F1 decides from the thread's and the 
transaction's state, word the entry text for that condition: "Do not report a 
Transaction Controller sample when the thread stops before any sampler in the 
transaction runs, for example at a timer whose delay would end after the 
scheduled end of the test. Such samples took close to 0 ms and distorted the 
minimum response time." If F1 is deferred, this finding is deferred with it.



##########
src/components/src/test/java/org/apache/jmeter/control/TestTransactionController.java:
##########
@@ -81,4 +90,82 @@ public void testIssue57958() throws Exception {
         assertEquals("Number of samples in transaction : 1, number of failing 
samples : 1",
                 listener.getEvents().get(0).getResult().getResponseMessage());
     }
+
+    /** Stops the thread before the sampler: overruns the schedule, or throws 
as Precise Throughput Timer does. */
+    private static class StoppingTimer extends AbstractTestElement implements 
Timer {
+        private static final long serialVersionUID = 1L;
+        private final boolean throwing;
+
+        StoppingTimer(boolean throwing) {
+            this.throwing = throwing;
+        }
+
+        @Override
+        public long delay() {
+            if (throwing) {
+                throw new JMeterStopThreadException();
+            }
+            return 60_000;
+        }
+    }
+
+    @ParameterizedTest
+    @CsvSource({"true,true", "true,false", "false,true", "false,false"})
+    void transactionWithoutExecutedSamplersIsNotReported(boolean parent, 
boolean throwing) {
+        assertEquals(List.of(), run(parent, tx -> addSkippedSampler(tx, 
throwing)));
+        assertEquals(List.of(),
+                run(parent, tx -> addSkippedSampler(addTransaction(tx, 
"inner", parent), throwing)));
+    }
+
+    @ParameterizedTest
+    @CsvSource({"true,true", "true,false", "false,true", "false,false"})
+    void transactionWithSomeExecutedSamplersIsReported(boolean parent, boolean 
throwing) {
+        List<String> labels = run(parent, tx -> {
+            tx.add(sampler("executed"));
+            addSkippedSampler(tx, throwing);
+        });

Review Comment:
   **F2 major** The description says a transaction in which a sampler ran is 
still reported, but in non-parent mode it is lost when another child follows 
the sampler skipped at the stop.
   
   Problem: Every new case makes the skipped sampler the last child, so the 
tests show the claim only where it holds. After the stop, JMeterThread.run 
calls threadGroupLoopController.next() once more; with a child after the 
skipped sampler that call returns the child instead of reaching the end of the 
controller, the loop exits, and the transaction sample is never sent.
   
   ```java
   List<String> labels = run(false /* non-parent */, tx -> {
       tx.add(sampler("executed"));
       addSkippedSampler(tx, true); // same with false
       tx.add(sampler("after"));
   });
   // current:  [executed]
   // expected per the description: [executed, transaction]
   ```
   
   Proposed solution: Ending a non-parent transaction where the thread stops, 
the way processSampler ends a parent-mode one, needs a design decision beyond 
this change. Narrow the description to what the change keeps, for example: "If 
at least one sampler ran, the transaction is reported as before; in non-parent 
mode it is still lost when the thread stops before the last child, as it was 
before this change." File an issue for that loss, or add it to the follow-up 
the review suggested for transactions cut short by a stop.



##########
src/core/src/main/java/org/apache/jmeter/threads/JMeterThread.java:
##########
@@ -819,6 +834,14 @@ public String getThreadName() {
         return threadName;
     }
 
+    /**
+     * @return {@code true} if the thread was told to stop before it could 
execute a sampler it had prepared
+     */
+    @API(status = API.Status.INTERNAL, since = "6.0.0")
+    public boolean isSamplerSkippedOnStop() {

Review Comment:
   **F6 minor** The Javadoc of isSamplerSkippedOnStop() says the thread "was 
told to stop", which misses the thread stopping itself at the scheduled end, 
and does not say the value is never reset.
   
   Problem: TransactionController relies on the value staying true to ignore 
every later empty transaction of the stopping thread.
   
   Proposed solution: If F1 removes the flag, this Javadoc goes with it.
   
   ```suggestion
       /**
        * @return {@code true} if the thread stopped during or right after the 
timers of a sampler, so the sampler
        *         did not run; once {@code true}, the value is never reset
        */
       @API(status = API.Status.INTERNAL, since = "6.0.0")
       public boolean isSamplerSkippedOnStop() {
   ```



##########
src/components/src/test/java/org/apache/jmeter/control/TestTransactionController.java:
##########
@@ -81,4 +90,82 @@ public void testIssue57958() throws Exception {
         assertEquals("Number of samples in transaction : 1, number of failing 
samples : 1",
                 listener.getEvents().get(0).getResult().getResponseMessage());
     }
+
+    /** Stops the thread before the sampler: overruns the schedule, or throws 
as Precise Throughput Timer does. */
+    private static class StoppingTimer extends AbstractTestElement implements 
Timer {
+        private static final long serialVersionUID = 1L;
+        private final boolean throwing;
+
+        StoppingTimer(boolean throwing) {
+            this.throwing = throwing;
+        }
+
+        @Override
+        public long delay() {
+            if (throwing) {
+                throw new JMeterStopThreadException();
+            }
+            return 60_000;
+        }
+    }
+
+    @ParameterizedTest
+    @CsvSource({"true,true", "true,false", "false,true", "false,false"})
+    void transactionWithoutExecutedSamplersIsNotReported(boolean parent, 
boolean throwing) {

Review Comment:
   **F11 nit** The parameterized cases are reported as `[1] "true", "true"` to 
`[4] "false", "false"`, so a red build does not say which mode or which way of 
stopping failed.
   
   Proposed solution: apache/jmeter#6889 compiles the tests with -parameters, 
and JUnit's default name then becomes `[1] parent = true, throwing = true` with 
no change here. Until it merges, `@ParameterizedTest(name = "[{index}] parent = 
{0}, throwing = {1}")` on both tests gives the same names.



##########
src/components/src/test/java/org/apache/jmeter/control/TestTransactionController.java:
##########
@@ -81,4 +90,82 @@ public void testIssue57958() throws Exception {
         assertEquals("Number of samples in transaction : 1, number of failing 
samples : 1",
                 listener.getEvents().get(0).getResult().getResponseMessage());
     }
+
+    /** Stops the thread before the sampler: overruns the schedule, or throws 
as Precise Throughput Timer does. */
+    private static class StoppingTimer extends AbstractTestElement implements 
Timer {
+        private static final long serialVersionUID = 1L;
+        private final boolean throwing;
+
+        StoppingTimer(boolean throwing) {
+            this.throwing = throwing;
+        }
+
+        @Override
+        public long delay() {
+            if (throwing) {
+                throw new JMeterStopThreadException();
+            }
+            return 60_000;
+        }
+    }
+
+    @ParameterizedTest
+    @CsvSource({"true,true", "true,false", "false,true", "false,false"})
+    void transactionWithoutExecutedSamplersIsNotReported(boolean parent, 
boolean throwing) {
+        assertEquals(List.of(), run(parent, tx -> addSkippedSampler(tx, 
throwing)));
+        assertEquals(List.of(),
+                run(parent, tx -> addSkippedSampler(addTransaction(tx, 
"inner", parent), throwing)));
+    }
+
+    @ParameterizedTest
+    @CsvSource({"true,true", "true,false", "false,true", "false,false"})
+    void transactionWithSomeExecutedSamplersIsReported(boolean parent, boolean 
throwing) {
+        List<String> labels = run(parent, tx -> {
+            tx.add(sampler("executed"));
+            addSkippedSampler(tx, throwing);
+        });
+
+        assertEquals(parent ? List.of("transaction") : List.of("executed", 
"transaction"), labels);
+    }
+
+    /** Runs one transaction on a scheduled thread and returns the labels its 
listener received. */
+    private static List<String> run(boolean parent, Consumer<HashTree> 
children) {
+        LoopController loop = new LoopController();
+        loop.setLoops(1);

Review Comment:
   **F5 minor** No test runs more than one iteration, so deleting the reset 
`hasChildSamples = false;` at the start of a transaction leaves every new test 
green.
   
   Problem: The issue's scenario is a scheduled test that runs many iterations 
and stops in the last one. In non-parent mode, without the reset, a transaction 
that ran a sampler in an earlier iteration reports its empty last iteration 
again, which is the 0 ms sample the change removes.
   
   ```java
   // Loop count 2; transaction(parent=false) -> sampler "s" -> timer that 
returns 0 on its first call
   // and 60_000 (past the 10 s end time) on its second.
   // expected: [s, transaction]
   // with TransactionController.java:205 `hasChildSamples = false;` removed: 
[s, transaction, transaction],
   // while all 8 cases of the PR still pass.
   ```
   
   Proposed solution: Add a case in both modes where the transaction runs its 
sampler in the first iteration and the thread stops at the timer in the second, 
asserting that only the first iteration's transaction is reported (loop count 
as a parameter of the run helper, a timer that stops on its second call).



##########
xdocs/changes.xml:
##########
@@ -150,6 +150,7 @@ Summary
   <ch_section>Bug fixes</ch_section>
   <h3>General</h3>
   <ul>
+    <li><pr>6884</pr><issue>6625</issue>Do not report a Transaction Controller 
sample when the thread stops at a timer before any sampler in the transaction 
runs, for example because the delay would end after the scheduled end of the 
test. Such samples took close to 0 ms and distorted the minimum response 
time.</li>

Review Comment:
   **F3 minor** The changes.xml entry cites issue 6625 as fixed, although the 
description says the cause the reporter hit is unconfirmed and the issue's 
reproduction has no timer.
   
   Problem: Readers of the release notes take an <issue> tag under Bug fixes as 
"this issue is fixed".
   
   Proposed solution: Drop `<issue>6625</issue>` from the entry, or keep it 
only once the reporter confirms the fix.



##########
src/core/src/main/java/org/apache/jmeter/threads/JMeterThread.java:
##########
@@ -557,11 +559,18 @@ private void executeSamplePackage(Sampler current,
         // Hack: save the package for any transaction controllers
         threadVars.putObject(PACKAGE_OBJECT, pack);
 
-        delay(pack.getTimers());
+        try {
+            delay(pack.getTimers());
+        } catch (JMeterStopThreadException e) {
+            samplerSkippedOnStop = true;
+            throw e;
+        }
         SampleResult result = null;
         if (running) {
             Sampler sampler = pack.getSampler();
             result = doSampling(threadContext, sampler);
+        } else {
+            samplerSkippedOnStop = true;
         }

Review Comment:
   **F1 major** An empty transaction is still reported when a timer stops the 
thread with JMeterStopTestException or JMeterStopTestNowException, or a 
pre-processor stops it, because samplerSkippedOnStop is set only for a 
JMeterStopThreadException out of delay() and for running == false right after 
it.
   
   Problem: The changelog promises no sample "when the thread stops at a timer 
before any sampler in the transaction runs"; a timer that stops the test is 
such a stop, yet the near-0 ms transaction sample the change sets out to remove 
is still written, in parent and non-parent mode. JSR223 Timer and JSR223 
PreProcessor scripts can throw all three stop exceptions, and __StringFromFile 
throws JMeterStopThreadException from a pre-processor at the end of its file.
   
   <details><summary>Reproducer</summary>
   
   ```java
   // In TestTransactionController, using the PR's run(...) and sampler(...)
   static class StopTestTimer extends AbstractTestElement implements Timer {
       @Override public long delay() { throw new JMeterStopTestException(); }
   }
   static class StopThreadPreProcessor extends AbstractTestElement implements 
PreProcessor {
       @Override public void process() { throw new 
JMeterStopThreadException("End of sequence"); }
   }
   
   @ParameterizedTest
   @CsvSource({"true", "false"})
   void stopTestTimer(boolean parent) {
       // current: [transaction] for parent=true and parent=false; expected: []
       assertEquals(List.of(), run(parent, tx -> 
tx.add(sampler("skipped")).add(new StopTestTimer())));
   }
   
   @Test
   void stopThreadPreProcessorParentMode() {
       // current: [transaction]; expected: []
       assertEquals(List.of(), run(true, tx -> 
tx.add(sampler("skipped")).add(new StopThreadPreProcessor())));
   }
   ```
   
   </details>
   
   Proposed solution: The decision that goes wrong is "did the thread stop 
before anything in the transaction ran": the change lists two places a stop can 
happen and sets a sticky flag at each. Decide it where the transaction ends, 
from state every stop leaves behind: in doEndTransactionSampler, skip the 
sample when `!running` and the transaction has no sub-results; in 
TransactionController.nextWithoutTransactionSampler, skip it when 
hasChildSamples is false and the thread is no longer running, through an 
`@API(status = INTERNAL)` running-state accessor on JMeterThread in place of 
isSamplerSkippedOnStop(). The samplerSkippedOnStop field, the try/catch around 
delay(), and the `else` branch then go. This also skips an empty transaction 
when the thread is stopped from outside (Stop, Shutdown) or by a Flow Control 
Action before any child produced a sample; such a transaction measures nothing 
either, and F4 asks the changelog to say so. Catching JMeterStopTestException 
and JMeterStopTestN
 owException around delay() as well is only a stopgap: it still misses the 
pre-processor path.



##########
src/core/src/main/java/org/apache/jmeter/threads/JMeterThread.java:
##########
@@ -138,6 +138,8 @@ public class JMeterThread implements Runnable, 
Interruptible {
      */
     private volatile boolean running; // may be set from a different thread
 
+    private boolean samplerSkippedOnStop;

Review Comment:
   **F7 minor** samplerSkippedOnStop is declared under the comment "The 
following variables may be set/read from multiple threads", but it is a plain 
field that only the thread itself writes and reads; move it above that comment.
   
   Problem: A maintainer reading the block comment concludes that the field is 
shared and is missing `volatile`, or that the comment is wrong.
   
   Proposed solution: If F1 removes the flag, this finding goes with it.



##########
src/components/src/test/java/org/apache/jmeter/control/TestTransactionController.java:
##########
@@ -81,4 +90,82 @@ public void testIssue57958() throws Exception {
         assertEquals("Number of samples in transaction : 1, number of failing 
samples : 1",
                 listener.getEvents().get(0).getResult().getResponseMessage());
     }
+
+    /** Stops the thread before the sampler: overruns the schedule, or throws 
as Precise Throughput Timer does. */
+    private static class StoppingTimer extends AbstractTestElement implements 
Timer {
+        private static final long serialVersionUID = 1L;
+        private final boolean throwing;
+
+        StoppingTimer(boolean throwing) {
+            this.throwing = throwing;
+        }
+
+        @Override
+        public long delay() {
+            if (throwing) {
+                throw new JMeterStopThreadException();
+            }
+            return 60_000;
+        }
+    }
+
+    @ParameterizedTest
+    @CsvSource({"true,true", "true,false", "false,true", "false,false"})
+    void transactionWithoutExecutedSamplersIsNotReported(boolean parent, 
boolean throwing) {
+        assertEquals(List.of(), run(parent, tx -> addSkippedSampler(tx, 
throwing)));
+        assertEquals(List.of(),
+                run(parent, tx -> addSkippedSampler(addTransaction(tx, 
"inner", parent), throwing)));

Review Comment:
   **F10 nit** transactionWithoutExecutedSamplersIsNotReported checks the flat 
and the nested transaction in one test, so a failure of the flat one hides the 
nested one, and the name does not mention the nested case.
   
   Proposed solution: Make the nested transaction a test of its own, or a 
parameter of this one, whose name says the stop happens inside an inner 
transaction.



-- 
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