Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.test.rest.ESRestTestCase;
import org.elasticsearch.xpack.ml.MachineLearning;
import org.joda.time.DateTime;

import java.time.Clock;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
Expand Down Expand Up @@ -266,7 +268,7 @@ public void testHRDSplit() throws Exception {
"\"time\": { \"type\": \"date\" } } }");

// Index some data
DateTime baseTime = new DateTime().minusYears(1);
ZonedDateTime baseTime = ZonedDateTime.now(Clock.systemDefaultZone()).minusYears(1);
TestConfiguration test = tests.get(randomInt(tests.size()-1));

// domainSplit() tests had subdomain, testHighestRegisteredDomainCases() did not, so we need a special case for sub
Expand All @@ -276,18 +278,24 @@ public void testHRDSplit() throws Exception {

for (int i = 0; i < 100; i++) {

DateTime time = baseTime.plusHours(i);
ZonedDateTime time = baseTime.plusHours(i);
if (i == 64) {
// Anomaly has 100 docs, but we don't care about the value
for (int j = 0; j < 100; j++) {
Request createDocRequest = new Request("PUT", "/painless/test/" + time.toDateTimeISO() + "_" + j);
createDocRequest.setJsonEntity("{\"domain\": \"" + "bar.bar.com\", \"time\": \"" + time.toDateTimeISO() + "\"}");
String endpoint = "/painless/test/" + time.format(DateTimeFormatter.ISO_DATE_TIME) + "_" + j;
Request createDocRequest = new Request("PUT", endpoint);
String entity = "{\"domain\": \"" + "bar.bar.com\", \"time\": \"" + time.format(DateTimeFormatter.ISO_DATE_TIME) +
"\"}";
createDocRequest.setJsonEntity(entity);
client().performRequest(createDocRequest);
}
} else {
// Non-anomalous values will be what's seen when the anomaly is reported
Request createDocRequest = new Request("PUT", "/painless/test/" + time.toDateTimeISO());
createDocRequest.setJsonEntity("{\"domain\": \"" + test.hostName + "\", \"time\": \"" + time.toDateTimeISO() + "\"}");
String endpoint = "/painless/test/" + time.format(DateTimeFormatter.ISO_DATE_TIME);
Request createDocRequest = new Request("PUT", endpoint);
String entity =
"{\"domain\": \"" + test.hostName + "\", \"time\": \"" + time.format(DateTimeFormatter.ISO_DATE_TIME) + "\"}";
createDocRequest.setJsonEntity(entity);
client().performRequest(createDocRequest);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
import org.elasticsearch.common.util.concurrent.FutureUtils;
import org.elasticsearch.threadpool.ThreadPool;
import org.elasticsearch.xpack.core.ml.action.DeleteExpiredDataAction;
import org.joda.time.DateTime;
import org.joda.time.chrono.ISOChronology;

import java.time.Clock;
import java.time.ZonedDateTime;
import java.util.Objects;
import java.util.Random;
import java.util.concurrent.ScheduledFuture;
Expand Down Expand Up @@ -70,9 +70,14 @@ public MlDailyMaintenanceService(ClusterName clusterName, ThreadPool threadPool,
private static TimeValue delayToNextTime(ClusterName clusterName) {
Random random = new Random(clusterName.hashCode());
int minutesOffset = random.ints(0, MAX_TIME_OFFSET_MINUTES).findFirst().getAsInt();
DateTime now = DateTime.now(ISOChronology.getInstance());
DateTime next = now.plusDays(1).withTimeAtStartOfDay().plusMinutes(30).plusMinutes(minutesOffset);
return TimeValue.timeValueMillis(next.getMillis() - now.getMillis());

ZonedDateTime now = ZonedDateTime.now(Clock.systemDefaultZone());
ZonedDateTime next = now.plusDays(1)
.toLocalDate()
.atStartOfDay(now.getZone())
.plusMinutes(30)
.plusMinutes(minutesOffset);
return TimeValue.timeValueMillis(next.toInstant().toEpochMilli() - now.toInstant().toEpochMilli());
Copy link
Contributor Author

@pgomulka pgomulka Nov 27, 2018

Choose a reason for hiding this comment

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

we did discuss to use just Instant.now(), but here I think it is better to use ZonedDateTime since it allows to use convenient DSL for calculating next variable

Copy link
Contributor

Choose a reason for hiding this comment

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

+1

}

public void start() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import org.elasticsearch.search.aggregations.metrics.Percentiles;
import org.elasticsearch.xpack.core.ml.datafeed.DatafeedConfig;
import org.elasticsearch.xpack.core.ml.job.messages.Messages;
import org.joda.time.DateTime;

import java.io.IOException;
import java.io.OutputStream;
Expand Down Expand Up @@ -176,17 +175,15 @@ private void processDateHistogram(Histogram agg) throws IOException {
}

/*
* Date Histograms have a {@link DateTime} object as the key,
* Date Histograms have a {@link ZonedDateTime} object as the key,
* Histograms have either a Double or Long.
*/
private long toHistogramKeyToEpoch(Object key) {
if (key instanceof DateTime) {
return ((DateTime)key).getMillis();
} else if (key instanceof ZonedDateTime) {
if (key instanceof ZonedDateTime) {
return ((ZonedDateTime)key).toInstant().toEpochMilli();
} else if (key instanceof Double) {
return ((Double)key).longValue();
} else if (key instanceof Long){
} else if (key instanceof Long) {
return (Long)key;
} else {
throw new IllegalStateException("Histogram key [" + key + "] cannot be converted to a timestamp");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import org.elasticsearch.common.document.DocumentField;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.fetch.subphase.DocValueFieldsContext;
import org.joda.time.base.BaseDateTime;

import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -112,8 +111,6 @@ public Object[] value(SearchHit hit) {
}
if (value[0] instanceof String) { // doc_value field with the epoch_millis format
value[0] = Long.parseLong((String) value[0]);
} else if (value[0] instanceof BaseDateTime) { // script field
value[0] = ((BaseDateTime) value[0]).getMillis();
} else if (value[0] instanceof Long == false) { // pre-6.0 field
throw new IllegalStateException("Unexpected value for a time field: " + value[0].getClass());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
import org.elasticsearch.xpack.core.ml.job.config.Job;
import org.elasticsearch.xpack.core.ml.job.results.Result;
import org.elasticsearch.xpack.ml.utils.VolatileCursorIterator;
import org.joda.time.DateTime;
import org.joda.time.chrono.ISOChronology;

import java.time.Clock;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
Expand Down Expand Up @@ -70,7 +70,7 @@ protected static <T> Iterator<T> createVolatileCursorIterator(List<T> items) {
}

private long calcCutoffEpochMs(long retentionDays) {
long nowEpochMs = DateTime.now(ISOChronology.getInstance()).getMillis();
long nowEpochMs = Instant.now(Clock.systemDefaultZone()).toEpochMilli();
return nowEpochMs - new TimeValue(retentionDays, TimeUnit.DAYS).getMillis();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@
import org.elasticsearch.xpack.core.ml.job.results.ForecastRequestStats;
import org.elasticsearch.xpack.core.ml.job.results.Result;
import org.elasticsearch.xpack.ml.MachineLearning;
import org.joda.time.DateTime;
import org.joda.time.chrono.ISOChronology;

import java.io.IOException;
import java.io.InputStream;
import java.time.Clock;
import java.time.Instant;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
Expand All @@ -66,7 +66,7 @@ public class ExpiredForecastsRemover implements MlDataRemover {
public ExpiredForecastsRemover(Client client, ThreadPool threadPool) {
this.client = Objects.requireNonNull(client);
this.threadPool = Objects.requireNonNull(threadPool);
this.cutoffEpochMs = DateTime.now(ISOChronology.getInstance()).getMillis();
this.cutoffEpochMs = Instant.now(Clock.systemDefaultZone()).toEpochMilli();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.fetch.subphase.DocValueFieldsContext;
import org.elasticsearch.test.ESTestCase;
import org.elasticsearch.xpack.ml.datafeed.extractor.fields.ExtractedField;
import org.elasticsearch.xpack.ml.test.SearchHitBuilder;
import org.joda.time.DateTime;

import java.util.Arrays;

Expand Down Expand Up @@ -98,16 +96,16 @@ public void testNewTimeFieldGivenSource() {
expectThrows(IllegalArgumentException.class, () -> ExtractedField.newTimeField("time", ExtractedField.ExtractionMethod.SOURCE));
}

public void testValueGivenTimeField() {
public void testValueGivenStringTimeField() {
final long millis = randomLong();
final SearchHit hit = new SearchHitBuilder(randomInt()).addField("time", new DateTime(millis)).build();
final SearchHit hit = new SearchHitBuilder(randomInt()).addField("time", Long.toString(millis)).build();
final ExtractedField timeField = ExtractedField.newTimeField("time", ExtractedField.ExtractionMethod.DOC_VALUE);
assertThat(timeField.value(hit), equalTo(new Object[] { millis }));
}

public void testValueGivenStringTimeField() {
public void testValueGivenLongTimeField() {
final long millis = randomLong();
final SearchHit hit = new SearchHitBuilder(randomInt()).addField("time", Long.toString(millis)).build();
final SearchHit hit = new SearchHitBuilder(randomInt()).addField("time", millis).build();
final ExtractedField timeField = ExtractedField.newTimeField("time", ExtractedField.ExtractionMethod.DOC_VALUE);
assertThat(timeField.value(hit), equalTo(new Object[] { millis }));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import org.elasticsearch.xpack.core.ml.job.config.Detector;
import org.elasticsearch.xpack.core.ml.job.config.Job;
import org.elasticsearch.xpack.ml.test.SearchHitBuilder;
import org.joda.time.DateTime;

import java.util.Arrays;
import java.util.Collections;
Expand Down Expand Up @@ -64,13 +63,6 @@ public void testAllTypesOfFields() {
assertThat(extractedFields.getSourceFields(), equalTo(new String[] {"src1", "src2"}));
}

public void testTimeFieldValue() {
long millis = randomLong();
SearchHit hit = new SearchHitBuilder(randomInt()).addField("time", new DateTime(millis)).build();
TimeBasedExtractedFields extractedFields = new TimeBasedExtractedFields(timeField, Collections.singletonList(timeField));
assertThat(extractedFields.timeFieldValue(hit), equalTo(millis));
}

public void testStringTimeFieldValue() {
long millis = randomLong();
SearchHit hit = new SearchHitBuilder(randomInt()).addField("time", Long.toString(millis)).build();
Expand Down