Esempio n. 1
0
  /**
   * Updates the request state and runs the container on the specified host. Assumes a container is
   * available on the preferred host, so the caller must verify that before invoking this method.
   *
   * @param request the {@link SamzaContainerRequest} which is being handled.
   * @param preferredHost the preferred host on which the container should be run or {@link
   *     ContainerRequestState#ANY_HOST} if there is no host preference.
   */
  protected void runContainer(SamzaContainerRequest request, String preferredHost) {
    // Get the available container
    Container container = peekAllocatedContainer(preferredHost);
    if (container == null)
      throw new SamzaException("Expected container was unavailable on host " + preferredHost);

    // Update state
    containerRequestState.updateStateAfterAssignment(request, preferredHost, container);
    int expectedContainerId = request.expectedContainerId;

    // Cancel request and run container
    log.info(
        "Found available containers on {}. Assigning request for container_id {} with "
            + "timestamp {} to container {}",
        new Object[] {
          preferredHost,
          String.valueOf(expectedContainerId),
          request.getRequestTimestamp(),
          container.getId()
        });
    try {
      if (preferredHost.equals(ANY_HOST)) {
        containerUtil.runContainer(expectedContainerId, container);
      } else {
        containerUtil.runMatchedContainer(expectedContainerId, container);
      }
    } catch (SamzaContainerLaunchException e) {
      log.warn(
          String.format(
              "Got exception while starting container %s. Requesting a new container on any host",
              container),
          e);
      containerRequestState.releaseUnstartableContainer(container);
      requestContainer(expectedContainerId, ContainerAllocator.ANY_HOST);
    }
  }