-
Notifications
You must be signed in to change notification settings - Fork 290
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Compute Spark SQL plan for each stage
- Loading branch information
1 parent
61ab1df
commit d90a497
Showing
5 changed files
with
201 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
138 changes: 138 additions & 0 deletions
138
...nstrumentation/spark/src/main/java/datadog/trace/instrumentation/spark/SparkSQLUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
package datadog.trace.instrumentation.spark; | ||
|
||
import com.fasterxml.jackson.core.JsonGenerator; | ||
import com.fasterxml.jackson.databind.DeserializationFeature; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import datadog.trace.bootstrap.instrumentation.api.AgentSpan; | ||
import org.apache.spark.sql.execution.SparkPlanInfo; | ||
import org.apache.spark.sql.execution.metric.SQLMetricInfo; | ||
import scala.collection.JavaConverters; | ||
|
||
import java.io.ByteArrayOutputStream; | ||
import java.io.IOException; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Set; | ||
|
||
public class SparkSQLUtils { | ||
|
||
public static void addSQLPlanToStageSpan( | ||
AgentSpan span, | ||
SparkPlanInfo sparkPlanInfo, | ||
HashMap<Long, Integer> accumulators, | ||
int stageId) { | ||
System.out.println("Computing planForStage: " + stageId); | ||
|
||
SparkPlanInfoForStage planForStage = computeStageInfoForStage(sparkPlanInfo, accumulators, stageId, false); | ||
|
||
if (planForStage != null) { | ||
System.out.println("Got non null planForStage: " + stageId); | ||
String json = planForStage.toJson(); | ||
span.setTag("_dd.spark.sql_plan", json); | ||
} | ||
} | ||
|
||
public static SparkPlanInfoForStage computeStageInfoForStage(SparkPlanInfo info, HashMap<Long, Integer> accumulatorToStage, int stageId, boolean alreadyStarted) { | ||
Set<Integer> stageIds = stageIdsForPlan(info, accumulatorToStage); | ||
|
||
boolean hasStageInfo = !stageIds.isEmpty(); | ||
boolean isForStage = stageIds.contains(stageId); | ||
|
||
System.out.println("On Node: " + info.nodeName()); | ||
|
||
if (alreadyStarted && hasStageInfo && !isForStage) { | ||
System.out.println("Skipping because other stage"); | ||
return null; | ||
} | ||
|
||
if (alreadyStarted || isForStage) { | ||
System.out.println("In computation with alreadyStarted: " + alreadyStarted + " isForStage: " + isForStage); | ||
List<SparkPlanInfoForStage> children = new ArrayList<>(); | ||
for (SparkPlanInfo child : JavaConverters.asJavaCollection(info.children())) { | ||
SparkPlanInfoForStage infoForStage = computeStageInfoForStage(child, accumulatorToStage, stageId, true); | ||
|
||
if (infoForStage != null) { | ||
children.add(infoForStage); | ||
} | ||
} | ||
|
||
return new SparkPlanInfoForStage(info.nodeName(), info.simpleString(), children); | ||
} | ||
else { | ||
System.out.println("Not started, looking in childrens"); | ||
for (SparkPlanInfo child : JavaConverters.asJavaCollection(info.children())) { | ||
System.out.println("Looking for children: " + child.nodeName()); | ||
SparkPlanInfoForStage infoForStage = computeStageInfoForStage(child, accumulatorToStage, stageId, false); | ||
|
||
if (infoForStage != null) { | ||
return infoForStage; | ||
} | ||
} | ||
} | ||
|
||
System.out.println("End of node: " + info.nodeName()); | ||
return null; | ||
} | ||
|
||
public static Set<Integer> stageIdsForPlan(SparkPlanInfo info, HashMap<Long, Integer> accumulatorToStage) { | ||
Set<Integer> stageIds = new HashSet<>(); | ||
|
||
for (SQLMetricInfo metric : JavaConverters.asJavaCollection(info.metrics())) { | ||
Integer stageId = accumulatorToStage.get(metric.accumulatorId()); | ||
|
||
if (stageId != null) { | ||
stageIds.add(stageId); | ||
} | ||
} | ||
|
||
return stageIds; | ||
} | ||
|
||
public static class SparkPlanInfoForStage { | ||
private final String nodeName; | ||
private final String simpleString; | ||
private final List<SparkPlanInfoForStage> children; | ||
|
||
public SparkPlanInfoForStage(String nodeName, String simpleString, List<SparkPlanInfoForStage> children) { | ||
this.nodeName = nodeName; | ||
this.simpleString = simpleString; | ||
this.children = children; | ||
} | ||
|
||
public String toJson() { | ||
ByteArrayOutputStream baos = new ByteArrayOutputStream(); | ||
ObjectMapper mapper = | ||
new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); | ||
try { | ||
JsonGenerator generator = mapper.getFactory().createGenerator(baos); | ||
this.toJson(generator); | ||
generator.close(); | ||
baos.close(); | ||
return new String(baos.toByteArray(), StandardCharsets.UTF_8); | ||
} catch (IOException e) { | ||
return null; | ||
} | ||
} | ||
|
||
private void toJson(JsonGenerator generator) throws IOException { | ||
generator.writeStartObject(); | ||
generator.writeStringField("node_name", nodeName); | ||
generator.writeStringField("simple_string", simpleString); | ||
|
||
// Writing child nodes | ||
if (children.size() > 0) { | ||
generator.writeFieldName("children"); | ||
generator.writeStartArray(); | ||
for (SparkPlanInfoForStage child : children) { | ||
child.toJson(generator); | ||
} | ||
generator.writeEndArray(); | ||
} | ||
|
||
generator.writeEndObject(); | ||
} | ||
} | ||
} |