merlimat opened a new pull request, #4928:
URL: https://github.com/apache/bookkeeper/pull/4928

   ### Problem
   
   `SingleThreadExecutor.awaitTermination()` returns the opposite of what it 
should, and waits forever for a timeout
   under 1 ms:
   
   ```java
   // SingleThreadExecutor, master
   public boolean awaitTermination(long timeout, TimeUnit unit) throws 
InterruptedException {
       runner.join(unit.toMillis(timeout));   // 0 when timeout < 1 ms: waits 
until the thread ends
       return runner.isAlive();               // true while still running
   }
   ```
   
   Every `OrderedExecutor` is made of `SingleThreadExecutor`s, so:
   
   - `OrderedExecutor.awaitTermination()` returns `false` as soon as the first 
thread has terminated, without waiting for
     the others. `BookKeeper.close()` logs `The mainWorkerPool did not shutdown 
cleanly` precisely when it did, and goes
     on while the other worker threads may still be running tasks.
   - `OrderedExecutor.forceShutdown()`, which `BookieRequestProcessor` uses for 
the bookie read, write and high priority
     thread pools, calls `shutdownNow()` on the threads that terminated and 
leaves alone the ones still running after the
     timeout.
   
   ### Example
   
   ```java
   // OrderedExecutor.awaitTermination(), master
   for (int i = 0; i < threads.length; i++) {
       ret = ret && threads[i].awaitTermination(timeout, unit);   // false once 
thread 0 has terminated
   }
   ```
   
   ```java
   // OrderedExecutor.forceShutdown(), master
   if (!threads[i].awaitTermination(timeout, unit)) {   // false for a 
terminated thread
       threads[i].shutdownNow();
   }
   ```
   
   ### Change
   
   ```java
   public boolean awaitTermination(long timeout, TimeUnit unit) throws 
InterruptedException {
       // Unlike join(unit.toMillis(timeout)), doesn't wait forever for a 
timeout under 1 ms
       unit.timedJoin(runner, timeout);
       return !runner.isAlive();
   }
   ```
   
   `TimeUnit.timedJoin()` doesn't wait for a timeout of 0 or less and rounds a 
sub-millisecond one up to 1 ms.
   
   ### Testing
   
   - New `TestSingleThreadExecutor.testAwaitTermination`: after `shutdown()`, 
while a task still runs,
     `awaitTermination(100 ms)` must return `false`, then `true` once the task 
ends. Fails on master
     (`Terminated while running a task`).
   - New 
`TestSingleThreadExecutor.testAwaitTerminationWithTimeoutUnderOneMillisecond`: 
while a task runs,
     `awaitTermination(0, MILLISECONDS)` returns `false` without waiting and 
`awaitTermination(500, MICROSECONDS)` after
     1 ms. On master the first call waits until the 10 s JUnit timeout.
   - All 164 tests of `bookkeeper-common` pass. With this `bookkeeper-common` 
in the same reactor build,
     `BookKeeperTest`, `BookKeeperCloseTest`, `LedgerCloseTest`, 
`BookieClientTest`, `TestPerChannelBookieClient`,
     `TestBookieRequestProcessor`, `ForceReadOnlyBookieTest` and 
`BookieShutdownTest` pass (64 tests), and their logs no
     longer have `The mainWorkerPool did not shutdown cleanly`. With master's 
`bookkeeper-common`, `BookKeeperTest` logs
     it 67 times, `BookKeeperCloseTest` 20 and `LedgerCloseTest` 5.
   - Checkstyle and SpotBugs pass.
   


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