Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ISSUE#1815]Adding lifecycle interface and resolve the service downtime caused by thread leak #2927

Merged
merged 23 commits into from
Jun 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
0ddbe7d
Merge pull request #2875 from alibaba/develop
yanlinly May 25, 2020
886fe6d
[#1815]define LifeCycle interface and related abstract class.
zongtanghu May 29, 2020
6981fbc
[#1815]add ResourceLifeCycleManager,adjust some code and fix some uni…
zongtanghu May 29, 2020
6d1baed
[#1815]add the related lifecycle test cases codes.
zongtanghu May 31, 2020
2cb690a
[#1815]add class file license.
zongtanghu May 31, 2020
ab3b234
[#1815]add class file license.
zongtanghu May 31, 2020
8d6dd8f
[#1815]adjust codes and reput the lifecycle to ConfigService class le…
zongtanghu Jun 1, 2020
2ab2742
[#1815]adjust and optimize codes, reput the lifecycle to ConfigServic…
zongtanghu Jun 2, 2020
5e2840a
[#1815]adjust and optimize codes.
zongtanghu Jun 3, 2020
0320e58
Merge remote-tracking branch 'upstream/master' into feature_lifecycle
zongtanghu Jun 3, 2020
e86c158
resolve codes conflicts
zongtanghu Jun 3, 2020
c726dd0
Merge branch 'feature_lifecycle' of https://github.com/zongtanghu/nac…
zongtanghu Jun 3, 2020
09752e3
[#1815]adjust this keywords which just includes constructor and set m…
zongtanghu Jun 3, 2020
94c41e1
[#1815]fix unit test cases.
zongtanghu Jun 3, 2020
860a2d0
resolve codes conflicts
zongtanghu Jun 3, 2020
15fad5d
[#1815]remove destroyService from Factory Class.
zongtanghu Jun 3, 2020
8a2921e
[#1815]remove no used package name from Factory Class.
zongtanghu Jun 4, 2020
8f7fb0f
[#1815]remove no used package name from Factory Class and adjust test…
zongtanghu Jun 9, 2020
eb39830
[#1815]remove lifeCycle, lifeCycleManager, and adjust some related co…
zongtanghu Jun 11, 2020
e6e3161
resolve the codes conflicts
zongtanghu Jun 11, 2020
9b87379
[#1815]fix ut.
zongtanghu Jun 11, 2020
d2b9806
[#1815]remove code comments.
zongtanghu Jun 11, 2020
e7796b3
[#1815]adjust codes for instance class attribute init.
zongtanghu Jun 11, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion api/src/main/java/com/alibaba/nacos/api/NacosFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,5 +97,4 @@ public static NamingMaintainService createMaintainService(String serverAddr) thr
public static NamingMaintainService createMaintainService(Properties properties) throws NacosException {
return NamingMaintainFactory.createMaintainService(properties);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,4 @@ public static ConfigService createConfigService(String serverAddr) throws NacosE
properties.put(PropertyKeyConst.SERVER_ADDR, serverAddr);
return createConfigService(properties);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,10 @@ public interface ConfigService {
*/
String getServerStatus();

/**
* Shutdown the resource service
*
* @throws NacosException exception.
*/
void shutDown() throws NacosException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -145,5 +145,4 @@ public String toString() {
public static final int OVER_THRESHOLD = 503;

public static final int RESOURCE_NOT_FOUND = -404;

}
Original file line number Diff line number Diff line change
Expand Up @@ -555,4 +555,11 @@ public interface NamingService {
* @return is server healthy
*/
String getServerStatus();

/**
* Shutdown the resource service.
*
* @throws NacosException exception.
*/
void shutDown() throws NacosException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,15 @@ public NacosConfigService(Properties properties) throws NacosException {
ValidatorUtils.checkInitParam(properties);
String encodeTmp = properties.getProperty(PropertyKeyConst.ENCODE);
if (StringUtils.isBlank(encodeTmp)) {
encode = Constants.ENCODE;
this.encode = Constants.ENCODE;
} else {
encode = encodeTmp.trim();
this.encode = encodeTmp.trim();
}
initNamespace(properties);
agent = new MetricsHttpAgent(new ServerHttpAgent(properties));
agent.start();
worker = new ClientWorker(agent, configFilterChainManager, properties);

this.agent = new MetricsHttpAgent(new ServerHttpAgent(properties));
this.agent.start();
this.worker = new ClientWorker(this.agent, this.configFilterChainManager, properties);
}

private void initNamespace(Properties properties) {
Expand Down Expand Up @@ -281,4 +282,9 @@ public String getServerStatus() {
}
}

@Override
public void shutDown() throws NacosException{
agent.shutdown();
worker.shutdown();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.client.config.impl.HttpSimpleClient.HttpResult;
import com.alibaba.nacos.common.lifecycle.Closeable;

import java.io.IOException;
import java.util.List;
Expand All @@ -27,7 +28,8 @@
*
* @author Nacos
*/
public interface HttpAgent {
public interface HttpAgent extends Closeable {

/**
* start to get nacos ip list
* @return Nothing.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* MetricsHttpAgent
*
* @author Nacos
*/
package com.alibaba.nacos.client.config.http;

import com.alibaba.nacos.api.exception.NacosException;
Expand All @@ -29,6 +34,7 @@
* @author Nacos
*/
public class MetricsHttpAgent implements HttpAgent {

private HttpAgent httpAgent;

public MetricsHttpAgent(HttpAgent httpAgent) {
Expand Down Expand Up @@ -108,5 +114,10 @@ public String getTenant() {
public String getEncode() {
return httpAgent.getEncode();
}

@Override
public void shutdown() throws NacosException{
httpAgent.shutdown();
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import com.alibaba.nacos.client.utils.TemplateUtils;
import com.alibaba.nacos.common.utils.IoUtils;
import com.alibaba.nacos.common.utils.JacksonUtils;
import com.alibaba.nacos.common.utils.ThreadUtils;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.type.TypeReference;
import org.apache.commons.lang3.StringUtils;
Expand All @@ -44,7 +45,11 @@
import java.util.Date;
import java.util.List;
import java.util.Properties;
import java.util.concurrent.*;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.Callable;

/**
* Server Agent
Expand All @@ -61,6 +66,8 @@ public class ServerHttpAgent implements HttpAgent {

private long securityInfoRefreshIntervalMills = TimeUnit.SECONDS.toMillis(5);

private ScheduledExecutorService executorService;

/**
* @param path 相对于web应用根,以/开头
* @param headers
Expand Down Expand Up @@ -235,7 +242,7 @@ public HttpResult httpDelete(String path, List<String> headers, List<String> par

private String getUrl(String serverAddr, String relativePath) {
String contextPath = serverListMgr.getContentPath().startsWith("/") ?
serverListMgr.getContentPath() : "/" + serverListMgr.getContentPath();
serverListMgr.getContentPath() : "/" + serverListMgr.getContentPath();
return serverAddr + contextPath + relativePath;
}

Expand All @@ -244,22 +251,24 @@ public static String getAppname() {
}

public ServerHttpAgent(ServerListManager mgr) {
serverListMgr = mgr;
this.serverListMgr = mgr;
}

public ServerHttpAgent(ServerListManager mgr, Properties properties) {
serverListMgr = mgr;
this.serverListMgr = mgr;
init(properties);
}

public ServerHttpAgent(Properties properties) throws NacosException {
serverListMgr = new ServerListManager(properties);
securityProxy = new SecurityProxy(properties);
namespaceId = properties.getProperty(PropertyKeyConst.NAMESPACE);
this.serverListMgr = new ServerListManager(properties);
this.securityProxy = new SecurityProxy(properties);
this.namespaceId = properties.getProperty(PropertyKeyConst.NAMESPACE);
init(properties);
securityProxy.login(serverListMgr.getServerUrls());
this.securityProxy.login(this.serverListMgr.getServerUrls());


ScheduledExecutorService executorService = new ScheduledThreadPoolExecutor(1, new ThreadFactory() {
// init executorService
this.executorService = new ScheduledThreadPoolExecutor(1, new ThreadFactory() {
@Override
public Thread newThread(Runnable r) {
Thread t = new Thread(r);
Expand All @@ -269,12 +278,13 @@ public Thread newThread(Runnable r) {
}
});

executorService.scheduleWithFixedDelay(new Runnable() {
this.executorService.scheduleWithFixedDelay(new Runnable() {
@Override
public void run() {
securityProxy.login(serverListMgr.getServerUrls());
}
}, 0, securityInfoRefreshIntervalMills, TimeUnit.MILLISECONDS);
}, 0, this.securityInfoRefreshIntervalMills, TimeUnit.MILLISECONDS);

}

private void injectSecurityInfo(List<String> params) {
Expand Down Expand Up @@ -329,7 +339,7 @@ private void initMaxRetry(Properties properties) {
}

@Override
public synchronized void start() throws NacosException {
public void start() throws NacosException {
serverListMgr.start();
}

Expand Down Expand Up @@ -430,6 +440,14 @@ public String getEncode() {
return encode;
}

@Override
public void shutdown() throws NacosException{
String className = this.getClass().getName();
LOGGER.info("{} do shutdown begin", className);
ThreadUtils.shutdownThreadPool(executorService, LOGGER);
LOGGER.info("{} do shutdown stop", className);
}

@SuppressWarnings("PMD.ClassNamingShouldBeCamelRule")
private static class STSCredential {
@JsonProperty(value = "AccessKeyId")
Expand Down
Loading