/
DeploymentManager.java
651 lines (599 loc) · 23.8 KB
/
DeploymentManager.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
/*
* Copyright (c) 2011-2014 The original author or authors
* ------------------------------------------------------
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Apache License v2.0 which accompanies this distribution.
*
* The Eclipse Public License is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* The Apache License v2.0 is available at
* http://www.opensource.org/licenses/apache2.0.php
*
* You may elect to redistribute this code under either of these licenses.
*/
package io.vertx.core.impl;
import io.vertx.core.AsyncResult;
import io.vertx.core.Context;
import io.vertx.core.DeploymentOptions;
import io.vertx.core.Future;
import io.vertx.core.Handler;
import io.vertx.core.ServiceHelper;
import io.vertx.core.Verticle;
import io.vertx.core.json.JsonObject;
import io.vertx.core.logging.Logger;
import io.vertx.core.logging.LoggerFactory;
import io.vertx.core.spi.VerticleFactory;
import io.vertx.core.spi.metrics.VertxMetrics;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.IdentityHashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import java.util.WeakHashMap;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Supplier;
/**
*
* @author <a href="http://tfox.org">Tim Fox</a>
*/
public class DeploymentManager {
private static final Logger log = LoggerFactory.getLogger(DeploymentManager.class);
private final VertxInternal vertx;
private final Map<String, Deployment> deployments = new ConcurrentHashMap<>();
private final Map<String, ClassLoader> classloaders = new WeakHashMap<>();
private final Map<String, List<VerticleFactory>> verticleFactories = new ConcurrentHashMap<>();
private final List<VerticleFactory> defaultFactories = new ArrayList<>();
public DeploymentManager(VertxInternal vertx) {
this.vertx = vertx;
loadVerticleFactories();
}
private void loadVerticleFactories() {
Collection<VerticleFactory> factories = ServiceHelper.loadFactories(VerticleFactory.class);
factories.forEach(this::registerVerticleFactory);
VerticleFactory defaultFactory = new JavaVerticleFactory();
defaultFactory.init(vertx);
defaultFactories.add(defaultFactory);
}
private String generateDeploymentID() {
return UUID.randomUUID().toString();
}
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");
}
if (options.isMultiThreaded() && !options.isWorker()) {
throw new IllegalArgumentException("If multi-threaded then must be worker too");
}
if (options.getExtraClasspath() != null) {
throw new IllegalArgumentException("Can't specify extraClasspath for already created verticle");
}
if (options.getIsolationGroup() != null) {
throw new IllegalArgumentException("Can't specify isolationGroup for already created verticle");
}
if (options.getIsolatedClasses() != null) {
throw new IllegalArgumentException("Can't specify isolatedClasses for already created verticle");
}
ContextImpl currentContext = vertx.getOrCreateContext();
ClassLoader cl = getClassLoader(options, currentContext);
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;
}
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,
DeploymentOptions options,
Handler<AsyncResult<String>> completionHandler) {
if (options.isMultiThreaded() && !options.isWorker()) {
throw new IllegalArgumentException("If multi-threaded then must be worker too");
}
ContextImpl callingContext = vertx.getOrCreateContext();
ClassLoader cl = getClassLoader(options, callingContext);
doDeployVerticle(identifier, generateDeploymentID(), options, callingContext, callingContext, cl, completionHandler);
}
private void doDeployVerticle(String identifier,
String deploymentID,
DeploymentOptions options,
ContextImpl parentContext,
ContextImpl callingContext,
ClassLoader cl,
Handler<AsyncResult<String>> completionHandler) {
List<VerticleFactory> verticleFactories = resolveFactories(identifier);
Iterator<VerticleFactory> iter = verticleFactories.iterator();
doDeployVerticle(iter, null, identifier, deploymentID, options, parentContext, callingContext, cl, completionHandler);
}
private void doDeployVerticle(Iterator<VerticleFactory> iter,
Throwable prevErr,
String identifier,
String deploymentID,
DeploymentOptions options,
ContextImpl parentContext,
ContextImpl callingContext,
ClassLoader cl,
Handler<AsyncResult<String>> completionHandler) {
if (iter.hasNext()) {
VerticleFactory verticleFactory = iter.next();
Future<String> fut = Future.future();
if (verticleFactory.requiresResolve()) {
try {
verticleFactory.resolve(identifier, options, cl, fut);
} catch (Exception e) {
try {
fut.fail(e);
} catch (Exception ignore) {
// Too late
}
}
} else {
fut.complete(identifier);
}
fut.setHandler(ar -> {
Throwable err;
if (ar.succeeded()) {
String resolvedName = ar.result();
if (!resolvedName.equals(identifier)) {
deployVerticle(resolvedName, options, completionHandler);
return;
} else {
if (verticleFactory.blockingCreate()) {
vertx.<Verticle[]>executeBlocking(createFut -> {
try {
Verticle[] verticles = createVerticles(verticleFactory, identifier, options.getInstances(), cl);
createFut.complete(verticles);
} catch (Exception e) {
createFut.fail(e);
}
}, res -> {
if (res.succeeded()) {
doDeploy(identifier, deploymentID, options, parentContext, callingContext, completionHandler, cl, res.result());
} else {
// Try the next one
doDeployVerticle(iter, res.cause(), identifier, deploymentID, options, parentContext, callingContext, cl, completionHandler);
}
});
return;
} else {
try {
Verticle[] verticles = createVerticles(verticleFactory, identifier, options.getInstances(), cl);
doDeploy(identifier, deploymentID, options, parentContext, callingContext, completionHandler, cl, verticles);
return;
} catch (Exception e) {
err = e;
}
}
}
} else {
err = ar.cause();
}
// Try the next one
doDeployVerticle(iter, err, identifier, deploymentID, options, parentContext, callingContext, cl, completionHandler);
});
} else {
if (prevErr != null) {
// Report failure if there are no more factories to try otherwise try the next one
reportFailure(prevErr, callingContext, completionHandler);
} else {
// not handled or impossible ?
}
}
}
private Verticle[] createVerticles(VerticleFactory verticleFactory, String identifier, int instances, ClassLoader cl) throws Exception {
Verticle[] verticles = new Verticle[instances];
for (int i = 0; i < instances; i++) {
verticles[i] = verticleFactory.createVerticle(identifier, cl);
if (verticles[i] == null) {
throw new NullPointerException("VerticleFactory::createVerticle returned null");
}
}
return verticles;
}
private String getSuffix(int pos, String str) {
if (pos + 1 >= str.length()) {
throw new IllegalArgumentException("Invalid name: " + str);
}
return str.substring(pos + 1);
}
public void undeployVerticle(String deploymentID, Handler<AsyncResult<Void>> completionHandler) {
Deployment deployment = deployments.get(deploymentID);
Context currentContext = vertx.getOrCreateContext();
if (deployment == null) {
reportFailure(new IllegalStateException("Unknown deployment"), currentContext, completionHandler);
} else {
deployment.undeploy(completionHandler);
}
}
public Set<String> deployments() {
return Collections.unmodifiableSet(deployments.keySet());
}
public Deployment getDeployment(String deploymentID) {
return deployments.get(deploymentID);
}
public void undeployAll(Handler<AsyncResult<Void>> completionHandler) {
// TODO timeout if it takes too long - e.g. async stop verticle fails to call future
// We only deploy the top level verticles as the children will be undeployed when the parent is
Set<String> deploymentIDs = new HashSet<>();
for (Map.Entry<String, Deployment> entry: deployments.entrySet()) {
if (!entry.getValue().isChild()) {
deploymentIDs.add(entry.getKey());
}
}
if (!deploymentIDs.isEmpty()) {
AtomicInteger count = new AtomicInteger(0);
for (String deploymentID : deploymentIDs) {
undeployVerticle(deploymentID, ar -> {
if (ar.failed()) {
// Log but carry on regardless
log.error("Undeploy failed", ar.cause());
}
if (count.incrementAndGet() == deploymentIDs.size()) {
completionHandler.handle(Future.succeededFuture());
}
});
}
} else {
Context context = vertx.getOrCreateContext();
context.runOnContext(v -> completionHandler.handle(Future.succeededFuture()));
}
}
public void registerVerticleFactory(VerticleFactory factory) {
String prefix = factory.prefix();
if (prefix == null) {
throw new IllegalArgumentException("factory.prefix() cannot be null");
}
List<VerticleFactory> facts = verticleFactories.get(prefix);
if (facts == null) {
facts = new ArrayList<>();
verticleFactories.put(prefix, facts);
}
if (facts.contains(factory)) {
throw new IllegalArgumentException("Factory already registered");
}
facts.add(factory);
// Sort list in ascending order
facts.sort((fact1, fact2) -> fact1.order() - fact2.order());
factory.init(vertx);
}
public void unregisterVerticleFactory(VerticleFactory factory) {
String prefix = factory.prefix();
if (prefix == null) {
throw new IllegalArgumentException("factory.prefix() cannot be null");
}
List<VerticleFactory> facts = verticleFactories.get(prefix);
boolean removed = false;
if (facts != null) {
if (facts.remove(factory)) {
removed = true;
}
if (facts.isEmpty()) {
verticleFactories.remove(prefix);
}
}
if (!removed) {
throw new IllegalArgumentException("factory isn't registered");
}
}
public Set<VerticleFactory> verticleFactories() {
Set<VerticleFactory> facts = new HashSet<>();
for (List<VerticleFactory> list: verticleFactories.values()) {
facts.addAll(list);
}
return facts;
}
private List<VerticleFactory> resolveFactories(String identifier) {
/*
We resolve the verticle factory list to use as follows:
1. We look for a prefix in the identifier.
E.g. the identifier might be "js:app.js" <-- the prefix is "js"
If it exists we use that to lookup the verticle factory list
2. We look for a suffix (like a file extension),
E.g. the identifier might be just "app.js"
If it exists we use that to lookup the factory list
3. If there is no prefix or suffix OR there is no match then defaults will be used
*/
List<VerticleFactory> factoryList = null;
int pos = identifier.indexOf(':');
String lookup = null;
if (pos != -1) {
// Infer factory from prefix, e.g. "java:" or "js:"
lookup = identifier.substring(0, pos);
} else {
// Try and infer name from extension
pos = identifier.lastIndexOf('.');
if (pos != -1) {
lookup = getSuffix(pos, identifier);
} else {
// No prefix, no extension - use defaults
factoryList = defaultFactories;
}
}
if (factoryList == null) {
factoryList = verticleFactories.get(lookup);
if (factoryList == null) {
factoryList = defaultFactories;
}
}
return factoryList;
}
private ClassLoader getClassLoader(DeploymentOptions options, ContextImpl parentContext) {
String isolationGroup = options.getIsolationGroup();
ClassLoader cl;
if (isolationGroup == null) {
cl = getCurrentClassLoader();
} else {
synchronized (this) {
cl = classloaders.get(isolationGroup);
if (cl == null) {
ClassLoader current = getCurrentClassLoader();
if (!(current instanceof URLClassLoader)) {
throw new IllegalStateException("Current classloader must be URLClassLoader");
}
List<URL> urls = new ArrayList<>();
// Add any extra URLs to the beginning of the classpath
List<String> extraClasspath = options.getExtraClasspath();
if (extraClasspath != null) {
for (String pathElement: extraClasspath) {
File file = new File(pathElement);
try {
URL url = file.toURI().toURL();
urls.add(url);
} catch (MalformedURLException e) {
throw new IllegalStateException(e);
}
}
}
// And add the URLs of the Vert.x classloader
URLClassLoader urlc = (URLClassLoader)current;
urls.addAll(Arrays.asList(urlc.getURLs()));
// Create an isolating cl with the urls
cl = new IsolatingClassLoader(urls.toArray(new URL[urls.size()]), getCurrentClassLoader(),
options.getIsolatedClasses());
classloaders.put(isolationGroup, cl);
}
}
}
return cl;
}
private ClassLoader getCurrentClassLoader() {
ClassLoader cl = Thread.currentThread().getContextClassLoader();
if (cl == null) {
cl = getClass().getClassLoader();
}
return cl;
}
private <T> void reportFailure(Throwable t, Context context, Handler<AsyncResult<T>> completionHandler) {
if (completionHandler != null) {
reportResult(context, completionHandler, Future.failedFuture(t));
} else {
log.error(t.getMessage(), t);
}
}
private <T> void reportSuccess(T result, Context context, Handler<AsyncResult<T>> completionHandler) {
if (completionHandler != null) {
reportResult(context, completionHandler, Future.succeededFuture(result));
}
}
private <T> void reportResult(Context context, Handler<AsyncResult<T>> completionHandler, AsyncResult<T> result) {
context.runOnContext(v -> {
try {
completionHandler.handle(result);
} catch (Throwable t) {
log.error("Failure in calling handler", t);
throw t;
}
});
}
private void doDeploy(String identifier, String deploymentID, DeploymentOptions options,
ContextImpl parentContext,
ContextImpl callingContext,
Handler<AsyncResult<String>> completionHandler,
ClassLoader tccl, Verticle... verticles) {
JsonObject conf = options.getConfig() == null ? new JsonObject() : options.getConfig().copy(); // Copy it
String poolName = options.getWorkerPoolName();
Deployment parent = parentContext.getDeployment();
DeploymentImpl deployment = new DeploymentImpl(parent, deploymentID, identifier, options);
AtomicInteger deployCount = new AtomicInteger();
AtomicBoolean failureReported = new AtomicBoolean();
for (Verticle verticle: verticles) {
WorkerExecutorImpl workerExec = poolName != null ? vertx.createSharedWorkerExecutor(poolName, options.getWorkerPoolSize()) : null;
WorkerPool pool = workerExec != null ? workerExec.getPool() : null;
ContextImpl context = options.isWorker() ? vertx.createWorkerContext(options.isMultiThreaded(), deploymentID, pool, conf, tccl) :
vertx.createEventLoopContext(deploymentID, pool, conf, tccl);
if (workerExec != null) {
context.addCloseHook(workerExec);
}
context.setDeployment(deployment);
deployment.addVerticle(new VerticleHolder(verticle, context));
context.runOnContext(v -> {
try {
verticle.init(vertx, context);
Future<Void> startFuture = Future.future();
verticle.start(startFuture);
startFuture.setHandler(ar -> {
if (ar.succeeded()) {
if (parent != null) {
parent.addChild(deployment);
deployment.child = true;
}
VertxMetrics metrics = vertx.metricsSPI();
if (metrics != null) {
metrics.verticleDeployed(verticle);
}
deployments.put(deploymentID, deployment);
if (deployCount.incrementAndGet() == verticles.length) {
reportSuccess(deploymentID, callingContext, completionHandler);
}
} else if (!failureReported.get()) {
reportFailure(ar.cause(), callingContext, completionHandler);
}
});
} catch (Throwable t) {
reportFailure(t, callingContext, completionHandler);
}
});
}
}
static class VerticleHolder {
final Verticle verticle;
final ContextImpl context;
VerticleHolder(Verticle verticle, ContextImpl context) {
this.verticle = verticle;
this.context = context;
}
}
private class DeploymentImpl implements Deployment {
private final Deployment parent;
private final String deploymentID;
private final String verticleIdentifier;
private final List<VerticleHolder> verticles = new CopyOnWriteArrayList<>();
private final Set<Deployment> children = new ConcurrentHashSet<>();
private final DeploymentOptions options;
private boolean undeployed;
private volatile boolean child;
private DeploymentImpl(Deployment parent, String deploymentID, String verticleIdentifier, DeploymentOptions options) {
this.parent = parent;
this.deploymentID = deploymentID;
this.verticleIdentifier = verticleIdentifier;
this.options = options;
}
public void addVerticle(VerticleHolder holder) {
verticles.add(holder);
}
@Override
public void undeploy(Handler<AsyncResult<Void>> completionHandler) {
ContextImpl currentContext = vertx.getOrCreateContext();
doUndeploy(currentContext, completionHandler);
}
public synchronized void doUndeploy(ContextImpl undeployingContext, Handler<AsyncResult<Void>> completionHandler) {
if (undeployed) {
reportFailure(new IllegalStateException("Already undeployed"), undeployingContext, completionHandler);
return;
}
if (!children.isEmpty()) {
final int size = children.size();
AtomicInteger childCount = new AtomicInteger();
boolean undeployedSome = false;
for (Deployment childDeployment: new HashSet<>(children)) {
undeployedSome = true;
childDeployment.doUndeploy(undeployingContext, ar -> {
children.remove(childDeployment);
if (ar.failed()) {
reportFailure(ar.cause(), undeployingContext, completionHandler);
} else if (childCount.incrementAndGet() == size) {
// All children undeployed
doUndeploy(undeployingContext, completionHandler);
}
});
}
if (!undeployedSome) {
// It's possible that children became empty before iterating
doUndeploy(undeployingContext, completionHandler);
}
} else {
undeployed = true;
AtomicInteger undeployCount = new AtomicInteger();
int numToUndeploy = verticles.size();
for (VerticleHolder verticleHolder: verticles) {
ContextImpl context = verticleHolder.context;
context.runOnContext(v -> {
Future<Void> stopFuture = Future.future();
AtomicBoolean failureReported = new AtomicBoolean();
stopFuture.setHandler(ar -> {
deployments.remove(deploymentID);
VertxMetrics metrics = vertx.metricsSPI();
if (metrics != null) {
metrics.verticleUndeployed(verticleHolder.verticle);
}
context.runCloseHooks(ar2 -> {
if (ar2.failed()) {
// Log error but we report success anyway
log.error("Failed to run close hook", ar2.cause());
}
if (ar.succeeded() && undeployCount.incrementAndGet() == numToUndeploy) {
reportSuccess(null, undeployingContext, completionHandler);
} else if (ar.failed() && !failureReported.get()) {
failureReported.set(true);
reportFailure(ar.cause(), undeployingContext, completionHandler);
}
});
});
try {
verticleHolder.verticle.stop(stopFuture);
} catch (Throwable t) {
stopFuture.fail(t);
} finally {
// Remove the deployment from any parents
if (parent != null) {
parent.removeChild(this);
}
}
});
}
}
}
@Override
public String verticleIdentifier() {
return verticleIdentifier;
}
@Override
public DeploymentOptions deploymentOptions() {
return options;
}
@Override
public void addChild(Deployment deployment) {
children.add(deployment);
}
@Override
public void removeChild(Deployment deployment) {
children.remove(deployment);
}
@Override
public Set<Verticle> getVerticles() {
Set<Verticle> verts = new HashSet<>();
for (VerticleHolder holder: verticles) {
verts.add(holder.verticle);
}
return verts;
}
@Override
public boolean isChild() {
return child;
}
@Override
public String deploymentID() {
return deploymentID;
}
}
}