mesosphere.marathon.client.model.v2.App Java Examples

The following examples show how to use mesosphere.marathon.client.model.v2.App. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MarathonAppDeployer.java    From spring-cloud-deployer-mesos with Apache License 2.0 6 votes vote down vote up
private AppStatus buildAppStatus(String id, App app) {
	logger.debug("Deployment " + id + " has " + app.getTasksRunning() + "/" + app.getInstances() + " tasks running");
	AppStatus.Builder result = AppStatus.of(id);
	int requestedInstances = app.getInstances();
	int actualInstances = 0;
	if (app.getTasks() != null) {
		for (Task task : app.getTasks()) {
			result.with(MarathonAppInstanceStatus.up(app, task));
			actualInstances++;
		}
	}
	for (int i = actualInstances; i < requestedInstances; i++) {
		result.with(MarathonAppInstanceStatus.down(app));
	}
	return result.build();
}
 
Example #2
Source File: MarathonAppDeployer.java    From spring-cloud-deployer-mesos with Apache License 2.0 6 votes vote down vote up
private AppInstanceStatus buildInstanceStatus(String id) throws MarathonException {
	App appInstance = marathon.getApp(id).getApp();
	logger.debug("Deployment " + id + " has " + appInstance.getTasksRunning() + "/" + appInstance.getInstances() + " tasks running");
	if (appInstance.getTasks() != null) {
		// there should only be one task for this type of deployment
		MarathonAppInstanceStatus status = null;
		for (Task task : appInstance.getTasks()) {
			if (status == null) {
				status = MarathonAppInstanceStatus.up(appInstance, task);
			}
		}
		if (status == null) {
			status = MarathonAppInstanceStatus.down(appInstance);
		}
		return status;
	}
	else {
		return MarathonAppInstanceStatus.down(appInstance);
	}
}
 
Example #3
Source File: MarathonAppDeployer.java    From spring-cloud-deployer-mesos with Apache License 2.0 6 votes vote down vote up
private void deleteAppsForGroupDeployment(String groupId) throws MarathonException {
	Group group = marathon.getGroup(groupId);
	for (App app : group.getApps()) {
		logger.debug(String.format("Deleting application %s in group %s", app.getId(), groupId));
		marathon.deleteApp(app.getId());
	}
	group = marathon.getGroup(groupId);
	if (logger.isDebugEnabled()) {
		logger.debug(String.format("Group %s has %d applications and %d groups", group.getId(),
				group.getApps().size(), group.getGroups().size()));
	}
	if (group.getApps().size() == 0 && group.getGroups().size() == 0) {
		logger.info(String.format("Deleting group: %s", groupId));
		marathon.deleteGroup(groupId);
	}
	deleteTopLevelGroupForDeployment(groupId);
}
 
Example #4
Source File: MarathonServerList.java    From spring-cloud-marathon with MIT License 6 votes vote down vote up
/**
 * Extract instances of a service for a specific marathon application
 *
 * @param app
 * @return
 */
private List<MarathonServer> extractServiceInstances(App app) {

	log.debug("Discovered service [{}]", app.getId());

    if (app.getTasks().isEmpty())
        return Collections.emptyList();

    return app.getTasks()
            .stream()
            .map(task -> {
                Collection<HealthCheckResults> healthChecks =
                        null != task.getHealthCheckResults()
                                ? task.getHealthCheckResults()
                                : new ArrayList<>();

                return new MarathonServer(
                        task.getHost(),
                        task.getPorts().stream().findFirst().orElse(0),
                        healthChecks
                ).withZone(extractZoneFromHostname(task.getHost()));
            })
            .collect(Collectors.toList());

}
 
Example #5
Source File: MarathonDiscoveryClient.java    From spring-cloud-marathon with MIT License 6 votes vote down vote up
private List<ServiceInstance> getInstances(Map<String, String> queryMap) throws MarathonException {
    List<ServiceInstance> instances = new ArrayList<>();

    GetAppsResponse appsResponse = queryMap == null ? client.getApps() : client.getApps(queryMap);

    if (appsResponse != null && appsResponse.getApps() != null) {
        List<VersionedApp> apps = appsResponse.getApps();

        log.debug("Discovered {} service{}{}", apps.size(), apps.size() == 1 ? "" : "s", queryMap == null ? "" : String.format(" with ids that contain [%s]", queryMap.get("id")));

        for (App app : apps) {
            // Fetch data for this specific service id, to collect task information
            GetAppResponse response = client.getApp(app.getId());

            if (response != null && response.getApp() != null) {
                instances.addAll(extractServiceInstances(response.getApp()));
            }
        }
    }

    return instances;
}
 
Example #6
Source File: MarathonBasedService.java    From pravega with Apache License 2.0 6 votes vote down vote up
@Override
public CompletableFuture<Void> scaleService(final int instanceCount) {
    Preconditions.checkArgument(instanceCount >= 0, "negative value: %s", instanceCount);
    try {
        App updatedConfig = new App();
        updatedConfig.setInstances(instanceCount);
        marathonClient.updateApp(getID(), updatedConfig, true);

          return waitUntilServiceRunning(); // wait until scale operation is complete.

    } catch (MarathonException ex) {
        if (ex.getStatus() == CONFLICT.code()) {
            log.error("Scaling operation failed as the application is locked by an ongoing deployment", ex);
            throw new TestFrameworkException(RequestFailed, "Scaling operation failed", ex);
        }
        handleMarathonException(ex);
    }
    return null;
}
 
Example #7
Source File: ZookeeperService.java    From pravega with Apache License 2.0 6 votes vote down vote up
private App createZookeeperApp() {
    App app = new App();
    app.setId(this.id);
    app.setCpus(cpu);
    app.setMem(mem);
    app.setInstances(instances);
    app.setContainer(new Container());
    app.getContainer().setType(CONTAINER_TYPE);
    app.getContainer().setDocker(new Docker());
    app.getContainer().getDocker().setImage(ZK_IMAGE);
    List<HealthCheck> healthCheckList = new ArrayList<>();
    final HealthCheck hc = setHealthCheck(300, "TCP", false, 60, 20, 0, ZKSERVICE_ZKPORT);
    healthCheckList.add(hc);
    app.setHealthChecks(healthCheckList);

    return app;
}
 
Example #8
Source File: MarathonContainer.java    From minimesos with Apache License 2.0 5 votes vote down vote up
/**
 * Updates a Marathon app by JSON string
 *
 * @param marathonJson JSON string
 */
@Override
public void updateApp(String marathonJson) {
    mesosphere.marathon.client.Marathon marathon = MarathonClient.getInstance(getMarathonEndpoint());
    try {
        App app = constructApp(marathonJson);
        marathon.updateApp(app.getId(), app, true);
    } catch (MarathonException e) {
        throw new MinimesosException("Marathon could not update the app, error: " + e.toString());
    }
    LOGGER.debug(format("Installing an app on marathon %s", getMarathonEndpoint()));
}
 
Example #9
Source File: BookkeeperService.java    From pravega with Apache License 2.0 5 votes vote down vote up
private App createBookieApp() {
    App app = new App();
    app.setId(this.id);
    app.setCpus(cpu);
    app.setMem(mem);
    app.setInstances(instances);
    app.setConstraints(setConstraint("hostname", "UNIQUE"));
    app.setContainer(new Container());
    app.getContainer().setType(CONTAINER_TYPE);
    app.getContainer().setDocker(new Docker());
    app.getContainer().getDocker().setImage(IMAGE_PATH + "/nautilus/bookkeeper:" + PRAVEGA_VERSION);
    Collection<Volume> volumeCollection = new ArrayList<>();
    volumeCollection.add(createVolume("/bk", "mnt", "RW"));
    //TODO: add persistent volume  (see issue https://github.com/pravega/pravega/issues/639)
    app.getContainer().setVolumes(volumeCollection);
    app.setPorts(Arrays.asList(BK_PORT));
    app.setRequirePorts(true);
    //set env
    String zk = zkUri.getHost() + ":" + ZKSERVICE_ZKPORT;
    Map<String, Object> map = new HashMap<>();
    map.put("ZK_URL", zk);
    map.put("ZK", zk);
    map.put("bookiePort", String.valueOf(BK_PORT));
    map.put("DLOG_EXTRA_OPTS", "-Xms512m");
    app.setEnv(map);
    //healthchecks
    List<HealthCheck> healthCheckList = new ArrayList<>();
    healthCheckList.add(setHealthCheck(300, "TCP", false, 60, 20, 0, BK_PORT));
    app.setHealthChecks(healthCheckList);

    return app;
}
 
Example #10
Source File: MarathonDiscoveryClient.java    From spring-cloud-marathon with MIT License 5 votes vote down vote up
@Override
public List<String> getServices() {
    try {
        return client.getApps()
                .getApps()
                .parallelStream()
                .map(App::getId)
                .map(ServiceIdConverter::convertToServiceId)
                .collect(Collectors.toList());
    } catch (MarathonException e) {
        log.error(e.getMessage(), e);
        return Collections.emptyList();
    }
}
 
Example #11
Source File: MarathonDiscoveryClient.java    From spring-cloud-marathon with MIT License 5 votes vote down vote up
/**
 * Extract instances of a service for a specific marathon application
 *
 * @param app
 * @return
 */
public List<ServiceInstance> extractServiceInstances(App app) {
    log.debug("Discovered service [{}]", app.getId());

    if (app.getTasks().isEmpty()) {
        return Collections.emptyList();
    }

    return app.getTasks()
            .parallelStream()
            .filter(task -> null == task.getHealthCheckResults() ||
                    task.getHealthCheckResults()
                            .stream()
                            .allMatch(HealthCheckResults::getAlive)
            )
            .map(task -> new DefaultServiceInstance(
                    ServiceIdConverter.convertToServiceId(task.getAppId()),
                    task.getHost(),
                    task.getPorts().stream().findFirst().orElse(0),
                    false
            )).map(serviceInstance -> {
                if (app.getLabels() != null && !app.getLabels().isEmpty())
                    serviceInstance.getMetadata().putAll(app.getLabels());
                return serviceInstance;
            })
            .collect(Collectors.toList());
}
 
Example #12
Source File: MarathonAppDeployer.java    From spring-cloud-deployer-mesos with Apache License 2.0 4 votes vote down vote up
private void createAppDeployment(AppDeploymentRequest request, String deploymentId, Container container, Integer index) {
	App app = new App();
	app.setContainer(container);
	app.setId(deploymentId);

	Map<String, String> env = new HashMap<>();
	env.putAll(request.getDefinition().getProperties());
	for (String envVar : properties.getEnvironmentVariables()) {
		String[] strings = envVar.split("=", 2);
		Assert.isTrue(strings.length == 2, "Invalid environment variable declared: " + envVar);
		env.put(strings[0], strings[1]);
	}
	if (index != null) {
		env.put(INSTANCE_INDEX_PROPERTY_KEY, index.toString());
	}
	app.setEnv(env);

	Collection<String> uris = deduceUris(request);
	app.setUris(uris);

	Collection<Constraint> constraints = deduceConstraints(request);
	app.setConstraints(constraints.stream().map(Constraint::toStringList).collect(Collectors.toList()));

	Double cpus = deduceCpus(request);
	Double memory = deduceMemory(request);
	Integer instances = index == null ? deduceInstances(request) : 1;

	app.setCpus(cpus);
	app.setMem(memory);
	app.setInstances(instances);

	HealthCheck healthCheck = new HealthCheck();
	healthCheck.setPath("/health");
	healthCheck.setGracePeriodSeconds(300);
	app.setHealthChecks(Arrays.asList(healthCheck));

	logger.debug("Creating app with definition:\n" + app.toString());
	try {
		marathon.createApp(app);
	}
	catch (MarathonException e) {
		throw new RuntimeException(e);
	}
}
 
Example #13
Source File: PravegaControllerService.java    From pravega with Apache License 2.0 4 votes vote down vote up
/**
 * To configure the controller app.
 *
 * @return App instance of marathon app
 */
private App createPravegaControllerApp() {
    App app = new App();
    app.setId(this.id);
    app.setCpus(cpu);
    app.setMem(mem);
    app.setInstances(instances);
    app.setConstraints(setConstraint("hostname", "UNIQUE"));
    app.setContainer(new Container());
    app.getContainer().setType(CONTAINER_TYPE);
    app.getContainer().setDocker(new Docker());
    app.getContainer().getDocker().setImage(IMAGE_PATH + "/nautilus/pravega:" + PRAVEGA_VERSION);
    String zk = zkUri.getHost() + ":" + ZKSERVICE_ZKPORT;
    //set port
    app.setPortDefinitions(Arrays.asList(createPortDefinition(CONTROLLER_PORT), createPortDefinition(REST_PORT)));
    app.setRequirePorts(true);
    List<HealthCheck> healthCheckList = new ArrayList<HealthCheck>();
    healthCheckList.add(setHealthCheck(300, "TCP", false, 60, 20, 0, CONTROLLER_PORT));
    app.setHealthChecks(healthCheckList);

    String componentCode = "controller";

    //set env
    String controllerSystemProperties = "-Xmx512m" +
            setSystemProperty(Config.PROPERTY_ZK_URL.getFullName(componentCode), zk) +
            setSystemProperty(Config.PROPERTY_RPC_HOST.getFullName(componentCode), this.id + ".marathon.mesos") +
            setSystemProperty(Config.PROPERTY_RPC_PORT.getFullName(componentCode), String.valueOf(CONTROLLER_PORT)) +
            setSystemProperty(Config.PROPERTY_SERVICE_PORT.getFullName(componentCode), String.valueOf(CONTROLLER_PORT)) +
            setSystemProperty(Config.PROPERTY_REST_PORT.getFullName(componentCode), String.valueOf(REST_PORT)) +
            setSystemProperty("log.level", "DEBUG") +
            setSystemProperty("log.dir", "$MESOS_SANDBOX/pravegaLogs") +
            setSystemProperty("curator-default-session-timeout", String.valueOf(10 * 1000)) +
            setSystemProperty(Config.PROPERTY_TXN_MAX_LEASE.getFullName(componentCode), String.valueOf(120 * 1000)) +
            setSystemProperty(Config.PROPERTY_RETENTION_FREQUENCY_MINUTES.getFullName(componentCode), String.valueOf(2));

    Map<String, Object> map = new HashMap<>();
    map.put("PRAVEGA_CONTROLLER_OPTS", controllerSystemProperties);
    app.setEnv(map);
    app.setArgs(Arrays.asList("controller"));

    return app;
}
 
Example #14
Source File: PravegaSegmentStoreService.java    From pravega with Apache License 2.0 4 votes vote down vote up
private App createPravegaSegmentStoreApp() {
    App app = new App();
    app.setId(this.id);
    app.setCpus(cpu);
    app.setMem(mem);
    app.setInstances(instances);
    //set constraints
    app.setConstraints(setConstraint("hostname", "UNIQUE"));
    //docker container
    app.setContainer(new Container());
    app.getContainer().setType(CONTAINER_TYPE);
    app.getContainer().setDocker(new Docker());
    //set the image and network
    app.getContainer().getDocker().setImage(IMAGE_PATH + "/nautilus/pravega:" + PRAVEGA_VERSION);
    //set port
    app.setPortDefinitions(Arrays.asList(createPortDefinition(SEGMENTSTORE_PORT)));
    app.setRequirePorts(true);
    //healthchecks
    List<HealthCheck> healthCheckList = new ArrayList<HealthCheck>();
    healthCheckList.add(setHealthCheck(300, "TCP", false, 60, 20, 0, SEGMENTSTORE_PORT));
    app.setHealthChecks(healthCheckList);
    //set env
    String zk = zkUri.getHost() + ":" + ZKSERVICE_ZKPORT;

    //Environment variables to configure SS service.
    Map<String, Object> map = new HashMap<>();
    map.put("ZK_URL", zk);
    map.put("BK_ZK_URL", zk);
    map.put("CONTROLLER_URL", conUri.toString());
    getCustomEnvVars(map, SEGMENTSTORE_EXTRA_ENV);

    //Properties set to override defaults for system tests
    String hostSystemProperties = "-Xmx1024m" +
            setSystemProperty("autoScale.muteInSeconds", "120") +
            setSystemProperty("autoScale.cooldownInSeconds", "120") +
            setSystemProperty("autoScale.cacheExpiryInSeconds", "120") +
            setSystemProperty("autoScale.cacheCleanUpInSeconds", "120") +
            setSystemProperty("log.level", "DEBUG") +
            setSystemProperty("log.dir", "$MESOS_SANDBOX/pravegaLogs") +
            setSystemProperty("curator-default-session-timeout", String.valueOf(30 * 1000)) +
            setSystemProperty("hdfs.replaceDataNodesOnFailure", "false");

    map.put("PRAVEGA_SEGMENTSTORE_OPTS", hostSystemProperties);
    app.setEnv(map);
    app.setArgs(Arrays.asList("segmentstore"));

    return app;
}
 
Example #15
Source File: MarathonContainer.java    From minimesos with Apache License 2.0 4 votes vote down vote up
private App constructApp(String appJson) {
    Gson gson = new Gson();
    return gson.fromJson(replaceTokens(appJson), App.class);
}
 
Example #16
Source File: Marathon.java    From marathon-client with Apache License 2.0 4 votes vote down vote up
@RequestLine("POST /v2/apps")
App createApp(App app) throws MarathonException;
 
Example #17
Source File: Marathon.java    From marathon-client with Apache License 2.0 4 votes vote down vote up
@RequestLine("PUT /v2/apps/{app_id}")
void updateApp(@Named("app_id") String appId, App app);
 
Example #18
Source File: Marathon.java    From marathon-client with Apache License 2.0 4 votes vote down vote up
@RequestLine("GET /v2/apps/{id}/versions/{version}")
@Headers(HeaderUtils.MARATHON_API_SOURCE_HEADER)
App getAppVersion(@Param("id") String id, @Param("version") String version) throws MarathonException;
 
Example #19
Source File: MarathonAppInstanceStatus.java    From spring-cloud-deployer-mesos with Apache License 2.0 4 votes vote down vote up
/**
 * Construct a status from a missing app task (maybe it crashed, maybe Mesos could not offer enough resources, etc.)
 */
static MarathonAppInstanceStatus down(App app) {
	return new MarathonAppInstanceStatus(app, null);
}
 
Example #20
Source File: MarathonAppInstanceStatus.java    From spring-cloud-deployer-mesos with Apache License 2.0 4 votes vote down vote up
/**
 * Construct a status from a running app task.
 */
static MarathonAppInstanceStatus up(App app, Task task) {
	return new MarathonAppInstanceStatus(app, task);
}
 
Example #21
Source File: MarathonAppInstanceStatus.java    From spring-cloud-deployer-mesos with Apache License 2.0 4 votes vote down vote up
private MarathonAppInstanceStatus(App app, Task task) {
	this.app = app;
	this.task = task;
}
 
Example #22
Source File: DCOS.java    From marathon-client with Apache License 2.0 4 votes vote down vote up
default Optional<App> maybeApp(final String id) throws DCOSException {
    return resource(() -> getApp(id).getApp());
}
 
Example #23
Source File: Marathon.java    From marathon-client with Apache License 2.0 4 votes vote down vote up
@RequestLine("PUT /v2/apps/{app_id}?force={force}")
@Headers(HeaderUtils.MARATHON_API_SOURCE_HEADER)
Result updateApp(@Param("app_id") String appId, App app,
		@Param("force") boolean force) throws MarathonException;
 
Example #24
Source File: Marathon.java    From marathon-client with Apache License 2.0 4 votes vote down vote up
@RequestLine("POST /v2/apps")
@Headers(HeaderUtils.MARATHON_API_SOURCE_HEADER)
VersionedApp createApp(App app) throws MarathonException;
 
Example #25
Source File: MarathonBuilder.java    From marathon-plugin with Apache License 2.0 2 votes vote down vote up
/**
 * Set Marathon application from JSON object.
 *
 * @param json JSON object to initially build Marathon application from
 */
protected void setAppFromJson(JSONObject json) throws JsonSyntaxException {
    this.app = ModelUtils.GSON.fromJson(json.toString(), App.class);
}
 
Example #26
Source File: MarathonBuilder.java    From marathon-plugin with Apache License 2.0 votes vote down vote up
public App getApp() { return this.app; }