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);
}
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;
}
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);
}
}
Aggregations