Skip to content

Commit

Permalink
redeploy
Browse files Browse the repository at this point in the history
  • Loading branch information
purplefox committed Jul 27, 2012
1 parent bd3626e commit 8ee8490
Show file tree
Hide file tree
Showing 10 changed files with 171 additions and 199 deletions.
@@ -0,0 +1,7 @@
load("vertx.js")

console.log("in test-mod1");

vertx.createHttpServer().requestHandler(function(req) {
req.response.end("<html><body><h1>Hello from vert.x!</h1></body></html>");
}).listen(8080, 'localhost');
@@ -0,0 +1,4 @@
{
"main": "app.js",
"auto-redeploy": true
}
Expand Up @@ -60,6 +60,9 @@ private boolean isJavaSource(String main) {
} }


public Verticle createVerticle(String main, ClassLoader loader) throws Exception { public Verticle createVerticle(String main, ClassLoader loader) throws Exception {

System.out.println("creating verticle " + main);

ClassLoader cl = loader; ClassLoader cl = loader;
String className = main; String className = main;
if (isJavaSource(main)) { if (isJavaSource(main)) {
Expand Down
Expand Up @@ -89,7 +89,7 @@ public void deployWorkerVerticle(String main, JsonObject config, int instances)
public void deployWorkerVerticle(String main, JsonObject config, int instances, Handler<String> doneHandler) { public void deployWorkerVerticle(String main, JsonObject config, int instances, Handler<String> doneHandler) {
URL[] currURLs = mgr.getDeploymentURLs(); URL[] currURLs = mgr.getDeploymentURLs();
File modDir = mgr.getDeploymentModDir(); File modDir = mgr.getDeploymentModDir();
mgr.deploy(true, main, config, currURLs, instances, modDir, doneHandler); mgr.deployVerticle(true, main, config, currURLs, instances, modDir, doneHandler);
} }


/** /**
Expand Down Expand Up @@ -186,7 +186,7 @@ public void deployVerticle(String main, JsonObject config, int instances) {
public void deployVerticle(String main, JsonObject config, int instances, Handler<String> doneHandler) { public void deployVerticle(String main, JsonObject config, int instances, Handler<String> doneHandler) {
URL[] currURLs = mgr.getDeploymentURLs(); URL[] currURLs = mgr.getDeploymentURLs();
File modDir = mgr.getDeploymentModDir(); File modDir = mgr.getDeploymentModDir();
mgr.deploy(false, main, config, currURLs, instances, modDir, doneHandler); mgr.deployVerticle(false, main, config, currURLs, instances, modDir, doneHandler);
} }


/** /**
Expand Down
Expand Up @@ -38,9 +38,11 @@ public class Deployment {
public final List<VerticleHolder> verticles = new ArrayList<>(); public final List<VerticleHolder> verticles = new ArrayList<>();
public final List<String> childDeployments = new ArrayList<>(); public final List<String> childDeployments = new ArrayList<>();
public final String parentDeploymentName; public final String parentDeploymentName;
public final boolean autoRedeploy;


public Deployment(String name, String modName, int instances, VerticleFactory factory, JsonObject config, public Deployment(String name, String modName, int instances, VerticleFactory factory, JsonObject config,
URL[] urls, File modDir, String parentDeploymentName) { URL[] urls, File modDir, String parentDeploymentName,
boolean autoRedeploy) {
this.name = name; this.name = name;
this.modName = modName; this.modName = modName;
this.instances = instances; this.instances = instances;
Expand All @@ -49,5 +51,6 @@ public Deployment(String name, String modName, int instances, VerticleFactory fa
this.urls = urls; this.urls = urls;
this.modDir = modDir; this.modDir = modDir;
this.parentDeploymentName = parentDeploymentName; this.parentDeploymentName = parentDeploymentName;
this.autoRedeploy = autoRedeploy;
} }
} }
Expand Up @@ -56,7 +56,6 @@ public class Redeployer {


private final File modRoot; private final File modRoot;
private final ModuleReloader reloader; private final ModuleReloader reloader;
private final Map<String, Deployment> deployments = new HashMap<>();
private final Map<Path, Set<Deployment>> watchedDeployments = new HashMap<>(); private final Map<Path, Set<Deployment>> watchedDeployments = new HashMap<>();
private final Map<WatchKey, Path> watchKeys = new HashMap<>(); private final Map<WatchKey, Path> watchKeys = new HashMap<>();
private final Map<Path, Path> moduleDirs = new HashMap<>(); private final Map<Path, Path> moduleDirs = new HashMap<>();
Expand All @@ -68,24 +67,6 @@ public class Redeployer {
private final Queue<Deployment> toUndeploy = new ConcurrentLinkedQueue<>(); private final Queue<Deployment> toUndeploy = new ConcurrentLinkedQueue<>();
private Context ctx; private Context ctx;


private void dumpSizes() {
log.info("watchkeys: " + watchKeys.size());
log.info("moduleDirs: " + moduleDirs.size());
log.info("changing: " + changing.size());
int size = 0;
for (Set<Deployment> s: this.watchedDeployments.values()) {
size += s.size();
}
log.info("watcheddeployments:" + size);
}

private void checkContext() {
//Sanity check
if (Context.getContext() != ctx) {
throw new IllegalStateException("Got context: " + Context.getContext() + " expected " + ctx);
}
}

public Redeployer(Vertx vertx, File modRoot, ModuleReloader reloader) { public Redeployer(Vertx vertx, File modRoot, ModuleReloader reloader) {
this.modRoot = modRoot; this.modRoot = modRoot;
this.reloader = reloader; this.reloader = reloader;
Expand Down Expand Up @@ -118,12 +99,10 @@ public void close() {
} }


public void moduleDeployed(Deployment deployment) { public void moduleDeployed(Deployment deployment) {
log.info("module deployed " + deployment.name);
toDeploy.add(deployment); toDeploy.add(deployment);
} }


public void moduleUndeployed(Deployment deployment) { public void moduleUndeployed(Deployment deployment) {
log.info("module undeployed " + deployment.name);
toUndeploy.add(deployment); toUndeploy.add(deployment);
} }


Expand All @@ -134,7 +113,6 @@ public void moduleUndeployed(Deployment deployment) {
private void processDeployments() { private void processDeployments() {
Deployment dep; Deployment dep;
while ((dep = toDeploy.poll()) != null) { while ((dep = toDeploy.poll()) != null) {
deployments.put(dep.name, dep);
File fmodDir = new File(modRoot, dep.modName); File fmodDir = new File(modRoot, dep.modName);
Path modDir = fmodDir.toPath(); Path modDir = fmodDir.toPath();
Set<Deployment> deps = watchedDeployments.get(modDir); Set<Deployment> deps = watchedDeployments.get(modDir);
Expand All @@ -156,14 +134,11 @@ private void processDeployments() {
private void processUndeployments() { private void processUndeployments() {
Deployment dep; Deployment dep;
while ((dep = toUndeploy.poll()) != null) { while ((dep = toUndeploy.poll()) != null) {
log.info("processing undeployment");
deployments.remove(dep.name);
File modDir = new File(modRoot, dep.modName); File modDir = new File(modRoot, dep.modName);
Path pModDir = modDir.toPath(); Path pModDir = modDir.toPath();
Set<Deployment> deps = watchedDeployments.get(pModDir); Set<Deployment> deps = watchedDeployments.get(pModDir);
deps.remove(dep); deps.remove(dep);
if (deps.isEmpty()) { if (deps.isEmpty()) {
log.info("unwatching module " + dep.modName);
watchedDeployments.remove(pModDir); watchedDeployments.remove(pModDir);
Set<Path> modPaths = new HashSet<>(); Set<Path> modPaths = new HashSet<>();
for (Map.Entry<Path, Path> entry: moduleDirs.entrySet()) { for (Map.Entry<Path, Path> entry: moduleDirs.entrySet()) {
Expand All @@ -186,7 +161,6 @@ private void processUndeployments() {
watchKeys.remove(key); watchKeys.remove(key);
} }
} }
this.dumpSizes();
} }
} }


Expand All @@ -210,35 +184,20 @@ void checkEvents() {
if (now - entry.getValue() > GRACE_PERIOD) { if (now - entry.getValue() > GRACE_PERIOD) {
// Module has changed but no changes for GRACE_PERIOD ms // Module has changed but no changes for GRACE_PERIOD ms
// we can assume the redeploy has finished // we can assume the redeploy has finished
log.info("module hasn't changed for 1000 ms so redeploy it");
toRedeploy.add(entry.getKey()); toRedeploy.add(entry.getKey());
} }
} }
if (!toRedeploy.isEmpty()) { if (!toRedeploy.isEmpty()) {
Set<Deployment> parents = new HashSet<>(); Set<Deployment> deployments = new HashSet<>();
for (Path moduleDir: toRedeploy) { for (Path moduleDir: toRedeploy) {
log.info("Module has changed - redeploying module from directory " + moduleDir.toString());
changing.remove(moduleDir); changing.remove(moduleDir);
computeParents(moduleDir, parents); deployments.addAll(watchedDeployments.get(moduleDir));
} }
reloader.reloadModules(parents); reloader.reloadModules(deployments);
} }
} }


// Some of the modules that need to be redeployed might have been programmatically
// deployed so we don't redeploy them directly. Instead we need to compute the
// set of parent modules which are the ones we need to redeploy
private void computeParents(Path modulePath, Set<Deployment> parents) {
Set<Deployment> deps = watchedDeployments.get(modulePath);
for (Deployment d: deps) {
if (d.parentDeploymentName != null) {
Deployment parent = deployments.get(d.parentDeploymentName);
File f = new File(modRoot, parent.modName);
computeParents(f.toPath(), parents);
} else {
parents.add(d);
}
}
}


private void handleEvent(WatchKey key, Set<Path> changed) { private void handleEvent(WatchKey key, Set<Path> changed) {
Path dir = watchKeys.get(key); Path dir = watchKeys.get(key);
Expand All @@ -254,21 +213,15 @@ private void handleEvent(WatchKey key, Set<Path> changed) {
continue; continue;
} }


log.info("got event in directory " + dir);

Path moduleDir = moduleDirs.get(dir); Path moduleDir = moduleDirs.get(dir);
log.info("module dir is " + moduleDir);


@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
WatchEvent<Path> ev = (WatchEvent<Path>) event; WatchEvent<Path> ev = (WatchEvent<Path>) event;
Path name = ev.context(); Path name = ev.context();


Path child = dir.resolve(name); Path child = dir.resolve(name);


if (kind == StandardWatchEventKinds.ENTRY_MODIFY) { if (kind == StandardWatchEventKinds.ENTRY_CREATE) {
log.info("entry modified: " + child);
} else if (kind == StandardWatchEventKinds.ENTRY_CREATE) {
log.info("entry created: " + child);
if (Files.isDirectory(child, LinkOption.NOFOLLOW_LINKS)) { if (Files.isDirectory(child, LinkOption.NOFOLLOW_LINKS)) {
try { try {
registerAll(moduleDir, child); registerAll(moduleDir, child);
Expand All @@ -278,7 +231,6 @@ private void handleEvent(WatchKey key, Set<Path> changed) {
} }
} }
} else if (kind == StandardWatchEventKinds.ENTRY_DELETE) { } else if (kind == StandardWatchEventKinds.ENTRY_DELETE) {
log.info("entry deleted: " + child);
moduleDirs.remove(child); moduleDirs.remove(child);
} }
changed.add(moduleDir); changed.add(moduleDir);
Expand All @@ -291,15 +243,13 @@ private void handleEvent(WatchKey key, Set<Path> changed) {
} }


private void register(Path modDir, Path dir) throws IOException { private void register(Path modDir, Path dir) throws IOException {
log.info("registering " + dir);
WatchKey key = dir.register(watchService, StandardWatchEventKinds.ENTRY_CREATE, WatchKey key = dir.register(watchService, StandardWatchEventKinds.ENTRY_CREATE,
StandardWatchEventKinds.ENTRY_MODIFY, StandardWatchEventKinds.ENTRY_DELETE); StandardWatchEventKinds.ENTRY_MODIFY, StandardWatchEventKinds.ENTRY_DELETE);
watchKeys.put(key, dir); watchKeys.put(key, dir);
moduleDirs.put(dir, modDir); moduleDirs.put(dir, modDir);
} }


private void registerAll(final Path modDir, final Path dir) throws IOException { private void registerAll(final Path modDir, final Path dir) throws IOException {
log.info("registering all " + modDir);
Files.walkFileTree(dir, new SimpleFileVisitor<Path>() { Files.walkFileTree(dir, new SimpleFileVisitor<Path>() {
@Override @Override
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException { public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
Expand All @@ -308,4 +258,23 @@ public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) th
} }
}); });
} }

// private void dumpSizes() {
// log.info("watchkeys: " + watchKeys.size());
// log.info("moduleDirs: " + moduleDirs.size());
// log.info("changing: " + changing.size());
// int size = 0;
// for (Set<Deployment> s: this.watchedDeployments.values()) {
// size += s.size();
// }
// log.info("watcheddeployments:" + size);
// }

private void checkContext() {
//Sanity check
if (Context.getContext() != ctx) {
throw new IllegalStateException("Got context: " + Context.getContext() + " expected " + ctx);
}
}

} }

0 comments on commit 8ee8490

Please sign in to comment.