shnapz commented on code in PR #36781:
URL: https://github.com/apache/beam/pull/36781#discussion_r3030534749


##########
sdks/java/core/src/main/java/org/apache/beam/sdk/metrics/Lineage.java:
##########
@@ -17,51 +17,117 @@
  */
 package org.apache.beam.sdk.metrics;
 
+import static 
org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.base.Preconditions.checkNotNull;
+
 import java.util.ArrayList;
 import java.util.HashSet;
 import java.util.Iterator;
 import java.util.List;
+import java.util.Objects;
 import java.util.Set;
+import java.util.concurrent.atomic.AtomicReference;
 import java.util.regex.Pattern;
 import org.apache.beam.sdk.annotations.Internal;
+import org.apache.beam.sdk.lineage.LineageBase;
+import org.apache.beam.sdk.lineage.LineageOptions;
 import org.apache.beam.sdk.metrics.Metrics.MetricsFlag;
+import org.apache.beam.sdk.options.PipelineOptions;
+import org.apache.beam.sdk.options.PipelineOptionsFactory;
 import 
org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.annotations.VisibleForTesting;
 import 
org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.base.Splitter;
-import 
org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.collect.ImmutableList;
 import org.checkerframework.checker.nullness.qual.Nullable;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 /**
  * Standard collection of metrics used to record source and sinks information 
for lineage tracking.
  */
 public class Lineage {
-
   public static final String LINEAGE_NAMESPACE = "lineage";
-  private static final Lineage SOURCES = new Lineage(Type.SOURCE);
-  private static final Lineage SINKS = new Lineage(Type.SINK);
+  private static final Logger LOG = LoggerFactory.getLogger(Lineage.class);
+  private static final AtomicReference<Lineage> SOURCES = new 
AtomicReference<>();
+  private static final AtomicReference<Lineage> SINKS = new 
AtomicReference<>();
+
+  private static final AtomicReference<@Nullable Class<? extends LineageBase>>
+      CURRENT_LINEAGE_TYPE = new AtomicReference<>();
+
   // Reserved characters are backtick, colon, whitespace (space, \t, \n) and 
dot.
   private static final Pattern RESERVED_CHARS = Pattern.compile("[:\\s.`]");
 
-  private final Metric metric;
+  private final LineageBase delegate;
 
-  private Lineage(Type type) {
-    if (MetricsFlag.lineageRollupEnabled()) {
-      this.metric =
-          Metrics.boundedTrie(
-              LINEAGE_NAMESPACE,
-              type == Type.SOURCE ? Type.SOURCEV2.toString() : 
Type.SINKV2.toString());
-    } else {
-      this.metric = Metrics.stringSet(LINEAGE_NAMESPACE, type.toString());
+  public enum LineageDirection {
+    SOURCE,
+    SINK
+  }
+
+  private Lineage(LineageBase delegate) {
+    this.delegate = checkNotNull(delegate, "delegate cannot be null");
+  }
+
+  @Internal
+  public static void setDefaultPipelineOptions(PipelineOptions options) {
+    checkNotNull(options, "options cannot be null");
+    Class<? extends LineageBase> requestedType = 
options.as(LineageOptions.class).getLineageType();
+
+    Class<? extends LineageBase> currentType = CURRENT_LINEAGE_TYPE.get();
+    if (Objects.equals(currentType, requestedType) && SOURCES.get() != null) {
+      return;
+    }
+    if (CURRENT_LINEAGE_TYPE.compareAndSet(currentType, requestedType)) {

Review Comment:
   you are right. I have simplified it to a simple `synchronized` to avoid a 
*dirty state* when two init processes are racing. It will reinitialize Lineage 
metrics on every call to `setDefaultPipelineOptions`



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to