Package net.kuujo.vertigo.network

Examples of net.kuujo.vertigo.network.NetworkContext


    // When deploying a bare network, we first attempt to load any existing
    // configuration from the cluster. This ensures that we don't overwrite
    // a cluster configuration. In some cases, the manager can be redeployed
    // by deploying a network name.
    String scontext = data.<String, String>getMap(String.format("%s.%s", cluster, name)).get(String.format("%s.%s", cluster, name));
    final NetworkContext context = scontext != null ? Contexts.<NetworkContext>deserialize(new JsonObject(scontext)) : ContextBuilder.buildContext(new DefaultNetworkConfig(name), cluster);

    // Simply deploy an empty network.
    if (!managers.containsKey(context.address())) {
      // If the network manager hasn't yet been deployed then deploy the manager
      // and then update the network's configuration.
      platform.deployVerticle(NetworkManager.class.getName(), new JsonObject().putString("cluster", cluster).putString("address", context.address()), 1, new Handler<AsyncResult<String>>() {
        @Override
        public void handle(AsyncResult<String> result) {
          if (result.failed()) {
            message.reply(new JsonObject().putString("status", "error").putString("message", "Failed to deploy network manager."));
          } else {
            // Once the manager has been deployed we can add the network name to
            // the set of networks that are deployed in the cluster.
            final String deploymentID = result.result();
            DefaultNodeManager.this.context.execute(new Action<Void>() {
              @Override
              public Void perform() {
                networks.add(context.name());
                return null;
              }
            }, new Handler<AsyncResult<Void>>() {
              @Override
              public void handle(AsyncResult<Void> result) {
                // And store the manager's deployment ID in the local managers map.
                managers.put(context.address(), deploymentID);
                doDeployNetwork(context, message);
              }
            });
          }
        }
View Full Code Here


    String scontext = data.<String, String>getMap(String.format("%s.%s", cluster, network.getName())).get(String.format("%s.%s", cluster, network.getName()));

    // Create the new network context. If a context already existed in the cluster
    // then merge the new configuration with the existing configuration. Otherwise
    // just build a network context.
    NetworkContext updatedContext;
    if (scontext != null) {
      updatedContext = ContextBuilder.buildContext(Configs.mergeNetworks(Contexts.<NetworkContext>deserialize(new JsonObject(scontext)).config(), network), cluster);
    } else {
      updatedContext = ContextBuilder.buildContext(network, cluster);
    }

    final NetworkContext context = updatedContext;

    // If the network's manager is deployed then its deployment ID will have
    // been stored in the local managers map.
    if (!managers.containsKey(context.address())) {
      // If the network manager hasn't yet been deployed then deploy the manager
      // and then update the network's configuration.
      platform.deployVerticle(NetworkManager.class.getName(), new JsonObject().putString("cluster", cluster).putString("address", context.address()), 1, new Handler<AsyncResult<String>>() {
        @Override
        public void handle(AsyncResult<String> result) {
          if (result.failed()) {
            message.reply(new JsonObject().putString("status", "error").putString("message", "Failed to deploy network manager."));
          } else {
            // Once the manager has been deployed we can add the network name to
            // the set of networks that are deployed in the cluster.
            final String deploymentID = result.result();
            DefaultNodeManager.this.context.execute(new Action<Void>() {
              @Override
              public Void perform() {
                networks.add(context.name());
                return null;
              }
            }, new Handler<AsyncResult<Void>>() {
              @Override
              public void handle(AsyncResult<Void> result) {
                // And store the manager's deployment ID in the local managers map.
                managers.put(context.address(), deploymentID);
                doDeployNetwork(context, message);
              }
            });
          }
        }
View Full Code Here

    if (scontext == null) {
      message.reply(new JsonObject().putString("status", "error").putString("message", "Network is not deployed."));
    } else {
      // If the network's manager is deployed locally then its deployment ID
      // will have been stored in the local managers map.
      final NetworkContext context = Contexts.<NetworkContext>deserialize(new JsonObject(scontext));
      if (managers.containsKey(context.address())) {
        // Now that we have the network's context we need to watch the network's
        // status key before we begin undeploying the network. Once we unset the
        // network's configuration key the network's manager will automatically
        // begin undeploying the network.
        data.watch(context.status(), MapEvent.Type.CHANGE, new Handler<MapEvent<String, String>>() {
          @Override
          public void handle(MapEvent<String, String> event) {
            // When the network's status key is set to an empty string, that indicates
            // that the network's manager has completely undeployed all components and
            // connections within the network. The manager is the only element of the
            // network left running, so we can go ahead and undeploy it.
            if (event.value() != null && event.value().equals("")) {
              // First, stop watching the status key so this handler doesn't accidentally
              // get called again for some reason.
              data.unwatch(context.status(), MapEvent.Type.CHANGE, this, new Handler<AsyncResult<Void>>() {
                @Override
                public void handle(AsyncResult<Void> result) {
                  // Now undeploy the manager from the context cluster. Once the manager
                  // has been undeployed the undeployment of the network is complete.
                  platform.undeployVerticle(managers.remove(context.address()), new Handler<AsyncResult<Void>>() {
                    @Override
                    public void handle(AsyncResult<Void> result) {
                      if (result.failed()) {
                        message.reply(new JsonObject().putString("status", "error").putString("message", result.cause().getMessage()));
                      } else {
                        // We can be nice and unset the status key since the manager was undeployed :-)
                        DefaultNodeManager.this.context.execute(new Action<Void>() {
                          @Override
                          public Void perform() {
                            networks.remove(context.address());
                            data.remove(context.status());
                            return null;
                          }
                        }, new Handler<AsyncResult<Void>>() {
                          @Override
                          public void handle(AsyncResult<Void> result) {
                            message.reply(new JsonObject().putString("status", "ok"));
                          }
                        });
                      }
                    }
                  });
                }
              });
            }
          }
        }, new Handler<AsyncResult<Void>>() {
          @Override
          public void handle(AsyncResult<Void> result) {
            // Once we've started watching the status key, unset the network's context in
            // the cluster. The network's manager will be notified of the configuration
            // change and will begin undeploying all of its components. Once the components
            // have been undeployed it will reset its status key.
            if (result.failed()) {
              message.reply(new JsonObject().putString("status", "error").putString("message", result.cause().getMessage()));
            } else {
              DefaultNodeManager.this.context.run(new Runnable() {
                @Override
                public void run() {
                  data.remove(context.address());
                }
              });
            }
          }
        });
View Full Code Here

    String scontext = data.get(String.format("%s.%s", cluster, network.getName()));
    if (scontext == null) {
      message.reply(new JsonObject().putString("status", "error").putString("message", "Network is not deployed."));
    } else {
      // Determine whether the network's manager is deployed.
      final NetworkContext tempContext = Contexts.<NetworkContext>deserialize(new JsonObject(scontext));
      if (managers.containsKey(tempContext.address())) {
        // If the network's manager is deployed then unmerge the given
        // configuration from the configuration of the network that's
        // already running. If the resulting configuration is a no-component
        // network then that indicates that the entire network is being undeployed.
        // Otherwise, we simply update the existing configuration and allow the
        // network's manager to handle deployment and undeployment of components.
        NetworkConfig updatedConfig = Configs.unmergeNetworks(tempContext.config(), network);
        final NetworkContext context = ContextBuilder.buildContext(updatedConfig, cluster);

        // If the new configuration has no components then undeploy the entire network.
        if (context.components().isEmpty()) {
          // We need to watch the network's status key to determine when the network's
          // components have been completely undeployed. Once that happens it's okay
          // to then undeploy the network's manager.
          data.watch(context.status(), MapEvent.Type.CHANGE, new Handler<MapEvent<String, String>>() {
            @Override
            public void handle(MapEvent<String, String> event) {
              // When the network's status key is set to an empty string, that indicates
              // that the network's manager has completely undeployed all components and
              // connections within the network. The manager is the only element of the
              // network left running, so we can go ahead and undeploy it.
              if (event.value() != null && event.value().equals("")) {
                // First, stop watching the status key so this handler doesn't accidentally
                // get called again for some reason.
                data.unwatch(context.status(), MapEvent.Type.CHANGE, this, new Handler<AsyncResult<Void>>() {
                  @Override
                  public void handle(AsyncResult<Void> result) {
                    // Now undeploy the manager from the context cluster. Once the manager
                    // has been undeployed the undeployment of the network is complete.
                    platform.undeployVerticle(managers.remove(context.address()), new Handler<AsyncResult<Void>>() {
                      @Override
                      public void handle(AsyncResult<Void> result) {
                        if (result.failed()) {
                          message.reply(new JsonObject().putString("status", "error").putString("message", result.cause().getMessage()));
                        } else {
                          // We can be nice and unset the status key since the manager was undeployed :-)
                          DefaultNodeManager.this.context.execute(new Action<Void>() {
                            @Override
                            public Void perform() {
                              networks.remove(context.address());
                              data.remove(context.status());
                              return null;
                            }
                          }, new Handler<AsyncResult<Void>>() {
                            @Override
                            public void handle(AsyncResult<Void> result) {
                              message.reply(new JsonObject().putString("status", "ok"));
                            }
                          });
                        }
                      }
                    });
                  }
                });
              }
            }
          }, new Handler<AsyncResult<Void>>() {
            @Override
            public void handle(AsyncResult<Void> result) {
              // Once we've started watching the status key, unset the network's context in
              // the cluster. The network's manager will be notified of the configuration
              // change and will begin undeploying all of its components. Once the components
              // have been undeployed it will reset its status key.
              if (result.failed()) {
                message.reply(new JsonObject().putString("status", "error").putString("message", result.cause().getMessage()));
              } else {
                DefaultNodeManager.this.context.run(new Runnable() {
                  @Override
                  public void run() {
                    data.remove(context.address());
                  }
                });
              }
            }
          });
        } else {
          // If only a portion of the running network is being undeployed, we need
          // to watch the network's status key for notification once the configuration
          // change is complete.
          data.watch(context.status(), MapEvent.Type.CHANGE, new Handler<MapEvent<String, String>>() {
            @Override
            public void handle(MapEvent<String, String> event) {
              if (event.value() != null && event.value().equals(context.version())) {
                // The network's configuration has been completely updated. Stop watching
                // the network's status key and call the async handler.
                data.unwatch(context.status(), null, this, new Handler<AsyncResult<Void>>() {
                  @Override
                  public void handle(AsyncResult<Void> result) {
                    message.reply(new JsonObject().putString("status", "ok").putObject("context", Contexts.serialize(context)));
                  }
                });
              }
            }
          }, new Handler<AsyncResult<Void>>() {
            @Override
            public void handle(AsyncResult<Void> result) {
              // Once we've started watching the status key, update the network's context in
              // the cluster. The network's manager will be notified of the configuration
              // change and will begin deploying or undeploying components and connections
              // as necessary.
              if (result.failed()) {
                message.reply(new JsonObject().putString("status", "error").putString("message", result.cause().getMessage()));
              } else {
                DefaultNodeManager.this.context.run(new Runnable() {
                  @Override
                  public void run() {
                    data.put(context.address(), Contexts.serialize(context).encode());
                  }
                });
              }
            }
          });
View Full Code Here

  @Test
  public void testDefaultFeederVerticleContext() {
    DefaultNetworkConfig network = new DefaultNetworkConfig("test");
    network.addVerticle("feeder", "feeder.py");
    NetworkContext context = ContextBuilder.buildContext(network, "vertigo");
    assertEquals("vertigo.test", context.address());
    VerticleContext verticleContext = context.component("feeder");
    assertEquals("feeder", verticleContext.name());
    assertEquals("vertigo.test.feeder", verticleContext.address());
    assertEquals("feeder.py", verticleContext.main());
    assertTrue(verticleContext.isVerticle());
    assertFalse(verticleContext.isModule());
View Full Code Here

    verticle.setConfig(new JsonObject().putString("foo", "bar"));
    verticle.setInstances(2);
    verticle.setGroup("test");
    verticle.setWorker(true);
    verticle.setMultiThreaded(true);
    NetworkContext context = ContextBuilder.buildContext(network, "vertigo");
    assertEquals("vertigo.test", context.address());
    VerticleContext verticleContext = context.component("feeder");
    assertEquals("feeder", verticleContext.name());
    assertEquals("vertigo.test.feeder", verticleContext.address());
    assertEquals("feeder.py", verticleContext.main());
    assertTrue(verticleContext.isVerticle());
    assertFalse(verticleContext.isModule());
View Full Code Here

  @Test
  public void testDefaultFeederModuleContext() {
    DefaultNetworkConfig network = new DefaultNetworkConfig("test");
    network.addModule("feeder", "com.test~test-module~1.0");
    NetworkContext context = ContextBuilder.buildContext(network, "vertigo");
    assertEquals("vertigo.test", context.address());
    ModuleContext moduleContext = context.component("feeder");
    assertEquals("feeder", moduleContext.name());
    assertEquals("vertigo.test.feeder", moduleContext.address());
    assertEquals("com.test~test-module~1.0", moduleContext.module());
    assertFalse(moduleContext.isVerticle());
    assertTrue(moduleContext.isModule());
View Full Code Here

    ModuleConfig verticle = network.addModule("feeder", "com.test~test-module~1.0");
    verticle.setModule("com.test~test-module~1.0");
    verticle.setConfig(new JsonObject().putString("foo", "bar"));
    verticle.setInstances(2);
    verticle.setGroup("test");
    NetworkContext context = ContextBuilder.buildContext(network, "vertigo");
    assertEquals("vertigo.test", context.address());
    ModuleContext moduleContext = context.component("feeder");
    assertEquals("feeder", moduleContext.name());
    assertEquals("vertigo.test.feeder", moduleContext.address());
    assertEquals("com.test~test-module~1.0", moduleContext.module());
    assertFalse(moduleContext.isVerticle());
    assertTrue(moduleContext.isModule());
View Full Code Here

  @Test
  public void testDefaultWorkerVerticleContext() {
    DefaultNetworkConfig network = new DefaultNetworkConfig("test");
    network.addVerticle("worker", "worker.py");
    NetworkContext context = ContextBuilder.buildContext(network, "vertigo");
    assertEquals("vertigo.test", context.address());
    VerticleContext verticleContext = context.component("worker");
    assertEquals("worker", verticleContext.name());
    assertEquals("vertigo.test.worker", verticleContext.address());
    assertEquals("worker.py", verticleContext.main());
    assertTrue(verticleContext.isVerticle());
    assertFalse(verticleContext.isModule());
View Full Code Here

    verticle.setConfig(new JsonObject().putString("foo", "bar"));
    verticle.setInstances(2);
    verticle.setGroup("test");
    verticle.setWorker(true);
    verticle.setMultiThreaded(true);
    NetworkContext context = ContextBuilder.buildContext(network, "vertigo");
    assertEquals("vertigo.test", context.address());
    VerticleContext verticleContext = context.component("worker");
    assertEquals("worker", verticleContext.name());
    assertEquals("vertigo.test.worker", verticleContext.address());
    assertEquals("worker.py", verticleContext.main());
    assertTrue(verticleContext.isVerticle());
    assertFalse(verticleContext.isModule());
View Full Code Here

TOP

Related Classes of net.kuujo.vertigo.network.NetworkContext

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.