Skip to content

Commit 51c314c

Browse files
authored
Merge pull request #240 from codeine-cd/codeine-239
codeine-239 peer should not change address during up time
2 parents 34fe890 + 78a46ef commit 51c314c

File tree

6 files changed

+180
-191
lines changed

6 files changed

+180
-191
lines changed

src/common/codeine/api/NodeWithPeerInfo.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public NodeWithPeerInfo(String name, String alias, PeerStatusJsonV2 peer) {
1515
super(name, alias);
1616
this.peer = peer;
1717
if (null != peer) {
18-
peer_host_port = peer.host_port();
18+
peer_host_port = peer.canonical_host_port();
1919
peer_address = peer.address_port();
2020
peer_key = peer.key();
2121
peer_status = peer.status();
@@ -32,7 +32,7 @@ public PeerStatusJsonV2 peer() {
3232

3333
public void peer(PeerStatusJsonV2 peer) {
3434
this.peer = peer;
35-
peer_host_port = peer.host_port();
35+
peer_host_port = peer.canonical_host_port();
3636
peer_address = peer.address_port();
3737
peer_key = peer.key();
3838
peer_status = peer.status();

src/common/codeine/db/mysql/connectors/StatusMysqlConnector.java

Lines changed: 24 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public void createTables() {
6060
public void putReplaceStatus(PeerStatusJsonV2 p) {
6161
String json = gson.toJson(p);
6262
log.info("will update status to " + dbUtils.server() + "\n" + json);
63-
dbUtils.executeUpdate("DELETE FROM "+TABLE_NAME+" WHERE peer_key = '" + p.peer_old_key() + "'");
63+
dbUtils.executeUpdate("DELETE FROM "+TABLE_NAME+" WHERE peer_key = '" + p.peer_key() + "'");
6464
dbUtils.executeUpdate("REPLACE INTO "+TABLE_NAME+" (peer_key, data, update_time ) VALUES (?, ?, CURRENT_TIMESTAMP())", p.peer_key(), json);
6565
}
6666

@@ -93,39 +93,35 @@ private void updateNodesWithPeer(PeerStatusJsonV2 peerStatus) {
9393
public void updatePeersStatus(final long timeToRemove, final long timeToDisc) {
9494
final List<String> idToRemove = Lists.newArrayList();
9595
final List<String> idToDisc = Lists.newArrayList();
96-
Function<ResultSet, Void> function = new Function<ResultSet, Void>() {
97-
@Override
98-
public Void apply(ResultSet rs){
99-
try {
100-
String key = rs.getString("peer_key");
96+
Function<ResultSet, Void> function = rs -> {
97+
try {
98+
String key = rs.getString("peer_key");
10199
// PeerStatusString status = PeerStatusString.valueOf(rs.getString("status"));
102-
String value = rs.getString("data");
103-
String status = rs.getString("status");
104-
PeerStatusJsonV2 peerStatus = gson.fromJson(value, PeerStatusJsonV2.class);
105-
PeerType peerType = peerStatus.peer_type();
106-
long timeToRemovePeer = peerType == PeerType.Reporter ? timeToRemove + TimeUnit.DAYS.toMinutes(7) : timeToRemove;
107-
long timeToDiscPeer = peerType == PeerType.Reporter ? timeToDisc + TimeUnit.DAYS.toMinutes(7) : timeToDisc;
108-
long timeDiff = rs.getLong("TIME_DIFF");
109-
log.debug("time diff is " + timeDiff);
110-
if (timeDiff > timeToRemovePeer){
111-
log.info("time diff is " + timeDiff);
112-
log.info("deleting " + peerStatus);
100+
String value = rs.getString("data");
101+
String status = rs.getString("status");
102+
PeerStatusJsonV2 peerStatus = gson.fromJson(value, PeerStatusJsonV2.class);
103+
PeerType peerType = peerStatus.peer_type();
104+
long timeToRemovePeer = peerType == PeerType.Reporter ? timeToRemove + TimeUnit.DAYS.toMinutes(7) : timeToRemove;
105+
long timeToDiscPeer = peerType == PeerType.Reporter ? timeToDisc + TimeUnit.DAYS.toMinutes(7) : timeToDisc;
106+
long timeDiff = rs.getLong("TIME_DIFF");
107+
log.debug("time diff is " + timeDiff);
108+
if (timeDiff > timeToRemovePeer){
109+
log.info("time diff is " + timeDiff);
110+
log.info("deleting " + peerStatus);
113111
// rs.deleteRow();
114-
idToRemove.add(key);
115-
}
116-
else if (timeDiff > timeToDiscPeer && !status.equals(PeerStatusString.Disc.toString())){
117-
log.info("time diff is " + timeDiff);
118-
log.info("update to disc " + peerStatus);
119-
idToDisc.add(key);
112+
idToRemove.add(key);
113+
}
114+
else if (timeDiff > timeToDiscPeer && !status.equals(PeerStatusString.Disc.toString())){
115+
log.info("time diff is " + timeDiff);
116+
log.info("update to disc " + peerStatus);
117+
idToDisc.add(key);
120118
// rs.updateString("status", "Disc");
121119
// rs.updateRow();
122-
}
123-
return null;
124-
} catch (SQLException e) {
125-
throw ExceptionUtils.asUnchecked(e);
126120
}
121+
return null;
122+
} catch (SQLException e) {
123+
throw ExceptionUtils.asUnchecked(e);
127124
}
128-
129125
};
130126
dbUtils.executeUpdateableQuery("select *,TIMESTAMPDIFF(MINUTE,update_time,CURRENT_TIMESTAMP()) as TIME_DIFF from " + TABLE_NAME, function);
131127
if (webConfJsonStore.get().readonly_web_server()) {

src/common/codeine/jsons/peer_status/PeerStatus.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ public class PeerStatus {
2626
private static final Logger log = Logger.getLogger(PeerStatus.class);
2727

2828
private Map<String, ProjectStatus> project_name_to_status = Maps.newConcurrentMap();
29+
private String canonical_host_name = InetUtils.getLocalHost().getCanonicalHostName();
30+
private String host_address = InetUtils.getLocalHost().getHostAddress();
2931

3032
@Inject
3133
private CodeineRuntimeInfo codeineRuntimeInfo;
@@ -96,9 +98,9 @@ public Map<String, ProjectStatus> project_name_to_status() {
9698
}
9799

98100
public PeerStatusJsonV2 createJson() {
99-
return new PeerStatusJsonV2(InetUtils.getLocalHost().getHostName(), codeineRuntimeInfo.port(),
101+
return new PeerStatusJsonV2(codeineRuntimeInfo.port(),
100102
codeineRuntimeInfo.version(), codeineRuntimeInfo.startTime(), Constants.getInstallDir(),
101-
PathHelper.getTarFile(), project_name_to_status(), InetUtils.getLocalHost().getHostAddress(), System.getProperty("DNS_DOMAIN_NAME"), InetUtils.getLocalHost().getCanonicalHostName());
103+
PathHelper.getTarFile(), project_name_to_status(), host_address, System.getProperty("DNS_DOMAIN_NAME"), canonical_host_name);
102104
}
103105

104106
public String updateVersion(ProjectJson project, String node, String alias, String version) {

src/common/codeine/jsons/peer_status/PeerStatusJsonV2.java

Lines changed: 137 additions & 135 deletions
Original file line numberDiff line numberDiff line change
@@ -16,139 +16,141 @@
1616

1717
@SuppressWarnings("unused")
1818
public class PeerStatusJsonV2 {
19-
private String peer_key;
20-
//TODO remove after cf-engine in build > 1.1.309
21-
private String peer_old_key;
22-
private String peer_host_port;
23-
private String peer_ip;
24-
private String user_dns_domain;
25-
private Map<String, ProjectStatus> project_name_to_status = Maps.newHashMap();//Lists.newArrayList();
26-
private String host;
27-
private String canonical_host; //TODO introduced in codeine 1202, can be used after cfengine is in that build
28-
private int port;
29-
private String version;
30-
private String tar;
31-
private long start_time;
32-
private long update_time;//updated in directory server when first seen
33-
private long update_time_from_peer;
34-
private String install_dir;
35-
private PeerType peer_type;
36-
private transient PeerStatusString status;
37-
38-
public PeerStatusJsonV2(String host, int port, String version, long start_time, String install_dir, String tar, Map<String, ProjectStatus> project_name_to_status, String peer_ip, String user_dns_domain, String canonical_host) {
39-
super();
40-
this.host = host;
41-
this.canonical_host = canonical_host;
42-
this.port = port;
43-
this.peer_ip = peer_ip;
44-
this.version = version;
45-
this.start_time = start_time;
46-
this.install_dir = install_dir;
47-
this.tar = tar;
48-
this.project_name_to_status = Maps.newHashMap(project_name_to_status);
49-
this.peer_old_key = host + ":" + install_dir;
50-
this.peer_key = host + ":" + HttpUtils.specialEncode(install_dir);
51-
this.peer_host_port = host + ":" + port;
52-
this.user_dns_domain = user_dns_domain;
53-
this.peer_type = PeerType.Daemon;
54-
this.project_name_to_status.put(Constants.CODEINE_NODES_PROJECT_NAME, createInternalProject());
55-
this.update_time = System.currentTimeMillis();
56-
this.update_time_from_peer = System.currentTimeMillis();
57-
}
58-
private ProjectStatus createInternalProject() {
59-
NodeWithMonitorsInfo node_info = new NodeWithMonitorsInfo(this, this.peer_key, this.host, Constants.CODEINE_NODES_PROJECT_NAME, Maps.<String, MonitorStatusInfo>newHashMap());
60-
node_info.version(this.version);
61-
node_info.tags(Lists.newArrayList(project_name_to_status.keySet()));
62-
ProjectStatus ps = new ProjectStatus(Constants.CODEINE_NODES_PROJECT_NAME, node_info);
63-
return ps;
64-
}
65-
public PeerStatusJsonV2(String peer_key, ProjectStatus projectStatus) {
66-
super();
67-
this.project_name_to_status = Maps.newHashMap();
68-
this.project_name_to_status.put(projectStatus.project_name(), projectStatus);
69-
this.update_time = System.currentTimeMillis();
70-
this.update_time_from_peer = System.currentTimeMillis();
71-
this.peer_key = peer_key;
72-
this.peer_type = PeerType.Reporter;
73-
}
74-
75-
public void addProjectStatus(String name, ProjectStatus status) {
76-
HashMap<String, ProjectStatus> tempList = Maps.newHashMap(project_name_to_status);
77-
tempList.put(name, status);
78-
project_name_to_status = tempList;
79-
}
80-
81-
public Map<String, ProjectStatus> project_name_to_status() {
82-
return Collections.unmodifiableMap(project_name_to_status);
83-
}
84-
85-
public String peer_key() {
86-
return peer_key;
87-
}
88-
89-
public String host_port() {
90-
return host + ":" + port;
91-
}
92-
public String canonical_host_port() {
93-
return canonical_host + ":" + port;
94-
}
95-
public String ip_port() {
96-
return peer_ip + ":" + port;
97-
}
98-
99-
public String address_port() {
100-
if (!StringUtils.isEmpty(user_dns_domain)) {
101-
return host + "." + user_dns_domain + ":" + port;
102-
} else if (!StringUtils.isEmpty(canonical_host)) {
103-
return canonical_host_port();
104-
} else {
105-
return host_port();
106-
}
107-
}
108-
109-
public long update_time() {
110-
return update_time;
111-
}
112-
public long update_time_from_peer() {
113-
return update_time_from_peer;
114-
}
115-
116-
public String key() {
117-
return peer_key();
118-
}
119-
120-
public String version() {
121-
return version;
122-
}
123-
124-
public String host() {
125-
return host;
126-
}
127-
128-
public String tar() {
129-
return tar;
130-
}
131-
public void status(PeerStatusString status) {
132-
this.status = status;
133-
}
134-
public PeerStatusString status() {
135-
return status;
136-
}
137-
public void updateNodesWithPeer() {
138-
for (ProjectStatus projectStatus : project_name_to_status.values()) {
139-
projectStatus.updateNodesWithPeer(this);
140-
}
141-
}
142-
public PeerType peer_type() {
143-
return peer_type;
144-
}
145-
public String peer_old_key() {
146-
return peer_old_key;
147-
}
148-
@Override
149-
public String toString() {
150-
return "PeerStatusJsonV2 [host_port()=" + host_port() + ", update_time()=" + new Date(update_time())
151-
+ ", update_time_from_peer()=" + new Date(update_time_from_peer()) + ", peer_type()=" + peer_type() + "]";
152-
}
153-
19+
20+
private String peer_key;
21+
private String peer_host_port;
22+
private String peer_ip;
23+
private String user_dns_domain;
24+
private Map<String, ProjectStatus> project_name_to_status;
25+
private String canonical_host;
26+
private int port;
27+
private String version;
28+
private String tar;
29+
private long start_time;
30+
private long update_time;//updated in directory server when first seen
31+
private long update_time_from_peer;
32+
private String install_dir;
33+
private PeerType peer_type;
34+
private transient PeerStatusString status;
35+
36+
public PeerStatusJsonV2(int port, String version, long start_time,
37+
String install_dir, String tar, Map<String, ProjectStatus> project_name_to_status,
38+
String peer_ip, String user_dns_domain, String canonical_host) {
39+
super();
40+
this.canonical_host = canonical_host;
41+
this.port = port;
42+
this.peer_ip = peer_ip;
43+
this.version = version;
44+
this.start_time = start_time;
45+
this.install_dir = install_dir;
46+
this.tar = tar;
47+
this.project_name_to_status = Maps.newHashMap(project_name_to_status);
48+
this.peer_key = canonical_host + ":" + HttpUtils.specialEncode(install_dir);
49+
this.peer_host_port = canonical_host + ":" + port;
50+
this.user_dns_domain = user_dns_domain;
51+
this.peer_type = PeerType.Daemon;
52+
this.project_name_to_status
53+
.put(Constants.CODEINE_NODES_PROJECT_NAME, createInternalProject());
54+
this.update_time = System.currentTimeMillis();
55+
this.update_time_from_peer = System.currentTimeMillis();
56+
}
57+
58+
private ProjectStatus createInternalProject() {
59+
NodeWithMonitorsInfo node_info = new NodeWithMonitorsInfo(this, this.peer_key, this.canonical_host,
60+
Constants.CODEINE_NODES_PROJECT_NAME, Maps.<String, MonitorStatusInfo>newHashMap());
61+
node_info.version(this.version);
62+
node_info.tags(Lists.newArrayList(project_name_to_status.keySet()));
63+
ProjectStatus ps = new ProjectStatus(Constants.CODEINE_NODES_PROJECT_NAME, node_info);
64+
return ps;
65+
}
66+
67+
public PeerStatusJsonV2(String peer_key, ProjectStatus projectStatus) {
68+
super();
69+
this.project_name_to_status = Maps.newHashMap();
70+
this.project_name_to_status.put(projectStatus.project_name(), projectStatus);
71+
this.update_time = System.currentTimeMillis();
72+
this.update_time_from_peer = System.currentTimeMillis();
73+
this.peer_key = peer_key;
74+
this.peer_type = PeerType.Reporter;
75+
}
76+
77+
public void addProjectStatus(String name, ProjectStatus status) {
78+
HashMap<String, ProjectStatus> tempList = Maps.newHashMap(project_name_to_status);
79+
tempList.put(name, status);
80+
project_name_to_status = tempList;
81+
}
82+
83+
public Map<String, ProjectStatus> project_name_to_status() {
84+
return Collections.unmodifiableMap(project_name_to_status);
85+
}
86+
87+
public String peer_key() {
88+
return peer_key;
89+
}
90+
91+
public String canonical_host_port() {
92+
return canonical_host + ":" + port;
93+
}
94+
95+
public String ip_port() {
96+
return peer_ip + ":" + port;
97+
}
98+
99+
public String address_port() {
100+
if (!StringUtils.isEmpty(user_dns_domain)) {
101+
return canonical_host + "." + user_dns_domain + ":" + port;
102+
}
103+
return canonical_host_port();
104+
}
105+
106+
public long update_time() {
107+
return update_time;
108+
}
109+
110+
public long update_time_from_peer() {
111+
return update_time_from_peer;
112+
}
113+
114+
public String key() {
115+
return peer_key();
116+
}
117+
118+
public String version() {
119+
return version;
120+
}
121+
122+
public String tar() {
123+
return tar;
124+
}
125+
126+
public void status(PeerStatusString status) {
127+
this.status = status;
128+
}
129+
130+
public PeerStatusString status() {
131+
return status;
132+
}
133+
134+
public void updateNodesWithPeer() {
135+
for (ProjectStatus projectStatus : project_name_to_status.values()) {
136+
projectStatus.updateNodesWithPeer(this);
137+
}
138+
}
139+
140+
public PeerType peer_type() {
141+
return peer_type;
142+
}
143+
144+
public String canonical_host() {
145+
return canonical_host;
146+
}
147+
148+
@Override
149+
public String toString() {
150+
return "PeerStatusJsonV2 [host_port()=" + canonical_host() + ", update_time()=" + new Date(
151+
update_time())
152+
+ ", update_time_from_peer()=" + new Date(update_time_from_peer()) + ", peer_type()="
153+
+ peer_type() + "]";
154+
}
155+
154156
}

0 commit comments

Comments
 (0)