Search in sources :

Example 11 with TaskLabelReader

use of com.mesosphere.sdk.offer.taskdata.TaskLabelReader in project dcos-commons by mesosphere.

the class DefaultConfigurationUpdater method cleanupDuplicateAndUnusedConfigs.

/**
 * Searches for any task configurations which are already identical to the target configuration
 * and updates the embedded config version label in those tasks to point to the current target
 * configuration.
 */
private void cleanupDuplicateAndUnusedConfigs(ServiceSpec targetConfig, UUID targetConfigId) throws ConfigStoreException {
    List<Protos.TaskInfo> taskInfosToUpdate = new ArrayList<>();
    Set<UUID> neededConfigs = new HashSet<>();
    neededConfigs.add(targetConfigId);
    // Search task labels for configs which need to be cleaned up.
    for (Protos.TaskInfo taskInfo : stateStore.fetchTasks()) {
        final UUID taskConfigId;
        try {
            taskConfigId = new TaskLabelReader(taskInfo).getTargetConfiguration();
        } catch (TaskException e) {
            LOGGER.warn(String.format("Unable to extract configuration ID from task %s: %s", taskInfo.getName(), TextFormat.shortDebugString(taskInfo)), e);
            continue;
        }
        if (taskConfigId.equals(targetConfigId)) {
            LOGGER.info("Task {} configuration ID matches target: {}", taskInfo.getName(), taskConfigId);
        } else {
            try {
                final ServiceSpec taskConfig = configStore.fetch(taskConfigId);
                if (!needsConfigUpdate(taskInfo, targetConfig, taskConfig)) {
                    // Task is effectively already on the target config. Update task's config ID to match target,
                    // and allow the duplicate config to be dropped from configStore.
                    TaskInfo.Builder taskBuilder = taskInfo.toBuilder();
                    taskBuilder.setLabels(new TaskLabelWriter(taskInfo).setTargetConfiguration(targetConfigId).toProto());
                    taskInfosToUpdate.add(taskBuilder.build());
                } else {
                    // Config isn't the same as the target. Refrain from updating task, mark config as 'needed'.
                    neededConfigs.add(taskConfigId);
                }
            } catch (Exception e) {
                LOGGER.error(String.format("Failed to fetch configuration %s for task %s", taskConfigId, taskInfo.getName()), e);
                // Cannot read this task's config. Do not delete the config.
                neededConfigs.add(taskConfigId);
            }
        }
    }
    if (!taskInfosToUpdate.isEmpty()) {
        LOGGER.info("Updating {} tasks in StateStore with target configuration ID {}", taskInfosToUpdate.size(), targetConfigId);
        stateStore.storeTasks(taskInfosToUpdate);
    }
    Collection<UUID> configIds = configStore.list();
    LOGGER.info("Testing deserialization of {} listed configurations before cleanup:", configIds.size());
    for (UUID configId : configIds) {
        try {
            configStore.fetch(configId);
            LOGGER.info("- {}: OK", configId);
        } catch (Exception e) {
            LOGGER.info("- {}: FAILED, leaving as-is: {}", configId, e.getMessage());
            neededConfigs.add(configId);
        }
    }
    clearConfigsNotListed(neededConfigs);
}
Also used : TaskLabelReader(com.mesosphere.sdk.offer.taskdata.TaskLabelReader) DefaultServiceSpec(com.mesosphere.sdk.specification.DefaultServiceSpec) ServiceSpec(com.mesosphere.sdk.specification.ServiceSpec) TaskInfo(org.apache.mesos.Protos.TaskInfo) TaskException(com.mesosphere.sdk.offer.TaskException) ConfigStoreException(com.mesosphere.sdk.state.ConfigStoreException) TaskInfo(org.apache.mesos.Protos.TaskInfo) TaskException(com.mesosphere.sdk.offer.TaskException) Protos(org.apache.mesos.Protos) TaskLabelWriter(com.mesosphere.sdk.offer.taskdata.TaskLabelWriter)

Example 12 with TaskLabelReader

use of com.mesosphere.sdk.offer.taskdata.TaskLabelReader in project dcos-commons by mesosphere.

the class DefaultConfigurationUpdater method needsConfigUpdate.

private static boolean needsConfigUpdate(Protos.TaskInfo taskInfo, ServiceSpec targetConfig, ServiceSpec taskConfig) {
    if (targetConfig.equals(taskConfig)) {
        LOGGER.info("Task '{}' is up to date: Task's target ServiceSpec matches the current ServiceSpec", taskInfo.getName());
        return false;
    }
    final String podType;
    final boolean isPermanentlyFailed;
    try {
        TaskLabelReader reader = new TaskLabelReader(taskInfo);
        podType = reader.getType();
        isPermanentlyFailed = reader.isPermanentlyFailed();
    } catch (TaskException e) {
        LOGGER.error(String.format("Unable to extract pod type from task '%s'. Will assume the task needs a configuration update", taskInfo.getName()), e);
        return true;
    }
    // to transition from their former config to the new target.
    if (isPermanentlyFailed) {
        return false;
    }
    Optional<PodSpec> targetSpecOptional = getPodSpec(targetConfig, podType);
    Optional<PodSpec> taskSpecOptional = getPodSpec(taskConfig, podType);
    if (!targetSpecOptional.isPresent() || !taskSpecOptional.isPresent()) {
        LOGGER.info("Task '{}' needs a configuration update: " + "PodSpec '{}' was {} in task's config, but is {} in current target config", taskInfo.getName(), podType, taskSpecOptional.isPresent() ? "present" : "missing", targetSpecOptional.isPresent() ? "present" : "missing");
        return true;
    }
    boolean updateNeeded = !areMatching(targetSpecOptional.get(), taskSpecOptional.get());
    if (updateNeeded) {
        LOGGER.info("Task '{}' needs a configuration update: PodSpec '{}' has changed", taskInfo.getName(), podType);
    } else {
        LOGGER.info("Task '{}' is up to date: PodSpec '{}' is the same", taskInfo.getName(), podType);
    }
    return updateNeeded;
}
Also used : TaskLabelReader(com.mesosphere.sdk.offer.taskdata.TaskLabelReader) TaskException(com.mesosphere.sdk.offer.TaskException) DefaultPodSpec(com.mesosphere.sdk.specification.DefaultPodSpec) PodSpec(com.mesosphere.sdk.specification.PodSpec)

Example 13 with TaskLabelReader

use of com.mesosphere.sdk.offer.taskdata.TaskLabelReader in project dcos-commons by mesosphere.

the class RoundRobinByAttributeRuleTest method getPodInstance.

private static PodInstance getPodInstance(TaskInfo taskInfo) {
    try {
        TaskLabelReader labels = new TaskLabelReader(taskInfo);
        ResourceSet resourceSet = PodInstanceRequirementTestUtils.getCpuResourceSet(1.0);
        PodSpec podSpec = PodInstanceRequirementTestUtils.getRequirement(resourceSet, labels.getType(), labels.getIndex()).getPodInstance().getPod();
        return new DefaultPodInstance(podSpec, labels.getIndex());
    } catch (Exception e) {
        throw new IllegalStateException(e);
    }
}
Also used : TaskLabelReader(com.mesosphere.sdk.offer.taskdata.TaskLabelReader) PodSpec(com.mesosphere.sdk.specification.PodSpec) DefaultPodSpec(com.mesosphere.sdk.specification.DefaultPodSpec) ResourceSet(com.mesosphere.sdk.specification.ResourceSet) DefaultPodInstance(com.mesosphere.sdk.scheduler.plan.DefaultPodInstance) InvalidRequirementException(com.mesosphere.sdk.offer.InvalidRequirementException) TaskException(com.mesosphere.sdk.offer.TaskException)

Aggregations

TaskLabelReader (com.mesosphere.sdk.offer.taskdata.TaskLabelReader)13 TaskException (com.mesosphere.sdk.offer.TaskException)8 Protos (org.apache.mesos.Protos)5 PodSpec (com.mesosphere.sdk.specification.PodSpec)4 InvalidRequirementException (com.mesosphere.sdk.offer.InvalidRequirementException)3 DefaultPodInstance (com.mesosphere.sdk.scheduler.plan.DefaultPodInstance)3 ResourceSet (com.mesosphere.sdk.specification.ResourceSet)3 VisibleForTesting (com.google.common.annotations.VisibleForTesting)2 DefaultPodSpec (com.mesosphere.sdk.specification.DefaultPodSpec)2 GoalState (com.mesosphere.sdk.specification.GoalState)2 ConfigStoreException (com.mesosphere.sdk.state.ConfigStoreException)2 TaskInfo (org.apache.mesos.Protos.TaskInfo)2 TaskLabelWriter (com.mesosphere.sdk.offer.taskdata.TaskLabelWriter)1 DefaultServiceSpec (com.mesosphere.sdk.specification.DefaultServiceSpec)1 ServiceSpec (com.mesosphere.sdk.specification.ServiceSpec)1 GoalStateOverride (com.mesosphere.sdk.state.GoalStateOverride)1 IOException (java.io.IOException)1 TreeSet (java.util.TreeSet)1 DiscoveryInfo (org.apache.mesos.Protos.DiscoveryInfo)1 Operation (org.apache.mesos.Protos.Offer.Operation)1