|
| 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.elasticsearch.action.bulk.BulkRequestBuilder; |
| 9 | +import org.elasticsearch.action.bulk.BulkResponse; |
| 10 | +import org.elasticsearch.action.index.IndexRequest; |
| 11 | +import org.elasticsearch.action.support.WriteRequest; |
| 12 | +import org.elasticsearch.common.unit.TimeValue; |
| 13 | +import org.elasticsearch.search.aggregations.AggregationBuilders; |
| 14 | +import org.elasticsearch.search.aggregations.AggregatorFactories; |
| 15 | +import org.elasticsearch.xpack.core.ml.action.GetBucketsAction; |
| 16 | +import org.elasticsearch.xpack.core.ml.action.GetDatafeedsStatsAction; |
| 17 | +import org.elasticsearch.xpack.core.ml.action.StopDatafeedAction; |
| 18 | +import org.elasticsearch.xpack.core.ml.datafeed.DatafeedConfig; |
| 19 | +import org.elasticsearch.xpack.core.ml.datafeed.DatafeedState; |
| 20 | +import org.elasticsearch.xpack.core.ml.job.config.AnalysisConfig; |
| 21 | +import org.elasticsearch.xpack.core.ml.job.config.DataDescription; |
| 22 | +import org.elasticsearch.xpack.core.ml.job.config.Detector; |
| 23 | +import org.elasticsearch.xpack.core.ml.job.config.Job; |
| 24 | +import org.elasticsearch.xpack.core.ml.job.results.Bucket; |
| 25 | +import org.junit.After; |
| 26 | + |
| 27 | +import java.util.Collections; |
| 28 | +import java.util.List; |
| 29 | +import java.util.concurrent.TimeUnit; |
| 30 | + |
| 31 | +import static org.hamcrest.Matchers.equalTo; |
| 32 | +import static org.hamcrest.Matchers.greaterThan; |
| 33 | +import static org.hamcrest.Matchers.greaterThanOrEqualTo; |
| 34 | + |
| 35 | +public class DatafeedWithAggsIT extends MlNativeAutodetectIntegTestCase { |
| 36 | + |
| 37 | + @After |
| 38 | + public void cleanup(){ |
| 39 | + cleanUp(); |
| 40 | + } |
| 41 | + |
| 42 | + public void testRealtime() throws Exception { |
| 43 | + String dataIndex = "datafeed-with-aggs-rt-data"; |
| 44 | + |
| 45 | + // A job with a bucket_span of 2s |
| 46 | + String jobId = "datafeed-with-aggs-rt-job"; |
| 47 | + DataDescription.Builder dataDescription = new DataDescription.Builder(); |
| 48 | + |
| 49 | + Detector.Builder d = new Detector.Builder("count", null); |
| 50 | + AnalysisConfig.Builder analysisConfig = new AnalysisConfig.Builder(Collections.singletonList(d.build())); |
| 51 | + analysisConfig.setBucketSpan(TimeValue.timeValueSeconds(2)); |
| 52 | + analysisConfig.setSummaryCountFieldName("doc_count"); |
| 53 | + |
| 54 | + Job.Builder jobBuilder = new Job.Builder(); |
| 55 | + jobBuilder.setId(jobId); |
| 56 | + |
| 57 | + jobBuilder.setAnalysisConfig(analysisConfig); |
| 58 | + jobBuilder.setDataDescription(dataDescription); |
| 59 | + |
| 60 | + // Datafeed with aggs |
| 61 | + String datafeedId = jobId + "-feed"; |
| 62 | + DatafeedConfig.Builder datafeedBuilder = new DatafeedConfig.Builder(datafeedId, jobId); |
| 63 | + datafeedBuilder.setQueryDelay(TimeValue.timeValueMillis(100)); |
| 64 | + datafeedBuilder.setFrequency(TimeValue.timeValueSeconds(1)); |
| 65 | + datafeedBuilder.setIndices(Collections.singletonList(dataIndex)); |
| 66 | + |
| 67 | + AggregatorFactories.Builder aggs = new AggregatorFactories.Builder(); |
| 68 | + aggs.addAggregator(AggregationBuilders.dateHistogram("time").field("time").interval(1000) |
| 69 | + .subAggregation(AggregationBuilders.max("time").field("time"))); |
| 70 | + datafeedBuilder.setParsedAggregations(aggs); |
| 71 | + |
| 72 | + DatafeedConfig datafeed = datafeedBuilder.build(); |
| 73 | + |
| 74 | + // Create stuff and open job |
| 75 | + registerJob(jobBuilder); |
| 76 | + putJob(jobBuilder); |
| 77 | + registerDatafeed(datafeed); |
| 78 | + putDatafeed(datafeed); |
| 79 | + openJob(jobId); |
| 80 | + |
| 81 | + // Now let's index the data |
| 82 | + client().admin().indices().prepareCreate(dataIndex) |
| 83 | + .addMapping("type", "time", "type=date") |
| 84 | + .get(); |
| 85 | + |
| 86 | + // Index a doc per second from a minute ago to a minute later |
| 87 | + long now = System.currentTimeMillis(); |
| 88 | + long aMinuteAgo = now - TimeValue.timeValueMinutes(1).millis(); |
| 89 | + long aMinuteLater = now + TimeValue.timeValueMinutes(1).millis(); |
| 90 | + long curTime = aMinuteAgo; |
| 91 | + BulkRequestBuilder bulkRequestBuilder = client().prepareBulk(); |
| 92 | + while (curTime < aMinuteLater) { |
| 93 | + IndexRequest indexRequest = new IndexRequest(dataIndex); |
| 94 | + indexRequest.source("time", curTime); |
| 95 | + bulkRequestBuilder.add(indexRequest); |
| 96 | + curTime += TimeValue.timeValueSeconds(1).millis(); |
| 97 | + } |
| 98 | + BulkResponse bulkResponse = bulkRequestBuilder |
| 99 | + .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE) |
| 100 | + .get(); |
| 101 | + if (bulkResponse.hasFailures()) { |
| 102 | + fail("Failed to index docs: " + bulkResponse.buildFailureMessage()); |
| 103 | + } |
| 104 | + |
| 105 | + // And start datafeed in real-time mode |
| 106 | + startDatafeed(datafeedId, 0L, null); |
| 107 | + |
| 108 | + // Wait until we finalize a bucket after now |
| 109 | + assertBusy(() -> { |
| 110 | + GetBucketsAction.Request getBucketsRequest = new GetBucketsAction.Request(jobId); |
| 111 | + getBucketsRequest.setExcludeInterim(true); |
| 112 | + getBucketsRequest.setSort("timestamp"); |
| 113 | + getBucketsRequest.setDescending(true); |
| 114 | + List<Bucket> buckets = getBuckets(getBucketsRequest); |
| 115 | + assertThat(buckets.size(), greaterThanOrEqualTo(1)); |
| 116 | + assertThat(buckets.get(0).getTimestamp().getTime(), greaterThan(now)); |
| 117 | + }, 30, TimeUnit.SECONDS); |
| 118 | + |
| 119 | + // Wrap up |
| 120 | + StopDatafeedAction.Response stopJobResponse = stopDatafeed(datafeedId); |
| 121 | + assertTrue(stopJobResponse.isStopped()); |
| 122 | + assertBusy(() -> { |
| 123 | + GetDatafeedsStatsAction.Request request = new GetDatafeedsStatsAction.Request(datafeedId); |
| 124 | + GetDatafeedsStatsAction.Response response = client().execute(GetDatafeedsStatsAction.INSTANCE, request).actionGet(); |
| 125 | + assertThat(response.getResponse().results().get(0).getDatafeedState(), equalTo(DatafeedState.STOPPED)); |
| 126 | + }); |
| 127 | + closeJob(jobId); |
| 128 | + |
| 129 | + // Assert we have not dropped any data - final buckets should contain 2 events each |
| 130 | + GetBucketsAction.Request getBucketsRequest = new GetBucketsAction.Request(jobId); |
| 131 | + getBucketsRequest.setExcludeInterim(true); |
| 132 | + List<Bucket> buckets = getBuckets(getBucketsRequest); |
| 133 | + for (Bucket bucket : buckets) { |
| 134 | + if (bucket.getEventCount() != 2) { |
| 135 | + fail("Bucket [" + bucket.getTimestamp().getTime() + "] has [" + bucket.getEventCount() + "] when 2 were expected"); |
| 136 | + } |
| 137 | + } |
| 138 | + } |
| 139 | +} |
0 commit comments