You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@beam.apache.org by "rohitsinha54 (via GitHub)" <gi...@apache.org> on 2025/02/06 04:21:24 UTC

[PR] Use BoundedTrie metric to track lineage in IO [beam]

rohitsinha54 opened a new pull request, #33891:
URL: https://github.com/apache/beam/pull/33891

   - Updates Lineage to use BoundeedTrie
   - Handles backtick in segments which needs escaping too. See https://b.corp.google.com/issues/380375877#comment3 for details. 
   - Update IO to use the updated Lineage class changes.
   
   ------------------------
   
   Thank you for your contribution! Follow this checklist to help us incorporate your contribution quickly and easily:
   
    - [ ] Mention the appropriate issue in your description (for example: `addresses #123`), if applicable. This will automatically add a link to the pull request in the issue. If you would like the issue to automatically close on merging the pull request, comment `fixes #<ISSUE NUMBER>` instead.
    - [ ] Update `CHANGES.md` with noteworthy changes.
    - [ ] If this contribution is large, please file an Apache [Individual Contributor License Agreement](https://www.apache.org/licenses/icla.pdf).
   
   See the [Contributor Guide](https://beam.apache.org/contribute) for more tips on [how to make review process smoother](https://github.com/apache/beam/blob/master/CONTRIBUTING.md#make-the-reviewers-job-easier).
   
   To check the build health, please visit [https://github.com/apache/beam/blob/master/.test-infra/BUILD_STATUS.md](https://github.com/apache/beam/blob/master/.test-infra/BUILD_STATUS.md)
   
   GitHub Actions Tests Status (on master branch)
   ------------------------------------------------------------------------------------------------
   [![Build python source distribution and wheels](https://github.com/apache/beam/actions/workflows/build_wheels.yml/badge.svg?event=schedule&&?branch=master)](https://github.com/apache/beam/actions?query=workflow%3A%22Build+python+source+distribution+and+wheels%22+branch%3Amaster+event%3Aschedule)
   [![Python tests](https://github.com/apache/beam/actions/workflows/python_tests.yml/badge.svg?event=schedule&&?branch=master)](https://github.com/apache/beam/actions?query=workflow%3A%22Python+Tests%22+branch%3Amaster+event%3Aschedule)
   [![Java tests](https://github.com/apache/beam/actions/workflows/java_tests.yml/badge.svg?event=schedule&&?branch=master)](https://github.com/apache/beam/actions?query=workflow%3A%22Java+Tests%22+branch%3Amaster+event%3Aschedule)
   [![Go tests](https://github.com/apache/beam/actions/workflows/go_tests.yml/badge.svg?event=schedule&&?branch=master)](https://github.com/apache/beam/actions?query=workflow%3A%22Go+tests%22+branch%3Amaster+event%3Aschedule)
   
   See [CI.md](https://github.com/apache/beam/blob/master/CI.md) for more information about GitHub Actions CI or the [workflows README](https://github.com/apache/beam/blob/master/.github/workflows/README.md) to see a list of phrases to trigger workflows.
   


-- 
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


Re: [PR] Use BoundedTrie metric to track lineage in IO [beam]

Posted by "rohitsinha54 (via GitHub)" <gi...@apache.org>.
rohitsinha54 commented on PR #33891:
URL: https://github.com/apache/beam/pull/33891#issuecomment-2641351237

   @robertwb Gentle nudge if you can have a look at this today that will be awesome. Thank you. 


-- 
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


Re: [PR] Use BoundedTrie metric to track lineage in IO [beam]

Posted by "robertwb (via GitHub)" <gi...@apache.org>.
robertwb merged PR #33891:
URL: https://github.com/apache/beam/pull/33891


-- 
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


Re: [PR] Use BoundedTrie metric to track lineage in IO [beam]

Posted by "rohitsinha54 (via GitHub)" <gi...@apache.org>.
rohitsinha54 commented on code in PR #33891:
URL: https://github.com/apache/beam/pull/33891#discussion_r1992730837


##########
sdks/java/core/src/main/java/org/apache/beam/sdk/metrics/Lineage.java:
##########
@@ -50,111 +55,130 @@ public static Lineage getSinks() {
     return SINKS;
   }
 
-  /**
-   * Wrap segment to valid segment name.
-   *
-   * <p>Specifically, If there are reserved chars (colon, whitespace, dot), escape with backtick. If
-   * the segment is already wrapped, return the original.
-   *
-   * <p>This helper method is for internal and testing usage only.
-   */
-  @Internal
-  public static String wrapSegment(String value) {
-    if (value.startsWith("`") && value.endsWith("`")) {
-      return value;
-    }
-    if (RESERVED_CHARS.matcher(value).find()) {
-      return String.format("`%s`", value);
-    }
-    return value;
-  }
-
-  /**
-   * Assemble fully qualified name (<a
-   * href="https://cloud.google.com/data-catalog/docs/fully-qualified-names">FQN</a>). Format:
-   *
-   * <ul>
-   *   <li>{@code system:segment1.segment2}
-   *   <li>{@code system:subtype:segment1.segment2}
-   *   <li>{@code system:`segment1.with.dots:clons`.segment2}
-   * </ul>
-   *
-   * <p>This helper method is for internal and testing usage only.
-   */
-  @Internal
-  public static String getFqName(
-      String system, @Nullable String subtype, Iterable<String> segments) {
-    StringBuilder builder = new StringBuilder(system);
-    if (!Strings.isNullOrEmpty(subtype)) {
-      builder.append(":").append(subtype);
+  @VisibleForTesting
+  static Iterator<String> getFQNParts(
+      String system,
+      @Nullable String subtype,
+      List<String> segments,
+      @Nullable String lastSegmentSep) {
+
+    List<String> parts = new ArrayList<>();
+    parts.add(system + ":");
+    if (subtype != null) {
+      parts.add(subtype + ":");
     }
-    int idx = 0;
-    for (String segment : segments) {
-      if (idx == 0) {
-        builder.append(":");
+    if (segments != null && segments.size() > 0) {
+      for (int i = 0; i < segments.size() - 1; i++) {
+        parts.add(wrapSegment(segments.get(i)) + ".");
+      }
+      if (lastSegmentSep != null) {
+        List<String> subSegments =
+            Splitter.onPattern(lastSegmentSep)
+                .splitToList(wrapSegment(segments.get(segments.size() - 1)));
+        for (int i = 0; i < subSegments.size() - 1; i++) {
+          parts.add(subSegments.get(i) + lastSegmentSep);
+        }
+        parts.add(subSegments.get(subSegments.size() - 1));
       } else {
-        builder.append(".");
+        parts.add(segments.get(segments.size() - 1));
       }
-      builder.append(wrapSegment(segment));
-      ++idx;
     }
-    return builder.toString();
+    return parts.iterator();
   }
 
   /**
-   * Assemble the FQN of given system, and segments.
-   *
-   * <p>This helper method is for internal and testing usage only.
+   * Add a FQN (fully-qualified name) to Lineage. Segments will be processed via {@link
+   * #getFQNParts}.
    */
-  @Internal
-  public static String getFqName(String system, Iterable<String> segments) {
-    return getFqName(system, null, segments);
+  public void add(
+      String system,
+      @Nullable String subtype,
+      Iterable<String> segments,
+      @Nullable String lastSegmentSep) {
+    List<String> result = new ArrayList<String>();

Review Comment:
   Done. 



-- 
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


Re: [PR] Use BoundedTrie metric to track lineage in IO [beam]

Posted by "rohitsinha54 (via GitHub)" <gi...@apache.org>.
rohitsinha54 commented on code in PR #33891:
URL: https://github.com/apache/beam/pull/33891#discussion_r1992817605


##########
sdks/java/core/src/main/java/org/apache/beam/sdk/metrics/Lineage.java:
##########
@@ -50,111 +55,130 @@ public static Lineage getSinks() {
     return SINKS;
   }
 
-  /**
-   * Wrap segment to valid segment name.
-   *
-   * <p>Specifically, If there are reserved chars (colon, whitespace, dot), escape with backtick. If
-   * the segment is already wrapped, return the original.
-   *
-   * <p>This helper method is for internal and testing usage only.
-   */
-  @Internal
-  public static String wrapSegment(String value) {
-    if (value.startsWith("`") && value.endsWith("`")) {
-      return value;
-    }
-    if (RESERVED_CHARS.matcher(value).find()) {
-      return String.format("`%s`", value);
-    }
-    return value;
-  }
-
-  /**
-   * Assemble fully qualified name (<a
-   * href="https://cloud.google.com/data-catalog/docs/fully-qualified-names">FQN</a>). Format:
-   *
-   * <ul>
-   *   <li>{@code system:segment1.segment2}
-   *   <li>{@code system:subtype:segment1.segment2}
-   *   <li>{@code system:`segment1.with.dots:clons`.segment2}
-   * </ul>
-   *
-   * <p>This helper method is for internal and testing usage only.
-   */
-  @Internal
-  public static String getFqName(
-      String system, @Nullable String subtype, Iterable<String> segments) {
-    StringBuilder builder = new StringBuilder(system);
-    if (!Strings.isNullOrEmpty(subtype)) {
-      builder.append(":").append(subtype);
+  @VisibleForTesting
+  static Iterator<String> getFQNParts(
+      String system,
+      @Nullable String subtype,
+      List<String> segments,
+      @Nullable String lastSegmentSep) {
+
+    List<String> parts = new ArrayList<>();
+    parts.add(system + ":");
+    if (subtype != null) {
+      parts.add(subtype + ":");
     }
-    int idx = 0;
-    for (String segment : segments) {
-      if (idx == 0) {
-        builder.append(":");
+    if (segments != null && segments.size() > 0) {
+      for (int i = 0; i < segments.size() - 1; i++) {
+        parts.add(wrapSegment(segments.get(i)) + ".");
+      }
+      if (lastSegmentSep != null) {
+        List<String> subSegments =
+            Splitter.onPattern(lastSegmentSep)
+                .splitToList(wrapSegment(segments.get(segments.size() - 1)));
+        for (int i = 0; i < subSegments.size() - 1; i++) {
+          parts.add(subSegments.get(i) + lastSegmentSep);
+        }
+        parts.add(subSegments.get(subSegments.size() - 1));
       } else {
-        builder.append(".");
+        parts.add(segments.get(segments.size() - 1));
       }
-      builder.append(wrapSegment(segment));
-      ++idx;
     }
-    return builder.toString();
+    return parts.iterator();
   }
 
   /**
-   * Assemble the FQN of given system, and segments.
-   *
-   * <p>This helper method is for internal and testing usage only.
+   * Add a FQN (fully-qualified name) to Lineage. Segments will be processed via {@link
+   * #getFQNParts}.
    */
-  @Internal
-  public static String getFqName(String system, Iterable<String> segments) {
-    return getFqName(system, null, segments);
+  public void add(
+      String system,
+      @Nullable String subtype,
+      Iterable<String> segments,
+      @Nullable String lastSegmentSep) {
+    List<String> result = new ArrayList<String>();
+    segments.forEach(result::add);
+
+    add(getFQNParts(system, subtype, result, lastSegmentSep));
   }
 
   /**
-   * Add a FQN (fully-qualified name) to Lineage. Segments will be processed via {@link #getFqName}.
+   * Add a FQN (fully-qualified name) to Lineage. Segments will be processed via {@link
+   * #getFQNParts}.
    */
-  public void add(String system, @Nullable String subtype, Iterable<String> segments) {
-    add(getFqName(system, subtype, segments));
+  public void add(String system, Iterable<String> segments, @Nullable String lastSegmentSep) {
+    add(system, null, segments, lastSegmentSep);
   }
 
   /**
-   * Add a FQN (fully-qualified name) to Lineage. Segments will be processed via {@link #getFqName}.
+   * Add a FQN (fully-qualified name) to Lineage. Segments will be processed via {@link
+   * #getFQNParts}.
    */
   public void add(String system, Iterable<String> segments) {
-    add(system, null, segments);
+    add(system, segments, null);
   }
 
   /**
-   * Adds the given details as Lineage. For asset level lineage the resource location should be
-   * specified as Dataplex FQN https://cloud.google.com/data-catalog/docs/fully-qualified-names
+   * Adds the given fqn as lineage.
+   *
+   * @param rollupSegments should be an iterable of strings whose concatenation is a valid <a

Review Comment:
   Good catch. Thanks. 



-- 
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


Re: [PR] Use BoundedTrie metric to track lineage in IO [beam]

Posted by "rohitsinha54 (via GitHub)" <gi...@apache.org>.
rohitsinha54 commented on PR #33891:
URL: https://github.com/apache/beam/pull/33891#issuecomment-2722425065

   both failing tests seem to be unrelated and checking their history shows it as flaky. https://screenshot.googleplex.com/4zRjtquCiEbhZcZ


-- 
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


Re: [PR] Use BoundedTrie metric to track lineage in IO [beam]

Posted by "robertwb (via GitHub)" <gi...@apache.org>.
robertwb commented on code in PR #33891:
URL: https://github.com/apache/beam/pull/33891#discussion_r1994322894


##########
sdks/java/core/src/main/java/org/apache/beam/sdk/metrics/Lineage.java:
##########
@@ -50,111 +55,130 @@ public static Lineage getSinks() {
     return SINKS;
   }
 
-  /**
-   * Wrap segment to valid segment name.
-   *
-   * <p>Specifically, If there are reserved chars (colon, whitespace, dot), escape with backtick. If
-   * the segment is already wrapped, return the original.
-   *
-   * <p>This helper method is for internal and testing usage only.
-   */
-  @Internal
-  public static String wrapSegment(String value) {
-    if (value.startsWith("`") && value.endsWith("`")) {
-      return value;
-    }
-    if (RESERVED_CHARS.matcher(value).find()) {
-      return String.format("`%s`", value);
-    }
-    return value;
-  }
-
-  /**
-   * Assemble fully qualified name (<a
-   * href="https://cloud.google.com/data-catalog/docs/fully-qualified-names">FQN</a>). Format:
-   *
-   * <ul>
-   *   <li>{@code system:segment1.segment2}
-   *   <li>{@code system:subtype:segment1.segment2}
-   *   <li>{@code system:`segment1.with.dots:clons`.segment2}
-   * </ul>
-   *
-   * <p>This helper method is for internal and testing usage only.
-   */
-  @Internal
-  public static String getFqName(
-      String system, @Nullable String subtype, Iterable<String> segments) {
-    StringBuilder builder = new StringBuilder(system);
-    if (!Strings.isNullOrEmpty(subtype)) {
-      builder.append(":").append(subtype);
+  @VisibleForTesting
+  static Iterator<String> getFQNParts(
+      String system,
+      @Nullable String subtype,
+      List<String> segments,
+      @Nullable String lastSegmentSep) {
+
+    List<String> parts = new ArrayList<>();
+    parts.add(system + ":");
+    if (subtype != null) {
+      parts.add(subtype + ":");
     }
-    int idx = 0;
-    for (String segment : segments) {
-      if (idx == 0) {
-        builder.append(":");
+    if (segments != null && segments.size() > 0) {
+      for (int i = 0; i < segments.size() - 1; i++) {
+        parts.add(wrapSegment(segments.get(i)) + ".");
+      }
+      if (lastSegmentSep != null) {
+        List<String> subSegments =
+            Splitter.onPattern(lastSegmentSep)
+                .splitToList(wrapSegment(segments.get(segments.size() - 1)));
+        for (int i = 0; i < subSegments.size() - 1; i++) {
+          parts.add(subSegments.get(i) + lastSegmentSep);
+        }
+        parts.add(subSegments.get(subSegments.size() - 1));
       } else {
-        builder.append(".");
+        parts.add(segments.get(segments.size() - 1));
       }
-      builder.append(wrapSegment(segment));
-      ++idx;
     }
-    return builder.toString();
+    return parts.iterator();
   }
 
   /**
-   * Assemble the FQN of given system, and segments.
-   *
-   * <p>This helper method is for internal and testing usage only.
+   * Add a FQN (fully-qualified name) to Lineage. Segments will be processed via {@link
+   * #getFQNParts}.
    */
-  @Internal
-  public static String getFqName(String system, Iterable<String> segments) {
-    return getFqName(system, null, segments);
+  public void add(
+      String system,
+      @Nullable String subtype,
+      Iterable<String> segments,
+      @Nullable String lastSegmentSep) {
+    List<String> result = new ArrayList<String>();
+    segments.forEach(result::add);
+
+    add(getFQNParts(system, subtype, result, lastSegmentSep));
   }
 
   /**
-   * Add a FQN (fully-qualified name) to Lineage. Segments will be processed via {@link #getFqName}.
+   * Add a FQN (fully-qualified name) to Lineage. Segments will be processed via {@link
+   * #getFQNParts}.
    */
-  public void add(String system, @Nullable String subtype, Iterable<String> segments) {
-    add(getFqName(system, subtype, segments));
+  public void add(String system, Iterable<String> segments, @Nullable String lastSegmentSep) {
+    add(system, null, segments, lastSegmentSep);
   }
 
   /**
-   * Add a FQN (fully-qualified name) to Lineage. Segments will be processed via {@link #getFqName}.
+   * Add a FQN (fully-qualified name) to Lineage. Segments will be processed via {@link
+   * #getFQNParts}.
    */
   public void add(String system, Iterable<String> segments) {
-    add(system, null, segments);
+    add(system, segments, null);
   }
 
   /**
-   * Adds the given details as Lineage. For asset level lineage the resource location should be
-   * specified as Dataplex FQN https://cloud.google.com/data-catalog/docs/fully-qualified-names
+   * Adds the given fqn as lineage.
+   *
+   * @param rollupSegments should be an iterable of strings whose concatenation is a valid <a
+   *     href="https://cloud.google.com/data-catalog/docs/fully-qualified-names">Dataplex FQN </a>
+   *     which is already escaped.
+   *     <p>In particular, this means they will often have trailing delimiters.
    */
-  public void add(String details) {
-    metric.add(details);
+  public void add(Iterator<String> rollupSegments) {
+    List<String> segments = new ArrayList<>();
+    rollupSegments.forEachRemaining(segments::add);
+    this.metric.add(segments);
   }
 
-  /** Query {@link StringSet} metrics from {@link MetricResults}. */
-  public static Set<String> query(MetricResults results, Type type) {
+  /**
+   * Query {@link BoundedTrie} metrics from {@link MetricResults}.
+   *
+   * @param results FQNs from the result.
+   * @param type sources or sinks.
+   * @param truncatedMarker the marker to use to represent truncated FQNs.
+   * @return A flat representation of all FQNs. If the FQN was truncated then it has a trailing
+   *     truncatedMarker.
+   */
+  public static Set<String> query(MetricResults results, Type type, String truncatedMarker) {
     MetricsFilter filter =
         MetricsFilter.builder()
             .addNameFilter(MetricNameFilter.named(LINEAGE_NAMESPACE, type.toString()))
             .build();
     Set<String> result = new HashSet<>();
-    for (MetricResult<StringSetResult> metrics : results.queryMetrics(filter).getStringSets()) {
+    truncatedMarker = truncatedMarker == null ? "*" : truncatedMarker;
+    for (MetricResult<BoundedTrieResult> metrics : results.queryMetrics(filter).getBoundedTries()) {
       try {
-        result.addAll(metrics.getCommitted().getStringSet());
+        for (List<String> fqn : metrics.getCommitted().getResult()) {
+          String end = Boolean.parseBoolean(fqn.get(fqn.size() - 1)) ? truncatedMarker : "";

Review Comment:
   Yes, this can be deferred to a future PR.



-- 
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


Re: [PR] Use BoundedTrie metric to track lineage in IO [beam]

Posted by "robertwb (via GitHub)" <gi...@apache.org>.
robertwb commented on code in PR #33891:
URL: https://github.com/apache/beam/pull/33891#discussion_r1979839515


##########
sdks/java/core/src/main/java/org/apache/beam/sdk/metrics/Lineage.java:
##########
@@ -50,111 +55,130 @@ public static Lineage getSinks() {
     return SINKS;
   }
 
-  /**
-   * Wrap segment to valid segment name.
-   *
-   * <p>Specifically, If there are reserved chars (colon, whitespace, dot), escape with backtick. If
-   * the segment is already wrapped, return the original.
-   *
-   * <p>This helper method is for internal and testing usage only.
-   */
-  @Internal
-  public static String wrapSegment(String value) {
-    if (value.startsWith("`") && value.endsWith("`")) {
-      return value;
-    }
-    if (RESERVED_CHARS.matcher(value).find()) {
-      return String.format("`%s`", value);
-    }
-    return value;
-  }
-
-  /**
-   * Assemble fully qualified name (<a
-   * href="https://cloud.google.com/data-catalog/docs/fully-qualified-names">FQN</a>). Format:
-   *
-   * <ul>
-   *   <li>{@code system:segment1.segment2}
-   *   <li>{@code system:subtype:segment1.segment2}
-   *   <li>{@code system:`segment1.with.dots:clons`.segment2}
-   * </ul>
-   *
-   * <p>This helper method is for internal and testing usage only.
-   */
-  @Internal
-  public static String getFqName(
-      String system, @Nullable String subtype, Iterable<String> segments) {
-    StringBuilder builder = new StringBuilder(system);
-    if (!Strings.isNullOrEmpty(subtype)) {
-      builder.append(":").append(subtype);
+  @VisibleForTesting
+  static Iterator<String> getFQNParts(
+      String system,
+      @Nullable String subtype,
+      List<String> segments,
+      @Nullable String lastSegmentSep) {
+
+    List<String> parts = new ArrayList<>();
+    parts.add(system + ":");
+    if (subtype != null) {
+      parts.add(subtype + ":");
     }
-    int idx = 0;
-    for (String segment : segments) {
-      if (idx == 0) {
-        builder.append(":");
+    if (segments != null && segments.size() > 0) {
+      for (int i = 0; i < segments.size() - 1; i++) {
+        parts.add(wrapSegment(segments.get(i)) + ".");
+      }
+      if (lastSegmentSep != null) {
+        List<String> subSegments =
+            Splitter.onPattern(lastSegmentSep)
+                .splitToList(wrapSegment(segments.get(segments.size() - 1)));
+        for (int i = 0; i < subSegments.size() - 1; i++) {
+          parts.add(subSegments.get(i) + lastSegmentSep);
+        }
+        parts.add(subSegments.get(subSegments.size() - 1));
       } else {
-        builder.append(".");
+        parts.add(segments.get(segments.size() - 1));
       }
-      builder.append(wrapSegment(segment));
-      ++idx;
     }
-    return builder.toString();
+    return parts.iterator();
   }
 
   /**
-   * Assemble the FQN of given system, and segments.
-   *
-   * <p>This helper method is for internal and testing usage only.
+   * Add a FQN (fully-qualified name) to Lineage. Segments will be processed via {@link
+   * #getFQNParts}.
    */
-  @Internal
-  public static String getFqName(String system, Iterable<String> segments) {
-    return getFqName(system, null, segments);
+  public void add(
+      String system,
+      @Nullable String subtype,
+      Iterable<String> segments,
+      @Nullable String lastSegmentSep) {
+    List<String> result = new ArrayList<String>();

Review Comment:
   The name result makes it sound like this is the thing that is being returned...
   
   It would be clearer (and more efficient, not that it matters much here) to just do something like
   
   `add(getFQNParts(system, subtype, ImmutableList.copyOf(result), lastSegmentSep))`
   
   Even better might be to let getFQNParts take an `Iterable<String>` rather than requiring a `List<String>`.



##########
sdks/java/core/src/main/java/org/apache/beam/sdk/metrics/Lineage.java:
##########
@@ -50,111 +55,130 @@ public static Lineage getSinks() {
     return SINKS;
   }
 
-  /**
-   * Wrap segment to valid segment name.
-   *
-   * <p>Specifically, If there are reserved chars (colon, whitespace, dot), escape with backtick. If
-   * the segment is already wrapped, return the original.
-   *
-   * <p>This helper method is for internal and testing usage only.
-   */
-  @Internal
-  public static String wrapSegment(String value) {
-    if (value.startsWith("`") && value.endsWith("`")) {
-      return value;
-    }
-    if (RESERVED_CHARS.matcher(value).find()) {
-      return String.format("`%s`", value);
-    }
-    return value;
-  }
-
-  /**
-   * Assemble fully qualified name (<a
-   * href="https://cloud.google.com/data-catalog/docs/fully-qualified-names">FQN</a>). Format:
-   *
-   * <ul>
-   *   <li>{@code system:segment1.segment2}
-   *   <li>{@code system:subtype:segment1.segment2}
-   *   <li>{@code system:`segment1.with.dots:clons`.segment2}
-   * </ul>
-   *
-   * <p>This helper method is for internal and testing usage only.
-   */
-  @Internal
-  public static String getFqName(
-      String system, @Nullable String subtype, Iterable<String> segments) {
-    StringBuilder builder = new StringBuilder(system);
-    if (!Strings.isNullOrEmpty(subtype)) {
-      builder.append(":").append(subtype);
+  @VisibleForTesting
+  static Iterator<String> getFQNParts(
+      String system,
+      @Nullable String subtype,
+      List<String> segments,
+      @Nullable String lastSegmentSep) {
+
+    List<String> parts = new ArrayList<>();
+    parts.add(system + ":");
+    if (subtype != null) {
+      parts.add(subtype + ":");
     }
-    int idx = 0;
-    for (String segment : segments) {
-      if (idx == 0) {
-        builder.append(":");
+    if (segments != null && segments.size() > 0) {
+      for (int i = 0; i < segments.size() - 1; i++) {
+        parts.add(wrapSegment(segments.get(i)) + ".");
+      }
+      if (lastSegmentSep != null) {
+        List<String> subSegments =
+            Splitter.onPattern(lastSegmentSep)
+                .splitToList(wrapSegment(segments.get(segments.size() - 1)));
+        for (int i = 0; i < subSegments.size() - 1; i++) {
+          parts.add(subSegments.get(i) + lastSegmentSep);
+        }
+        parts.add(subSegments.get(subSegments.size() - 1));
       } else {
-        builder.append(".");
+        parts.add(segments.get(segments.size() - 1));
       }
-      builder.append(wrapSegment(segment));
-      ++idx;
     }
-    return builder.toString();
+    return parts.iterator();
   }
 
   /**
-   * Assemble the FQN of given system, and segments.
-   *
-   * <p>This helper method is for internal and testing usage only.
+   * Add a FQN (fully-qualified name) to Lineage. Segments will be processed via {@link
+   * #getFQNParts}.
    */
-  @Internal
-  public static String getFqName(String system, Iterable<String> segments) {
-    return getFqName(system, null, segments);
+  public void add(
+      String system,
+      @Nullable String subtype,
+      Iterable<String> segments,
+      @Nullable String lastSegmentSep) {
+    List<String> result = new ArrayList<String>();
+    segments.forEach(result::add);
+
+    add(getFQNParts(system, subtype, result, lastSegmentSep));
   }
 
   /**
-   * Add a FQN (fully-qualified name) to Lineage. Segments will be processed via {@link #getFqName}.
+   * Add a FQN (fully-qualified name) to Lineage. Segments will be processed via {@link
+   * #getFQNParts}.
    */
-  public void add(String system, @Nullable String subtype, Iterable<String> segments) {
-    add(getFqName(system, subtype, segments));
+  public void add(String system, Iterable<String> segments, @Nullable String lastSegmentSep) {
+    add(system, null, segments, lastSegmentSep);
   }
 
   /**
-   * Add a FQN (fully-qualified name) to Lineage. Segments will be processed via {@link #getFqName}.
+   * Add a FQN (fully-qualified name) to Lineage. Segments will be processed via {@link
+   * #getFQNParts}.
    */
   public void add(String system, Iterable<String> segments) {
-    add(system, null, segments);
+    add(system, segments, null);
   }
 
   /**
-   * Adds the given details as Lineage. For asset level lineage the resource location should be
-   * specified as Dataplex FQN https://cloud.google.com/data-catalog/docs/fully-qualified-names
+   * Adds the given fqn as lineage.
+   *
+   * @param rollupSegments should be an iterable of strings whose concatenation is a valid <a

Review Comment:
   This is typed as an Iterator but the docs say it's an iterable. Do we need an iterator? (Taking an Iterator is often problematic in an API as iterating over it (destructively) mutates it.)



##########
sdks/java/core/src/main/java/org/apache/beam/sdk/metrics/Lineage.java:
##########
@@ -50,111 +55,130 @@ public static Lineage getSinks() {
     return SINKS;
   }
 
-  /**
-   * Wrap segment to valid segment name.
-   *
-   * <p>Specifically, If there are reserved chars (colon, whitespace, dot), escape with backtick. If
-   * the segment is already wrapped, return the original.
-   *
-   * <p>This helper method is for internal and testing usage only.
-   */
-  @Internal
-  public static String wrapSegment(String value) {
-    if (value.startsWith("`") && value.endsWith("`")) {
-      return value;
-    }
-    if (RESERVED_CHARS.matcher(value).find()) {
-      return String.format("`%s`", value);
-    }
-    return value;
-  }
-
-  /**
-   * Assemble fully qualified name (<a
-   * href="https://cloud.google.com/data-catalog/docs/fully-qualified-names">FQN</a>). Format:
-   *
-   * <ul>
-   *   <li>{@code system:segment1.segment2}
-   *   <li>{@code system:subtype:segment1.segment2}
-   *   <li>{@code system:`segment1.with.dots:clons`.segment2}
-   * </ul>
-   *
-   * <p>This helper method is for internal and testing usage only.
-   */
-  @Internal
-  public static String getFqName(
-      String system, @Nullable String subtype, Iterable<String> segments) {
-    StringBuilder builder = new StringBuilder(system);
-    if (!Strings.isNullOrEmpty(subtype)) {
-      builder.append(":").append(subtype);
+  @VisibleForTesting
+  static Iterator<String> getFQNParts(
+      String system,
+      @Nullable String subtype,
+      List<String> segments,
+      @Nullable String lastSegmentSep) {
+
+    List<String> parts = new ArrayList<>();
+    parts.add(system + ":");
+    if (subtype != null) {
+      parts.add(subtype + ":");
     }
-    int idx = 0;
-    for (String segment : segments) {
-      if (idx == 0) {
-        builder.append(":");
+    if (segments != null && segments.size() > 0) {
+      for (int i = 0; i < segments.size() - 1; i++) {
+        parts.add(wrapSegment(segments.get(i)) + ".");
+      }
+      if (lastSegmentSep != null) {
+        List<String> subSegments =
+            Splitter.onPattern(lastSegmentSep)
+                .splitToList(wrapSegment(segments.get(segments.size() - 1)));
+        for (int i = 0; i < subSegments.size() - 1; i++) {
+          parts.add(subSegments.get(i) + lastSegmentSep);
+        }
+        parts.add(subSegments.get(subSegments.size() - 1));
       } else {
-        builder.append(".");
+        parts.add(segments.get(segments.size() - 1));
       }
-      builder.append(wrapSegment(segment));
-      ++idx;
     }
-    return builder.toString();
+    return parts.iterator();
   }
 
   /**
-   * Assemble the FQN of given system, and segments.
-   *
-   * <p>This helper method is for internal and testing usage only.
+   * Add a FQN (fully-qualified name) to Lineage. Segments will be processed via {@link
+   * #getFQNParts}.
    */
-  @Internal
-  public static String getFqName(String system, Iterable<String> segments) {
-    return getFqName(system, null, segments);
+  public void add(
+      String system,
+      @Nullable String subtype,
+      Iterable<String> segments,
+      @Nullable String lastSegmentSep) {
+    List<String> result = new ArrayList<String>();
+    segments.forEach(result::add);
+
+    add(getFQNParts(system, subtype, result, lastSegmentSep));
   }
 
   /**
-   * Add a FQN (fully-qualified name) to Lineage. Segments will be processed via {@link #getFqName}.
+   * Add a FQN (fully-qualified name) to Lineage. Segments will be processed via {@link
+   * #getFQNParts}.
    */
-  public void add(String system, @Nullable String subtype, Iterable<String> segments) {
-    add(getFqName(system, subtype, segments));
+  public void add(String system, Iterable<String> segments, @Nullable String lastSegmentSep) {
+    add(system, null, segments, lastSegmentSep);
   }
 
   /**
-   * Add a FQN (fully-qualified name) to Lineage. Segments will be processed via {@link #getFqName}.
+   * Add a FQN (fully-qualified name) to Lineage. Segments will be processed via {@link
+   * #getFQNParts}.
    */
   public void add(String system, Iterable<String> segments) {
-    add(system, null, segments);
+    add(system, segments, null);
   }
 
   /**
-   * Adds the given details as Lineage. For asset level lineage the resource location should be
-   * specified as Dataplex FQN https://cloud.google.com/data-catalog/docs/fully-qualified-names
+   * Adds the given fqn as lineage.
+   *
+   * @param rollupSegments should be an iterable of strings whose concatenation is a valid <a
+   *     href="https://cloud.google.com/data-catalog/docs/fully-qualified-names">Dataplex FQN </a>
+   *     which is already escaped.
+   *     <p>In particular, this means they will often have trailing delimiters.
    */
-  public void add(String details) {
-    metric.add(details);
+  public void add(Iterator<String> rollupSegments) {
+    List<String> segments = new ArrayList<>();
+    rollupSegments.forEachRemaining(segments::add);
+    this.metric.add(segments);

Review Comment:
   Again, just add `ImmutableList.copyOf(...)`.



##########
sdks/java/core/src/main/java/org/apache/beam/sdk/metrics/Lineage.java:
##########
@@ -50,111 +55,130 @@ public static Lineage getSinks() {
     return SINKS;
   }
 
-  /**
-   * Wrap segment to valid segment name.
-   *
-   * <p>Specifically, If there are reserved chars (colon, whitespace, dot), escape with backtick. If
-   * the segment is already wrapped, return the original.
-   *
-   * <p>This helper method is for internal and testing usage only.
-   */
-  @Internal
-  public static String wrapSegment(String value) {
-    if (value.startsWith("`") && value.endsWith("`")) {
-      return value;
-    }
-    if (RESERVED_CHARS.matcher(value).find()) {
-      return String.format("`%s`", value);
-    }
-    return value;
-  }
-
-  /**
-   * Assemble fully qualified name (<a
-   * href="https://cloud.google.com/data-catalog/docs/fully-qualified-names">FQN</a>). Format:
-   *
-   * <ul>
-   *   <li>{@code system:segment1.segment2}
-   *   <li>{@code system:subtype:segment1.segment2}
-   *   <li>{@code system:`segment1.with.dots:clons`.segment2}
-   * </ul>
-   *
-   * <p>This helper method is for internal and testing usage only.
-   */
-  @Internal
-  public static String getFqName(
-      String system, @Nullable String subtype, Iterable<String> segments) {
-    StringBuilder builder = new StringBuilder(system);
-    if (!Strings.isNullOrEmpty(subtype)) {
-      builder.append(":").append(subtype);
+  @VisibleForTesting
+  static Iterator<String> getFQNParts(
+      String system,
+      @Nullable String subtype,
+      List<String> segments,
+      @Nullable String lastSegmentSep) {
+
+    List<String> parts = new ArrayList<>();
+    parts.add(system + ":");
+    if (subtype != null) {
+      parts.add(subtype + ":");
     }
-    int idx = 0;
-    for (String segment : segments) {
-      if (idx == 0) {
-        builder.append(":");
+    if (segments != null && segments.size() > 0) {
+      for (int i = 0; i < segments.size() - 1; i++) {
+        parts.add(wrapSegment(segments.get(i)) + ".");
+      }
+      if (lastSegmentSep != null) {
+        List<String> subSegments =
+            Splitter.onPattern(lastSegmentSep)
+                .splitToList(wrapSegment(segments.get(segments.size() - 1)));
+        for (int i = 0; i < subSegments.size() - 1; i++) {
+          parts.add(subSegments.get(i) + lastSegmentSep);
+        }
+        parts.add(subSegments.get(subSegments.size() - 1));
       } else {
-        builder.append(".");
+        parts.add(segments.get(segments.size() - 1));
       }
-      builder.append(wrapSegment(segment));
-      ++idx;
     }
-    return builder.toString();
+    return parts.iterator();
   }
 
   /**
-   * Assemble the FQN of given system, and segments.
-   *
-   * <p>This helper method is for internal and testing usage only.
+   * Add a FQN (fully-qualified name) to Lineage. Segments will be processed via {@link
+   * #getFQNParts}.
    */
-  @Internal
-  public static String getFqName(String system, Iterable<String> segments) {
-    return getFqName(system, null, segments);
+  public void add(
+      String system,
+      @Nullable String subtype,
+      Iterable<String> segments,
+      @Nullable String lastSegmentSep) {
+    List<String> result = new ArrayList<String>();
+    segments.forEach(result::add);
+
+    add(getFQNParts(system, subtype, result, lastSegmentSep));
   }
 
   /**
-   * Add a FQN (fully-qualified name) to Lineage. Segments will be processed via {@link #getFqName}.
+   * Add a FQN (fully-qualified name) to Lineage. Segments will be processed via {@link
+   * #getFQNParts}.
    */
-  public void add(String system, @Nullable String subtype, Iterable<String> segments) {
-    add(getFqName(system, subtype, segments));
+  public void add(String system, Iterable<String> segments, @Nullable String lastSegmentSep) {
+    add(system, null, segments, lastSegmentSep);
   }
 
   /**
-   * Add a FQN (fully-qualified name) to Lineage. Segments will be processed via {@link #getFqName}.
+   * Add a FQN (fully-qualified name) to Lineage. Segments will be processed via {@link
+   * #getFQNParts}.
    */
   public void add(String system, Iterable<String> segments) {
-    add(system, null, segments);
+    add(system, segments, null);
   }
 
   /**
-   * Adds the given details as Lineage. For asset level lineage the resource location should be
-   * specified as Dataplex FQN https://cloud.google.com/data-catalog/docs/fully-qualified-names
+   * Adds the given fqn as lineage.
+   *
+   * @param rollupSegments should be an iterable of strings whose concatenation is a valid <a
+   *     href="https://cloud.google.com/data-catalog/docs/fully-qualified-names">Dataplex FQN </a>
+   *     which is already escaped.
+   *     <p>In particular, this means they will often have trailing delimiters.
    */
-  public void add(String details) {
-    metric.add(details);
+  public void add(Iterator<String> rollupSegments) {
+    List<String> segments = new ArrayList<>();
+    rollupSegments.forEachRemaining(segments::add);
+    this.metric.add(segments);
   }
 
-  /** Query {@link StringSet} metrics from {@link MetricResults}. */
-  public static Set<String> query(MetricResults results, Type type) {
+  /**
+   * Query {@link BoundedTrie} metrics from {@link MetricResults}.
+   *
+   * @param results FQNs from the result.
+   * @param type sources or sinks.
+   * @param truncatedMarker the marker to use to represent truncated FQNs.
+   * @return A flat representation of all FQNs. If the FQN was truncated then it has a trailing
+   *     truncatedMarker.
+   */
+  public static Set<String> query(MetricResults results, Type type, String truncatedMarker) {
     MetricsFilter filter =
         MetricsFilter.builder()
             .addNameFilter(MetricNameFilter.named(LINEAGE_NAMESPACE, type.toString()))
             .build();
     Set<String> result = new HashSet<>();
-    for (MetricResult<StringSetResult> metrics : results.queryMetrics(filter).getStringSets()) {
+    truncatedMarker = truncatedMarker == null ? "*" : truncatedMarker;
+    for (MetricResult<BoundedTrieResult> metrics : results.queryMetrics(filter).getBoundedTries()) {
       try {
-        result.addAll(metrics.getCommitted().getStringSet());
+        for (List<String> fqn : metrics.getCommitted().getResult()) {
+          String end = Boolean.parseBoolean(fqn.get(fqn.size() - 1)) ? truncatedMarker : "";

Review Comment:
   Is it too late to change `metrics.getCommitted().getResult()` to itself take a truncatedMarker rather than always use boolean strings?



-- 
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


Re: [PR] Use BoundedTrie metric to track lineage in IO [beam]

Posted by "rohitsinha54 (via GitHub)" <gi...@apache.org>.
rohitsinha54 commented on PR #33891:
URL: https://github.com/apache/beam/pull/33891#issuecomment-2640969367

   Legacy worker tests are failing because service backend (router) does not know BoundedTrie metric yet and fails the job. 


-- 
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


Re: [PR] Use BoundedTrie metric to track lineage in IO [beam]

Posted by "rohitsinha54 (via GitHub)" <gi...@apache.org>.
rohitsinha54 commented on code in PR #33891:
URL: https://github.com/apache/beam/pull/33891#discussion_r1992818823


##########
sdks/java/core/src/main/java/org/apache/beam/sdk/metrics/Lineage.java:
##########
@@ -50,111 +55,130 @@ public static Lineage getSinks() {
     return SINKS;
   }
 
-  /**
-   * Wrap segment to valid segment name.
-   *
-   * <p>Specifically, If there are reserved chars (colon, whitespace, dot), escape with backtick. If
-   * the segment is already wrapped, return the original.
-   *
-   * <p>This helper method is for internal and testing usage only.
-   */
-  @Internal
-  public static String wrapSegment(String value) {
-    if (value.startsWith("`") && value.endsWith("`")) {
-      return value;
-    }
-    if (RESERVED_CHARS.matcher(value).find()) {
-      return String.format("`%s`", value);
-    }
-    return value;
-  }
-
-  /**
-   * Assemble fully qualified name (<a
-   * href="https://cloud.google.com/data-catalog/docs/fully-qualified-names">FQN</a>). Format:
-   *
-   * <ul>
-   *   <li>{@code system:segment1.segment2}
-   *   <li>{@code system:subtype:segment1.segment2}
-   *   <li>{@code system:`segment1.with.dots:clons`.segment2}
-   * </ul>
-   *
-   * <p>This helper method is for internal and testing usage only.
-   */
-  @Internal
-  public static String getFqName(
-      String system, @Nullable String subtype, Iterable<String> segments) {
-    StringBuilder builder = new StringBuilder(system);
-    if (!Strings.isNullOrEmpty(subtype)) {
-      builder.append(":").append(subtype);
+  @VisibleForTesting
+  static Iterator<String> getFQNParts(
+      String system,
+      @Nullable String subtype,
+      List<String> segments,
+      @Nullable String lastSegmentSep) {
+
+    List<String> parts = new ArrayList<>();
+    parts.add(system + ":");
+    if (subtype != null) {
+      parts.add(subtype + ":");
     }
-    int idx = 0;
-    for (String segment : segments) {
-      if (idx == 0) {
-        builder.append(":");
+    if (segments != null && segments.size() > 0) {
+      for (int i = 0; i < segments.size() - 1; i++) {
+        parts.add(wrapSegment(segments.get(i)) + ".");
+      }
+      if (lastSegmentSep != null) {
+        List<String> subSegments =
+            Splitter.onPattern(lastSegmentSep)
+                .splitToList(wrapSegment(segments.get(segments.size() - 1)));
+        for (int i = 0; i < subSegments.size() - 1; i++) {
+          parts.add(subSegments.get(i) + lastSegmentSep);
+        }
+        parts.add(subSegments.get(subSegments.size() - 1));
       } else {
-        builder.append(".");
+        parts.add(segments.get(segments.size() - 1));
       }
-      builder.append(wrapSegment(segment));
-      ++idx;
     }
-    return builder.toString();
+    return parts.iterator();
   }
 
   /**
-   * Assemble the FQN of given system, and segments.
-   *
-   * <p>This helper method is for internal and testing usage only.
+   * Add a FQN (fully-qualified name) to Lineage. Segments will be processed via {@link
+   * #getFQNParts}.
    */
-  @Internal
-  public static String getFqName(String system, Iterable<String> segments) {
-    return getFqName(system, null, segments);
+  public void add(
+      String system,
+      @Nullable String subtype,
+      Iterable<String> segments,
+      @Nullable String lastSegmentSep) {
+    List<String> result = new ArrayList<String>();
+    segments.forEach(result::add);
+
+    add(getFQNParts(system, subtype, result, lastSegmentSep));
   }
 
   /**
-   * Add a FQN (fully-qualified name) to Lineage. Segments will be processed via {@link #getFqName}.
+   * Add a FQN (fully-qualified name) to Lineage. Segments will be processed via {@link
+   * #getFQNParts}.
    */
-  public void add(String system, @Nullable String subtype, Iterable<String> segments) {
-    add(getFqName(system, subtype, segments));
+  public void add(String system, Iterable<String> segments, @Nullable String lastSegmentSep) {
+    add(system, null, segments, lastSegmentSep);
   }
 
   /**
-   * Add a FQN (fully-qualified name) to Lineage. Segments will be processed via {@link #getFqName}.
+   * Add a FQN (fully-qualified name) to Lineage. Segments will be processed via {@link
+   * #getFQNParts}.
    */
   public void add(String system, Iterable<String> segments) {
-    add(system, null, segments);
+    add(system, segments, null);
   }
 
   /**
-   * Adds the given details as Lineage. For asset level lineage the resource location should be
-   * specified as Dataplex FQN https://cloud.google.com/data-catalog/docs/fully-qualified-names
+   * Adds the given fqn as lineage.
+   *
+   * @param rollupSegments should be an iterable of strings whose concatenation is a valid <a
+   *     href="https://cloud.google.com/data-catalog/docs/fully-qualified-names">Dataplex FQN </a>
+   *     which is already escaped.
+   *     <p>In particular, this means they will often have trailing delimiters.
    */
-  public void add(String details) {
-    metric.add(details);
+  public void add(Iterator<String> rollupSegments) {
+    List<String> segments = new ArrayList<>();
+    rollupSegments.forEachRemaining(segments::add);
+    this.metric.add(segments);

Review Comment:
   Done



##########
sdks/java/core/src/main/java/org/apache/beam/sdk/metrics/Lineage.java:
##########
@@ -50,111 +55,130 @@ public static Lineage getSinks() {
     return SINKS;
   }
 
-  /**
-   * Wrap segment to valid segment name.
-   *
-   * <p>Specifically, If there are reserved chars (colon, whitespace, dot), escape with backtick. If
-   * the segment is already wrapped, return the original.
-   *
-   * <p>This helper method is for internal and testing usage only.
-   */
-  @Internal
-  public static String wrapSegment(String value) {
-    if (value.startsWith("`") && value.endsWith("`")) {
-      return value;
-    }
-    if (RESERVED_CHARS.matcher(value).find()) {
-      return String.format("`%s`", value);
-    }
-    return value;
-  }
-
-  /**
-   * Assemble fully qualified name (<a
-   * href="https://cloud.google.com/data-catalog/docs/fully-qualified-names">FQN</a>). Format:
-   *
-   * <ul>
-   *   <li>{@code system:segment1.segment2}
-   *   <li>{@code system:subtype:segment1.segment2}
-   *   <li>{@code system:`segment1.with.dots:clons`.segment2}
-   * </ul>
-   *
-   * <p>This helper method is for internal and testing usage only.
-   */
-  @Internal
-  public static String getFqName(
-      String system, @Nullable String subtype, Iterable<String> segments) {
-    StringBuilder builder = new StringBuilder(system);
-    if (!Strings.isNullOrEmpty(subtype)) {
-      builder.append(":").append(subtype);
+  @VisibleForTesting
+  static Iterator<String> getFQNParts(
+      String system,
+      @Nullable String subtype,
+      List<String> segments,
+      @Nullable String lastSegmentSep) {
+
+    List<String> parts = new ArrayList<>();
+    parts.add(system + ":");
+    if (subtype != null) {
+      parts.add(subtype + ":");
     }
-    int idx = 0;
-    for (String segment : segments) {
-      if (idx == 0) {
-        builder.append(":");
+    if (segments != null && segments.size() > 0) {
+      for (int i = 0; i < segments.size() - 1; i++) {
+        parts.add(wrapSegment(segments.get(i)) + ".");
+      }
+      if (lastSegmentSep != null) {
+        List<String> subSegments =
+            Splitter.onPattern(lastSegmentSep)
+                .splitToList(wrapSegment(segments.get(segments.size() - 1)));
+        for (int i = 0; i < subSegments.size() - 1; i++) {
+          parts.add(subSegments.get(i) + lastSegmentSep);
+        }
+        parts.add(subSegments.get(subSegments.size() - 1));
       } else {
-        builder.append(".");
+        parts.add(segments.get(segments.size() - 1));
       }
-      builder.append(wrapSegment(segment));
-      ++idx;
     }
-    return builder.toString();
+    return parts.iterator();
   }
 
   /**
-   * Assemble the FQN of given system, and segments.
-   *
-   * <p>This helper method is for internal and testing usage only.
+   * Add a FQN (fully-qualified name) to Lineage. Segments will be processed via {@link
+   * #getFQNParts}.
    */
-  @Internal
-  public static String getFqName(String system, Iterable<String> segments) {
-    return getFqName(system, null, segments);
+  public void add(
+      String system,
+      @Nullable String subtype,
+      Iterable<String> segments,
+      @Nullable String lastSegmentSep) {
+    List<String> result = new ArrayList<String>();
+    segments.forEach(result::add);
+
+    add(getFQNParts(system, subtype, result, lastSegmentSep));
   }
 
   /**
-   * Add a FQN (fully-qualified name) to Lineage. Segments will be processed via {@link #getFqName}.
+   * Add a FQN (fully-qualified name) to Lineage. Segments will be processed via {@link
+   * #getFQNParts}.
    */
-  public void add(String system, @Nullable String subtype, Iterable<String> segments) {
-    add(getFqName(system, subtype, segments));
+  public void add(String system, Iterable<String> segments, @Nullable String lastSegmentSep) {
+    add(system, null, segments, lastSegmentSep);
   }
 
   /**
-   * Add a FQN (fully-qualified name) to Lineage. Segments will be processed via {@link #getFqName}.
+   * Add a FQN (fully-qualified name) to Lineage. Segments will be processed via {@link
+   * #getFQNParts}.
    */
   public void add(String system, Iterable<String> segments) {
-    add(system, null, segments);
+    add(system, segments, null);
   }
 
   /**
-   * Adds the given details as Lineage. For asset level lineage the resource location should be
-   * specified as Dataplex FQN https://cloud.google.com/data-catalog/docs/fully-qualified-names
+   * Adds the given fqn as lineage.
+   *
+   * @param rollupSegments should be an iterable of strings whose concatenation is a valid <a
+   *     href="https://cloud.google.com/data-catalog/docs/fully-qualified-names">Dataplex FQN </a>
+   *     which is already escaped.
+   *     <p>In particular, this means they will often have trailing delimiters.
    */
-  public void add(String details) {
-    metric.add(details);
+  public void add(Iterator<String> rollupSegments) {
+    List<String> segments = new ArrayList<>();
+    rollupSegments.forEachRemaining(segments::add);
+    this.metric.add(segments);
   }
 
-  /** Query {@link StringSet} metrics from {@link MetricResults}. */
-  public static Set<String> query(MetricResults results, Type type) {
+  /**
+   * Query {@link BoundedTrie} metrics from {@link MetricResults}.
+   *
+   * @param results FQNs from the result.
+   * @param type sources or sinks.
+   * @param truncatedMarker the marker to use to represent truncated FQNs.
+   * @return A flat representation of all FQNs. If the FQN was truncated then it has a trailing
+   *     truncatedMarker.
+   */
+  public static Set<String> query(MetricResults results, Type type, String truncatedMarker) {
     MetricsFilter filter =
         MetricsFilter.builder()
             .addNameFilter(MetricNameFilter.named(LINEAGE_NAMESPACE, type.toString()))
             .build();
     Set<String> result = new HashSet<>();
-    for (MetricResult<StringSetResult> metrics : results.queryMetrics(filter).getStringSets()) {
+    truncatedMarker = truncatedMarker == null ? "*" : truncatedMarker;
+    for (MetricResult<BoundedTrieResult> metrics : results.queryMetrics(filter).getBoundedTries()) {
       try {
-        result.addAll(metrics.getCommitted().getStringSet());
+        for (List<String> fqn : metrics.getCommitted().getResult()) {
+          String end = Boolean.parseBoolean(fqn.get(fqn.size() - 1)) ? truncatedMarker : "";

Review Comment:
   Good idea. We can update it to do so. 
   I will like to do it in a follow up PR after this one as it is an internal clean up and have no customer visible impact due to this once I finish the service side dfe plumbing for this. If that's ok with you. 
   



-- 
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


Re: [PR] Use BoundedTrie metric to track lineage in IO [beam]

Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] commented on PR #33891:
URL: https://github.com/apache/beam/pull/33891#issuecomment-2638791446

   Stopping reviewer notifications for this pull request: review requested by someone other than the bot, ceding control. If you'd like to restart, comment `assign set of reviewers`


-- 
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


Re: [PR] Use BoundedTrie metric to track lineage in IO [beam]

Posted by "rohitsinha54 (via GitHub)" <gi...@apache.org>.
rohitsinha54 commented on PR #33891:
URL: https://github.com/apache/beam/pull/33891#issuecomment-2638790308

   R: @robertwb 
   


-- 
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