Search in sources :

Example 1 with JsonObject

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);
    }
}
Also used : JsonArray(io.vertx.core.json.JsonArray) JsonObject(io.vertx.core.json.JsonObject)

Example 2 with JsonObject

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);
            }
        });
    }
}
Also used : JsonArray(io.vertx.core.json.JsonArray) JsonObject(io.vertx.core.json.JsonObject) JsonObject(io.vertx.core.json.JsonObject)

Example 3 with JsonObject

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);
    }
}
Also used : DeploymentOptions(io.vertx.core.DeploymentOptions) VertxException(io.vertx.core.VertxException) JsonObject(io.vertx.core.json.JsonObject) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 4 with JsonObject

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;
    }
}
Also used : ArrayList(java.util.ArrayList) JsonObject(io.vertx.core.json.JsonObject)

Example 5 with JsonObject

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);
            }
        }
    }
}
Also used : JsonObject(io.vertx.core.json.JsonObject) Map(java.util.Map)

Aggregations

JsonObject (io.vertx.core.json.JsonObject)185 Test (org.junit.Test)136 JsonArray (io.vertx.core.json.JsonArray)44 ArrayList (java.util.ArrayList)11 DeploymentOptions (io.vertx.core.DeploymentOptions)8 HashMap (java.util.HashMap)8 Launcher (io.vertx.core.Launcher)7 Buffer (io.vertx.core.buffer.Buffer)7 IOException (java.io.IOException)7 Instant (java.time.Instant)7 AsciiString (io.netty.util.AsciiString)5 CountDownLatch (java.util.concurrent.CountDownLatch)5 Message (io.vertx.core.eventbus.Message)4 File (java.io.File)4 Random (java.util.Random)4 AbstractVerticle (io.vertx.core.AbstractVerticle)3 Vertx (io.vertx.core.Vertx)3 VertxOptions (io.vertx.core.VertxOptions)3 VertxInternal (io.vertx.core.impl.VertxInternal)3 DecodeException (io.vertx.core.json.DecodeException)3