szetszwo commented on code in PR #955:
URL: https://github.com/apache/ratis/pull/955#discussion_r1375473296


##########
ratis-netty/src/main/java/org/apache/ratis/netty/client/NettyClientStreamRpc.java:
##########
@@ -78,7 +80,33 @@ public class NettyClientStreamRpc implements 
DataStreamClientRpc {
   public static final Logger LOG = 
LoggerFactory.getLogger(NettyClientStreamRpc.class);
 
   private static class WorkerGroupGetter implements Supplier<EventLoopGroup> {
-    private static final AtomicReference<EventLoopGroup> SHARED_WORKER_GROUP = 
new AtomicReference<>();
+
+    private static final 
AtomicReference<ReferenceCountedObject<EventLoopGroup>> SHARED_WORKER_GROUP =
+        new AtomicReference<>();

Review Comment:
   We need 
   ```java
       private static final 
AtomicReference<ReferenceCountedObject<Supplier<EventLoopGroup>>> 
SHARED_WORKER_GROUP
           = new AtomicReference<>();
   ```
   since `AtomicReference.updateAndGet(updateFunction)` may call the 
`updateFunction` without using the result.  The current code will then called 
`newWorkerGroup(..)` without shutting it down.
   
   Use a `MemoizedSupplier` inside:
   ```java
           final Supplier<ReferenceCountedObject<Supplier<EventLoopGroup>>> 
supplier = MemoizedSupplier.valueOf(
               () -> ReferenceCountedObject.wrap(MemoizedSupplier.valueOf(() -> 
newWorkerGroup(properties))));
           final ReferenceCountedObject<Supplier<EventLoopGroup>> 
sharedWorkerGroup = SHARED_WORKER_GROUP.updateAndGet(
               g -> g != null ? g : supplier.get());
           return new WorkerGroupGetter(sharedWorkerGroup.retain().get()) {
             ...
           };
   ```



##########
ratis-netty/src/main/java/org/apache/ratis/netty/client/NettyClientStreamRpc.java:
##########
@@ -78,7 +80,33 @@ public class NettyClientStreamRpc implements 
DataStreamClientRpc {
   public static final Logger LOG = 
LoggerFactory.getLogger(NettyClientStreamRpc.class);
 
   private static class WorkerGroupGetter implements Supplier<EventLoopGroup> {
-    private static final AtomicReference<EventLoopGroup> SHARED_WORKER_GROUP = 
new AtomicReference<>();
+
+    private static final 
AtomicReference<ReferenceCountedObject<EventLoopGroup>> SHARED_WORKER_GROUP =
+        new AtomicReference<>();
+
+    static WorkerGroupGetter newInstance(RaftProperties properties) {
+      final boolean shared = 
NettyConfigKeys.DataStream.Client.workerGroupShare(properties);
+      if (shared) {
+        final Supplier<ReferenceCountedObject<EventLoopGroup>> supplier = 
MemoizedSupplier.valueOf(
+            () -> ReferenceCountedObject.wrap(newWorkerGroup((properties))));
+        final ReferenceCountedObject<EventLoopGroup> sharedWorkerGroup = 
SHARED_WORKER_GROUP.updateAndGet(
+            g -> g != null ? g : supplier.get());

Review Comment:
   We should call `retain()`.



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