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