[ 
https://issues.apache.org/jira/browse/RATIS-2781?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
 ]

Andrey Yarovoy updated RATIS-2781:
----------------------------------
    Description: 
*Problem*

The DataStream client's worker group (`NettyClientStreamRpc-workerGroup`) runs 
non-daemon threads. A JVM that has
streamed data doesn't exit when its main thread returns until every 
`NettyClientStreamRpc` is closed, because only
then is the shared worker group released.

Where the threads come from:
 - `NettyUtils.newEventLoopGroup` builds the group with 
`ConcurrentUtils.newThreadFactory(name)`, which creates plain
`new Thread(runnable)` threads without setting the daemon flag.
 - So each thread inherits the flag of the thread that creates it. Netty 
creates event-loop threads lazily, from the
thread that first registers a channel on that loop. For the client, that is the 
application thread that opens the
stream, usually a non-daemon thread such as `main`.
 - With `raft.netty.dataStream.client.worker-group.share=true` (the default), 
the group is shared per JVM through a
`ReferenceCountedObject`. It is shut down only when the last 
`NettyClientStreamRpc` is closed.

Applications can't change any of this: the thread factory, `WorkerGroupGetter` 
and the shared group are private.

The impact is that any client that streams and relies on a shutdown hook to 
close its Ratis clients never exits. The
hook can't run while a non-daemon thread is alive. The case that reported it is 
Apache Ozone (HDDS-17278): every
MapReduce task that writes with data streaming hangs until the AppMaster kills 
it after 60 s. That delays every
extra wave of tasks by 60 s, and in TestDFSIO raised total job time from 15.2 
to 27.8 minutes, although each task
wrote 27% faster. The thread dump of each stuck task shows only one non-daemon 
thread besides `DestroyJavaVM`:

```
"NettyClientStreamRpc-workerGroup--thread1" #69 prio=5 os_prio=0 ... runnable
java.lang.Thread.State: RUNNABLE
at org.apache.ratis.thirdparty.io.netty.channel.epoll.Native.epollWait(Native 
Method)
...
at 
org.apache.ratis.thirdparty.io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:1007)
at java.lang.Thread.run(Thread.java:840)
```

gRPC's client event-loop threads are daemon threads, so the non-streaming path 
of the same application exits
normally.

*Proposed change*
 - Add a daemon option to the thread factory, for example 
`ConcurrentUtils.newThreadFactory(namePrefix, daemon)`, and
pass it through `NettyUtils.newEventLoopGroup`.
 - Create the client-side groups with daemon threads: the DataStream client 
worker group in
`NettyClientStreamRpc.WorkerGroupGetter.newWorkerGroup`, and, by the same 
reasoning, the `NettyRpcProxy` client group.
 - Leave server groups (`NettyServerStreamRpc`, `NettyRpcService`) as they are. 
Servers keep their JVM alive
deliberately.
 - Closing a client keeps working as today: the group is still released and 
shut down gracefully when the last client
is closed. Daemon threads only stop an unclosed client from blocking JVM exit.

*Testing*
 - A unit test that creates a `NettyClientStreamRpc`, connects it from a 
non-daemon thread, and asserts that the
worker-group threads are daemon threads.
 - Optionally, a child-JVM test: stream from `main`, return without closing the 
client, and assert that the JVM exits
within a few seconds.

Seen with Ratis 3.3.1; Ratis master has the same code.

  was:
#### Problem

The DataStream client's worker group (`NettyClientStreamRpc-workerGroup`) runs 
non-daemon threads. A JVM that has
streamed data doesn't exit when its main thread returns until every 
`NettyClientStreamRpc` is closed, because only
then is the shared worker group released.

Where the threads come from:
- `NettyUtils.newEventLoopGroup` builds the group with 
`ConcurrentUtils.newThreadFactory(name)`, which creates plain
`new Thread(runnable)` threads without setting the daemon flag.
- So each thread inherits the flag of the thread that creates it. Netty creates 
event-loop threads lazily, from the
thread that first registers a channel on that loop. For the client, that is the 
application thread that opens the
stream, usually a non-daemon thread such as `main`.
- With `raft.netty.dataStream.client.worker-group.share=true` (the default), 
the group is shared per JVM through a
`ReferenceCountedObject`. It is shut down only when the last 
`NettyClientStreamRpc` is closed.

Applications can't change any of this: the thread factory, `WorkerGroupGetter` 
and the shared group are private.

The impact is that any client that streams and relies on a shutdown hook to 
close its Ratis clients never exits. The
hook can't run while a non-daemon thread is alive. The case that reported it is 
Apache Ozone (HDDS-17278): every
MapReduce task that writes with data streaming hangs until the AppMaster kills 
it after 60 s. That delays every
extra wave of tasks by 60 s, and in TestDFSIO raised total job time from 15.2 
to 27.8 minutes, although each task
wrote 27% faster. The thread dump of each stuck task shows only one non-daemon 
thread besides `DestroyJavaVM`:

```
"NettyClientStreamRpc-workerGroup--thread1" #69 prio=5 os_prio=0 ... runnable
java.lang.Thread.State: RUNNABLE
at org.apache.ratis.thirdparty.io.netty.channel.epoll.Native.epollWait(Native 
Method)
...
at 
org.apache.ratis.thirdparty.io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:1007)
at java.lang.Thread.run(Thread.java:840)
```

gRPC's client event-loop threads are daemon threads, so the non-streaming path 
of the same application exits
normally.

#### Proposed change

- Add a daemon option to the thread factory, for example 
`ConcurrentUtils.newThreadFactory(namePrefix, daemon)`, and
pass it through `NettyUtils.newEventLoopGroup`.
- Create the client-side groups with daemon threads: the DataStream client 
worker group in
`NettyClientStreamRpc.WorkerGroupGetter.newWorkerGroup`, and, by the same 
reasoning, the `NettyRpcProxy` client group.
- Leave server groups (`NettyServerStreamRpc`, `NettyRpcService`) as they are. 
Servers keep their JVM alive
deliberately.
- Closing a client keeps working as today: the group is still released and shut 
down gracefully when the last client
is closed. Daemon threads only stop an unclosed client from blocking JVM exit.

#### Testing

- A unit test that creates a `NettyClientStreamRpc`, connects it from a 
non-daemon thread, and asserts that the
worker-group threads are daemon threads.
- Optionally, a child-JVM test: stream from `main`, return without closing the 
client, and assert that the JVM exits
within a few seconds.

Seen with Ratis 3.3.1; Ratis master has the same code.


> Netty DataStream client threads should be daemon threads
> --------------------------------------------------------
>
>                 Key: RATIS-2781
>                 URL: https://issues.apache.org/jira/browse/RATIS-2781
>             Project: Ratis
>          Issue Type: Bug
>          Components: Netty
>            Reporter: Andrey Yarovoy
>            Assignee: Andrey Yarovoy
>            Priority: Major
>
> *Problem*
> The DataStream client's worker group (`NettyClientStreamRpc-workerGroup`) 
> runs non-daemon threads. A JVM that has
> streamed data doesn't exit when its main thread returns until every 
> `NettyClientStreamRpc` is closed, because only
> then is the shared worker group released.
> Where the threads come from:
>  - `NettyUtils.newEventLoopGroup` builds the group with 
> `ConcurrentUtils.newThreadFactory(name)`, which creates plain
> `new Thread(runnable)` threads without setting the daemon flag.
>  - So each thread inherits the flag of the thread that creates it. Netty 
> creates event-loop threads lazily, from the
> thread that first registers a channel on that loop. For the client, that is 
> the application thread that opens the
> stream, usually a non-daemon thread such as `main`.
>  - With `raft.netty.dataStream.client.worker-group.share=true` (the default), 
> the group is shared per JVM through a
> `ReferenceCountedObject`. It is shut down only when the last 
> `NettyClientStreamRpc` is closed.
> Applications can't change any of this: the thread factory, 
> `WorkerGroupGetter` and the shared group are private.
> The impact is that any client that streams and relies on a shutdown hook to 
> close its Ratis clients never exits. The
> hook can't run while a non-daemon thread is alive. The case that reported it 
> is Apache Ozone (HDDS-17278): every
> MapReduce task that writes with data streaming hangs until the AppMaster 
> kills it after 60 s. That delays every
> extra wave of tasks by 60 s, and in TestDFSIO raised total job time from 15.2 
> to 27.8 minutes, although each task
> wrote 27% faster. The thread dump of each stuck task shows only one 
> non-daemon thread besides `DestroyJavaVM`:
> ```
> "NettyClientStreamRpc-workerGroup--thread1" #69 prio=5 os_prio=0 ... runnable
> java.lang.Thread.State: RUNNABLE
> at org.apache.ratis.thirdparty.io.netty.channel.epoll.Native.epollWait(Native 
> Method)
> ...
> at 
> org.apache.ratis.thirdparty.io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:1007)
> at java.lang.Thread.run(Thread.java:840)
> ```
> gRPC's client event-loop threads are daemon threads, so the non-streaming 
> path of the same application exits
> normally.
> *Proposed change*
>  - Add a daemon option to the thread factory, for example 
> `ConcurrentUtils.newThreadFactory(namePrefix, daemon)`, and
> pass it through `NettyUtils.newEventLoopGroup`.
>  - Create the client-side groups with daemon threads: the DataStream client 
> worker group in
> `NettyClientStreamRpc.WorkerGroupGetter.newWorkerGroup`, and, by the same 
> reasoning, the `NettyRpcProxy` client group.
>  - Leave server groups (`NettyServerStreamRpc`, `NettyRpcService`) as they 
> are. Servers keep their JVM alive
> deliberately.
>  - Closing a client keeps working as today: the group is still released and 
> shut down gracefully when the last client
> is closed. Daemon threads only stop an unclosed client from blocking JVM exit.
> *Testing*
>  - A unit test that creates a `NettyClientStreamRpc`, connects it from a 
> non-daemon thread, and asserts that the
> worker-group threads are daemon threads.
>  - Optionally, a child-JVM test: stream from `main`, return without closing 
> the client, and assert that the JVM exits
> within a few seconds.
> Seen with Ratis 3.3.1; Ratis master has the same code.



--
This message was sent by Atlassian Jira
(v10.3.26#10030026)

Reply via email to