Search in sources :

Example 1 with WeaveContainerController

use of com.continuuity.weave.internal.WeaveContainerController in project weave by continuuity.

the class RunningContainers method stopAll.

/**
   * Stops all running services. Only called when the AppMaster stops.
   */
void stopAll() {
    containerLock.lock();
    try {
        // Stop it one by one in reverse order of start sequence
        Iterator<String> itor = startSequence.descendingIterator();
        List<ListenableFuture<ServiceController.State>> futures = Lists.newLinkedList();
        while (itor.hasNext()) {
            String runnableName = itor.next();
            LOG.info("Stopping all instances of " + runnableName);
            futures.clear();
            // Parallel stops all running containers of the current runnable.
            for (WeaveContainerController controller : containers.row(runnableName).values()) {
                futures.add(controller.stop());
            }
            // Wait for containers to stop. Assumes the future returned by Futures.successfulAsList won't throw exception.
            Futures.getUnchecked(Futures.successfulAsList(futures));
            LOG.info("Terminated all instances of " + runnableName);
        }
        containers.clear();
        runnableInstances.clear();
    } finally {
        containerLock.unlock();
    }
}
Also used : WeaveContainerController(com.continuuity.weave.internal.WeaveContainerController) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) ServiceController(com.continuuity.weave.api.ServiceController)

Example 2 with WeaveContainerController

use of com.continuuity.weave.internal.WeaveContainerController in project weave by continuuity.

the class RunningContainers method getRunId.

private RunId getRunId(String runnableName, int instanceId) {
    RunId baseId;
    Collection<WeaveContainerController> controllers = containers.row(runnableName).values();
    if (controllers.isEmpty()) {
        baseId = RunIds.generate();
    } else {
        String id = controllers.iterator().next().getRunId().getId();
        baseId = RunIds.fromString(id.substring(0, id.lastIndexOf('-')));
    }
    return RunIds.fromString(baseId.getId() + '-' + instanceId);
}
Also used : WeaveContainerController(com.continuuity.weave.internal.WeaveContainerController) RunId(com.continuuity.weave.api.RunId)

Example 3 with WeaveContainerController

use of com.continuuity.weave.internal.WeaveContainerController in project weave by continuuity.

the class RunningContainers method handleCompleted.

/**
   * Handle completion of container.
   * @param status The completion status.
   * @param restartRunnables Set of runnable names that requires restart.
   */
void handleCompleted(YarnContainerStatus status, Multiset<String> restartRunnables) {
    containerLock.lock();
    String containerId = status.getContainerId();
    int exitStatus = status.getExitStatus();
    ContainerState state = status.getState();
    try {
        Map<String, WeaveContainerController> lookup = containers.column(containerId);
        if (lookup.isEmpty()) {
            // It's OK because if a container is stopped through removeLast, this would be empty.
            return;
        }
        if (lookup.size() != 1) {
            LOG.warn("More than one controller found for container {}", containerId);
        }
        if (exitStatus != 0) {
            LOG.warn("Container {} exited abnormally with state {}, exit code {}. Re-request the container.", containerId, state, exitStatus);
            restartRunnables.add(lookup.keySet().iterator().next());
        } else {
            LOG.info("Container {} exited normally with state {}", containerId, state);
        }
        for (Map.Entry<String, WeaveContainerController> completedEntry : lookup.entrySet()) {
            String runnableName = completedEntry.getKey();
            WeaveContainerController controller = completedEntry.getValue();
            controller.completed(exitStatus);
            removeInstanceId(runnableName, getInstanceId(controller.getRunId()));
            resourceReport.removeRunnableResources(runnableName, containerId);
        }
        lookup.clear();
        containerChange.signalAll();
    } finally {
        containerLock.unlock();
    }
}
Also used : WeaveContainerController(com.continuuity.weave.internal.WeaveContainerController) ContainerState(org.apache.hadoop.yarn.api.records.ContainerState) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 4 with WeaveContainerController

use of com.continuuity.weave.internal.WeaveContainerController in project weave by continuuity.

the class RunningContainers method removeLast.

/**
   * Stops and removes the last running container of the given runnable.
   */
void removeLast(String runnableName) {
    containerLock.lock();
    try {
        int maxInstanceId = getMaxInstanceId(runnableName);
        if (maxInstanceId < 0) {
            LOG.warn("No running container found for {}", runnableName);
            return;
        }
        String lastContainerId = null;
        WeaveContainerController lastController = null;
        // Find the controller with the maxInstanceId
        for (Map.Entry<String, WeaveContainerController> entry : containers.row(runnableName).entrySet()) {
            if (getInstanceId(entry.getValue().getRunId()) == maxInstanceId) {
                lastContainerId = entry.getKey();
                lastController = entry.getValue();
                break;
            }
        }
        Preconditions.checkState(lastContainerId != null, "No container found for {} with instanceId = {}", runnableName, maxInstanceId);
        LOG.info("Stopping service: {} {}", runnableName, lastController.getRunId());
        lastController.stopAndWait();
        containers.remove(runnableName, lastContainerId);
        removeInstanceId(runnableName, maxInstanceId);
        resourceReport.removeRunnableResources(runnableName, lastContainerId);
        containerChange.signalAll();
    } finally {
        containerLock.unlock();
    }
}
Also used : WeaveContainerController(com.continuuity.weave.internal.WeaveContainerController) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 5 with WeaveContainerController

use of com.continuuity.weave.internal.WeaveContainerController in project weave by continuuity.

the class RunningContainers method sendToRunnable.

void sendToRunnable(String runnableName, Message message, Runnable completion) {
    containerLock.lock();
    try {
        Collection<WeaveContainerController> controllers = containers.row(runnableName).values();
        if (controllers.isEmpty()) {
            completion.run();
        }
        AtomicInteger count = new AtomicInteger(controllers.size());
        for (WeaveContainerController controller : controllers) {
            sendMessage(runnableName, message, controller, count, completion);
        }
    } finally {
        containerLock.unlock();
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) WeaveContainerController(com.continuuity.weave.internal.WeaveContainerController)

Aggregations

WeaveContainerController (com.continuuity.weave.internal.WeaveContainerController)6 RunId (com.continuuity.weave.api.RunId)2 ImmutableMap (com.google.common.collect.ImmutableMap)2 Map (java.util.Map)2 ServiceController (com.continuuity.weave.api.ServiceController)1 WeaveRunResources (com.continuuity.weave.api.WeaveRunResources)1 DefaultWeaveRunResources (com.continuuity.weave.internal.DefaultWeaveRunResources)1 ListenableFuture (com.google.common.util.concurrent.ListenableFuture)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 ContainerState (org.apache.hadoop.yarn.api.records.ContainerState)1