Skip to content

Commit 8f67e39

Browse files
author
yang.guo
authored
Merge pull request #313 from FlowCI/feature/lombok
Feature/lombok
2 parents a9d0f31 + 7d6f65e commit 8f67e39

File tree

156 files changed

+1321
-4028
lines changed

Some content is hidden

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

156 files changed

+1321
-4028
lines changed

platform-agent/pom.xml

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,6 @@
5656
<artifactId>curator-recipes</artifactId>
5757
</dependency>
5858

59-
<dependency>
60-
<groupId>com.google.guava</groupId>
61-
<artifactId>guava</artifactId>
62-
</dependency>
63-
6459
<dependency>
6560
<groupId>com.google.code.gson</groupId>
6661
<artifactId>gson</artifactId>
@@ -76,6 +71,12 @@
7671
<artifactId>httpmime</artifactId>
7772
</dependency>
7873

74+
<dependency>
75+
<groupId>org.projectlombok</groupId>
76+
<artifactId>lombok</artifactId>
77+
<scope>provided</scope>
78+
</dependency>
79+
7980
<dependency>
8081
<groupId>junit</groupId>
8182
<artifactId>junit</artifactId>

platform-agent/src/main/java/com/flow/platform/agent/AgentManager.java

Lines changed: 25 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,12 @@
1818

1919
import com.flow.platform.domain.Cmd;
2020
import com.flow.platform.domain.Jsonable;
21-
import com.flow.platform.util.Logger;
2221
import com.flow.platform.util.zk.ZKClient;
2322
import java.io.IOException;
2423
import java.util.LinkedList;
2524
import java.util.List;
25+
import lombok.Getter;
26+
import lombok.extern.log4j.Log4j2;
2627
import org.apache.curator.framework.CuratorFramework;
2728
import org.apache.curator.framework.recipes.cache.ChildData;
2829
import org.apache.curator.framework.recipes.cache.TreeCacheEvent;
@@ -33,49 +34,32 @@
3334
/**
3435
3536
*/
37+
@Log4j2
3638
public class AgentManager implements Runnable, TreeCacheListener, AutoCloseable {
3739

38-
private final static Logger LOGGER = new Logger(AgentManager.class);
39-
4040
// Zk root path /flow-agents/{zone}/{name}
4141
private final static Object STATUS_LOCKER = new Object();
4242

4343
private final static int ZK_RECONNECT_TIME = 1;
44-
private final static int ZK_RETRY_PERIOD = 500;
4544

46-
private String zkHost;
47-
private int zkTimeout;
48-
private ZKClient zkClient;
45+
private final static int ZK_RETRY_PERIOD = 500;
4946

50-
private String zone; // agent running zone
51-
private String name; // agent name, can be machine name
47+
@Getter
48+
private final ZKClient zkClient;
5249

53-
private String zonePath; // zone path, /flow-agents/{zone}
54-
private String nodePath; // zk node path, /flow-agents/{zone}/{name}
50+
@Getter
51+
private final String zonePath; // zone path, /flow-agents/{zone}
5552

56-
private List<Cmd> cmdHistory = new LinkedList<>();
53+
@Getter
54+
private final String nodePath; // zk node path, /flow-agents/{zone}/{name}
5755

58-
public AgentManager(String zkHost, int zkTimeout, String zone, String name) throws IOException {
59-
this.zkHost = zkHost;
60-
this.zkTimeout = zkTimeout;
56+
@Getter
57+
private final List<Cmd> cmdHistory = new LinkedList<>();
6158

59+
public AgentManager(String zkHost, int zkTimeout, String zone, String name) {
6260
this.zkClient = new ZKClient(zkHost, ZK_RETRY_PERIOD, ZK_RECONNECT_TIME);
63-
this.zone = zone;
64-
this.name = name;
65-
this.zonePath = ZKPaths.makePath(Config.ZK_ROOT, this.zone);
66-
this.nodePath = ZKPaths.makePath(this.zonePath, this.name);
67-
}
68-
69-
public ZKClient getZkClient() {
70-
return zkClient;
71-
}
72-
73-
public String getNodePath() {
74-
return nodePath;
75-
}
76-
77-
public List<Cmd> getCmdHistory() {
78-
return cmdHistory;
61+
this.zonePath = ZKPaths.makePath(Config.ZK_ROOT, zone);
62+
this.nodePath = ZKPaths.makePath(this.zonePath, name);
7963
}
8064

8165
/**
@@ -98,56 +82,38 @@ public void run() {
9882
try {
9983
STATUS_LOCKER.wait();
10084
} catch (InterruptedException e) {
101-
LOGGER.warn("InterruptedException : " + e.getMessage());
85+
log.warn("InterruptedException : " + e.getMessage());
10286
}
10387
}
10488
}
10589

10690
private void exit() {
107-
LOGGER.info("One Agent is running in other place. Please first to stop another agent, thx!");
91+
log.info("One Agent is running in other place. Please first to stop another agent, thx!");
10892
Runtime.getRuntime().exit(1);
10993
}
11094

11195
@Override
112-
public void childEvent(CuratorFramework client, TreeCacheEvent event) throws Exception {
96+
public void childEvent(CuratorFramework client, TreeCacheEvent event) {
11397
ChildData eventData = event.getData();
98+
log.trace("========= Event: {} =========", event.getType());
11499

115100
if (event.getType() == Type.CONNECTION_RECONNECTED) {
116-
LOGGER.traceMarker("ZK-Event", "========= Reconnect =========");
117101
registerZkNodeAndWatch();
118102
return;
119103
}
120104

121-
if (event.getType() == Type.CONNECTION_LOST) {
122-
LOGGER.traceMarker("ZK-Event", "========= Lost =========");
123-
return;
124-
}
125-
126-
if (event.getType() == Type.INITIALIZED) {
127-
LOGGER.traceMarker("ZK-Event", "========= Initialized =========");
128-
return;
129-
}
130-
131-
if (event.getType() == Type.NODE_ADDED) {
132-
LOGGER.traceMarker("ZK-Event", "========= Node Added: %s =========", eventData.getPath());
133-
return;
134-
}
135-
136105
if (event.getType() == Type.NODE_UPDATED) {
137-
LOGGER.traceMarker("ZK-Event", "========= Node Updated: %s =========", eventData.getPath());
138106
onDataChanged(eventData.getPath());
139107
return;
140108
}
141109

142110
if (event.getType() == Type.NODE_REMOVED) {
143-
LOGGER.traceMarker("ZK-Event", "========= Node Removed: %s =========", eventData.getPath());
144111
close();
145-
return;
146112
}
147113
}
148114

149115
@Override
150-
public void close() throws IOException {
116+
public void close() {
151117
removeZkNode();
152118
stop();
153119
}
@@ -158,7 +124,7 @@ public void close() throws IOException {
158124
private void onDeleted() {
159125
try {
160126
CmdManager.getInstance().shutdown(null);
161-
LOGGER.trace("========= Agent been deleted =========");
127+
log.trace("========= Agent been deleted =========");
162128

163129
stop();
164130
} finally {
@@ -172,22 +138,22 @@ private void onDataChanged(String path) {
172138
try {
173139
final byte[] rawData = zkClient.getData(path);
174140
if (rawData == null) {
175-
LOGGER.warn("Zookeeper node data is null");
141+
log.warn("Zookeeper node data is null");
176142
return;
177143
}
178144

179145
cmd = Jsonable.parse(rawData, Cmd.class);
180146
if (cmd == null) {
181-
LOGGER.warn("Unable to parse cmd from zk node: " + new String(rawData));
147+
log.warn("Unable to parse cmd from zk node: " + new String(rawData));
182148
return;
183149
}
184150

185151
cmdHistory.add(cmd);
186-
LOGGER.trace("Received command: " + cmd.toString());
152+
log.trace("Received command: " + cmd.toString());
187153
CmdManager.getInstance().execute(cmd);
188154

189155
} catch (Throwable e) {
190-
LOGGER.error("Invalid cmd from server", e);
156+
log.error("Invalid cmd from server", e);
191157
// TODO: should report agent status directly...
192158
}
193159
}

platform-agent/src/main/java/com/flow/platform/agent/App.java

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,14 @@
1616

1717
package com.flow.platform.agent;
1818

19-
import com.flow.platform.util.Logger;
20-
import java.io.IOException;
19+
import lombok.extern.log4j.Log4j2;
2120

2221
/**
2322
2423
*/
24+
@Log4j2
2525
public class App {
2626

27-
private final static Logger LOGGER = new Logger(App.class);
28-
2927
private static AgentManager agentManager;
3028

3129
public static void main(String args[]) {
@@ -42,38 +40,38 @@ public static void main(String args[]) {
4240
token = args[1];
4341
}
4442

45-
LOGGER.trace("========= Flow Agent Started =========");
46-
LOGGER.trace("=== Server: " + baseUrl);
47-
LOGGER.trace("=== Token: " + token);
43+
log.trace("========= Flow Agent Started =========");
44+
log.trace("=== Server: " + baseUrl);
45+
log.trace("=== Token: " + token);
4846

4947
Runtime.getRuntime().addShutdownHook(new ShutdownHook());
5048

5149
try {
52-
LOGGER.trace("=== Start to load configuration");
50+
log.trace("=== Start to load configuration");
5351

5452
Config.AGENT_SETTINGS = Config.loadAgentConfig(baseUrl, token);
55-
LOGGER.trace("====== Settings: %s", Config.agentSettings());
53+
log.trace("====== Settings: {}", Config.agentSettings());
5654

5755
Config.ZK_URL = Config.AGENT_SETTINGS.getZookeeperUrl();
58-
LOGGER.trace("====== Zookeeper host: %s", Config.zkUrl());
56+
log.trace("====== Zookeeper host: {}", Config.zkUrl());
5957

6058
Config.ZONE = Config.AGENT_SETTINGS.getAgentPath().getZone();
61-
LOGGER.trace("====== Working zone: %s", Config.zone());
59+
log.trace("====== Working zone: {}", Config.zone());
6260

6361
Config.NAME = Config.AGENT_SETTINGS.getAgentPath().getName();
64-
LOGGER.trace("====== Agent agent: %s", Config.name());
62+
log.trace("====== Agent agent: {}", Config.name());
6563

66-
LOGGER.trace("========= Config initialized =========");
64+
log.trace("========= Config initialized =========");
6765
} catch (Throwable e) {
68-
LOGGER.error("Cannot load agent config from zone", e);
66+
log.error("Cannot load agent config from zone", e);
6967
Runtime.getRuntime().exit(1);
7068
}
7169

7270
try {
7371
agentManager = new AgentManager(Config.zkUrl(), Config.zkTimeout(), Config.zone(), Config.name());
7472
new Thread(agentManager).start();
7573
} catch (Throwable e) {
76-
LOGGER.error("Got exception when agent running", e);
74+
log.error("Got exception when agent running", e);
7775
Runtime.getRuntime().exit(1);
7876
}
7977
}
@@ -83,15 +81,11 @@ private static class ShutdownHook extends Thread {
8381
@Override
8482
public void run() {
8583
if (agentManager != null) {
86-
try {
87-
agentManager.close();
88-
} catch (IOException ignore) {
89-
90-
}
84+
agentManager.close();
9185
}
9286

93-
LOGGER.trace("========= Agent end =========");
94-
LOGGER.trace("========= JVM EXIT =========");
87+
log.trace("========= Agent end =========");
88+
log.trace("========= JVM EXIT =========");
9589
}
9690
}
9791
}

platform-agent/src/main/java/com/flow/platform/agent/CmdManager.java

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import com.flow.platform.domain.CmdStatus;
2626
import com.flow.platform.domain.CmdType;
2727
import com.flow.platform.domain.Jsonable;
28-
import com.flow.platform.util.Logger;
2928
import com.google.common.collect.Lists;
3029
import com.google.common.collect.Maps;
3130
import java.time.ZonedDateTime;
@@ -39,17 +38,17 @@
3938
import java.util.concurrent.ThreadFactory;
4039
import java.util.concurrent.ThreadPoolExecutor;
4140
import java.util.concurrent.TimeUnit;
41+
import lombok.extern.log4j.Log4j2;
4242

4343
/**
4444
* Singleton class to handle command
4545
* <p>
4646
*
4747
4848
*/
49+
@Log4j2
4950
public class CmdManager {
5051

51-
private final static Logger LOGGER = new Logger(CmdManager.class);
52-
5352
private final static CmdManager INSTANCE = new CmdManager();
5453

5554
public static CmdManager getInstance() {
@@ -129,12 +128,12 @@ public void shutdown(String password) {
129128
}
130129

131130
if (password == null) {
132-
LOGGER.trace("Shutdown cannot be executed since sudo password is null");
131+
log.trace("Shutdown cannot be executed since sudo password is null");
133132
return;
134133
}
135134

136135
String shutdownCmd = String.format("echo %s | sudo -S shutdown -h now", password);
137-
LOGGER.trace("Shutdown command: " + shutdownCmd);
136+
log.trace("Shutdown command: " + shutdownCmd);
138137

139138
// exec shutdown command
140139
CmdExecutor executor = new CmdExecutor(null, Lists.newArrayList(shutdownCmd));
@@ -155,7 +154,7 @@ public void execute(final Cmd cmd) {
155154
// check max concurrent proc
156155
int max = cmdExecutor.getMaximumPoolSize();
157156
int cur = cmdExecutor.getActiveCount();
158-
LOGGER.trace(" ===== CmdExecutor: max=%s, current=%s =====", max, cur);
157+
log.trace(" ===== CmdExecutor: max={}, current={} =====", max, cur);
159158

160159
// reach max proc number, reject this execute
161160
if (max == cur) {
@@ -166,7 +165,7 @@ public void execute(final Cmd cmd) {
166165
cmdExecutor.execute(new TaskRunner(cmd) {
167166
@Override
168167
public void run() {
169-
LOGGER.debug("start cmd ...");
168+
log.debug("start cmd ...");
170169

171170
LogEventHandler logListener = new LogEventHandler(getCmd());
172171

@@ -185,7 +184,7 @@ public void run() {
185184

186185
executor.run();
187186
} catch (Throwable e) {
188-
LOGGER.errorMarker("execute", "Cannot init CmdExecutor for cmd " + cmd, e);
187+
log.error("Cannot init CmdExecutor for cmd: " + cmd, e);
189188
CmdResult result = new CmdResult();
190189
result.getExceptions().add(e);
191190
procEventHandler.onException(result);
@@ -268,7 +267,7 @@ public synchronized void kill() {
268267
r.getProcess().destroy();
269268

270269
ReportManager.getInstance().cmdReportSync(cmd.getId(), CmdStatus.KILLED, r);
271-
LOGGER.trace("Kill process : %s", r.toString());
270+
log.trace("Kill process : {}", r.toString());
272271
}
273272

274273
try {
@@ -277,7 +276,7 @@ public synchronized void kill() {
277276

278277
} finally {
279278
cmdExecutor = createExecutor(); // reset cmd executor
280-
LOGGER.trace("Cmd thread terminated");
279+
log.trace("Cmd thread terminated");
281280
}
282281
}
283282

@@ -292,7 +291,7 @@ private void onReject(final Cmd cmd) {
292291

293292
rejected.put(cmd, rejectResult);
294293
ReportManager.getInstance().cmdReportSync(cmd.getId(), CmdStatus.REJECTED, null);
295-
LOGGER.warn("Reject cmd '%s' since over the limit proc of agent", cmd.getId());
294+
log.warn("Reject cmd '{}' since over the limit proc of agent", cmd.getId());
296295
}
297296

298297
private ThreadPoolExecutor createExecutor() {
@@ -307,7 +306,7 @@ private ThreadPoolExecutor createExecutor() {
307306
if (r instanceof TaskRunner) {
308307
TaskRunner task = (TaskRunner) r;
309308
onReject(task.getCmd());
310-
LOGGER.warn("Reject cmd: %s", task.getCmd());
309+
log.warn("Reject cmd: {}", task.getCmd());
311310
}
312311
});
313312
}

0 commit comments

Comments
 (0)