Skip to content
This repository has been archived by the owner on Aug 2, 2022. It is now read-only.

Improve profile API's error fetching efficiency #117

Merged

Conversation

kaituo
Copy link
Member

@kaituo kaituo commented May 8, 2020

Issue #, if available:
#111

Description of changes:

Previously, profile API scans all anomaly result indices to get a detector's most recent error, which can cause performance bottleneck with large anomaly result indices. This PR improves this aspect via various efforts.

First, when a detector is running, we only need to scan the current index, not all of the rolled over ones since we are interested in the latest error.
Second, when a detector is disabled, we only need to scan the latest anomaly result indices created before the detector's disabled time.
Third, setting track total hits false makes ES terminate search early. ES will not try to count the number of documents and will be able to end the query as soon as N document have been collected per segment.

Testing done:

  1. patched a cluster with 1,000 detectors and 2GB anomaly result indices. Without the PR, scanning anomaly result indices 1000 times would timeout after 30 seconds. After the PR, we would not see the timeout.
  2. A detector's error message can be on a rotated index. Adds a test case to makes sure we get error info from .opendistro-anomaly-results index that has been rolled over.

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

Previously, profile API scans all anomaly result indices to get a detector's most recent error, which can cause performance bottleneck with large anomaly result indices. This PR improves this aspect via various efforts.

First, when a detector is running, we only need to scan the current index, not all of the rolled over ones since we are interested in the latest error.
Second, when a detector is disabled, we only need to scan the latest anomaly result indices created before the detector's enable time.
Third, setting track total hits false makes ES terminate search early. ES will not try to count the number of documents and will be able to end the query as soon as N document have been collected per segment.

Testing done:
1. patched a cluster with 1,000 detectors and 2GB anomaly result indices. Without the PR, scanning anomaly result indices 1000 times would timeout after 30 seconds. After the PR, we would not see the timeout.
2. A detector's error message can be on a rotated index. Adds a test case to makes sure we get error info from .opendistro-anomaly-results index that has been rolled over.
int date = Integer.parseInt(m.group(3));
// month starts with 0
calendar.clear();
calendar.set(year, month - 1, date);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will Jan. cause any issue? I guess in case of Jan., month is 1, not sure if this can cause any issue

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would suggest you use current year/month/date to initialize calendar, and do calendar.add(Calendar.MONTH, -1) instead.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will Jan. cause any issue? I guess in case of Jan., month is 1, not sure if this can cause any issue

just checked java doc. month for Jan is 0 here.

// time
if (timestamp <= disabledTimeMillis && maxTimestamp <= timestamp) {
maxTimestamp = timestamp;
// we can have two rotations on the same day and we don't know which one has our data, so we keep all
Copy link
Contributor

@ylwu-amzn ylwu-amzn May 8, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One edge case: suppose detector interval is 1 minute
1.Detector last run is at 2020-05-07, 11:59:50 PM, then AD result indices rolled over as .opendistro-anomaly-results-history-2020.05.07-001
2.Detector next run will be 2020-05-08, 00:00:50 AM. If user stop the detector at 2020-05-08 00:00:10 AM, detector will not have AD result on 2020-05-08.
So this code change will check latest AD result index on 2020-05-08, as 2020-05-08 <= 2020-05-08 00:00:10 AM(disabledTime). But we can't find any AD result for this detector on 2020-05-08. How about we check last two days' AD result indices to make sure we can always get AD result? Similar to set monitor interval as 2*detector_interval

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. Changed.

Copy link
Contributor

@ohltyler ohltyler left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great, LGTM

Copy link
Contributor

@ylwu-amzn ylwu-amzn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Thanks for the change.

@kaituo kaituo merged commit a40ccf6 into opendistro-for-elasticsearch:opendistro-1.4 May 9, 2020
kaituo added a commit to kaituo/anomaly-detection that referenced this pull request May 19, 2020
…icsearch#117)

Previously, profile API scans all anomaly result indices to get a detector's most recent error, which can cause performance bottleneck with large anomaly result indices. This PR improves this aspect via various efforts.

First, when a detector is running, we only need to scan the current index, not all of the rolled over ones since we are interested in the latest error.
Second, when a detector is disabled, we only need to scan the latest anomaly result indices created before the detector's enable time.
Third, setting track total hits false makes ES terminate search early. ES will not try to count the number of documents and will be able to end the query as soon as N document have been collected per segment.

Testing done:
1. patched a cluster with 1,000 detectors and 2GB anomaly result indices. Without the PR, scanning anomaly result indices 1000 times would timeout after 30 seconds. After the PR, we would not see the timeout.
2. A detector's error message can be on a rotated index. Adds a test case to makes sure we get error info from .opendistro-anomaly-results index that has been rolled over.
kaituo added a commit that referenced this pull request May 20, 2020
* Add shingle size, total model size, and model's hash ring to profile API (#113)

Hash ring helps identify node X runs the AD job for a detector Y with models on node 1,2,3.  This helps oncalls locate logs. Total model size gives transparency relating to the current memory usage. What's more, shingle size help answer question "why my detector does not report anything?"

This PR adds the above info to profile API via a broadcast call that consults ModelManager and FeatureManager about current state pertaining to a detector.  Then these states are consolidated into information humans can parse.

This PR also queries all AD result indices instead of only current result index so that we can fetch a stopped detector's error after the result index with the error is rotated.

Testing done:
1. add unit tests for the newly added code
2. Run end-to-end testing to verify new profiles make senses when a detector stops running and is running

* Fix bug in profile API (#115)

DetectorProfile's merge does not include new fields added.  This PR fixes that.

Testing done:
* Manually verified profile API works as expected

* Improve profile API's error fetching efficiency (#117)

Previously, profile API scans all anomaly result indices to get a detector's most recent error, which can cause performance bottleneck with large anomaly result indices. This PR improves this aspect via various efforts.

First, when a detector is running, we only need to scan the current index, not all of the rolled over ones since we are interested in the latest error.
Second, when a detector is disabled, we only need to scan the latest anomaly result indices created before the detector's enable time.
Third, setting track total hits false makes ES terminate search early. ES will not try to count the number of documents and will be able to end the query as soon as N document have been collected per segment.

Testing done:
1. patched a cluster with 1,000 detectors and 2GB anomaly result indices. Without the PR, scanning anomaly result indices 1000 times would timeout after 30 seconds. After the PR, we would not see the timeout.
2. A detector's error message can be on a rotated index. Adds a test case to makes sure we get error info from .opendistro-anomaly-results index that has been rolled over.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants