Skip to content

Commit ffa0b8e

Browse files
committed
Merge pull request #162 from qq254963746/develop
增加 MasterSalveLoadBalance
2 parents bfbce49 + 93e23b0 commit ffa0b8e

File tree

6 files changed

+52
-17
lines changed

6 files changed

+52
-17
lines changed

lts-core/src/main/java/com/lts/core/json/JSON.java

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,22 @@
1111
*/
1212
public class JSON {
1313

14-
private static JSONAdapter aware = JSONFactory.getJSONAdapter();
14+
private static JSONAdapter adapter = JSONFactory.getJSONAdapter();
1515

1616
public static final <T> T parse(String json, Type type) {
1717
try {
1818
if (StringUtils.isEmpty(json)) {
1919
return null;
2020
}
21-
return aware.parse(json, type);
21+
return adapter.parse(json, type);
2222
} catch (Exception e) {
2323
throw new JSONException(e);
2424
}
2525
}
2626

2727
public static final <T> T parse(String json, TypeReference<T> typeReference) {
2828
try {
29-
return aware.parse(json, typeReference.getType());
29+
return adapter.parse(json, typeReference.getType());
3030
} catch (Exception e) {
3131
throw new JSONException(e);
3232
}
@@ -37,7 +37,7 @@ public static final String toJSONString(Object obj) {
3737
if (obj == null) {
3838
return null;
3939
}
40-
return aware.toJSONString(obj);
40+
return adapter.toJSONString(obj);
4141
} catch (Exception e) {
4242
throw new JSONException(e);
4343
}
@@ -48,7 +48,7 @@ public static final JSONObject toJSONObject(Object obj) {
4848
if (obj == null) {
4949
return null;
5050
}
51-
return aware.toJSONObject(obj);
51+
return adapter.toJSONObject(obj);
5252
} catch (Exception e) {
5353
throw new JSONException(e);
5454
}
@@ -59,71 +59,71 @@ public static final JSONArray toJSONArray(Object obj) {
5959
if (obj == null) {
6060
return null;
6161
}
62-
return aware.toJSONArray(obj);
62+
return adapter.toJSONArray(obj);
6363
} catch (Exception e) {
6464
throw new JSONException(e);
6565
}
6666
}
6767

6868
public static final JSONArray parseArray(String obj) {
6969
try {
70-
return aware.parseArray(obj);
70+
return adapter.parseArray(obj);
7171
} catch (Exception e) {
7272
throw new JSONException(e);
7373
}
7474
}
7575

7676
public static final JSONObject parseObject(String obj) {
7777
try {
78-
return aware.parseObject(obj);
78+
return adapter.parseObject(obj);
7979
} catch (Exception e) {
8080
throw new JSONException(e);
8181
}
8282
}
8383

8484
public static final JSONObject newJSONObject() {
8585
try {
86-
return aware.newJSONObject();
86+
return adapter.newJSONObject();
8787
} catch (Exception e) {
8888
throw new JSONException(e);
8989
}
9090
}
9191

9292
public static final JSONArray newJSONArray() {
9393
try {
94-
return aware.newJSONArray();
94+
return adapter.newJSONArray();
9595
} catch (Exception e) {
9696
throw new JSONException(e);
9797
}
9898
}
9999

100100
public static final JSONObject newJSONObject(Map<String, Object> map) {
101101
try {
102-
return aware.newJSONObject(map);
102+
return adapter.newJSONObject(map);
103103
} catch (Exception e) {
104104
throw new JSONException(e);
105105
}
106106
}
107107

108108
public static final JSONObject newJSONObject(int initialCapacity) {
109109
try {
110-
return aware.newJSONObject(initialCapacity);
110+
return adapter.newJSONObject(initialCapacity);
111111
} catch (Exception e) {
112112
throw new JSONException(e);
113113
}
114114
}
115115

116116
public static final JSONArray newJSONArray(List<Object> list) {
117117
try {
118-
return aware.newJSONArray(list);
118+
return adapter.newJSONArray(list);
119119
} catch (Exception e) {
120120
throw new JSONException(e);
121121
}
122122
}
123123

124124
public static final JSONArray newJSONArray(int initialCapacity) {
125125
try {
126-
return aware.newJSONArray(initialCapacity);
126+
return adapter.newJSONArray(initialCapacity);
127127
} catch (Exception e) {
128128
throw new JSONException(e);
129129
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.lts.core.loadbalance;
2+
3+
import com.lts.core.cluster.Node;
4+
5+
import java.util.Collections;
6+
import java.util.Comparator;
7+
import java.util.List;
8+
9+
/**
10+
* 主从模式: 譬如将JobClient和TaskTracker设置为这种负载均衡模式,
11+
* 那么总会去连接最老的一台JobTracker,而从达到主从模式的效果
12+
*
13+
* @author Robert HG (254963746@qq.com) on 11/21/15.
14+
*/
15+
public class MasterSalveLoadBalance extends AbstractLoadBalance {
16+
17+
@Override
18+
protected <S> S doSelect(List<S> shards, String seed) {
19+
20+
if (shards.get(0) instanceof Node) {
21+
Collections.sort(shards, new Comparator<S>() {
22+
@Override
23+
public int compare(S left, S right) {
24+
return ((Node) left).getCreateTime().compareTo(((Node) right).getCreateTime());
25+
}
26+
});
27+
}
28+
29+
return shards.get(0);
30+
}
31+
}

lts-jobtracker/src/main/java/com/lts/jobtracker/complete/chain/GetNewJobChain.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
import com.lts.remoting.protocol.RemotingProtos;
1111

1212
/**
13+
* 接受新任务Chain
14+
*
1315
* @author Robert HG (254963746@qq.com) on 11/11/15.
1416
*/
1517
public class GetNewJobChain implements JobCompletedChain {

lts-jobtracker/src/main/java/com/lts/jobtracker/complete/chain/JobProcessChain.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.util.List;
2121

2222
/**
23+
* 任务完成 Chian
2324
* @author Robert HG (254963746@qq.com) on 11/11/15.
2425
*/
2526
public class JobProcessChain implements JobCompletedChain {

lts-jobtracker/src/main/java/com/lts/jobtracker/complete/chain/JobStatisticChain.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.util.List;
2020

2121
/**
22+
* 任务数据统计 Chain
2223
* @author Robert HG (254963746@qq.com) on 11/11/15.
2324
*/
2425
public class JobStatisticChain implements JobCompletedChain {

lts-jobtracker/src/main/java/com/lts/jobtracker/processor/JobCompletedProcessor.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ public JobCompletedProcessor(final JobTrackerApplication application) {
2626
super(application);
2727

2828
this.chains = new CopyOnWriteArrayList<JobCompletedChain>();
29-
this.chains.add(new JobStatisticChain(application));
30-
this.chains.add(new JobProcessChain(application));
31-
this.chains.add(new GetNewJobChain(application));
29+
this.chains.add(new JobStatisticChain(application)); // 统计
30+
this.chains.add(new JobProcessChain(application)); // 完成处理
31+
this.chains.add(new GetNewJobChain(application)); // 获取新任务
3232

3333
}
3434

0 commit comments

Comments
 (0)