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);
}
}
}
}
use of io.vertx.core.json.JsonObject in project vert.x by eclipse.
the class JSONEventBusTest method testChangesNotVisibleObject3.
@Test
public void testChangesNotVisibleObject3() {
Map<String, Object> map = new HashMap<>();
final JsonObject obj = new JsonObject(map);
eb.<JsonObject>consumer("foo").handler((Message<JsonObject> msg) -> {
vertx.setTimer(1000, id -> {
assertFalse(msg.body().containsKey("b"));
testComplete();
});
});
eb.send("foo", obj);
map.put("b", "uhqdihuqwd");
await();
}
use of io.vertx.core.json.JsonObject in project vert.x by eclipse.
the class JSONEventBusTest method testChangesNotVisibleObject1.
@Test
public void testChangesNotVisibleObject1() {
JsonObject obj = new JsonObject();
eb.<JsonObject>consumer("foo").handler((Message<JsonObject> msg) -> {
assertFalse(msg.body().containsKey("b"));
testComplete();
});
eb.send("foo", obj);
obj.put("b", "blurrgg");
await();
}
Aggregations