|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License; |
| 4 | + * you may not use this file except in compliance with the Elastic License. |
| 5 | + */ |
| 6 | +package org.elasticsearch.xpack.ml.integration; |
| 7 | + |
| 8 | +import org.apache.logging.log4j.Logger; |
| 9 | +import org.elasticsearch.action.bulk.BulkItemResponse; |
| 10 | +import org.elasticsearch.action.bulk.BulkRequestBuilder; |
| 11 | +import org.elasticsearch.action.bulk.BulkResponse; |
| 12 | +import org.elasticsearch.action.index.IndexRequest; |
| 13 | +import org.elasticsearch.action.support.WriteRequest; |
| 14 | +import org.elasticsearch.common.unit.TimeValue; |
| 15 | +import org.elasticsearch.index.query.RangeQueryBuilder; |
| 16 | +import org.elasticsearch.search.aggregations.AggregationBuilders; |
| 17 | +import org.elasticsearch.search.aggregations.AggregatorFactories; |
| 18 | +import org.elasticsearch.search.aggregations.metrics.avg.AvgAggregationBuilder; |
| 19 | +import org.elasticsearch.search.aggregations.metrics.max.MaxAggregationBuilder; |
| 20 | +import org.elasticsearch.xpack.core.ml.action.GetBucketsAction; |
| 21 | +import org.elasticsearch.xpack.core.ml.action.util.PageParams; |
| 22 | +import org.elasticsearch.xpack.core.ml.datafeed.DatafeedConfig; |
| 23 | +import org.elasticsearch.xpack.core.ml.job.config.AnalysisConfig; |
| 24 | +import org.elasticsearch.xpack.core.ml.job.config.DataDescription; |
| 25 | +import org.elasticsearch.xpack.core.ml.job.config.Detector; |
| 26 | +import org.elasticsearch.xpack.core.ml.job.config.Job; |
| 27 | +import org.elasticsearch.xpack.core.ml.job.results.Bucket; |
| 28 | +import org.elasticsearch.xpack.core.ml.job.results.Result; |
| 29 | +import org.elasticsearch.xpack.ml.datafeed.DelayedDataDetector; |
| 30 | +import org.elasticsearch.xpack.ml.datafeed.DelayedDataDetector.BucketWithMissingData; |
| 31 | +import org.junit.After; |
| 32 | +import org.junit.Before; |
| 33 | + |
| 34 | +import java.util.Collections; |
| 35 | +import java.util.Date; |
| 36 | +import java.util.List; |
| 37 | + |
| 38 | +import static org.elasticsearch.xpack.ml.support.BaseMlIntegTestCase.createDatafeed; |
| 39 | +import static org.elasticsearch.xpack.ml.support.BaseMlIntegTestCase.createDatafeedBuilder; |
| 40 | +import static org.hamcrest.Matchers.equalTo; |
| 41 | + |
| 42 | +public class DelayedDataDetectorIT extends MlNativeAutodetectIntegTestCase { |
| 43 | + |
| 44 | + private String index = "delayed-data"; |
| 45 | + private long now = System.currentTimeMillis(); |
| 46 | + private long numDocs; |
| 47 | + |
| 48 | + @Before |
| 49 | + public void putDataintoIndex() { |
| 50 | + client().admin().indices().prepareCreate(index) |
| 51 | + .addMapping("type", "time", "type=date", "value", "type=long") |
| 52 | + .get(); |
| 53 | + numDocs = randomIntBetween(32, 128); |
| 54 | + long oneDayAgo = now - 86400000; |
| 55 | + writeData(logger, index, numDocs, oneDayAgo, now); |
| 56 | + } |
| 57 | + |
| 58 | + @After |
| 59 | + public void cleanUpTest() { |
| 60 | + cleanUp(); |
| 61 | + } |
| 62 | + |
| 63 | + public void testMissingDataDetection() throws Exception { |
| 64 | + final String jobId = "delayed-data-detection-job"; |
| 65 | + Job.Builder job = createJob(jobId, TimeValue.timeValueMinutes(5), "count", null); |
| 66 | + |
| 67 | + DatafeedConfig datafeedConfig = createDatafeed(job.getId() + "-datafeed", job.getId(), Collections.singletonList(index)); |
| 68 | + registerJob(job); |
| 69 | + putJob(job); |
| 70 | + openJob(job.getId()); |
| 71 | + |
| 72 | + registerDatafeed(datafeedConfig); |
| 73 | + putDatafeed(datafeedConfig); |
| 74 | + startDatafeed(datafeedConfig.getId(), 0L, now); |
| 75 | + waitUntilJobIsClosed(jobId); |
| 76 | + |
| 77 | + // Get the latest finalized bucket |
| 78 | + Bucket lastBucket = getLatestFinalizedBucket(jobId); |
| 79 | + |
| 80 | + DelayedDataDetector delayedDataDetector = |
| 81 | + new DelayedDataDetector(job.build(new Date()), datafeedConfig, TimeValue.timeValueHours(12), client()); |
| 82 | + |
| 83 | + List<BucketWithMissingData> response = delayedDataDetector.detectMissingData(lastBucket.getEpoch()*1000); |
| 84 | + assertThat(response.stream().mapToLong(BucketWithMissingData::getMissingDocumentCount).sum(), equalTo(0L)); |
| 85 | + |
| 86 | + long missingDocs = randomIntBetween(32, 128); |
| 87 | + // Simply adding data within the current delayed data detection, the choice of 43100000 is arbitrary and within the window |
| 88 | + // for the DelayedDataDetector |
| 89 | + writeData(logger, index, missingDocs, now - 43100000, lastBucket.getEpoch()*1000); |
| 90 | + |
| 91 | + response = delayedDataDetector.detectMissingData(lastBucket.getEpoch()*1000); |
| 92 | + assertThat(response.stream().mapToLong(BucketWithMissingData::getMissingDocumentCount).sum(), equalTo(missingDocs)); |
| 93 | + } |
| 94 | + |
| 95 | + public void testMissingDataDetectionInSpecificBucket() throws Exception { |
| 96 | + final String jobId = "delayed-data-detection-job-missing-test-specific-bucket"; |
| 97 | + Job.Builder job = createJob(jobId, TimeValue.timeValueMinutes(5), "count", null); |
| 98 | + |
| 99 | + DatafeedConfig datafeedConfig = createDatafeed(job.getId() + "-datafeed", job.getId(), Collections.singletonList(index)); |
| 100 | + registerJob(job); |
| 101 | + putJob(job); |
| 102 | + openJob(job.getId()); |
| 103 | + |
| 104 | + registerDatafeed(datafeedConfig); |
| 105 | + putDatafeed(datafeedConfig); |
| 106 | + |
| 107 | + startDatafeed(datafeedConfig.getId(), 0L, now); |
| 108 | + waitUntilJobIsClosed(jobId); |
| 109 | + |
| 110 | + // Get the latest finalized bucket |
| 111 | + Bucket lastBucket = getLatestFinalizedBucket(jobId); |
| 112 | + |
| 113 | + DelayedDataDetector delayedDataDetector = |
| 114 | + new DelayedDataDetector(job.build(new Date()), datafeedConfig, TimeValue.timeValueHours(12), client()); |
| 115 | + |
| 116 | + long missingDocs = randomIntBetween(1, 10); |
| 117 | + |
| 118 | + // Write our missing data in the bucket right before the last finalized bucket |
| 119 | + writeData(logger, index, missingDocs, (lastBucket.getEpoch() - lastBucket.getBucketSpan())*1000, lastBucket.getEpoch()*1000); |
| 120 | + List<BucketWithMissingData> response = delayedDataDetector.detectMissingData(lastBucket.getEpoch()*1000); |
| 121 | + |
| 122 | + boolean hasBucketWithMissing = false; |
| 123 | + for (BucketWithMissingData bucketWithMissingData : response) { |
| 124 | + if (bucketWithMissingData.getBucket().getEpoch() == lastBucket.getEpoch() - lastBucket.getBucketSpan()) { |
| 125 | + assertThat(bucketWithMissingData.getMissingDocumentCount(), equalTo(missingDocs)); |
| 126 | + hasBucketWithMissing = true; |
| 127 | + } |
| 128 | + } |
| 129 | + assertThat(hasBucketWithMissing, equalTo(true)); |
| 130 | + } |
| 131 | + |
| 132 | + public void testMissingDataDetectionWithAggregationsAndQuery() throws Exception { |
| 133 | + TimeValue bucketSpan = TimeValue.timeValueMinutes(10); |
| 134 | + final String jobId = "delayed-data-detection-job-aggs-no-missing-test"; |
| 135 | + Job.Builder job = createJob(jobId, bucketSpan, "mean", "value", "doc_count"); |
| 136 | + |
| 137 | + MaxAggregationBuilder maxTime = AggregationBuilders.max("time").field("time"); |
| 138 | + AvgAggregationBuilder avgAggregationBuilder = AggregationBuilders.avg("value").field("value"); |
| 139 | + DatafeedConfig.Builder datafeedConfigBuilder = createDatafeedBuilder(job.getId() + "-datafeed", |
| 140 | + job.getId(), |
| 141 | + Collections.singletonList(index)); |
| 142 | + datafeedConfigBuilder.setAggregations(new AggregatorFactories.Builder().addAggregator( |
| 143 | + AggregationBuilders.histogram("time") |
| 144 | + .subAggregation(maxTime) |
| 145 | + .subAggregation(avgAggregationBuilder) |
| 146 | + .field("time") |
| 147 | + .interval(TimeValue.timeValueMinutes(5).millis()))); |
| 148 | + datafeedConfigBuilder.setQuery(new RangeQueryBuilder("value").gte(numDocs/2)); |
| 149 | + datafeedConfigBuilder.setFrequency(TimeValue.timeValueMinutes(5)); |
| 150 | + DatafeedConfig datafeedConfig = datafeedConfigBuilder.build(); |
| 151 | + registerJob(job); |
| 152 | + putJob(job); |
| 153 | + openJob(job.getId()); |
| 154 | + |
| 155 | + registerDatafeed(datafeedConfig); |
| 156 | + putDatafeed(datafeedConfig); |
| 157 | + startDatafeed(datafeedConfig.getId(), 0L, now); |
| 158 | + waitUntilJobIsClosed(jobId); |
| 159 | + |
| 160 | + // Get the latest finalized bucket |
| 161 | + Bucket lastBucket = getLatestFinalizedBucket(jobId); |
| 162 | + |
| 163 | + DelayedDataDetector delayedDataDetector = |
| 164 | + new DelayedDataDetector(job.build(new Date()), datafeedConfig, TimeValue.timeValueHours(12), client()); |
| 165 | + |
| 166 | + List<BucketWithMissingData> response = delayedDataDetector.detectMissingData(lastBucket.getEpoch()*1000); |
| 167 | + assertThat(response.stream().mapToLong(BucketWithMissingData::getMissingDocumentCount).sum(), equalTo(0L)); |
| 168 | + |
| 169 | + long missingDocs = numDocs; |
| 170 | + // Simply adding data within the current delayed data detection, the choice of 43100000 is arbitrary and within the window |
| 171 | + // for the DelayedDataDetector |
| 172 | + writeData(logger, index, missingDocs, now - 43100000, lastBucket.getEpoch()*1000); |
| 173 | + |
| 174 | + response = delayedDataDetector.detectMissingData(lastBucket.getEpoch()*1000); |
| 175 | + assertThat(response.stream().mapToLong(BucketWithMissingData::getMissingDocumentCount).sum(), equalTo((missingDocs+1)/2)); |
| 176 | + } |
| 177 | + |
| 178 | + private Job.Builder createJob(String id, TimeValue bucketSpan, String function, String field) { |
| 179 | + return createJob(id, bucketSpan, function, field, null); |
| 180 | + } |
| 181 | + |
| 182 | + private Job.Builder createJob(String id, TimeValue bucketSpan, String function, String field, String summaryCountField) { |
| 183 | + DataDescription.Builder dataDescription = new DataDescription.Builder(); |
| 184 | + dataDescription.setFormat(DataDescription.DataFormat.XCONTENT); |
| 185 | + dataDescription.setTimeField("time"); |
| 186 | + dataDescription.setTimeFormat(DataDescription.EPOCH_MS); |
| 187 | + |
| 188 | + Detector.Builder d = new Detector.Builder(function, field); |
| 189 | + AnalysisConfig.Builder analysisConfig = new AnalysisConfig.Builder(Collections.singletonList(d.build())); |
| 190 | + analysisConfig.setBucketSpan(bucketSpan); |
| 191 | + analysisConfig.setSummaryCountFieldName(summaryCountField); |
| 192 | + |
| 193 | + Job.Builder builder = new Job.Builder(); |
| 194 | + builder.setId(id); |
| 195 | + builder.setAnalysisConfig(analysisConfig); |
| 196 | + builder.setDataDescription(dataDescription); |
| 197 | + return builder; |
| 198 | + } |
| 199 | + |
| 200 | + private void writeData(Logger logger, String index, long numDocs, long start, long end) { |
| 201 | + int maxDelta = (int) (end - start - 1); |
| 202 | + BulkRequestBuilder bulkRequestBuilder = client().prepareBulk(); |
| 203 | + for (int i = 0; i < numDocs; i++) { |
| 204 | + IndexRequest indexRequest = new IndexRequest(index, "type"); |
| 205 | + long timestamp = start + randomIntBetween(0, maxDelta); |
| 206 | + assert timestamp >= start && timestamp < end; |
| 207 | + indexRequest.source("time", timestamp, "value", i); |
| 208 | + bulkRequestBuilder.add(indexRequest); |
| 209 | + } |
| 210 | + BulkResponse bulkResponse = bulkRequestBuilder |
| 211 | + .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE) |
| 212 | + .get(); |
| 213 | + if (bulkResponse.hasFailures()) { |
| 214 | + int failures = 0; |
| 215 | + for (BulkItemResponse itemResponse : bulkResponse) { |
| 216 | + if (itemResponse.isFailed()) { |
| 217 | + failures++; |
| 218 | + logger.error("Item response failure [{}]", itemResponse.getFailureMessage()); |
| 219 | + } |
| 220 | + } |
| 221 | + fail("Bulk response contained " + failures + " failures"); |
| 222 | + } |
| 223 | + logger.info("Indexed [{}] documents", numDocs); |
| 224 | + } |
| 225 | + |
| 226 | + private Bucket getLatestFinalizedBucket(String jobId) { |
| 227 | + GetBucketsAction.Request getBucketsRequest = new GetBucketsAction.Request(jobId); |
| 228 | + getBucketsRequest.setExcludeInterim(true); |
| 229 | + getBucketsRequest.setSort(Result.TIMESTAMP.getPreferredName()); |
| 230 | + getBucketsRequest.setDescending(true); |
| 231 | + getBucketsRequest.setPageParams(new PageParams(0, 1)); |
| 232 | + return getBuckets(getBucketsRequest).get(0); |
| 233 | + } |
| 234 | +} |
0 commit comments