Package org.apache.helix

Examples of org.apache.helix.HelixConnection


        || path.endsWith("zip");
  }

  public HelixConnection pollForConnection() {
    String prevReport = "";
    HelixConnection connection = null;

    while (true) {
      try {
        // Get application report for the appId we are interested in
        ApplicationReport report = yarnClient.getApplicationReport(_appId);

        String reportMessage = generateReport(report);
        if (!reportMessage.equals(prevReport)) {
          LOG.info(reportMessage);
        }
        YarnApplicationState state = report.getYarnApplicationState();
        if (YarnApplicationState.RUNNING == state) {
          if (connection == null) {
            String hostName = null;
            int ind = report.getHost().indexOf('/');
            if (ind > -1) {
              hostName = report.getHost().substring(ind + 1);
            } else {
              hostName = report.getHost();
            }
            connection = new ZkHelixConnection(hostName + ":2181");

            try {
              connection.connect();
            } catch (Exception e) {
              LOG.warn("AppMaster started but not yet initialized");
              connection = null;
            }
          }
          if (connection.isConnected()) {
            return connection;
          }
        }
        prevReport = reportMessage;
        Thread.sleep(10000);
View Full Code Here


  /**
   * @return true if successfully completed, it will print status every X seconds
   */
  public boolean waitUntilDone() {
    String prevReport = "";
    HelixConnection connection = null;

    while (true) {
      try {
        // Get application report for the appId we are interested in
        ApplicationReport report = yarnClient.getApplicationReport(_appId);

        String reportMessage = generateReport(report);
        if (!reportMessage.equals(prevReport)) {
          LOG.info(reportMessage);
        }
        YarnApplicationState state = report.getYarnApplicationState();
        FinalApplicationStatus dsStatus = report.getFinalApplicationStatus();
        if (YarnApplicationState.FINISHED == state) {
          if (FinalApplicationStatus.SUCCEEDED == dsStatus) {
            LOG.info("Application has completed successfully. Breaking monitoring loop");
            return true;
          } else {
            LOG.info("Application did finished unsuccessfully." + " YarnState=" + state.toString()
                + ", DSFinalStatus=" + dsStatus.toString() + ". Breaking monitoring loop");
            return false;
          }
        } else if (YarnApplicationState.KILLED == state || YarnApplicationState.FAILED == state) {
          LOG.info("Application did not finish." + " YarnState=" + state.toString()
              + ", DSFinalStatus=" + dsStatus.toString() + ". Breaking monitoring loop");
          return false;
        }
        if (YarnApplicationState.RUNNING == state) {
          if (connection == null) {
            String hostName = null;
            int ind = report.getHost().indexOf('/');
            if (ind > -1) {
              hostName = report.getHost().substring(ind + 1);
            } else {
              hostName = report.getHost();
            }
            connection = new ZkHelixConnection(hostName + ":2181");

            try {
              connection.connect();
            } catch (Exception e) {
              LOG.warn("AppMaster started but not yet initialized");
              connection = null;
            }
          }
          if (connection.isConnected()) {
            AppStatusReportGenerator generator = new AppStatusReportGenerator();
            ClusterId clusterId = ClusterId.from(_applicationSpec.getAppName());
            String generateReport = generator.generateReport(connection, clusterId);
            LOG.info(generateReport);
          }
View Full Code Here

    }
  }

  @Override
  public void connect() throws Exception {
    HelixConnection conn = (ZkHelixConnection) _role.getConnection();

    if (!conn.isConnected()) {
      conn.connect();
    }

    HelixService service = (HelixService) _role;
    service.start();
  }
View Full Code Here

  }

  @Override
  public void disconnect() {
    HelixService service = (HelixService) _role;
    HelixConnection conn = _role.getConnection();

    service.stop();

    if (conn.isConnected()) {
      conn.disconnect();
    }
  }
View Full Code Here

        Transition.from(State.from("RELEASED"), State.from("LOCKED")), 1);

    ClusterConfig cluster = clusterBuilder.build();

    // set up a connection to work with ZooKeeper-persisted data
    HelixConnection connection = new ZkHelixConnection(args[0]);
    connection.connect();

    // create the cluster
    createCluster(cluster, connection);

    // update the resource
    updateResource(resource, clusterId, connection);

    // update the participant
    updateParticipant(participant, clusterId, connection);

    // start the controller
    ControllerId controllerId = ControllerId.from("exampleController");
    HelixController helixController = connection.createController(clusterId, controllerId);
    helixController.startAsync();

    // start the specified participant
    HelixParticipant helixParticipant =
        connection.createParticipant(clusterId, participant.getId());
    helixParticipant.getStateMachineEngine().registerStateModelFactory(
        lockUnlock.getStateModelDefId(), new LockUnlockFactory());
    helixParticipant.startAsync();

    // start another participant via auto join
    HelixParticipant autoJoinParticipant =
        connection.createParticipant(clusterId, ParticipantId.from("localhost_12120"));
    autoJoinParticipant.getStateMachineEngine().registerStateModelFactory(
        lockUnlock.getStateModelDefId(), new LockUnlockFactory());
    autoJoinParticipant.startAsync();

    Thread.sleep(5000);
    printExternalView(connection, clusterId, resource.getId());

    // stop the participants
    helixParticipant.stopAsync();
    autoJoinParticipant.stopAsync();

    // stop the controller
    helixController.stopAsync();

    // drop the cluster
    dropCluster(clusterId, connection);
    connection.disconnect();
  }
View Full Code Here

    State offline = State.from("OFFLINE");
    State dropped = State.from("DROPPED");
    StateModelDefId stateModelDefId = StateModelDefId.from("MasterSlave");

    // create connection
    HelixConnection connection = new ZkHelixConnection(zkAddr);
    connection.connect();

    // setup cluster
    ClusterAccessor clusterAccessor = connection.createClusterAccessor(clusterId);
    clusterAccessor.dropCluster();

    StateModelDefinition stateModelDef =
        new StateModelDefinition.Builder(stateModelDefId).addState(master, 1).addState(slave, 2)
            .addState(offline, 3).addState(dropped).addTransition(offline, slave, 3)
            .addTransition(slave, offline, 4).addTransition(slave, master, 2)
            .addTransition(master, slave, 1).addTransition(offline, dropped).initialState(offline)
            .upperBound(master, 1).dynamicUpperBound(slave, "R").build();
    RebalancerContext rebalancerCtx =
        new SemiAutoRebalancerContext.Builder(resourceId).addPartitions(1).replicaCount(1)
            .stateModelDefId(stateModelDefId)
            .preferenceList(PartitionId.from("testDB_0"), Arrays.asList(participantId)).build();
    clusterAccessor.createCluster(new ClusterConfig.Builder(clusterId).addStateModelDefinition(
        stateModelDef).build());
    clusterAccessor.addResourceToCluster(new ResourceConfig.Builder(resourceId).rebalancerContext(
        rebalancerCtx).build());
    clusterAccessor.addParticipantToCluster(new ParticipantConfig.Builder(participantId).build());

    // start controller
    HelixController controller = connection.createController(clusterId, controllerId);
    controller.startAsync();

    // start participant
    HelixParticipant participant = connection.createParticipant(clusterId, participantId);
    participant.getStateMachineEngine().registerStateModelFactory(
        StateModelDefId.from("MasterSlave"), new MockStateModelFactory());

    participant.startAsync();

    // verify
    final HelixDataAccessor accessor = connection.createDataAccessor(clusterId);
    final PropertyKey.Builder keyBuilder = accessor.keyBuilder();
    boolean success = TestHelper.verify(new TestHelper.Verifier() {

      @Override
      public boolean verify() throws Exception {
        ExternalView externalView = accessor.getProperty(keyBuilder.externalView("testDB"));
        Map<ParticipantId, State> stateMap = externalView.getStateMap(PartitionId.from("testDB_0"));

        if (stateMap == null || !stateMap.containsKey(participantId)) {
          return false;
        }

        return stateMap.get(participantId).equals(State.from("MASTER"));
      }
    }, 10 * 1000);

    Assert.assertTrue(success);

    // clean up
    controller.stopAsync();
    participant.stopAsync();
    connection.disconnect();

    System.out.println("END " + clusterName + " at " + new Date(System.currentTimeMillis()));
  }
View Full Code Here

        n, // number of nodes
        2, // replicas
        "MasterSlave", true); // do rebalance

    // create connection
    HelixConnection connection = new ZkHelixConnection(ZK_ADDR);
    connection.connect();

    // start auto-controller
    ClusterId clusterId = ClusterId.from(clusterName);
    final HelixAutoController[] controllers = new HelixAutoController[n];
    for (int i = 0; i < n; i++) {
      int port = 12918 + i;
      ControllerId controllerId = ControllerId.from("localhost_" + port);
      controllers[i] = connection.createAutoController(clusterId, controllerId);
      controllers[i].startAsync();
    }

    // check live-instance znode for localhost_12918/12919 exists
    final HelixDataAccessor accessor =
        new ZKHelixDataAccessor(clusterName, new ZkBaseDataAccessor<ZNRecord>(_gZkClient));
    final PropertyKey.Builder keyBuilder = accessor.keyBuilder();

    for (int i = 0; i < n; i++) {
      String instanceName = controllers[i].getControllerId().stringify();
      Assert.assertNotNull(accessor.getProperty(keyBuilder.liveInstance(instanceName)));
    }

    // check leader znode exists
    LiveInstance leader = accessor.getProperty(keyBuilder.controllerLeader());
    Assert.assertNotNull(leader);
    Assert.assertEquals(leader.getInstanceName(), controllers[0].getControllerId().stringify());

    // stop controller localhost_12918
    controllers[0].stopAsync();

    // check live-instance znode for localhost_12918 is gone
    String instanceName = controllers[0].getControllerId().stringify();
    Assert.assertNull(accessor.getProperty(keyBuilder.liveInstance(instanceName)));

    // check localhost_12919 becomes the new leader
    boolean success = TestHelper.verify(new TestHelper.Verifier() {

      @Override
      public boolean verify() throws Exception {
        LiveInstance leader = accessor.getProperty(keyBuilder.controllerLeader());
        if (leader == null) {
          return false;
        }
        return leader.getInstanceName().equals(controllers[1].getControllerId().stringify());

      }
    }, 3 * 1000);
    Assert.assertTrue(success, "fail to re-elect new leader");

    // clean up
    connection.disconnect();

    // check live-instance znode for localhost_12919 is gone
    instanceName = controllers[1].getControllerId().stringify();
    Assert.assertNull(accessor.getProperty(keyBuilder.liveInstance(instanceName)));

View Full Code Here

        n, // number of nodes
        2, // replicas
        "MasterSlave", true); // do rebalance

    // create connection
    HelixConnection connection = new ZkHelixConnection(ZK_ADDR);
    connection.connect();

    // start participant
    ClusterId clusterId = ClusterId.from(clusterName);
    HelixParticipant[] participants = new HelixParticipant[n];
    for (int i = 0; i < n; i++) {
      int port = 12918 + i;
      ParticipantId participantId = ParticipantId.from("localhost_" + port);

      participants[i] = connection.createParticipant(clusterId, participantId);
      participants[i].getStateMachineEngine().registerStateModelFactory(
        StateModelDefId.from("MasterSlave"), new TestHelixConnection.MockStateModelFactory());

      participants[i].startAsync();
    }

    // check live-instance znode for localhost_12918/12919 exist
    HelixDataAccessor accessor =
        new ZKHelixDataAccessor(clusterName, new ZkBaseDataAccessor<ZNRecord>(_gZkClient));
    PropertyKey.Builder keyBuilder = accessor.keyBuilder();

    for (int i = 0; i < n; i++) {
      Assert.assertNotNull(accessor.getProperty(keyBuilder.liveInstance(participants[i].getParticipantId().stringify())));
    }

    // stop participant localhost_12918
    participants[0].stopAsync();

    // check live-instance znode for localhost_12918 is gone
    Assert.assertNull(accessor.getProperty(keyBuilder.liveInstance(participants[0]
        .getParticipantId().stringify())));

    // clean up
    connection.disconnect();

    // check live-instance znode for localhost_12919 is gone
    Assert.assertNull(accessor.getProperty(keyBuilder.liveInstance(participants[1]
        .getParticipantId().stringify())));
View Full Code Here

        n, // number of nodes
        2, // replicas
        "MasterSlave", true); // do rebalance

    // create connection
    HelixConnection connection = new ZkHelixConnection(ZK_ADDR);
    connection.connect();

    // start controller
    ClusterId clusterId = ClusterId.from(clusterName);
    ControllerId controllerId = ControllerId.from("controller");
    HelixController controller = connection.createController(clusterId, controllerId);
    controller.startAsync();

    // check leader znode exists
    HelixDataAccessor accessor = connection.createDataAccessor(clusterId);
    PropertyKey.Builder keyBuilder = accessor.keyBuilder();
    LiveInstance leader = accessor.getProperty(keyBuilder.controllerLeader());
    Assert.assertNotNull(leader);
    Assert.assertEquals(leader.getInstanceName(), controllerId.stringify());

    // stop participant
    controller.stopAsync();

    // check leader znode is gone
    Assert.assertNull(accessor.getProperty(keyBuilder.controllerLeader()));

    // clean up
    connection.disconnect();

    System.out.println("END " + clusterName + " at " + new Date(System.currentTimeMillis()));
  }
View Full Code Here

        n, // number of nodes
        2, // replicas
        "MasterSlave", true); // do rebalance

    // create connection
    HelixConnection connection = new ZkHelixConnection(ZK_ADDR);
    connection.connect();

    // start controller
    ClusterId clusterId = ClusterId.from(clusterName);
    final ControllerId controllerId = ControllerId.from("controller");

    // start controller
    HelixController controller = connection.createController(clusterId, controllerId);
    controller.startAsync();

    // check live-instance znode for localhost_12918 exists
    final HelixDataAccessor accessor =
        new ZKHelixDataAccessor(clusterName, new ZkBaseDataAccessor<ZNRecord>(_gZkClient));
    final PropertyKey.Builder keyBuilder = accessor.keyBuilder();
    LiveInstance leader = accessor.getProperty(keyBuilder.controllerLeader());
    Assert.assertNotNull(leader);
    Assert.assertEquals(leader.getInstanceName(), controllerId.stringify());

    // remove leader znode externally
    accessor.removeProperty(keyBuilder.controllerLeader());

    // verify leader is re-elected
    boolean result = TestHelper.verify(new TestHelper.Verifier() {

      @Override
      public boolean verify() throws Exception {
        LiveInstance leader = accessor.getProperty(keyBuilder.controllerLeader());
        if (leader == null) {
          return false;
        }

        return leader.getInstanceName().equals(controllerId.stringify());
      }
    }, 3 * 1000);

    Assert.assertTrue(result, "Fail to re-elect a new leader");

    // clean up
    connection.disconnect();

    // check leader znode is gone
    Assert.assertNull(accessor.getProperty(keyBuilder.controllerLeader()));

    System.out.println("END " + clusterName + " at " + new Date(System.currentTimeMillis()));
View Full Code Here

TOP

Related Classes of org.apache.helix.HelixConnection

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.