mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
feat(emr-serverless): send Spark driver output to CloudWatch Logs (#6703)
With CloudWatch logging enabled on a job run in the docker runner mode, its Spark driver's standard output and error now go line by line, while it runs, to Floci's CloudWatch Logs, as well as to Floci's log. - Which configuration: the run's cloudWatchLoggingConfiguration, or else the application's; the user guide merges them by configuration type, and "enabled": false at the run removes it. - Where: the log group logGroupName, /aws/emr-serverless by default, and the log streams <logStreamNamePrefix>/applications/<applicationId> /jobs/<jobRunId>/SPARK_DRIVER/stdout and .../stderr, with attempts/1/ before SPARK_DRIVER for a retried run on 7.1.0 or later, as for S3. The guide gives the default group and the stream up to its worker type; the stdout and stderr streams under it are as Dagster's EMR Serverless client reads them, and Airflow's operator builds the same prefix. Group and streams are created as needed, in the run's account and Region. - What: the driver's stdout and stderr, "By default, EMR Serverless publishes only the driver stdout and stderr logs". A logTypes that names some types sends only the driver's it names, so none of them when it names only SPARK_EXECUTOR: the API reference's logTypes is "The types of logs that you want to publish to CloudWatch". ContainerLogStreamer gets attachByStreamForAccount, which sends a container's standard output and error to log streams of their own; its reassembly callback now keeps the stream each line came from. The shared Spark runner uses it when a job carries a CloudWatch destination.
This commit is contained in:
1 parent
1520e436ae
commit
5c9eb3eb30
10 files changed
+333
-42
No files matched your search
+70
@@ -130,6 +130,61 @@ class ContainerLogStreamerTest {
|
||||
verify(cloudWatchLogsService, never()).putLogEventsForAccount(any(), any(), any(), anyList(), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void aByStreamAttachSendsEachStreamToItsOwnLogStream() {
|
||||
DockerClient dockerClient = mock(DockerClient.class);
|
||||
LogContainerCmd command = mock(LogContainerCmd.class, RETURNS_SELF);
|
||||
when(dockerClient.logContainerCmd("container-1")).thenReturn(command);
|
||||
CloudWatchLogsService cloudWatchLogsService = mock(CloudWatchLogsService.class);
|
||||
ContainerLogStreamer streamer = new ContainerLogStreamer(dockerClient, cloudWatchLogsService);
|
||||
|
||||
streamer.attachByStreamForAccount("555555555555", "container-1", "/aws/emr-serverless",
|
||||
"/jobs/1/SPARK_DRIVER/stdout", "/jobs/1/SPARK_DRIVER/stderr", "us-east-1", "emr-serverless:1");
|
||||
ArgumentCaptor<ContainerLogStreamer.LogReassemblyCallback> callback =
|
||||
ArgumentCaptor.forClass(ContainerLogStreamer.LogReassemblyCallback.class);
|
||||
verify(command).exec(callback.capture());
|
||||
|
||||
List<LogRecord> records = LogCapture.capture(ContainerLogStreamer.class, () -> {
|
||||
callback.getValue().onNext(new Frame(StreamType.STDOUT, utf8("Pi is roughly 3.14\n")));
|
||||
callback.getValue().onNext(new Frame(StreamType.STDERR, utf8("INFO SparkContext: stopped\n")));
|
||||
});
|
||||
|
||||
assertEquals(2, records.size());
|
||||
verify(cloudWatchLogsService).createLogStreamForAccount(
|
||||
"555555555555", "/aws/emr-serverless", "/jobs/1/SPARK_DRIVER/stdout", "us-east-1");
|
||||
verify(cloudWatchLogsService).createLogStreamForAccount(
|
||||
"555555555555", "/aws/emr-serverless", "/jobs/1/SPARK_DRIVER/stderr", "us-east-1");
|
||||
verify(cloudWatchLogsService).putLogEventsForAccount(eq("555555555555"), eq("/aws/emr-serverless"),
|
||||
eq("/jobs/1/SPARK_DRIVER/stdout"),
|
||||
argThat(events -> "Pi is roughly 3.14".equals(events.getFirst().get("message"))), eq("us-east-1"));
|
||||
verify(cloudWatchLogsService).putLogEventsForAccount(eq("555555555555"), eq("/aws/emr-serverless"),
|
||||
eq("/jobs/1/SPARK_DRIVER/stderr"),
|
||||
argThat(events -> "INFO SparkContext: stopped".equals(events.getFirst().get("message"))),
|
||||
eq("us-east-1"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void aByStreamAttachForwardsAStreamWithoutALogStreamOnlyToTheConsole() {
|
||||
DockerClient dockerClient = mock(DockerClient.class);
|
||||
LogContainerCmd command = mock(LogContainerCmd.class, RETURNS_SELF);
|
||||
when(dockerClient.logContainerCmd("container-1")).thenReturn(command);
|
||||
CloudWatchLogsService cloudWatchLogsService = mock(CloudWatchLogsService.class);
|
||||
ContainerLogStreamer streamer = new ContainerLogStreamer(dockerClient, cloudWatchLogsService);
|
||||
|
||||
streamer.attachByStreamForAccount("555555555555", "container-1", "/aws/emr-serverless",
|
||||
"/jobs/1/SPARK_DRIVER/stdout", null, "us-east-1", "emr-serverless:1");
|
||||
ArgumentCaptor<ContainerLogStreamer.LogReassemblyCallback> callback =
|
||||
ArgumentCaptor.forClass(ContainerLogStreamer.LogReassemblyCallback.class);
|
||||
verify(command).exec(callback.capture());
|
||||
|
||||
List<LogRecord> records = LogCapture.capture(ContainerLogStreamer.class, () ->
|
||||
callback.getValue().onNext(new Frame(StreamType.STDERR, utf8("INFO SparkContext: stopped\n"))));
|
||||
|
||||
assertEquals(1, records.size());
|
||||
verify(cloudWatchLogsService, never()).createLogStreamForAccount(any(), any(), eq(null), any());
|
||||
verify(cloudWatchLogsService, never()).putLogEventsForAccount(any(), any(), any(), anyList(), any());
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(booleans = {true, false})
|
||||
void execStreamAlwaysReachesCloudWatchAndConsoleIsOptional(boolean logToConsole) {
|
||||
@@ -511,6 +566,21 @@ class ContainerLogStreamerTest {
|
||||
assertEquals(List.of("no-newline-tail"), emitted);
|
||||
}
|
||||
|
||||
@Test
|
||||
void reassembledLinesKeepTheStreamTheyCameFrom() {
|
||||
List<String> emitted = new ArrayList<>();
|
||||
ContainerLogStreamer.LogReassemblyCallback cb = new ContainerLogStreamer.LogReassemblyCallback(
|
||||
1_000, (stream, line) -> emitted.add(stream + ": " + line));
|
||||
|
||||
cb.onNext(new Frame(StreamType.STDOUT, utf8("Pi is ")));
|
||||
cb.onNext(new Frame(StreamType.STDERR, utf8("INFO starting\n")));
|
||||
cb.onNext(new Frame(StreamType.STDOUT, utf8("roughly 3.14\n")));
|
||||
cb.onNext(new Frame(StreamType.STDERR, utf8("INFO stopp")));
|
||||
cb.onComplete();
|
||||
|
||||
assertEquals(List.of("STDERR: INFO starting", "STDOUT: Pi is roughly 3.14", "STDERR: INFO stopp"), emitted);
|
||||
}
|
||||
|
||||
@Test
|
||||
void flushesStreamTailsInTheirLastFrameArrivalOrder() {
|
||||
List<String> emitted = new ArrayList<>();
|
||||
|
||||
+1
@@ -82,6 +82,7 @@ class EmrServiceContainerStepsTest {
|
||||
assertEquals(Map.of("spark.executor.cores", "2"), job.sparkDefaults());
|
||||
assertEquals(Duration.ZERO, job.timeout());
|
||||
assertNull(job.logFolder());
|
||||
assertNull(job.cloudWatchLogs());
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
+73
@@ -264,6 +264,79 @@ class EmrServerlessJobRunServiceTest {
|
||||
assertEquals(Duration.ofMinutes(30), job.timeout());
|
||||
assertEquals(Duration.ZERO, runner.jobs.get(1).timeout());
|
||||
assertNull(job.logFolder());
|
||||
assertNull(job.cloudWatchLogs());
|
||||
}
|
||||
|
||||
@Test
|
||||
void aRunsDriverOutputGoesToCloudWatchWhenItsOwnOrTheApplicationsLoggingIsEnabled() {
|
||||
FakeRunner runner = new FakeRunner();
|
||||
EmrServerlessJobRunService service = dockerService(runner);
|
||||
CreateApplicationRequest create = new CreateApplicationRequest();
|
||||
create.setName("watched");
|
||||
create.setReleaseLabel("emr-7.5.0");
|
||||
create.setType("SPARK");
|
||||
create.setClientToken(UUID.randomUUID().toString());
|
||||
create.setMonitoringConfiguration(cloudWatchLogging(Map.of("enabled", true)));
|
||||
String watched = applications.createApplication(create).getApplicationId();
|
||||
StartJobRunRequest own = request(null, null);
|
||||
own.setConfigurationOverrides(Map.of("monitoringConfiguration", Map.of("cloudWatchLoggingConfiguration",
|
||||
Map.of("enabled", true, "logGroupName", "spark-jobs", "logStreamNamePrefix", "nightly",
|
||||
"logTypes", Map.of("SPARK_DRIVER", List.of("STDERR"))))));
|
||||
// "To remove cloudWatchLogging, pass the enabled flag as false."
|
||||
StartJobRunRequest removed = request(null, null);
|
||||
removed.setConfigurationOverrides(Map.of("monitoringConfiguration",
|
||||
Map.of("cloudWatchLoggingConfiguration", Map.of("enabled", false))));
|
||||
|
||||
String inherited = service.startJobRun(watched, request(null, null)).getJobRunId();
|
||||
String overridden = service.startJobRun(watched, own).getJobRunId();
|
||||
service.startJobRun(watched, removed);
|
||||
for (int i = 0; i < 3; i++) {
|
||||
runNext();
|
||||
}
|
||||
|
||||
String driver = "/applications/" + watched + "/jobs/" + inherited + "/SPARK_DRIVER/";
|
||||
assertEquals(new SparkJobRunner.CloudWatchLogs("/aws/emr-serverless", driver + "stdout", driver + "stderr"),
|
||||
runner.jobs.get(0).cloudWatchLogs());
|
||||
assertEquals(new SparkJobRunner.CloudWatchLogs("spark-jobs", null,
|
||||
"nightly/applications/" + watched + "/jobs/" + overridden + "/SPARK_DRIVER/stderr"),
|
||||
runner.jobs.get(1).cloudWatchLogs());
|
||||
assertNull(runner.jobs.get(2).cloudWatchLogs());
|
||||
}
|
||||
|
||||
@Test
|
||||
void aRetriedRunsCloudWatchLogStreamsAreUnderItsAttempt() {
|
||||
Application app = new Application();
|
||||
app.setMonitoringConfiguration(cloudWatchLogging(Map.of("enabled", true)));
|
||||
|
||||
assertEquals("/applications/app/jobs/run/attempts/1/SPARK_DRIVER/stdout",
|
||||
EmrServerlessJobRunService.driverCloudWatchLogs(app, run("emr-7.1.0", "STREAMING", null))
|
||||
.stdoutStream());
|
||||
assertEquals("/applications/app/jobs/run/SPARK_DRIVER/stdout",
|
||||
EmrServerlessJobRunService.driverCloudWatchLogs(app, run("emr-7.0.0", "STREAMING", null))
|
||||
.stdoutStream());
|
||||
}
|
||||
|
||||
@Test
|
||||
void logTypesThatNameOnlyTheExecutorsSendNoneOfTheDriversOutput() {
|
||||
Application app = new Application();
|
||||
// "If you don't specify any log types, driver STDOUT and STDERR logs will be published to CloudWatch Logs
|
||||
// by default."
|
||||
app.setMonitoringConfiguration(cloudWatchLogging(Map.of("enabled", true,
|
||||
"logTypes", Map.of("SPARK_EXECUTOR", List.of("STDOUT", "STDERR")))));
|
||||
|
||||
assertNull(EmrServerlessJobRunService.driverCloudWatchLogs(app, run("emr-7.5.0", "BATCH", null)));
|
||||
|
||||
app.setMonitoringConfiguration(cloudWatchLogging(Map.of("enabled", true, "logTypes", Map.of())));
|
||||
|
||||
assertEquals(new SparkJobRunner.CloudWatchLogs("/aws/emr-serverless",
|
||||
"/applications/app/jobs/run/SPARK_DRIVER/stdout", "/applications/app/jobs/run/SPARK_DRIVER/stderr"),
|
||||
EmrServerlessJobRunService.driverCloudWatchLogs(app, run("emr-7.5.0", "BATCH", null)));
|
||||
}
|
||||
|
||||
private static MonitoringConfiguration cloudWatchLogging(Map<String, Object> configuration) {
|
||||
MonitoringConfiguration monitoring = new MonitoringConfiguration();
|
||||
monitoring.setProperty("cloudWatchLoggingConfiguration", configuration);
|
||||
return monitoring;
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
+33
-6
@@ -60,6 +60,7 @@ class EmrServerlessSparkRunnerTest {
|
||||
private final DockerClient docker = mock(DockerClient.class, RETURNS_DEEP_STUBS);
|
||||
private final SparkJobRunner.Progress progress = mock(SparkJobRunner.Progress.class);
|
||||
private final S3Service s3 = mock(S3Service.class);
|
||||
private final ContainerLogStreamer logStreamer = mock(ContainerLogStreamer.class);
|
||||
|
||||
@Test
|
||||
void sparkSubmitRunsLocallyOnFlocisS3ThenTheRunsOwnSettings() {
|
||||
@@ -354,6 +355,27 @@ class EmrServerlessSparkRunnerTest {
|
||||
verify(s3, never()).putObject(anyString(), anyString(), any(), anyString(), anyMap());
|
||||
}
|
||||
|
||||
@Test
|
||||
void theDriversOutputGoesToTheJobsCloudWatchLogStreamsWhileItRuns() {
|
||||
EmrServerlessSparkRunner runner = runner();
|
||||
String driver = "/applications/00f0000000000000/jobs/00f0000000000001/SPARK_DRIVER/";
|
||||
|
||||
runner.run(job(new SparkJobRunner.CloudWatchLogs("/aws/emr-serverless", driver + "stdout", driver + "stderr")),
|
||||
progress);
|
||||
|
||||
verify(logStreamer).attachByStreamForAccount("123456789012", "driver", "/aws/emr-serverless",
|
||||
driver + "stdout", driver + "stderr", "eu-west-1", "emr-serverless:00f0000000000001");
|
||||
verify(logStreamer, never()).attachConsoleOnly(anyString(), anyString());
|
||||
}
|
||||
|
||||
@Test
|
||||
void withoutCloudWatchLogStreamsTheDriversOutputGoesOnlyToFlocisLog() {
|
||||
runner().run(job(Duration.ZERO), progress);
|
||||
|
||||
verify(logStreamer).attachConsoleOnly("driver", "emr-serverless:00f0000000000001");
|
||||
verify(logStreamer, never()).attachByStreamForAccount(any(), any(), any(), any(), any(), any(), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void aRunWithoutALogFolderReadsNoOutput() {
|
||||
runner().run(job(Duration.ZERO), progress);
|
||||
@@ -396,7 +418,7 @@ class EmrServerlessSparkRunnerTest {
|
||||
when(lifecycle.createAndStart(any())).thenReturn(new ContainerLifecycleManager.ContainerInfo("driver", Map.of()));
|
||||
when(docker.inspectContainerCmd("driver").exec().getState().getRunning()).thenReturn(false);
|
||||
when(docker.inspectContainerCmd("driver").exec().getState().getExitCodeLong()).thenReturn(0L);
|
||||
return new EmrServerlessSparkRunner(builder, lifecycle, mock(ContainerLogStreamer.class),
|
||||
return new EmrServerlessSparkRunner(builder, lifecycle, logStreamer,
|
||||
mock(LaunchedContainerAwsEnv.class), mock(LambdaExecutionRoleCredentials.class), s3, config,
|
||||
new MutableClock(), Duration.ZERO);
|
||||
}
|
||||
@@ -440,7 +462,11 @@ class EmrServerlessSparkRunnerTest {
|
||||
}
|
||||
|
||||
private static SparkJobRunner.Job job(String logFolder) {
|
||||
return job(null, List.of("s3://bucket/jobs/app.jar"), Map.of(), Duration.ZERO, logFolder);
|
||||
return job(null, List.of("s3://bucket/jobs/app.jar"), Map.of(), Duration.ZERO, logFolder, null);
|
||||
}
|
||||
|
||||
private static SparkJobRunner.Job job(SparkJobRunner.CloudWatchLogs cloudWatchLogs) {
|
||||
return job(null, List.of("s3://bucket/jobs/app.jar"), Map.of(), Duration.ZERO, null, cloudWatchLogs);
|
||||
}
|
||||
|
||||
private static SparkJobRunner.Job job(List<String> application, Map<String, String> sparkDefaults) {
|
||||
@@ -449,18 +475,19 @@ class EmrServerlessSparkRunnerTest {
|
||||
|
||||
private static SparkJobRunner.Job job(List<String> application, Map<String, String> sparkDefaults,
|
||||
Duration timeout) {
|
||||
return job(null, application, sparkDefaults, timeout, null);
|
||||
return job(null, application, sparkDefaults, timeout, null, null);
|
||||
}
|
||||
|
||||
private static SparkJobRunner.Job job(String imageTemplate, List<String> application,
|
||||
Map<String, String> sparkDefaults, Duration timeout) {
|
||||
return job(imageTemplate, application, sparkDefaults, timeout, null);
|
||||
return job(imageTemplate, application, sparkDefaults, timeout, null, null);
|
||||
}
|
||||
|
||||
private static SparkJobRunner.Job job(String imageTemplate, List<String> application,
|
||||
Map<String, String> sparkDefaults, Duration timeout, String logFolder) {
|
||||
Map<String, String> sparkDefaults, Duration timeout, String logFolder,
|
||||
SparkJobRunner.CloudWatchLogs cloudWatchLogs) {
|
||||
return new SparkJobRunner.Job("emr-serverless", "00f0000000000001", "123456789012", "eu-west-1", "emr-7.5.0",
|
||||
imageTemplate, "floci-network", "arn:aws:iam::123456789012:role/job", sparkDefaults, application,
|
||||
timeout, logFolder);
|
||||
timeout, logFolder, cloudWatchLogs);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user