Package org.apache.helix.api

Examples of org.apache.helix.api.Participant


    Set<ParticipantId> participantSet = new HashSet<ParticipantId>(participantMap.keySet());
    Set<ParticipantId> disabledParticipantsForPartition =
        Sets.filter(participantSet, new Predicate<ParticipantId>() {
          @Override
          public boolean apply(ParticipantId participantId) {
            Participant participant = participantMap.get(participantId);
            return !participant.isEnabled()
                || participant.getDisabledPartitionIds().contains(partitionId);
          }
        });
    return disabledParticipantsForPartition;
  }
View Full Code Here


    // create the participant
    boolean created = createParticipant(participantId, accessor, MODIFIER, 1);
    Assert.assertTrue(created);

    // read the participant
    Participant participantSnapshot = accessor.readParticipant(participantId);
    Assert.assertEquals(participantSnapshot.getUserConfig().getIntField(MODIFIER, -1), 1);

    // create a participant with the same id
    boolean created2 = createParticipant(participantId, accessor, MODIFIER, 2);
    Assert.assertFalse(created2); // should fail since participant exists

    // remove a required property
    helixAccessor.removeProperty(helixAccessor.keyBuilder().messages(participantId.stringify()));

    // try again, should work this time
    created2 = createParticipant(participantId, accessor, MODIFIER, 2);
    Assert.assertTrue(created2);

    // read the cluster again
    participantSnapshot = accessor.readParticipant(participantId);
    Assert.assertEquals(participantSnapshot.getUserConfig().getIntField(MODIFIER, -1), 2);

    accessor.dropCluster();
  }
View Full Code Here

        // set up some participants
        for (String nodeName : _liveNodes) {
          ParticipantId participantId = ParticipantId.from(nodeName);
          ParticipantConfig participantConfig =
              new ParticipantConfig.Builder(participantId).hostName("hostname").port(0).build();
          Participant participant =
              new Participant(participantConfig, null, currentStateMap, messageMap, null);
          liveParticipantMap.put(participantId, participant);
        }
        List<ParticipantId> participantPreferenceList =
            Lists.transform(listResult.get(partition), new Function<String, ParticipantId>() {
              @Override
View Full Code Here

    Assert.assertEquals(id, _clusterId);
    Map<ParticipantId, Participant> liveParticipantMap = cluster.getLiveParticipantMap();
    Assert.assertEquals(liveParticipantMap.size(), n);

    for (ParticipantId participantId : liveParticipantMap.keySet()) {
      Participant participant = liveParticipantMap.get(participantId);
      Map<ResourceId, CurrentState> curStateMap = participant.getCurrentStateMap();
      Assert.assertEquals(curStateMap.size(), 1);

      ResourceId resourceId = ResourceId.from("TestDB0");
      Assert.assertTrue(curStateMap.containsKey(resourceId));
      CurrentState curState = curStateMap.get(resourceId);
View Full Code Here

      liveInstances[i] = new LiveInstance("localhost_" + i);
      liveInstances[i].setSessionId("session_" + i);
      liveInstances[i].setHelixVersion("1.2.3.4");
      liveInstances[i].setLiveInstance(i + "@localhost");
    }
    liveParticipants.put(participantId0, new Participant(new ParticipantConfig.Builder(
        participantId0).hostName("localhost").port(0).build(), liveInstances[0], currentStateMap,
        messageMap, null));
    liveParticipants.put(participantId1, new Participant(new ParticipantConfig.Builder(
        participantId1).hostName("localhost").port(1).build(), liveInstances[1], currentStateMap,
        messageMap, null));

    Map<ParticipantId, State> currentStates = new HashMap<ParticipantId, State>();
    currentStates.put(ParticipantId.from("localhost_0"), State.from("SLAVE"));
View Full Code Here

      liveInstances[i] = new LiveInstance("localhost_" + i);
      liveInstances[i].setSessionId("session_" + i);
      liveInstances[i].setHelixVersion("1.2.3.4");
      liveInstances[i].setLiveInstance(i + "@localhost");
    }
    liveParticipants.put(participantId0, new Participant(new ParticipantConfig.Builder(
        participantId0).hostName("localhost").port(0).build(), liveInstances[0], currentStateMap,
        messageMap, null));
    liveParticipants.put(participantId1, new Participant(new ParticipantConfig.Builder(
        participantId1).hostName("localhost").port(1).build(), liveInstances[1], currentStateMap,
        messageMap, null));

    Map<ParticipantId, State> currentStates = new HashMap<ParticipantId, State>();
    currentStates.put(ParticipantId.from("localhost_0"), State.from("SLAVE"));
View Full Code Here

  public void stopContainer(String appName, String participantName) throws Exception {
    ClusterId clusterId = ClusterId.from(appName);
    ClusterAccessor clusterAccessor = _connection.createClusterAccessor(clusterId);
    HelixDataAccessor dataAccessor = _connection.createDataAccessor(clusterId);
    ParticipantId participantId = ParticipantId.from(participantName);
    Participant participant = clusterAccessor.readParticipant(participantId);
    if (participant != null && participant.isAlive()) {
      Message message = new Message(MessageType.SHUTDOWN, UUID.randomUUID().toString());
      message.setTgtName(participant.getId().toString());
      message.setTgtSessionId(participant.getLiveInstance().getSessionId());
      message.setMsgId(message.getId());
      dataAccessor.createProperty(
          dataAccessor.keyBuilder().message(participant.getId().toString(), message.getId()),
          message);
      do {
        participant = clusterAccessor.readParticipant(participantId);
        Thread.sleep(1000);
        LOG.info("Waiting for container:" + participantName + " to shutdown");
      } while (participant != null && participant.isAlive());
    }

  }
View Full Code Here

      Message message = iter.next();
      ResourceId resourceId = message.getResourceId();
      ResourceConfig resource = resourceMap.get(resourceId);

      ParticipantId participantId = ParticipantId.from(message.getTgtName());
      Participant liveParticipant = liveParticipantMap.get(participantId);
      String participantVersion = null;
      if (liveParticipant != null) {
        participantVersion = liveParticipant.getLiveInstance().getHelixVersion();
      }

      if (resource == null || resource.getIdealState() == null
          || !resource.getIdealState().getBatchMessageMode() || participantVersion == null
          || !properties.isFeatureSupported("batch_message", participantVersion)) {
View Full Code Here

        // If a process died, we need to mark it as DISCONNECTED or if the process is ready, mark as
        // CONNECTED
        Map<ParticipantId, Participant> participantMap = cluster.getParticipantMap();
        for (ParticipantId participantId : participantMap.keySet()) {
          Participant participant = participantMap.get(participantId);
          ContainerConfig config = participant.getContainerConfig();
          if (config != null) {
            ContainerState containerState = config.getState();
            if (!participant.isAlive() && ContainerState.CONNECTED.equals(containerState)) {
              // Need to mark as disconnected if process died
              LOG.info("Participant " + participantId + " died, marking as DISCONNECTED");
              updateContainerState(cache, accessor, keyBuilder, cluster, null, participantId,
                  ContainerState.DISCONNECTED);
            } else if (participant.isAlive() && ContainerState.CONNECTING.equals(containerState)) {
              // Need to mark as connected only when the live instance is visible
              LOG.info("Participant " + participantId + " is ready, marking as CONNECTED");
              updateContainerState(cache, accessor, keyBuilder, cluster, null, participantId,
                  ContainerState.CONNECTED);
            } else if (!participant.isAlive() && ContainerState.HALTING.equals(containerState)) {
              // Need to mark as connected only when the live instance is visible
              LOG.info("Participant " + participantId + " is has been killed, marking as HALTED");
              updateContainerState(cache, accessor, keyBuilder, cluster, null, participantId,
                  ContainerState.HALTED);
            }
          }
        }

        // Participants registered in helix
        // Give those participants to targetprovider
        // Provide the response that contains, new containerspecs, containers to be released,
        // containers to be stopped
        // call the respective provisioner to allocate and start the container.
        // Each container is then started its state is changed from any place.
        // The target provider is given the state of container and asked for its new state. For each
        // state there is a corresponding handler function.

        // TargetProvider should be stateless, given the state of cluster and existing participants
        // it should return the same result
        final TargetProviderResponse response =
            targetProvider.evaluateExistingContainers(cluster, resourceId, participants);

        // allocate new containers
        for (final ContainerSpec spec : response.getContainersToAcquire()) {
          final ParticipantId participantId = spec.getParticipantId();
          if (!cluster.getParticipantMap().containsKey(participantId)) {
            // create a new Participant, attach the container spec
            InstanceConfig instanceConfig = new InstanceConfig(participantId);
            instanceConfig.setInstanceEnabled(false);
            instanceConfig.setContainerSpec(spec);
            // create a helix_participant in ACQUIRING state
            instanceConfig.setContainerState(ContainerState.ACQUIRING);
            // create the helix participant and add it to cluster
            helixAdmin.addInstance(cluster.getId().toString(), instanceConfig);
            cache.requireFullRefresh();
          }
          LOG.info("Allocating container for " + participantId);
          ListenableFuture<ContainerId> future = containerProvider.allocateContainer(spec);
          FutureCallback<ContainerId> callback = new FutureCallback<ContainerId>() {
            @Override
            public void onSuccess(ContainerId containerId) {
              LOG.info("Container " + containerId + " acquired. Marking " + participantId);
              updateContainerState(cache, accessor, keyBuilder, cluster, containerId,
                  participantId, ContainerState.ACQUIRED);
            }

            @Override
            public void onFailure(Throwable t) {
              LOG.error("Could not allocate a container for participant " + participantId, t);
              updateContainerState(cache, accessor, keyBuilder, cluster, null, participantId,
                  ContainerState.FAILED);
            }
          };
          safeAddCallback(future, callback);
        }

        // start new containers
        for (final Participant participant : response.getContainersToStart()) {
          final ContainerId containerId = participant.getInstanceConfig().getContainerId();
          updateContainerState(cache, accessor, keyBuilder, cluster, null, participant.getId(),
              ContainerState.CONNECTING);
          // create the helix participant and add it to cluster
          LOG.info("Starting container " + containerId + " for " + participant.getId());
          ListenableFuture<Boolean> future =
              containerProvider.startContainer(containerId, participant);
          FutureCallback<Boolean> callback = new FutureCallback<Boolean>() {
            @Override
            public void onSuccess(Boolean result) {
              // Do nothing yet, need to wait for live instance
              LOG.info("Container " + containerId + " started for " + participant.getId());
            }

            @Override
            public void onFailure(Throwable t) {
              LOG.error("Could not start container" + containerId + "for participant "
                  + participant.getId(), t);
              updateContainerState(cache, accessor, keyBuilder, cluster, null, participant.getId(),
                  ContainerState.FAILED);
            }
          };
          safeAddCallback(future, callback);
        }

        // release containers
        for (final Participant participant : response.getContainersToRelease()) {
          // mark it as finalizing
          final ContainerId containerId = participant.getInstanceConfig().getContainerId();
          updateContainerState(cache, accessor, keyBuilder, cluster, null, participant.getId(),
              ContainerState.FINALIZING);
          // remove the participant
          LOG.info("Deallocating container " + containerId + " for " + participant.getId());
          ListenableFuture<Boolean> future = containerProvider.deallocateContainer(containerId);
          FutureCallback<Boolean> callback = new FutureCallback<Boolean>() {
            @Override
            public void onSuccess(Boolean result) {
              LOG.info("Container " + containerId + " deallocated. Dropping " + participant.getId());
              InstanceConfig existingInstance =
                  helixAdmin.getInstanceConfig(cluster.getId().toString(), participant.getId()
                      .toString());
              helixAdmin.dropInstance(cluster.getId().toString(), existingInstance);
              cache.requireFullRefresh();
            }

            @Override
            public void onFailure(Throwable t) {
              LOG.error("Could not deallocate container" + containerId + "for participant "
                  + participant.getId(), t);
              updateContainerState(cache, accessor, keyBuilder, cluster, null, participant.getId(),
                  ContainerState.FAILED);
            }
          };
          safeAddCallback(future, callback);
        }

        // stop but don't remove
        for (final Participant participant : response.getContainersToStop()) {
          // switch to halting
          final ContainerId containerId = participant.getInstanceConfig().getContainerId();
          updateContainerState(cache, accessor, keyBuilder, cluster, null, participant.getId(),
              ContainerState.HALTING);
          // stop the container
          LOG.info("Stopping container " + containerId + " for " + participant.getId());
          ListenableFuture<Boolean> future = containerProvider.stopContainer(containerId);
          FutureCallback<Boolean> callback = new FutureCallback<Boolean>() {
            @Override
            public void onSuccess(Boolean result) {
              // Don't update the state here, wait for the live instance, but do send a shutdown
              // message
              LOG.info("Container " + containerId + " stopped for " + participant.getId());
              if (participant.isAlive()) {
                Message message = new Message(MessageType.SHUTDOWN, UUID.randomUUID().toString());
                message.setTgtName(participant.getId().toString());
                message.setTgtSessionId(participant.getLiveInstance().getSessionId());
                message.setMsgId(message.getId());
                accessor.createProperty(
                    keyBuilder.message(participant.getId().toString(), message.getId()), message);
              }
            }

            @Override
            public void onFailure(Throwable t) {
              LOG.error(
                  "Could not stop container" + containerId + "for participant "
                      + participant.getId(), t);
              updateContainerState(cache, accessor, keyBuilder, cluster, null, participant.getId(),
                  ContainerState.FAILED);
            }
          };
          safeAddCallback(future, callback);
        }
View Full Code Here

   * @return ParticipantConfig, or null if participant is not persisted
   */
  public ParticipantConfig updateParticipant(ParticipantId participantId,
      ParticipantConfig.Delta participantDelta) {
    participantDelta.merge(_accessor);
    Participant participant = readParticipant(participantId);
    return (participant != null) ? participant.getConfig() : null;
  }
View Full Code Here

TOP

Related Classes of org.apache.helix.api.Participant

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.