Skip to content

Commit

Permalink
Add command array to available Titus launcher options
Browse files Browse the repository at this point in the history
  • Loading branch information
tgianos committed Mar 4, 2021
1 parent bcced55 commit 1ed012e
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 49 deletions.
9 changes: 7 additions & 2 deletions genie-docs/src/docs/asciidoc/_properties.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,11 @@ https://docs.spring.io/spring-boot/docs/current/reference/html/spring-boot-featu
|default
|yes

|genie.agent.launcher.titus.command-template
|The container command array, placeholder values are substituted at runtime
|[exec, --api-job, --launchInJobDirectory, --job-id, <JOB_ID>, --server-host, <SERVER_HOST>, --server-port, <SERVER_PORT>]
|no

|genie.agent.launcher.titus.container-attributes
|Map attributes to send to Titus specific to the container
|empty
Expand All @@ -208,8 +213,8 @@ https://docs.spring.io/spring-boot/docs/current/reference/html/spring-boot-featu
|no

|genie.agent.launcher.titus.entry-point-template
|The container entry point expression, placeholder values are substituted at runtime
|[/bin/genie-agent, exec, --api-job, --launchInJobDirectory, --job-id, <JOB_ID>, --server-host, <SERVER_HOST>, --server-port, <SERVER_PORT>]
|The container entry point array, placeholder values are substituted at runtime
|[/bin/genie-agent]
|no

|genie.agent.launcher.titus.genie-server-host
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,13 @@ public static class Container {
@NonNull
private Image image;
@NotNull
@NotEmpty
@NonNull
private List<String> entryPoint;
@NotNull
@NonNull
private List<String> command;
@NotNull
@NonNull
private Map<String, String> env;
@NotNull
@NonNull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.function.BiFunction;
import java.util.stream.Collectors;

/**
Expand All @@ -76,6 +77,11 @@ public class TitusAgentLauncherImpl implements AgentLauncher {
private static final Tag CLASS_TAG = Tag.of(LAUNCHER_CLASS_KEY, THIS_CLASS);
private static final int TITUS_JOB_BATCH_SIZE = 1;
private static final int ZERO = 0;
private static final BiFunction<List<String>, Map<String, String>, List<String>> REPLACE_PLACEHOLDERS =
(template, placeholders) -> template
.stream()
.map(s -> placeholders.getOrDefault(s, s))
.collect(Collectors.toList());
private final RestTemplate restTemplate;
private final Cache<String, String> healthIndicatorCache;
private final GenieHostInfo genieHostInfo;
Expand Down Expand Up @@ -216,11 +222,15 @@ private TitusBatchJobRequest createJobRequest(final ResolvedJob resolvedJob) thr
String.valueOf(this.titusAgentLauncherProperties.getGenieServerPort())
);

// Substitute all placeholders with their values
final List<String> entryPoint = this.titusAgentLauncherProperties.getEntryPointTemplate()
.stream()
.map(s -> placeholdersMap.getOrDefault(s, s))
.collect(Collectors.toList());
// Substitute all placeholders with their values for the container entry point and command
final List<String> entryPoint = REPLACE_PLACEHOLDERS.apply(
this.titusAgentLauncherProperties.getEntryPointTemplate(),
placeholdersMap
);
final List<String> command = REPLACE_PLACEHOLDERS.apply(
this.titusAgentLauncherProperties.getCommandTemplate(),
placeholdersMap
);

final long memory = Math.max(
this.getDataSizeProperty(
Expand Down Expand Up @@ -278,19 +288,7 @@ private TitusBatchJobRequest createJobRequest(final ResolvedJob resolvedJob) thr
);
final Duration runtimeLimit = this.titusAgentLauncherProperties.getRuntimeLimit();

final Map<String, String> jobAttributes = new HashMap<>();
jobAttributes.put(GENIE_USER_ATTR, resolvedJob.getJobMetadata().getUser());
jobAttributes.put(GENIE_SOURCE_HOST_ATTR, this.genieHostInfo.getHostname());
jobAttributes.put(GENIE_ENDPOINT_ATTR, this.titusAgentLauncherProperties.getGenieServerHost());
jobAttributes.put(GENIE_JOB_ID_ATTR, jobId);
jobAttributes.putAll(
this.binder
.bind(
TitusAgentLauncherProperties.ADDITIONAL_JOB_ATTRIBUTES_PROPERTY,
Bindable.mapOf(String.class, String.class)
)
.orElse(new HashMap<>())
);
final Map<String, String> jobAttributes = this.createJobAttributes(jobId, resolvedJob);

final TitusBatchJobRequest request = TitusBatchJobRequest.builder()
.owner(
Expand Down Expand Up @@ -346,6 +344,7 @@ private TitusBatchJobRequest createJobRequest(final ResolvedJob resolvedJob) thr
.build()
)
.entryPoint(entryPoint)
.command(command)
.env(
this.binder
.bind(
Expand Down Expand Up @@ -431,6 +430,23 @@ private DataSize getDataSizeProperty(final String propertyKey, final DataSize de
}
}

private Map<String, String> createJobAttributes(final String jobId, final ResolvedJob resolvedJob) {
final Map<String, String> jobAttributes = new HashMap<>();
jobAttributes.put(GENIE_USER_ATTR, resolvedJob.getJobMetadata().getUser());
jobAttributes.put(GENIE_SOURCE_HOST_ATTR, this.genieHostInfo.getHostname());
jobAttributes.put(GENIE_ENDPOINT_ATTR, this.titusAgentLauncherProperties.getGenieServerHost());
jobAttributes.put(GENIE_JOB_ID_ATTR, jobId);
jobAttributes.putAll(
this.binder
.bind(
TitusAgentLauncherProperties.ADDITIONAL_JOB_ATTRIBUTES_PROPERTY,
Bindable.mapOf(String.class, String.class)
)
.orElse(new HashMap<>())
);
return jobAttributes;
}

/**
* An interface that should be implemented by any class which wants to modify the Titus job request before it is
* sent.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,9 +178,13 @@ public class TitusAgentLauncherProperties {
* The Titus job container entry point.
* Placeholder values are substituted before submission
*/
@NotEmpty(message = "The command-line launch template cannot be empty")
private List<@NotBlank String> entryPointTemplate = Arrays.asList(
"/bin/genie-agent",
private List<@NotBlank String> entryPointTemplate = Arrays.asList("/bin/genie-agent");

/**
* The Titus job container command array.
* Placeholder values are substituted before submission
*/
private List<@NotBlank String> commandTemplate = Arrays.asList(
"exec",
"--api-job",
"--launchInJobDirectory",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,9 @@ class TitusAgentLauncherImplSpec extends Specification {
requestCapture.getContainer().getImage().getName() == launcherProperties.getImageName()
requestCapture.getContainer().getImage().getTag() == launcherProperties.getImageTag()
requestCapture.getContainer().getEntryPoint() == [
"/bin/genie-agent",
"/bin/genie-agent"
]
requestCapture.getContainer().getCommand() == [
"exec",
"--api-job",
"--launchInJobDirectory",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,7 @@ class TitusAgentLauncherPropertiesSpec extends Specification {
expect:
!p.isEnabled()
p.getEndpoint() == URI.create("https://example-titus-endpoint.tld:1234")
p.getEntryPointTemplate() == [
"/bin/genie-agent",
"exec",
"--api-job",
"--launchInJobDirectory",
"--job-id", TitusAgentLauncherProperties.JOB_ID_PLACEHOLDER,
"--server-host", TitusAgentLauncherProperties.SERVER_HOST_PLACEHOLDER,
"--server-port", TitusAgentLauncherProperties.SERVER_PORT_PLACEHOLDER
]
p.getEntryPointTemplate() == ["/bin/genie-agent"]
p.getOwnerEmail() == "owners@genie.tld"
p.getApplicationName() == "genie"
p.getCapacityGroup() == "default"
Expand Down Expand Up @@ -70,19 +62,24 @@ class TitusAgentLauncherPropertiesSpec extends Specification {
p.getStack() == ""
p.getDetail() == ""
p.getSequence() == ""
p.getCommandTemplate() == [
"exec",
"--api-job",
"--launchInJobDirectory",
"--job-id", TitusAgentLauncherProperties.JOB_ID_PLACEHOLDER,
"--server-host", TitusAgentLauncherProperties.SERVER_HOST_PLACEHOLDER,
"--server-port", TitusAgentLauncherProperties.SERVER_PORT_PLACEHOLDER
]

when:
p.setEnabled(true)
p.setEndpoint(URI.create("https://test-titus-endpoint.tld:4321"))
p.setEntryPointTemplate([
"/usr/local/bin/genie-agent",
"exec",
"--server-host", TitusAgentLauncherProperties.SERVER_HOST_PLACEHOLDER,
"--server-port", TitusAgentLauncherProperties.SERVER_PORT_PLACEHOLDER,
"--job-id", TitusAgentLauncherProperties.JOB_ID_PLACEHOLDER,
"--api-job",
"--launchInJobDirectory",
])
p.setEntryPointTemplate(
[
"/usr/local/bin/genie-agent",
"exec"
]
)
p.setOwnerEmail("genie@genie.tld")
p.setApplicationName("genie-foo")
p.setCapacityGroup("genie-cg")
Expand Down Expand Up @@ -113,18 +110,22 @@ class TitusAgentLauncherPropertiesSpec extends Specification {
p.setStack("stack")
p.setDetail("detail")
p.setSequence("sequence")
p.setCommandTemplate(
[
"--server-host", TitusAgentLauncherProperties.SERVER_HOST_PLACEHOLDER,
"--server-port", TitusAgentLauncherProperties.SERVER_PORT_PLACEHOLDER,
"--job-id", TitusAgentLauncherProperties.JOB_ID_PLACEHOLDER,
"--api-job",
"--launchInJobDirectory"
]
)

then:
p.isEnabled()
p.getEndpoint() == URI.create("https://test-titus-endpoint.tld:4321")
p.getEntryPointTemplate() == [
"/usr/local/bin/genie-agent",
"exec",
"--server-host", TitusAgentLauncherProperties.SERVER_HOST_PLACEHOLDER,
"--server-port", TitusAgentLauncherProperties.SERVER_PORT_PLACEHOLDER,
"--job-id", TitusAgentLauncherProperties.JOB_ID_PLACEHOLDER,
"--api-job",
"--launchInJobDirectory",
"exec"
]
p.getOwnerEmail() == "genie@genie.tld"
p.getApplicationName() == "genie-foo"
Expand Down Expand Up @@ -157,5 +158,12 @@ class TitusAgentLauncherPropertiesSpec extends Specification {
p.getStack() == "stack"
p.getDetail() == "detail"
p.getSequence() == "sequence"
p.getCommandTemplate() == [
"--server-host", TitusAgentLauncherProperties.SERVER_HOST_PLACEHOLDER,
"--server-port", TitusAgentLauncherProperties.SERVER_PORT_PLACEHOLDER,
"--job-id", TitusAgentLauncherProperties.JOB_ID_PLACEHOLDER,
"--api-job",
"--launchInJobDirectory"
]
}
}

0 comments on commit 1ed012e

Please sign in to comment.