use of io.vertx.core.json.JsonObject in project vert.x by eclipse.
the class HAManager method addToHA.
// Add some information on a deployment in the cluster so other nodes know about it
private void addToHA(String deploymentID, String verticleName, DeploymentOptions deploymentOptions) {
String encoded;
synchronized (haInfo) {
JsonObject verticleConf = new JsonObject().put("dep_id", deploymentID);
verticleConf.put("verticle_name", verticleName);
verticleConf.put("options", deploymentOptions.toJson());
JsonArray haMods = haInfo.getJsonArray("verticles");
haMods.add(verticleConf);
encoded = haInfo.encode();
clusterMap.put(nodeID, encoded);
}
}
use of io.vertx.core.json.JsonObject in project vert.x by eclipse.
the class HAManager method checkFailover.
// Handle failover
private void checkFailover(String failedNodeID, JsonObject theHAInfo) {
try {
JsonArray deployments = theHAInfo.getJsonArray("verticles");
String group = theHAInfo.getString("group");
String chosen = chooseHashedNode(group, failedNodeID.hashCode());
if (chosen != null && chosen.equals(this.nodeID)) {
if (deployments != null && deployments.size() != 0) {
log.info("node" + nodeID + " says: Node " + failedNodeID + " has failed. This node will deploy " + deployments.size() + " deploymentIDs from that node.");
for (Object obj : deployments) {
JsonObject app = (JsonObject) obj;
processFailover(app);
}
}
// Failover is complete! We can now remove the failed node from the cluster map
clusterMap.remove(failedNodeID);
runOnContextAndWait(() -> {
if (failoverCompleteHandler != null) {
failoverCompleteHandler.handle(failedNodeID, theHAInfo, true);
}
});
}
} catch (Throwable t) {
log.error("Failed to handle failover", t);
runOnContextAndWait(() -> {
if (failoverCompleteHandler != null) {
failoverCompleteHandler.handle(failedNodeID, theHAInfo, false);
}
});
}
}
use of io.vertx.core.json.JsonObject in project vert.x by eclipse.
the class HAManager method processFailover.
// Process the failover of a deployment
private void processFailover(JsonObject failedVerticle) {
if (failDuringFailover) {
throw new VertxException("Oops!");
}
// This method must block until the failover is complete - i.e. the verticle is successfully redeployed
final String verticleName = failedVerticle.getString("verticle_name");
final CountDownLatch latch = new CountDownLatch(1);
final AtomicReference<Throwable> err = new AtomicReference<>();
// Now deploy this verticle on this node
ContextImpl ctx = vertx.getContext();
if (ctx != null) {
// We could be on main thread in which case we don't want to overwrite tccl
ContextImpl.setContext(null);
}
JsonObject options = failedVerticle.getJsonObject("options");
try {
doDeployVerticle(verticleName, new DeploymentOptions(options), result -> {
if (result.succeeded()) {
log.info("Successfully redeployed verticle " + verticleName + " after failover");
} else {
log.error("Failed to redeploy verticle after failover", result.cause());
err.set(result.cause());
}
latch.countDown();
Throwable t = err.get();
if (t != null) {
throw new VertxException(t);
}
});
} finally {
if (ctx != null) {
ContextImpl.setContext(ctx);
}
}
try {
if (!latch.await(120, TimeUnit.SECONDS)) {
throw new VertxException("Timed out waiting for redeploy on failover");
}
} catch (InterruptedException e) {
throw new IllegalStateException(e);
}
}
use of io.vertx.core.json.JsonObject in project vert.x by eclipse.
the class HAManager method chooseHashedNode.
// Compute the failover node
private String chooseHashedNode(String group, int hashCode) {
List<String> nodes = clusterManager.getNodes();
ArrayList<String> matchingMembers = new ArrayList<>();
for (String node : nodes) {
String sclusterInfo = clusterMap.get(node);
if (sclusterInfo != null) {
JsonObject clusterInfo = new JsonObject(sclusterInfo);
String memberGroup = clusterInfo.getString("group");
if (group == null || group.equals(memberGroup)) {
matchingMembers.add(node);
}
}
}
if (!matchingMembers.isEmpty()) {
// Hashcodes can be -ve so make it positive
long absHash = (long) hashCode + Integer.MAX_VALUE;
long lpos = absHash % matchingMembers.size();
return matchingMembers.get((int) lpos);
} else {
return null;
}
}
use of io.vertx.core.json.JsonObject in project vert.x by eclipse.
the class HAManager method nodeLeft.
// A node has left the cluster
// synchronize this in case the cluster manager is naughty and calls it concurrently
private synchronized void nodeLeft(String leftNodeID) {
addHaInfoIfLost();
checkQuorum();
if (attainedQuorum) {
checkSubs(leftNodeID);
// Check for failover
String sclusterInfo = clusterMap.get(leftNodeID);
if (sclusterInfo == null) {
// Clean close - do nothing
} else {
JsonObject clusterInfo = new JsonObject(sclusterInfo);
checkFailover(leftNodeID, clusterInfo);
}
// We also check for and potentially resume any previous failovers that might have failed
// We can determine this if there any ids in the cluster map which aren't in the node list
List<String> nodes = clusterManager.getNodes();
for (Map.Entry<String, String> entry : clusterMap.entrySet()) {
if (!leftNodeID.equals(entry.getKey()) && !nodes.contains(entry.getKey())) {
JsonObject haInfo = new JsonObject(entry.getValue());
checkFailover(entry.getKey(), haInfo);
}
}
}
}
Aggregations