1.When the integer reach Integer.MAX, reset it. (#4506)

2.Format some log
3.Fix some typo
This commit is contained in:
赵延 2020-12-17 10:12:48 +08:00 committed by GitHub
parent 898c8fb3c8
commit 04c5056f13
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -106,7 +106,7 @@ public abstract class RpcClient implements Closeable {
public RpcClient(ServerListFactory serverListFactory) { public RpcClient(ServerListFactory serverListFactory) {
this.serverListFactory = serverListFactory; this.serverListFactory = serverListFactory;
rpcClientStatus.compareAndSet(RpcClientStatus.WAIT_INIT, RpcClientStatus.INITED); rpcClientStatus.compareAndSet(RpcClientStatus.WAIT_INIT, RpcClientStatus.INITED);
LoggerUtils.printIfInfoEnabled(LOGGER, "RpcClient init in constructor , ServerListFactory ={}", LoggerUtils.printIfInfoEnabled(LOGGER, "RpcClient init in constructor, ServerListFactory ={}",
serverListFactory.getClass().getName()); serverListFactory.getClass().getName());
} }
@ -114,7 +114,7 @@ public abstract class RpcClient implements Closeable {
this(name); this(name);
this.serverListFactory = serverListFactory; this.serverListFactory = serverListFactory;
rpcClientStatus.compareAndSet(RpcClientStatus.WAIT_INIT, RpcClientStatus.INITED); rpcClientStatus.compareAndSet(RpcClientStatus.WAIT_INIT, RpcClientStatus.INITED);
LoggerUtils.printIfInfoEnabled(LOGGER, "RpcClient init in constructor , ServerListFactory ={}", LoggerUtils.printIfInfoEnabled(LOGGER, "RpcClient init in constructor, ServerListFactory ={}",
serverListFactory.getClass().getName()); serverListFactory.getClass().getName());
} }
@ -180,7 +180,7 @@ public abstract class RpcClient implements Closeable {
this.serverListFactory = serverListFactory; this.serverListFactory = serverListFactory;
rpcClientStatus.compareAndSet(RpcClientStatus.WAIT_INIT, RpcClientStatus.INITED); rpcClientStatus.compareAndSet(RpcClientStatus.WAIT_INIT, RpcClientStatus.INITED);
LoggerUtils.printIfInfoEnabled(LOGGER, "RpcClient init , ServerListFactory ={}", LoggerUtils.printIfInfoEnabled(LOGGER, "RpcClient init, ServerListFactory ={}",
serverListFactory.getClass().getName()); serverListFactory.getClass().getName());
} }
@ -191,7 +191,7 @@ public abstract class RpcClient implements Closeable {
*/ */
public void initLabels(Map<String, String> labels) { public void initLabels(Map<String, String> labels) {
this.labels.putAll(labels); this.labels.putAll(labels);
LoggerUtils.printIfInfoEnabled(LOGGER, "RpcClient init label ,labels={}", this.labels); LoggerUtils.printIfInfoEnabled(LOGGER, "RpcClient init label, labels={}", this.labels);
} }
/** /**
@ -238,25 +238,25 @@ public abstract class RpcClient implements Closeable {
Connection connectToServer = null; Connection connectToServer = null;
rpcClientStatus.set(RpcClientStatus.STARTING); rpcClientStatus.set(RpcClientStatus.STARTING);
int startUpretyTimes = 3; int startUpRetryTimes = 3;
while (startUpretyTimes > 0 && connectToServer == null) { while (startUpRetryTimes > 0 && connectToServer == null) {
try { try {
startUpretyTimes--; startUpRetryTimes--;
ServerInfo serverInfo = nextRpcServer(); ServerInfo serverInfo = nextRpcServer();
LoggerUtils.printIfInfoEnabled(LOGGER, LoggerUtils.printIfInfoEnabled(LOGGER,
String.format("[%s]try to connect to server on start up,server : %s", name, serverInfo)); String.format("[%s] try to connect to server on start up, server: %s", name, serverInfo));
connectToServer = connectToServer(serverInfo); connectToServer = connectToServer(serverInfo);
} catch (Exception e) { } catch (Exception e) {
LoggerUtils.printIfWarnEnabled(LOGGER, String.format( LoggerUtils.printIfWarnEnabled(LOGGER, String.format(
"fail to connect to server on start up,error message=%s,start up trytimes left :%s", "Fail to connect to server on start up, error message=%s, start up retry times left: %s",
e.getMessage(), startUpretyTimes)); e.getMessage(), startUpRetryTimes));
} }
} }
if (connectToServer != null) { if (connectToServer != null) {
LoggerUtils.printIfInfoEnabled(LOGGER, String.format("[%s]success to connect to server on start up", name)); LoggerUtils.printIfInfoEnabled(LOGGER, String.format("[%s] success to connect to server on start up", name));
this.currentConnection = connectToServer; this.currentConnection = connectToServer;
rpcClientStatus.set(RpcClientStatus.RUNNING); rpcClientStatus.set(RpcClientStatus.RUNNING);
eventLinkedBlockingQueue.offer(new ConnectionEvent(ConnectionEvent.CONNECTED)); eventLinkedBlockingQueue.offer(new ConnectionEvent(ConnectionEvent.CONNECTED));
@ -288,7 +288,7 @@ public abstract class RpcClient implements Closeable {
} }
} }
} catch (Exception e) { } catch (Exception e) {
LoggerUtils.printIfErrorEnabled(LOGGER, "switch server error ", e); LoggerUtils.printIfErrorEnabled(LOGGER, "Switch server error ", e);
} }
return new ConnectResetResponse(); return new ConnectResetResponse();
} }
@ -364,7 +364,7 @@ public abstract class RpcClient implements Closeable {
Connection connectNew = connectToServer(serverInfo); Connection connectNew = connectToServer(serverInfo);
if (connectNew != null) { if (connectNew != null) {
LoggerUtils.printIfInfoEnabled(LOGGER, LoggerUtils.printIfInfoEnabled(LOGGER,
String.format("[%s]-success to connect server : %s", name, serverInfo)); String.format("[%s] success to connect server : %s", name, serverInfo));
//successfully create a new connect. //successfully create a new connect.
if (currentConnection != null) { if (currentConnection != null) {
currentConnection.setAbandon(true); currentConnection.setAbandon(true);
@ -394,9 +394,13 @@ public abstract class RpcClient implements Closeable {
if (reConnectTimes > 0 if (reConnectTimes > 0
&& reConnectTimes % RpcClient.this.serverListFactory.getServerList().size() == 0) { && reConnectTimes % RpcClient.this.serverListFactory.getServerList().size() == 0) {
LoggerUtils.printIfInfoEnabled(LOGGER, String.format( LoggerUtils.printIfInfoEnabled(LOGGER, String.format(
"[%s]-fail to connect server,after trying %s times, last tryed server is %s", name, "[%s] fail to connect server,after trying %s times, last try server is %s", name,
reConnectTimes, serverInfo)); reConnectTimes, serverInfo));
retryTurns++; if (Integer.MAX_VALUE == retryTurns) {
retryTurns = 10;
} else {
retryTurns++;
}
} }
reConnectTimes++; reConnectTimes++;
@ -414,11 +418,11 @@ public abstract class RpcClient implements Closeable {
if (isShutdown()) { if (isShutdown()) {
LoggerUtils.printIfInfoEnabled(LOGGER, LoggerUtils.printIfInfoEnabled(LOGGER,
String.format("[%s]- client is shutdown ,stop reconnect to server", name)); String.format("[%s] client is shutdown ,stop reconnect to server", name));
} }
} catch (Exception e) { } catch (Exception e) {
LoggerUtils.printIfWarnEnabled(LOGGER, String.format("[%s]- fail to connect to server", name)); LoggerUtils.printIfWarnEnabled(LOGGER, String.format("[%s] fail to connect to server", name));
} finally { } finally {
switchingFlag.set(false); switchingFlag.set(false);
switchingLock.unlock(); switchingLock.unlock();
@ -483,7 +487,7 @@ public abstract class RpcClient implements Closeable {
while (retryTimes > 0) { while (retryTimes > 0) {
try { try {
if (this.currentConnection == null || !isRunning()) { if (this.currentConnection == null || !isRunning()) {
throw new NacosException(NacosException.CLIENT_INVALID_PARAM, "client not connected."); throw new NacosException(NacosException.CLIENT_INVALID_PARAM, "Client not connected.");
} }
response = this.currentConnection.request(request, buildMeta()); response = this.currentConnection.request(request, buildMeta());
@ -501,7 +505,7 @@ public abstract class RpcClient implements Closeable {
} }
} catch (Exception e) { } catch (Exception e) {
LoggerUtils.printIfErrorEnabled(LOGGER, "Fail to send request,request={},errorMessage={}", request, LoggerUtils.printIfErrorEnabled(LOGGER, "Fail to send request, request={}, errorMessage={}", request,
e.getMessage()); e.getMessage());
exceptionToThrow = e; exceptionToThrow = e;
} }
@ -531,12 +535,12 @@ public abstract class RpcClient implements Closeable {
while (retryTimes > 0) { while (retryTimes > 0) {
try { try {
if (this.currentConnection == null) { if (this.currentConnection == null) {
throw new NacosException(NacosException.CLIENT_INVALID_PARAM, "client not connected."); throw new NacosException(NacosException.CLIENT_INVALID_PARAM, "Client not connected.");
} }
this.currentConnection.asyncRequest(request, buildMeta(), callback); this.currentConnection.asyncRequest(request, buildMeta(), callback);
return; return;
} catch (Exception e) { } catch (Exception e) {
LoggerUtils.printIfErrorEnabled(LOGGER, "Fail to send request,request={},errorMesssage={}", request, LoggerUtils.printIfErrorEnabled(LOGGER, "Fail to send request, request={}, error Message={}", request,
e.getMessage()); e.getMessage());
exceptionToThrow = e; exceptionToThrow = e;
} }
@ -556,7 +560,7 @@ public abstract class RpcClient implements Closeable {
*/ */
public RequestFuture requestFuture(Request request) throws NacosException { public RequestFuture requestFuture(Request request) throws NacosException {
if (this.currentConnection == null) { if (this.currentConnection == null) {
throw new NacosException(NacosException.CLIENT_INVALID_PARAM, "client not connected."); throw new NacosException(NacosException.CLIENT_INVALID_PARAM, "Client not connected.");
} }
RequestFuture requestFuture = this.currentConnection.requestFuture(request, buildMeta()); RequestFuture requestFuture = this.currentConnection.requestFuture(request, buildMeta());
return requestFuture; return requestFuture;
@ -607,7 +611,7 @@ public abstract class RpcClient implements Closeable {
* @param serverRequestHandler serverRequestHandler * @param serverRequestHandler serverRequestHandler
*/ */
public synchronized void registerServerPushResponseHandler(ServerRequestHandler serverRequestHandler) { public synchronized void registerServerPushResponseHandler(ServerRequestHandler serverRequestHandler) {
LoggerUtils.printIfInfoEnabled(LOGGER, " Register server push request handler :{}", LoggerUtils.printIfInfoEnabled(LOGGER, "Register server push request handler:{}",
serverRequestHandler.getClass().getName()); serverRequestHandler.getClass().getName());
this.serverRequestHandlers.add(serverRequestHandler); this.serverRequestHandlers.add(serverRequestHandler);