Skip to content

Commit

Permalink
Review changes
Browse files Browse the repository at this point in the history
Signed-off-by: Thomas Segismont <tsegismont@gmail.com>
  • Loading branch information
tsegismont committed Aug 4, 2017
1 parent 57d23d3 commit 2fcd4fb
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 58 deletions.
22 changes: 8 additions & 14 deletions src/main/java/io/vertx/core/Vertx.java
Expand Up @@ -346,15 +346,12 @@ static void clusteredVertx(VertxOptions options, Handler<AsyncResult<Vertx>> res
* {@code verticleSupplier}.
* <p>
* The supplier will be invoked as many times as {@link DeploymentOptions#getInstances()}.
* It must not return:
* <ul>
* <li>null</li>
* <li>the same instance twice</li>
* <li>instances of different verticle classes</li>
* </ul>
* It must not return the same instance twice.
* <p>
* Note that the supplier will be invoked on the caller thread.
*/
@GenIgnore
void deployVerticle(Supplier<? extends Verticle> verticleSupplier, DeploymentOptions options);
void deployVerticle(Supplier<Verticle> verticleSupplier, DeploymentOptions options);

/**
* Like {@link #deployVerticle(Verticle, Handler)} but {@link io.vertx.core.DeploymentOptions} are provided to configure the
Expand All @@ -379,15 +376,12 @@ static void clusteredVertx(VertxOptions options, Handler<AsyncResult<Vertx>> res
* invoking the {@code verticleSupplier}.
* <p>
* The supplier will be invoked as many times as {@link DeploymentOptions#getInstances()}.
* It must not return:
* <ul>
* <li>null</li>
* <li>the same instance twice</li>
* <li>instances of different verticle classes</li>
* </ul>
* It must not return the same instance twice.
* <p>
* Note that the supplier will be invoked on the caller thread.
*/
@GenIgnore
void deployVerticle(Supplier<? extends Verticle> verticleSupplier, DeploymentOptions options, Handler<AsyncResult<String>> completionHandler);
void deployVerticle(Supplier<Verticle> verticleSupplier, DeploymentOptions options, Handler<AsyncResult<String>> completionHandler);

/**
* Deploy a verticle instance given a name.
Expand Down
53 changes: 22 additions & 31 deletions src/main/java/io/vertx/core/impl/DeploymentManager.java
Expand Up @@ -82,7 +82,7 @@ private String generateDeploymentID() {
return UUID.randomUUID().toString();
}

public void deployVerticle(Supplier<? extends Verticle> verticleSupplier, DeploymentOptions options, Handler<AsyncResult<String>> completionHandler) {
public void deployVerticle(Supplier<Verticle> verticleSupplier, DeploymentOptions options, Handler<AsyncResult<String>> completionHandler) {
if (options.getInstances() < 1) {
throw new IllegalArgumentException("Can't specify < 1 instances to deploy");
}
Expand All @@ -100,38 +100,29 @@ public void deployVerticle(Supplier<? extends Verticle> verticleSupplier, Deploy
}
ContextImpl currentContext = vertx.getOrCreateContext();
ClassLoader cl = getClassLoader(options, currentContext);
currentContext.<Set<Verticle>>executeBlocking(fut -> {
int nbInstances = options.getInstances();
IdentityHashMap<Class<? extends Verticle>, Set<Verticle>> verticles = new IdentityHashMap<>();
for (int i = 0; i < nbInstances; i++) {
Verticle verticle = verticleSupplier.get();
if (verticle == null) {
throw new RuntimeException("Supplied verticle is null");
}
verticles.compute(verticle.getClass(), (k, v) -> {
if (v == null) v = new HashSet<>();
v.add(verticle);
return v;
});
}
if (verticles.size() != 1) {
throw new RuntimeException("Supplied verticles are not from the same class");
}
Set<Verticle> instances = verticles.values().iterator().next();
if (instances.size() != nbInstances) {
throw new RuntimeException("Same verticle supplied more than once");
int nbInstances = options.getInstances();
Set<Verticle> verticles = Collections.newSetFromMap(new IdentityHashMap<>());
for (int i = 0; i < nbInstances; i++) {
Verticle verticle;
try {
verticle = verticleSupplier.get();
} catch (Exception e) {
completionHandler.handle(Future.failedFuture(e));
return;
}
fut.complete(instances);
}, false, ar -> {
if (ar.succeeded()) {
Set<Verticle> result = ar.result();
Verticle[] verticles = result.toArray(new Verticle[result.size()]);
String verticleClass = verticles[0].getClass().getName();
doDeploy("java:" + verticleClass, generateDeploymentID(), options, currentContext, currentContext, completionHandler, cl, verticles);
} else {
completionHandler.handle(Future.failedFuture(ar.cause()));
if (verticle == null) {
completionHandler.handle(Future.failedFuture("Supplied verticle is null"));
return;
}
});
verticles.add(verticle);
}
if (verticles.size() != nbInstances) {
completionHandler.handle(Future.failedFuture("Same verticle supplied more than once"));
return;
}
Verticle[] verticlesArray = verticles.toArray(new Verticle[verticles.size()]);
String verticleClass = verticlesArray[0].getClass().getName();
doDeploy("java:" + verticleClass, generateDeploymentID(), options, currentContext, currentContext, completionHandler, cl, verticlesArray);
}

public void deployVerticle(String identifier,
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/io/vertx/core/impl/VertxImpl.java
Expand Up @@ -569,7 +569,7 @@ public void deployVerticle(Class<? extends Verticle> verticleClass, DeploymentOp
}

@Override
public void deployVerticle(Supplier<? extends Verticle> verticleSupplier, DeploymentOptions options) {
public void deployVerticle(Supplier<Verticle> verticleSupplier, DeploymentOptions options) {
deployVerticle(verticleSupplier, options, null);
}

Expand All @@ -593,7 +593,7 @@ public void deployVerticle(Class<? extends Verticle> verticleClass, DeploymentOp
}

@Override
public void deployVerticle(Supplier<? extends Verticle> verticleSupplier, DeploymentOptions options, Handler<AsyncResult<String>> completionHandler) {
public void deployVerticle(Supplier<Verticle> verticleSupplier, DeploymentOptions options, Handler<AsyncResult<String>> completionHandler) {
boolean closed;
synchronized (this) {
closed = this.closed;
Expand Down
17 changes: 6 additions & 11 deletions src/test/java/io/vertx/test/core/DeploymentTest.java
Expand Up @@ -41,7 +41,6 @@
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
Expand All @@ -54,7 +53,6 @@
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Consumer;
import java.util.function.Supplier;

/**
*
Expand Down Expand Up @@ -1292,7 +1290,7 @@ public void start() throws Exception {
@Test
public void testDeploySupplier() throws Exception {
JsonObject config = generateJSONObject();
Set<MyVerticle> myVerticles = Collections.synchronizedSet(new HashSet<>());
Set<MyVerticle> myVerticles = new HashSet<>();
vertx.deployVerticle(() -> {
MyVerticle myVerticle = new MyVerticle();
myVerticles.add(myVerticle);
Expand Down Expand Up @@ -1327,13 +1325,10 @@ public void testDeploySupplierDuplicate() throws Exception {
}

@Test
public void testDeploySupplierDifferentClasses() throws Exception {
AtomicInteger idx = new AtomicInteger();
MyVerticle myVerticle = new MyVerticle();
MyAsyncVerticle myAsyncVerticle = new MyAsyncVerticle(null, null);
Supplier<Verticle> supplier = () -> idx.getAndIncrement() % 2 == 0 ? myAsyncVerticle : myAsyncVerticle;
vertx.deployVerticle(supplier, new DeploymentOptions().setInstances(2), onFailure(t -> {
assertFalse(myVerticle.startCalled);
public void testDeploySupplierThrowsException() throws Exception {
vertx.deployVerticle(() -> {
throw new RuntimeException("boum");
}, new DeploymentOptions().setInstances(2), onFailure(t -> {
testComplete();
}));
await();
Expand Down Expand Up @@ -1472,7 +1467,7 @@ public void stop() throws Exception {
}

public static class ReferenceSavingMyVerticle extends MyVerticle {
static Set<MyVerticle> myVerticles = Collections.synchronizedSet(new HashSet<>());
static Set<MyVerticle> myVerticles = new HashSet<>();

public ReferenceSavingMyVerticle() {
super();
Expand Down

0 comments on commit 2fcd4fb

Please sign in to comment.