-
Notifications
You must be signed in to change notification settings - Fork 442
TEZ-4733: Fix flaky TestHistoryParser.testParserWithSuccessfulJob #519
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -33,6 +33,7 @@ | |
| import java.util.Iterator; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.concurrent.TimeUnit; | ||
|
|
||
| import org.apache.commons.cli.ParseException; | ||
| import org.apache.commons.io.FileUtils; | ||
|
|
@@ -118,6 +119,7 @@ public class TestHistoryParser { | |
| private final static String SUMMATION = "Summation"; | ||
| private final static String SIMPLE_HISTORY_DIR = "/tmp/simplehistory/"; | ||
| private final static String HISTORY_TXT = "history.txt"; | ||
| private static final long ATS_RETRY_DELAY_MS = 5_000L; | ||
|
|
||
| private static Configuration conf = new Configuration(); | ||
| private static FileSystem fs; | ||
|
|
@@ -209,22 +211,21 @@ public void testParserWithSuccessfulJob() throws Exception { | |
| String dagId = runWordCount(WordCount.TokenProcessor.class.getName(), | ||
| WordCount.SumProcessor.class.getName(), "WordCount", true); | ||
|
|
||
| //Export the data from ATS | ||
| String[] args = { "--dagId=" + dagId, "--downloadDir=" + DOWNLOAD_DIR, "--yarnTimelineAddress=" + yarnTimelineAddress }; | ||
| //Retry the ATS export+parse pipeline until the resulting DagInfo actually contains | ||
| //the expected DAG (two vertices, non-empty vertices/tasks). Under load the AM's async | ||
| //flush and the timeline server's write path can race the export, leaving empty/partial | ||
| //entities in the zip. | ||
| DagInfo dagInfoFromATS = fetchDagInfoFromAtsWithRetry(dagId, 6, 2); | ||
|
|
||
| int result = ATSImportTool.process(args); | ||
| assertEquals(0, result); | ||
|
|
||
| //Parse ATS data and verify results | ||
| DagInfo dagInfoFromATS = getDagInfo(dagId); | ||
| verifyDagInfo(dagInfoFromATS, true); | ||
| verifyJobSpecificInfo(dagInfoFromATS); | ||
| checkConfig(dagInfoFromATS); | ||
|
|
||
| //Now run with SimpleHistoryLogging | ||
| dagId = runWordCount(WordCount.TokenProcessor.class.getName(), | ||
| WordCount.SumProcessor.class.getName(), "WordCount", false); | ||
| Thread.sleep(10000); //For all flushes to happen and to avoid half-cooked download. | ||
|
|
||
| waitForHistoryFileReady(dagId, 60_000L); | ||
|
|
||
| DagInfo shDagInfo = getDagInfoFromSimpleHistory(dagId); | ||
| verifyDagInfo(shDagInfo, false); | ||
|
|
@@ -234,6 +235,83 @@ public void testParserWithSuccessfulJob() throws Exception { | |
| isDAGEqual(dagInfoFromATS, shDagInfo); | ||
| } | ||
|
|
||
| /** | ||
| * The ATS write path is async (AM event queue → timeline client → timeline server). Even | ||
| * after the DAG client reports the job complete, timeline entities may still be in transit, | ||
| * so an export triggered right after completion can capture a partial snapshot (empty zip | ||
| * entries, or a DagInfo with fewer vertices / empty task lists). Retry the export+parse | ||
| * pipeline until {@link #isDagInfoComplete} confirms the snapshot is populated. | ||
| */ | ||
| private DagInfo fetchDagInfoFromAtsWithRetry(String dagId, int maxAttempts, | ||
| int expectedNumOfVertices) throws Exception { | ||
| String[] args = { "--dagId=" + dagId, "--downloadDir=" + DOWNLOAD_DIR, | ||
| "--yarnTimelineAddress=" + yarnTimelineAddress }; | ||
| Exception lastError = null; | ||
| DagInfo lastPartial = null; | ||
| for (int attempt = 0; attempt < maxAttempts; attempt++) { | ||
| try { | ||
| // Fresh download every attempt — ATSImportTool overwrites the zip. | ||
| int result = ATSImportTool.process(args); | ||
| assertEquals(0, result); | ||
| DagInfo info = getDagInfo(dagId); | ||
| if (isDagInfoComplete(info, expectedNumOfVertices)) { | ||
| return info; | ||
| } | ||
| lastPartial = info; | ||
| } catch (Exception e) { | ||
| lastError = e; | ||
| } | ||
| if (attempt < maxAttempts - 1) { | ||
| Thread.sleep(ATS_RETRY_DELAY_MS); | ||
| } | ||
| } | ||
| fail("Could not fetch a complete DagInfo for " + dagId + " after " + maxAttempts | ||
| + " attempts (lastError=" + lastError + ", lastPartial=" | ||
| + (lastPartial == null ? "null" | ||
| : "vertices=" + lastPartial.getVertices().size() | ||
| + ", tasks=" + (lastPartial.getVertices().isEmpty() ? 0 | ||
| : lastPartial.getVertices().iterator().next().getTasks().size())) | ||
| + ")"); | ||
| return null; | ||
| } | ||
|
|
||
| /** | ||
| * ATS parsing can succeed on a partially-written zip: the reader returns a DagInfo with | ||
| * fewer vertices than the DAG actually has, or vertices whose task/attempt lists are still | ||
| * empty. That's the race we're guarding against — a "successful" parse is not proof the | ||
| * export was complete. We know the expected vertex count from the DAG under test, so require | ||
| * it explicitly and require every vertex to have at least one task with at least one attempt. | ||
| */ | ||
| private static boolean isDagInfoComplete(DagInfo info, int expectedNumOfVertices) { | ||
| return info != null | ||
| && info.getVertices().size() >= expectedNumOfVertices | ||
| && info.getVertices().stream().allMatch(v -> | ||
| !v.getTasks().isEmpty() | ||
| && v.getTasks().stream().allMatch(t -> !t.getTaskAttempts().isEmpty())); | ||
|
Comment on lines
+286
to
+290
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why is this check needed? is there a chance that the |
||
| } | ||
|
|
||
| private void waitForHistoryFileReady(String dagId, long timeoutMs) throws Exception { | ||
| TezDAGID tezDAGID = TezDAGID.fromString(dagId); | ||
| ApplicationAttemptId applicationAttemptId = ApplicationAttemptId.newInstance(tezDAGID.getApplicationId(), 1); | ||
| Path historyPath = new Path(conf.get("fs.defaultFS") | ||
| + SIMPLE_HISTORY_DIR + HISTORY_TXT + "." + applicationAttemptId); | ||
| FileSystem hfs = historyPath.getFileSystem(conf); | ||
| long deadlineNanos = System.nanoTime() + TimeUnit.MILLISECONDS.toNanos(timeoutMs); | ||
| long lastLen = -1L; | ||
| while (System.nanoTime() < deadlineNanos) { | ||
| if (hfs.exists(historyPath)) { | ||
| long len = hfs.getFileStatus(historyPath).getLen(); | ||
| if (len > 0 && len == lastLen) { | ||
| return; | ||
| } | ||
| lastLen = len; | ||
| } | ||
| Thread.sleep(500); | ||
| } | ||
| fail("Timed out waiting for SimpleHistory file " + historyPath | ||
| + " to be ready within " + timeoutMs + "ms (lastLen=" + lastLen + ")"); | ||
| } | ||
|
|
||
| private DagInfo getDagInfoFromSimpleHistory(String dagId) throws TezException, IOException { | ||
| TezDAGID tezDAGID = TezDAGID.fromString(dagId); | ||
| ApplicationAttemptId applicationAttemptId = ApplicationAttemptId.newInstance(tezDAGID | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
| package org.apache.tez.history.parser; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
| import static org.junit.jupiter.api.Assertions.assertThrows; | ||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
|
||
| import java.io.File; | ||
| import java.io.FileOutputStream; | ||
| import java.nio.charset.StandardCharsets; | ||
| import java.nio.file.Path; | ||
| import java.util.Collections; | ||
| import java.util.zip.ZipEntry; | ||
| import java.util.zip.ZipOutputStream; | ||
|
|
||
| import org.apache.tez.dag.api.TezException; | ||
| import org.apache.tez.history.parser.datamodel.DagInfo; | ||
|
|
||
| import org.codehaus.jettison.json.JSONArray; | ||
| import org.codehaus.jettison.json.JSONException; | ||
| import org.codehaus.jettison.json.JSONObject; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.junit.jupiter.api.io.TempDir; | ||
|
|
||
| public class TestATSFileParser { | ||
|
|
||
| private static final String DAG_ID = "dag_1234567890_0001_1"; | ||
|
|
||
| private static JSONObject minimalDagJson() throws JSONException { | ||
| JSONObject dag = new JSONObject(); | ||
| dag.put("entityId", DAG_ID); | ||
| dag.put("entityType", "TEZ_DAG_ID"); | ||
| JSONObject otherInfo = new JSONObject(); | ||
| otherInfo.put("startTime", 1L); | ||
| otherInfo.put("endTime", 2L); | ||
| otherInfo.put("status", "SUCCEEDED"); | ||
| otherInfo.put("counters", new JSONObject().put("counterGroups", new JSONArray())); | ||
| dag.put("otherInfo", otherInfo); | ||
| return dag; | ||
| } | ||
|
|
||
| private static File writeZip(Path dir, String name, ZipContent... entries) throws Exception { | ||
| File zip = dir.resolve(name).toFile(); | ||
| try (FileOutputStream fos = new FileOutputStream(zip); | ||
| ZipOutputStream zos = new ZipOutputStream(fos)) { | ||
| for (ZipContent entry : entries) { | ||
| zos.putNextEntry(new ZipEntry(entry.name())); | ||
| zos.write(entry.payload().getBytes(StandardCharsets.UTF_8)); | ||
| zos.closeEntry(); | ||
| } | ||
| } | ||
| return zip; | ||
| } | ||
|
|
||
| @Test | ||
| public void parserSkipsEmptyZipEntryAndParsesRemaining(@TempDir Path tmp) throws Exception { | ||
| JSONObject dagRoot = new JSONObject().put("dag", minimalDagJson()); | ||
|
|
||
| File zip = writeZip(tmp, "empty-then-good.zip", | ||
| new ZipContent("empty-part.json", ""), | ||
| new ZipContent("whitespace-part.json", " \n\t "), | ||
| new ZipContent(DAG_ID, dagRoot.toString())); | ||
|
|
||
| ATSFileParser parser = new ATSFileParser(Collections.singletonList(zip)); | ||
| DagInfo info = parser.getDAGData(DAG_ID); | ||
|
|
||
| assertNotNull(info, "Parser should return DagInfo even when some entries are empty"); | ||
| assertEquals(DAG_ID, info.getDagId()); | ||
| assertEquals("SUCCEEDED", info.getStatus()); | ||
| } | ||
|
|
||
| @Test | ||
| public void parserReportsOffendingEntryOnMalformedJson(@TempDir Path tmp) throws Exception { | ||
| // Simulates the timeline server returning an HTML error page instead of JSON. | ||
| File zip = writeZip(tmp, "malformed.zip", | ||
| new ZipContent(DAG_ID, "<html><body>internal error</body></html>")); | ||
|
Comment on lines
+93
to
+94
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. no need to break line here |
||
|
|
||
| ATSFileParser parser = new ATSFileParser(Collections.singletonList(zip)); | ||
| TezException thrown = assertThrows(TezException.class, () -> parser.getDAGData(DAG_ID)); | ||
|
|
||
| Throwable cause = thrown.getCause(); | ||
| assertNotNull(cause); | ||
| String msg = cause.getMessage(); | ||
| assertNotNull(msg); | ||
| assertTrue(msg.contains(DAG_ID), "Error should name the offending zip entry, got: " + msg); | ||
| assertTrue(msg.contains("<html>"), "Error should include a snippet of the offending payload, got: " + msg); | ||
| } | ||
|
|
||
| private record ZipContent(String name, String payload) { | ||
| } | ||
|
Comment on lines
+107
to
+108
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what is the record for?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @abstractdog |
||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is for " enrich JSON parse errors with the offending entry name + payload snippet", which makes sense to me
for clarity's sake what a JSONException was like before? was it really only "A JSONObject text must begin with '{' at character 0 of "
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@abstractdog Yes, Jettison's JSONObject(String) constructor calls into JSONTokener, and on an empty payload the message we saw was below
org.codehaus.jettison.json.JSONException: A JSONObject text must begin with '{' at character 0 of