mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(emr-serverless): report a Spark job run's total and billed resource utilization (#9187)
GetJobRun gave an ended run no totalResourceUtilization or billedResourceUtilization, which a cost or usage report reads. - An ended Spark run gives both, reckoned from the workers it asks for: its driver and the executors it starts with, sized as the user guide's supported worker configurations size them. A worker has the vCPUs its cores ask for, and memory for its Spark memory and its 10% overhead, at least 384 MB, rounded up to the sizes its vCPUs allow. Unset, a worker has 4 vCPUs, 14G and 20G of disk, and a run starts 3 executors, as the Spark job properties give them; spark-submit's sizing options set over --conf, as Spark takes them. Sizes are read as the executors' capacity reads them, which a size too large for a long no longer breaks. - The total covers the time "from the time the job starts to execute, until the time the job terminates, rounded up to the nearest second"; the billed figure includes "a 1-minute minimum usage for workers, plus additional storage over 20 GB per worker". - Its driver's worker counts for the whole attempt. Executors with containers of their own count for the time each container ran, as they come and go with Spark's target; otherwise the executors it starts with count for the whole attempt. - Each attempt gives its own, and one that never started used nothing. A Hive run gives none, its Tez tasks coming and going with its query. A Spark run stored before this gets its workers as it ends.
This commit is contained in:
1 parent
8bc759420f
commit
a1f3379198
13 files changed
+613
-21
No files matched your search
@@ -26,6 +26,7 @@ import io.floci.aws.services.emrserverless.model.JobRunAttemptSummary;
|
||||
import io.floci.aws.services.emrserverless.model.JobRunRecord;
|
||||
import io.floci.aws.services.emrserverless.model.JobRunSummary;
|
||||
import io.floci.aws.services.emrserverless.model.MaximumCapacity;
|
||||
import io.floci.aws.services.emrserverless.model.RunWorkers;
|
||||
import io.floci.aws.services.emrserverless.model.RuntimeConfiguration;
|
||||
import io.floci.aws.services.emrserverless.model.Session;
|
||||
import io.floci.aws.services.emrserverless.model.StartJobRunRequest;
|
||||
@@ -676,6 +677,7 @@ public class EmrServerlessJobRunService implements Resettable {
|
||||
record.setClientToken(request.getClientToken());
|
||||
record.setRequest(original);
|
||||
record.setApplicationType(app.getType());
|
||||
record.setWorkers(workers(app, run));
|
||||
record.setContainerRun(sparkRunner != null);
|
||||
keepDriverLogs(record, app);
|
||||
store.put(jobRunId, record);
|
||||
@@ -1223,6 +1225,17 @@ public class EmrServerlessJobRunService implements Resettable {
|
||||
});
|
||||
}
|
||||
|
||||
// How long each of a run's executors' workers ran, for its attempt's resource utilization.
|
||||
private synchronized void keepExecutorLifetimes(String jobRunId, long launchedIn, List<Duration> lifetimes) {
|
||||
if (launchedIn != generation) {
|
||||
return;
|
||||
}
|
||||
store.get(jobRunId).ifPresent(record -> {
|
||||
record.setExecutorLifetimes(lifetimes.stream().map(Duration::toMillis).toList());
|
||||
store.put(jobRunId, record);
|
||||
});
|
||||
}
|
||||
|
||||
// A run past its timeout is CANCELLING while its driver is stopped: "the job run has timed out".
|
||||
private synchronized void driverTimedOut(String jobRunId, long launchedIn) {
|
||||
if (launchedIn != generation) {
|
||||
@@ -1318,7 +1331,8 @@ public class EmrServerlessJobRunService implements Resettable {
|
||||
JobRun run = record.getJobRun();
|
||||
JobRun failed = objectMapper.convertValue(run, JobRun.class);
|
||||
failed.setStateDetails(details);
|
||||
end(failed, STATE_FAILED, now);
|
||||
end(record, failed, STATE_FAILED, now);
|
||||
record.setExecutorLifetimes(null);
|
||||
record.getEarlierAttempts().add(failed);
|
||||
run.setAttempt(run.getAttempt() + 1);
|
||||
run.setAttemptCreatedAt(now);
|
||||
@@ -1329,6 +1343,8 @@ public class EmrServerlessJobRunService implements Resettable {
|
||||
run.setStartedAt(null);
|
||||
run.setEndedAt(null);
|
||||
run.setTotalExecutionDurationSeconds(null);
|
||||
run.setTotalResourceUtilization(null);
|
||||
run.setBilledResourceUtilization(null);
|
||||
applyImages(run, app);
|
||||
keepDriverLogs(record, app);
|
||||
store.put(run.getJobRunId(), record);
|
||||
@@ -1337,6 +1353,16 @@ public class EmrServerlessJobRunService implements Resettable {
|
||||
launch(record, app);
|
||||
}
|
||||
|
||||
/**
|
||||
* The workers a Spark run asks for ({@link EmrServerlessWorkers}), from its properties as {@code spark-submit} gets
|
||||
* them; none for a Hive run, whose Tez tasks come and go with its query.
|
||||
*/
|
||||
private static RunWorkers workers(Application app, JobRun run) {
|
||||
JobDriver.SparkSubmit sparkSubmit = run.getJobDriver() != null ? run.getJobDriver().getSparkSubmit() : null;
|
||||
return sparkSubmit != null ? EmrServerlessWorkers.plan(sparkProperties(sparkDefaults(app, run), sparkSubmit),
|
||||
sparkSubmit.getSparkSubmitParameters()) : null;
|
||||
}
|
||||
|
||||
/** The {@code spark-defaults} properties for a run, as {@link #classificationProperties} merges them. */
|
||||
private static Map<String, String> sparkDefaults(Application app, JobRun run) {
|
||||
return classificationProperties(app, run, SPARK_DEFAULTS);
|
||||
@@ -1507,7 +1533,7 @@ public class EmrServerlessJobRunService implements Resettable {
|
||||
queued.add(record);
|
||||
} else if (record.isContainerRun() && !executing.contains(run.getJobRunId())) {
|
||||
run.setStateDetails(INTERRUPTED);
|
||||
end(run, STATE_FAILED, now);
|
||||
end(record, run, STATE_FAILED, now);
|
||||
replay.changed.add(record);
|
||||
} else {
|
||||
replay.running(record);
|
||||
@@ -1606,11 +1632,11 @@ public class EmrServerlessJobRunService implements Resettable {
|
||||
// No source gives AWS's stateDetails for it; the wording is Floci's.
|
||||
run.setStateDetails("Job run timed out after " + limits.queueTimeout().toMinutes()
|
||||
+ " minutes in the queue.");
|
||||
end(run, STATE_CANCELLED, timesOutAt);
|
||||
end(head, run, STATE_CANCELLED, timesOutAt);
|
||||
changed.add(head);
|
||||
} else if (endsAt != null) {
|
||||
timed.poll();
|
||||
end(first.record().getJobRun(), first.ending().state(), endsAt);
|
||||
end(first.record(), first.record().getJobRun(), first.ending().state(), endsAt);
|
||||
changed.add(first.record());
|
||||
startQueued(endsAt);
|
||||
} else if (!startQueued(now)) {
|
||||
@@ -1643,7 +1669,7 @@ public class EmrServerlessJobRunService implements Resettable {
|
||||
running(record);
|
||||
} else if (!driverIsTheApplications(run, app)) {
|
||||
run.setStateDetails(DRIVER_NOT_THE_APPLICATIONS);
|
||||
end(run, STATE_FAILED, at);
|
||||
end(record, run, STATE_FAILED, at);
|
||||
} else {
|
||||
run.setState(STATE_SCHEDULED);
|
||||
launched.add(record);
|
||||
@@ -1701,11 +1727,12 @@ public class EmrServerlessJobRunService implements Resettable {
|
||||
}
|
||||
|
||||
private void finish(JobRunRecord record, String state, Instant endedAt) {
|
||||
end(record.getJobRun(), state, endedAt);
|
||||
end(record, record.getJobRun(), state, endedAt);
|
||||
store.put(record.getJobRun().getJobRunId(), record);
|
||||
}
|
||||
|
||||
private static void end(JobRun run, String state, Instant endedAt) {
|
||||
// Ends an attempt of a run, as of endedAt, with what its workers used.
|
||||
private void end(JobRunRecord record, JobRun run, String state, Instant endedAt) {
|
||||
run.setState(state);
|
||||
run.setEndedAt(endedAt);
|
||||
run.setUpdatedAt(endedAt);
|
||||
@@ -1713,6 +1740,19 @@ public class EmrServerlessJobRunService implements Resettable {
|
||||
// A container run cancelled or failed before its driver ran never started.
|
||||
run.setTotalExecutionDurationSeconds(run.getStartedAt() != null
|
||||
? (int) Duration.between(run.getStartedAt(), endedAt).toSeconds() : 0);
|
||||
EmrServerlessWorkers.account(run, workersOf(record), record.getExecutorLifetimes());
|
||||
}
|
||||
|
||||
/**
|
||||
* The workers a run asked for: as it was admitted, or, for a Spark run stored before Floci kept them, from its
|
||||
* application and request now, while its application is live.
|
||||
*/
|
||||
private RunWorkers workersOf(JobRunRecord record) {
|
||||
JobRun run = record.getJobRun();
|
||||
if (record.getWorkers() == null && run.getJobDriver() != null && run.getJobDriver().getSparkSubmit() != null) {
|
||||
applications.findLive(run.getApplicationId()).ifPresent(app -> record.setWorkers(workers(app, run)));
|
||||
}
|
||||
return record.getWorkers();
|
||||
}
|
||||
|
||||
// Every attempt of a run, oldest first: the earlier ones, then the latest.
|
||||
@@ -1945,5 +1985,11 @@ public class EmrServerlessJobRunService implements Resettable {
|
||||
return !RequestScopes.callAs(job.accountId(), job.region(),
|
||||
() -> stillGoing(job.runId(), launchedIn));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void executorsRan(List<Duration> lifetimes) {
|
||||
RequestScopes.runAs(job.accountId(), job.region(),
|
||||
() -> keepExecutorLifetimes(job.runId(), launchedIn, lifetimes));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -725,6 +725,7 @@ public class EmrServerlessSparkRunner implements SparkJobRunner, ContainerTeardo
|
||||
// application's capacity with them.
|
||||
if (executors != null) {
|
||||
executors.removeAll();
|
||||
progress.executorsRan(executors.lifetimes());
|
||||
}
|
||||
if (capacity != null) {
|
||||
leaveCapacity(job.executors().application(), capacity);
|
||||
|
||||
@@ -0,0 +1,193 @@
|
||||
package io.floci.aws.services.emrserverless;
|
||||
|
||||
import io.floci.aws.services.emrserverless.model.JobRun;
|
||||
import io.floci.aws.services.emrserverless.model.RunWorkers;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* The workers a Spark job run asks for, sized as EMR Serverless sizes them, and the resources they use while it runs
|
||||
* (EMR Serverless User Guide, "Supported worker configurations" and "Spark job properties").
|
||||
*
|
||||
* <p>A worker has the vCPUs its {@code cores} ask for, "1, 2, 4, 8, 16, or 32 vCPUs", and memory for its Spark memory
|
||||
* and that memory's overhead, "10% of container memory, with a minimum of 384 MB", rounded up to the sizes its vCPUs
|
||||
* allow, read as {@link SparkExecutors} reads them for an application's maximum capacity. Its disk is 20 GB to
|
||||
* 200 GB. Unset, the cores are 4, the memory "For workers smaller than 32 vCPUs, 14G" and "For 32 vCPUs, 109G", the
|
||||
* disk 20G, and the executors 3. With dynamic allocation on, as it is by default, a run starts with
|
||||
* {@code spark.dynamicAllocation.initialExecutors}, or {@code spark.executor.instances} where that is larger, as
|
||||
* Spark's own configuration reads, and no fewer than {@code minExecutors}.
|
||||
*/
|
||||
final class EmrServerlessWorkers {
|
||||
|
||||
private static final List<Integer> VCPUS = List.of(1, 2, 4, 8, 16, 32);
|
||||
// The memory sizes of a 32-vCPU worker: "Three discrete values: 60 GB, 120 GB, or 244 GB".
|
||||
private static final List<Long> LARGEST_WORKER_MEMORY = List.of(60L, 120L, 244L);
|
||||
private static final int DEFAULT_CORES = 4;
|
||||
private static final long DEFAULT_MEMORY_MIB = 14 * 1024;
|
||||
private static final long LARGEST_WORKER_DEFAULT_MEMORY_MIB = 109 * 1024;
|
||||
private static final int DEFAULT_EXECUTORS = 3;
|
||||
private static final long MIN_DISK_GB = 20;
|
||||
private static final long MAX_DISK_GB = 200;
|
||||
// "You only pay for additional storage beyond 20 GB that you configure per worker."
|
||||
private static final long INCLUDED_DISK_GB = 20;
|
||||
// "The billed resources include a 1-minute minimum usage for workers" (the API reference's JobRun).
|
||||
private static final long MINIMUM_BILLED_MILLIS = 60_000;
|
||||
private static final double SECONDS_PER_HOUR = 3600;
|
||||
/**
|
||||
* spark-submit's options that size a run, and the property each sets, which "--conf" cannot override: Spark's
|
||||
* option parser takes them over a property of the same setting.
|
||||
*/
|
||||
private static final Map<String, String> SIZE_OPTIONS = Map.of("--driver-cores", "spark.driver.cores",
|
||||
"--driver-memory", "spark.driver.memory", "--executor-cores", "spark.executor.cores",
|
||||
"--executor-memory", "spark.executor.memory", "--num-executors", "spark.executor.instances");
|
||||
|
||||
private EmrServerlessWorkers() {
|
||||
}
|
||||
|
||||
/**
|
||||
* The workers of a Spark run whose properties, its {@code spark-defaults} and {@code --conf} settings, these are,
|
||||
* with what its {@code sparkSubmitParameters}' sizing options set over them.
|
||||
*/
|
||||
static RunWorkers plan(Map<String, String> properties, String sparkSubmitParameters) {
|
||||
Map<String, String> settings = new HashMap<>(properties);
|
||||
List<String> parameters = EmrServerlessSparkRunner.splitParameters(sparkSubmitParameters);
|
||||
for (int i = 0; i < parameters.size(); i++) {
|
||||
String parameter = parameters.get(i);
|
||||
int equals = parameter.indexOf('=');
|
||||
String option = equals > 0 ? parameter.substring(0, equals) : parameter;
|
||||
String property = SIZE_OPTIONS.get(option);
|
||||
if (property != null && equals > 0) {
|
||||
settings.put(property, parameter.substring(equals + 1));
|
||||
} else if (property != null && i + 1 < parameters.size()) {
|
||||
settings.put(property, parameters.get(++i));
|
||||
}
|
||||
}
|
||||
int driverVcpu = vcpus(settings.get("spark.driver.cores"));
|
||||
int executorVcpu = vcpus(settings.get("spark.executor.cores"));
|
||||
return new RunWorkers(driverVcpu, memoryGb(settings, "spark.driver", driverVcpu),
|
||||
diskGb(settings.get("spark.emr-serverless.driver.disk")), executorVcpu,
|
||||
memoryGb(settings, "spark.executor", executorVcpu),
|
||||
diskGb(settings.get("spark.emr-serverless.executor.disk")), executors(settings));
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets an ended attempt's resource utilization from its {@code workers}: "The aggregate vCPU, memory, and storage
|
||||
* resources used from the time the job starts to execute, until the time the job terminates, rounded up to the
|
||||
* nearest second", and what was billed for them, which includes "a 1-minute minimum usage for workers, plus
|
||||
* additional storage over 20 GB per worker". Its driver's worker runs for the whole attempt. Its executors' run
|
||||
* for {@code executorLifetimes}, each worker's from its start to its end, where its executors had containers that
|
||||
* came and went with Spark's target; else the executors it starts with run for the whole attempt. An attempt
|
||||
* that never started used none. Nothing is set without workers, as for a Hive run.
|
||||
*/
|
||||
static void account(JobRun run, RunWorkers workers, List<Long> executorLifetimes) {
|
||||
if (workers == null) {
|
||||
return;
|
||||
}
|
||||
boolean started = run.getStartedAt() != null && run.getEndedAt() != null;
|
||||
long millis = started ? Duration.between(run.getStartedAt(), run.getEndedAt()).toMillis() : 0;
|
||||
double driverSeconds = seconds(millis);
|
||||
double driverBilled = started ? seconds(Math.max(millis, MINIMUM_BILLED_MILLIS)) : 0;
|
||||
double executorSeconds;
|
||||
double executorBilled;
|
||||
if (executorLifetimes != null) {
|
||||
executorSeconds = seconds(executorLifetimes.stream().mapToLong(Long::longValue).sum());
|
||||
executorBilled = seconds(executorLifetimes.stream()
|
||||
.mapToLong(lifetime -> Math.max(lifetime, MINIMUM_BILLED_MILLIS)).sum());
|
||||
} else {
|
||||
executorSeconds = (double) workers.getExecutors() * driverSeconds;
|
||||
executorBilled = (double) workers.getExecutors() * driverBilled;
|
||||
}
|
||||
run.setTotalResourceUtilization(utilization(workers, driverSeconds, executorSeconds,
|
||||
workers.getDriverDiskGb(), workers.getExecutorDiskGb()));
|
||||
run.setBilledResourceUtilization(utilization(workers, driverBilled, executorBilled,
|
||||
Math.max(0, workers.getDriverDiskGb() - INCLUDED_DISK_GB),
|
||||
Math.max(0, workers.getExecutorDiskGb() - INCLUDED_DISK_GB)));
|
||||
}
|
||||
|
||||
// Milliseconds rounded up to whole seconds.
|
||||
private static double seconds(long millis) {
|
||||
return Math.ceilDiv(millis, 1000);
|
||||
}
|
||||
|
||||
private static Map<String, Double> utilization(RunWorkers workers, double driverSeconds, double executorSeconds,
|
||||
long driverDiskGb, long executorDiskGb) {
|
||||
Map<String, Double> utilization = new LinkedHashMap<>();
|
||||
utilization.put("vCPUHour", hours(workers.getDriverVcpu(), driverSeconds, workers.getExecutorVcpu(),
|
||||
executorSeconds));
|
||||
utilization.put("memoryGBHour", hours(workers.getDriverMemoryGb(), driverSeconds,
|
||||
workers.getExecutorMemoryGb(), executorSeconds));
|
||||
utilization.put("storageGBHour", hours(driverDiskGb, driverSeconds, executorDiskGb, executorSeconds));
|
||||
return utilization;
|
||||
}
|
||||
|
||||
private static double hours(long driverAmount, double driverSeconds, long executorAmount, double executorSeconds) {
|
||||
return (driverAmount * driverSeconds + executorAmount * executorSeconds) / SECONDS_PER_HOUR;
|
||||
}
|
||||
|
||||
// The fewest vCPUs a worker can have for its cores, at most 32.
|
||||
private static int vcpus(String cores) {
|
||||
int asked = whole(cores, DEFAULT_CORES);
|
||||
return VCPUS.stream().filter(vcpu -> vcpu >= asked).findFirst().orElse(VCPUS.getLast());
|
||||
}
|
||||
|
||||
/**
|
||||
* A worker's memory in GB: its Spark memory and that memory's overhead, rounded up to the sizes its vCPUs allow
|
||||
* ("Minimum 8 GB, maximum 30 GB, in 1 GB increments" for 4 vCPUs, say).
|
||||
*/
|
||||
private static int memoryGb(Map<String, String> settings, String role, int vcpu) {
|
||||
long heap = SparkExecutors.mebibytes(settings.get(role + ".memory"),
|
||||
vcpu == VCPUS.getLast() ? LARGEST_WORKER_DEFAULT_MEMORY_MIB : DEFAULT_MEMORY_MIB);
|
||||
long needed = SparkExecutors.workerGb(heap, settings, role);
|
||||
if (vcpu == VCPUS.getLast()) {
|
||||
return LARGEST_WORKER_MEMORY.stream().filter(size -> size >= needed).findFirst()
|
||||
.orElse(LARGEST_WORKER_MEMORY.getLast()).intValue();
|
||||
}
|
||||
long min = 2L * vcpu;
|
||||
long max = switch (vcpu) {
|
||||
case 1 -> 8;
|
||||
case 2 -> 16;
|
||||
case 4 -> 30;
|
||||
case 8 -> 60;
|
||||
default -> 120;
|
||||
};
|
||||
long step = vcpu <= 4 ? 1 : vcpu / 2;
|
||||
long steps = Math.ceilDiv(Math.max(0, Math.min(needed, max) - min), step);
|
||||
return (int) Math.min(max, min + steps * step);
|
||||
}
|
||||
|
||||
// A worker's disk in GB, 20 to 200.
|
||||
private static int diskGb(String disk) {
|
||||
return (int) Math.max(MIN_DISK_GB, Math.min(MAX_DISK_GB, SparkExecutors.gigabytes(disk)));
|
||||
}
|
||||
|
||||
/**
|
||||
* The executors a run starts with: {@code spark.executor.instances} without dynamic allocation; with it, its
|
||||
* {@code initialExecutors}, "If --num-executors (or spark.executor.instances) is set and larger than this value,
|
||||
* it will be used as the initial number of executors" (Spark's configuration), no fewer than {@code minExecutors}
|
||||
* and no more than {@code maxExecutors}.
|
||||
*/
|
||||
private static int executors(Map<String, String> settings) {
|
||||
int instances = whole(settings.get("spark.executor.instances"), DEFAULT_EXECUTORS);
|
||||
if ("false".equalsIgnoreCase(settings.get("spark.dynamicAllocation.enabled"))) {
|
||||
return instances;
|
||||
}
|
||||
int initial = Math.max(whole(settings.get("spark.dynamicAllocation.initialExecutors"), DEFAULT_EXECUTORS),
|
||||
whole(settings.get("spark.dynamicAllocation.minExecutors"), 0));
|
||||
if (settings.containsKey("spark.executor.instances")) {
|
||||
initial = Math.max(initial, instances);
|
||||
}
|
||||
return Math.min(initial, whole(settings.get("spark.dynamicAllocation.maxExecutors"), initial));
|
||||
}
|
||||
|
||||
private static int whole(String value, int fallback) {
|
||||
try {
|
||||
return value != null ? Math.max(0, Integer.parseInt(value.trim())) : fallback;
|
||||
} catch (NumberFormatException e) {
|
||||
return fallback;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -211,6 +211,9 @@ final class SparkExecutors {
|
||||
private final List<Worker> workers = new ArrayList<>();
|
||||
// Containers that ended or were removed while Docker would not remove them, to try again at the run's end.
|
||||
private final List<String> leftovers = new ArrayList<>();
|
||||
// When each running executor's worker started, by its container, and how long each that has ended ran.
|
||||
private final Map<String, Long> started = new HashMap<>();
|
||||
private final List<Duration> lifetimes = new ArrayList<>();
|
||||
private int nextIndex = 1;
|
||||
private boolean looked;
|
||||
private long nextLook;
|
||||
@@ -251,6 +254,7 @@ final class SparkExecutors {
|
||||
if (!containers.running(worker.containerId())) {
|
||||
each.remove();
|
||||
capacity.release(size.executor());
|
||||
ended(worker.containerId());
|
||||
remove(worker.containerId());
|
||||
}
|
||||
}
|
||||
@@ -275,6 +279,7 @@ final class SparkExecutors {
|
||||
throw e;
|
||||
}
|
||||
workers.add(worker);
|
||||
started.put(worker.containerId(), nanoTime.getAsLong());
|
||||
}
|
||||
if (workers.size() > wanted) {
|
||||
removeIdle(workers.size() - wanted);
|
||||
@@ -284,10 +289,27 @@ final class SparkExecutors {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* How long each executor's worker ran, from its start to its removal, for those that have ended: every one, once
|
||||
* {@link #removeAll} has run.
|
||||
*/
|
||||
List<Duration> lifetimes() {
|
||||
return List.copyOf(lifetimes);
|
||||
}
|
||||
|
||||
// An executor's worker has ended: how long it ran is kept.
|
||||
private void ended(String containerId) {
|
||||
Long since = started.remove(containerId);
|
||||
if (since != null) {
|
||||
lifetimes.add(Duration.ofNanos(Math.max(0, nanoTime.getAsLong() - since)));
|
||||
}
|
||||
}
|
||||
|
||||
/** Removes every executor's container, and gives back what its workers held, as the run has ended. */
|
||||
void removeAll() {
|
||||
for (Worker worker : workers) {
|
||||
capacity.release(size.executor());
|
||||
ended(worker.containerId());
|
||||
remove(worker.containerId());
|
||||
}
|
||||
workers.clear();
|
||||
@@ -376,12 +398,12 @@ final class SparkExecutors {
|
||||
// 1 GB. The overhead is the role's memoryOverhead when set, else its share of the heap, by the role's
|
||||
// memoryOverheadFactor, else EMR Serverless's, else 10%, and at least 384 MB, as "The overhead has a default
|
||||
// value of 10% of container memory, with a minimum of 384 MB".
|
||||
private static long workerGb(long heapMib, Map<String, String> settings, String role) {
|
||||
static long workerGb(long heapMib, Map<String, String> settings, String role) {
|
||||
String overhead = settings.get(role + ".memoryOverhead");
|
||||
String factor = settings.getOrDefault(role + ".memoryOverheadFactor", settings.get(EMR_OVERHEAD_FACTOR));
|
||||
long overheadMib = overhead != null ? mebibytes(overhead, MINIMUM_OVERHEAD_MIB)
|
||||
: Math.max(MINIMUM_OVERHEAD_MIB, (long) (heapMib * fraction(factor)));
|
||||
return (heapMib + overheadMib + 1023) / 1024;
|
||||
return Math.ceilDiv(heapMib > Long.MAX_VALUE - overheadMib ? Long.MAX_VALUE : heapMib + overheadMib, 1024);
|
||||
}
|
||||
|
||||
private static double fraction(String value) {
|
||||
@@ -432,6 +454,7 @@ final class SparkExecutors {
|
||||
Worker worker = workers.get(i);
|
||||
if (Integer.valueOf(0).equals(coresUsed.get(worker.address()))) {
|
||||
containers.remove(worker.containerId());
|
||||
ended(worker.containerId());
|
||||
workers.remove(i);
|
||||
capacity.release(size.executor());
|
||||
count--;
|
||||
@@ -458,24 +481,32 @@ final class SparkExecutors {
|
||||
}
|
||||
}
|
||||
|
||||
// A size as Spark takes it, "14G" or "1024m", in MiB, a bare number being MiB, as spark.executor.memory has it.
|
||||
/**
|
||||
* A size as Spark takes it, "14G" or "1024m", in MiB, a bare number being MiB, as spark.executor.memory has it;
|
||||
* {@code otherwise} for one that is not a size, or too large for a long.
|
||||
*/
|
||||
static long mebibytes(String value, long otherwise) {
|
||||
Matcher size = value != null ? SIZE.matcher(value.trim()) : null;
|
||||
if (size == null || !size.matches()) {
|
||||
return otherwise;
|
||||
}
|
||||
long amount = Long.parseLong(size.group(1));
|
||||
return switch (size.group(2).toLowerCase(Locale.ROOT)) {
|
||||
case "k" -> Math.max(1, amount / 1024);
|
||||
case "g" -> amount * 1024;
|
||||
case "t" -> amount * 1024 * 1024;
|
||||
case "p" -> amount * 1024 * 1024 * 1024;
|
||||
default -> amount;
|
||||
};
|
||||
try {
|
||||
long amount = Long.parseLong(size.group(1));
|
||||
return switch (size.group(2).toLowerCase(Locale.ROOT)) {
|
||||
case "k" -> Math.max(1, amount / 1024);
|
||||
case "g" -> Math.multiplyExact(amount, 1024L);
|
||||
case "t" -> Math.multiplyExact(amount, 1024L * 1024);
|
||||
case "p" -> Math.multiplyExact(amount, 1024L * 1024 * 1024);
|
||||
default -> amount;
|
||||
};
|
||||
} catch (NumberFormatException | ArithmeticException e) {
|
||||
return otherwise;
|
||||
}
|
||||
}
|
||||
|
||||
// A disk size in GB, as "20G" gives it, EMR Serverless's 20 GB when not set.
|
||||
static long gigabytes(String value) {
|
||||
return value != null ? Math.max(1, (mebibytes(value, DEFAULT_DISK_GB * 1024) + 1023) / 1024) : DEFAULT_DISK_GB;
|
||||
return value != null ? Math.max(1, Math.ceilDiv(mebibytes(value, DEFAULT_DISK_GB * 1024), 1024))
|
||||
: DEFAULT_DISK_GB;
|
||||
}
|
||||
}
|
||||
@@ -96,6 +96,13 @@ public interface SparkJobRunner {
|
||||
* that runs is stopped.
|
||||
*/
|
||||
boolean cancelled();
|
||||
|
||||
/**
|
||||
* How long each of the run's executors' workers ran, from its start to its end, where its executors had
|
||||
* containers of their own, told as the run ends.
|
||||
*/
|
||||
default void executorsRan(List<Duration> lifetimes) {
|
||||
}
|
||||
}
|
||||
|
||||
/** What a run's container runs as: its driver, or one of its executors. */
|
||||
|
||||
@@ -35,6 +35,9 @@ public class JobRun {
|
||||
private Map<String, Object> retryPolicy;
|
||||
// StartJobRun's flexExecution as sent; null where it gave none.
|
||||
private Map<String, Object> flexExecution;
|
||||
// The ended attempt's TotalResourceUtilization and billed ResourceUtilization; null until it ends.
|
||||
private Map<String, Double> totalResourceUtilization;
|
||||
private Map<String, Double> billedResourceUtilization;
|
||||
private Integer attempt;
|
||||
@JsonFormat(shape = JsonFormat.Shape.NUMBER)
|
||||
private Instant attemptCreatedAt;
|
||||
@@ -92,6 +95,16 @@ public class JobRun {
|
||||
|
||||
public Map<String, Object> getFlexExecution() { return flexExecution; }
|
||||
public void setFlexExecution(Map<String, Object> flexExecution) { this.flexExecution = flexExecution; }
|
||||
|
||||
public Map<String, Double> getTotalResourceUtilization() { return totalResourceUtilization; }
|
||||
public void setTotalResourceUtilization(Map<String, Double> totalResourceUtilization) {
|
||||
this.totalResourceUtilization = totalResourceUtilization;
|
||||
}
|
||||
|
||||
public Map<String, Double> getBilledResourceUtilization() { return billedResourceUtilization; }
|
||||
public void setBilledResourceUtilization(Map<String, Double> billedResourceUtilization) {
|
||||
this.billedResourceUtilization = billedResourceUtilization;
|
||||
}
|
||||
public Integer getAttempt() { return attempt; }
|
||||
public void setAttempt(Integer attempt) { this.attempt = attempt; }
|
||||
public Instant getAttemptCreatedAt() { return attemptCreatedAt; }
|
||||
|
||||
@@ -16,7 +16,8 @@ import java.util.Map;
|
||||
* mode runs, whose state its container decides. A run retried after a failed attempt keeps each earlier
|
||||
* attempt as GetJobRun reports it for that attempt, oldest first; {@code jobRun} is the latest. Each attempt's
|
||||
* driver log destinations are kept as it was admitted, by attempt number. A container run cancelled with a grace
|
||||
* period keeps when that period ends, until which its driver may run on to shut down by itself.
|
||||
* period keeps when that period ends, until which its driver may run on to shut down by itself. A Spark run keeps
|
||||
* the workers it asked for as it was admitted, which each attempt's resource utilization is reckoned from.
|
||||
*/
|
||||
@RegisterForReflection
|
||||
public class JobRunRecord {
|
||||
@@ -30,6 +31,9 @@ public class JobRunRecord {
|
||||
private List<JobRun> earlierAttempts = new ArrayList<>();
|
||||
private Map<Integer, DriverLogDestinations> driverLogs = new LinkedHashMap<>();
|
||||
private Instant shutdownDeadline;
|
||||
private RunWorkers workers;
|
||||
// How long, in milliseconds, each executor's worker of its latest attempt ran, where they had containers.
|
||||
private List<Long> executorLifetimes;
|
||||
|
||||
public JobRun getJobRun() { return jobRun; }
|
||||
public void setJobRun(JobRun jobRun) { this.jobRun = jobRun; }
|
||||
@@ -49,4 +53,8 @@ public class JobRunRecord {
|
||||
public void setDriverLogs(Map<Integer, DriverLogDestinations> driverLogs) { this.driverLogs = driverLogs; }
|
||||
public Instant getShutdownDeadline() { return shutdownDeadline; }
|
||||
public void setShutdownDeadline(Instant shutdownDeadline) { this.shutdownDeadline = shutdownDeadline; }
|
||||
public RunWorkers getWorkers() { return workers; }
|
||||
public void setWorkers(RunWorkers workers) { this.workers = workers; }
|
||||
public List<Long> getExecutorLifetimes() { return executorLifetimes; }
|
||||
public void setExecutorLifetimes(List<Long> executorLifetimes) { this.executorLifetimes = executorLifetimes; }
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
package io.floci.aws.services.emrserverless.model;
|
||||
|
||||
import io.quarkus.runtime.annotations.RegisterForReflection;
|
||||
|
||||
/**
|
||||
* The workers a Spark job run asks for, as EMR Serverless sizes them: its driver and each of its executors, in vCPUs
|
||||
* and in GB of memory and of disk, and how many executors it starts with. A run's resource utilization is reckoned
|
||||
* from them.
|
||||
*/
|
||||
@RegisterForReflection
|
||||
public class RunWorkers {
|
||||
private int driverVcpu;
|
||||
private int driverMemoryGb;
|
||||
private int driverDiskGb;
|
||||
private int executorVcpu;
|
||||
private int executorMemoryGb;
|
||||
private int executorDiskGb;
|
||||
private int executors;
|
||||
|
||||
public RunWorkers() {}
|
||||
|
||||
public RunWorkers(int driverVcpu, int driverMemoryGb, int driverDiskGb, int executorVcpu, int executorMemoryGb,
|
||||
int executorDiskGb, int executors) {
|
||||
this.driverVcpu = driverVcpu;
|
||||
this.driverMemoryGb = driverMemoryGb;
|
||||
this.driverDiskGb = driverDiskGb;
|
||||
this.executorVcpu = executorVcpu;
|
||||
this.executorMemoryGb = executorMemoryGb;
|
||||
this.executorDiskGb = executorDiskGb;
|
||||
this.executors = executors;
|
||||
}
|
||||
|
||||
public int getDriverVcpu() { return driverVcpu; }
|
||||
public void setDriverVcpu(int driverVcpu) { this.driverVcpu = driverVcpu; }
|
||||
public int getDriverMemoryGb() { return driverMemoryGb; }
|
||||
public void setDriverMemoryGb(int driverMemoryGb) { this.driverMemoryGb = driverMemoryGb; }
|
||||
public int getDriverDiskGb() { return driverDiskGb; }
|
||||
public void setDriverDiskGb(int driverDiskGb) { this.driverDiskGb = driverDiskGb; }
|
||||
public int getExecutorVcpu() { return executorVcpu; }
|
||||
public void setExecutorVcpu(int executorVcpu) { this.executorVcpu = executorVcpu; }
|
||||
public int getExecutorMemoryGb() { return executorMemoryGb; }
|
||||
public void setExecutorMemoryGb(int executorMemoryGb) { this.executorMemoryGb = executorMemoryGb; }
|
||||
public int getExecutorDiskGb() { return executorDiskGb; }
|
||||
public void setExecutorDiskGb(int executorDiskGb) { this.executorDiskGb = executorDiskGb; }
|
||||
public int getExecutors() { return executors; }
|
||||
public void setExecutors(int executors) { this.executors = executors; }
|
||||
}
|
||||
Reference in new issue
Block a user