Skip to content

Commit f0e4534

Browse files
author
Yang Guo
committed
simplify agent manager
1 parent ebab37d commit f0e4534

File tree

3 files changed

+17
-144
lines changed

3 files changed

+17
-144
lines changed

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

Lines changed: 15 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.io.IOException;
2323
import java.util.LinkedList;
2424
import java.util.List;
25+
import lombok.Getter;
2526
import lombok.extern.log4j.Log4j2;
2627
import org.apache.curator.framework.CuratorFramework;
2728
import org.apache.curator.framework.recipes.cache.ChildData;
@@ -40,41 +41,25 @@ public class AgentManager implements Runnable, TreeCacheListener, AutoCloseable
4041
private final static Object STATUS_LOCKER = new Object();
4142

4243
private final static int ZK_RECONNECT_TIME = 1;
43-
private final static int ZK_RETRY_PERIOD = 500;
4444

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

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

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

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

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

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

8065
/**
@@ -108,38 +93,21 @@ private void exit() {
10893
}
10994

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

114100
if (event.getType() == Type.CONNECTION_RECONNECTED) {
115-
log.trace("========= Reconnect =========");
116101
registerZkNodeAndWatch();
117102
return;
118103
}
119104

120-
if (event.getType() == Type.CONNECTION_LOST) {
121-
log.trace("========= Lost =========");
122-
return;
123-
}
124-
125-
if (event.getType() == Type.INITIALIZED) {
126-
log.trace("========= Initialized =========");
127-
return;
128-
}
129-
130-
if (event.getType() == Type.NODE_ADDED) {
131-
log.trace("========= Node Added: {} =========", eventData.getPath());
132-
return;
133-
}
134-
135105
if (event.getType() == Type.NODE_UPDATED) {
136-
log.trace("========= Node Updated: {} =========", eventData.getPath());
137106
onDataChanged(eventData.getPath());
138107
return;
139108
}
140109

141110
if (event.getType() == Type.NODE_REMOVED) {
142-
log.trace("========= Node Removed: {} =========", eventData.getPath());
143111
close();
144112
}
145113
}

platform-agent/src/test/java/com/flow/platform/agent/test/AgentManagerTest.java

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,8 @@
2121
import com.flow.platform.domain.Cmd;
2222
import com.flow.platform.domain.CmdType;
2323
import com.flow.platform.util.zk.ZKClient;
24-
import com.flow.platform.util.zk.ZkException;
25-
import java.util.concurrent.ArrayBlockingQueue;
26-
import java.util.concurrent.CountDownLatch;
27-
import java.util.concurrent.ThreadPoolExecutor;
28-
import java.util.concurrent.TimeUnit;
29-
import java.util.concurrent.atomic.AtomicInteger;
3024
import org.apache.curator.test.TestingServer;
3125
import org.apache.curator.utils.ZKPaths;
32-
import org.apache.zookeeper.ZKUtil;
3326
import org.junit.After;
3427
import org.junit.AfterClass;
3528
import org.junit.Assert;
@@ -89,13 +82,13 @@ public void should_agent_registered() throws Throwable {
8982
public void should_receive_command() throws Throwable {
9083
AgentManager agent = new AgentManager(server.getConnectString(), 20000, ZONE, MACHINE);
9184
new Thread(agent).start();
92-
Thread.sleep(5000); // waitting for node created
85+
Thread.sleep(5000); // waiting for node created
9386

9487
// when: send command to agent
9588
Cmd cmd = new Cmd(ZONE, MACHINE, CmdType.RUN_SHELL, "echo hello");
9689
cmd.setId("mock-cmd-id");
9790
zkClient.setData(agent.getNodePath(), cmd.toBytes());
98-
Thread.sleep(2000); // waitting for cmd recieved
91+
Thread.sleep(2000); // waiting for cmd received
9992

10093
// then: check agent status when command received
10194
Assert.assertEquals(1, agent.getCmdHistory().size());

platform-util/src/main/java/com/flow/platform/util/Logger.java

Lines changed: 0 additions & 88 deletions
This file was deleted.

0 commit comments

Comments
 (0)