-
Notifications
You must be signed in to change notification settings - Fork 4.5k
Implement pluggable Lineage in Java SDK #36781
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
base: master
Are you sure you want to change the base?
Changes from all commits
6b93302
9877c1e
87ef3f9
1cd1dc4
e6a4b9b
86f08c4
80560ad
f376e0c
e772e80
3272b85
1d051af
4799abe
b4039c8
16e8e1e
f61e592
7bcab65
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| /* | ||
| * 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.sdk.lineage; | ||
|
|
||
| import org.apache.beam.sdk.annotations.Internal; | ||
|
|
||
| /** | ||
| * Plugin interface for lineage implementations. | ||
| * | ||
| * <p>This is the core contract that lineage plugins must implement. Plugins should implement this | ||
| * interface and register via {@link LineageRegistrar}. | ||
| * | ||
| * <p>End users should use the {@link org.apache.beam.sdk.metrics.Lineage} facade class instead of | ||
| * implementing this interface directly. | ||
| */ | ||
| @Internal | ||
| public interface LineageBase { | ||
| /** | ||
| * 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. | ||
| */ | ||
| void add(Iterable<String> rollupSegments); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| /* | ||
| * 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.sdk.lineage; | ||
|
|
||
| import javax.annotation.Nullable; | ||
| import org.apache.beam.sdk.metrics.Lineage; | ||
| import org.apache.beam.sdk.options.PipelineOptions; | ||
|
|
||
| /** Interface for discovering and creating lineage plugin implementations. */ | ||
| public interface LineageRegistrar { | ||
|
|
||
| @Nullable | ||
| LineageBase fromOptions(PipelineOptions options, Lineage.LineageDirection direction); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| /* | ||
| * 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. | ||
| */ | ||
| /** | ||
| * Lineage tracking support for Apache Beam pipelines. | ||
| * | ||
| * <p>This package provides a plugin mechanism to support different lineage implementations through | ||
| * the {@link org.apache.beam.sdk.lineage.LineageRegistrar} interface. Lineage implementations can | ||
| * be registered and discovered at runtime to track data lineage information during pipeline | ||
| * execution. | ||
| * | ||
| * <p>For lineage capabilities, see {@link org.apache.beam.sdk.metrics.Lineage}. | ||
| */ | ||
| package org.apache.beam.sdk.lineage; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,51 +17,136 @@ | |
| */ | ||
| 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.ServiceLoader; | ||
| 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.LineageRegistrar; | ||
| 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.sdk.util.common.ReflectHelpers; | ||
| import org.apache.beam.sdk.values.KV; | ||
| 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.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.collect.Lists; | ||
| import org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.collect.Sets; | ||
| 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 final 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<KV<Long, Integer>> LINEAGE_REVISION = | ||
| 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"); | ||
| long optionsId = options.getOptionsId(); | ||
| int nextRevision = options.revision(); | ||
|
|
||
| while (true) { | ||
| KV<Long, Integer> currentRevision = LINEAGE_REVISION.get(); | ||
|
|
||
| if (currentRevision != null | ||
| && currentRevision.getKey().equals(optionsId) | ||
| && currentRevision.getValue() >= nextRevision) { | ||
| LOG.debug( | ||
| "Lineage already initialized with options ID {} revision {}, skipping", | ||
| optionsId, | ||
| currentRevision.getValue()); | ||
| return; | ||
| } | ||
|
|
||
| if (LINEAGE_REVISION.compareAndSet(currentRevision, KV.of(optionsId, nextRevision))) { | ||
| Lineage sources = createLineage(options, LineageDirection.SOURCE); | ||
| Lineage sinks = createLineage(options, LineageDirection.SINK); | ||
|
|
||
| SOURCES.set(sources); | ||
| SINKS.set(sinks); | ||
|
|
||
| if (currentRevision == null) { | ||
| LOG.info("Lineage initialized with options ID {} revision {}", optionsId, nextRevision); | ||
| } else { | ||
| LOG.info( | ||
| "Lineage re-initialized from options ID {} to {} (revision {} -> {})", | ||
| currentRevision.getKey(), | ||
| optionsId, | ||
| currentRevision.getValue(), | ||
| nextRevision); | ||
| } | ||
| return; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private static Lineage createLineage(PipelineOptions options, LineageDirection direction) { | ||
| Set<LineageRegistrar> registrars = | ||
| Sets.newTreeSet(ReflectHelpers.ObjectsClassComparator.INSTANCE); | ||
| registrars.addAll( | ||
| Lists.newArrayList( | ||
| ServiceLoader.load(LineageRegistrar.class, ReflectHelpers.findClassLoader()))); | ||
|
|
||
| for (LineageRegistrar registrar : registrars) { | ||
| LineageBase reporter = registrar.fromOptions(options, direction); | ||
| if (reporter != null) { | ||
| LOG.info("Using {} for lineage direction {}", reporter.getClass().getName(), direction); | ||
| return new Lineage(reporter); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If multiple registrars in classpath, the activated one is the alphabetically smallest one (for Tree Set). This could be confusing. Consider using a pipeline option to select a Lineage implementation. |
||
| } | ||
| } | ||
|
|
||
| LOG.debug("Using default Metrics-based lineage for direction {}", direction); | ||
| return new Lineage(new MetricsLineage(direction)); | ||
| } | ||
|
|
||
| /** {@link Lineage} representing sources and optionally side inputs. */ | ||
| public static Lineage getSources() { | ||
| return SOURCES; | ||
| Lineage sources = SOURCES.get(); | ||
| if (sources == null) { | ||
| setDefaultPipelineOptions(PipelineOptionsFactory.create()); | ||
| sources = SOURCES.get(); | ||
| } | ||
| return sources; | ||
| } | ||
|
|
||
| /** {@link Lineage} representing sinks. */ | ||
| public static Lineage getSinks() { | ||
| return SINKS; | ||
| Lineage sinks = SINKS.get(); | ||
| if (sinks == null) { | ||
| setDefaultPipelineOptions(PipelineOptionsFactory.create()); | ||
| sinks = SINKS.get(); | ||
| } | ||
| return sinks; | ||
| } | ||
|
|
||
| @VisibleForTesting | ||
|
|
@@ -140,12 +225,7 @@ public void add(String system, Iterable<String> segments) { | |
| * <p>In particular, this means they will often have trailing delimiters. | ||
| */ | ||
| public void add(Iterable<String> rollupSegments) { | ||
| ImmutableList<String> segments = ImmutableList.copyOf(rollupSegments); | ||
| if (MetricsFlag.lineageRollupEnabled()) { | ||
| ((BoundedTrie) this.metric).add(segments); | ||
| } else { | ||
| ((StringSet) this.metric).add(String.join("", segments)); | ||
| } | ||
| delegate.add(rollupSegments); | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -156,6 +236,8 @@ public void add(Iterable<String> rollupSegments) { | |
| * @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. | ||
| * <p>NOTE: When using a custom Lineage plugin, this method will return empty results since | ||
| * lineage is not stored in Metrics. | ||
| */ | ||
| public static Set<String> query(MetricResults results, Type type, String truncatedMarker) { | ||
| MetricQueryResults lineageQueryResults = getLineageQueryResults(results, type); | ||
|
|
@@ -184,6 +266,8 @@ public static Set<String> query(MetricResults results, Type type, String truncat | |
| * @param results FQNs from the result | ||
| * @param type sources or sinks | ||
| * @return A flat representation of all FQNs. If the FQN was truncated then it has a trailing '*'. | ||
| * <p>NOTE: When using a custom Lineage plugin, this method will return empty results since | ||
| * lineage is not stored in Metrics. | ||
| */ | ||
| public static Set<String> query(MetricResults results, Type type) { | ||
| if (MetricsFlag.lineageRollupEnabled()) { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| /* | ||
| * 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.sdk.metrics; | ||
|
|
||
| import org.apache.beam.sdk.lineage.LineageBase; | ||
| import org.apache.beam.sdk.metrics.Metrics.MetricsFlag; | ||
| import org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.collect.ImmutableList; | ||
|
|
||
| /** | ||
| * Default lineage implementation that stores lineage information in Beam metrics. | ||
| * | ||
| * <p>This implementation uses either {@link BoundedTrie} or {@link StringSet} metrics depending on | ||
| * the {@link MetricsFlag#lineageRollupEnabled()} flag. | ||
| */ | ||
| public class MetricsLineage implements LineageBase { | ||
|
|
||
| private final Metric metric; | ||
|
|
||
| public MetricsLineage(final Lineage.LineageDirection direction) { | ||
| // Derive Metrics-specific Type from LineageDirection | ||
| Lineage.Type type = | ||
| (direction == Lineage.LineageDirection.SOURCE) ? Lineage.Type.SOURCE : Lineage.Type.SINK; | ||
|
|
||
| if (MetricsFlag.lineageRollupEnabled()) { | ||
| this.metric = | ||
| Metrics.boundedTrie( | ||
| Lineage.LINEAGE_NAMESPACE, | ||
| direction == Lineage.LineageDirection.SOURCE | ||
| ? Lineage.Type.SOURCEV2.toString() | ||
| : Lineage.Type.SINKV2.toString()); | ||
| } else { | ||
| this.metric = Metrics.stringSet(Lineage.LINEAGE_NAMESPACE, type.toString()); | ||
| } | ||
| } | ||
|
Comment on lines
+34
to
+49
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The constructor logic can be simplified for better readability. The public MetricsLineage(Lineage.LineageDirection direction) {
if (MetricsFlag.lineageRollupEnabled()) {
Lineage.Type metricType =
(direction == Lineage.LineageDirection.SOURCE)
? Lineage.Type.SOURCEV2
: Lineage.Type.SINKV2;
this.metric = Metrics.boundedTrie(Lineage.LINEAGE_NAMESPACE, metricType.toString());
} else {
Lineage.Type metricType =
(direction == Lineage.LineageDirection.SOURCE) ? Lineage.Type.SOURCE : Lineage.Type.SINK;
this.metric = Metrics.stringSet(Lineage.LINEAGE_NAMESPACE, metricType.toString());
}
} |
||
|
|
||
| @Override | ||
| public void add(final Iterable<String> rollupSegments) { | ||
| ImmutableList<String> segments = ImmutableList.copyOf(rollupSegments); | ||
| if (MetricsFlag.lineageRollupEnabled()) { | ||
| ((BoundedTrie) this.metric).add(segments); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was an assumption (and coupling) between lineageRollupEnabled<->BoundedTrie. With common interface we can now avoid unchecked cast by implementing CounterMetricsLineage and TrieMetricsLineage separately. |
||
| } else { | ||
| ((StringSet) this.metric).add(String.join("", segments)); | ||
| } | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FYI
We are using
Lineageas a facade aroundLineageBase, so we don't expose the latter and we avoid any (breaking) changes in other parts of Beam