Skip to content

Commit d947e28

Browse files
committed
解决一个jvm 运行两个 node 的问题
1 parent 07a22c7 commit d947e28

File tree

61 files changed

+522
-549
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

61 files changed

+522
-549
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ LTS 轻量级分布式任务调度框架(Light Task Schedule)
2929
## 特性
3030

3131
* 负载均衡:
32-
* JobClient 和 TaskTracker会随机连接JobTracker节点组中的一个节点,实现JobTracker负载均衡。当连接上后,将一直保持连接这个节点,保持连接通道,知道这个节点不可用,减少每次都重新连接一个节点带来的性能开销。
32+
* JobClient 和 TaskTracker会随机连接JobTracker节点组中的一个节点,实现JobTracker负载均衡。当连接上后,将一直保持连接这个节点,保持连接通道,直到这个节点不可用,减少每次都重新连接一个节点带来的性能开销。
3333
* JobTracker 分发任务时,是优先分配给最空闲的一个TaskTracker节点,实现TaskTracker节点的负载均衡。
3434

3535
* 健壮性:

job-admin/pom.xml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>light-task-schedule</artifactId>
7+
<groupId>com.lts</groupId>
8+
<version>1.3-SNAPSHOT</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
<packaging>war</packaging>
12+
<artifactId>job-admin</artifactId>
13+
14+
15+
</project>

job-client/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<parent>
66
<artifactId>light-task-schedule</artifactId>
77
<groupId>com.lts</groupId>
8-
<version>1.2-SNAPSHOT</version>
8+
<version>1.3-SNAPSHOT</version>
99
</parent>
1010
<modelVersion>4.0.0</modelVersion>
1111

job-client/src/main/java/com/lts/job/client/JobClient.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import com.lts.job.client.domain.ResponseCode;
1111
import com.lts.job.core.exception.JobTrackerNotFoundException;
1212
import com.lts.job.core.protocol.JobProtos;
13+
import com.lts.job.core.protocol.command.CommandWrapper;
1314
import com.lts.job.core.protocol.command.JobSubmitRequest;
1415
import com.lts.job.core.protocol.command.JobSubmitResponse;
1516
import com.lts.job.core.util.BatchUtils;
@@ -40,6 +41,7 @@ public class JobClient<T extends JobClientNode> extends AbstractClientNode<JobCl
4041
public JobClient() {
4142
// 设置默认节点组
4243
config.setNodeGroup(Constants.DEFAULT_NODE_JOB_CLIENT_GROUP);
44+
4345
}
4446

4547
/**
@@ -55,7 +57,7 @@ protected Response _submitJob(final List<Job> jobs) {
5557
final Response response = new Response();
5658

5759
try {
58-
JobSubmitRequest jobSubmitRequest = new JobSubmitRequest();
60+
JobSubmitRequest jobSubmitRequest = application.getCommandWrapper().wrapper(new JobSubmitRequest());
5961
jobSubmitRequest.setJobs(jobs);
6062

6163
RemotingCommand requestCommand = RemotingCommand.createRequestCommand(JobProtos.RequestCode.SUBMIT_JOB.code(), jobSubmitRequest);

job-client/src/main/java/com/lts/job/client/RetryJobClient.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public class RetryJobClient extends JobClient<JobClientNode> {
2020
@Override
2121
protected void nodeStart() {
2222

23-
retryScheduler = new RetryScheduler<Job>(30) {
23+
retryScheduler = new RetryScheduler<Job>(application, 30) {
2424
@Override
2525
protected boolean isRemotingEnable() {
2626
return isServerEnable();

job-client/src/main/java/com/lts/job/client/processor/RemotingDispatcher.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
*/
2222
public class RemotingDispatcher extends AbstractProcessor {
2323

24-
private static final Map<JobProtos.RequestCode, NettyRequestProcessor> processors = new HashMap<JobProtos.RequestCode, NettyRequestProcessor>();
24+
private final Map<JobProtos.RequestCode, NettyRequestProcessor> processors = new HashMap<JobProtos.RequestCode, NettyRequestProcessor>();
2525

2626
public RemotingDispatcher(RemotingClientDelegate remotingClient, JobFinishedHandler jobFinishedHandler) {
2727
super(remotingClient);

job-core/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<parent>
66
<artifactId>light-task-schedule</artifactId>
77
<groupId>com.lts</groupId>
8-
<version>1.2-SNAPSHOT</version>
8+
<version>1.3-SNAPSHOT</version>
99
</parent>
1010
<modelVersion>4.0.0</modelVersion>
1111

job-core/src/main/java/com/lts/job/core/cluster/AbstractClientNode.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import com.lts.job.core.constant.Constants;
44
import com.lts.job.core.remoting.HeartBeatMonitor;
55
import com.lts.job.core.remoting.RemotingClientDelegate;
6-
import com.lts.job.core.support.Application;
76
import com.lts.job.core.util.StringUtils;
87
import com.lts.job.remoting.netty.NettyClientConfig;
98
import com.lts.job.remoting.netty.NettyRemotingClient;
@@ -13,15 +12,15 @@
1312

1413
/**
1514
* @author Robert HG (254963746@qq.com) on 8/18/14.
16-
* 抽象 netty 客户端
15+
* 抽象 netty 客户端
1716
*/
1817
public abstract class AbstractClientNode<T extends Node> extends AbstractJobNode<T> {
1918

2019
protected RemotingClientDelegate remotingClient;
2120
private HeartBeatMonitor heartBeatMonitor;
2221

2322
public AbstractClientNode() {
24-
this.remotingClient = new RemotingClientDelegate(new NettyRemotingClient(getNettyClientConfig()));
23+
this.remotingClient = new RemotingClientDelegate(new NettyRemotingClient(getNettyClientConfig()), application);
2524
this.heartBeatMonitor = new HeartBeatMonitor(remotingClient);
2625
}
2726

@@ -53,19 +52,20 @@ protected void nodeStop() {
5352

5453
public void setWorkThreads(int workThreads) {
5554
config.setWorkThreads(workThreads);
56-
Application.setAttribute(Constants.KEY_AVAILABLE_THREADS, config.getWorkThreads());
55+
application.setAttribute(Constants.KEY_AVAILABLE_THREADS, config.getWorkThreads());
5756
}
5857

5958
/**
6059
* 设置节点组名
60+
*
6161
* @param nodeGroup
6262
*/
63-
public void setNodeGroup(String nodeGroup){
63+
public void setNodeGroup(String nodeGroup) {
6464
config.setNodeGroup(nodeGroup);
6565
}
6666

6767
public void setJobInfoSavePath(String jobInfoSavePath) {
68-
if(StringUtils.isNotEmpty(jobInfoSavePath)){
68+
if (StringUtils.isNotEmpty(jobInfoSavePath)) {
6969
config.setJobInfoSavePath(jobInfoSavePath + "/.job");
7070
}
7171
}

job-core/src/main/java/com/lts/job/core/cluster/AbstractJobNode.java

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import com.lts.job.core.domain.JobNodeConfig;
55
import com.lts.job.core.listener.MasterNodeChangeListener;
66
import com.lts.job.core.listener.NodeChangeListener;
7+
import com.lts.job.core.protocol.command.CommandWrapper;
78
import com.lts.job.core.registry.NodeRegistry;
89
import com.lts.job.core.support.Application;
910
import com.lts.job.core.listener.MasterNodeElectionListener;
@@ -23,8 +24,11 @@ public abstract class AbstractJobNode<T extends Node> implements JobNode {
2324
protected NodeRegistry registry;
2425
protected T node;
2526
protected JobNodeConfig config;
27+
protected Application application;
28+
protected NodeFactory nodeFactory;
2629

2730
public AbstractJobNode() {
31+
application = new Application();
2832
config = new JobNodeConfig();
2933
config.setIdentity(StringUtils.generateUUID());
3034
config.setWorkThreads(Constants.AVAILABLE_PROCESSOR);
@@ -35,20 +39,24 @@ public AbstractJobNode() {
3539
config.setJobInfoSavePath(Constants.USER_HOME + "/.job");
3640
config.setClusterName(Constants.DEFAULT_CLUSTER_NAME);
3741
// 可用的线程数
38-
Application.setAttribute(Constants.KEY_AVAILABLE_THREADS, config.getWorkThreads());
42+
application.setAttribute(Constants.KEY_AVAILABLE_THREADS, config.getWorkThreads());
3943

40-
Application.Config = config;
44+
application.setConfig(config);
45+
application.setCommandWrapper(new CommandWrapper(application));
46+
application.setNodeManager(new NodeManager(application));
47+
nodeFactory = new NodeFactory(application);
48+
application.setMasterElector(new MasterElector(application));
4149

42-
this.registry = new NodeRegistry();
50+
this.registry = new NodeRegistry(application);
4351
// 用于master选举的监听器
44-
addNodeChangeListener(new MasterNodeElectionListener());
52+
addNodeChangeListener(new MasterNodeElectionListener(application));
4553
}
4654

4755
final public void start() {
4856
try {
4957

5058
Class<T> nodeClass = GenericsUtils.getSuperClassGenericType(this.getClass());
51-
node = NodeFactory.create(nodeClass, config);
59+
node = nodeFactory.create(nodeClass, config);
5260
config.setNodeType(node.getNodeType());
5361

5462
LOGGER.info("当前节点配置:{}", config);
@@ -116,6 +124,6 @@ public void addNodeChangeListener(NodeChangeListener nodeChangeListener) {
116124
* @param masterNodeChangeListener
117125
*/
118126
public void addMasterNodeChangeListener(MasterNodeChangeListener masterNodeChangeListener) {
119-
MasterElector.addMasterNodeChangeListener(masterNodeChangeListener);
127+
application.getMasterElector().addMasterNodeChangeListener(masterNodeChangeListener);
120128
}
121129
}

job-core/src/main/java/com/lts/job/core/cluster/AbstractServerNode.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.lts.job.core.cluster;
22

33
import com.lts.job.core.remoting.RemotingServerDelegate;
4-
import com.lts.job.core.support.Application;
54
import com.lts.job.remoting.netty.NettyRemotingServer;
65
import com.lts.job.remoting.netty.NettyRequestProcessor;
76
import com.lts.job.remoting.netty.NettyServerConfig;
@@ -22,7 +21,7 @@ protected void nodeStart() {
2221
// config 配置
2322
config.setListenPort(getListenPort());
2423

25-
remotingServer = new RemotingServerDelegate(new NettyRemotingServer(config));
24+
remotingServer = new RemotingServerDelegate(new NettyRemotingServer(config), application);
2625

2726
remotingServer.start();
2827

@@ -55,7 +54,7 @@ protected void nodeStop() {
5554
* @return
5655
*/
5756
protected Integer getListenPort() {
58-
return Application.Config.getListenPort();
57+
return application.getConfig().getListenPort();
5958
}
6059

6160
}

0 commit comments

Comments
 (0)