Search in sources :

Example 16 with TaskLabelWriter

use of com.mesosphere.sdk.offer.taskdata.TaskLabelWriter 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 17 with TaskLabelWriter

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

the class PodInfoBuilder method setReadinessCheck.

private void setReadinessCheck(Protos.TaskInfo.Builder taskInfoBuilder, String serviceName, PodInstance podInstance, TaskSpec taskSpec, GoalStateOverride override, SchedulerConfig schedulerConfig) {
    Optional<ReadinessCheckSpec> readinessCheckSpecOptional = getReadinessCheck(taskSpec, override);
    if (!readinessCheckSpecOptional.isPresent()) {
        LOGGER.debug("No readiness check defined for taskSpec: {}", taskSpec.getName());
        return;
    }
    ReadinessCheckSpec readinessCheckSpec = readinessCheckSpecOptional.get();
    if (useDefaultExecutor) {
        // Default executors supports the newer TaskInfo.check field:
        Protos.CheckInfo.Builder builder = taskInfoBuilder.getCheckBuilder().setType(Protos.CheckInfo.Type.COMMAND).setDelaySeconds(readinessCheckSpec.getDelay()).setIntervalSeconds(readinessCheckSpec.getInterval()).setTimeoutSeconds(readinessCheckSpec.getTimeout());
        builder.getCommandBuilder().getCommandBuilder().setValue(readinessCheckSpec.getCommand()).setEnvironment(EnvUtils.toProto(getTaskEnvironment(serviceName, podInstance, taskSpec, schedulerConfig)));
    } else {
        // Custom executor implies older Mesos where TaskInfo.check doesn't exist yet. Fall back to label hack:
        Protos.HealthCheck.Builder builder = Protos.HealthCheck.newBuilder().setDelaySeconds(readinessCheckSpec.getDelay()).setIntervalSeconds(readinessCheckSpec.getInterval()).setTimeoutSeconds(readinessCheckSpec.getTimeout());
        builder.getCommandBuilder().setValue(readinessCheckSpec.getCommand()).setEnvironment(EnvUtils.toProto(getTaskEnvironment(serviceName, podInstance, taskSpec, schedulerConfig)));
        taskInfoBuilder.setLabels(new TaskLabelWriter(taskInfoBuilder).setReadinessCheck(builder.build()).toProto());
    }
}
Also used : TaskLabelWriter(com.mesosphere.sdk.offer.taskdata.TaskLabelWriter)

Example 18 with TaskLabelWriter

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

the class LaunchEvaluationStage method evaluate.

@Override
public EvaluationOutcome evaluate(MesosResourcePool mesosResourcePool, PodInfoBuilder podInfoBuilder) {
    Protos.ExecutorInfo.Builder executorBuilder = podInfoBuilder.getExecutorBuilder().get();
    Protos.Offer offer = mesosResourcePool.getOffer();
    Protos.TaskInfo.Builder taskBuilder = podInfoBuilder.getTaskBuilder(taskName);
    taskBuilder.setTaskId(CommonIdUtils.toTaskId(serviceName, taskBuilder.getName()));
    // Store metadata in the TaskInfo for later access by placement constraints:
    TaskLabelWriter writer = new TaskLabelWriter(taskBuilder);
    writer.setOfferAttributes(offer).setType(podInfoBuilder.getType()).setIndex(podInfoBuilder.getIndex()).setHostname(offer);
    if (offer.hasDomain() && offer.getDomain().hasFaultDomain()) {
        writer.setRegion(offer.getDomain().getFaultDomain().getRegion());
        writer.setZone(offer.getDomain().getFaultDomain().getZone());
    }
    taskBuilder.setLabels(writer.toProto());
    updateFaultDomainEnv(taskBuilder, offer);
    if (!useDefaultExecutor) {
        taskBuilder.setExecutor(executorBuilder);
    }
    return pass(this, Arrays.asList(new LaunchOfferRecommendation(offer, taskBuilder.build(), executorBuilder.build(), shouldLaunch, useDefaultExecutor)), "Added launch information to offer requirement").build();
}
Also used : LaunchOfferRecommendation(com.mesosphere.sdk.offer.LaunchOfferRecommendation) Protos(org.apache.mesos.Protos) TaskLabelWriter(com.mesosphere.sdk.offer.taskdata.TaskLabelWriter)

Example 19 with TaskLabelWriter

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

the class MaxPerAttributeRuleTest method getTask.

private static TaskInfo getTask(String id, Offer offer) {
    TaskInfo.Builder taskBuilder = TaskTestUtils.getTaskInfo(Collections.emptyList()).toBuilder();
    taskBuilder.getTaskIdBuilder().setValue(id);
    try {
        taskBuilder.setName(CommonIdUtils.toTaskName(taskBuilder.getTaskId()));
    } catch (Exception e) {
        throw new IllegalStateException(e);
    }
    taskBuilder.setLabels(new TaskLabelWriter(taskBuilder).setOfferAttributes(offer).toProto());
    return taskBuilder.build();
}
Also used : TaskInfo(org.apache.mesos.Protos.TaskInfo) TaskLabelWriter(com.mesosphere.sdk.offer.taskdata.TaskLabelWriter) IOException(java.io.IOException)

Example 20 with TaskLabelWriter

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

the class PersistentLaunchRecorderTest method testUpdateResourcesOneSharedTaskInStateStore.

@Test
public void testUpdateResourcesOneSharedTaskInStateStore() throws TaskException {
    Protos.Resource targetResource = Protos.Resource.newBuilder().setName("cpus").setType(Protos.Value.Type.SCALAR).setScalar(Protos.Value.Scalar.newBuilder().setValue(1.0)).build();
    Protos.Resource previousResource = Protos.Resource.newBuilder().setName("cpus").setType(Protos.Value.Type.SCALAR).setScalar(Protos.Value.Scalar.newBuilder().setValue(2.0)).build();
    String initTaskName = "pod-0-init";
    Protos.TaskInfo initTaskInfo = baseTaskInfo.toBuilder().setLabels(new TaskLabelWriter(baseTaskInfo).setType("pod").setIndex(0).toProto()).setName(initTaskName).addAllResources(Arrays.asList(previousResource)).build();
    String serverTaskName = "pod-0-server";
    Protos.TaskInfo serverTaskInfo = baseTaskInfo.toBuilder().setLabels(new TaskLabelWriter(baseTaskInfo).setType("pod").setIndex(0).toProto()).setName(serverTaskName).addAllResources(Arrays.asList(targetResource)).build();
    stateStore.storeTasks(Arrays.asList(initTaskInfo, serverTaskInfo));
    Assert.assertEquals(2, stateStore.fetchTaskNames().size());
    serverTaskInfo = stateStore.fetchTask(serverTaskName).get();
    Assert.assertFalse(stateStore.fetchTask(initTaskName).get().getResources(0).equals(stateStore.fetchTask(serverTaskName).get().getResources(0)));
    persistentLaunchRecorder.updateTaskResourcesWithinResourceSet(persistentLaunchRecorder.getPodInstance(serverTaskInfo).get(), serverTaskInfo);
    Assert.assertEquals(2, stateStore.fetchTaskNames().size());
    Assert.assertEquals(targetResource, stateStore.fetchTask(initTaskName).get().getResources(0));
    Assert.assertEquals(targetResource, stateStore.fetchTask(serverTaskName).get().getResources(0));
}
Also used : Protos(org.apache.mesos.Protos) TaskLabelWriter(com.mesosphere.sdk.offer.taskdata.TaskLabelWriter) Test(org.junit.Test)

Aggregations

TaskLabelWriter (com.mesosphere.sdk.offer.taskdata.TaskLabelWriter)21 Protos (org.apache.mesos.Protos)11 Test (org.junit.Test)9 UUID (java.util.UUID)7 StateStore (com.mesosphere.sdk.state.StateStore)6 MemPersister (com.mesosphere.sdk.storage.MemPersister)6 Persister (com.mesosphere.sdk.storage.Persister)6 TaskInfo (org.apache.mesos.Protos.TaskInfo)6 ImmutableList (com.google.common.collect.ImmutableList)5 Capabilities (com.mesosphere.sdk.dcos.Capabilities)5 CommonIdUtils (com.mesosphere.sdk.offer.CommonIdUtils)5 SchedulerConfig (com.mesosphere.sdk.scheduler.SchedulerConfig)5 com.mesosphere.sdk.specification (com.mesosphere.sdk.specification)5 ConfigStore (com.mesosphere.sdk.state.ConfigStore)5 SchedulerConfigTestUtils (com.mesosphere.sdk.testutils.SchedulerConfigTestUtils)5 TestConstants (com.mesosphere.sdk.testutils.TestConstants)5 TestPodFactory (com.mesosphere.sdk.testutils.TestPodFactory)5 Arrays (java.util.Arrays)5 List (java.util.List)5 Collectors (java.util.stream.Collectors)5