FMX commented on code in PR #3154:
URL: https://github.com/apache/celeborn/pull/3154#discussion_r2000375283


##########
common/src/main/java/org/apache/celeborn/common/network/client/ReconnectHandler.java:
##########
@@ -0,0 +1,82 @@
+/*
+ * 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.celeborn.common.network.client;
+
+import java.util.concurrent.TimeUnit;
+
+import io.netty.bootstrap.Bootstrap;
+import io.netty.channel.ChannelFuture;
+import io.netty.channel.ChannelHandlerContext;
+import io.netty.channel.ChannelInboundHandlerAdapter;
+
+import org.apache.celeborn.common.network.util.TransportConf;
+
+public class ReconnectHandler extends ChannelInboundHandlerAdapter {
+
+  private final int maxReconnectRetries;
+  private final int reconnectRetryWaitTimeMs;
+  private final Bootstrap bootstrap;
+
+  private volatile boolean shouldReconnect = true;
+  private int reconnectRetries = 0;
+
+  public ReconnectHandler(TransportConf conf, Bootstrap bootstrap) {
+    this.maxReconnectRetries = conf.maxReconnectRetries();
+    this.reconnectRetryWaitTimeMs = conf.reconnectRetryWaitTimeMs();
+    this.bootstrap = bootstrap;
+  }
+
+  @Override
+  public void channelActive(ChannelHandlerContext context) throws Exception {
+    reconnectRetries = 0;
+    super.channelActive(context);
+  }
+
+  @Override
+  public void channelInactive(ChannelHandlerContext context) throws Exception {
+    if (shouldReconnect && reconnectRetries < maxReconnectRetries) {
+      scheduleReconnect(context);
+    } else {
+      super.channelInactive(context);
+    }
+  }
+
+  private void scheduleReconnect(ChannelHandlerContext context) {
+    reconnectRetries++;
+    context
+        .channel()
+        .eventLoop()
+        .schedule(
+            () -> {
+              bootstrap
+                  .connect()
+                  .addListener(
+                      (ChannelFuture future) -> {
+                        if (!future.isSuccess()) {

Review Comment:
   If the connection fails, there is a risk that Netty will attempt to connect 
indefinitely.



##########
common/src/main/java/org/apache/celeborn/common/network/client/ReconnectHandler.java:
##########
@@ -0,0 +1,82 @@
+/*
+ * 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.celeborn.common.network.client;
+
+import java.util.concurrent.TimeUnit;
+
+import io.netty.bootstrap.Bootstrap;
+import io.netty.channel.ChannelFuture;
+import io.netty.channel.ChannelHandlerContext;
+import io.netty.channel.ChannelInboundHandlerAdapter;
+
+import org.apache.celeborn.common.network.util.TransportConf;
+
+public class ReconnectHandler extends ChannelInboundHandlerAdapter {
+
+  private final int maxReconnectRetries;
+  private final int reconnectRetryWaitTimeMs;
+  private final Bootstrap bootstrap;
+
+  private volatile boolean shouldReconnect = true;
+  private int reconnectRetries = 0;
+
+  public ReconnectHandler(TransportConf conf, Bootstrap bootstrap) {
+    this.maxReconnectRetries = conf.maxReconnectRetries();
+    this.reconnectRetryWaitTimeMs = conf.reconnectRetryWaitTimeMs();
+    this.bootstrap = bootstrap;
+  }
+
+  @Override
+  public void channelActive(ChannelHandlerContext context) throws Exception {
+    reconnectRetries = 0;
+    super.channelActive(context);
+  }
+
+  @Override
+  public void channelInactive(ChannelHandlerContext context) throws Exception {
+    if (shouldReconnect && reconnectRetries < maxReconnectRetries) {
+      scheduleReconnect(context);
+    } else {
+      super.channelInactive(context);
+    }
+  }
+
+  private void scheduleReconnect(ChannelHandlerContext context) {
+    reconnectRetries++;
+    context
+        .channel()
+        .eventLoop()
+        .schedule(
+            () -> {
+              bootstrap
+                  .connect()
+                  .addListener(
+                      (ChannelFuture future) -> {
+                        if (!future.isSuccess()) {

Review Comment:
   I think you should check the reconnectRetries is lesser than 
maxReconnectRetries in the `scheduleReconnect` method.



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