Search in sources :

Example 6 with ClusterManager

use of io.vertx.core.spi.cluster.ClusterManager in project api-framework by vinscom.

the class SingletonServiceImplTest method testStart.

@Test
public void testStart(TestContext context) throws InterruptedException, ExecutionException {
    Async async = context.async(2);
    Vertx vertx = Glue.instance().resolve("/io/vertx/core/Vertx");
    ClusterManager cm = Glue.instance().resolve("/io/vertx/spi/cluster/ClusterManager");
    vertx.sharedData().<String, String>rxGetClusterWideMap("__in.erail.services").flatMapCompletable(// Service is running on some other node
    (m) -> m.rxPut("DummySingletonService", "NodeDownID")).subscribe(() -> {
        DummySingletonService service = Glue.instance().resolve("/in/erail/service/DummySingletonService");
        Observable.timer(100, TimeUnit.MILLISECONDS).subscribe((t) -> {
            vertx.sharedData().<String, String>rxGetClusterWideMap("__in.erail.services").flatMap((m) -> {
                return m.rxGet("DummySingletonService");
            }).doOnSuccess((nodeId) -> {
                // Validate after starting this service. Control is still with other node
                context.assertNotEquals(cm.getNodeID(), nodeId);
                async.countDown();
            }).subscribe((nodeId) -> {
                // Trigger remote node leave
                service.nodeLeft("NodeDownID");
                Observable.timer(100, TimeUnit.MILLISECONDS).subscribe((p) -> {
                    vertx.sharedData().<String, String>rxGetClusterWideMap("__in.erail.services").flatMap(m2 -> {
                        return m2.rxGet("DummySingletonService");
                    }).subscribe((updatedNodeId) -> {
                        context.assertEquals(cm.getNodeID(), updatedNodeId);
                        context.assertEquals(service.getRecorder().size(), 1);
                        async.countDown();
                    });
                });
            });
        });
    });
}
Also used : TestContext(io.vertx.ext.unit.TestContext) Async(io.vertx.ext.unit.Async) VertxInstance(io.vertx.core.VertxInstance) ClusterManager(io.vertx.core.spi.cluster.ClusterManager) RunWith(org.junit.runner.RunWith) Timeout(io.vertx.ext.unit.junit.Timeout) Test(org.junit.Test) VertxUnitRunner(io.vertx.ext.unit.junit.VertxUnitRunner) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) Vertx(io.vertx.reactivex.core.Vertx) Rule(org.junit.Rule) Observable(io.reactivex.Observable) Glue(in.erail.glue.Glue) Async(io.vertx.ext.unit.Async) Vertx(io.vertx.reactivex.core.Vertx) ClusterManager(io.vertx.core.spi.cluster.ClusterManager) Test(org.junit.Test)

Example 7 with ClusterManager

use of io.vertx.core.spi.cluster.ClusterManager in project vertx-zero by silentbalanceyh.

the class Motor method start.

public static <T> void start(final Class<?> clazz, final Consumer<T> consumer, final Consumer<Consumer<T>> fnSingle, final BiConsumer<ClusterManager, Consumer<T>> fnCluster, final Annal logger) {
    if (null == consumer) {
        throw new VertxCallbackException(clazz);
    }
    // 1. Check if clustered mode
    final ClusterOptions cluster = ZeroGrid.getClusterOption();
    if (cluster.isEnabled()) {
        // 2.1. Clustered
        final ClusterManager manager = cluster.getManager();
        logger.info(Info.APP_CLUSTERD, manager.getClass().getName(), manager.getNodeID(), manager.isActive());
        fnCluster.accept(manager, consumer);
    } else {
        // 2.2. Standalone
        fnSingle.accept(consumer);
    }
}
Also used : VertxCallbackException(io.vertx.zero.exception.VertxCallbackException) ClusterManager(io.vertx.core.spi.cluster.ClusterManager) ClusterOptions(io.vertx.core.ClusterOptions)

Example 8 with ClusterManager

use of io.vertx.core.spi.cluster.ClusterManager in project vert.x by eclipse.

the class VertxOptionsTest method testOptions.

@Test
public void testOptions() {
    VertxOptions options = new VertxOptions();
    assertEquals(2 * Runtime.getRuntime().availableProcessors(), options.getEventLoopPoolSize());
    int rand = TestUtils.randomPositiveInt();
    assertEquals(options, options.setEventLoopPoolSize(rand));
    assertEquals(rand, options.getEventLoopPoolSize());
    try {
        options.setEventLoopPoolSize(0);
        fail("Should throw exception");
    } catch (IllegalArgumentException e) {
    // OK
    }
    assertEquals(20, options.getWorkerPoolSize());
    rand = TestUtils.randomPositiveInt();
    assertEquals(options, options.setWorkerPoolSize(rand));
    assertEquals(rand, options.getWorkerPoolSize());
    try {
        options.setWorkerPoolSize(0);
        fail("Should throw exception");
    } catch (IllegalArgumentException e) {
    // OK
    }
    assertEquals(20, options.getInternalBlockingPoolSize());
    rand = TestUtils.randomPositiveInt();
    assertEquals(options, options.setInternalBlockingPoolSize(rand));
    assertEquals(rand, options.getInternalBlockingPoolSize());
    try {
        options.setInternalBlockingPoolSize(0);
        fail("Should throw exception");
    } catch (IllegalArgumentException e) {
    // OK
    }
    assertEquals(0, options.getEventBusOptions().getPort());
    options.getEventBusOptions().setPort(1234);
    assertEquals(1234, options.getEventBusOptions().getPort());
    try {
        options.getEventBusOptions().setPort(-1);
        fail("Should throw exception");
    } catch (IllegalArgumentException e) {
    // OK
    }
    try {
        options.getEventBusOptions().setPort(65536);
        fail("Should throw exception");
    } catch (IllegalArgumentException e) {
    // OK
    }
    assertEquals(-1, options.getEventBusOptions().getClusterPublicPort());
    options.getEventBusOptions().setClusterPublicPort(1234);
    assertEquals(1234, options.getEventBusOptions().getClusterPublicPort());
    try {
        options.getEventBusOptions().setClusterPublicPort(-1);
        fail("Should throw exception");
    } catch (IllegalArgumentException e) {
    // OK
    }
    try {
        options.getEventBusOptions().setClusterPublicPort(65536);
        fail("Should throw exception");
    } catch (IllegalArgumentException e) {
    // OK
    }
    assertNull(options.getEventBusOptions().getHost());
    String randString = TestUtils.randomUnicodeString(100);
    options.getEventBusOptions().setHost(randString);
    assertEquals(randString, options.getEventBusOptions().getHost());
    assertEquals(null, options.getEventBusOptions().getClusterPublicHost());
    randString = TestUtils.randomUnicodeString(100);
    options.getEventBusOptions().setClusterPublicHost(randString);
    assertEquals(randString, options.getEventBusOptions().getClusterPublicHost());
    assertEquals(20000, options.getEventBusOptions().getClusterPingInterval());
    long randomLong = TestUtils.randomPositiveLong();
    options.getEventBusOptions().setClusterPingInterval(randomLong);
    assertEquals(randomLong, options.getEventBusOptions().getClusterPingInterval());
    try {
        options.getEventBusOptions().setClusterPingInterval(-1);
        fail("Should throw exception");
    } catch (IllegalArgumentException e) {
        assertEquals(randomLong, options.getEventBusOptions().getClusterPingInterval());
    }
    assertEquals(20000, options.getEventBusOptions().getClusterPingReplyInterval());
    randomLong = TestUtils.randomPositiveLong();
    options.getEventBusOptions().setClusterPingReplyInterval(randomLong);
    assertEquals(randomLong, options.getEventBusOptions().getClusterPingReplyInterval());
    try {
        options.getEventBusOptions().setClusterPingReplyInterval(-1);
        fail("Should throw exception");
    } catch (IllegalArgumentException e) {
        assertEquals(randomLong, options.getEventBusOptions().getClusterPingReplyInterval());
    }
    assertEquals(1000, options.getBlockedThreadCheckInterval());
    rand = TestUtils.randomPositiveInt();
    assertEquals(options, options.setBlockedThreadCheckInterval(rand));
    assertEquals(rand, options.getBlockedThreadCheckInterval());
    try {
        options.setBlockedThreadCheckInterval(0);
        fail("Should throw exception");
    } catch (IllegalArgumentException e) {
    // OK
    }
    // 2 seconds in nano seconds
    assertEquals(2000l * 1000000, options.getMaxEventLoopExecuteTime());
    rand = TestUtils.randomPositiveInt();
    assertEquals(options, options.setMaxEventLoopExecuteTime(rand));
    assertEquals(rand, options.getMaxEventLoopExecuteTime());
    try {
        options.setMaxEventLoopExecuteTime(0);
        fail("Should throw exception");
    } catch (IllegalArgumentException e) {
    // OK
    }
    // 1 minute in nano seconds
    assertEquals(1l * 60 * 1000 * 1000000, options.getMaxWorkerExecuteTime());
    rand = TestUtils.randomPositiveInt();
    assertEquals(options, options.setMaxWorkerExecuteTime(rand));
    assertEquals(rand, options.getMaxWorkerExecuteTime());
    try {
        options.setMaxWorkerExecuteTime(0);
        fail("Should throw exception");
    } catch (IllegalArgumentException e) {
    // OK
    }
    ClusterManager mgr = new FakeClusterManager();
    assertNull(options.getClusterManager());
    assertEquals(options, options.setClusterManager(mgr));
    assertSame(mgr, options.getClusterManager());
    assertFalse(options.isHAEnabled());
    assertEquals(options, options.setHAEnabled(true));
    assertTrue(options.isHAEnabled());
    rand = TestUtils.randomPositiveInt();
    assertEquals(1, options.getQuorumSize());
    assertEquals(options, options.setQuorumSize(rand));
    assertEquals(rand, options.getQuorumSize());
    try {
        options.setQuorumSize(0);
        fail("Should throw exception");
    } catch (IllegalArgumentException e) {
    // OK
    }
    try {
        options.setQuorumSize(-1);
        fail("Should throw exception");
    } catch (IllegalArgumentException e) {
    // OK
    }
    assertEquals(VertxOptions.DEFAULT_HA_GROUP, options.getHAGroup());
    randString = TestUtils.randomUnicodeString(100);
    assertEquals(options, options.setHAGroup(randString));
    assertEquals(randString, options.getHAGroup());
    try {
        options.setHAGroup(null);
        fail("Should throw exception");
    } catch (NullPointerException e) {
    // OK
    }
    assertNotNull(options.getMetricsOptions());
    try {
        options.setWarningExceptionTime(-1);
        fail("Should throw exception");
    } catch (IllegalArgumentException e) {
    // OK
    }
    assertEquals(options, options.setWarningExceptionTime(1000000000l));
    assertEquals(1000000000l, options.getWarningExceptionTime());
    assertEquals(options, options.setMaxEventLoopExecuteTimeUnit(TimeUnit.SECONDS));
    assertEquals(TimeUnit.SECONDS, options.getMaxEventLoopExecuteTimeUnit());
    assertEquals(options, options.setMaxWorkerExecuteTimeUnit(TimeUnit.MILLISECONDS));
    assertEquals(TimeUnit.MILLISECONDS, options.getMaxWorkerExecuteTimeUnit());
    assertEquals(options, options.setWarningExceptionTimeUnit(TimeUnit.MINUTES));
    assertEquals(TimeUnit.MINUTES, options.getWarningExceptionTimeUnit());
    assertEquals(options, options.setBlockedThreadCheckIntervalUnit(TimeUnit.MILLISECONDS));
    assertEquals(TimeUnit.MILLISECONDS, options.getBlockedThreadCheckIntervalUnit());
}
Also used : ClusterManager(io.vertx.core.spi.cluster.ClusterManager) FakeClusterManager(io.vertx.test.fakecluster.FakeClusterManager) FakeClusterManager(io.vertx.test.fakecluster.FakeClusterManager) Test(org.junit.Test)

Example 9 with ClusterManager

use of io.vertx.core.spi.cluster.ClusterManager in project vertx-examples by vert-x3.

the class MainClassNode1 method main.

public static void main(String[] args) {
    JsonObject zkConfig = configureClusterManager();
    ClusterManager zookeeperClusterManager = new ZookeeperClusterManager(zkConfig);
    VertxOptions options = configureVertx(zookeeperClusterManager);
    Vertx.clusteredVertx(options, res -> {
        if (res.succeeded()) {
            Vertx vertx = res.result();
            DeploymentOptions deploymentOptions = new DeploymentOptions().setInstances(2);
            vertx.deployVerticle("verticle.SenderVerticle", deploymentOptions);
            System.out.println("Sender verticle deployed");
        }
    });
}
Also used : DeploymentOptions(io.vertx.core.DeploymentOptions) ZookeeperClusterManager(io.vertx.spi.cluster.zookeeper.ZookeeperClusterManager) JsonObject(io.vertx.core.json.JsonObject) Vertx(io.vertx.core.Vertx) ZookeeperClusterManager(io.vertx.spi.cluster.zookeeper.ZookeeperClusterManager) ClusterManager(io.vertx.core.spi.cluster.ClusterManager) VertxOptions(io.vertx.core.VertxOptions)

Example 10 with ClusterManager

use of io.vertx.core.spi.cluster.ClusterManager in project vertx-examples by vert-x3.

the class MainClassNode2 method main.

public static void main(String[] args) {
    JsonObject zkConfig = configureClusterManager();
    ClusterManager zookeeperClusterManager = new ZookeeperClusterManager(zkConfig);
    VertxOptions options = configureVertx(zookeeperClusterManager);
    Vertx.clusteredVertx(options, res -> {
        if (res.succeeded()) {
            Vertx vertx = res.result();
            DeploymentOptions deploymentOptions = new DeploymentOptions().setInstances(2);
            vertx.deployVerticle("verticle.ConsumerVerticle", deploymentOptions);
            System.out.println("consumer verticle deployed");
        }
    });
}
Also used : DeploymentOptions(io.vertx.core.DeploymentOptions) ZookeeperClusterManager(io.vertx.spi.cluster.zookeeper.ZookeeperClusterManager) JsonObject(io.vertx.core.json.JsonObject) Vertx(io.vertx.core.Vertx) ZookeeperClusterManager(io.vertx.spi.cluster.zookeeper.ZookeeperClusterManager) ClusterManager(io.vertx.core.spi.cluster.ClusterManager) VertxOptions(io.vertx.core.VertxOptions)

Aggregations

ClusterManager (io.vertx.core.spi.cluster.ClusterManager)15 JsonObject (io.vertx.core.json.JsonObject)5 Test (org.junit.Test)5 Vertx (io.vertx.core.Vertx)3 VertxOptions (io.vertx.core.VertxOptions)3 FakeClusterManager (io.vertx.test.fakecluster.FakeClusterManager)3 DeploymentOptions (io.vertx.core.DeploymentOptions)2 VertxInternal (io.vertx.core.impl.VertxInternal)2 ZookeeperClusterManager (io.vertx.spi.cluster.zookeeper.ZookeeperClusterManager)2 Set (java.util.Set)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 TimeUnit (java.util.concurrent.TimeUnit)2 Glue (in.erail.glue.Glue)1 Observable (io.reactivex.Observable)1 AbstractVerticle (io.vertx.core.AbstractVerticle)1 ClusterOptions (io.vertx.core.ClusterOptions)1 Context (io.vertx.core.Context)1 Future (io.vertx.core.Future)1 Promise (io.vertx.core.Promise)1 VertxInstance (io.vertx.core.VertxInstance)1