From 85b4bfe3ff119b3b7fa48631c6fc4a0bf9e1816d Mon Sep 17 00:00:00 2001 From: Przemyslaw Gomulka Date: Mon, 4 Feb 2019 14:47:08 +0100 Subject: [PATCH] Core: Migrating from joda to java.time. Monitoring plugin (#36297) monitoring plugin migration from joda to java.time refers #27330 --- .../monitoring/exporter/MonitoringDoc.java | 13 ++++-- .../exporter/MonitoringTemplateUtils.java | 11 ++--- .../monitoring/cleaner/CleanerService.java | 20 +++++---- .../xpack/monitoring/exporter/Exporter.java | 10 ++--- .../exporter/http/HttpExportBulk.java | 7 +-- .../exporter/http/HttpExporter.java | 4 +- .../monitoring/exporter/local/LocalBulk.java | 6 +-- .../exporter/local/LocalExporter.java | 16 ++++--- .../AbstractIndicesCleanerTestCase.java | 44 +++++++++---------- .../cleaner/CleanerServiceTests.java | 23 ++++++---- .../local/LocalIndicesCleanerTests.java | 7 +-- .../ml/JobStatsMonitoringDocTests.java | 17 ++++--- .../exporter/BaseMonitoringDocTestCase.java | 6 +-- .../monitoring/exporter/ExportersTests.java | 20 +++++++++ .../MonitoringTemplateUtilsTests.java | 15 ++++--- .../exporter/http/HttpExporterIT.java | 13 ++++-- .../local/LocalExporterIntegTests.java | 25 +++++------ .../monitoring/integration/MonitoringIT.java | 12 ++--- 18 files changed, 156 insertions(+), 113 deletions(-) diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/monitoring/exporter/MonitoringDoc.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/monitoring/exporter/MonitoringDoc.java index 93785053889c8..0642ed5955f77 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/monitoring/exporter/MonitoringDoc.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/monitoring/exporter/MonitoringDoc.java @@ -10,13 +10,15 @@ import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; +import org.elasticsearch.common.time.DateFormatter; import org.elasticsearch.common.xcontent.ToXContentObject; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.xpack.core.monitoring.MonitoredSystem; -import org.joda.time.DateTime; -import org.joda.time.DateTimeZone; import java.io.IOException; +import java.time.Instant; +import java.time.ZoneOffset; +import java.time.ZonedDateTime; import java.util.Objects; /** @@ -24,6 +26,7 @@ */ public abstract class MonitoringDoc implements ToXContentObject { + private static final DateFormatter dateTimeFormatter = DateFormatter.forPattern("strict_date_time"); private final String cluster; private final long timestamp; private final long intervalMillis; @@ -123,7 +126,9 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws * @return a string representing the timestamp */ public static String toUTC(final long timestamp) { - return new DateTime(timestamp, DateTimeZone.UTC).toString(); + ZonedDateTime zonedDateTime = Instant.ofEpochMilli(timestamp).atZone(ZoneOffset.UTC); + return dateTimeFormatter.format(zonedDateTime); + } /** @@ -250,4 +255,4 @@ public int hashCode() { return Objects.hash(uuid, host, transportAddress, ip, name, timestamp); } } -} \ No newline at end of file +} diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/monitoring/exporter/MonitoringTemplateUtils.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/monitoring/exporter/MonitoringTemplateUtils.java index 78e094cb8cefe..7666ac6ca72eb 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/monitoring/exporter/MonitoringTemplateUtils.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/monitoring/exporter/MonitoringTemplateUtils.java @@ -6,15 +6,16 @@ package org.elasticsearch.xpack.core.monitoring.exporter; import org.elasticsearch.Version; +import org.elasticsearch.common.Strings; import org.elasticsearch.common.bytes.BytesReference; +import org.elasticsearch.common.time.DateFormatter; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.xpack.core.monitoring.MonitoredSystem; import org.elasticsearch.xpack.core.template.TemplateUtils; -import org.joda.time.format.DateTimeFormatter; -import org.elasticsearch.common.Strings; import java.io.IOException; +import java.time.Instant; import java.util.Locale; import java.util.regex.Pattern; @@ -252,12 +253,12 @@ public static XContentBuilder emptyPipeline(final XContentType type) { /** * Get the index name given a specific date format, a monitored system and a timestamp. * - * @param formatter the {@link DateTimeFormatter} to use to compute the timestamped index name + * @param formatter the {@link DateFormatter} to use to compute the timestamped index name * @param system the {@link MonitoredSystem} for which the index name is computed * @param timestamp the timestamp value to use to compute the timestamped index name * @return the index name as a @{link String} */ - public static String indexName(final DateTimeFormatter formatter, final MonitoredSystem system, final long timestamp) { - return ".monitoring-" + system.getSystem() + "-" + TEMPLATE_VERSION + "-" + formatter.print(timestamp); + public static String indexName(final DateFormatter formatter, final MonitoredSystem system, final long timestamp) { + return ".monitoring-" + system.getSystem() + "-" + TEMPLATE_VERSION + "-" + formatter.format(Instant.ofEpochMilli(timestamp)); } } diff --git a/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/cleaner/CleanerService.java b/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/cleaner/CleanerService.java index 571bfc7c1a964..1dc2216839b59 100644 --- a/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/cleaner/CleanerService.java +++ b/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/cleaner/CleanerService.java @@ -17,9 +17,10 @@ import org.elasticsearch.threadpool.Scheduler; import org.elasticsearch.threadpool.ThreadPool; import org.elasticsearch.xpack.core.monitoring.MonitoringField; -import org.joda.time.DateTime; -import org.joda.time.chrono.ISOChronology; +import java.time.Clock; +import java.time.Duration; +import java.time.ZonedDateTime; import java.util.List; import java.util.concurrent.CopyOnWriteArrayList; @@ -56,7 +57,8 @@ public CleanerService(Settings settings, ClusterSettings clusterSettings, Thread @Override protected void doStart() { logger.debug("starting cleaning service"); - threadPool.schedule(runnable, executionScheduler.nextExecutionDelay(new DateTime(ISOChronology.getInstance())), executorName()); + threadPool.schedule(runnable, executionScheduler.nextExecutionDelay(ZonedDateTime.now(Clock.systemDefaultZone())), + executorName()); logger.debug("cleaning service started"); } @@ -190,7 +192,7 @@ protected void doRunInLifecycle() throws Exception { */ @Override protected void onAfterInLifecycle() { - DateTime start = new DateTime(ISOChronology.getInstance()); + ZonedDateTime start = ZonedDateTime.now(Clock.systemUTC()); TimeValue delay = executionScheduler.nextExecutionDelay(start); logger.debug("scheduling next execution in [{}] seconds", delay.seconds()); @@ -233,7 +235,7 @@ interface ExecutionScheduler { * @param now the current time * @return the delay in millis */ - TimeValue nextExecutionDelay(DateTime now); + TimeValue nextExecutionDelay(ZonedDateTime now); } /** @@ -242,14 +244,16 @@ interface ExecutionScheduler { static class DefaultExecutionScheduler implements ExecutionScheduler { @Override - public TimeValue nextExecutionDelay(DateTime now) { + public TimeValue nextExecutionDelay(ZonedDateTime now) { // Runs at 01:00 AM today or the next day if it's too late - DateTime next = now.withTimeAtStartOfDay().plusHours(1); + ZonedDateTime next = now.toLocalDate() + .atStartOfDay(now.getZone()) + .plusHours(1); // if it's not after now, then it needs to be the next day! if (next.isAfter(now) == false) { next = next.plusDays(1); } - return TimeValue.timeValueMillis(next.getMillis() - now.getMillis()); + return TimeValue.timeValueMillis(Duration.between(now, next).toMillis()); } } } diff --git a/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/exporter/Exporter.java b/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/exporter/Exporter.java index 34c069adb2a02..6b7d49a60222a 100644 --- a/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/exporter/Exporter.java +++ b/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/exporter/Exporter.java @@ -11,10 +11,10 @@ import org.elasticsearch.common.settings.Setting.Property; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.SettingsException; +import org.elasticsearch.common.time.DateFormatter; import org.elasticsearch.license.XPackLicenseState; -import org.joda.time.format.DateTimeFormat; -import org.joda.time.format.DateTimeFormatter; +import java.time.ZoneOffset; import java.util.Arrays; import java.util.Collections; import java.util.List; @@ -70,7 +70,7 @@ public abstract class Exporter implements AutoCloseable { Setting.affixKeySetting("xpack.monitoring.exporters.","index.name.time_format", key -> Setting.simpleString(key, Property.Dynamic, Property.NodeScope)); - private static final String INDEX_FORMAT = "YYYY.MM.dd"; + private static final String INDEX_FORMAT = "yyyy.MM.dd"; protected final Config config; @@ -113,11 +113,11 @@ public void close() { protected abstract void doClose(); - protected static DateTimeFormatter dateTimeFormatter(final Config config) { + protected static DateFormatter dateTimeFormatter(final Config config) { Setting setting = INDEX_NAME_TIME_FORMAT_SETTING.getConcreteSettingForNamespace(config.name); String format = setting.exists(config.settings()) ? setting.get(config.settings()) : INDEX_FORMAT; try { - return DateTimeFormat.forPattern(format).withZoneUTC(); + return DateFormatter.forPattern(format).withZone(ZoneOffset.UTC); } catch (IllegalArgumentException e) { throw new SettingsException("[" + INDEX_NAME_TIME_FORMAT_SETTING.getKey() + "] invalid index name time format: [" + format + "]", e); diff --git a/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/exporter/http/HttpExportBulk.java b/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/exporter/http/HttpExportBulk.java index c00448c903a47..7d17705decfbe 100644 --- a/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/exporter/http/HttpExportBulk.java +++ b/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/exporter/http/HttpExportBulk.java @@ -19,6 +19,7 @@ import org.elasticsearch.client.RestClient; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.io.stream.BytesStreamOutput; +import org.elasticsearch.common.time.DateFormatter; import org.elasticsearch.common.util.concurrent.ThreadContext; import org.elasticsearch.common.xcontent.XContent; import org.elasticsearch.common.xcontent.XContentBuilder; @@ -28,9 +29,9 @@ import org.elasticsearch.xpack.core.monitoring.exporter.MonitoringTemplateUtils; import org.elasticsearch.xpack.monitoring.exporter.ExportBulk; import org.elasticsearch.xpack.monitoring.exporter.ExportException; -import org.joda.time.format.DateTimeFormatter; import java.io.IOException; +import java.time.format.DateTimeFormatter; import java.util.Collection; import java.util.Map; @@ -54,7 +55,7 @@ class HttpExportBulk extends ExportBulk { /** * {@link DateTimeFormatter} used to resolve timestamped index name. */ - private final DateTimeFormatter formatter; + private final DateFormatter formatter; /** * The bytes payload that represents the bulk body is created via {@link #doAdd(Collection)}. @@ -62,7 +63,7 @@ class HttpExportBulk extends ExportBulk { private byte[] payload = null; HttpExportBulk(final String name, final RestClient client, final Map parameters, - final DateTimeFormatter dateTimeFormatter, final ThreadContext threadContext) { + final DateFormatter dateTimeFormatter, final ThreadContext threadContext) { super(name, threadContext); this.client = client; diff --git a/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/exporter/http/HttpExporter.java b/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/exporter/http/HttpExporter.java index e89f3c3e64d35..19844be819a67 100644 --- a/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/exporter/http/HttpExporter.java +++ b/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/exporter/http/HttpExporter.java @@ -30,6 +30,7 @@ import org.elasticsearch.common.settings.Setting.Property; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.SettingsException; +import org.elasticsearch.common.time.DateFormatter; import org.elasticsearch.common.unit.TimeValue; import org.elasticsearch.common.util.concurrent.ThreadContext; import org.elasticsearch.common.util.set.Sets; @@ -41,7 +42,6 @@ import org.elasticsearch.xpack.monitoring.exporter.ClusterAlertsUtil; import org.elasticsearch.xpack.monitoring.exporter.ExportBulk; import org.elasticsearch.xpack.monitoring.exporter.Exporter; -import org.joda.time.format.DateTimeFormatter; import javax.net.ssl.SSLContext; import java.util.ArrayList; @@ -193,7 +193,7 @@ public class HttpExporter extends Exporter { private final AtomicBoolean clusterAlertsAllowed = new AtomicBoolean(false); private final ThreadContext threadContext; - private final DateTimeFormatter dateTimeFormatter; + private final DateFormatter dateTimeFormatter; /** * Create an {@link HttpExporter}. diff --git a/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/exporter/local/LocalBulk.java b/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/exporter/local/LocalBulk.java index 05320224eded7..b8337bea17d92 100644 --- a/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/exporter/local/LocalBulk.java +++ b/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/exporter/local/LocalBulk.java @@ -14,13 +14,13 @@ import org.elasticsearch.client.Client; import org.elasticsearch.common.Strings; import org.elasticsearch.common.bytes.BytesReference; +import org.elasticsearch.common.time.DateFormatter; import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.xpack.core.monitoring.exporter.MonitoringDoc; import org.elasticsearch.xpack.core.monitoring.exporter.MonitoringTemplateUtils; import org.elasticsearch.xpack.monitoring.exporter.ExportBulk; import org.elasticsearch.xpack.monitoring.exporter.ExportException; -import org.joda.time.format.DateTimeFormatter; import java.util.Arrays; import java.util.Collection; @@ -37,13 +37,13 @@ public class LocalBulk extends ExportBulk { private final Logger logger; private final Client client; - private final DateTimeFormatter formatter; + private final DateFormatter formatter; private final boolean usePipeline; private BulkRequestBuilder requestBuilder; - LocalBulk(String name, Logger logger, Client client, DateTimeFormatter dateTimeFormatter, boolean usePipeline) { + LocalBulk(String name, Logger logger, Client client, DateFormatter dateTimeFormatter, boolean usePipeline) { super(name, client.threadPool().getThreadContext()); this.logger = logger; this.client = client; diff --git a/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/exporter/local/LocalExporter.java b/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/exporter/local/LocalExporter.java index e18485fc237da..12f2f77a9d40c 100644 --- a/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/exporter/local/LocalExporter.java +++ b/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/exporter/local/LocalExporter.java @@ -28,6 +28,7 @@ import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.regex.Regex; +import org.elasticsearch.common.time.DateFormatter; import org.elasticsearch.common.unit.TimeValue; import org.elasticsearch.common.util.concurrent.ThreadContext; import org.elasticsearch.common.xcontent.XContentType; @@ -51,10 +52,11 @@ import org.elasticsearch.xpack.monitoring.exporter.ClusterAlertsUtil; import org.elasticsearch.xpack.monitoring.exporter.ExportBulk; import org.elasticsearch.xpack.monitoring.exporter.Exporter; -import org.joda.time.DateTime; -import org.joda.time.DateTimeZone; -import org.joda.time.format.DateTimeFormatter; +import java.time.Instant; +import java.time.ZoneOffset; +import java.time.ZonedDateTime; +import java.time.temporal.ChronoUnit; import java.util.ArrayList; import java.util.Arrays; import java.util.HashSet; @@ -89,7 +91,7 @@ public class LocalExporter extends Exporter implements ClusterStateListener, Cle private final XPackLicenseState licenseState; private final CleanerService cleanerService; private final boolean useIngest; - private final DateTimeFormatter dateTimeFormatter; + private final DateFormatter dateTimeFormatter; private final List clusterAlertBlacklist; private final AtomicReference state = new AtomicReference<>(State.INITIALIZED); @@ -489,12 +491,12 @@ public void onCleanUpIndices(TimeValue retention) { if (clusterService.state().nodes().isLocalNodeElectedMaster()) { // Reference date time will be compared to index.creation_date settings, // that's why it must be in UTC - DateTime expiration = new DateTime(DateTimeZone.UTC).minus(retention.millis()); + ZonedDateTime expiration = ZonedDateTime.now(ZoneOffset.UTC).minus(retention.millis(), ChronoUnit.MILLIS); logger.debug("cleaning indices [expiration={}, retention={}]", expiration, retention); ClusterState clusterState = clusterService.state(); if (clusterState != null) { - final long expirationTimeMillis = expiration.getMillis(); + final long expirationTimeMillis = expiration.toInstant().toEpochMilli(); final long currentTimeMillis = System.currentTimeMillis(); final boolean cleanUpWatcherHistory = clusterService.getClusterSettings().get(CLEAN_WATCHER_HISTORY); @@ -524,7 +526,7 @@ public void onCleanUpIndices(TimeValue retention) { if (creationDate <= expirationTimeMillis) { if (logger.isDebugEnabled()) { logger.debug("detected expired index [name={}, created={}, expired={}]", - indexName, new DateTime(creationDate, DateTimeZone.UTC), expiration); + indexName, Instant.ofEpochMilli(creationDate).atZone(ZoneOffset.UTC), expiration); } indices.add(indexName); } diff --git a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/cleaner/AbstractIndicesCleanerTestCase.java b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/cleaner/AbstractIndicesCleanerTestCase.java index 762ab49bba73c..42d89608efd46 100644 --- a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/cleaner/AbstractIndicesCleanerTestCase.java +++ b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/cleaner/AbstractIndicesCleanerTestCase.java @@ -6,6 +6,7 @@ package org.elasticsearch.xpack.monitoring.cleaner; import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.common.time.DateFormatter; import org.elasticsearch.common.unit.TimeValue; import org.elasticsearch.test.ESIntegTestCase.ClusterScope; import org.elasticsearch.xpack.core.monitoring.MonitoringField; @@ -13,11 +14,9 @@ import org.elasticsearch.xpack.monitoring.exporter.Exporter; import org.elasticsearch.xpack.monitoring.exporter.Exporters; import org.elasticsearch.xpack.monitoring.test.MonitoringIntegTestCase; -import org.joda.time.DateTime; -import org.joda.time.DateTimeZone; -import org.joda.time.format.DateTimeFormat; -import org.joda.time.format.DateTimeFormatter; +import java.time.ZoneOffset; +import java.time.ZonedDateTime; import java.util.Locale; import static org.elasticsearch.test.ESIntegTestCase.Scope.TEST; @@ -25,6 +24,7 @@ @ClusterScope(scope = TEST, numDataNodes = 0, numClientNodes = 0, transportClientRatio = 0.0) public abstract class AbstractIndicesCleanerTestCase extends MonitoringIntegTestCase { + static final DateFormatter DATE_FORMATTER = DateFormatter.forPattern("yyyy.MM.dd").withZone(ZoneOffset.UTC); static Integer INDEX_TEMPLATE_VERSION = null; public void testNothingToDelete() throws Exception { @@ -108,7 +108,7 @@ public void testDeleteIndices() throws Exception { CleanerService.Listener listener = getListener(); - final DateTime now = now(); + final ZonedDateTime now = now(); createTimestampedIndex(now.minusYears(1)); createTimestampedIndex(now.minusMonths(6)); createTimestampedIndex(now.minusMonths(1)); @@ -147,7 +147,7 @@ public void testRetentionAsGlobalSetting() throws Exception { internalCluster().startNode(Settings.builder().put(MonitoringField.HISTORY_DURATION.getKey(), String.format(Locale.ROOT, "%dd", retention))); - final DateTime now = now(); + final ZonedDateTime now = now(); for (int i = 0; i < max; i++) { createTimestampedIndex(now.minusDays(i)); } @@ -172,21 +172,21 @@ protected CleanerService.Listener getListener() { /** * Creates a monitoring alerts index from the current version. */ - protected void createAlertsIndex(final DateTime creationDate) { + protected void createAlertsIndex(final ZonedDateTime creationDate) { createAlertsIndex(creationDate, MonitoringTemplateUtils.TEMPLATE_VERSION); } /** * Creates a monitoring alerts index from the specified version. */ - protected void createAlertsIndex(final DateTime creationDate, final String version) { + protected void createAlertsIndex(final ZonedDateTime creationDate, final String version) { createIndex(".monitoring-alerts-" + version, creationDate); } /** * Creates a watcher history index from the current version. */ - protected void createWatcherHistoryIndex(final DateTime creationDate) { + protected void createWatcherHistoryIndex(final ZonedDateTime creationDate) { if (INDEX_TEMPLATE_VERSION == null) { INDEX_TEMPLATE_VERSION = randomIntBetween(1, 20); } @@ -196,9 +196,8 @@ protected void createWatcherHistoryIndex(final DateTime creationDate) { /** * Creates a watcher history index from the specified version. */ - protected void createWatcherHistoryIndex(final DateTime creationDate, final String version) { - final DateTimeFormatter formatter = DateTimeFormat.forPattern("YYYY.MM.dd").withZoneUTC(); - final String index = ".watcher-history-" + version + "-" + formatter.print(creationDate.getMillis()); + protected void createWatcherHistoryIndex(final ZonedDateTime creationDate, final String version) { + final String index = ".watcher-history-" + version + "-" + DATE_FORMATTER.format(creationDate); createIndex(index, creationDate); } @@ -206,38 +205,37 @@ protected void createWatcherHistoryIndex(final DateTime creationDate, final Stri /** * Creates a monitoring timestamped index using the current template version. */ - protected void createTimestampedIndex(DateTime creationDate) { + protected void createTimestampedIndex(ZonedDateTime creationDate) { createTimestampedIndex(creationDate, MonitoringTemplateUtils.TEMPLATE_VERSION); } /** * Creates a monitoring timestamped index using a given template version. */ - protected void createTimestampedIndex(DateTime creationDate, String version) { - final DateTimeFormatter formatter = DateTimeFormat.forPattern("YYYY.MM.dd").withZoneUTC(); - final String index = ".monitoring-es-" + version + "-" + formatter.print(creationDate.getMillis()); + protected void createTimestampedIndex(ZonedDateTime creationDate, String version) { + final String index = ".monitoring-es-" + version + "-" + DATE_FORMATTER.format(creationDate); createIndex(index, creationDate); } - protected abstract void createIndex(String name, DateTime creationDate); + protected abstract void createIndex(String name, ZonedDateTime creationDate); protected abstract void assertIndicesCount(int count) throws Exception; protected static TimeValue years(int years) { - DateTime now = now(); - return TimeValue.timeValueMillis(now.getMillis() - now.minusYears(years).getMillis()); + ZonedDateTime now = now(); + return TimeValue.timeValueMillis(now.toInstant().toEpochMilli() - now.minusYears(years).toInstant().toEpochMilli()); } protected static TimeValue months(int months) { - DateTime now = now(); - return TimeValue.timeValueMillis(now.getMillis() - now.minusMonths(months).getMillis()); + ZonedDateTime now = now(); + return TimeValue.timeValueMillis(now.toInstant().toEpochMilli() - now.minusMonths(months).toInstant().toEpochMilli()); } protected static TimeValue days(int days) { return TimeValue.timeValueHours(days * 24); } - protected static DateTime now() { - return new DateTime(DateTimeZone.UTC); + protected static ZonedDateTime now() { + return ZonedDateTime.now(ZoneOffset.UTC); } } diff --git a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/cleaner/CleanerServiceTests.java b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/cleaner/CleanerServiceTests.java index a38406c7c721c..6ebcebd625a3a 100644 --- a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/cleaner/CleanerServiceTests.java +++ b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/cleaner/CleanerServiceTests.java @@ -13,13 +13,15 @@ import org.elasticsearch.threadpool.TestThreadPool; import org.elasticsearch.threadpool.ThreadPool; import org.elasticsearch.xpack.core.monitoring.MonitoringField; -import org.joda.time.DateTime; -import org.joda.time.DateTimeZone; import org.junit.After; import org.junit.Before; import org.junit.Rule; import org.junit.rules.ExpectedException; +import java.time.Clock; +import java.time.ZoneId; +import java.time.ZoneOffset; +import java.time.ZonedDateTime; import java.util.Collections; import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; @@ -129,20 +131,23 @@ public void testSetGlobalRetentionAppliesEvenIfLicenseDisallows() { public void testNextExecutionDelay() { CleanerService.ExecutionScheduler scheduler = new CleanerService.DefaultExecutionScheduler(); - DateTime now = new DateTime(2015, 1, 1, 0, 0, DateTimeZone.UTC); + ZonedDateTime now = ZonedDateTime.of(2015, 1, 1, 0, 0,0,0, ZoneOffset.UTC); assertThat(scheduler.nextExecutionDelay(now).millis(), equalTo(TimeValue.timeValueHours(1).millis())); - now = new DateTime(2015, 1, 1, 1, 0, DateTimeZone.UTC); + now = ZonedDateTime.of(2015, 1, 1, 1, 0, 0, 0, ZoneOffset.UTC); assertThat(scheduler.nextExecutionDelay(now).millis(), equalTo(TimeValue.timeValueHours(24).millis())); - now = new DateTime(2015, 1, 1, 0, 59, DateTimeZone.UTC); + now = ZonedDateTime.of(2015, 1, 1, 0, 59, 0, 0, ZoneOffset.UTC); assertThat(scheduler.nextExecutionDelay(now).millis(), equalTo(TimeValue.timeValueMinutes(1).millis())); - now = new DateTime(2015, 1, 1, 23, 59, DateTimeZone.UTC); + now = ZonedDateTime.of(2015, 1, 1, 23, 59, 0, 0, ZoneOffset.UTC); assertThat(scheduler.nextExecutionDelay(now).millis(), equalTo(TimeValue.timeValueMinutes(60 + 1).millis())); - now = new DateTime(2015, 1, 1, 12, 34, 56); - assertThat(scheduler.nextExecutionDelay(now).millis(), equalTo(new DateTime(2015, 1, 2, 1, 0, 0).getMillis() - now.getMillis())); + ZoneId defaultZone = Clock.systemDefaultZone().getZone(); + now = ZonedDateTime.of(2015, 1, 1, 12, 34, 56, 0, defaultZone); + long nextScheduledMillis = ZonedDateTime.of(2015, 1, 2, 1, 0, 0,0, + defaultZone).toInstant().toEpochMilli(); + assertThat(scheduler.nextExecutionDelay(now).millis(), equalTo(nextScheduledMillis - now.toInstant().toEpochMilli())); } @@ -197,7 +202,7 @@ class TestExecutionScheduler implements CleanerService.ExecutionScheduler { } @Override - public TimeValue nextExecutionDelay(DateTime now) { + public TimeValue nextExecutionDelay(ZonedDateTime now) { return TimeValue.timeValueMillis(offset); } } diff --git a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/cleaner/local/LocalIndicesCleanerTests.java b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/cleaner/local/LocalIndicesCleanerTests.java index eff2c607840db..21624ee9548e5 100644 --- a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/cleaner/local/LocalIndicesCleanerTests.java +++ b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/cleaner/local/LocalIndicesCleanerTests.java @@ -14,8 +14,8 @@ import org.elasticsearch.xpack.monitoring.cleaner.AbstractIndicesCleanerTestCase; import org.elasticsearch.xpack.monitoring.cleaner.CleanerService; import org.elasticsearch.xpack.monitoring.exporter.local.LocalExporter; -import org.joda.time.DateTime; +import java.time.ZonedDateTime; import java.util.ArrayList; import java.util.Collection; @@ -43,9 +43,10 @@ protected Settings nodeSettings(int nodeOrdinal) { } @Override - protected void createIndex(String name, DateTime creationDate) { + protected void createIndex(String name, ZonedDateTime creationDate) { + long creationMillis = creationDate.toInstant().toEpochMilli(); assertAcked(prepareCreate(name) - .setSettings(Settings.builder().put(IndexMetaData.SETTING_CREATION_DATE, creationDate.getMillis()).build())); + .setSettings(Settings.builder().put(IndexMetaData.SETTING_CREATION_DATE, creationMillis).build())); } @Override diff --git a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/collector/ml/JobStatsMonitoringDocTests.java b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/collector/ml/JobStatsMonitoringDocTests.java index 9d37073a426cc..15b0f324d1a47 100644 --- a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/collector/ml/JobStatsMonitoringDocTests.java +++ b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/collector/ml/JobStatsMonitoringDocTests.java @@ -20,10 +20,10 @@ import org.elasticsearch.xpack.core.monitoring.MonitoredSystem; import org.elasticsearch.xpack.core.monitoring.exporter.MonitoringDoc; import org.elasticsearch.xpack.monitoring.exporter.BaseMonitoringDocTestCase; -import org.joda.time.DateTime; import org.junit.Before; import java.io.IOException; +import java.time.ZonedDateTime; import java.util.Date; import static java.util.Collections.singleton; @@ -70,14 +70,13 @@ public void testConstructorJobStatsMustNotBeNull() { @Override public void testToXContent() throws IOException { final TimeValue time = TimeValue.timeValueHours(13L); - final Date date1 = DateTime.parse("2017-01-01T01:01:01.001+01").toDate(); - final Date date2 = DateTime.parse("2017-01-02T02:02:02.002+02").toDate(); - final Date date3 = DateTime.parse("2017-01-03T03:03:03.003+03").toDate(); - final Date date4 = DateTime.parse("2017-01-04T04:04:04.004+04").toDate(); - final Date date5 = DateTime.parse("2017-01-05T05:05:05.005+05").toDate(); - final Date date6 = DateTime.parse("2017-01-06T06:06:06.006+06").toDate(); - final Date date7 = DateTime.parse("2017-01-07T07:07:07.007+07").toDate(); - + final Date date1 = new Date(ZonedDateTime.parse("2017-01-01T01:01:01.001+01:00").toInstant().toEpochMilli()); + final Date date2 = new Date(ZonedDateTime.parse("2017-01-02T02:02:02.002+02:00").toInstant().toEpochMilli()); + final Date date3 = new Date(ZonedDateTime.parse("2017-01-03T03:03:03.003+03:00").toInstant().toEpochMilli()); + final Date date4 = new Date(ZonedDateTime.parse("2017-01-04T04:04:04.004+04:00").toInstant().toEpochMilli()); + final Date date5 = new Date(ZonedDateTime.parse("2017-01-05T05:05:05.005+05:00").toInstant().toEpochMilli()); + final Date date6 = new Date(ZonedDateTime.parse("2017-01-06T06:06:06.006+06:00").toInstant().toEpochMilli()); + final Date date7 = new Date(ZonedDateTime.parse("2017-01-07T07:07:07.007+07:00").toInstant().toEpochMilli()); final DiscoveryNode discoveryNode = new DiscoveryNode("_node_name", "_node_id", diff --git a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/exporter/BaseMonitoringDocTestCase.java b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/exporter/BaseMonitoringDocTestCase.java index 46ba34dcd1a50..a3b5f5d018da3 100644 --- a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/exporter/BaseMonitoringDocTestCase.java +++ b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/exporter/BaseMonitoringDocTestCase.java @@ -23,11 +23,11 @@ import org.elasticsearch.xpack.core.monitoring.exporter.MonitoringDoc; import org.elasticsearch.xpack.monitoring.MonitoringTestUtils; import org.elasticsearch.xpack.monitoring.collector.shards.ShardMonitoringDoc; -import org.joda.time.DateTime; -import org.joda.time.DateTimeZone; import org.junit.Before; import java.io.IOException; +import java.time.Instant; +import java.time.ZoneOffset; import java.util.ArrayList; import java.util.List; import java.util.Map; @@ -168,7 +168,7 @@ public final void testToXContentContainsCommonFields() throws IOException { public void testToUTC() { final long timestamp = System.currentTimeMillis(); - final String expected = new DateTime(timestamp, DateTimeZone.UTC).toString(); + final String expected = Instant.ofEpochMilli(timestamp).atZone(ZoneOffset.UTC).toString(); assertEquals(expected, MonitoringDoc.toUTC(timestamp)); } diff --git a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/exporter/ExportersTests.java b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/exporter/ExportersTests.java index e349b19b8d2b0..fde975bfab165 100644 --- a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/exporter/ExportersTests.java +++ b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/exporter/ExportersTests.java @@ -15,6 +15,7 @@ import org.elasticsearch.common.settings.Setting; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.SettingsException; +import org.elasticsearch.common.time.DateFormatter; import org.elasticsearch.common.util.concurrent.AbstractRunnable; import org.elasticsearch.common.util.concurrent.ThreadContext; import org.elasticsearch.common.xcontent.XContentBuilder; @@ -30,11 +31,15 @@ import org.junit.Before; import java.io.IOException; +import java.time.Instant; +import java.time.ZoneOffset; +import java.time.ZonedDateTime; import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; import java.util.HashSet; import java.util.List; +import java.util.Locale; import java.util.Map; import java.util.Set; import java.util.concurrent.CopyOnWriteArrayList; @@ -92,6 +97,21 @@ public void init() { exporters = new Exporters(Settings.EMPTY, factories, clusterService, licenseState, threadContext); } + public void testExporterIndexPattern() { + Exporter.Config config = mock(Exporter.Config.class); + when(config.name()).thenReturn("anything"); + when(config.settings()).thenReturn(Settings.EMPTY); + DateFormatter formatter = Exporter.dateTimeFormatter(config); + Instant instant = Instant.ofEpochSecond(randomLongBetween(0, 86400 * 365 * 130L)); + ZonedDateTime zonedDateTime = instant.atZone(ZoneOffset.UTC); + int year = zonedDateTime.getYear(); + int month = zonedDateTime.getMonthValue(); + int day = zonedDateTime.getDayOfMonth(); + String expecdateDate = String.format(Locale.ROOT, "%02d.%02d.%02d", year, month, day); + String formattedDate = formatter.format(instant); + assertThat("input date was " + instant, expecdateDate, is(formattedDate)); + } + public void testInitExportersDefault() throws Exception { factories.put("_type", TestExporter::new); Map internalExporters = exporters.initExporters(Settings.builder().build()); diff --git a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/exporter/MonitoringTemplateUtilsTests.java b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/exporter/MonitoringTemplateUtilsTests.java index 5c010439243a0..18c872a2bdeef 100644 --- a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/exporter/MonitoringTemplateUtilsTests.java +++ b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/exporter/MonitoringTemplateUtilsTests.java @@ -6,16 +6,15 @@ package org.elasticsearch.xpack.monitoring.exporter; import org.elasticsearch.common.Strings; +import org.elasticsearch.common.time.DateFormatter; import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xpack.core.monitoring.MonitoredSystem; import org.elasticsearch.xpack.core.monitoring.exporter.MonitoringTemplateUtils; -import org.joda.time.DateTime; -import org.joda.time.DateTimeZone; -import org.joda.time.format.DateTimeFormat; -import org.joda.time.format.DateTimeFormatter; import java.io.IOException; +import java.time.ZoneOffset; +import java.time.ZonedDateTime; import static org.elasticsearch.xpack.core.monitoring.exporter.MonitoringTemplateUtils.LAST_UPDATED_VERSION; import static org.elasticsearch.xpack.core.monitoring.exporter.MonitoringTemplateUtils.OLD_TEMPLATE_IDS; @@ -93,9 +92,10 @@ public void testEmptyPipeline() throws IOException { } public void testIndexName() { - final long timestamp = new DateTime(2017, 8, 3, 13, 47, 58, DateTimeZone.UTC).getMillis(); + final long timestamp = ZonedDateTime.of(2017, 8, 3, 13, 47, 58, + 0, ZoneOffset.UTC).toInstant().toEpochMilli(); - DateTimeFormatter formatter = DateTimeFormat.forPattern("YYYY.MM.dd").withZoneUTC(); + DateFormatter formatter = DateFormatter.forPattern("yyyy.MM.dd").withZone(ZoneOffset.UTC); assertThat(indexName(formatter, MonitoredSystem.ES, timestamp), equalTo(".monitoring-es-" + TEMPLATE_VERSION + "-2017.08.03")); assertThat(indexName(formatter, MonitoredSystem.KIBANA, timestamp), @@ -105,7 +105,7 @@ public void testIndexName() { assertThat(indexName(formatter, MonitoredSystem.BEATS, timestamp), equalTo(".monitoring-beats-" + TEMPLATE_VERSION + "-2017.08.03")); - formatter = DateTimeFormat.forPattern("YYYY-dd-MM-HH.mm.ss").withZoneUTC(); + formatter = DateFormatter.forPattern("yyyy-dd-MM-HH.mm.ss").withZone(ZoneOffset.UTC); assertThat(indexName(formatter, MonitoredSystem.ES, timestamp), equalTo(".monitoring-es-" + TEMPLATE_VERSION + "-2017-03-08-13.47.58")); assertThat(indexName(formatter, MonitoredSystem.KIBANA, timestamp), @@ -115,4 +115,5 @@ public void testIndexName() { assertThat(indexName(formatter, MonitoredSystem.BEATS, timestamp), equalTo(".monitoring-beats-" + TEMPLATE_VERSION + "-2017-03-08-13.47.58")); } + } diff --git a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/exporter/http/HttpExporterIT.java b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/exporter/http/HttpExporterIT.java index a6cddcf35d370..ac3abda0599dd 100644 --- a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/exporter/http/HttpExporterIT.java +++ b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/exporter/http/HttpExporterIT.java @@ -19,6 +19,7 @@ import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.collect.Tuple; import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.common.time.DateFormatter; import org.elasticsearch.common.util.concurrent.ThreadContext; import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.common.xcontent.XContentType; @@ -39,12 +40,13 @@ import org.elasticsearch.xpack.monitoring.exporter.ExportBulk; import org.elasticsearch.xpack.monitoring.exporter.Exporter; import org.elasticsearch.xpack.monitoring.test.MonitoringIntegTestCase; -import org.joda.time.format.DateTimeFormat; import org.junit.After; import org.junit.Before; import java.io.IOException; import java.nio.charset.StandardCharsets; +import java.time.Instant; +import java.time.ZoneOffset; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; @@ -352,7 +354,8 @@ public void testDynamicIndexFormatChange() throws Exception { remoteClusterAllowsWatcher, currentLicenseAllowsWatcher, watcherAlreadyExists); MockRequest recordedRequest = assertBulk(webServer); - String indexName = indexName(DateTimeFormat.forPattern("YYYY.MM.dd").withZoneUTC(), doc.getSystem(), doc.getTimestamp()); + DateFormatter formatter = DateFormatter.forPattern("yyyy.MM.dd").withZone(ZoneOffset.UTC); + String indexName = indexName(formatter, doc.getSystem(), doc.getTimestamp()); byte[] bytes = recordedRequest.getBody().getBytes(StandardCharsets.UTF_8); Map data = XContentHelper.convertToMap(new BytesArray(bytes), false, XContentType.JSON).v2(); @@ -360,7 +363,7 @@ public void testDynamicIndexFormatChange() throws Exception { Map index = (Map) data.get("index"); assertThat(index.get("_index"), equalTo(indexName)); - String newTimeFormat = randomFrom("YY", "YYYY", "YYYY.MM", "YYYY-MM", "MM.YYYY", "MM"); + String newTimeFormat = randomFrom("yy", "yyyy", "yyyy.MM", "yyyy-MM", "MM.yyyy", "MM"); final Settings newSettings = Settings.builder() .put(settings) @@ -375,8 +378,10 @@ public void testDynamicIndexFormatChange() throws Exception { doc = newRandomMonitoringDoc(); export(newSettings, Collections.singletonList(doc)); + DateFormatter newTimeFormatter = DateFormatter.forPattern(newTimeFormat).withZone(ZoneOffset.UTC); + String expectedMonitoringIndex = ".monitoring-es-" + TEMPLATE_VERSION + "-" - + DateTimeFormat.forPattern(newTimeFormat).withZoneUTC().print(doc.getTimestamp()); + + newTimeFormatter.format(Instant.ofEpochMilli(doc.getTimestamp())); assertMonitorResources(webServer, true, includeOldTemplates, true, true, true, true); diff --git a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/exporter/local/LocalExporterIntegTests.java b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/exporter/local/LocalExporterIntegTests.java index 6caefe148b28a..cf82f3e55cd78 100644 --- a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/exporter/local/LocalExporterIntegTests.java +++ b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/exporter/local/LocalExporterIntegTests.java @@ -15,6 +15,7 @@ import org.elasticsearch.common.Strings; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.common.time.DateFormatter; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.index.query.QueryBuilders; @@ -30,13 +31,11 @@ import org.elasticsearch.xpack.core.monitoring.exporter.MonitoringTemplateUtils; import org.elasticsearch.xpack.monitoring.MonitoringService; import org.elasticsearch.xpack.monitoring.MonitoringTestUtils; -import org.joda.time.DateTime; -import org.joda.time.DateTimeZone; -import org.joda.time.format.DateTimeFormat; -import org.joda.time.format.DateTimeFormatter; -import org.joda.time.format.ISODateTimeFormat; import java.io.IOException; +import java.time.Instant; +import java.time.ZoneOffset; +import java.time.ZonedDateTime; import java.util.ArrayList; import java.util.Arrays; import java.util.HashSet; @@ -183,7 +182,7 @@ public void testExport() throws Exception { // node_stats document collected for each node is at least 10 seconds old, corresponding to // 2 or 3 elapsed collection intervals. final int elapsedInSeconds = 10; - final DateTime startTime = DateTime.now(DateTimeZone.UTC); + final ZonedDateTime startTime = ZonedDateTime.now(ZoneOffset.UTC); assertBusy(() -> { IndicesExistsResponse indicesExistsResponse = client().admin().indices().prepareExists(".monitoring-*").get(); if (indicesExistsResponse.isExists()) { @@ -205,11 +204,11 @@ public void testExport() throws Exception { assertTrue(bucket.getDocCount() >= 1L); Max subAggregation = bucket.getAggregations().get("agg_last_time_collected"); - DateTime lastCollection = new DateTime(Math.round(subAggregation.getValue()), DateTimeZone.UTC); - assertTrue(lastCollection.plusSeconds(elapsedInSeconds).isBefore(DateTime.now(DateTimeZone.UTC))); + ZonedDateTime lastCollection = Instant.ofEpochMilli(Math.round(subAggregation.getValue())).atZone(ZoneOffset.UTC); + assertTrue(lastCollection.plusSeconds(elapsedInSeconds).isBefore(ZonedDateTime.now(ZoneOffset.UTC))); } } else { - assertTrue(DateTime.now(DateTimeZone.UTC).isAfter(startTime.plusSeconds(elapsedInSeconds))); + assertTrue(ZonedDateTime.now(ZoneOffset.UTC).isAfter(startTime.plusSeconds(elapsedInSeconds))); } }, 30L, TimeUnit.SECONDS); } @@ -256,11 +255,11 @@ private void checkMonitoringDocs() { .get("xpack.monitoring.exporters._local.index.name.time_format"); assertEquals(indexTimeFormat, customTimeFormat); if (customTimeFormat == null) { - customTimeFormat = "YYYY.MM.dd"; + customTimeFormat = "yyyy.MM.dd"; } - DateTimeFormatter dateParser = ISODateTimeFormat.dateTime().withZoneUTC(); - DateTimeFormatter dateFormatter = DateTimeFormat.forPattern(customTimeFormat).withZoneUTC(); + DateFormatter dateParser = DateFormatter.forPattern("strict_date_time"); + DateFormatter dateFormatter = DateFormatter.forPattern(customTimeFormat).withZone(ZoneOffset.UTC); SearchResponse searchResponse = client().prepareSearch(".monitoring-*").setSize(100).get(); assertThat(searchResponse.getHits().getTotalHits().value, greaterThan(0L)); @@ -290,7 +289,7 @@ private void checkMonitoringDocs() { expectedSystem = MonitoredSystem.fromSystem((String) docSource.get("expected_system")); } - String dateTime = dateFormatter.print(dateParser.parseDateTime(timestamp)); + String dateTime = dateFormatter.format(dateParser.parse(timestamp)); final String expectedIndex = ".monitoring-" + expectedSystem.getSystem() + "-" + TEMPLATE_VERSION + "-" + dateTime; assertEquals("Expected " + expectedIndex + " but got " + hit.getIndex(), expectedIndex, hit.getIndex()); diff --git a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/integration/MonitoringIT.java b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/integration/MonitoringIT.java index 555f2659113fd..1685de2667a76 100644 --- a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/integration/MonitoringIT.java +++ b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/integration/MonitoringIT.java @@ -18,6 +18,7 @@ import org.elasticsearch.common.Strings; import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.common.time.DateFormatter; import org.elasticsearch.common.unit.TimeValue; import org.elasticsearch.common.xcontent.ToXContentObject; import org.elasticsearch.common.xcontent.XContentBuilder; @@ -50,8 +51,6 @@ import org.elasticsearch.xpack.monitoring.collector.node.NodeStatsMonitoringDoc; import org.elasticsearch.xpack.monitoring.collector.shards.ShardMonitoringDoc; import org.elasticsearch.xpack.monitoring.test.MockIngestPlugin; -import org.joda.time.format.DateTimeFormat; -import org.joda.time.format.ISODateTimeFormat; import java.io.IOException; import java.lang.Thread.State; @@ -59,6 +58,8 @@ import java.lang.management.ManagementFactory; import java.lang.management.MonitorInfo; import java.lang.management.ThreadInfo; +import java.time.Instant; +import java.time.ZoneOffset; import java.util.Arrays; import java.util.Collection; import java.util.List; @@ -277,9 +278,10 @@ private void assertMonitoringDoc(final Map document, assertThat(((Number) source.get("interval_ms")).longValue(), equalTo(interval.getMillis())); - assertThat(index, equalTo(MonitoringTemplateUtils.indexName(DateTimeFormat.forPattern("YYYY.MM.dd").withZoneUTC(), - expectedSystem, - ISODateTimeFormat.dateTime().parseMillis(timestamp)))); + DateFormatter formatter = DateFormatter.forPattern("yyyy.MM.dd"); + long isoTimestamp = Instant.from(DateFormatter.forPattern("strict_date_time").parse(timestamp)).toEpochMilli(); + String isoDateTime = MonitoringTemplateUtils.indexName(formatter.withZone(ZoneOffset.UTC), expectedSystem, isoTimestamp); + assertThat(index, equalTo(isoDateTime)); final Map sourceNode = (Map) source.get("source_node"); if (sourceNode != null) {