You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@beam.apache.org by GitBox <gi...@apache.org> on 2021/08/10 21:41:13 UTC

[GitHub] [beam] xinyuiscool commented on a change in pull request #14945: [BEAM-12453]: Add interface to access I/O topic information for a Samza Beam job and PipelineJsonRenderer to create the JSON Beam DAG

xinyuiscool commented on a change in pull request #14945:
URL: https://github.com/apache/beam/pull/14945#discussion_r686333507



##########
File path: runners/samza/src/main/java/org/apache/beam/runners/samza/SamzaIOInfo.java
##########
@@ -0,0 +1,38 @@
+/*
+ * 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;
+
+import java.util.Optional;
+import org.apache.beam.sdk.annotations.Experimental;
+import org.apache.beam.sdk.runners.TransformHierarchy;
+
+/**
+ * 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 {

Review comment:
       Seems this interface is only used by the json rendering part. Please make it an inner interface in the render instead of top level.

##########
File path: runners/samza/src/main/java/org/apache/beam/runners/samza/renderer/PipelineJsonRenderer.java
##########
@@ -0,0 +1,186 @@
+/*
+ * 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.renderer;

Review comment:
       No need for a new package. Move this to util.

##########
File path: runners/samza/src/main/java/org/apache/beam/runners/samza/renderer/PipelineJsonRenderer.java
##########
@@ -0,0 +1,186 @@
+/*
+ * 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.renderer;
+
+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.runners.samza.SamzaIOInfo;
+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 {
+  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();
+
+  public static String toJsonString(Pipeline pipeline) {

Review comment:
       java doc for public static methods.

##########
File path: runners/samza/src/main/java/org/apache/beam/runners/samza/renderer/PipelineJsonRenderer.java
##########
@@ -0,0 +1,186 @@
+/*
+ * 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.renderer;
+
+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.runners.samza.SamzaIOInfo;
+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 {
+  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();
+
+  public static String toJsonString(Pipeline pipeline) {
+    final PipelineJsonRenderer visitor = new PipelineJsonRenderer();
+    visitor.begin();
+    pipeline.traverseTopologically(visitor);
+    visitor.end();
+    return visitor.jsonBuilder.toString();
+  }
+
+  public static String toJsonString(RunnerApi.Pipeline pipeline) {
+    return "";
+  }
+
+  private final StringBuilder jsonBuilder = new StringBuilder();
+  private final StringBuilder graphLinks = new StringBuilder();
+  private final Map<TransformHierarchy.Node, Integer> nodeToId = new HashMap<>();
+  private final Map<PValue, String> valueToProducerNodeName = new HashMap<>();
+  private int indent;
+  private int nextNodeId;
+
+  private PipelineJsonRenderer() {}
+
+  @Nullable
+  private static SamzaIOInfo loadSamzaIOInfo() {
+    final Iterator<SamzaIOInfo.SamzaIORegistrar> beamIORegistrarIterator =
+        ServiceLoader.load(SamzaIOInfo.SamzaIORegistrar.class).iterator();
+    return beamIORegistrarIterator.hasNext()
+        ? Iterators.getOnlyElement(beamIORegistrarIterator).getSamzaIO()
+        : null;
+  }
+
+  @Override
+  public void enterPipeline(Pipeline p) {}
+
+  @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 = getIOTopicInfo(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) {}
+
+  private void begin() {
+    writeLine("{ \n \"RootNode\": [");
+    graphLinks.append(",\"graphLinks\": [");
+    enterBlock();
+  }
+
+  private void end() {
+    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) {
+    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> getIOTopicInfo(TransformHierarchy.Node node) {

Review comment:
       Remove 'topic' in the name.

##########
File path: runners/samza/src/main/java/org/apache/beam/runners/samza/renderer/PipelineJsonRenderer.java
##########
@@ -0,0 +1,186 @@
+/*
+ * 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.renderer;
+
+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.runners.samza.SamzaIOInfo;
+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 {
+  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();
+
+  public static String toJsonString(Pipeline pipeline) {
+    final PipelineJsonRenderer visitor = new PipelineJsonRenderer();
+    visitor.begin();

Review comment:
       instead of using begin() and end(), you can use the beam enterPipeline() and leavePipeline() interfaces.

##########
File path: runners/samza/src/main/java/org/apache/beam/runners/samza/renderer/PipelineJsonRenderer.java
##########
@@ -0,0 +1,186 @@
+/*
+ * 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.renderer;
+
+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.runners.samza.SamzaIOInfo;
+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 {
+  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();
+
+  public static String toJsonString(Pipeline pipeline) {
+    final PipelineJsonRenderer visitor = new PipelineJsonRenderer();
+    visitor.begin();
+    pipeline.traverseTopologically(visitor);
+    visitor.end();
+    return visitor.jsonBuilder.toString();
+  }
+
+  public static String toJsonString(RunnerApi.Pipeline pipeline) {
+    return "";

Review comment:
       throw java lang unsupportedoperationexception instead of silent fail here.

##########
File path: runners/samza/src/main/java/org/apache/beam/runners/samza/SamzaRunner.java
##########
@@ -61,7 +61,7 @@
 })
 public class SamzaRunner extends PipelineRunner<SamzaPipelineResult> {
   private static final Logger LOG = LoggerFactory.getLogger(SamzaRunner.class);
-  private static final String BEAM_DOT_GRAPH = "beamDotGraph";

Review comment:
       Please keep the dot rendering as it is. We rely on it in open source to view beam dags.

##########
File path: runners/samza/src/main/java/org/apache/beam/runners/samza/renderer/package-info.java
##########
@@ -0,0 +1,20 @@
+/*
+ * 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.
+ */
+
+/** Internal implementation of the Beam runner for Apache Samza. */
+package org.apache.beam.runners.samza.renderer;

Review comment:
       No new package.

##########
File path: runners/samza/src/main/java/org/apache/beam/runners/samza/renderer/PipelineJsonRenderer.java
##########
@@ -0,0 +1,186 @@
+/*
+ * 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.renderer;
+
+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.runners.samza.SamzaIOInfo;
+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 {
+  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();
+
+  public static String toJsonString(Pipeline pipeline) {
+    final PipelineJsonRenderer visitor = new PipelineJsonRenderer();
+    visitor.begin();
+    pipeline.traverseTopologically(visitor);
+    visitor.end();
+    return visitor.jsonBuilder.toString();
+  }
+
+  public static String toJsonString(RunnerApi.Pipeline pipeline) {

Review comment:
       java doc for public static methods.

##########
File path: runners/samza/src/main/java/org/apache/beam/runners/samza/SamzaIOInfo.java
##########
@@ -0,0 +1,38 @@
+/*
+ * 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;
+
+import java.util.Optional;
+import org.apache.beam.sdk.annotations.Experimental;
+import org.apache.beam.sdk.runners.TransformHierarchy;
+
+/**
+ * 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}. */
+  interface SamzaIORegistrar {

Review comment:
       Make this on the same level as SamzaIOInfo

##########
File path: runners/samza/src/main/java/org/apache/beam/runners/samza/renderer/PipelineJsonRenderer.java
##########
@@ -0,0 +1,186 @@
+/*
+ * 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.renderer;
+
+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.runners.samza.SamzaIOInfo;
+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 {
+  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();
+
+  public static String toJsonString(Pipeline pipeline) {
+    final PipelineJsonRenderer visitor = new PipelineJsonRenderer();
+    visitor.begin();
+    pipeline.traverseTopologically(visitor);
+    visitor.end();
+    return visitor.jsonBuilder.toString();
+  }
+
+  public static String toJsonString(RunnerApi.Pipeline pipeline) {
+    return "";
+  }
+
+  private final StringBuilder jsonBuilder = new StringBuilder();
+  private final StringBuilder graphLinks = new StringBuilder();
+  private final Map<TransformHierarchy.Node, Integer> nodeToId = new HashMap<>();
+  private final Map<PValue, String> valueToProducerNodeName = new HashMap<>();
+  private int indent;
+  private int nextNodeId;
+
+  private PipelineJsonRenderer() {}
+
+  @Nullable
+  private static SamzaIOInfo loadSamzaIOInfo() {
+    final Iterator<SamzaIOInfo.SamzaIORegistrar> beamIORegistrarIterator =
+        ServiceLoader.load(SamzaIOInfo.SamzaIORegistrar.class).iterator();
+    return beamIORegistrarIterator.hasNext()
+        ? Iterators.getOnlyElement(beamIORegistrarIterator).getSamzaIO()
+        : null;
+  }
+
+  @Override
+  public void enterPipeline(Pipeline p) {}
+
+  @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 = getIOTopicInfo(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) {}
+
+  private void begin() {
+    writeLine("{ \n \"RootNode\": [");
+    graphLinks.append(",\"graphLinks\": [");
+    enterBlock();
+  }
+
+  private void end() {
+    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) {
+    int secondLastCharIndex = jsonBuilder.length() - 2;
+    if (jsonBuilder.length() > 1
+        && jsonBuilder.charAt(secondLastCharIndex) == ','
+        && (format.startsWith("}") || format.startsWith("]"))) {
+      jsonBuilder.deleteCharAt(secondLastCharIndex);

Review comment:
       This condition and the deletion here is very hard to understand. Please add java doc.

##########
File path: runners/samza/src/main/java/org/apache/beam/runners/samza/renderer/PipelineJsonRenderer.java
##########
@@ -0,0 +1,186 @@
+/*
+ * 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.renderer;
+
+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.runners.samza.SamzaIOInfo;
+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 {

Review comment:
       I suggest to have a inner class in this renderer to implement the visitor instead of letting the rendering implementing it. The methods in the visitor look weird as public methods in the renderer.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@beam.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org