Search in sources :

Example 1 with TaskLabelReader

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

the class EndpointsQueries method getDiscoveryEndpoints.

/**
 * Returns a mapping of endpoint type to host:port (or ip:port) endpoints, endpoint type.
 */
private static Map<String, JSONObject> getDiscoveryEndpoints(StateStore stateStore, String frameworkName, SchedulerConfig schedulerConfig) throws TaskException {
    Map<String, JSONObject> endpointsByName = new TreeMap<>();
    for (TaskInfo taskInfo : stateStore.fetchTasks()) {
        if (!taskInfo.hasDiscovery()) {
            LOGGER.debug("Task lacks any discovery information, no endpoints to report: {}", taskInfo.getName());
            continue;
        }
        // TODO(mrb): Also extract DiscoveryInfo from executor, when executors get the ability to specify resources
        DiscoveryInfo discoveryInfo = taskInfo.getDiscovery();
        // Autoip hostname:
        String autoIpTaskName = discoveryInfo.hasName() ? discoveryInfo.getName() : taskInfo.getName();
        // Hostname of agent at offer time:
        String nativeHost = new TaskLabelReader(taskInfo).getHostname();
        // get IP address(es) from container status on the latest TaskStatus, if the latest TaskStatus has an IP
        // otherwise use the lastest TaskStatus' IP stored in the stateStore
        List<String> ipAddresses = reconcileIpAddresses(stateStore, taskInfo.getName());
        for (Port port : discoveryInfo.getPorts().getPortsList()) {
            if (port.getVisibility() != Constants.DISPLAYED_PORT_VISIBILITY) {
                LOGGER.debug("Port {} in task {} has {} visibility. {} is needed to be listed in endpoints.", port.getName(), taskInfo.getName(), port.getVisibility(), Constants.DISPLAYED_PORT_VISIBILITY);
                continue;
            }
            final String hostIpString;
            switch(ipAddresses.size()) {
                case 0:
                    hostIpString = nativeHost;
                    break;
                case 1:
                    hostIpString = ipAddresses.get(0);
                    break;
                default:
                    hostIpString = ipAddresses.toString();
                    break;
            }
            addPortToEndpoints(endpointsByName, frameworkName, taskInfo.getName(), port, EndpointUtils.toAutoIpEndpoint(frameworkName, autoIpTaskName, port.getNumber(), schedulerConfig), EndpointUtils.toEndpoint(hostIpString, port.getNumber()));
        }
    }
    return endpointsByName;
}
Also used : TaskInfo(org.apache.mesos.Protos.TaskInfo) DiscoveryInfo(org.apache.mesos.Protos.DiscoveryInfo) TaskLabelReader(com.mesosphere.sdk.offer.taskdata.TaskLabelReader) JSONObject(org.json.JSONObject) Port(org.apache.mesos.Protos.Port)

Example 2 with TaskLabelReader

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

the class PodQueries method list.

/**
 * Produces a listing of all pod instance names.
 */
public static Response list(StateStore stateStore) {
    try {
        Set<String> podNames = new TreeSet<>();
        List<String> unknownTaskNames = new ArrayList<>();
        for (Protos.TaskInfo taskInfo : stateStore.fetchTasks()) {
            TaskLabelReader labels = new TaskLabelReader(taskInfo);
            try {
                podNames.add(PodInstance.getName(labels.getType(), labels.getIndex()));
            } catch (Exception e) {
                LOGGER.warn(String.format("Failed to extract pod information from task %s", taskInfo.getName()), e);
                unknownTaskNames.add(taskInfo.getName());
            }
        }
        JSONArray jsonArray = new JSONArray(podNames);
        if (!unknownTaskNames.isEmpty()) {
            Collections.sort(unknownTaskNames);
            for (String unknownName : unknownTaskNames) {
                jsonArray.put(String.format("%s_%s", UNKNOWN_POD_LABEL, unknownName));
            }
        }
        return jsonOkResponse(jsonArray);
    } catch (Exception e) {
        LOGGER.error("Failed to fetch list of pods", e);
        return Response.serverError().build();
    }
}
Also used : TaskLabelReader(com.mesosphere.sdk.offer.taskdata.TaskLabelReader) Protos(org.apache.mesos.Protos) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) TaskException(com.mesosphere.sdk.offer.TaskException)

Example 3 with TaskLabelReader

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

the class TaskUtils method getPodSpec.

private static PodSpec getPodSpec(ConfigStore<ServiceSpec> configStore, Protos.TaskInfo taskInfo) throws TaskException {
    UUID configId = new TaskLabelReader(taskInfo).getTargetConfiguration();
    ServiceSpec serviceSpec;
    try {
        serviceSpec = configStore.fetch(configId);
    } catch (ConfigStoreException e) {
        throw new TaskException(String.format("Unable to retrieve ServiceSpecification ID %s referenced by TaskInfo[%s]", configId, taskInfo.getName()), e);
    }
    Optional<PodSpec> podSpecOptional = getPodSpec(serviceSpec, taskInfo);
    if (!podSpecOptional.isPresent()) {
        throw new TaskException(String.format("No TaskSpecification found for TaskInfo[%s]", taskInfo.getName()));
    } else {
        return podSpecOptional.get();
    }
}
Also used : TaskLabelReader(com.mesosphere.sdk.offer.taskdata.TaskLabelReader) ConfigStoreException(com.mesosphere.sdk.state.ConfigStoreException)

Example 4 with TaskLabelReader

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

the class TaskTypeRuleTest 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) ResourceSet(com.mesosphere.sdk.specification.ResourceSet) DefaultPodInstance(com.mesosphere.sdk.scheduler.plan.DefaultPodInstance) InvalidRequirementException(com.mesosphere.sdk.offer.InvalidRequirementException) IOException(java.io.IOException)

Example 5 with TaskLabelReader

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

the class OfferEvaluatorTest method testLaunchAttributesEmbedded.

@Test
public void testLaunchAttributesEmbedded() throws Exception {
    PodInstanceRequirement podInstanceRequirement = PodInstanceRequirementTestUtils.getCpuRequirement(1.0);
    // Launch for the first time.
    String resourceId = getFirstResourceId(recordLaunchWithCompleteOfferedResources(podInstanceRequirement, ResourceTestUtils.getUnreservedCpus(2.0)));
    Collection<Resource> expectedResources = getExpectedExecutorResources(stateStore.fetchTasks().iterator().next().getExecutor());
    expectedResources.add(ResourceTestUtils.getReservedCpus(1.0, resourceId));
    Offer.Builder offerBuilder = OfferTestUtils.getOffer(expectedResources).toBuilder();
    Attribute.Builder attrBuilder = offerBuilder.addAttributesBuilder().setName("rack").setType(Value.Type.TEXT);
    attrBuilder.getTextBuilder().setValue("foo");
    attrBuilder = offerBuilder.addAttributesBuilder().setName("diskspeed").setType(Value.Type.SCALAR);
    attrBuilder.getScalarBuilder().setValue(1234.5678);
    List<OfferRecommendation> recommendations = evaluator.evaluate(podInstanceRequirement, Arrays.asList(offerBuilder.build()));
    Assert.assertEquals(1, recommendations.size());
    // Validate LAUNCH Operation
    Operation launchOperation = recommendations.get(0).getOperation();
    Assert.assertEquals(Operation.Type.LAUNCH_GROUP, launchOperation.getType());
    // Validate that TaskInfo has embedded the Attributes from the selected offer:
    TaskInfo launchTask = launchOperation.getLaunchGroup().getTaskGroup().getTasks(0);
    Assert.assertEquals(Arrays.asList("rack:foo", "diskspeed:1234.568"), new TaskLabelReader(launchTask).getOfferAttributeStrings());
    Resource launchResource = launchTask.getResources(0);
    Assert.assertEquals(resourceId, getResourceId(launchResource));
}
Also used : TaskLabelReader(com.mesosphere.sdk.offer.taskdata.TaskLabelReader) Operation(org.apache.mesos.Protos.Offer.Operation) Test(org.junit.Test)

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