-
Notifications
You must be signed in to change notification settings - Fork 4.5k
[BEAM-12453]: Add interface to access I/O topic information for a Samza Beam job and PipelineJsonRenderer to create the JSON Beam DAG #14945
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
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
8982c63
Add interface to access I/O topic information for a Samza Beam job
PawasChhokra 341d589
Add JSON config for Beam DAG visualization
PawasChhokra bd4901a
Address review
PawasChhokra b042fc0
Address review
PawasChhokra 0578f9c
Remove `shortName` and `id`
PawasChhokra 9b3421e
Remove checker framework errors
PawasChhokra 3b0a69d
Fix json
PawasChhokra bd060f0
Address review
PawasChhokra c0354af
Address review
PawasChhokra 0fc4a06
Address review
PawasChhokra 0631185
Address review
PawasChhokra b91a607
Make interface public
PawasChhokra File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
209 changes: 209 additions & 0 deletions
209
runners/samza/src/main/java/org/apache/beam/runners/samza/util/PipelineJsonRenderer.java
This file contains hidden or 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,209 @@ | ||
| /* | ||
| * 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.beam.runners.samza.util; | ||
|
|
||
| import java.util.HashMap; | ||
| import java.util.Iterator; | ||
| import java.util.Map; | ||
| import java.util.Optional; | ||
| import java.util.ServiceLoader; | ||
| import javax.annotation.Nullable; | ||
| import org.apache.beam.model.pipeline.v1.RunnerApi; | ||
| import org.apache.beam.sdk.Pipeline; | ||
| import org.apache.beam.sdk.annotations.Experimental; | ||
| import org.apache.beam.sdk.runners.TransformHierarchy; | ||
| import org.apache.beam.sdk.values.PValue; | ||
| import org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.Iterators; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| /** | ||
| * A JSON renderer for BEAM {@link Pipeline} DAG. This can help us with visualization of the Beam | ||
| * DAG. | ||
| */ | ||
| @Experimental | ||
| public class PipelineJsonRenderer implements Pipeline.PipelineVisitor { | ||
|
|
||
| /** | ||
| * Interface to get I/O information for a Beam job. This will help add I/O information to the Beam | ||
| * DAG. | ||
| */ | ||
| @Experimental | ||
| public interface SamzaIOInfo { | ||
|
|
||
| /** Get I/O topic name and cluster. */ | ||
| Optional<String> getIOInfo(TransformHierarchy.Node node); | ||
| } | ||
|
|
||
| /** A registrar for {@link SamzaIOInfo}. */ | ||
| public interface SamzaIORegistrar { | ||
|
|
||
| SamzaIOInfo getSamzaIO(); | ||
| } | ||
|
|
||
| private static final Logger LOG = LoggerFactory.getLogger(PipelineJsonRenderer.class); | ||
| private static final String OUTERMOST_NODE = "OuterMostNode"; | ||
| @Nullable private static final SamzaIOInfo SAMZA_IO_INFO = loadSamzaIOInfo(); | ||
|
|
||
| /** | ||
| * This method creates a JSON representation of the Beam pipeline. | ||
| * | ||
| * @param pipeline The beam pipeline | ||
| * @return JSON string representation of the pipeline | ||
| */ | ||
| public static String toJsonString(Pipeline pipeline) { | ||
| final PipelineJsonRenderer visitor = new PipelineJsonRenderer(); | ||
| pipeline.traverseTopologically(visitor); | ||
| return visitor.jsonBuilder.toString(); | ||
| } | ||
|
|
||
| /** | ||
| * This method creates a JSON representation for Beam Portable Pipeline. | ||
| * | ||
| * @param pipeline The beam portable pipeline | ||
| * @return JSON string representation of the pipeline | ||
| */ | ||
| public static String toJsonString(RunnerApi.Pipeline pipeline) { | ||
| throw new UnsupportedOperationException("JSON DAG for portable pipeline is not supported yet."); | ||
| } | ||
|
|
||
| private final StringBuilder jsonBuilder = new StringBuilder(); | ||
| private final StringBuilder graphLinks = new StringBuilder(); | ||
| private final Map<PValue, String> valueToProducerNodeName = new HashMap<>(); | ||
| private int indent; | ||
|
|
||
| private PipelineJsonRenderer() {} | ||
|
|
||
| @Nullable | ||
| private static SamzaIOInfo loadSamzaIOInfo() { | ||
| final Iterator<SamzaIORegistrar> beamIORegistrarIterator = | ||
| ServiceLoader.load(SamzaIORegistrar.class).iterator(); | ||
| return beamIORegistrarIterator.hasNext() | ||
| ? Iterators.getOnlyElement(beamIORegistrarIterator).getSamzaIO() | ||
| : null; | ||
| } | ||
|
|
||
| @Override | ||
| public void enterPipeline(Pipeline p) { | ||
| writeLine("{ \n \"RootNode\": ["); | ||
| graphLinks.append(",\"graphLinks\": ["); | ||
| enterBlock(); | ||
| } | ||
|
|
||
| @Override | ||
| public CompositeBehavior enterCompositeTransform(TransformHierarchy.Node node) { | ||
| String fullName = node.getFullName(); | ||
| writeLine("{ \"fullName\":\"%s\",", assignNodeName(fullName)); | ||
| if (node.getEnclosingNode() != null) { | ||
| String enclosingNodeName = node.getEnclosingNode().getFullName(); | ||
| writeLine(" \"enclosingNode\":\"%s\",", assignNodeName(enclosingNodeName)); | ||
| } | ||
|
|
||
| Optional<String> ioInfo = getIOInfo(node); | ||
| if (ioInfo.isPresent() && !ioInfo.get().isEmpty()) { | ||
| writeLine(" \"ioInfo\":\"%s\",", escapeString(ioInfo.get())); | ||
| } | ||
|
|
||
| writeLine(" \"ChildNodes\":["); | ||
| enterBlock(); | ||
| return CompositeBehavior.ENTER_TRANSFORM; | ||
| } | ||
|
|
||
| @Override | ||
| public void leaveCompositeTransform(TransformHierarchy.Node node) { | ||
| exitBlock(); | ||
| writeLine("]},"); | ||
| } | ||
|
|
||
| @Override | ||
| public void visitPrimitiveTransform(TransformHierarchy.Node node) { | ||
| String fullName = node.getFullName(); | ||
| writeLine("{ \"fullName\":\"%s\",", escapeString(fullName)); | ||
| String enclosingNodeName = node.getEnclosingNode().getFullName(); | ||
| writeLine(" \"enclosingNode\":\"%s\"},", assignNodeName(enclosingNodeName)); | ||
|
|
||
| node.getOutputs().values().forEach(x -> valueToProducerNodeName.put(x, fullName)); | ||
| node.getInputs() | ||
| .forEach( | ||
| (key, value) -> { | ||
| final String producerName = valueToProducerNodeName.get(value); | ||
| graphLinks.append( | ||
| String.format("{\"from\":\"%s\"," + "\"to\":\"%s\"},", producerName, fullName)); | ||
| }); | ||
| } | ||
|
|
||
| @Override | ||
| public void visitValue(PValue value, TransformHierarchy.Node producer) {} | ||
|
|
||
| @Override | ||
| public void leavePipeline(Pipeline pipeline) { | ||
| exitBlock(); | ||
| writeLine("]"); | ||
| // delete the last comma | ||
| int lastIndex = graphLinks.length() - 1; | ||
| if (graphLinks.charAt(lastIndex) == ',') { | ||
| graphLinks.deleteCharAt(lastIndex); | ||
| } | ||
| graphLinks.append("]"); | ||
| jsonBuilder.append(graphLinks); | ||
| jsonBuilder.append("}"); | ||
| } | ||
|
|
||
| private void enterBlock() { | ||
| indent += 4; | ||
| } | ||
|
|
||
| private void exitBlock() { | ||
| indent -= 4; | ||
| } | ||
|
|
||
| private void writeLine(String format, Object... args) { | ||
| // Since we append a comma after every entry to the graph, we will need to remove that one extra | ||
| // comma towards the end of the JSON. | ||
| int secondLastCharIndex = jsonBuilder.length() - 2; | ||
| if (jsonBuilder.length() > 1 | ||
| && jsonBuilder.charAt(secondLastCharIndex) == ',' | ||
| && (format.startsWith("}") || format.startsWith("]"))) { | ||
| jsonBuilder.deleteCharAt(secondLastCharIndex); | ||
| } | ||
| if (indent != 0) { | ||
| jsonBuilder.append(String.format("%-" + indent + "s", "")); | ||
| } | ||
| jsonBuilder.append(String.format(format, args)); | ||
| jsonBuilder.append("\n"); | ||
| } | ||
|
|
||
| private static String escapeString(String x) { | ||
| return x.replace("\"", "\\\""); | ||
| } | ||
|
|
||
| private static String shortenTag(String tag) { | ||
| return tag.replaceFirst(".*:([a-zA-Z#0-9]+).*", "$1"); | ||
| } | ||
|
|
||
| private String assignNodeName(String nodeName) { | ||
| return escapeString(nodeName.isEmpty() ? OUTERMOST_NODE : nodeName); | ||
| } | ||
|
|
||
| private Optional<String> getIOInfo(TransformHierarchy.Node node) { | ||
| if (SAMZA_IO_INFO == null) { | ||
| return Optional.empty(); | ||
| } | ||
| return SAMZA_IO_INFO.getIOInfo(node); | ||
| } | ||
| } |
105 changes: 105 additions & 0 deletions
105
runners/samza/src/test/java/org/apache/beam/runners/samza/util/PipelineJsonRendererTest.java
This file contains hidden or 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,105 @@ | ||
| /* | ||
| * 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.beam.runners.samza.util; | ||
|
|
||
| import static org.junit.Assert.assertEquals; | ||
|
|
||
| import com.google.auto.service.AutoService; | ||
| import com.google.gson.JsonParser; | ||
| import java.io.IOException; | ||
| import java.nio.charset.StandardCharsets; | ||
| import java.nio.file.Files; | ||
| import java.nio.file.Paths; | ||
| import java.util.Optional; | ||
| import org.apache.beam.runners.samza.SamzaPipelineOptions; | ||
| import org.apache.beam.runners.samza.SamzaRunner; | ||
| import org.apache.beam.sdk.Pipeline; | ||
| import org.apache.beam.sdk.options.PipelineOptionsFactory; | ||
| import org.apache.beam.sdk.runners.TransformHierarchy; | ||
| import org.apache.beam.sdk.transforms.Create; | ||
| import org.apache.beam.sdk.transforms.Sum; | ||
| import org.apache.beam.sdk.transforms.windowing.FixedWindows; | ||
| import org.apache.beam.sdk.transforms.windowing.Window; | ||
| import org.apache.beam.sdk.values.KV; | ||
| import org.apache.beam.sdk.values.TimestampedValue; | ||
| import org.joda.time.Duration; | ||
| import org.joda.time.Instant; | ||
| import org.junit.Test; | ||
|
|
||
| /** Tests for {@link org.apache.beam.runners.samza.util.PipelineJsonRenderer}. */ | ||
| public class PipelineJsonRendererTest { | ||
|
|
||
| @Test | ||
| public void testEmptyPipeline() { | ||
| SamzaPipelineOptions options = PipelineOptionsFactory.create().as(SamzaPipelineOptions.class); | ||
| options.setRunner(SamzaRunner.class); | ||
|
|
||
| Pipeline p = Pipeline.create(options); | ||
|
|
||
| String jsonDag = | ||
| "{ \"RootNode\": [" | ||
| + " { \"fullName\":\"OuterMostNode\"," | ||
| + " \"ioInfo\":\"TestTopic\"," | ||
| + " \"ChildNodes\":[ ]}],\"graphLinks\": []" | ||
| + "}"; | ||
|
|
||
| System.out.println(PipelineJsonRenderer.toJsonString(p)); | ||
| assertEquals( | ||
| JsonParser.parseString(jsonDag), | ||
| JsonParser.parseString( | ||
| PipelineJsonRenderer.toJsonString(p).replaceAll(System.lineSeparator(), ""))); | ||
| } | ||
|
|
||
| @Test | ||
| public void testCompositePipeline() throws IOException { | ||
| SamzaPipelineOptions options = PipelineOptionsFactory.create().as(SamzaPipelineOptions.class); | ||
| options.setRunner(SamzaRunner.class); | ||
|
|
||
| Pipeline p = Pipeline.create(options); | ||
|
|
||
| p.apply(Create.timestamped(TimestampedValue.of(KV.of(1, 1), new Instant(1)))) | ||
| .apply(Window.into(FixedWindows.of(Duration.millis(10)))) | ||
| .apply(Sum.integersPerKey()); | ||
|
|
||
| String jsonDagFileName = "src/test/resources/ExpectedDag.json"; | ||
| String jsonDag = | ||
| new String(Files.readAllBytes(Paths.get(jsonDagFileName)), StandardCharsets.UTF_8); | ||
|
|
||
| assertEquals( | ||
| JsonParser.parseString(jsonDag), | ||
| JsonParser.parseString( | ||
| PipelineJsonRenderer.toJsonString(p).replaceAll(System.lineSeparator(), ""))); | ||
| } | ||
|
|
||
| @AutoService(PipelineJsonRenderer.SamzaIORegistrar.class) | ||
| public static class Registrar implements PipelineJsonRenderer.SamzaIORegistrar { | ||
|
|
||
| @Override | ||
| public PipelineJsonRenderer.SamzaIOInfo getSamzaIO() { | ||
| return new PipelineJsonRenderer.SamzaIOInfo() { | ||
| @Override | ||
| public Optional<String> getIOInfo(TransformHierarchy.Node node) { | ||
| if (node.isRootNode()) { | ||
| return Optional.of("TestTopic"); | ||
| } | ||
| return Optional.empty(); | ||
| } | ||
| }; | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.