Skip to content

Commit

Permalink
Merge pull request #240 from codeine-cd/codeine-239
Browse files Browse the repository at this point in the history
codeine-239 peer should not change address during up time
  • Loading branch information
lchayoun authored Jun 5, 2018
2 parents 34fe890 + 78a46ef commit 51c314c
Show file tree
Hide file tree
Showing 6 changed files with 180 additions and 191 deletions.
4 changes: 2 additions & 2 deletions src/common/codeine/api/NodeWithPeerInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public NodeWithPeerInfo(String name, String alias, PeerStatusJsonV2 peer) {
super(name, alias);
this.peer = peer;
if (null != peer) {
peer_host_port = peer.host_port();
peer_host_port = peer.canonical_host_port();
peer_address = peer.address_port();
peer_key = peer.key();
peer_status = peer.status();
Expand All @@ -32,7 +32,7 @@ public PeerStatusJsonV2 peer() {

public void peer(PeerStatusJsonV2 peer) {
this.peer = peer;
peer_host_port = peer.host_port();
peer_host_port = peer.canonical_host_port();
peer_address = peer.address_port();
peer_key = peer.key();
peer_status = peer.status();
Expand Down
52 changes: 24 additions & 28 deletions src/common/codeine/db/mysql/connectors/StatusMysqlConnector.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public void createTables() {
public void putReplaceStatus(PeerStatusJsonV2 p) {
String json = gson.toJson(p);
log.info("will update status to " + dbUtils.server() + "\n" + json);
dbUtils.executeUpdate("DELETE FROM "+TABLE_NAME+" WHERE peer_key = '" + p.peer_old_key() + "'");
dbUtils.executeUpdate("DELETE FROM "+TABLE_NAME+" WHERE peer_key = '" + p.peer_key() + "'");
dbUtils.executeUpdate("REPLACE INTO "+TABLE_NAME+" (peer_key, data, update_time ) VALUES (?, ?, CURRENT_TIMESTAMP())", p.peer_key(), json);
}

Expand Down Expand Up @@ -93,39 +93,35 @@ private void updateNodesWithPeer(PeerStatusJsonV2 peerStatus) {
public void updatePeersStatus(final long timeToRemove, final long timeToDisc) {
final List<String> idToRemove = Lists.newArrayList();
final List<String> idToDisc = Lists.newArrayList();
Function<ResultSet, Void> function = new Function<ResultSet, Void>() {
@Override
public Void apply(ResultSet rs){
try {
String key = rs.getString("peer_key");
Function<ResultSet, Void> function = rs -> {
try {
String key = rs.getString("peer_key");
// PeerStatusString status = PeerStatusString.valueOf(rs.getString("status"));
String value = rs.getString("data");
String status = rs.getString("status");
PeerStatusJsonV2 peerStatus = gson.fromJson(value, PeerStatusJsonV2.class);
PeerType peerType = peerStatus.peer_type();
long timeToRemovePeer = peerType == PeerType.Reporter ? timeToRemove + TimeUnit.DAYS.toMinutes(7) : timeToRemove;
long timeToDiscPeer = peerType == PeerType.Reporter ? timeToDisc + TimeUnit.DAYS.toMinutes(7) : timeToDisc;
long timeDiff = rs.getLong("TIME_DIFF");
log.debug("time diff is " + timeDiff);
if (timeDiff > timeToRemovePeer){
log.info("time diff is " + timeDiff);
log.info("deleting " + peerStatus);
String value = rs.getString("data");
String status = rs.getString("status");
PeerStatusJsonV2 peerStatus = gson.fromJson(value, PeerStatusJsonV2.class);
PeerType peerType = peerStatus.peer_type();
long timeToRemovePeer = peerType == PeerType.Reporter ? timeToRemove + TimeUnit.DAYS.toMinutes(7) : timeToRemove;
long timeToDiscPeer = peerType == PeerType.Reporter ? timeToDisc + TimeUnit.DAYS.toMinutes(7) : timeToDisc;
long timeDiff = rs.getLong("TIME_DIFF");
log.debug("time diff is " + timeDiff);
if (timeDiff > timeToRemovePeer){
log.info("time diff is " + timeDiff);
log.info("deleting " + peerStatus);
// rs.deleteRow();
idToRemove.add(key);
}
else if (timeDiff > timeToDiscPeer && !status.equals(PeerStatusString.Disc.toString())){
log.info("time diff is " + timeDiff);
log.info("update to disc " + peerStatus);
idToDisc.add(key);
idToRemove.add(key);
}
else if (timeDiff > timeToDiscPeer && !status.equals(PeerStatusString.Disc.toString())){
log.info("time diff is " + timeDiff);
log.info("update to disc " + peerStatus);
idToDisc.add(key);
// rs.updateString("status", "Disc");
// rs.updateRow();
}
return null;
} catch (SQLException e) {
throw ExceptionUtils.asUnchecked(e);
}
return null;
} catch (SQLException e) {
throw ExceptionUtils.asUnchecked(e);
}

};
dbUtils.executeUpdateableQuery("select *,TIMESTAMPDIFF(MINUTE,update_time,CURRENT_TIMESTAMP()) as TIME_DIFF from " + TABLE_NAME, function);
if (webConfJsonStore.get().readonly_web_server()) {
Expand Down
6 changes: 4 additions & 2 deletions src/common/codeine/jsons/peer_status/PeerStatus.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ public class PeerStatus {
private static final Logger log = Logger.getLogger(PeerStatus.class);

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

@Inject
private CodeineRuntimeInfo codeineRuntimeInfo;
Expand Down Expand Up @@ -96,9 +98,9 @@ public Map<String, ProjectStatus> project_name_to_status() {
}

public PeerStatusJsonV2 createJson() {
return new PeerStatusJsonV2(InetUtils.getLocalHost().getHostName(), codeineRuntimeInfo.port(),
return new PeerStatusJsonV2(codeineRuntimeInfo.port(),
codeineRuntimeInfo.version(), codeineRuntimeInfo.startTime(), Constants.getInstallDir(),
PathHelper.getTarFile(), project_name_to_status(), InetUtils.getLocalHost().getHostAddress(), System.getProperty("DNS_DOMAIN_NAME"), InetUtils.getLocalHost().getCanonicalHostName());
PathHelper.getTarFile(), project_name_to_status(), host_address, System.getProperty("DNS_DOMAIN_NAME"), canonical_host_name);
}

public String updateVersion(ProjectJson project, String node, String alias, String version) {
Expand Down
272 changes: 137 additions & 135 deletions src/common/codeine/jsons/peer_status/PeerStatusJsonV2.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,139 +16,141 @@

@SuppressWarnings("unused")
public class PeerStatusJsonV2 {
private String peer_key;
//TODO remove after cf-engine in build > 1.1.309
private String peer_old_key;
private String peer_host_port;
private String peer_ip;
private String user_dns_domain;
private Map<String, ProjectStatus> project_name_to_status = Maps.newHashMap();//Lists.newArrayList();
private String host;
private String canonical_host; //TODO introduced in codeine 1202, can be used after cfengine is in that build
private int port;
private String version;
private String tar;
private long start_time;
private long update_time;//updated in directory server when first seen
private long update_time_from_peer;
private String install_dir;
private PeerType peer_type;
private transient PeerStatusString status;

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) {
super();
this.host = host;
this.canonical_host = canonical_host;
this.port = port;
this.peer_ip = peer_ip;
this.version = version;
this.start_time = start_time;
this.install_dir = install_dir;
this.tar = tar;
this.project_name_to_status = Maps.newHashMap(project_name_to_status);
this.peer_old_key = host + ":" + install_dir;
this.peer_key = host + ":" + HttpUtils.specialEncode(install_dir);
this.peer_host_port = host + ":" + port;
this.user_dns_domain = user_dns_domain;
this.peer_type = PeerType.Daemon;
this.project_name_to_status.put(Constants.CODEINE_NODES_PROJECT_NAME, createInternalProject());
this.update_time = System.currentTimeMillis();
this.update_time_from_peer = System.currentTimeMillis();
}
private ProjectStatus createInternalProject() {
NodeWithMonitorsInfo node_info = new NodeWithMonitorsInfo(this, this.peer_key, this.host, Constants.CODEINE_NODES_PROJECT_NAME, Maps.<String, MonitorStatusInfo>newHashMap());
node_info.version(this.version);
node_info.tags(Lists.newArrayList(project_name_to_status.keySet()));
ProjectStatus ps = new ProjectStatus(Constants.CODEINE_NODES_PROJECT_NAME, node_info);
return ps;
}
public PeerStatusJsonV2(String peer_key, ProjectStatus projectStatus) {
super();
this.project_name_to_status = Maps.newHashMap();
this.project_name_to_status.put(projectStatus.project_name(), projectStatus);
this.update_time = System.currentTimeMillis();
this.update_time_from_peer = System.currentTimeMillis();
this.peer_key = peer_key;
this.peer_type = PeerType.Reporter;
}

public void addProjectStatus(String name, ProjectStatus status) {
HashMap<String, ProjectStatus> tempList = Maps.newHashMap(project_name_to_status);
tempList.put(name, status);
project_name_to_status = tempList;
}

public Map<String, ProjectStatus> project_name_to_status() {
return Collections.unmodifiableMap(project_name_to_status);
}

public String peer_key() {
return peer_key;
}

public String host_port() {
return host + ":" + port;
}
public String canonical_host_port() {
return canonical_host + ":" + port;
}
public String ip_port() {
return peer_ip + ":" + port;
}

public String address_port() {
if (!StringUtils.isEmpty(user_dns_domain)) {
return host + "." + user_dns_domain + ":" + port;
} else if (!StringUtils.isEmpty(canonical_host)) {
return canonical_host_port();
} else {
return host_port();
}
}

public long update_time() {
return update_time;
}
public long update_time_from_peer() {
return update_time_from_peer;
}

public String key() {
return peer_key();
}

public String version() {
return version;
}

public String host() {
return host;
}

public String tar() {
return tar;
}
public void status(PeerStatusString status) {
this.status = status;
}
public PeerStatusString status() {
return status;
}
public void updateNodesWithPeer() {
for (ProjectStatus projectStatus : project_name_to_status.values()) {
projectStatus.updateNodesWithPeer(this);
}
}
public PeerType peer_type() {
return peer_type;
}
public String peer_old_key() {
return peer_old_key;
}
@Override
public String toString() {
return "PeerStatusJsonV2 [host_port()=" + host_port() + ", update_time()=" + new Date(update_time())
+ ", update_time_from_peer()=" + new Date(update_time_from_peer()) + ", peer_type()=" + peer_type() + "]";
}


private String peer_key;
private String peer_host_port;
private String peer_ip;
private String user_dns_domain;
private Map<String, ProjectStatus> project_name_to_status;
private String canonical_host;
private int port;
private String version;
private String tar;
private long start_time;
private long update_time;//updated in directory server when first seen
private long update_time_from_peer;
private String install_dir;
private PeerType peer_type;
private transient PeerStatusString status;

public PeerStatusJsonV2(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) {
super();
this.canonical_host = canonical_host;
this.port = port;
this.peer_ip = peer_ip;
this.version = version;
this.start_time = start_time;
this.install_dir = install_dir;
this.tar = tar;
this.project_name_to_status = Maps.newHashMap(project_name_to_status);
this.peer_key = canonical_host + ":" + HttpUtils.specialEncode(install_dir);
this.peer_host_port = canonical_host + ":" + port;
this.user_dns_domain = user_dns_domain;
this.peer_type = PeerType.Daemon;
this.project_name_to_status
.put(Constants.CODEINE_NODES_PROJECT_NAME, createInternalProject());
this.update_time = System.currentTimeMillis();
this.update_time_from_peer = System.currentTimeMillis();
}

private ProjectStatus createInternalProject() {
NodeWithMonitorsInfo node_info = new NodeWithMonitorsInfo(this, this.peer_key, this.canonical_host,
Constants.CODEINE_NODES_PROJECT_NAME, Maps.<String, MonitorStatusInfo>newHashMap());
node_info.version(this.version);
node_info.tags(Lists.newArrayList(project_name_to_status.keySet()));
ProjectStatus ps = new ProjectStatus(Constants.CODEINE_NODES_PROJECT_NAME, node_info);
return ps;
}

public PeerStatusJsonV2(String peer_key, ProjectStatus projectStatus) {
super();
this.project_name_to_status = Maps.newHashMap();
this.project_name_to_status.put(projectStatus.project_name(), projectStatus);
this.update_time = System.currentTimeMillis();
this.update_time_from_peer = System.currentTimeMillis();
this.peer_key = peer_key;
this.peer_type = PeerType.Reporter;
}

public void addProjectStatus(String name, ProjectStatus status) {
HashMap<String, ProjectStatus> tempList = Maps.newHashMap(project_name_to_status);
tempList.put(name, status);
project_name_to_status = tempList;
}

public Map<String, ProjectStatus> project_name_to_status() {
return Collections.unmodifiableMap(project_name_to_status);
}

public String peer_key() {
return peer_key;
}

public String canonical_host_port() {
return canonical_host + ":" + port;
}

public String ip_port() {
return peer_ip + ":" + port;
}

public String address_port() {
if (!StringUtils.isEmpty(user_dns_domain)) {
return canonical_host + "." + user_dns_domain + ":" + port;
}
return canonical_host_port();
}

public long update_time() {
return update_time;
}

public long update_time_from_peer() {
return update_time_from_peer;
}

public String key() {
return peer_key();
}

public String version() {
return version;
}

public String tar() {
return tar;
}

public void status(PeerStatusString status) {
this.status = status;
}

public PeerStatusString status() {
return status;
}

public void updateNodesWithPeer() {
for (ProjectStatus projectStatus : project_name_to_status.values()) {
projectStatus.updateNodesWithPeer(this);
}
}

public PeerType peer_type() {
return peer_type;
}

public String canonical_host() {
return canonical_host;
}

@Override
public String toString() {
return "PeerStatusJsonV2 [host_port()=" + canonical_host() + ", update_time()=" + new Date(
update_time())
+ ", update_time_from_peer()=" + new Date(update_time_from_peer()) + ", peer_type()="
+ peer_type() + "]";
}

}
Loading

0 comments on commit 51c314c

Please sign in to comment.