/**
   * Tests that one process claims a coordinate, then another process tries to claim the same
   * coordinate. The first coordinate looses connection to ZooKeeper and the other process gets the
   * coordinate.
   *
   * @throws Exception
   */
  @Test
  public void testFastHardRestart() throws Exception {
    final Coordinate c = Coordinate.parse("1.service.user.cell");
    final CountDownLatch claimLatch1 = new CountDownLatch(1);
    forwarderPort = Net.getFreePort();
    forwarder = new PortForwarder(forwarderPort, "127.0.0.1", zkport);
    Cloudname cn1 =
        new ZkCloudname.Builder().setConnectString("localhost:" + forwarderPort).build().connect();
    cn1.createCoordinate(c);

    ServiceHandle handle1 = cn1.claim(c);
    handle1.registerCoordinateListener(
        new CoordinateListener() {

          @Override
          public void onCoordinateEvent(Event event, String message) {
            if (event == Event.COORDINATE_OK) {
              claimLatch1.countDown();
            }
          }
        });
    assertTrue(claimLatch1.await(5, TimeUnit.SECONDS));

    Cloudname cn2 =
        new ZkCloudname.Builder().setConnectString("localhost:" + zkport).build().connect();

    ServiceHandle handle2 = cn2.claim(c);

    forwarder.terminate();

    assertTrue(handle2.waitForCoordinateOkSeconds(20));

    ServiceStatus status = new ServiceStatus(ServiceState.RUNNING, "updated status");
    handle2.setStatus(status);

    Cloudname cn3 =
        new ZkCloudname.Builder().setConnectString("localhost:" + zkport).build().connect();
    ServiceStatus statusRetrieved = cn3.getStatus(c);
    assertEquals("updated status", statusRetrieved.getMessage());
  }
示例#2
0
  private static void handleCoordinateOperation() {
    final Resolver resolver = cloudname.getResolver();
    final Coordinate coordinate = Coordinate.parse(coordinateFlag);
    switch (operationFlag) {
      case CREATE:
        try {
          cloudname.createCoordinate(coordinate);
        } catch (CloudnameException e) {
          System.err.println("Got error: " + e.getMessage());
          break;
        } catch (CoordinateExistsException e) {
          e.printStackTrace();
          break;
        }
        System.err.println("Created coordinate.");
        break;
      case DELETE:
        try {
          cloudname.destroyCoordinate(coordinate);
        } catch (CoordinateDeletionException e) {
          System.err.println("Got error: " + e.getMessage());
          return;
        } catch (CoordinateMissingException e) {
          System.err.println("Got error: " + e.getMessage());
          break;
        } catch (CloudnameException e) {
          System.err.println("Got error: " + e.getMessage());
          break;
        }
        System.err.println("Deleted coordinate.");
        break;
      case STATUS:
        {
          ServiceStatus status;
          try {
            status = cloudname.getStatus(coordinate);
          } catch (CloudnameException e) {
            System.err.println(
                "Problems loading status, is service running? Error:\n" + e.getMessage());
            break;
          }
          System.err.println(
              "Status:\n" + status.getState().toString() + " " + status.getMessage());
          List<Endpoint> endpoints = null;
          try {
            endpoints =
                resolver.resolve(
                    "all."
                        + coordinate.getService()
                        + "."
                        + coordinate.getUser()
                        + "."
                        + coordinate.getCell());
          } catch (CloudnameException e) {
            System.err.println("Got error: " + e.getMessage());
            break;
          }
          System.err.println("Endpoints:");
          for (Endpoint endpoint : endpoints) {
            if (endpoint.getCoordinate().getInstance() == coordinate.getInstance()) {
              System.err.println(
                  endpoint.getName()
                      + "-->"
                      + endpoint.getHost()
                      + ":"
                      + endpoint.getPort()
                      + " protocol:"
                      + endpoint.getProtocol());
              System.err.println("Endpoint data:\n" + endpoint.getEndpointData());
            }
          }
          break;
        }
      case HOST:
        {
          List<Endpoint> endpoints = null;
          try {
            endpoints = resolver.resolve(coordinate.asString());
          } catch (CloudnameException e) {
            System.err.println(
                "Could not resolve " + coordinate.asString() + " Error:\n" + e.getMessage());
            break;
          }
          for (Endpoint endpoint : endpoints) {
            System.out.println("Host: " + endpoint.getHost());
          }
        }
        break;
      case SET_CONFIG:
        try {
          cloudname.setConfig(coordinate, configFlag, null);
        } catch (CloudnameException e) {
          System.err.println("Got error: " + e.getMessage());
          break;

        } catch (CoordinateMissingException e) {
          System.err.println("Non-existing coordinate.");
        }
        System.err.println("Config updated.");
        break;

      case READ_CONFIG:
        try {
          System.out.println("Config is:" + cloudname.getConfig(coordinate));
        } catch (CoordinateMissingException e) {
          System.err.println("Non-existing coordinate.");
        } catch (CloudnameException e) {
          System.err.println("Problem with cloudname: " + e.getMessage());
        }
        break;
      default:
        System.out.println("Unknown command " + operationFlag);
    }
  }