Search in sources :

Example 1 with Port

use of org.apache.mesos.Protos.Port 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 Port

use of org.apache.mesos.Protos.Port in project dcos-commons by mesosphere.

the class OfferEvaluatorPortsTest method testReserveTaskNamedVIPPort.

@SuppressWarnings("PMD.AvoidUsingHardCodedIP")
@Test
public void testReserveTaskNamedVIPPort() throws Exception {
    List<OfferRecommendation> recommendations = evaluator.evaluate(PodInstanceRequirementTestUtils.getVIPRequirement(80, 10000), OfferTestUtils.getCompleteOffers(ResourceTestUtils.getUnreservedPorts(10000, 10000)));
    Assert.assertEquals(5, recommendations.size());
    Operation launchOperation = recommendations.get(4).getOperation();
    TaskInfo taskInfo = launchOperation.getLaunchGroup().getTaskGroup().getTasks(0);
    Resource fulfilledPortResource = taskInfo.getResources(0);
    Assert.assertEquals(10000, fulfilledPortResource.getRanges().getRange(0).getBegin());
    Assert.assertFalse(getResourceId(fulfilledPortResource).isEmpty());
    DiscoveryInfo discoveryInfo = taskInfo.getDiscovery();
    Assert.assertEquals(discoveryInfo.getName(), taskInfo.getName());
    Assert.assertEquals(discoveryInfo.getVisibility(), DiscoveryInfo.Visibility.CLUSTER);
    Port discoveryPort = discoveryInfo.getPorts().getPorts(0);
    Assert.assertEquals(discoveryPort.getProtocol(), "tcp");
    Assert.assertEquals(discoveryPort.getVisibility(), DiscoveryInfo.Visibility.EXTERNAL);
    Assert.assertEquals(discoveryPort.getNumber(), 10000);
    Label vipLabel = discoveryPort.getLabels().getLabels(0);
    Assert.assertTrue(vipLabel.getKey().startsWith("VIP_"));
    Assert.assertEquals(vipLabel.getValue(), TestConstants.VIP_NAME + "-10000:80");
    Map<String, String> envvars = EnvUtils.toMap(TaskPackingUtils.unpack(taskInfo).getCommand().getEnvironment());
    Assert.assertEquals(String.valueOf(10000), envvars.get(TestConstants.PORT_ENV_NAME + "_VIP_10000"));
}
Also used : TaskInfo(org.apache.mesos.Protos.TaskInfo) DiscoveryInfo(org.apache.mesos.Protos.DiscoveryInfo) Port(org.apache.mesos.Protos.Port) Resource(org.apache.mesos.Protos.Resource) Label(org.apache.mesos.Protos.Label) Operation(org.apache.mesos.Protos.Offer.Operation) OfferRecommendation(com.mesosphere.sdk.offer.OfferRecommendation) Test(org.junit.Test)

Example 3 with Port

use of org.apache.mesos.Protos.Port in project dcos-commons by mesosphere.

the class OfferEvaluatorPortsTest method testReserveTaskDynamicVIPPortCustomExecutor.

@SuppressWarnings("PMD.AvoidUsingHardCodedIP")
@Test
public void testReserveTaskDynamicVIPPortCustomExecutor() throws Exception {
    useCustomExecutor();
    List<OfferRecommendation> recommendations = evaluator.evaluate(PodInstanceRequirementTestUtils.getVIPRequirement(80, 0), OfferTestUtils.getOffers(ResourceTestUtils.getUnreservedPorts(10000, 10000)));
    Assert.assertEquals(2, recommendations.size());
    Operation launchOperation = recommendations.get(1).getOperation();
    TaskInfo taskInfo = launchOperation.getLaunch().getTaskInfos(0);
    Resource fulfilledPortResource = taskInfo.getResources(0);
    Assert.assertEquals(10000, fulfilledPortResource.getRanges().getRange(0).getBegin());
    Assert.assertFalse(getResourceId(fulfilledPortResource).isEmpty());
    DiscoveryInfo discoveryInfo = taskInfo.getDiscovery();
    Assert.assertEquals(discoveryInfo.getName(), taskInfo.getName());
    Assert.assertEquals(discoveryInfo.getVisibility(), DiscoveryInfo.Visibility.CLUSTER);
    Port discoveryPort = discoveryInfo.getPorts().getPorts(0);
    Assert.assertEquals(discoveryPort.getProtocol(), "tcp");
    Assert.assertEquals(discoveryPort.getVisibility(), DiscoveryInfo.Visibility.EXTERNAL);
    Assert.assertEquals(discoveryPort.getNumber(), 10000);
    Label vipLabel = discoveryPort.getLabels().getLabels(0);
    Assert.assertTrue(vipLabel.getKey().startsWith("VIP_"));
    Assert.assertEquals(vipLabel.getValue(), TestConstants.VIP_NAME + "-0:80");
    Map<String, String> envvars = EnvUtils.toMap(TaskPackingUtils.unpack(taskInfo).getCommand().getEnvironment());
    Assert.assertEquals(String.valueOf(10000), envvars.get(TestConstants.PORT_ENV_NAME + "_VIP_0"));
}
Also used : TaskInfo(org.apache.mesos.Protos.TaskInfo) DiscoveryInfo(org.apache.mesos.Protos.DiscoveryInfo) Port(org.apache.mesos.Protos.Port) Resource(org.apache.mesos.Protos.Resource) Label(org.apache.mesos.Protos.Label) Operation(org.apache.mesos.Protos.Offer.Operation) OfferRecommendation(com.mesosphere.sdk.offer.OfferRecommendation) Test(org.junit.Test)

Example 4 with Port

use of org.apache.mesos.Protos.Port in project dcos-commons by mesosphere.

the class OfferEvaluatorPortsTest method testReserveTaskDynamicVIPPort.

@SuppressWarnings("PMD.AvoidUsingHardCodedIP")
@Test
public void testReserveTaskDynamicVIPPort() throws Exception {
    List<OfferRecommendation> recommendations = evaluator.evaluate(PodInstanceRequirementTestUtils.getVIPRequirement(80, 0), OfferTestUtils.getCompleteOffers(ResourceTestUtils.getUnreservedPorts(10000, 10000)));
    Assert.assertEquals(5, recommendations.size());
    Operation launchOperation = recommendations.get(4).getOperation();
    TaskInfo taskInfo = launchOperation.getLaunchGroup().getTaskGroup().getTasks(0);
    Resource fulfilledPortResource = taskInfo.getResources(0);
    Assert.assertEquals(10000, fulfilledPortResource.getRanges().getRange(0).getBegin());
    Assert.assertFalse(getResourceId(fulfilledPortResource).isEmpty());
    DiscoveryInfo discoveryInfo = taskInfo.getDiscovery();
    Assert.assertEquals(discoveryInfo.getName(), taskInfo.getName());
    Assert.assertEquals(discoveryInfo.getVisibility(), DiscoveryInfo.Visibility.CLUSTER);
    Port discoveryPort = discoveryInfo.getPorts().getPorts(0);
    Assert.assertEquals(discoveryPort.getProtocol(), "tcp");
    Assert.assertEquals(discoveryPort.getVisibility(), DiscoveryInfo.Visibility.EXTERNAL);
    Assert.assertEquals(discoveryPort.getNumber(), 10000);
    Label vipLabel = discoveryPort.getLabels().getLabels(0);
    Assert.assertTrue(vipLabel.getKey().startsWith("VIP_"));
    Assert.assertEquals(vipLabel.getValue(), TestConstants.VIP_NAME + "-0:80");
    Map<String, String> envvars = EnvUtils.toMap(TaskPackingUtils.unpack(taskInfo).getCommand().getEnvironment());
    Assert.assertEquals(String.valueOf(10000), envvars.get(TestConstants.PORT_ENV_NAME + "_VIP_0"));
}
Also used : TaskInfo(org.apache.mesos.Protos.TaskInfo) DiscoveryInfo(org.apache.mesos.Protos.DiscoveryInfo) Port(org.apache.mesos.Protos.Port) Resource(org.apache.mesos.Protos.Resource) Label(org.apache.mesos.Protos.Label) Operation(org.apache.mesos.Protos.Offer.Operation) OfferRecommendation(com.mesosphere.sdk.offer.OfferRecommendation) Test(org.junit.Test)

Aggregations

DiscoveryInfo (org.apache.mesos.Protos.DiscoveryInfo)4 Port (org.apache.mesos.Protos.Port)4 TaskInfo (org.apache.mesos.Protos.TaskInfo)4 OfferRecommendation (com.mesosphere.sdk.offer.OfferRecommendation)3 Label (org.apache.mesos.Protos.Label)3 Operation (org.apache.mesos.Protos.Offer.Operation)3 Resource (org.apache.mesos.Protos.Resource)3 Test (org.junit.Test)3 TaskLabelReader (com.mesosphere.sdk.offer.taskdata.TaskLabelReader)1 JSONObject (org.json.JSONObject)1