├── README.md
├── mglory
├── .classpath
├── .project
├── .settings
│ ├── org.eclipse.core.resources.prefs
│ └── org.eclipse.jdt.core.prefs
├── bulider.xml
├── lib
│ ├── commons-logging-1.1.1.jar
│ ├── log4j-1.2.15.jar
│ └── netty-all-4.0.18.Final.jar
└── src
│ ├── com
│ ├── lingo
│ │ └── mglory
│ │ │ ├── job
│ │ │ └── JobExecutor.java
│ │ │ ├── listener
│ │ │ ├── ExitSystemExecutor.java
│ │ │ └── StartMglory.java
│ │ │ ├── mq
│ │ │ ├── MQReceiver.java
│ │ │ ├── MQStore.java
│ │ │ ├── MgloryMQ.java
│ │ │ └── MsgExecutorTask.java
│ │ │ ├── param
│ │ │ ├── MgloryIn.java
│ │ │ ├── MgloryOut.java
│ │ │ └── WorkFlowDAG.java
│ │ │ ├── resource
│ │ │ ├── ElectManager.java
│ │ │ ├── NodeManager.java
│ │ │ ├── ResourceHelper.java
│ │ │ ├── ResourceManager.java
│ │ │ ├── ResourcePool.java
│ │ │ ├── ResourceScheduler.java
│ │ │ ├── ResourceUsage.java
│ │ │ └── SystemResource.java
│ │ │ ├── route
│ │ │ ├── MgloryHash.java
│ │ │ ├── MgloryHashHelper.java
│ │ │ └── Node.java
│ │ │ ├── server
│ │ │ ├── MgloryGroupServer.java
│ │ │ └── MgloryWorkerServer.java
│ │ │ ├── store
│ │ │ ├── MgloryMap.java
│ │ │ ├── MgloryMapManager.java
│ │ │ └── MgloryMapStore.java
│ │ │ ├── task
│ │ │ └── TaskState.java
│ │ │ ├── transport
│ │ │ ├── client
│ │ │ │ └── Client.java
│ │ │ ├── handler
│ │ │ │ ├── ClientHandler.java
│ │ │ │ └── ServerHandler.java
│ │ │ └── server
│ │ │ │ └── Server.java
│ │ │ └── util
│ │ │ ├── PropertiesUtil.java
│ │ │ └── Utils.java
│ └── test
│ │ ├── MQTest.java
│ │ ├── MgloryMapTest.java
│ │ ├── ResourceUsageTest.java
│ │ ├── Test.java
│ │ ├── Test4.java
│ │ ├── Test5.java
│ │ ├── WordCount.java
│ │ └── WordCount2.java
│ ├── config.properties
│ └── log4j.properties
└── mglory简介.doc
/README.md:
--------------------------------------------------------------------------------
1 | # mglory
2 | mglory是一个java分布式框架。具备以下功能:分布式计算、分布式内存存储(支持持久化)、分布式消息队列
3 |
--------------------------------------------------------------------------------
/mglory/.classpath:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
--------------------------------------------------------------------------------
/mglory/.project:
--------------------------------------------------------------------------------
1 |
2 |
3 | mglory
4 |
5 |
6 |
7 |
8 |
9 | org.eclipse.jdt.core.javabuilder
10 |
11 |
12 |
13 |
14 |
15 | org.eclipse.jdt.core.javanature
16 |
17 |
18 |
--------------------------------------------------------------------------------
/mglory/.settings/org.eclipse.core.resources.prefs:
--------------------------------------------------------------------------------
1 | eclipse.preferences.version=1
2 | encoding/=UTF-8
3 |
--------------------------------------------------------------------------------
/mglory/.settings/org.eclipse.jdt.core.prefs:
--------------------------------------------------------------------------------
1 | eclipse.preferences.version=1
2 | org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
3 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6
4 | org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
5 | org.eclipse.jdt.core.compiler.compliance=1.6
6 | org.eclipse.jdt.core.compiler.debug.lineNumber=generate
7 | org.eclipse.jdt.core.compiler.debug.localVariable=generate
8 | org.eclipse.jdt.core.compiler.debug.sourceFile=generate
9 | org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
10 | org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
11 | org.eclipse.jdt.core.compiler.source=1.6
12 |
--------------------------------------------------------------------------------
/mglory/bulider.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | uniTrade jar builder
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
--------------------------------------------------------------------------------
/mglory/lib/commons-logging-1.1.1.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/lingo623/mglory/a89bf52fd91698de799736f21cb772c8c476abe9/mglory/lib/commons-logging-1.1.1.jar
--------------------------------------------------------------------------------
/mglory/lib/log4j-1.2.15.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/lingo623/mglory/a89bf52fd91698de799736f21cb772c8c476abe9/mglory/lib/log4j-1.2.15.jar
--------------------------------------------------------------------------------
/mglory/lib/netty-all-4.0.18.Final.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/lingo623/mglory/a89bf52fd91698de799736f21cb772c8c476abe9/mglory/lib/netty-all-4.0.18.Final.jar
--------------------------------------------------------------------------------
/mglory/src/com/lingo/mglory/job/JobExecutor.java:
--------------------------------------------------------------------------------
1 | package com.lingo.mglory.job;
2 |
3 | import java.util.ArrayList;
4 | import java.util.HashMap;
5 | import java.util.List;
6 | import java.util.Map;
7 | import java.util.concurrent.Callable;
8 | import java.util.concurrent.ExecutorService;
9 | import java.util.concurrent.Executors;
10 | import java.util.concurrent.FutureTask;
11 |
12 | import org.apache.commons.logging.Log;
13 | import org.apache.commons.logging.LogFactory;
14 |
15 | import com.lingo.mglory.param.WorkFlowDAG;
16 | import com.lingo.mglory.param.MgloryIn;
17 | import com.lingo.mglory.param.MgloryOut;
18 | import com.lingo.mglory.resource.ResourceHelper;
19 | import com.lingo.mglory.task.TaskState;
20 | import com.lingo.mglory.transport.client.Client;
21 |
22 | public class JobExecutor {
23 | private static Log log = LogFactory.getLog(JobExecutor.class);
24 | private String masterGroup;//中心服务(资源管理器)主机地址 如:114.215.178.115:1099
25 | private String slaveGroup;//中心服务(资源管理器)备机地址
26 | private WorkFlowDAG dag;
27 | private boolean isInterrupted=false;
28 | private static ExecutorService taskExecutor=null;
29 | static
30 | {
31 | taskExecutor=Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
32 | }
33 | public JobExecutor(String masterGroup,String slaveGroup)
34 | {
35 | this.masterGroup=masterGroup;
36 | this.slaveGroup=slaveGroup;
37 | }
38 | /**
39 | * 任务执行
40 | * @param in
41 | * @return
42 | */
43 | public MgloryOut execute(MgloryIn in)
44 | {
45 | return execute(in,"");
46 | }
47 | /**
48 | * 任务执行
49 | * @param in
50 | * @param execResource
51 | * @return
52 | */
53 | public MgloryOut execute(MgloryIn in,String execResource)
54 | {
55 | String taskId=in.getTaskId();
56 | MgloryOut out =new MgloryOut();
57 | out.setTaskId(taskId);
58 | try
59 | {
60 | String resource=execResource;
61 | if (resource.equals(""))
62 | {
63 | resource=ResourceHelper.getInstance().getIdleResource(masterGroup, slaveGroup);
64 | }
65 | dag.setTaskExcutor(taskId,resource);
66 | String ip=resource.split(":")[0];
67 | int port=Integer.parseInt(resource.split(":")[1]);
68 | out=Client.getInstance().call(ip, port,in);
69 | out.setTaskId(taskId);
70 | }
71 | catch(Exception e)
72 | {
73 | out.setTaskId(taskId);
74 | out.setStatus(MgloryOut.EXCEPTION);
75 | out.setErrorMsg(e.getMessage());
76 | log.warn("忽略执行任务异常:"+e);
77 | }
78 | return out;
79 | }
80 | /**
81 | * 任务执行
82 | * @param in
83 | * @param dag
84 | * @return
85 | * @throws Exception
86 | */
87 | public MgloryOut execute(MgloryIn in,WorkFlowDAG dag) throws Exception
88 | {
89 | MgloryOut out =new MgloryOut();
90 | if (dag==null || dag.getTaskMap().isEmpty())
91 | {
92 | out.setErrorMsg("空DAG无须执行");
93 | return out;
94 | }
95 | this.dag=dag;
96 | preDealForStartTaskNode(in);
97 | execute("start");
98 | if (isInterrupted)
99 | {
100 | interrupt();
101 | }
102 | out=dag.getTaskResult().get("end");
103 | return out;
104 | }
105 | private void execute(String taskId)
106 | {
107 | Map needDealTaskMap=getNeedDealTask(taskId);
108 | if (!needDealTaskMap.isEmpty())
109 | {
110 | //end任务节点不处理
111 | if(needDealTaskMap.containsKey("end"))
112 | {
113 | dag.setTaskState(taskId,TaskState.COMPLETE);
114 | //taskExecutor.shutdown();
115 | return;
116 | }
117 | //ExecutorService taskExecutor=Executors.newFixedThreadPool(needDealTaskMap.size());
118 | List> futureTasks = new ArrayList>();
119 | for (final String waitExecuteTaskId:needDealTaskMap.keySet())
120 | {
121 | final String taskClazzName=dag.getTaskMap().get(waitExecuteTaskId).get("taskClazzName");
122 | final List preTaskList=dag.getPreTaskMap().get(waitExecuteTaskId);
123 | final String operMethod=dag.getTaskMap().get(waitExecuteTaskId).get("operMethod");
124 | final long timeout=Long.parseLong(dag.getTaskMap().get(waitExecuteTaskId).get("timeout"));
125 | dag.setTaskState(waitExecuteTaskId,TaskState.RUNNING);
126 | FutureTask futureTask = new FutureTask(
127 | new Callable() {
128 | @Override
129 | public MgloryOut call() throws Exception {
130 | String execResource="";
131 | String locationType=MgloryOut.RESULT;
132 | MgloryIn in=new MgloryIn();
133 | in.setTaskId(waitExecuteTaskId);
134 | in.setInvokeMethod(taskClazzName+"."+operMethod);
135 | Map inMap=new HashMap();
136 | for (String preTaskId:preTaskList)
137 | {
138 | MgloryOut out=dag.getTaskResult().get(preTaskId);
139 | inMap.put(preTaskId,out);
140 | if (out.getStoreLocation().equals(MgloryOut.LOCAL_MEM) && !locationType.equals(MgloryOut.LOCAL_FILE)
141 | && execResource.equals(""))
142 | {
143 | execResource=out.getMap().get("locationAddress")!=null?out.getMap().get("locationAddress").toString():"";
144 | }
145 | else if (out.getStoreLocation().equals(MgloryOut.LOCAL_FILE) && !locationType.equals(MgloryOut.LOCAL_FILE))
146 | {
147 | if (out.getMap().get("locationAddress")!=null)
148 | {
149 | execResource=out.getMap().get("locationAddress").toString();
150 | locationType=MgloryOut.LOCAL_FILE;
151 | }
152 | }
153 | }
154 | in.setMap(inMap);
155 | in.setTimeout(timeout);
156 | return execute(in,execResource);
157 | }
158 | });
159 | futureTasks.add(futureTask);
160 | taskExecutor.submit(futureTask);
161 | }
162 | for (FutureTask futureTask : futureTasks) {
163 | try {
164 | if (!isInterrupted)
165 | {
166 | MgloryOut out=futureTask.get();
167 | if (out.getStatus().equals(MgloryOut.NORMAL))
168 | {
169 | dag.setTaskState(out.getTaskId(),TaskState.COMPLETE);
170 | dag.setTaskResult(out.getTaskId(),out);
171 | if (dag.getNextTaskMap().get(out.getTaskId())!=null && dag.getNextTaskMap().get(out.getTaskId()).get(0).equals("end"))
172 | {
173 | dag.setTaskResult("end",out);
174 | }
175 | execute(out.getTaskId());
176 | }
177 | else
178 | {
179 | dag.setTaskState(out.getTaskId(),TaskState.ERROR);
180 | dag.setTaskResult(out.getTaskId(),out);
181 | dag.setTaskResult("end",out);
182 | //终止任务执行
183 | if(!futureTask.isCancelled())
184 | {
185 | futureTask.cancel(true);
186 | }
187 | isInterrupted=true;
188 | }
189 | }
190 | else
191 | {
192 | //终止任务执行
193 | if(!futureTask.isCancelled())
194 | {
195 | futureTask.cancel(true);
196 | }
197 | }
198 |
199 | } catch (Exception e) {
200 | if (!isInterrupted)
201 | {
202 | MgloryOut out=new MgloryOut();
203 | out.setStatus(MgloryOut.EXCEPTION);
204 | out.setErrorMsg(e.getMessage());
205 | dag.setTaskResult("end",out);
206 | isInterrupted=true;
207 | log.warn("忽略执行任务异常:"+e);
208 | }
209 |
210 | }
211 | }
212 | //taskExecutor.shutdown();
213 | }
214 | }
215 | private void preDealForStartTaskNode(MgloryIn in)
216 | {
217 | dag.setTaskState("start",TaskState.COMPLETE);
218 | MgloryOut startOut =new MgloryOut();
219 | startOut.setTaskId("start");
220 | startOut.setMap(in.getMap());
221 | dag.setTaskResult("start", startOut);
222 | }
223 | private Map getNeedDealTask(String taskId)
224 | {
225 | Map needDealTaskMap=new HashMap ();
226 | //获取孩子节点
227 | List nextTaskList=dag.getNextTaskMap().get(taskId);
228 | if (nextTaskList!=null && nextTaskList.size()>0)
229 | {
230 | for (int i=0;i preTaskList=dag.getPreTaskMap().get(childTaskId);
234 | //判断是否可以执行
235 | boolean flag=true;
236 | for (int j=0;j program exit now.");
18 | System.exit(0);
19 | }
20 | String startType=args[0].trim();
21 | String lockName="";
22 | //集团服务
23 | if (startType.equals("group"))
24 | {
25 | try
26 | {
27 | MgloryGroupServer.getInstance().startGroupServer();
28 | NodeManager.getInstance().startHeart(true);
29 | }
30 | catch (Exception e)
31 | {
32 | System.err.println("start mgloryGroupServer failed, program exit now.");
33 | System.err.println(e);
34 | System.exit(0);
35 | }
36 | if (MgloryGroupServer.getInstance().getMasterFlag())
37 | {
38 | lockName="localhost_"+MgloryGroupServer.getInstance().getPort()+"_group_master";
39 | }
40 | else
41 | {
42 | lockName="localhost__"+MgloryGroupServer.getInstance().getPort()+"_group_slave";
43 | }
44 | }
45 | else
46 | {
47 | if (args.length < 4) {
48 | System.err.println("Usage:\tcom.lingo.mglory.server.StartMglory program exit now.");
49 | System.exit(0);
50 | }
51 | String ip=args[1].trim();
52 | String port=args[2].trim();
53 | String resourceType=args[3].trim();
54 | lockName="localhost__"+port+"_worker";
55 | try
56 | {
57 | MgloryWorkerServer.getInstance().startResource(ip, Integer.parseInt(port),resourceType);
58 | NodeManager.getInstance().startHeart(false);
59 | }
60 | catch (Exception e)
61 | {
62 | System.err.println("start mgloryWorkerServer failed, program exit now.");
63 | System.err.println(e);
64 | System.exit(0);
65 | }
66 | }
67 | // 根据当前输入参数的文件名生成lock锁文件
68 | File flock = new File("./lock", lockName + ".lock");
69 | if (flock.exists()) {
70 | System.out.println("Lock file " + flock.getName() + " \t exists already,program continue.");
71 | System.out.println("flock :" + flock.getAbsolutePath());
72 | } else {
73 | try {
74 | if (flock.createNewFile()) {
75 | System.out.println("Lock successfully created");
76 | } else {
77 | System.out.println("Create Lock file failed,but this program continue");
78 | }
79 | } catch (IOException e) {
80 | System.err.println("Create File Lock failed, program exit now.");
81 | System.err.println(e);
82 | System.exit(0);
83 | }
84 | }
85 | ScheduledExecutorService executorService = Executors.newScheduledThreadPool(1);
86 | ExitSystemExecutor exitSystemExecutor=new ExitSystemExecutor(flock);
87 | executorService.scheduleAtFixedRate(exitSystemExecutor, 1, 1, TimeUnit.SECONDS);
88 | System.out.println("mglory start complete......");
89 | }
90 | }
91 |
--------------------------------------------------------------------------------
/mglory/src/com/lingo/mglory/mq/MQReceiver.java:
--------------------------------------------------------------------------------
1 | package com.lingo.mglory.mq;
2 |
3 | import java.util.Map;
4 |
5 | import com.lingo.mglory.param.MgloryOut;
6 |
7 | public class MQReceiver{
8 | public MgloryOut receive(String taskId,Map inMap)throws Exception
9 | {
10 | MgloryOut out =new MgloryOut();
11 | MQStore.getInstance().msgQueue.add(inMap);
12 | return out;
13 | }
14 | }
15 |
--------------------------------------------------------------------------------
/mglory/src/com/lingo/mglory/mq/MQStore.java:
--------------------------------------------------------------------------------
1 | package com.lingo.mglory.mq;
2 |
3 | import java.util.HashMap;
4 | import java.util.Map;
5 | import java.util.Properties;
6 | import java.util.concurrent.Callable;
7 | import java.util.concurrent.ConcurrentHashMap;
8 | import java.util.concurrent.ConcurrentLinkedQueue;
9 | import java.util.concurrent.ConcurrentMap;
10 | import java.util.concurrent.ExecutorService;
11 | import java.util.concurrent.Executors;
12 | import java.util.concurrent.FutureTask;
13 | import java.util.concurrent.ScheduledExecutorService;
14 | import java.util.concurrent.TimeUnit;
15 |
16 | import org.apache.commons.logging.Log;
17 | import org.apache.commons.logging.LogFactory;
18 |
19 | import com.lingo.mglory.param.MgloryOut;
20 | import com.lingo.mglory.store.MgloryMap;
21 | import com.lingo.mglory.util.PropertiesUtil;
22 |
23 |
24 | public class MQStore {
25 | private static Log log = LogFactory.getLog(MQStore.class);
26 | private static MQStore mqStore;
27 | private Properties config= PropertiesUtil.getInstance().getConfig("config.properties");
28 | public ConcurrentLinkedQueue