Repository navigation
[2.0] feat(data-insights): add the DataInsightsExtension hook (backport of #33079's hook) #37815
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
...penmetadata/service/apps/bundles/insights/workflows/dataAssets/DataInsightsExtension.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| package org.openmetadata.service.apps.bundles.insights.workflows.dataAssets; | ||
|
|
||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.concurrent.CancellationException; | ||
| import java.util.function.BooleanSupplier; | ||
| import org.openmetadata.schema.EntityInterface; | ||
| import org.openmetadata.schema.entity.applications.configuration.internal.DataAssetsConfig; | ||
| import org.openmetadata.service.jdbi3.CollectionDAO; | ||
| import org.openmetadata.service.search.SearchRepository; | ||
|
|
||
| /** Run-scoped additions to DI snapshots and consumers of a successfully indexed DI run. */ | ||
| public interface DataInsightsExtension { | ||
| Session open(RunContext context); | ||
|
|
||
| record RunContext( | ||
| String runId, | ||
| long capturedAt, | ||
| long snapshotTimestamp, | ||
| CollectionDAO collectionDAO, | ||
| SearchRepository searchRepository, | ||
| DataAssetsConfig configuration, | ||
| BooleanSupplier cancelled) { | ||
| public RunContext( | ||
| String runId, | ||
| long capturedAt, | ||
| long snapshotTimestamp, | ||
| CollectionDAO collectionDAO, | ||
| SearchRepository searchRepository, | ||
| DataAssetsConfig configuration) { | ||
| this( | ||
| runId, | ||
| capturedAt, | ||
| snapshotTimestamp, | ||
| collectionDAO, | ||
| searchRepository, | ||
| configuration, | ||
| () -> false); | ||
| } | ||
|
|
||
| public void requireActive() { | ||
| if (cancelled.getAsBoolean() || Thread.currentThread().isInterrupted()) { | ||
| throw new CancellationException( | ||
| "Data Insights run was cancelled or lost its execution lease"); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** Enrichment can run concurrently; completion runs once, while the DI job still owns its lease. */ | ||
| interface Session extends AutoCloseable { | ||
| /** Allows bounded bulk lookups before the batch's concurrent enrichment starts. */ | ||
| default void beforeBatch(List<? extends EntityInterface> entities) {} | ||
|
|
||
| default void enrich(Map<String, Object> snapshot) {} | ||
|
|
||
| default void complete() {} | ||
|
|
||
| @Override | ||
| default void close() {} | ||
| } | ||
| } |
81 changes: 81 additions & 0 deletions
81
...enmetadata/service/apps/bundles/insights/workflows/dataAssets/DataInsightsExtensions.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| package org.openmetadata.service.apps.bundles.insights.workflows.dataAssets; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.ServiceLoader; | ||
| import org.openmetadata.schema.EntityInterface; | ||
| import org.openmetadata.service.apps.bundles.insights.workflows.dataAssets.DataInsightsExtension.RunContext; | ||
| import org.openmetadata.service.apps.bundles.insights.workflows.dataAssets.DataInsightsExtension.Session; | ||
|
|
||
| /** Keeps extension state local to one DI run, including cleanup after startup or indexing failure. */ | ||
| public final class DataInsightsExtensions implements AutoCloseable { | ||
| private final RunContext context; | ||
| private final List<Session> sessions = new ArrayList<>(); | ||
|
|
||
| private DataInsightsExtensions(RunContext context) { | ||
| this.context = context; | ||
| } | ||
|
|
||
| public static DataInsightsExtensions open(RunContext context) { | ||
| return open(context, ServiceLoader.load(DataInsightsExtension.class)); | ||
| } | ||
|
|
||
| static DataInsightsExtensions open( | ||
| RunContext context, Iterable<DataInsightsExtension> extensions) { | ||
| DataInsightsExtensions run = new DataInsightsExtensions(context); | ||
| try { | ||
| for (DataInsightsExtension extension : extensions) { | ||
| Session session = extension.open(context); | ||
| if (session == null) { | ||
| throw new IllegalStateException( | ||
| "Data Insights extension %s returned a null session" | ||
| .formatted(extension.getClass().getName())); | ||
| } | ||
| run.sessions.add(session); | ||
| } | ||
| } catch (RuntimeException | Error failure) { | ||
| try { | ||
| run.close(); | ||
| } catch (RuntimeException cleanupFailure) { | ||
| failure.addSuppressed(cleanupFailure); | ||
| } | ||
| throw failure; | ||
| } | ||
| return run; | ||
| } | ||
|
|
||
| public void enrich(Map<String, Object> snapshot) { | ||
| context.requireActive(); | ||
| sessions.forEach(session -> session.enrich(snapshot)); | ||
| } | ||
|
|
||
| public void complete() { | ||
| context.requireActive(); | ||
| sessions.forEach(Session::complete); | ||
| } | ||
|
|
||
| public void beforeBatch(List<? extends EntityInterface> entities) { | ||
| context.requireActive(); | ||
| sessions.forEach(session -> session.beforeBatch(entities)); | ||
| } | ||
|
|
||
| @Override | ||
| public void close() { | ||
| RuntimeException failure = null; | ||
| for (Session session : sessions.reversed()) { | ||
| try { | ||
| session.close(); | ||
| } catch (RuntimeException exception) { | ||
| if (failure == null) { | ||
| failure = exception; | ||
| } else { | ||
| failure.addSuppressed(exception); | ||
| } | ||
| } | ||
| } | ||
| if (failure != null) { | ||
| throw failure; | ||
| } | ||
| } | ||
| } |
166 changes: 166 additions & 0 deletions
166
...tadata/service/apps/bundles/insights/workflows/dataAssets/DataInsightsExtensionsTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,166 @@ | ||
| package org.openmetadata.service.apps.bundles.insights.workflows.dataAssets; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertThrows; | ||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.HashMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.concurrent.CancellationException; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.openmetadata.service.apps.bundles.insights.workflows.dataAssets.DataInsightsExtension.RunContext; | ||
| import org.openmetadata.service.apps.bundles.insights.workflows.dataAssets.DataInsightsExtension.Session; | ||
|
|
||
| class DataInsightsExtensionsTest { | ||
| private static final RunContext CONTEXT = new RunContext("run", 2000, 1000, null, null, null); | ||
|
|
||
| @Test | ||
| void batchPreparationPrecedesEnrichmentAndCancellationStopsCompletion() { | ||
| var cancelled = new java.util.concurrent.atomic.AtomicBoolean(); | ||
| var context = new RunContext("run", 2000, 1000, null, null, null, cancelled::get); | ||
| List<String> calls = new ArrayList<>(); | ||
| DataInsightsExtension extension = | ||
| ignored -> | ||
| new Session() { | ||
| @Override | ||
| public void beforeBatch( | ||
| List<? extends org.openmetadata.schema.EntityInterface> entities) { | ||
| calls.add("batch"); | ||
| } | ||
|
|
||
| @Override | ||
| public void enrich(Map<String, Object> snapshot) { | ||
| calls.add("asset"); | ||
| } | ||
|
|
||
| @Override | ||
| public void complete() { | ||
| calls.add("complete"); | ||
| } | ||
| }; | ||
| try (var run = DataInsightsExtensions.open(context, List.of(extension))) { | ||
| run.beforeBatch(List.of()); | ||
| run.enrich(new HashMap<>()); | ||
| cancelled.set(true); | ||
| assertThrows(CancellationException.class, run::complete); | ||
| assertThrows(CancellationException.class, () -> run.enrich(new HashMap<>())); | ||
| assertEquals(List.of("batch", "asset"), calls); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| void startupAndCleanupFailuresAreBothPreserved() { | ||
| DataInsightsExtension opened = | ||
| context -> | ||
| new Session() { | ||
| @Override | ||
| public void close() { | ||
| throw new IllegalStateException("close"); | ||
| } | ||
| }; | ||
| DataInsightsExtension failed = | ||
| context -> { | ||
| throw new IllegalArgumentException("open"); | ||
| }; | ||
| var error = | ||
| assertThrows( | ||
| IllegalArgumentException.class, | ||
| () -> DataInsightsExtensions.open(CONTEXT, List.of(opened, failed))); | ||
| assertEquals("open", error.getMessage()); | ||
| assertEquals("close", error.getSuppressed()[0].getMessage()); | ||
| } | ||
|
|
||
| @Test | ||
| void nullSessionFailsFastWithProviderName() { | ||
| var error = | ||
| assertThrows( | ||
| IllegalStateException.class, | ||
| () -> DataInsightsExtensions.open(CONTEXT, List.of(new NullSessionExtension()))); | ||
|
|
||
| assertTrue(error.getMessage().contains(NullSessionExtension.class.getName())); | ||
| } | ||
|
|
||
| @Test | ||
| void onlySessionsWriteToTheSnapshotAndCompletionIsExplicit() { | ||
| List<String> events = new ArrayList<>(); | ||
| DataInsightsExtension extension = | ||
| context -> | ||
| new Session() { | ||
| @Override | ||
| public void enrich(Map<String, Object> snapshot) { | ||
| events.add("enriched"); | ||
| snapshot.put("added", true); | ||
| } | ||
|
|
||
| @Override | ||
| public void complete() { | ||
| events.add("completed"); | ||
| } | ||
|
|
||
| @Override | ||
| public void close() { | ||
| events.add("closed"); | ||
| } | ||
| }; | ||
| var snapshot = new HashMap<String, Object>(); | ||
| try (var run = DataInsightsExtensions.open(CONTEXT, List.of(extension))) { | ||
| run.enrich(snapshot); | ||
| assertEquals(List.of("enriched"), events); | ||
| run.complete(); | ||
| } | ||
|
|
||
| assertEquals(Map.of("added", true), snapshot); | ||
| assertEquals(List.of("enriched", "completed", "closed"), events); | ||
| } | ||
|
|
||
| @Test | ||
| void failedStartupClosesAlreadyOpenedSessions() { | ||
| List<String> events = new ArrayList<>(); | ||
| DataInsightsExtension opened = context -> closingSession(events, "first"); | ||
| DataInsightsExtension failed = | ||
| context -> { | ||
| throw new IllegalStateException("source failed"); | ||
| }; | ||
|
|
||
| assertThrows( | ||
| IllegalStateException.class, | ||
| () -> DataInsightsExtensions.open(CONTEXT, List.of(opened, failed))); | ||
| assertEquals(List.of("first"), events); | ||
| } | ||
|
|
||
| @Test | ||
| void failureToCloseOneSessionDoesNotLeakAnother() { | ||
| List<String> events = new ArrayList<>(); | ||
| DataInsightsExtension opened = context -> closingSession(events, "first"); | ||
| DataInsightsExtension failed = | ||
| context -> | ||
| new Session() { | ||
| @Override | ||
| public void close() { | ||
| throw new IllegalStateException("cleanup failed"); | ||
| } | ||
| }; | ||
| var run = DataInsightsExtensions.open(CONTEXT, List.of(opened, failed)); | ||
|
|
||
| assertThrows(IllegalStateException.class, run::close); | ||
| assertEquals(List.of("first"), events); | ||
| } | ||
|
|
||
| private static Session closingSession(List<String> events, String event) { | ||
| return new Session() { | ||
| @Override | ||
| public void close() { | ||
| events.add(event); | ||
| } | ||
| }; | ||
| } | ||
|
|
||
| private static final class NullSessionExtension implements DataInsightsExtension { | ||
| @Override | ||
| public Session open(RunContext context) { | ||
| return null; | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
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.
💡 Edge Case: Cancellation in beforeBatch skips the per-source stats update
extensions.beforeBatch(...)(line 316) callsrequireActive(), which throwsCancellationExceptionifstop()ran between batches or if the thread was left interrupted after theInterruptedExceptionbranch at line 372, which only breaks out of the current source. The inner try only catchesSearchIndexExceptionandInterruptedException, so the exception goes straight out ofprocessSource. That skips the trailingdrainAndFlush/updateWorkflowStats/mergeEnricherStepStatsblock (lines 381-386), andprocess()swallows the exception at line 247. Before this change, a stop inside a source still ended the loop normally and recorded that source's stats. With this change, they are lost on cancellation even when no extension is registered, sincerequireActive()runs no matter how many sessions exist. Fix: catchCancellationExceptioninside the batch loop (mark it stopped and break), or checkstoppedinstead of callingrequireActive()before the batch.Treat cancellation from the extension hook as a normal stop so the per-source finalisation still runs.:
Was this helpful? React with 👍 / 👎