-
Notifications
You must be signed in to change notification settings - Fork 25.6k
[ML] Refactor data extraction logic and improve cleanup handling #138060
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
Open
valeriy42
wants to merge
6
commits into
elastic:main
Choose a base branch
from
valeriy42:tests/is-84268
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+228
−124
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
1a7472c
Refactor data extraction logic and improve cleanup handling
valeriy42 056a804
Add busy wait instead of waiting for context baseline.
valeriy42 9ea0115
declare lambda method throw exception
valeriy42 2dd5e8a
Enhance DatafeedCcsIT cleanup logic
valeriy42 3c2adae
Refine DatafeedCcsIT cleanup and context handling
valeriy42 35d33db
Improve error handling in DatafeedCcsIT during context cleanup
valeriy42 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
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 |
|---|---|---|
|
|
@@ -363,112 +363,117 @@ private void run(long start, long end, FlushJobAction.Request flushRequest) { | |
|
|
||
| long recordCount = 0; | ||
| DataExtractor dataExtractor = dataExtractorFactory.newExtractor(start, end); | ||
| while (dataExtractor.hasNext()) { | ||
| if ((isIsolated || isRunning() == false) && dataExtractor.isCancelled() == false) { | ||
| dataExtractor.cancel(); | ||
| } | ||
| if (isIsolated) { | ||
| return; | ||
| } | ||
|
|
||
| Optional<InputStream> extractedData; | ||
| try { | ||
| DataExtractor.Result result = dataExtractor.next(); | ||
| extractedData = result.data(); | ||
| searchInterval = result.searchInterval(); | ||
| } catch (Exception e) { | ||
| LOGGER.warn(() -> "[" + jobId + "] error while extracting data", e); | ||
| // When extraction problems are encountered, we do not want to advance time. | ||
| // Instead, it is preferable to retry the given interval next time an extraction | ||
| // is triggered. | ||
|
|
||
| // For aggregated datafeeds it is possible for our users to use fields without doc values. | ||
| // In that case, it is really useful to display an error message explaining exactly that. | ||
| // Unfortunately, there are no great ways to identify the issue but search for 'doc values' | ||
| // deep in the exception. | ||
| if (e.toString().contains("doc values")) { | ||
| throw new ExtractionProblemException( | ||
| nextRealtimeTimestamp(), | ||
| new IllegalArgumentException( | ||
| "One or more fields do not have doc values; please enable doc values for all analysis fields for datafeeds" | ||
| + " using aggregations" | ||
| ) | ||
| ); | ||
| try { | ||
| while (dataExtractor.hasNext()) { | ||
| if ((isIsolated || isRunning() == false) && dataExtractor.isCancelled() == false) { | ||
| dataExtractor.cancel(); | ||
| } | ||
| throw new ExtractionProblemException(nextRealtimeTimestamp(), e); | ||
| } | ||
| if (isIsolated) { | ||
| return; | ||
| } | ||
| if (extractedData.isPresent()) { | ||
| DataCounts counts; | ||
| try (InputStream in = extractedData.get()) { | ||
| counts = postData(in, XContentType.JSON); | ||
| LOGGER.trace( | ||
| () -> format( | ||
| "[%s] Processed another %s records with latest timestamp [%s]", | ||
| jobId, | ||
| counts.getProcessedRecordCount(), | ||
| counts.getLatestRecordTimeStamp() | ||
| ) | ||
| ); | ||
| timingStatsReporter.reportDataCounts(counts); | ||
| if (isIsolated) { | ||
| return; | ||
| } | ||
|
|
||
| Optional<InputStream> extractedData; | ||
| try { | ||
| DataExtractor.Result result = dataExtractor.next(); | ||
| extractedData = result.data(); | ||
| searchInterval = result.searchInterval(); | ||
| } catch (Exception e) { | ||
| if (e instanceof InterruptedException) { | ||
| Thread.currentThread().interrupt(); | ||
| LOGGER.warn(() -> "[" + jobId + "] error while extracting data", e); | ||
| // When extraction problems are encountered, we do not want to advance time. | ||
| // Instead, it is preferable to retry the given interval next time an extraction | ||
| // is triggered. | ||
|
|
||
| // For aggregated datafeeds it is possible for our users to use fields without doc values. | ||
| // In that case, it is really useful to display an error message explaining exactly that. | ||
| // Unfortunately, there are no great ways to identify the issue but search for 'doc values' | ||
| // deep in the exception. | ||
| if (e.toString().contains("doc values")) { | ||
| throw new ExtractionProblemException( | ||
| nextRealtimeTimestamp(), | ||
| new IllegalArgumentException( | ||
| "One or more fields do not have doc values; please enable doc values for all analysis fields for datafeeds" | ||
| + " using aggregations" | ||
| ) | ||
| ); | ||
| } | ||
| if (isIsolated) { | ||
| return; | ||
| } | ||
| LOGGER.error(() -> "[" + jobId + "] error while posting data", e); | ||
|
|
||
| // a conflict exception means the job state is not open any more. | ||
| // we should therefore stop the datafeed. | ||
| boolean shouldStop = isConflictException(e); | ||
|
|
||
| // When an analysis problem occurs, it means something catastrophic has | ||
| // happened to the c++ process. We sent a batch of data to the c++ process | ||
| // yet we do not know how many of those were processed. It is better to | ||
| // advance time in order to avoid importing duplicate data. | ||
| error = new AnalysisProblemException(nextRealtimeTimestamp(), shouldStop, e); | ||
| break; | ||
| throw new ExtractionProblemException(nextRealtimeTimestamp(), e); | ||
| } | ||
| recordCount += counts.getProcessedRecordCount(); | ||
| haveEverSeenData |= (recordCount > 0); | ||
| if (counts.getLatestRecordTimeStamp() != null) { | ||
| lastEndTimeMs = counts.getLatestRecordTimeStamp().getTime(); | ||
| if (isIsolated) { | ||
| return; | ||
| } | ||
| if (extractedData.isPresent()) { | ||
| DataCounts counts; | ||
| try (InputStream in = extractedData.get()) { | ||
| counts = postData(in, XContentType.JSON); | ||
| LOGGER.trace( | ||
| () -> format( | ||
| "[%s] Processed another %s records with latest timestamp [%s]", | ||
| jobId, | ||
| counts.getProcessedRecordCount(), | ||
| counts.getLatestRecordTimeStamp() | ||
| ) | ||
| ); | ||
| timingStatsReporter.reportDataCounts(counts); | ||
| } catch (Exception e) { | ||
| if (e instanceof InterruptedException) { | ||
| Thread.currentThread().interrupt(); | ||
| } | ||
| if (isIsolated) { | ||
| return; | ||
| } | ||
| LOGGER.error(() -> "[" + jobId + "] error while posting data", e); | ||
|
|
||
| // a conflict exception means the job state is not open any more. | ||
| // we should therefore stop the datafeed. | ||
| boolean shouldStop = isConflictException(e); | ||
|
|
||
| // When an analysis problem occurs, it means something catastrophic has | ||
| // happened to the c++ process. We sent a batch of data to the c++ process | ||
| // yet we do not know how many of those were processed. It is better to | ||
| // advance time in order to avoid importing duplicate data. | ||
| error = new AnalysisProblemException(nextRealtimeTimestamp(), shouldStop, e); | ||
| break; | ||
| } | ||
| recordCount += counts.getProcessedRecordCount(); | ||
| haveEverSeenData |= (recordCount > 0); | ||
| if (counts.getLatestRecordTimeStamp() != null) { | ||
| lastEndTimeMs = counts.getLatestRecordTimeStamp().getTime(); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| lastEndTimeMs = Math.max(lastEndTimeMs == null ? 0 : lastEndTimeMs, dataExtractor.getEndTime() - 1); | ||
| LOGGER.debug( | ||
| "[{}] Complete iterating data extractor [{}], [{}], [{}], [{}], [{}]", | ||
| jobId, | ||
| error, | ||
| recordCount, | ||
| lastEndTimeMs, | ||
| isRunning(), | ||
| dataExtractor.isCancelled() | ||
| ); | ||
|
|
||
| // We can now throw any stored error as we have updated time. | ||
| if (error != null) { | ||
| throw error; | ||
| } | ||
| lastEndTimeMs = Math.max(lastEndTimeMs == null ? 0 : lastEndTimeMs, dataExtractor.getEndTime() - 1); | ||
| LOGGER.debug( | ||
| "[{}] Complete iterating data extractor [{}], [{}], [{}], [{}], [{}]", | ||
| jobId, | ||
| error, | ||
| recordCount, | ||
| lastEndTimeMs, | ||
| isRunning(), | ||
| dataExtractor.isCancelled() | ||
| ); | ||
|
|
||
| // We can now throw any stored error as we have updated time. | ||
| if (error != null) { | ||
| throw error; | ||
| } | ||
|
|
||
| // If the datafeed was stopped, then it is possible that by the time | ||
| // we call flush the job is closed. Thus, we don't flush unless the | ||
| // datafeed is still running. | ||
| if (isRunning() && isIsolated == false) { | ||
| Instant lastFinalizedBucketEnd = flushJob(flushRequest).getLastFinalizedBucketEnd(); | ||
| if (lastFinalizedBucketEnd != null) { | ||
| this.latestFinalBucketEndTimeMs = lastFinalizedBucketEnd.toEpochMilli(); | ||
| // If the datafeed was stopped, then it is possible that by the time | ||
| // we call flush the job is closed. Thus, we don't flush unless the | ||
| // datafeed is still running. | ||
| if (isRunning() && isIsolated == false) { | ||
| Instant lastFinalizedBucketEnd = flushJob(flushRequest).getLastFinalizedBucketEnd(); | ||
| if (lastFinalizedBucketEnd != null) { | ||
| this.latestFinalBucketEndTimeMs = lastFinalizedBucketEnd.toEpochMilli(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| if (recordCount == 0) { | ||
| throw new EmptyDataCountException(nextRealtimeTimestamp(), haveEverSeenData); | ||
| if (recordCount == 0) { | ||
| throw new EmptyDataCountException(nextRealtimeTimestamp(), haveEverSeenData); | ||
| } | ||
| } finally { | ||
| // Ensure the extractor is always destroyed to clean up scroll contexts | ||
| dataExtractor.destroy(); | ||
|
Comment on lines
+474
to
+476
Contributor
Author
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. This is the block I actually added |
||
| } | ||
| } | ||
|
|
||
|
|
||
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.
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.
Wrapped DataExtractor usage in a try-finally block so
destroy()is always called, including on early returns due to isolation.