Skip to content

Commit

Permalink
Retry bind if bind failed
Browse files Browse the repository at this point in the history
  • Loading branch information
AlbumenJ committed Aug 31, 2023
1 parent c12a57e commit 63a34ad
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ private void startQosServer(URL url) {

String host = url.getParameter(QOS_HOST);
int port = url.getParameter(QOS_PORT, QosConstants.DEFAULT_PORT);
boolean qosCheck = url.getParameter(QOS_CHECK, false);
boolean acceptForeignIp = Boolean.parseBoolean(url.getParameter(ACCEPT_FOREIGN_IP, "false"));
String acceptForeignIpWhitelist = url.getParameter(ACCEPT_FOREIGN_IP_WHITELIST, StringUtils.EMPTY_STRING);
String anonymousAccessPermissionLevel = url.getParameter(ANONYMOUS_ACCESS_PERMISSION_LEVEL, PermissionLevel.PUBLIC.name());
Expand All @@ -128,7 +129,7 @@ private void startQosServer(URL url) {
server.setAcceptForeignIpWhitelist(acceptForeignIpWhitelist);
server.setAnonymousAccessPermissionLevel(anonymousAccessPermissionLevel);
server.setAnonymousAllowCommands(anonymousAllowCommands);
server.start();
server.start(qosCheck);

} catch (Throwable throwable) {
logger.warn(QOS_FAILED_START_SERVER, "", "", "Fail to start qos server: ", throwable);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public int getPort() {
/**
* start server, bind port
*/
public void start() throws Throwable {
public void start(boolean qosCheck) throws Throwable {
if (!started.compareAndSet(false, true)) {
return;
}
Expand All @@ -115,18 +115,32 @@ protected void initChannel(Channel ch) throws Exception {
));
}
});
try {
if (StringUtils.isBlank(host)) {
serverBootstrap.bind(port).sync();
Throwable lastError = null;
for (int i = 0; i < 10; i++) {
try {
if (StringUtils.isBlank(host)) {
serverBootstrap.bind(port).sync();
} else {
serverBootstrap.bind(host, port).sync();
}

logger.info("qos-server bind localhost:" + port);
return;
} catch (Throwable throwable) {
logger.error(QOS_FAILED_START_SERVER, "", "", "qos-server can not bind localhost:" + port, throwable);
lastError = throwable;
}
if (qosCheck) {
// If enable qos check, we will retry 10 times and wait 3 seconds between each retry.
logger.error(QOS_FAILED_START_SERVER, "", "", "qos-server can not bind localhost:" + port +
" and will retry 10 times. Current retry times: " + i);
Thread.sleep(3000);
} else {
serverBootstrap.bind(host, port).sync();
// If disable qos check, throw exception directly.
throw lastError;
}

logger.info("qos-server bind localhost:" + port);
} catch (Throwable throwable) {
logger.error(QOS_FAILED_START_SERVER, "", "", "qos-server can not bind localhost:" + port, throwable);
throw throwable;
}
throw lastError;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import org.apache.dubbo.common.URL;
import org.apache.dubbo.common.config.ConfigurationUtils;
import org.apache.dubbo.common.constants.LoggerCodeConstants;
import org.apache.dubbo.common.logger.ErrorTypeAwareLogger;
import org.apache.dubbo.common.logger.LoggerFactory;
import org.apache.dubbo.common.utils.CollectionUtils;
Expand Down Expand Up @@ -97,14 +98,14 @@ public void close() {
}
}

public void bind() {
public void bind() throws Throwable {
if (channel == null) {
doOpen();
}
}

@Override
public void doOpen() {
public void doOpen() throws Throwable {
bootstrap = new ServerBootstrap();

bossGroup = NettyEventLoopFactory.eventLoopGroup(1, EVENT_LOOP_BOSS_POOL_NAME);
Expand Down Expand Up @@ -138,9 +139,27 @@ protected void initChannel(SocketChannel ch) throws Exception {
bindIp = ANYHOST_VALUE;
}
InetSocketAddress bindAddress = new InetSocketAddress(bindIp, bindPort);
ChannelFuture channelFuture = bootstrap.bind(bindAddress);
channelFuture.syncUninterruptibly();
channel = channelFuture.channel();
Throwable lastError = null;
for (int i = 0; i < 10; i++) {
try {
ChannelFuture channelFuture = bootstrap.bind(bindAddress);
channelFuture.syncUninterruptibly();
channel = channelFuture.channel();
return;
} catch (Throwable t) {
lastError = t;
}
logger.error(LoggerCodeConstants.TRANSPORT_UNEXPECTED_EXCEPTION, "", "",
"Failed to bind " + getClass().getSimpleName()
+ " on " + bindAddress + ", cause: " + lastError.getMessage() + "will retry 10 times. Current retry times: " + i, lastError);
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new IllegalStateException(e);
}
}
throw lastError;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import org.apache.dubbo.common.URL;
import org.apache.dubbo.common.config.ConfigurationUtils;
import org.apache.dubbo.common.constants.LoggerCodeConstants;
import org.apache.dubbo.common.logger.ErrorTypeAwareLogger;
import org.apache.dubbo.common.logger.LoggerFactory;
import org.apache.dubbo.common.utils.CollectionUtils;
Expand Down Expand Up @@ -105,9 +106,27 @@ protected void doOpen() throws Throwable {
initServerBootstrap(nettyServerHandler);

// bind
ChannelFuture channelFuture = bootstrap.bind(getBindAddress());
channelFuture.syncUninterruptibly();
channel = channelFuture.channel();
Throwable lastError = null;
for (int i = 0; i < 10; i++) {
try {
ChannelFuture channelFuture = bootstrap.bind(getBindAddress());
channelFuture.syncUninterruptibly();
channel = channelFuture.channel();
return;
} catch (Throwable t) {
lastError = t;
}
logger.error(LoggerCodeConstants.TRANSPORT_UNEXPECTED_EXCEPTION, "", "",
"Failed to bind " + getClass().getSimpleName()
+ " on " + getBindAddress() + ", cause: " + lastError.getMessage() + "will retry 10 times. Current retry times: " + i, lastError);
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new IllegalStateException(e);
}
}
throw lastError;

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@
import org.apache.dubbo.common.utils.NetUtils;
import org.apache.dubbo.config.ApplicationConfig;
import org.apache.dubbo.config.context.ConfigManager;
import org.apache.dubbo.remoting.RemotingException;
import org.apache.dubbo.remoting.api.connection.AbstractConnectionClient;
import org.apache.dubbo.remoting.api.connection.ConnectionManager;
import org.apache.dubbo.remoting.api.connection.MultiplexProtocolConnectionManager;
import org.apache.dubbo.remoting.api.pu.DefaultPuHandler;
import org.apache.dubbo.rpc.model.ApplicationModel;
import org.apache.dubbo.rpc.model.ModuleModel;

import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
Expand All @@ -50,7 +50,7 @@ public class ConnectionTest {
private static ConnectionManager connectionManager;

@BeforeAll
public static void init() throws RemotingException {
public static void init() throws Throwable {
int port = NetUtils.getAvailablePort();
url = URL.valueOf("empty://127.0.0.1:" + port + "?foo=bar");
ApplicationModel applicationModel = ApplicationModel.defaultModel();
Expand Down Expand Up @@ -123,7 +123,7 @@ void testRefCnt2() throws InterruptedException {
}

@Test
void connectSyncTest() throws RemotingException {
void connectSyncTest() throws Throwable {
int port = NetUtils.getAvailablePort();
URL url = URL.valueOf("empty://127.0.0.1:" + port + "?foo=bar");
NettyPortUnificationServer nettyPortUnificationServer = new NettyPortUnificationServer(url, new DefaultPuHandler());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,10 @@
import org.apache.dubbo.common.utils.NetUtils;
import org.apache.dubbo.config.ApplicationConfig;
import org.apache.dubbo.config.context.ConfigManager;
import org.apache.dubbo.remoting.RemotingException;
import org.apache.dubbo.remoting.api.pu.DefaultPuHandler;

import org.apache.dubbo.rpc.model.ApplicationModel;
import org.apache.dubbo.rpc.model.ModuleModel;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

Expand All @@ -34,7 +33,7 @@
class PortUnificationServerTest {

@Test
void testBind() throws RemotingException {
void testBind() throws Throwable {
int port = NetUtils.getAvailablePort();
URL url = URL.valueOf("empty://127.0.0.1:" + port + "?foo=bar");
ApplicationModel applicationModel = ApplicationModel.defaultModel();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,14 @@
import org.apache.dubbo.common.constants.CommonConstants;
import org.apache.dubbo.config.ApplicationConfig;
import org.apache.dubbo.config.context.ConfigManager;
import org.apache.dubbo.remoting.RemotingException;
import org.apache.dubbo.remoting.api.connection.AbstractConnectionClient;
import org.apache.dubbo.remoting.api.connection.ConnectionManager;
import org.apache.dubbo.remoting.api.connection.MultiplexProtocolConnectionManager;
import org.apache.dubbo.remoting.api.pu.DefaultPuHandler;
import org.apache.dubbo.remoting.transport.netty4.NettyPortUnificationServer;

import org.apache.dubbo.rpc.model.ApplicationModel;
import org.apache.dubbo.rpc.model.ModuleModel;

import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
Expand All @@ -52,7 +51,7 @@ public class MultiplexProtocolConnectionManagerTest {
private static ConnectionManager connectionManager;

@BeforeAll
public static void init() throws RemotingException {
public static void init() throws Throwable {
ApplicationModel applicationModel = ApplicationModel.defaultModel();
ApplicationConfig applicationConfig = new ApplicationConfig("provider-app");
applicationConfig.setExecutorManagementMode(EXECUTOR_MANAGEMENT_MODE_DEFAULT);
Expand Down Expand Up @@ -95,7 +94,7 @@ public void testConnect() throws Exception {
}

@Test
public void testForEachConnection() throws RemotingException {
public void testForEachConnection() throws Throwable {
DefaultPuHandler handler = new DefaultPuHandler();

NettyPortUnificationServer server2 = new NettyPortUnificationServer(url2, handler);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.apache.dubbo.remoting.transport.netty4.NettyPortUnificationServer;
import org.apache.dubbo.rpc.model.ApplicationModel;
import org.apache.dubbo.rpc.model.ModuleModel;

import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
Expand All @@ -51,7 +52,7 @@ public class SingleProtocolConnectionManagerTest {
private static ConnectionManager connectionManager;

@BeforeAll
public static void init() throws RemotingException {
public static void init() throws Throwable {
int port = NetUtils.getAvailablePort();
url = URL.valueOf("empty://127.0.0.1:" + port + "?foo=bar");
ApplicationModel applicationModel = ApplicationModel.defaultModel();
Expand Down

0 comments on commit 63a34ad

Please sign in to comment.