Search in sources :

Example 46 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 47 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 48 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)

Example 49 with JsonObject

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();
}
Also used : Message(io.vertx.core.eventbus.Message) HashMap(java.util.HashMap) JsonObject(io.vertx.core.json.JsonObject) JsonObject(io.vertx.core.json.JsonObject) Test(org.junit.Test)

Example 50 with JsonObject

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();
}
Also used : Message(io.vertx.core.eventbus.Message) JsonObject(io.vertx.core.json.JsonObject) Test(org.junit.Test)

Aggregations

JsonObject (io.vertx.core.json.JsonObject)383 Test (org.junit.Test)210 JsonArray (io.vertx.core.json.JsonArray)89 HttpURLConnection (java.net.HttpURLConnection)68 Future (io.vertx.core.Future)55 Handler (io.vertx.core.Handler)51 Async (io.vertx.ext.unit.Async)50 Vertx (io.vertx.core.Vertx)48 TestContext (io.vertx.ext.unit.TestContext)48 VertxUnitRunner (io.vertx.ext.unit.junit.VertxUnitRunner)47 RunWith (org.junit.runner.RunWith)47 Timeout (org.junit.rules.Timeout)43 Before (org.junit.Before)41 Rule (org.junit.Rule)33 EventBusMessage (org.eclipse.hono.util.EventBusMessage)27 AsyncResult (io.vertx.core.AsyncResult)26 Buffer (io.vertx.core.buffer.Buffer)26 Constants (org.eclipse.hono.util.Constants)25 Mockito (org.mockito.Mockito)25 Objects (java.util.Objects)23