Core: Migrating from joda to java.time. Monitoring plugin (#36297)

monitoring plugin migration from joda to java.time

refers #27330
This commit is contained in:
Przemyslaw Gomulka 2019-02-04 14:47:08 +01:00 committed by GitHub
parent 7ed3e6e07e
commit 85b4bfe3ff
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
18 changed files with 156 additions and 113 deletions

View File

@ -10,13 +10,15 @@ import org.elasticsearch.common.Nullable;
import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.common.io.stream.Writeable;
import org.elasticsearch.common.time.DateFormatter;
import org.elasticsearch.common.xcontent.ToXContentObject; import org.elasticsearch.common.xcontent.ToXContentObject;
import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.xpack.core.monitoring.MonitoredSystem; import org.elasticsearch.xpack.core.monitoring.MonitoredSystem;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import java.io.IOException; import java.io.IOException;
import java.time.Instant;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.util.Objects; import java.util.Objects;
/** /**
@ -24,6 +26,7 @@ import java.util.Objects;
*/ */
public abstract class MonitoringDoc implements ToXContentObject { public abstract class MonitoringDoc implements ToXContentObject {
private static final DateFormatter dateTimeFormatter = DateFormatter.forPattern("strict_date_time");
private final String cluster; private final String cluster;
private final long timestamp; private final long timestamp;
private final long intervalMillis; private final long intervalMillis;
@ -123,7 +126,9 @@ public abstract class MonitoringDoc implements ToXContentObject {
* @return a string representing the timestamp * @return a string representing the timestamp
*/ */
public static String toUTC(final long 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 abstract class MonitoringDoc implements ToXContentObject {
return Objects.hash(uuid, host, transportAddress, ip, name, timestamp); return Objects.hash(uuid, host, transportAddress, ip, name, timestamp);
} }
} }
} }

View File

@ -6,15 +6,16 @@
package org.elasticsearch.xpack.core.monitoring.exporter; package org.elasticsearch.xpack.core.monitoring.exporter;
import org.elasticsearch.Version; import org.elasticsearch.Version;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.time.DateFormatter;
import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.xpack.core.monitoring.MonitoredSystem; import org.elasticsearch.xpack.core.monitoring.MonitoredSystem;
import org.elasticsearch.xpack.core.template.TemplateUtils; import org.elasticsearch.xpack.core.template.TemplateUtils;
import org.joda.time.format.DateTimeFormatter;
import org.elasticsearch.common.Strings;
import java.io.IOException; import java.io.IOException;
import java.time.Instant;
import java.util.Locale; import java.util.Locale;
import java.util.regex.Pattern; import java.util.regex.Pattern;
@ -252,12 +253,12 @@ public final class MonitoringTemplateUtils {
/** /**
* Get the index name given a specific date format, a monitored system and a timestamp. * 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 system the {@link MonitoredSystem} for which the index name is computed
* @param timestamp the timestamp value to use to compute the timestamped index name * @param timestamp the timestamp value to use to compute the timestamped index name
* @return the index name as a @{link String} * @return the index name as a @{link String}
*/ */
public static String indexName(final DateTimeFormatter formatter, final MonitoredSystem system, final long timestamp) { public static String indexName(final DateFormatter formatter, final MonitoredSystem system, final long timestamp) {
return ".monitoring-" + system.getSystem() + "-" + TEMPLATE_VERSION + "-" + formatter.print(timestamp); return ".monitoring-" + system.getSystem() + "-" + TEMPLATE_VERSION + "-" + formatter.format(Instant.ofEpochMilli(timestamp));
} }
} }

View File

@ -17,9 +17,10 @@ import org.elasticsearch.license.XPackLicenseState;
import org.elasticsearch.threadpool.Scheduler; import org.elasticsearch.threadpool.Scheduler;
import org.elasticsearch.threadpool.ThreadPool; import org.elasticsearch.threadpool.ThreadPool;
import org.elasticsearch.xpack.core.monitoring.MonitoringField; 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.List;
import java.util.concurrent.CopyOnWriteArrayList; import java.util.concurrent.CopyOnWriteArrayList;
@ -56,7 +57,8 @@ public class CleanerService extends AbstractLifecycleComponent {
@Override @Override
protected void doStart() { protected void doStart() {
logger.debug("starting cleaning service"); 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"); logger.debug("cleaning service started");
} }
@ -190,7 +192,7 @@ public class CleanerService extends AbstractLifecycleComponent {
*/ */
@Override @Override
protected void onAfterInLifecycle() { protected void onAfterInLifecycle() {
DateTime start = new DateTime(ISOChronology.getInstance()); ZonedDateTime start = ZonedDateTime.now(Clock.systemUTC());
TimeValue delay = executionScheduler.nextExecutionDelay(start); TimeValue delay = executionScheduler.nextExecutionDelay(start);
logger.debug("scheduling next execution in [{}] seconds", delay.seconds()); logger.debug("scheduling next execution in [{}] seconds", delay.seconds());
@ -233,7 +235,7 @@ public class CleanerService extends AbstractLifecycleComponent {
* @param now the current time * @param now the current time
* @return the delay in millis * @return the delay in millis
*/ */
TimeValue nextExecutionDelay(DateTime now); TimeValue nextExecutionDelay(ZonedDateTime now);
} }
/** /**
@ -242,14 +244,16 @@ public class CleanerService extends AbstractLifecycleComponent {
static class DefaultExecutionScheduler implements ExecutionScheduler { static class DefaultExecutionScheduler implements ExecutionScheduler {
@Override @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 // 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 it's not after now, then it needs to be the next day!
if (next.isAfter(now) == false) { if (next.isAfter(now) == false) {
next = next.plusDays(1); next = next.plusDays(1);
} }
return TimeValue.timeValueMillis(next.getMillis() - now.getMillis()); return TimeValue.timeValueMillis(Duration.between(now, next).toMillis());
} }
} }
} }

View File

@ -11,10 +11,10 @@ import org.elasticsearch.common.settings.Setting;
import org.elasticsearch.common.settings.Setting.Property; import org.elasticsearch.common.settings.Setting.Property;
import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.settings.SettingsException; import org.elasticsearch.common.settings.SettingsException;
import org.elasticsearch.common.time.DateFormatter;
import org.elasticsearch.license.XPackLicenseState; 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.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
@ -70,7 +70,7 @@ public abstract class Exporter implements AutoCloseable {
Setting.affixKeySetting("xpack.monitoring.exporters.","index.name.time_format", Setting.affixKeySetting("xpack.monitoring.exporters.","index.name.time_format",
key -> Setting.simpleString(key, Property.Dynamic, Property.NodeScope)); 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; protected final Config config;
@ -113,11 +113,11 @@ public abstract class Exporter implements AutoCloseable {
protected abstract void doClose(); protected abstract void doClose();
protected static DateTimeFormatter dateTimeFormatter(final Config config) { protected static DateFormatter dateTimeFormatter(final Config config) {
Setting<String> setting = INDEX_NAME_TIME_FORMAT_SETTING.getConcreteSettingForNamespace(config.name); Setting<String> setting = INDEX_NAME_TIME_FORMAT_SETTING.getConcreteSettingForNamespace(config.name);
String format = setting.exists(config.settings()) ? setting.get(config.settings()) : INDEX_FORMAT; String format = setting.exists(config.settings()) ? setting.get(config.settings()) : INDEX_FORMAT;
try { try {
return DateTimeFormat.forPattern(format).withZoneUTC(); return DateFormatter.forPattern(format).withZone(ZoneOffset.UTC);
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {
throw new SettingsException("[" + INDEX_NAME_TIME_FORMAT_SETTING.getKey() + "] invalid index name time format: [" throw new SettingsException("[" + INDEX_NAME_TIME_FORMAT_SETTING.getKey() + "] invalid index name time format: ["
+ format + "]", e); + format + "]", e);

View File

@ -19,6 +19,7 @@ import org.elasticsearch.client.ResponseListener;
import org.elasticsearch.client.RestClient; import org.elasticsearch.client.RestClient;
import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.io.stream.BytesStreamOutput; import org.elasticsearch.common.io.stream.BytesStreamOutput;
import org.elasticsearch.common.time.DateFormatter;
import org.elasticsearch.common.util.concurrent.ThreadContext; import org.elasticsearch.common.util.concurrent.ThreadContext;
import org.elasticsearch.common.xcontent.XContent; import org.elasticsearch.common.xcontent.XContent;
import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentBuilder;
@ -28,9 +29,9 @@ import org.elasticsearch.xpack.core.monitoring.exporter.MonitoringDoc;
import org.elasticsearch.xpack.core.monitoring.exporter.MonitoringTemplateUtils; import org.elasticsearch.xpack.core.monitoring.exporter.MonitoringTemplateUtils;
import org.elasticsearch.xpack.monitoring.exporter.ExportBulk; import org.elasticsearch.xpack.monitoring.exporter.ExportBulk;
import org.elasticsearch.xpack.monitoring.exporter.ExportException; import org.elasticsearch.xpack.monitoring.exporter.ExportException;
import org.joda.time.format.DateTimeFormatter;
import java.io.IOException; import java.io.IOException;
import java.time.format.DateTimeFormatter;
import java.util.Collection; import java.util.Collection;
import java.util.Map; import java.util.Map;
@ -54,7 +55,7 @@ class HttpExportBulk extends ExportBulk {
/** /**
* {@link DateTimeFormatter} used to resolve timestamped index name. * {@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)}. * 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; private byte[] payload = null;
HttpExportBulk(final String name, final RestClient client, final Map<String, String> parameters, HttpExportBulk(final String name, final RestClient client, final Map<String, String> parameters,
final DateTimeFormatter dateTimeFormatter, final ThreadContext threadContext) { final DateFormatter dateTimeFormatter, final ThreadContext threadContext) {
super(name, threadContext); super(name, threadContext);
this.client = client; this.client = client;

View File

@ -30,6 +30,7 @@ import org.elasticsearch.common.settings.Setting;
import org.elasticsearch.common.settings.Setting.Property; import org.elasticsearch.common.settings.Setting.Property;
import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.settings.SettingsException; import org.elasticsearch.common.settings.SettingsException;
import org.elasticsearch.common.time.DateFormatter;
import org.elasticsearch.common.unit.TimeValue; import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.util.concurrent.ThreadContext; import org.elasticsearch.common.util.concurrent.ThreadContext;
import org.elasticsearch.common.util.set.Sets; import org.elasticsearch.common.util.set.Sets;
@ -41,7 +42,6 @@ import org.elasticsearch.xpack.core.ssl.SSLService;
import org.elasticsearch.xpack.monitoring.exporter.ClusterAlertsUtil; import org.elasticsearch.xpack.monitoring.exporter.ClusterAlertsUtil;
import org.elasticsearch.xpack.monitoring.exporter.ExportBulk; import org.elasticsearch.xpack.monitoring.exporter.ExportBulk;
import org.elasticsearch.xpack.monitoring.exporter.Exporter; import org.elasticsearch.xpack.monitoring.exporter.Exporter;
import org.joda.time.format.DateTimeFormatter;
import javax.net.ssl.SSLContext; import javax.net.ssl.SSLContext;
import java.util.ArrayList; import java.util.ArrayList;
@ -193,7 +193,7 @@ public class HttpExporter extends Exporter {
private final AtomicBoolean clusterAlertsAllowed = new AtomicBoolean(false); private final AtomicBoolean clusterAlertsAllowed = new AtomicBoolean(false);
private final ThreadContext threadContext; private final ThreadContext threadContext;
private final DateTimeFormatter dateTimeFormatter; private final DateFormatter dateTimeFormatter;
/** /**
* Create an {@link HttpExporter}. * Create an {@link HttpExporter}.

View File

@ -14,13 +14,13 @@ import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.Client; import org.elasticsearch.client.Client;
import org.elasticsearch.common.Strings; import org.elasticsearch.common.Strings;
import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.time.DateFormatter;
import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.common.xcontent.XContentHelper;
import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.xpack.core.monitoring.exporter.MonitoringDoc; import org.elasticsearch.xpack.core.monitoring.exporter.MonitoringDoc;
import org.elasticsearch.xpack.core.monitoring.exporter.MonitoringTemplateUtils; import org.elasticsearch.xpack.core.monitoring.exporter.MonitoringTemplateUtils;
import org.elasticsearch.xpack.monitoring.exporter.ExportBulk; import org.elasticsearch.xpack.monitoring.exporter.ExportBulk;
import org.elasticsearch.xpack.monitoring.exporter.ExportException; import org.elasticsearch.xpack.monitoring.exporter.ExportException;
import org.joda.time.format.DateTimeFormatter;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
@ -37,13 +37,13 @@ public class LocalBulk extends ExportBulk {
private final Logger logger; private final Logger logger;
private final Client client; private final Client client;
private final DateTimeFormatter formatter; private final DateFormatter formatter;
private final boolean usePipeline; private final boolean usePipeline;
private BulkRequestBuilder requestBuilder; 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()); super(name, client.threadPool().getThreadContext());
this.logger = logger; this.logger = logger;
this.client = client; this.client = client;

View File

@ -28,6 +28,7 @@ import org.elasticsearch.common.Nullable;
import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.common.bytes.BytesArray;
import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.regex.Regex; import org.elasticsearch.common.regex.Regex;
import org.elasticsearch.common.time.DateFormatter;
import org.elasticsearch.common.unit.TimeValue; import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.util.concurrent.ThreadContext; import org.elasticsearch.common.util.concurrent.ThreadContext;
import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.common.xcontent.XContentType;
@ -51,10 +52,11 @@ import org.elasticsearch.xpack.monitoring.cleaner.CleanerService;
import org.elasticsearch.xpack.monitoring.exporter.ClusterAlertsUtil; import org.elasticsearch.xpack.monitoring.exporter.ClusterAlertsUtil;
import org.elasticsearch.xpack.monitoring.exporter.ExportBulk; import org.elasticsearch.xpack.monitoring.exporter.ExportBulk;
import org.elasticsearch.xpack.monitoring.exporter.Exporter; 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.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.HashSet; import java.util.HashSet;
@ -89,7 +91,7 @@ public class LocalExporter extends Exporter implements ClusterStateListener, Cle
private final XPackLicenseState licenseState; private final XPackLicenseState licenseState;
private final CleanerService cleanerService; private final CleanerService cleanerService;
private final boolean useIngest; private final boolean useIngest;
private final DateTimeFormatter dateTimeFormatter; private final DateFormatter dateTimeFormatter;
private final List<String> clusterAlertBlacklist; private final List<String> clusterAlertBlacklist;
private final AtomicReference<State> state = new AtomicReference<>(State.INITIALIZED); private final AtomicReference<State> state = new AtomicReference<>(State.INITIALIZED);
@ -489,12 +491,12 @@ public class LocalExporter extends Exporter implements ClusterStateListener, Cle
if (clusterService.state().nodes().isLocalNodeElectedMaster()) { if (clusterService.state().nodes().isLocalNodeElectedMaster()) {
// Reference date time will be compared to index.creation_date settings, // Reference date time will be compared to index.creation_date settings,
// that's why it must be in UTC // 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); logger.debug("cleaning indices [expiration={}, retention={}]", expiration, retention);
ClusterState clusterState = clusterService.state(); ClusterState clusterState = clusterService.state();
if (clusterState != null) { if (clusterState != null) {
final long expirationTimeMillis = expiration.getMillis(); final long expirationTimeMillis = expiration.toInstant().toEpochMilli();
final long currentTimeMillis = System.currentTimeMillis(); final long currentTimeMillis = System.currentTimeMillis();
final boolean cleanUpWatcherHistory = clusterService.getClusterSettings().get(CLEAN_WATCHER_HISTORY); final boolean cleanUpWatcherHistory = clusterService.getClusterSettings().get(CLEAN_WATCHER_HISTORY);
@ -524,7 +526,7 @@ public class LocalExporter extends Exporter implements ClusterStateListener, Cle
if (creationDate <= expirationTimeMillis) { if (creationDate <= expirationTimeMillis) {
if (logger.isDebugEnabled()) { if (logger.isDebugEnabled()) {
logger.debug("detected expired index [name={}, created={}, expired={}]", 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); indices.add(indexName);
} }

View File

@ -6,6 +6,7 @@
package org.elasticsearch.xpack.monitoring.cleaner; package org.elasticsearch.xpack.monitoring.cleaner;
import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.time.DateFormatter;
import org.elasticsearch.common.unit.TimeValue; import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.test.ESIntegTestCase.ClusterScope; import org.elasticsearch.test.ESIntegTestCase.ClusterScope;
import org.elasticsearch.xpack.core.monitoring.MonitoringField; import org.elasticsearch.xpack.core.monitoring.MonitoringField;
@ -13,11 +14,9 @@ import org.elasticsearch.xpack.core.monitoring.exporter.MonitoringTemplateUtils;
import org.elasticsearch.xpack.monitoring.exporter.Exporter; import org.elasticsearch.xpack.monitoring.exporter.Exporter;
import org.elasticsearch.xpack.monitoring.exporter.Exporters; import org.elasticsearch.xpack.monitoring.exporter.Exporters;
import org.elasticsearch.xpack.monitoring.test.MonitoringIntegTestCase; 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 java.util.Locale;
import static org.elasticsearch.test.ESIntegTestCase.Scope.TEST; import static org.elasticsearch.test.ESIntegTestCase.Scope.TEST;
@ -25,6 +24,7 @@ import static org.elasticsearch.test.ESIntegTestCase.Scope.TEST;
@ClusterScope(scope = TEST, numDataNodes = 0, numClientNodes = 0, transportClientRatio = 0.0) @ClusterScope(scope = TEST, numDataNodes = 0, numClientNodes = 0, transportClientRatio = 0.0)
public abstract class AbstractIndicesCleanerTestCase extends MonitoringIntegTestCase { 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; static Integer INDEX_TEMPLATE_VERSION = null;
public void testNothingToDelete() throws Exception { public void testNothingToDelete() throws Exception {
@ -108,7 +108,7 @@ public abstract class AbstractIndicesCleanerTestCase extends MonitoringIntegTest
CleanerService.Listener listener = getListener(); CleanerService.Listener listener = getListener();
final DateTime now = now(); final ZonedDateTime now = now();
createTimestampedIndex(now.minusYears(1)); createTimestampedIndex(now.minusYears(1));
createTimestampedIndex(now.minusMonths(6)); createTimestampedIndex(now.minusMonths(6));
createTimestampedIndex(now.minusMonths(1)); createTimestampedIndex(now.minusMonths(1));
@ -147,7 +147,7 @@ public abstract class AbstractIndicesCleanerTestCase extends MonitoringIntegTest
internalCluster().startNode(Settings.builder().put(MonitoringField.HISTORY_DURATION.getKey(), internalCluster().startNode(Settings.builder().put(MonitoringField.HISTORY_DURATION.getKey(),
String.format(Locale.ROOT, "%dd", retention))); String.format(Locale.ROOT, "%dd", retention)));
final DateTime now = now(); final ZonedDateTime now = now();
for (int i = 0; i < max; i++) { for (int i = 0; i < max; i++) {
createTimestampedIndex(now.minusDays(i)); createTimestampedIndex(now.minusDays(i));
} }
@ -172,21 +172,21 @@ public abstract class AbstractIndicesCleanerTestCase extends MonitoringIntegTest
/** /**
* Creates a monitoring alerts index from the current version. * 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); createAlertsIndex(creationDate, MonitoringTemplateUtils.TEMPLATE_VERSION);
} }
/** /**
* Creates a monitoring alerts index from the specified 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); createIndex(".monitoring-alerts-" + version, creationDate);
} }
/** /**
* Creates a watcher history index from the current version. * 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) { if (INDEX_TEMPLATE_VERSION == null) {
INDEX_TEMPLATE_VERSION = randomIntBetween(1, 20); INDEX_TEMPLATE_VERSION = randomIntBetween(1, 20);
} }
@ -196,9 +196,8 @@ public abstract class AbstractIndicesCleanerTestCase extends MonitoringIntegTest
/** /**
* Creates a watcher history index from the specified version. * Creates a watcher history index from the specified version.
*/ */
protected void createWatcherHistoryIndex(final DateTime creationDate, final String version) { protected void createWatcherHistoryIndex(final ZonedDateTime creationDate, final String version) {
final DateTimeFormatter formatter = DateTimeFormat.forPattern("YYYY.MM.dd").withZoneUTC(); final String index = ".watcher-history-" + version + "-" + DATE_FORMATTER.format(creationDate);
final String index = ".watcher-history-" + version + "-" + formatter.print(creationDate.getMillis());
createIndex(index, creationDate); createIndex(index, creationDate);
} }
@ -206,38 +205,37 @@ public abstract class AbstractIndicesCleanerTestCase extends MonitoringIntegTest
/** /**
* Creates a monitoring timestamped index using the current template version. * 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); createTimestampedIndex(creationDate, MonitoringTemplateUtils.TEMPLATE_VERSION);
} }
/** /**
* Creates a monitoring timestamped index using a given template version. * Creates a monitoring timestamped index using a given template version.
*/ */
protected void createTimestampedIndex(DateTime creationDate, String version) { protected void createTimestampedIndex(ZonedDateTime creationDate, String version) {
final DateTimeFormatter formatter = DateTimeFormat.forPattern("YYYY.MM.dd").withZoneUTC(); final String index = ".monitoring-es-" + version + "-" + DATE_FORMATTER.format(creationDate);
final String index = ".monitoring-es-" + version + "-" + formatter.print(creationDate.getMillis());
createIndex(index, 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 abstract void assertIndicesCount(int count) throws Exception;
protected static TimeValue years(int years) { protected static TimeValue years(int years) {
DateTime now = now(); ZonedDateTime now = now();
return TimeValue.timeValueMillis(now.getMillis() - now.minusYears(years).getMillis()); return TimeValue.timeValueMillis(now.toInstant().toEpochMilli() - now.minusYears(years).toInstant().toEpochMilli());
} }
protected static TimeValue months(int months) { protected static TimeValue months(int months) {
DateTime now = now(); ZonedDateTime now = now();
return TimeValue.timeValueMillis(now.getMillis() - now.minusMonths(months).getMillis()); return TimeValue.timeValueMillis(now.toInstant().toEpochMilli() - now.minusMonths(months).toInstant().toEpochMilli());
} }
protected static TimeValue days(int days) { protected static TimeValue days(int days) {
return TimeValue.timeValueHours(days * 24); return TimeValue.timeValueHours(days * 24);
} }
protected static DateTime now() { protected static ZonedDateTime now() {
return new DateTime(DateTimeZone.UTC); return ZonedDateTime.now(ZoneOffset.UTC);
} }
} }

View File

@ -13,13 +13,15 @@ import org.elasticsearch.test.ESTestCase;
import org.elasticsearch.threadpool.TestThreadPool; import org.elasticsearch.threadpool.TestThreadPool;
import org.elasticsearch.threadpool.ThreadPool; import org.elasticsearch.threadpool.ThreadPool;
import org.elasticsearch.xpack.core.monitoring.MonitoringField; import org.elasticsearch.xpack.core.monitoring.MonitoringField;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.junit.After; import org.junit.After;
import org.junit.Before; import org.junit.Before;
import org.junit.Rule; import org.junit.Rule;
import org.junit.rules.ExpectedException; 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.Collections;
import java.util.concurrent.CountDownLatch; import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
@ -129,20 +131,23 @@ public class CleanerServiceTests extends ESTestCase {
public void testNextExecutionDelay() { public void testNextExecutionDelay() {
CleanerService.ExecutionScheduler scheduler = new CleanerService.DefaultExecutionScheduler(); 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())); 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())); 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())); 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())); assertThat(scheduler.nextExecutionDelay(now).millis(), equalTo(TimeValue.timeValueMinutes(60 + 1).millis()));
now = new DateTime(2015, 1, 1, 12, 34, 56); ZoneId defaultZone = Clock.systemDefaultZone().getZone();
assertThat(scheduler.nextExecutionDelay(now).millis(), equalTo(new DateTime(2015, 1, 2, 1, 0, 0).getMillis() - now.getMillis())); 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 @@ public class CleanerServiceTests extends ESTestCase {
} }
@Override @Override
public TimeValue nextExecutionDelay(DateTime now) { public TimeValue nextExecutionDelay(ZonedDateTime now) {
return TimeValue.timeValueMillis(offset); return TimeValue.timeValueMillis(offset);
} }
} }

View File

@ -14,8 +14,8 @@ import org.elasticsearch.test.InternalSettingsPlugin;
import org.elasticsearch.xpack.monitoring.cleaner.AbstractIndicesCleanerTestCase; import org.elasticsearch.xpack.monitoring.cleaner.AbstractIndicesCleanerTestCase;
import org.elasticsearch.xpack.monitoring.cleaner.CleanerService; import org.elasticsearch.xpack.monitoring.cleaner.CleanerService;
import org.elasticsearch.xpack.monitoring.exporter.local.LocalExporter; import org.elasticsearch.xpack.monitoring.exporter.local.LocalExporter;
import org.joda.time.DateTime;
import java.time.ZonedDateTime;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
@ -43,9 +43,10 @@ public class LocalIndicesCleanerTests extends AbstractIndicesCleanerTestCase {
} }
@Override @Override
protected void createIndex(String name, DateTime creationDate) { protected void createIndex(String name, ZonedDateTime creationDate) {
long creationMillis = creationDate.toInstant().toEpochMilli();
assertAcked(prepareCreate(name) 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 @Override

View File

@ -20,10 +20,10 @@ import org.elasticsearch.xpack.core.ml.stats.ForecastStats;
import org.elasticsearch.xpack.core.monitoring.MonitoredSystem; import org.elasticsearch.xpack.core.monitoring.MonitoredSystem;
import org.elasticsearch.xpack.core.monitoring.exporter.MonitoringDoc; import org.elasticsearch.xpack.core.monitoring.exporter.MonitoringDoc;
import org.elasticsearch.xpack.monitoring.exporter.BaseMonitoringDocTestCase; import org.elasticsearch.xpack.monitoring.exporter.BaseMonitoringDocTestCase;
import org.joda.time.DateTime;
import org.junit.Before; import org.junit.Before;
import java.io.IOException; import java.io.IOException;
import java.time.ZonedDateTime;
import java.util.Date; import java.util.Date;
import static java.util.Collections.singleton; import static java.util.Collections.singleton;
@ -70,14 +70,13 @@ public class JobStatsMonitoringDocTests extends BaseMonitoringDocTestCase<JobSta
@Override @Override
public void testToXContent() throws IOException { public void testToXContent() throws IOException {
final TimeValue time = TimeValue.timeValueHours(13L); final TimeValue time = TimeValue.timeValueHours(13L);
final Date date1 = DateTime.parse("2017-01-01T01:01:01.001+01").toDate(); final Date date1 = new Date(ZonedDateTime.parse("2017-01-01T01:01:01.001+01:00").toInstant().toEpochMilli());
final Date date2 = DateTime.parse("2017-01-02T02:02:02.002+02").toDate(); final Date date2 = new Date(ZonedDateTime.parse("2017-01-02T02:02:02.002+02:00").toInstant().toEpochMilli());
final Date date3 = DateTime.parse("2017-01-03T03:03:03.003+03").toDate(); final Date date3 = new Date(ZonedDateTime.parse("2017-01-03T03:03:03.003+03:00").toInstant().toEpochMilli());
final Date date4 = DateTime.parse("2017-01-04T04:04:04.004+04").toDate(); final Date date4 = new Date(ZonedDateTime.parse("2017-01-04T04:04:04.004+04:00").toInstant().toEpochMilli());
final Date date5 = DateTime.parse("2017-01-05T05:05:05.005+05").toDate(); final Date date5 = new Date(ZonedDateTime.parse("2017-01-05T05:05:05.005+05:00").toInstant().toEpochMilli());
final Date date6 = DateTime.parse("2017-01-06T06:06:06.006+06").toDate(); final Date date6 = new Date(ZonedDateTime.parse("2017-01-06T06:06:06.006+06:00").toInstant().toEpochMilli());
final Date date7 = DateTime.parse("2017-01-07T07:07:07.007+07").toDate(); final Date date7 = new Date(ZonedDateTime.parse("2017-01-07T07:07:07.007+07:00").toInstant().toEpochMilli());
final DiscoveryNode discoveryNode = new DiscoveryNode("_node_name", final DiscoveryNode discoveryNode = new DiscoveryNode("_node_name",
"_node_id", "_node_id",

View File

@ -23,11 +23,11 @@ import org.elasticsearch.xpack.core.monitoring.MonitoredSystem;
import org.elasticsearch.xpack.core.monitoring.exporter.MonitoringDoc; import org.elasticsearch.xpack.core.monitoring.exporter.MonitoringDoc;
import org.elasticsearch.xpack.monitoring.MonitoringTestUtils; import org.elasticsearch.xpack.monitoring.MonitoringTestUtils;
import org.elasticsearch.xpack.monitoring.collector.shards.ShardMonitoringDoc; import org.elasticsearch.xpack.monitoring.collector.shards.ShardMonitoringDoc;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.junit.Before; import org.junit.Before;
import java.io.IOException; import java.io.IOException;
import java.time.Instant;
import java.time.ZoneOffset;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -168,7 +168,7 @@ public abstract class BaseMonitoringDocTestCase<T extends MonitoringDoc> extends
public void testToUTC() { public void testToUTC() {
final long timestamp = System.currentTimeMillis(); 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)); assertEquals(expected, MonitoringDoc.toUTC(timestamp));
} }

View File

@ -15,6 +15,7 @@ import org.elasticsearch.common.settings.ClusterSettings;
import org.elasticsearch.common.settings.Setting; import org.elasticsearch.common.settings.Setting;
import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.settings.SettingsException; 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.AbstractRunnable;
import org.elasticsearch.common.util.concurrent.ThreadContext; import org.elasticsearch.common.util.concurrent.ThreadContext;
import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentBuilder;
@ -30,11 +31,15 @@ import org.elasticsearch.xpack.monitoring.exporter.local.LocalExporter;
import org.junit.Before; import org.junit.Before;
import java.io.IOException; import java.io.IOException;
import java.time.Instant;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Locale;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import java.util.concurrent.CopyOnWriteArrayList; import java.util.concurrent.CopyOnWriteArrayList;
@ -92,6 +97,21 @@ public class ExportersTests extends ESTestCase {
exporters = new Exporters(Settings.EMPTY, factories, clusterService, licenseState, threadContext); 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 { public void testInitExportersDefault() throws Exception {
factories.put("_type", TestExporter::new); factories.put("_type", TestExporter::new);
Map<String, Exporter> internalExporters = exporters.initExporters(Settings.builder().build()); Map<String, Exporter> internalExporters = exporters.initExporters(Settings.builder().build());

View File

@ -6,16 +6,15 @@
package org.elasticsearch.xpack.monitoring.exporter; package org.elasticsearch.xpack.monitoring.exporter;
import org.elasticsearch.common.Strings; import org.elasticsearch.common.Strings;
import org.elasticsearch.common.time.DateFormatter;
import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.test.ESTestCase; import org.elasticsearch.test.ESTestCase;
import org.elasticsearch.xpack.core.monitoring.MonitoredSystem; import org.elasticsearch.xpack.core.monitoring.MonitoredSystem;
import org.elasticsearch.xpack.core.monitoring.exporter.MonitoringTemplateUtils; 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.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.LAST_UPDATED_VERSION;
import static org.elasticsearch.xpack.core.monitoring.exporter.MonitoringTemplateUtils.OLD_TEMPLATE_IDS; import static org.elasticsearch.xpack.core.monitoring.exporter.MonitoringTemplateUtils.OLD_TEMPLATE_IDS;
@ -93,9 +92,10 @@ public class MonitoringTemplateUtilsTests extends ESTestCase {
} }
public void testIndexName() { 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), assertThat(indexName(formatter, MonitoredSystem.ES, timestamp),
equalTo(".monitoring-es-" + TEMPLATE_VERSION + "-2017.08.03")); equalTo(".monitoring-es-" + TEMPLATE_VERSION + "-2017.08.03"));
assertThat(indexName(formatter, MonitoredSystem.KIBANA, timestamp), assertThat(indexName(formatter, MonitoredSystem.KIBANA, timestamp),
@ -105,7 +105,7 @@ public class MonitoringTemplateUtilsTests extends ESTestCase {
assertThat(indexName(formatter, MonitoredSystem.BEATS, timestamp), assertThat(indexName(formatter, MonitoredSystem.BEATS, timestamp),
equalTo(".monitoring-beats-" + TEMPLATE_VERSION + "-2017.08.03")); 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), assertThat(indexName(formatter, MonitoredSystem.ES, timestamp),
equalTo(".monitoring-es-" + TEMPLATE_VERSION + "-2017-03-08-13.47.58")); equalTo(".monitoring-es-" + TEMPLATE_VERSION + "-2017-03-08-13.47.58"));
assertThat(indexName(formatter, MonitoredSystem.KIBANA, timestamp), assertThat(indexName(formatter, MonitoredSystem.KIBANA, timestamp),
@ -115,4 +115,5 @@ public class MonitoringTemplateUtilsTests extends ESTestCase {
assertThat(indexName(formatter, MonitoredSystem.BEATS, timestamp), assertThat(indexName(formatter, MonitoredSystem.BEATS, timestamp),
equalTo(".monitoring-beats-" + TEMPLATE_VERSION + "-2017-03-08-13.47.58")); equalTo(".monitoring-beats-" + TEMPLATE_VERSION + "-2017-03-08-13.47.58"));
} }
} }

View File

@ -19,6 +19,7 @@ import org.elasticsearch.common.bytes.BytesArray;
import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.collect.Tuple; import org.elasticsearch.common.collect.Tuple;
import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.time.DateFormatter;
import org.elasticsearch.common.util.concurrent.ThreadContext; import org.elasticsearch.common.util.concurrent.ThreadContext;
import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.common.xcontent.XContentHelper;
import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.common.xcontent.XContentType;
@ -39,12 +40,13 @@ import org.elasticsearch.xpack.monitoring.exporter.ClusterAlertsUtil;
import org.elasticsearch.xpack.monitoring.exporter.ExportBulk; import org.elasticsearch.xpack.monitoring.exporter.ExportBulk;
import org.elasticsearch.xpack.monitoring.exporter.Exporter; import org.elasticsearch.xpack.monitoring.exporter.Exporter;
import org.elasticsearch.xpack.monitoring.test.MonitoringIntegTestCase; import org.elasticsearch.xpack.monitoring.test.MonitoringIntegTestCase;
import org.joda.time.format.DateTimeFormat;
import org.junit.After; import org.junit.After;
import org.junit.Before; import org.junit.Before;
import java.io.IOException; import java.io.IOException;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.time.Instant;
import java.time.ZoneOffset;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
@ -352,7 +354,8 @@ public class HttpExporterIT extends MonitoringIntegTestCase {
remoteClusterAllowsWatcher, currentLicenseAllowsWatcher, watcherAlreadyExists); remoteClusterAllowsWatcher, currentLicenseAllowsWatcher, watcherAlreadyExists);
MockRequest recordedRequest = assertBulk(webServer); 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); byte[] bytes = recordedRequest.getBody().getBytes(StandardCharsets.UTF_8);
Map<String, Object> data = XContentHelper.convertToMap(new BytesArray(bytes), false, XContentType.JSON).v2(); Map<String, Object> data = XContentHelper.convertToMap(new BytesArray(bytes), false, XContentType.JSON).v2();
@ -360,7 +363,7 @@ public class HttpExporterIT extends MonitoringIntegTestCase {
Map<String, Object> index = (Map<String, Object>) data.get("index"); Map<String, Object> index = (Map<String, Object>) data.get("index");
assertThat(index.get("_index"), equalTo(indexName)); 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() final Settings newSettings = Settings.builder()
.put(settings) .put(settings)
@ -375,8 +378,10 @@ public class HttpExporterIT extends MonitoringIntegTestCase {
doc = newRandomMonitoringDoc(); doc = newRandomMonitoringDoc();
export(newSettings, Collections.singletonList(doc)); export(newSettings, Collections.singletonList(doc));
DateFormatter newTimeFormatter = DateFormatter.forPattern(newTimeFormat).withZone(ZoneOffset.UTC);
String expectedMonitoringIndex = ".monitoring-es-" + TEMPLATE_VERSION + "-" String expectedMonitoringIndex = ".monitoring-es-" + TEMPLATE_VERSION + "-"
+ DateTimeFormat.forPattern(newTimeFormat).withZoneUTC().print(doc.getTimestamp()); + newTimeFormatter.format(Instant.ofEpochMilli(doc.getTimestamp()));
assertMonitorResources(webServer, true, includeOldTemplates, true, assertMonitorResources(webServer, true, includeOldTemplates, true,
true, true, true); true, true, true);

View File

@ -15,6 +15,7 @@ import org.elasticsearch.cluster.metadata.IndexTemplateMetaData;
import org.elasticsearch.common.Strings; import org.elasticsearch.common.Strings;
import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.time.DateFormatter;
import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.index.query.QueryBuilders;
@ -30,13 +31,11 @@ import org.elasticsearch.xpack.core.monitoring.action.MonitoringBulkRequestBuild
import org.elasticsearch.xpack.core.monitoring.exporter.MonitoringTemplateUtils; import org.elasticsearch.xpack.core.monitoring.exporter.MonitoringTemplateUtils;
import org.elasticsearch.xpack.monitoring.MonitoringService; import org.elasticsearch.xpack.monitoring.MonitoringService;
import org.elasticsearch.xpack.monitoring.MonitoringTestUtils; 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.io.IOException;
import java.time.Instant;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.HashSet; import java.util.HashSet;
@ -183,7 +182,7 @@ public class LocalExporterIntegTests extends LocalExporterIntegTestCase {
// node_stats document collected for each node is at least 10 seconds old, corresponding to // node_stats document collected for each node is at least 10 seconds old, corresponding to
// 2 or 3 elapsed collection intervals. // 2 or 3 elapsed collection intervals.
final int elapsedInSeconds = 10; final int elapsedInSeconds = 10;
final DateTime startTime = DateTime.now(DateTimeZone.UTC); final ZonedDateTime startTime = ZonedDateTime.now(ZoneOffset.UTC);
assertBusy(() -> { assertBusy(() -> {
IndicesExistsResponse indicesExistsResponse = client().admin().indices().prepareExists(".monitoring-*").get(); IndicesExistsResponse indicesExistsResponse = client().admin().indices().prepareExists(".monitoring-*").get();
if (indicesExistsResponse.isExists()) { if (indicesExistsResponse.isExists()) {
@ -205,11 +204,11 @@ public class LocalExporterIntegTests extends LocalExporterIntegTestCase {
assertTrue(bucket.getDocCount() >= 1L); assertTrue(bucket.getDocCount() >= 1L);
Max subAggregation = bucket.getAggregations().get("agg_last_time_collected"); Max subAggregation = bucket.getAggregations().get("agg_last_time_collected");
DateTime lastCollection = new DateTime(Math.round(subAggregation.getValue()), DateTimeZone.UTC); ZonedDateTime lastCollection = Instant.ofEpochMilli(Math.round(subAggregation.getValue())).atZone(ZoneOffset.UTC);
assertTrue(lastCollection.plusSeconds(elapsedInSeconds).isBefore(DateTime.now(DateTimeZone.UTC))); assertTrue(lastCollection.plusSeconds(elapsedInSeconds).isBefore(ZonedDateTime.now(ZoneOffset.UTC)));
} }
} else { } else {
assertTrue(DateTime.now(DateTimeZone.UTC).isAfter(startTime.plusSeconds(elapsedInSeconds))); assertTrue(ZonedDateTime.now(ZoneOffset.UTC).isAfter(startTime.plusSeconds(elapsedInSeconds)));
} }
}, 30L, TimeUnit.SECONDS); }, 30L, TimeUnit.SECONDS);
} }
@ -256,11 +255,11 @@ public class LocalExporterIntegTests extends LocalExporterIntegTestCase {
.get("xpack.monitoring.exporters._local.index.name.time_format"); .get("xpack.monitoring.exporters._local.index.name.time_format");
assertEquals(indexTimeFormat, customTimeFormat); assertEquals(indexTimeFormat, customTimeFormat);
if (customTimeFormat == null) { if (customTimeFormat == null) {
customTimeFormat = "YYYY.MM.dd"; customTimeFormat = "yyyy.MM.dd";
} }
DateTimeFormatter dateParser = ISODateTimeFormat.dateTime().withZoneUTC(); DateFormatter dateParser = DateFormatter.forPattern("strict_date_time");
DateTimeFormatter dateFormatter = DateTimeFormat.forPattern(customTimeFormat).withZoneUTC(); DateFormatter dateFormatter = DateFormatter.forPattern(customTimeFormat).withZone(ZoneOffset.UTC);
SearchResponse searchResponse = client().prepareSearch(".monitoring-*").setSize(100).get(); SearchResponse searchResponse = client().prepareSearch(".monitoring-*").setSize(100).get();
assertThat(searchResponse.getHits().getTotalHits().value, greaterThan(0L)); assertThat(searchResponse.getHits().getTotalHits().value, greaterThan(0L));
@ -290,7 +289,7 @@ public class LocalExporterIntegTests extends LocalExporterIntegTestCase {
expectedSystem = MonitoredSystem.fromSystem((String) docSource.get("expected_system")); 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; final String expectedIndex = ".monitoring-" + expectedSystem.getSystem() + "-" + TEMPLATE_VERSION + "-" + dateTime;
assertEquals("Expected " + expectedIndex + " but got " + hit.getIndex(), expectedIndex, hit.getIndex()); assertEquals("Expected " + expectedIndex + " but got " + hit.getIndex(), expectedIndex, hit.getIndex());

View File

@ -18,6 +18,7 @@ import org.elasticsearch.common.CheckedRunnable;
import org.elasticsearch.common.Strings; import org.elasticsearch.common.Strings;
import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.common.bytes.BytesArray;
import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.time.DateFormatter;
import org.elasticsearch.common.unit.TimeValue; import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.ToXContentObject; import org.elasticsearch.common.xcontent.ToXContentObject;
import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentBuilder;
@ -50,8 +51,6 @@ import org.elasticsearch.xpack.monitoring.collector.indices.IndicesStatsMonitori
import org.elasticsearch.xpack.monitoring.collector.node.NodeStatsMonitoringDoc; import org.elasticsearch.xpack.monitoring.collector.node.NodeStatsMonitoringDoc;
import org.elasticsearch.xpack.monitoring.collector.shards.ShardMonitoringDoc; import org.elasticsearch.xpack.monitoring.collector.shards.ShardMonitoringDoc;
import org.elasticsearch.xpack.monitoring.test.MockIngestPlugin; 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.io.IOException;
import java.lang.Thread.State; import java.lang.Thread.State;
@ -59,6 +58,8 @@ import java.lang.management.LockInfo;
import java.lang.management.ManagementFactory; import java.lang.management.ManagementFactory;
import java.lang.management.MonitorInfo; import java.lang.management.MonitorInfo;
import java.lang.management.ThreadInfo; import java.lang.management.ThreadInfo;
import java.time.Instant;
import java.time.ZoneOffset;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
import java.util.List; import java.util.List;
@ -277,9 +278,10 @@ public class MonitoringIT extends ESSingleNodeTestCase {
assertThat(((Number) source.get("interval_ms")).longValue(), equalTo(interval.getMillis())); assertThat(((Number) source.get("interval_ms")).longValue(), equalTo(interval.getMillis()));
assertThat(index, equalTo(MonitoringTemplateUtils.indexName(DateTimeFormat.forPattern("YYYY.MM.dd").withZoneUTC(), DateFormatter formatter = DateFormatter.forPattern("yyyy.MM.dd");
expectedSystem, long isoTimestamp = Instant.from(DateFormatter.forPattern("strict_date_time").parse(timestamp)).toEpochMilli();
ISODateTimeFormat.dateTime().parseMillis(timestamp)))); String isoDateTime = MonitoringTemplateUtils.indexName(formatter.withZone(ZoneOffset.UTC), expectedSystem, isoTimestamp);
assertThat(index, equalTo(isoDateTime));
final Map<String, Object> sourceNode = (Map<String, Object>) source.get("source_node"); final Map<String, Object> sourceNode = (Map<String, Object>) source.get("source_node");
if (sourceNode != null) { if (sourceNode != null) {