diff --git a/docs/en/settings/monitoring-settings.asciidoc b/docs/en/settings/monitoring-settings.asciidoc index 0a602c80b80..9458bbaba68 100644 --- a/docs/en/settings/monitoring-settings.asciidoc +++ b/docs/en/settings/monitoring-settings.asciidoc @@ -30,13 +30,9 @@ Set to `false` to disable {es} {monitoring} for Elasticsearch. The `xpack.monitoring.collection` settings control how data is collected from your Elasticsearch nodes. -`xpack.monitoring.collection.cluster.state.timeout`:: - -Sets the timeout for collecting the cluster state. Defaults to `10m`. - `xpack.monitoring.collection.cluster.stats.timeout`:: -Sets the timeout for collecting the cluster statistics. Defaults to `10m`. +Sets the timeout for collecting the cluster statistics. Defaults to `10s`. `xpack.monitoring.collection.indices`:: @@ -50,11 +46,11 @@ You can update this setting through the Cluster Update Settings API. `xpack.monitoring.collection.index.stats.timeout`:: -Sets the timeout for collecting index statistics. Defaults to `10m`. +Sets the timeout for collecting index statistics. Defaults to `10s`. `xpack.monitoring.collection.indices.stats.timeout`:: -Sets the timeout for collecting total indices statistics. Defaults to `10m`. +Sets the timeout for collecting total indices statistics. Defaults to `10s`. `xpack.monitoring.collection.index.recovery.active_only`:: @@ -63,7 +59,7 @@ collect only active recoveries. Defaults to `false`. `xpack.monitoring.collection.index.recovery.timeout`:: -Sets the timeout for collecting the recovery information. Defaults to `10m`. +Sets the timeout for collecting the recovery information. Defaults to `10s`. `xpack.monitoring.collection.interval`:: diff --git a/plugin/src/main/java/org/elasticsearch/license/XPackLicenseState.java b/plugin/src/main/java/org/elasticsearch/license/XPackLicenseState.java index de331f153c2..e5c33c782eb 100644 --- a/plugin/src/main/java/org/elasticsearch/license/XPackLicenseState.java +++ b/plugin/src/main/java/org/elasticsearch/license/XPackLicenseState.java @@ -10,7 +10,7 @@ import org.elasticsearch.common.logging.LoggerMessageFormat; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.license.License.OperationMode; import org.elasticsearch.xpack.XPackPlugin; -import org.elasticsearch.xpack.monitoring.MonitoringSettings; +import org.elasticsearch.xpack.monitoring.Monitoring; import java.util.Collections; import java.util.LinkedHashMap; @@ -155,7 +155,7 @@ public class XPackLicenseState { newMode, newMode, newMode), LoggerMessageFormat.format( "Automatic index cleanup is locked to {} days for clusters with [{}] license.", - MonitoringSettings.HISTORY_DURATION.getDefault(Settings.EMPTY).days(), newMode) + Monitoring.HISTORY_DURATION.getDefault(Settings.EMPTY).days(), newMode) }; } break; diff --git a/plugin/src/main/java/org/elasticsearch/xpack/XPackPlugin.java b/plugin/src/main/java/org/elasticsearch/xpack/XPackPlugin.java index 00e33344090..bcf338698ac 100644 --- a/plugin/src/main/java/org/elasticsearch/xpack/XPackPlugin.java +++ b/plugin/src/main/java/org/elasticsearch/xpack/XPackPlugin.java @@ -81,7 +81,6 @@ import org.elasticsearch.xpack.ml.MachineLearning; import org.elasticsearch.xpack.ml.MachineLearningFeatureSet; import org.elasticsearch.xpack.monitoring.Monitoring; import org.elasticsearch.xpack.monitoring.MonitoringFeatureSet; -import org.elasticsearch.xpack.monitoring.MonitoringSettings; import org.elasticsearch.xpack.notification.email.Account; import org.elasticsearch.xpack.notification.email.EmailService; import org.elasticsearch.xpack.notification.email.attachment.DataAttachmentParser; @@ -385,7 +384,7 @@ public class XPackPlugin extends Plugin implements ScriptPlugin, ActionPlugin, I public List> getSettings() { ArrayList> settings = new ArrayList<>(); settings.addAll(Security.getSettings(transportClientMode, extensionsService)); - settings.addAll(MonitoringSettings.getSettings()); + settings.addAll(monitoring.getSettings()); settings.addAll(watcher.getSettings()); settings.addAll(machineLearning.getSettings()); settings.addAll(licensing.getSettings()); @@ -421,7 +420,7 @@ public class XPackPlugin extends Plugin implements ScriptPlugin, ActionPlugin, I filters.add("xpack.notification.pagerduty.account.*." + PagerDutyAccount.SERVICE_KEY_SETTING); filters.add("xpack.notification.hipchat.account.*.auth_token"); filters.addAll(security.getSettingsFilter(extensionsService)); - filters.addAll(MonitoringSettings.getSettingsFilter()); + filters.addAll(monitoring.getSettingsFilter()); if (transportClientMode == false) { for (XPackExtension extension : extensionsService.getExtensions()) { filters.addAll(extension.getSettingsFilter()); diff --git a/plugin/src/main/java/org/elasticsearch/xpack/monitoring/Monitoring.java b/plugin/src/main/java/org/elasticsearch/xpack/monitoring/Monitoring.java index 4ba172ab524..f11a7031f10 100644 --- a/plugin/src/main/java/org/elasticsearch/xpack/monitoring/Monitoring.java +++ b/plugin/src/main/java/org/elasticsearch/xpack/monitoring/Monitoring.java @@ -14,8 +14,10 @@ import org.elasticsearch.common.inject.Module; import org.elasticsearch.common.inject.util.Providers; import org.elasticsearch.common.settings.ClusterSettings; import org.elasticsearch.common.settings.IndexScopedSettings; +import org.elasticsearch.common.settings.Setting; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.SettingsFilter; +import org.elasticsearch.common.unit.TimeValue; import org.elasticsearch.license.LicenseService; import org.elasticsearch.license.XPackLicenseState; import org.elasticsearch.plugins.ActionPlugin; @@ -55,6 +57,7 @@ import java.util.function.Supplier; import static java.util.Collections.emptyList; import static java.util.Collections.singletonList; +import static org.elasticsearch.common.settings.Setting.timeSetting; /** * This class activates/deactivates the monitoring modules depending if we're running a node client, transport client or tribe client: @@ -66,6 +69,27 @@ public class Monitoring implements ActionPlugin { public static final String NAME = "monitoring"; + /** + * The minimum amount of time allowed for the history duration. + */ + public static final TimeValue HISTORY_DURATION_MINIMUM = TimeValue.timeValueHours(24); + + /** + * The default retention duration of the monitoring history data. + *

+ * Expected values: + *

+ * + * @see #HISTORY_DURATION_MINIMUM + */ + public static final Setting HISTORY_DURATION = timeSetting("xpack.monitoring.history.duration", + TimeValue.timeValueHours(7 * 24), // default value (7 days) + HISTORY_DURATION_MINIMUM, // minimum value + Setting.Property.Dynamic, Setting.Property.NodeScope); + private final Settings settings; private final XPackLicenseState licenseState; private final boolean enabled; @@ -106,7 +130,6 @@ public class Monitoring implements ActionPlugin { } final ClusterSettings clusterSettings = clusterService.getClusterSettings(); - final MonitoringSettings monitoringSettings = new MonitoringSettings(settings, clusterSettings); final CleanerService cleanerService = new CleanerService(settings, clusterSettings, threadPool, licenseState); final SSLService dynamicSSLService = sslService.createDynamicSSLService(); @@ -116,16 +139,16 @@ public class Monitoring implements ActionPlugin { final Exporters exporters = new Exporters(settings, exporterFactories, clusterService, licenseState, threadPool.getThreadContext()); Set collectors = new HashSet<>(); - collectors.add(new IndexStatsCollector(settings, clusterService, monitoringSettings, licenseState, client)); - collectors.add(new ClusterStatsCollector(settings, clusterService, monitoringSettings, licenseState, client, licenseService)); - collectors.add(new ShardsCollector(settings, clusterService, monitoringSettings, licenseState)); - collectors.add(new NodeStatsCollector(settings, clusterService, monitoringSettings, licenseState, client)); - collectors.add(new IndexRecoveryCollector(settings, clusterService, monitoringSettings, licenseState, client)); - collectors.add(new JobStatsCollector(settings, clusterService, monitoringSettings, licenseState, client)); + collectors.add(new IndexStatsCollector(settings, clusterService, licenseState, client)); + collectors.add(new ClusterStatsCollector(settings, clusterService, licenseState, client, licenseService)); + collectors.add(new ShardsCollector(settings, clusterService, licenseState)); + collectors.add(new NodeStatsCollector(settings, clusterService, licenseState, client)); + collectors.add(new IndexRecoveryCollector(settings, clusterService, licenseState, client)); + collectors.add(new JobStatsCollector(settings, clusterService, licenseState, client)); final MonitoringService monitoringService = new MonitoringService(settings, clusterSettings, threadPool, collectors, exporters); - return Arrays.asList(monitoringService, monitoringSettings, exporters, cleanerService); + return Arrays.asList(monitoringService, exporters, cleanerService); } @Override @@ -145,4 +168,25 @@ public class Monitoring implements ActionPlugin { } return singletonList(new RestMonitoringBulkAction(settings, restController)); } + + public List> getSettings() { + return Collections.unmodifiableList( + Arrays.asList(HISTORY_DURATION, + MonitoringService.INTERVAL, + Exporters.EXPORTERS_SETTINGS, + Collector.INDICES, + ClusterStatsCollector.CLUSTER_STATS_TIMEOUT, + IndexRecoveryCollector.INDEX_RECOVERY_TIMEOUT, + IndexRecoveryCollector.INDEX_RECOVERY_ACTIVE_ONLY, + IndexStatsCollector.INDEX_STATS_TIMEOUT, + JobStatsCollector.JOB_STATS_TIMEOUT, + NodeStatsCollector.NODE_STATS_TIMEOUT) + ); + } + + public List getSettingsFilter() { + final String exportersKey = Exporters.EXPORTERS_SETTINGS.getKey(); + return Collections.unmodifiableList(Arrays.asList(exportersKey + "*.auth.*", exportersKey + "*.ssl.*")); + } + } diff --git a/plugin/src/main/java/org/elasticsearch/xpack/monitoring/MonitoringService.java b/plugin/src/main/java/org/elasticsearch/xpack/monitoring/MonitoringService.java index 2200fee305c..ad0a24e62fc 100644 --- a/plugin/src/main/java/org/elasticsearch/xpack/monitoring/MonitoringService.java +++ b/plugin/src/main/java/org/elasticsearch/xpack/monitoring/MonitoringService.java @@ -10,6 +10,7 @@ import org.apache.logging.log4j.util.Supplier; import org.elasticsearch.action.ActionListener; import org.elasticsearch.common.component.AbstractLifecycleComponent; import org.elasticsearch.common.settings.ClusterSettings; +import org.elasticsearch.common.settings.Setting; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.unit.TimeValue; import org.elasticsearch.common.util.concurrent.AbstractRunnable; @@ -36,6 +37,24 @@ import java.util.concurrent.atomic.AtomicBoolean; */ public class MonitoringService extends AbstractLifecycleComponent { + /** + * Minimum value for sampling interval (1 second) + */ + static final TimeValue MIN_INTERVAL = TimeValue.timeValueSeconds(1L); + + /** + * Sampling interval between two collections (default to 10s) + */ + public static final Setting INTERVAL = new Setting<>("xpack.monitoring.collection.interval", "10s", + (s) -> { + TimeValue value = TimeValue.parseTimeValue(s, null, "xpack.monitoring.collection.interval"); + if (TimeValue.MINUS_ONE.equals(value) || value.millis() >= MIN_INTERVAL.millis()) { + return value; + } + throw new IllegalArgumentException("Failed to parse monitoring interval [" + s + "], value must be >= " + MIN_INTERVAL); + }, + Setting.Property.Dynamic, Setting.Property.NodeScope); + /** State of the monitoring service, either started or stopped **/ private final AtomicBoolean started = new AtomicBoolean(false); @@ -55,8 +74,8 @@ public class MonitoringService extends AbstractLifecycleComponent { this.threadPool = Objects.requireNonNull(threadPool); this.collectors = Objects.requireNonNull(collectors); this.exporters = Objects.requireNonNull(exporters); - this.interval = MonitoringSettings.INTERVAL.get(settings); - clusterSettings.addSettingsUpdateConsumer(MonitoringSettings.INTERVAL, this::setInterval); + this.interval = INTERVAL.get(settings); + clusterSettings.addSettingsUpdateConsumer(INTERVAL, this::setInterval); } void setInterval(TimeValue interval) { @@ -71,7 +90,7 @@ public class MonitoringService extends AbstractLifecycleComponent { boolean isMonitoringActive() { return isStarted() && interval != null - && interval.millis() >= MonitoringSettings.MIN_INTERVAL.millis(); + && interval.millis() >= MIN_INTERVAL.millis(); } private String threadPoolName() { diff --git a/plugin/src/main/java/org/elasticsearch/xpack/monitoring/MonitoringSettings.java b/plugin/src/main/java/org/elasticsearch/xpack/monitoring/MonitoringSettings.java deleted file mode 100644 index 8ced786435a..00000000000 --- a/plugin/src/main/java/org/elasticsearch/xpack/monitoring/MonitoringSettings.java +++ /dev/null @@ -1,258 +0,0 @@ -/* - * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one - * or more contributor license agreements. Licensed under the Elastic License; - * you may not use this file except in compliance with the Elastic License. - */ -package org.elasticsearch.xpack.monitoring; - -import org.elasticsearch.common.component.AbstractComponent; -import org.elasticsearch.common.settings.ClusterSettings; -import org.elasticsearch.common.settings.Setting; -import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.common.unit.TimeValue; -import org.elasticsearch.xpack.XPackPlugin; - -import java.util.Arrays; -import java.util.Collections; -import java.util.List; -import java.util.function.Function; - -import static org.elasticsearch.common.settings.Setting.Property; -import static org.elasticsearch.common.settings.Setting.boolSetting; -import static org.elasticsearch.common.settings.Setting.groupSetting; -import static org.elasticsearch.common.settings.Setting.listSetting; -import static org.elasticsearch.common.settings.Setting.timeSetting; - -// TODO Remove this class and put the settings in Monitoring class -public class MonitoringSettings extends AbstractComponent { - - public static final String HISTORY_DURATION_SETTING_NAME = "history.duration"; - /** - * The minimum amount of time allowed for the history duration. - */ - public static final TimeValue HISTORY_DURATION_MINIMUM = TimeValue.timeValueHours(24); - - /** - * Minimum value for sampling interval (1 second) - */ - static final TimeValue MIN_INTERVAL = TimeValue.timeValueSeconds(1L); - - /** - * Sampling interval between two collections (default to 10s) - */ - public static final Setting INTERVAL = new Setting<>(collectionKey("interval"), "10s", - (s) -> { - TimeValue value = TimeValue.parseTimeValue(s, null, collectionKey("interval")); - if (TimeValue.MINUS_ONE.equals(value) || value.millis() >= MIN_INTERVAL.millis()) { - return value; - } - throw new IllegalArgumentException("Failed to parse monitoring interval [" + s + "], value must be >= " + MIN_INTERVAL); - }, - Property.Dynamic, Property.NodeScope); - - /** - * Timeout value when collecting index statistics (default to 10s) - */ - public static final Setting INDEX_STATS_TIMEOUT = - timeSetting(collectionKey("index.stats.timeout"), TimeValue.timeValueSeconds(10), Property.Dynamic, Property.NodeScope); - - /** - * List of indices names whose stats will be exported (default to all indices) - */ - public static final Setting> INDICES = - listSetting(collectionKey("indices"), Collections.emptyList(), Function.identity(), Property.Dynamic, Property.NodeScope); - - /** - * Timeout value when collecting the cluster state (default to 10s) - */ - public static final Setting CLUSTER_STATE_TIMEOUT = - timeSetting(collectionKey("cluster.state.timeout"), TimeValue.timeValueSeconds(10), Property.Dynamic, Property.NodeScope); - - /** - * Timeout value when collecting the recovery information (default to 10s) - */ - public static final Setting CLUSTER_STATS_TIMEOUT = - timeSetting(collectionKey("cluster.stats.timeout"), TimeValue.timeValueSeconds(10), Property.Dynamic, Property.NodeScope); - - /** - * Timeout value when collecting ML job statistics (default to 10s) - */ - public static final Setting JOB_STATS_TIMEOUT = - timeSetting(collectionKey("ml.job.stats.timeout"), TimeValue.timeValueSeconds(10), Property.Dynamic, Property.NodeScope); - - /** - * Timeout value when collecting the nodes statistics (default to 10s) - */ - public static final Setting NODE_STATS_TIMEOUT = - timeSetting(collectionKey("node.stats.timeout"), TimeValue.timeValueSeconds(10), Property.Dynamic, Property.NodeScope); - - /** - * Timeout value when collecting the recovery information (default to 10s) - */ - public static final Setting INDEX_RECOVERY_TIMEOUT = - timeSetting(collectionKey("index.recovery.timeout"), TimeValue.timeValueSeconds(10), Property.Dynamic, Property.NodeScope); - - /** - * Flag to indicate if only active recoveries should be collected (default to false: all recoveries are collected) - */ - public static final Setting INDEX_RECOVERY_ACTIVE_ONLY = - boolSetting(collectionKey("index.recovery.active_only"), false, Property.Dynamic, Property.NodeScope) ; - - /** - * The default retention duration of the monitoring history data. - *

- * Expected values: - *

    - *
  • Default: 7 days
  • - *
  • Minimum: 1 day
  • - *
- * - * @see #HISTORY_DURATION_MINIMUM - */ - public static final Setting HISTORY_DURATION = - timeSetting(key(HISTORY_DURATION_SETTING_NAME), - TimeValue.timeValueHours(7 * 24), // default value (7 days) - HISTORY_DURATION_MINIMUM, // minimum value - Property.Dynamic, Property.NodeScope); - - /** - * Settings/Options per configured exporter - */ - public static final Setting EXPORTERS_SETTINGS = - groupSetting(key("exporters."), Property.Dynamic, Property.NodeScope); - - public static List> getSettings() { - return Arrays.asList(INDICES, - INTERVAL, - INDEX_RECOVERY_TIMEOUT, - INDEX_STATS_TIMEOUT, - INDEX_RECOVERY_ACTIVE_ONLY, - CLUSTER_STATE_TIMEOUT, - CLUSTER_STATS_TIMEOUT, - JOB_STATS_TIMEOUT, - NODE_STATS_TIMEOUT, - HISTORY_DURATION, - EXPORTERS_SETTINGS); - } - - public static List getSettingsFilter() { - return Arrays.asList(key("exporters.*.auth.*"), key("exporters.*.ssl.*")); - } - - - private volatile TimeValue indexStatsTimeout; - private volatile TimeValue clusterStateTimeout; - private volatile TimeValue clusterStatsTimeout; - private volatile TimeValue recoveryTimeout; - private volatile TimeValue jobStatsTimeout; - private volatile TimeValue nodeStatsTimeout; - private volatile boolean recoveryActiveOnly; - private volatile String[] indices; - - public MonitoringSettings(Settings settings, ClusterSettings clusterSettings) { - super(settings); - - setIndexStatsTimeout(INDEX_STATS_TIMEOUT.get(settings)); - clusterSettings.addSettingsUpdateConsumer(INDEX_STATS_TIMEOUT, this::setIndexStatsTimeout); - setIndices(INDICES.get(settings)); - clusterSettings.addSettingsUpdateConsumer(INDICES, this::setIndices); - setClusterStateTimeout(CLUSTER_STATE_TIMEOUT.get(settings)); - clusterSettings.addSettingsUpdateConsumer(CLUSTER_STATE_TIMEOUT, this::setClusterStateTimeout); - setClusterStatsTimeout(CLUSTER_STATS_TIMEOUT.get(settings)); - clusterSettings.addSettingsUpdateConsumer(CLUSTER_STATS_TIMEOUT, this::setClusterStatsTimeout); - setJobStatsTimeout(JOB_STATS_TIMEOUT.get(settings)); - clusterSettings.addSettingsUpdateConsumer(JOB_STATS_TIMEOUT, this::setJobStatsTimeout); - setNodeStatsTimeout(NODE_STATS_TIMEOUT.get(settings)); - clusterSettings.addSettingsUpdateConsumer(NODE_STATS_TIMEOUT, this::setNodeStatsTimeout); - setRecoveryTimeout(INDEX_RECOVERY_TIMEOUT.get(settings)); - clusterSettings.addSettingsUpdateConsumer(INDEX_RECOVERY_TIMEOUT, this::setRecoveryTimeout); - setRecoveryActiveOnly(INDEX_RECOVERY_ACTIVE_ONLY.get(settings)); - clusterSettings.addSettingsUpdateConsumer(INDEX_RECOVERY_ACTIVE_ONLY, this::setRecoveryActiveOnly); - } - - public TimeValue indexStatsTimeout() { - return indexStatsTimeout; - } - - public String[] indices() { - return indices; - } - - public TimeValue clusterStateTimeout() { - return clusterStateTimeout; - } - - public TimeValue clusterStatsTimeout() { - return clusterStatsTimeout; - } - - public TimeValue jobStatsTimeout() { - return jobStatsTimeout; - } - - public TimeValue nodeStatsTimeout() { - return nodeStatsTimeout; - } - - public TimeValue recoveryTimeout() { - return recoveryTimeout; - } - - public boolean recoveryActiveOnly() { - return recoveryActiveOnly; - } - - private void setIndexStatsTimeout(TimeValue indexStatsTimeout) { - this.indexStatsTimeout = indexStatsTimeout; - } - - private void setClusterStateTimeout(TimeValue clusterStateTimeout) { - this.clusterStateTimeout = clusterStateTimeout; - } - - private void setClusterStatsTimeout(TimeValue clusterStatsTimeout) { - this.clusterStatsTimeout = clusterStatsTimeout; - } - - private void setJobStatsTimeout(TimeValue jobStatsTimeout) { - this.jobStatsTimeout = jobStatsTimeout; - } - - public void setNodeStatsTimeout(TimeValue nodeStatsTimeout) { - this.nodeStatsTimeout = nodeStatsTimeout; - } - - private void setRecoveryTimeout(TimeValue recoveryTimeout) { - this.recoveryTimeout = recoveryTimeout; - } - - private void setRecoveryActiveOnly(boolean recoveryActiveOnly) { - this.recoveryActiveOnly = recoveryActiveOnly; - } - - private void setIndices(List indices) { - this.indices = indices.toArray(new String[0]); - } - - /** - * Prefix the {@code key} with the Monitoring prefix and "collection." . - * - * @param key The key to prefix - * @return The key prefixed by the product prefixes + "collection." . - * @see #key(String) - */ - static String collectionKey(String key) { - return key("collection." + key); - } - - /** - * Prefix the {@code key} with the Monitoring prefix. - * - * @param key The key to prefix - * @return The key prefixed by the product prefixes. - */ - static String key(String key) { - return XPackPlugin.featureSettingPrefix(Monitoring.NAME) + "." + key; - } - -} diff --git a/plugin/src/main/java/org/elasticsearch/xpack/monitoring/cleaner/CleanerService.java b/plugin/src/main/java/org/elasticsearch/xpack/monitoring/cleaner/CleanerService.java index 790c13c444b..871a5b68a08 100644 --- a/plugin/src/main/java/org/elasticsearch/xpack/monitoring/cleaner/CleanerService.java +++ b/plugin/src/main/java/org/elasticsearch/xpack/monitoring/cleaner/CleanerService.java @@ -5,10 +5,6 @@ */ package org.elasticsearch.xpack.monitoring.cleaner; -import java.util.List; -import java.util.concurrent.CopyOnWriteArrayList; -import java.util.concurrent.ScheduledFuture; - import org.elasticsearch.common.component.AbstractLifecycleComponent; import org.elasticsearch.common.settings.ClusterSettings; import org.elasticsearch.common.settings.Settings; @@ -18,10 +14,14 @@ import org.elasticsearch.common.util.concurrent.EsRejectedExecutionException; import org.elasticsearch.common.util.concurrent.FutureUtils; import org.elasticsearch.license.XPackLicenseState; import org.elasticsearch.threadpool.ThreadPool; -import org.elasticsearch.xpack.monitoring.MonitoringSettings; +import org.elasticsearch.xpack.monitoring.Monitoring; import org.joda.time.DateTime; import org.joda.time.chrono.ISOChronology; +import java.util.List; +import java.util.concurrent.CopyOnWriteArrayList; +import java.util.concurrent.ScheduledFuture; + /** * {@code CleanerService} takes care of deleting old monitoring indices. */ @@ -41,11 +41,11 @@ public class CleanerService extends AbstractLifecycleComponent { this.licenseState = licenseState; this.threadPool = threadPool; this.executionScheduler = executionScheduler; - this.globalRetention = MonitoringSettings.HISTORY_DURATION.get(settings); + this.globalRetention = Monitoring.HISTORY_DURATION.get(settings); this.runnable = new IndicesCleaner(); // the validation is performed by the setting's object itself - clusterSettings.addSettingsUpdateConsumer(MonitoringSettings.HISTORY_DURATION, this::setGlobalRetention); + clusterSettings.addSettingsUpdateConsumer(Monitoring.HISTORY_DURATION, this::setGlobalRetention); } public CleanerService(Settings settings, ClusterSettings clusterSettings, ThreadPool threadPool, XPackLicenseState licenseState) { @@ -91,7 +91,7 @@ public class CleanerService extends AbstractLifecycleComponent { return globalRetention; } else { - return MonitoringSettings.HISTORY_DURATION.getDefault(Settings.EMPTY); + return Monitoring.HISTORY_DURATION.getDefault(Settings.EMPTY); } } @@ -106,8 +106,7 @@ public class CleanerService extends AbstractLifecycleComponent { public void setGlobalRetention(TimeValue globalRetention) { // notify the user that their setting will be ignored until they get the right license if (licenseState.isUpdateRetentionAllowed() == false) { - logger.warn("[{}] setting will be ignored until an appropriate license is applied", - MonitoringSettings.HISTORY_DURATION.getKey()); + logger.warn("[{}] setting will be ignored until an appropriate license is applied", Monitoring.HISTORY_DURATION.getKey()); } this.globalRetention = globalRetention; diff --git a/plugin/src/main/java/org/elasticsearch/xpack/monitoring/collector/Collector.java b/plugin/src/main/java/org/elasticsearch/xpack/monitoring/collector/Collector.java index 4bd5779f610..d7073619ccf 100644 --- a/plugin/src/main/java/org/elasticsearch/xpack/monitoring/collector/Collector.java +++ b/plugin/src/main/java/org/elasticsearch/xpack/monitoring/collector/Collector.java @@ -10,32 +10,50 @@ import org.apache.logging.log4j.util.Supplier; import org.elasticsearch.ElasticsearchTimeoutException; import org.elasticsearch.cluster.node.DiscoveryNode; import org.elasticsearch.cluster.service.ClusterService; +import org.elasticsearch.common.Strings; import org.elasticsearch.common.component.AbstractComponent; import org.elasticsearch.common.inject.internal.Nullable; +import org.elasticsearch.common.settings.Setting; import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.common.unit.TimeValue; import org.elasticsearch.license.XPackLicenseState; -import org.elasticsearch.xpack.monitoring.MonitoringSettings; +import org.elasticsearch.xpack.XPackPlugin; +import org.elasticsearch.xpack.monitoring.Monitoring; import org.elasticsearch.xpack.monitoring.exporter.MonitoringDoc; import java.util.Collection; +import java.util.List; +import java.util.Objects; +import java.util.function.Function; + +import static java.util.Collections.emptyList; +import static org.elasticsearch.common.settings.Setting.Property; +import static org.elasticsearch.common.settings.Setting.listSetting; +import static org.elasticsearch.common.settings.Setting.timeSetting; /** * {@link Collector} are used to collect monitoring data about the cluster, nodes and indices. */ public abstract class Collector extends AbstractComponent { + /** + * List of indices names whose stats will be exported (default to all indices) + */ + public static final Setting> INDICES = + listSetting(collectionSetting("indices"), emptyList(), Function.identity(), Property.Dynamic, Property.NodeScope); + private final String name; + private final Setting collectionTimeoutSetting; protected final ClusterService clusterService; - protected final MonitoringSettings monitoringSettings; protected final XPackLicenseState licenseState; - public Collector(Settings settings, String name, ClusterService clusterService, - MonitoringSettings monitoringSettings, XPackLicenseState licenseState) { + public Collector(final Settings settings, final String name, final ClusterService clusterService, + final Setting timeoutSetting, final XPackLicenseState licenseState) { super(settings); this.name = name; this.clusterService = clusterService; - this.monitoringSettings = monitoringSettings; + this.collectionTimeoutSetting = timeoutSetting; this.licenseState = licenseState; } @@ -92,6 +110,33 @@ public abstract class Collector extends AbstractComponent { return System.currentTimeMillis(); } + /** + * Returns the value of the collection timeout configured for the current {@link Collector}. + * + * @return the collection timeout, or {@code null} if the collector has not timeout defined. + */ + public TimeValue getCollectionTimeout() { + if (collectionTimeoutSetting == null) { + return null; + } + return clusterService.getClusterSettings().get(collectionTimeoutSetting); + } + + /** + * Returns the names of indices Monitoring collects data from. + * + * @return a array of indices + */ + public String[] getCollectionIndices() { + final List indices = clusterService.getClusterSettings().get(INDICES); + assert indices != null; + if (indices.isEmpty()) { + return Strings.EMPTY_ARRAY; + } else { + return indices.toArray(new String[indices.size()]); + } + } + /** * Creates a {@link MonitoringDoc.Node} from a {@link DiscoveryNode} and a timestamp, copying over the * required information. @@ -112,4 +157,14 @@ public abstract class Collector extends AbstractComponent { node.getName(), timestamp); } + + protected static String collectionSetting(final String settingName) { + Objects.requireNonNull(settingName, "setting name must not be null"); + return XPackPlugin.featureSettingPrefix(Monitoring.NAME) + ".collection." + settingName; + } + + protected static Setting collectionTimeoutSetting(final String settingName) { + String name = collectionSetting(settingName); + return timeSetting(name, TimeValue.timeValueSeconds(10), Property.Dynamic, Property.NodeScope); + } } diff --git a/plugin/src/main/java/org/elasticsearch/xpack/monitoring/collector/cluster/ClusterStatsCollector.java b/plugin/src/main/java/org/elasticsearch/xpack/monitoring/collector/cluster/ClusterStatsCollector.java index 674209f0b5d..ef7f9d33ca1 100644 --- a/plugin/src/main/java/org/elasticsearch/xpack/monitoring/collector/cluster/ClusterStatsCollector.java +++ b/plugin/src/main/java/org/elasticsearch/xpack/monitoring/collector/cluster/ClusterStatsCollector.java @@ -14,17 +14,17 @@ import org.elasticsearch.client.Client; import org.elasticsearch.cluster.ClusterState; import org.elasticsearch.cluster.service.ClusterService; import org.elasticsearch.common.Nullable; +import org.elasticsearch.common.settings.Setting; import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.common.unit.TimeValue; import org.elasticsearch.license.License; import org.elasticsearch.license.LicenseService; import org.elasticsearch.license.LicenseUtils; import org.elasticsearch.license.XPackLicenseState; import org.elasticsearch.xpack.XPackFeatureSet; import org.elasticsearch.xpack.action.XPackUsageRequestBuilder; -import org.elasticsearch.xpack.monitoring.MonitoringSettings; import org.elasticsearch.xpack.monitoring.collector.Collector; import org.elasticsearch.xpack.monitoring.exporter.MonitoringDoc; -import org.elasticsearch.xpack.security.InternalClient; import java.util.Collection; import java.util.Collections; @@ -42,16 +42,20 @@ import java.util.List; */ public class ClusterStatsCollector extends Collector { + /** + * Timeout value when collecting the cluster stats information (default to 10s) + */ + public static final Setting CLUSTER_STATS_TIMEOUT = collectionTimeoutSetting("cluster.stats.timeout"); + private final LicenseService licenseService; private final Client client; public ClusterStatsCollector(final Settings settings, final ClusterService clusterService, - final MonitoringSettings monitoringSettings, final XPackLicenseState licenseState, final Client client, final LicenseService licenseService) { - super(settings, ClusterStatsMonitoringDoc.TYPE, clusterService, monitoringSettings, licenseState); + super(settings, ClusterStatsMonitoringDoc.TYPE, clusterService, CLUSTER_STATS_TIMEOUT, licenseState); this.client = client; this.licenseService = licenseService; } @@ -65,8 +69,7 @@ public class ClusterStatsCollector extends Collector { @Override protected Collection doCollect(final MonitoringDoc.Node node) throws Exception { final Supplier clusterStatsSupplier = - () -> client.admin().cluster().prepareClusterStats() - .get(monitoringSettings.clusterStatsTimeout()); + () -> client.admin().cluster().prepareClusterStats().get(getCollectionTimeout()); final Supplier> usageSupplier = () -> new XPackUsageRequestBuilder(client).get().getUsages(); diff --git a/plugin/src/main/java/org/elasticsearch/xpack/monitoring/collector/indices/IndexRecoveryCollector.java b/plugin/src/main/java/org/elasticsearch/xpack/monitoring/collector/indices/IndexRecoveryCollector.java index ea88eea3143..a258fb3de0a 100644 --- a/plugin/src/main/java/org/elasticsearch/xpack/monitoring/collector/indices/IndexRecoveryCollector.java +++ b/plugin/src/main/java/org/elasticsearch/xpack/monitoring/collector/indices/IndexRecoveryCollector.java @@ -9,9 +9,10 @@ import org.elasticsearch.action.admin.indices.recovery.RecoveryResponse; import org.elasticsearch.action.support.IndicesOptions; import org.elasticsearch.client.Client; import org.elasticsearch.cluster.service.ClusterService; +import org.elasticsearch.common.settings.Setting; import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.common.unit.TimeValue; import org.elasticsearch.license.XPackLicenseState; -import org.elasticsearch.xpack.monitoring.MonitoringSettings; import org.elasticsearch.xpack.monitoring.collector.Collector; import org.elasticsearch.xpack.monitoring.exporter.MonitoringDoc; @@ -21,6 +22,8 @@ import java.util.Collections; import java.util.List; import java.util.Objects; +import static org.elasticsearch.common.settings.Setting.boolSetting; + /** * Collector for the Recovery API. *

@@ -29,18 +32,32 @@ import java.util.Objects; */ public class IndexRecoveryCollector extends Collector { + /** + * Timeout value when collecting the recovery information (default to 10s) + */ + public static final Setting INDEX_RECOVERY_TIMEOUT = collectionTimeoutSetting("index.recovery.timeout"); + + /** + * Flag to indicate if only active recoveries should be collected (default to false: all recoveries are collected) + */ + public static final Setting INDEX_RECOVERY_ACTIVE_ONLY = + boolSetting(collectionSetting("index.recovery.active_only"), false, Setting.Property.Dynamic, Setting.Property.NodeScope); + private final Client client; public IndexRecoveryCollector(final Settings settings, final ClusterService clusterService, - final MonitoringSettings monitoringSettings, final XPackLicenseState licenseState, final Client client) { - super(settings, IndexRecoveryMonitoringDoc.TYPE, clusterService, monitoringSettings, licenseState); + super(settings, IndexRecoveryMonitoringDoc.TYPE, clusterService, INDEX_RECOVERY_TIMEOUT, licenseState); this.client = Objects.requireNonNull(client); } + boolean getActiveRecoveriesOnly() { + return clusterService.getClusterSettings().get(INDEX_RECOVERY_ACTIVE_ONLY); + } + @Override protected boolean shouldCollect() { return super.shouldCollect() && isLocalNodeMaster(); @@ -50,10 +67,10 @@ public class IndexRecoveryCollector extends Collector { protected Collection doCollect(final MonitoringDoc.Node node) throws Exception { List results = new ArrayList<>(1); RecoveryResponse recoveryResponse = client.admin().indices().prepareRecoveries() - .setIndices(monitoringSettings.indices()) + .setIndices(getCollectionIndices()) .setIndicesOptions(IndicesOptions.lenientExpandOpen()) - .setActiveOnly(monitoringSettings.recoveryActiveOnly()) - .get(monitoringSettings.recoveryTimeout()); + .setActiveOnly(getActiveRecoveriesOnly()) + .get(getCollectionTimeout()); if (recoveryResponse.hasRecoveries()) { results.add(new IndexRecoveryMonitoringDoc(clusterUUID(), timestamp(), node, recoveryResponse)); diff --git a/plugin/src/main/java/org/elasticsearch/xpack/monitoring/collector/indices/IndexStatsCollector.java b/plugin/src/main/java/org/elasticsearch/xpack/monitoring/collector/indices/IndexStatsCollector.java index e8969247f84..175c885aead 100644 --- a/plugin/src/main/java/org/elasticsearch/xpack/monitoring/collector/indices/IndexStatsCollector.java +++ b/plugin/src/main/java/org/elasticsearch/xpack/monitoring/collector/indices/IndexStatsCollector.java @@ -10,9 +10,10 @@ import org.elasticsearch.action.admin.indices.stats.IndicesStatsResponse; import org.elasticsearch.action.support.IndicesOptions; import org.elasticsearch.client.Client; import org.elasticsearch.cluster.service.ClusterService; +import org.elasticsearch.common.settings.Setting; import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.common.unit.TimeValue; import org.elasticsearch.license.XPackLicenseState; -import org.elasticsearch.xpack.monitoring.MonitoringSettings; import org.elasticsearch.xpack.monitoring.collector.Collector; import org.elasticsearch.xpack.monitoring.exporter.MonitoringDoc; @@ -29,14 +30,18 @@ import java.util.List; */ public class IndexStatsCollector extends Collector { + /** + * Timeout value when collecting index statistics (default to 10s) + */ + public static final Setting INDEX_STATS_TIMEOUT = collectionTimeoutSetting("index.stats.timeout"); + private final Client client; public IndexStatsCollector(final Settings settings, final ClusterService clusterService, - final MonitoringSettings monitoringSettings, final XPackLicenseState licenseState, final Client client) { - super(settings, "index-stats", clusterService, monitoringSettings, licenseState); + super(settings, "index-stats", clusterService, INDEX_STATS_TIMEOUT, licenseState); this.client = client; } @@ -49,7 +54,7 @@ public class IndexStatsCollector extends Collector { protected Collection doCollect(final MonitoringDoc.Node node) throws Exception { final List results = new ArrayList<>(); final IndicesStatsResponse indicesStats = client.admin().indices().prepareStats() - .setIndices(monitoringSettings.indices()) + .setIndices(getCollectionIndices()) .setIndicesOptions(IndicesOptions.lenientExpandOpen()) .clear() .setDocs(true) @@ -62,7 +67,7 @@ public class IndexStatsCollector extends Collector { .setRefresh(true) .setQueryCache(true) .setRequestCache(true) - .get(monitoringSettings.indexStatsTimeout()); + .get(getCollectionTimeout()); final long timestamp = timestamp(); final String clusterUuid = clusterUUID(); diff --git a/plugin/src/main/java/org/elasticsearch/xpack/monitoring/collector/ml/JobStatsCollector.java b/plugin/src/main/java/org/elasticsearch/xpack/monitoring/collector/ml/JobStatsCollector.java index 35ac3c073de..d564a7f0d59 100644 --- a/plugin/src/main/java/org/elasticsearch/xpack/monitoring/collector/ml/JobStatsCollector.java +++ b/plugin/src/main/java/org/elasticsearch/xpack/monitoring/collector/ml/JobStatsCollector.java @@ -6,15 +6,15 @@ package org.elasticsearch.xpack.monitoring.collector.ml; import org.elasticsearch.cluster.metadata.MetaData; -import org.elasticsearch.cluster.node.DiscoveryNode; import org.elasticsearch.cluster.service.ClusterService; +import org.elasticsearch.common.settings.Setting; import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.common.unit.TimeValue; import org.elasticsearch.license.XPackLicenseState; import org.elasticsearch.xpack.XPackClient; import org.elasticsearch.xpack.XPackSettings; import org.elasticsearch.xpack.ml.action.GetJobsStatsAction; import org.elasticsearch.xpack.ml.client.MachineLearningClient; -import org.elasticsearch.xpack.monitoring.MonitoringSettings; import org.elasticsearch.xpack.monitoring.collector.Collector; import org.elasticsearch.xpack.monitoring.exporter.MonitoringDoc; import org.elasticsearch.xpack.security.InternalClient; @@ -32,18 +32,21 @@ import java.util.stream.Collectors; */ public class JobStatsCollector extends Collector { + /** + * Timeout value when collecting ML job statistics (default to 10s) + */ + public static final Setting JOB_STATS_TIMEOUT = collectionTimeoutSetting("ml.job.stats.timeout"); + private final MachineLearningClient client; public JobStatsCollector(final Settings settings, final ClusterService clusterService, - final MonitoringSettings monitoringSettings, final XPackLicenseState licenseState, final InternalClient client) { - this(settings, clusterService, monitoringSettings, licenseState, new XPackClient(client).machineLearning()); + this(settings, clusterService, licenseState, new XPackClient(client).machineLearning()); } JobStatsCollector(final Settings settings, final ClusterService clusterService, - final MonitoringSettings monitoringSettings, final XPackLicenseState licenseState, final MachineLearningClient client) { - super(settings, JobStatsMonitoringDoc.TYPE, clusterService, monitoringSettings, licenseState); + super(settings, JobStatsMonitoringDoc.TYPE, clusterService, JOB_STATS_TIMEOUT, licenseState); this.client = client; } @@ -61,7 +64,7 @@ public class JobStatsCollector extends Collector { // fetch details about all jobs final GetJobsStatsAction.Response jobs = client.getJobsStats(new GetJobsStatsAction.Request(MetaData.ALL)) - .actionGet(monitoringSettings.jobStatsTimeout()); + .actionGet(getCollectionTimeout()); final long timestamp = timestamp(); final String clusterUuid = clusterUUID(); diff --git a/plugin/src/main/java/org/elasticsearch/xpack/monitoring/collector/node/NodeStatsCollector.java b/plugin/src/main/java/org/elasticsearch/xpack/monitoring/collector/node/NodeStatsCollector.java index 9c19931906b..5901c937057 100644 --- a/plugin/src/main/java/org/elasticsearch/xpack/monitoring/collector/node/NodeStatsCollector.java +++ b/plugin/src/main/java/org/elasticsearch/xpack/monitoring/collector/node/NodeStatsCollector.java @@ -12,9 +12,10 @@ import org.elasticsearch.action.admin.indices.stats.CommonStatsFlags; import org.elasticsearch.bootstrap.BootstrapInfo; import org.elasticsearch.client.Client; import org.elasticsearch.cluster.service.ClusterService; +import org.elasticsearch.common.settings.Setting; import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.common.unit.TimeValue; import org.elasticsearch.license.XPackLicenseState; -import org.elasticsearch.xpack.monitoring.MonitoringSettings; import org.elasticsearch.xpack.monitoring.collector.Collector; import org.elasticsearch.xpack.monitoring.exporter.MonitoringDoc; @@ -30,6 +31,11 @@ import java.util.Objects; */ public class NodeStatsCollector extends Collector { + /** + * Timeout value when collecting the nodes statistics (default to 10s) + */ + public static final Setting NODE_STATS_TIMEOUT = collectionTimeoutSetting("node.stats.timeout"); + private static final CommonStatsFlags FLAGS = new CommonStatsFlags(CommonStatsFlags.Flag.Docs, CommonStatsFlags.Flag.FieldData, @@ -44,10 +50,10 @@ public class NodeStatsCollector extends Collector { public NodeStatsCollector(final Settings settings, final ClusterService clusterService, - final MonitoringSettings monitoringSettings, final XPackLicenseState licenseState, final Client client) { - super(settings, NodeStatsMonitoringDoc.TYPE, clusterService, monitoringSettings, licenseState); + + super(settings, NodeStatsMonitoringDoc.TYPE, clusterService, NODE_STATS_TIMEOUT, licenseState); this.client = Objects.requireNonNull(client); } @@ -67,7 +73,7 @@ public class NodeStatsCollector extends Collector { request.threadPool(true); request.fs(true); - final NodesStatsResponse response = client.admin().cluster().nodesStats(request).actionGet(monitoringSettings.nodeStatsTimeout()); + final NodesStatsResponse response = client.admin().cluster().nodesStats(request).actionGet(getCollectionTimeout()); // if there's a failure, then we failed to work with the // _local node (guaranteed a single exception) diff --git a/plugin/src/main/java/org/elasticsearch/xpack/monitoring/collector/shards/ShardsCollector.java b/plugin/src/main/java/org/elasticsearch/xpack/monitoring/collector/shards/ShardsCollector.java index c80380a3df9..f8d7244814c 100644 --- a/plugin/src/main/java/org/elasticsearch/xpack/monitoring/collector/shards/ShardsCollector.java +++ b/plugin/src/main/java/org/elasticsearch/xpack/monitoring/collector/shards/ShardsCollector.java @@ -13,7 +13,6 @@ import org.elasticsearch.cluster.service.ClusterService; import org.elasticsearch.common.regex.Regex; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.license.XPackLicenseState; -import org.elasticsearch.xpack.monitoring.MonitoringSettings; import org.elasticsearch.xpack.monitoring.collector.Collector; import org.elasticsearch.xpack.monitoring.exporter.MonitoringDoc; @@ -33,10 +32,9 @@ public class ShardsCollector extends Collector { public ShardsCollector(final Settings settings, final ClusterService clusterService, - final MonitoringSettings monitoringSettings, final XPackLicenseState licenseState) { - super(settings, ShardMonitoringDoc.TYPE, clusterService, monitoringSettings, licenseState); + super(settings, ShardMonitoringDoc.TYPE, clusterService, null, licenseState); } @Override @@ -48,7 +46,7 @@ public class ShardsCollector extends Collector { protected Collection doCollect(final MonitoringDoc.Node node) throws Exception { final List results = new ArrayList<>(1); - ClusterState clusterState = clusterService.state(); + final ClusterState clusterState = clusterService.state(); if (clusterState != null) { RoutingTable routingTable = clusterState.routingTable(); if (routingTable != null) { @@ -58,8 +56,11 @@ public class ShardsCollector extends Collector { final String stateUUID = clusterState.stateUUID(); final long timestamp = timestamp(); + final String[] indices = getCollectionIndices(); + final boolean isAllIndices = IndexNameExpressionResolver.isAllIndices(Arrays.asList(indices)); + for (ShardRouting shard : shards) { - if (match(shard.getIndexName())) { + if (isAllIndices || Regex.simpleMatch(indices, shard.getIndexName())) { MonitoringDoc.Node shardNode = null; if (shard.assignedToNode()) { // If the shard is assigned to a node, the shard monitoring document refers to this node @@ -73,9 +74,4 @@ public class ShardsCollector extends Collector { } return Collections.unmodifiableCollection(results); } - - private boolean match(final String indexName) { - final String[] indices = monitoringSettings.indices(); - return IndexNameExpressionResolver.isAllIndices(Arrays.asList(indices)) || Regex.simpleMatch(indices, indexName); - } } diff --git a/plugin/src/main/java/org/elasticsearch/xpack/monitoring/exporter/Exporter.java b/plugin/src/main/java/org/elasticsearch/xpack/monitoring/exporter/Exporter.java index e469d71970e..59f3c27db14 100644 --- a/plugin/src/main/java/org/elasticsearch/xpack/monitoring/exporter/Exporter.java +++ b/plugin/src/main/java/org/elasticsearch/xpack/monitoring/exporter/Exporter.java @@ -9,7 +9,6 @@ import org.elasticsearch.cluster.service.ClusterService; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.SettingsException; import org.elasticsearch.license.XPackLicenseState; -import org.elasticsearch.xpack.monitoring.MonitoringSettings; import org.joda.time.format.DateTimeFormat; import org.joda.time.format.DateTimeFormatter; @@ -73,11 +72,11 @@ public abstract class Exporter implements AutoCloseable { protected abstract void doClose(); protected static String settingFQN(final Config config) { - return MonitoringSettings.EXPORTERS_SETTINGS.getKey() + config.name; + return Exporters.EXPORTERS_SETTINGS.getKey() + config.name; } protected static String settingFQN(final Config config, final String setting) { - return MonitoringSettings.EXPORTERS_SETTINGS.getKey() + config.name + "." + setting; + return Exporters.EXPORTERS_SETTINGS.getKey() + config.name + "." + setting; } protected static DateTimeFormatter dateTimeFormatter(final Config config) { diff --git a/plugin/src/main/java/org/elasticsearch/xpack/monitoring/exporter/Exporters.java b/plugin/src/main/java/org/elasticsearch/xpack/monitoring/exporter/Exporters.java index f66f7bbc8c1..04478e01f9b 100644 --- a/plugin/src/main/java/org/elasticsearch/xpack/monitoring/exporter/Exporters.java +++ b/plugin/src/main/java/org/elasticsearch/xpack/monitoring/exporter/Exporters.java @@ -13,11 +13,11 @@ import org.elasticsearch.cluster.ClusterState; import org.elasticsearch.cluster.service.ClusterService; import org.elasticsearch.common.component.AbstractLifecycleComponent; import org.elasticsearch.common.component.Lifecycle; +import org.elasticsearch.common.settings.Setting; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.SettingsException; import org.elasticsearch.common.util.concurrent.ThreadContext; import org.elasticsearch.license.XPackLicenseState; -import org.elasticsearch.xpack.monitoring.MonitoringSettings; import org.elasticsearch.xpack.monitoring.exporter.local.LocalExporter; import java.util.ArrayList; @@ -32,9 +32,16 @@ import java.util.Set; import java.util.concurrent.atomic.AtomicReference; import static java.util.Collections.emptyMap; +import static org.elasticsearch.common.settings.Setting.groupSetting; public class Exporters extends AbstractLifecycleComponent implements Iterable { + /** + * Settings/Options per configured exporter + */ + public static final Setting EXPORTERS_SETTINGS = + groupSetting("xpack.monitoring.exporters.", Setting.Property.Dynamic, Setting.Property.NodeScope); + private final Map factories; private final AtomicReference> exporters; private final ClusterService clusterService; @@ -52,7 +59,7 @@ public class Exporters extends AbstractLifecycleComponent implements Iterable Monitoring.HISTORY_DURATION.get(buildSettings(Monitoring.HISTORY_DURATION.getKey(), oneSecondEarly))); } private Settings buildSettings(String key, String value) { diff --git a/plugin/src/test/java/org/elasticsearch/xpack/monitoring/MonitoringPluginTests.java b/plugin/src/test/java/org/elasticsearch/xpack/monitoring/MonitoringPluginTests.java index 1e697884b07..21834f5add4 100644 --- a/plugin/src/test/java/org/elasticsearch/xpack/monitoring/MonitoringPluginTests.java +++ b/plugin/src/test/java/org/elasticsearch/xpack/monitoring/MonitoringPluginTests.java @@ -86,7 +86,7 @@ public class MonitoringPluginTests extends MonitoringIntegTestCase { protected Settings nodeSettings(int nodeOrdinal) { return Settings.builder() .put(super.nodeSettings(nodeOrdinal)) - .put(MonitoringSettings.INTERVAL.getKey(), "-1") + .put(MonitoringService.INTERVAL.getKey(), "-1") .build(); } diff --git a/plugin/src/test/java/org/elasticsearch/xpack/monitoring/MonitoringServiceTests.java b/plugin/src/test/java/org/elasticsearch/xpack/monitoring/MonitoringServiceTests.java index 54665675c4d..25d33890e12 100644 --- a/plugin/src/test/java/org/elasticsearch/xpack/monitoring/MonitoringServiceTests.java +++ b/plugin/src/test/java/org/elasticsearch/xpack/monitoring/MonitoringServiceTests.java @@ -33,18 +33,20 @@ import static org.mockito.Mockito.when; public class MonitoringServiceTests extends ESTestCase { - TestThreadPool threadPool; - MonitoringService monitoringService; - XPackLicenseState licenseState = mock(XPackLicenseState.class); - ClusterService clusterService; - ClusterSettings clusterSettings; + private TestThreadPool threadPool; + private MonitoringService monitoringService; + private XPackLicenseState licenseState = mock(XPackLicenseState.class); + private ClusterService clusterService; + private ClusterSettings clusterSettings; @Before public void setUp() throws Exception { super.setUp(); threadPool = new TestThreadPool(getTestName()); clusterService = mock(ClusterService.class); - clusterSettings = new ClusterSettings(Settings.EMPTY, new HashSet<>(MonitoringSettings.getSettings())); + + final Monitoring monitoring = new Monitoring(Settings.EMPTY, licenseState); + clusterSettings = new ClusterSettings(Settings.EMPTY, new HashSet<>(monitoring.getSettings())); when(clusterService.getClusterSettings()).thenReturn(clusterSettings); } @@ -77,7 +79,7 @@ public class MonitoringServiceTests extends ESTestCase { } public void testInterval() throws Exception { - Settings settings = Settings.builder().put(MonitoringSettings.INTERVAL.getKey(), TimeValue.MINUS_ONE).build(); + Settings settings = Settings.builder().put(MonitoringService.INTERVAL.getKey(), TimeValue.MINUS_ONE).build(); CountingExporter exporter = new CountingExporter(); monitoringService = new MonitoringService(settings, clusterSettings, threadPool, emptySet(), exporter); @@ -102,7 +104,7 @@ public class MonitoringServiceTests extends ESTestCase { final CountDownLatch latch = new CountDownLatch(1); final BlockingExporter exporter = new BlockingExporter(latch); - Settings settings = Settings.builder().put(MonitoringSettings.INTERVAL.getKey(), MonitoringSettings.MIN_INTERVAL).build(); + Settings settings = Settings.builder().put(MonitoringService.INTERVAL.getKey(), MonitoringService.MIN_INTERVAL).build(); monitoringService = new MonitoringService(settings, clusterSettings, threadPool, emptySet(), exporter); monitoringService.start(); diff --git a/plugin/src/test/java/org/elasticsearch/xpack/monitoring/MonitoringSettingsIntegTests.java b/plugin/src/test/java/org/elasticsearch/xpack/monitoring/MonitoringSettingsIntegTests.java deleted file mode 100644 index 7a05a291e06..00000000000 --- a/plugin/src/test/java/org/elasticsearch/xpack/monitoring/MonitoringSettingsIntegTests.java +++ /dev/null @@ -1,167 +0,0 @@ -/* - * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one - * or more contributor license agreements. Licensed under the Elastic License; - * you may not use this file except in compliance with the Elastic License. - */ -package org.elasticsearch.xpack.monitoring; - -import org.elasticsearch.action.admin.cluster.settings.ClusterUpdateSettingsRequestBuilder; -import org.elasticsearch.common.network.NetworkModule; -import org.elasticsearch.common.settings.Setting; -import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.common.unit.TimeValue; -import org.elasticsearch.plugins.Plugin; -import org.elasticsearch.test.ESIntegTestCase; -import org.elasticsearch.transport.Netty4Plugin; -import org.elasticsearch.xpack.monitoring.test.MonitoringIntegTestCase; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collection; -import java.util.List; - -import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked; -import static org.hamcrest.Matchers.equalTo; - -@ESIntegTestCase.ClusterScope(scope = ESIntegTestCase.Scope.TEST, supportsDedicatedMasters = false, numDataNodes = 1, numClientNodes = 0) -public class MonitoringSettingsIntegTests extends MonitoringIntegTestCase { - private final TimeValue interval = newRandomTimeValue(); - private final TimeValue indexStatsTimeout = newRandomTimeValue(); - private final String[] indices = randomStringArray(); - private final TimeValue clusterStateTimeout = newRandomTimeValue(); - private final TimeValue clusterStatsTimeout = newRandomTimeValue(); - private final TimeValue jobStatsTimeout = newRandomTimeValue(); - private final TimeValue recoveryTimeout = newRandomTimeValue(); - private final Boolean recoveryActiveOnly = randomBoolean(); - - @Override - protected Settings nodeSettings(int nodeOrdinal) { - return Settings.builder() - .put(super.nodeSettings(nodeOrdinal)) - .put(NetworkModule.HTTP_ENABLED.getKey(), false) - .put(monitoringSettings()) - .build(); - } - - @Override - protected Collection> nodePlugins() { - ArrayList> plugins = new ArrayList<>(super.nodePlugins()); - plugins.add(Netty4Plugin.class); // for http - return plugins; - } - - private Settings monitoringSettings() { - return Settings.builder() - .put(MonitoringSettings.INTERVAL.getKey(), interval) - .put(MonitoringSettings.INDEX_STATS_TIMEOUT.getKey(), indexStatsTimeout) - .putArray(MonitoringSettings.INDICES.getKey(), indices) - .put(MonitoringSettings.CLUSTER_STATE_TIMEOUT.getKey(), clusterStateTimeout) - .put(MonitoringSettings.CLUSTER_STATS_TIMEOUT.getKey(), clusterStatsTimeout) - .put(MonitoringSettings.JOB_STATS_TIMEOUT.getKey(), jobStatsTimeout) - .put(MonitoringSettings.INDEX_RECOVERY_TIMEOUT.getKey(), recoveryTimeout) - .put(MonitoringSettings.INDEX_RECOVERY_ACTIVE_ONLY.getKey(), recoveryActiveOnly) - .build(); - } - - public void testMonitoringSettings() throws Exception { - for (final MonitoringSettings monitoringSettings : internalCluster().getInstances(MonitoringSettings.class)) { - assertThat(monitoringSettings.indexStatsTimeout().millis(), equalTo(indexStatsTimeout.millis())); - assertArrayEquals(monitoringSettings.indices(), indices); - assertThat(monitoringSettings.clusterStateTimeout().millis(), equalTo(clusterStateTimeout.millis())); - assertThat(monitoringSettings.clusterStatsTimeout().millis(), equalTo(clusterStatsTimeout.millis())); - assertThat(monitoringSettings.jobStatsTimeout().millis(), equalTo(jobStatsTimeout.millis())); - assertThat(monitoringSettings.recoveryTimeout().millis(), equalTo(recoveryTimeout.millis())); - assertThat(monitoringSettings.recoveryActiveOnly(), equalTo(recoveryActiveOnly)); - } - - for (final MonitoringService service : internalCluster().getInstances(MonitoringService.class)) { - assertThat(service.getInterval().millis(), equalTo(interval.millis())); - - } - - logger.info("--> testing monitoring dynamic settings update"); - Settings.Builder transientSettings = Settings.builder(); - final Setting[] monitoringSettings = new Setting[]{ - MonitoringSettings.INDICES, - MonitoringSettings.INTERVAL, - MonitoringSettings.INDEX_RECOVERY_TIMEOUT, - MonitoringSettings.INDEX_STATS_TIMEOUT, - MonitoringSettings.INDEX_RECOVERY_ACTIVE_ONLY, - MonitoringSettings.CLUSTER_STATE_TIMEOUT, - MonitoringSettings.CLUSTER_STATS_TIMEOUT, - MonitoringSettings.JOB_STATS_TIMEOUT}; - for (Setting setting : monitoringSettings) { - if (setting.isDynamic()) { - if (setting.get(Settings.EMPTY) instanceof TimeValue) { - transientSettings.put(setting.getKey(), newRandomTimeValue().toString()); - } else if (setting.get(Settings.EMPTY) instanceof Boolean) { - transientSettings.put(setting.getKey(), randomBoolean()); - } else if (setting.get(Settings.EMPTY) instanceof List) { - transientSettings.putArray(setting.getKey(), randomStringArray()); - } else { - fail("unknown dynamic setting [" + setting + "]"); - } - } - } - - logger.error("--> updating settings"); - final Settings updatedSettings = transientSettings.build(); - assertAcked(prepareRandomUpdateSettings(updatedSettings).get()); - - logger.error("--> checking that the value has been correctly updated on all monitoring settings services"); - for (Setting setting : monitoringSettings) { - if (setting.isDynamic() == false) { - continue; - } - if (setting == MonitoringSettings.INTERVAL) { - for (final MonitoringService service : internalCluster().getInstances(MonitoringService.class)) { - assertEquals(service.getInterval(), setting.get(updatedSettings)); - } - } else { - for (final MonitoringSettings monitoringSettings1 : internalCluster().getInstances(MonitoringSettings.class)) { - if (setting == MonitoringSettings.INDEX_STATS_TIMEOUT) { - assertEquals(monitoringSettings1.indexStatsTimeout(), setting.get(updatedSettings)); - } else if (setting == MonitoringSettings.CLUSTER_STATS_TIMEOUT) { - assertEquals(monitoringSettings1.clusterStatsTimeout(), setting.get(updatedSettings)); - } else if (setting == MonitoringSettings.JOB_STATS_TIMEOUT) { - assertEquals(monitoringSettings1.jobStatsTimeout(), setting.get(updatedSettings)); - } else if (setting == MonitoringSettings.CLUSTER_STATE_TIMEOUT) { - assertEquals(monitoringSettings1.clusterStateTimeout(), setting.get(updatedSettings)); - } else if (setting == MonitoringSettings.INDEX_RECOVERY_TIMEOUT) { - assertEquals(monitoringSettings1.recoveryTimeout(), setting.get(updatedSettings)); - } else if (setting == MonitoringSettings.INDEX_RECOVERY_ACTIVE_ONLY) { - assertEquals(Boolean.valueOf(monitoringSettings1.recoveryActiveOnly()), setting.get(updatedSettings)); - } else if (setting == MonitoringSettings.INDICES) { - assertEquals(Arrays.asList(monitoringSettings1.indices()), setting.get(updatedSettings)); - } else { - fail("unable to check value for unknown dynamic setting [" + setting + "]"); - } - } - } - } - } - - private ClusterUpdateSettingsRequestBuilder prepareRandomUpdateSettings(Settings updateSettings) { - ClusterUpdateSettingsRequestBuilder requestBuilder = client().admin().cluster().prepareUpdateSettings(); - if (randomBoolean()) { - requestBuilder.setTransientSettings(updateSettings); - } else { - requestBuilder.setPersistentSettings(updateSettings); - } - return requestBuilder; - } - - private TimeValue newRandomTimeValue() { - return TimeValue.parseTimeValue(randomFrom("30m", "1h", "3h", "5h", "7h", "10h", "1d"), null, getClass().getSimpleName()); - } - - private String[] randomStringArray() { - final int size = scaledRandomIntBetween(1, 10); - String[] items = new String[size]; - - for (int i = 0; i < size; i++) { - items[i] = randomAlphaOfLength(5); - } - return items; - } -} diff --git a/plugin/src/test/java/org/elasticsearch/xpack/monitoring/MultiNodesStatsTests.java b/plugin/src/test/java/org/elasticsearch/xpack/monitoring/MultiNodesStatsTests.java index 41f7ef91ed2..d5ced18fc0c 100644 --- a/plugin/src/test/java/org/elasticsearch/xpack/monitoring/MultiNodesStatsTests.java +++ b/plugin/src/test/java/org/elasticsearch/xpack/monitoring/MultiNodesStatsTests.java @@ -14,7 +14,6 @@ import org.elasticsearch.search.aggregations.AggregationBuilders; import org.elasticsearch.search.aggregations.bucket.terms.StringTerms; import org.elasticsearch.test.ESIntegTestCase.ClusterScope; import org.elasticsearch.test.ESIntegTestCase.Scope; -import org.elasticsearch.xpack.monitoring.MonitoringSettings; import org.elasticsearch.xpack.monitoring.collector.node.NodeStatsMonitoringDoc; import org.elasticsearch.xpack.monitoring.test.MonitoringIntegTestCase; import org.junit.After; @@ -33,7 +32,7 @@ public class MultiNodesStatsTests extends MonitoringIntegTestCase { protected Settings nodeSettings(int nodeOrdinal) { return Settings.builder() .put(super.nodeSettings(nodeOrdinal)) - .put(MonitoringSettings.INTERVAL.getKey(), "-1") + .put(MonitoringService.INTERVAL.getKey(), "-1") .put("xpack.monitoring.exporters.default_local.type", "local") .build(); } diff --git a/plugin/src/test/java/org/elasticsearch/xpack/monitoring/OldMonitoringIndicesBackwardsCompatibilityTests.java b/plugin/src/test/java/org/elasticsearch/xpack/monitoring/OldMonitoringIndicesBackwardsCompatibilityTests.java index e08a45d33b7..a284c8f1d8a 100644 --- a/plugin/src/test/java/org/elasticsearch/xpack/monitoring/OldMonitoringIndicesBackwardsCompatibilityTests.java +++ b/plugin/src/test/java/org/elasticsearch/xpack/monitoring/OldMonitoringIndicesBackwardsCompatibilityTests.java @@ -27,6 +27,7 @@ import org.elasticsearch.xpack.monitoring.collector.indices.IndexStatsMonitoring import org.elasticsearch.xpack.monitoring.collector.indices.IndicesStatsMonitoringDoc; import org.elasticsearch.xpack.monitoring.collector.node.NodeStatsMonitoringDoc; import org.elasticsearch.xpack.monitoring.collector.shards.ShardMonitoringDoc; +import org.elasticsearch.xpack.monitoring.exporter.Exporters; import org.hamcrest.Matcher; import org.joda.time.format.DateTimeFormat; @@ -63,9 +64,9 @@ public class OldMonitoringIndicesBackwardsCompatibilityTests extends AbstractOld Settings.Builder settings = Settings.builder().put(super.nodeSettings(ord)) .put(XPackSettings.MONITORING_ENABLED.getKey(), true) // Don't clean old monitoring indexes - we want to make sure we can load them - .put(MonitoringSettings.HISTORY_DURATION.getKey(), TimeValue.timeValueHours(1000 * 365 * 24).getStringRep()) + .put(Monitoring.HISTORY_DURATION.getKey(), TimeValue.timeValueHours(1000 * 365 * 24).getStringRep()) // Do not start monitoring exporters at startup - .put(MonitoringSettings.INTERVAL.getKey(), "-1"); + .put(MonitoringService.INTERVAL.getKey(), "-1"); if (httpExporter) { /* If we want to test the http exporter we have to create it but disable it. We need to create it so we don't use the default @@ -85,7 +86,7 @@ public class OldMonitoringIndicesBackwardsCompatibilityTests extends AbstractOld httpExporter.put("auth.username", SecuritySettingsSource.TEST_USER_NAME); httpExporter.put("auth.password", SecuritySettingsSource.TEST_PASSWORD); - settings.putProperties(httpExporter, k -> MonitoringSettings.EXPORTERS_SETTINGS.getKey() + "my_exporter." + k); + settings.putProperties(httpExporter, k -> Exporters.EXPORTERS_SETTINGS.getKey() + "my_exporter." + k); } @Override @@ -105,7 +106,7 @@ public class OldMonitoringIndicesBackwardsCompatibilityTests extends AbstractOld } // Monitoring can now start to collect new data - Settings.Builder settings = Settings.builder().put(MonitoringSettings.INTERVAL.getKey(), timeValueSeconds(3).getStringRep()); + Settings.Builder settings = Settings.builder().put(MonitoringService.INTERVAL.getKey(), timeValueSeconds(3).getStringRep()); assertAcked(client().admin().cluster().prepareUpdateSettings().setTransientSettings(settings).get()); final String prefix = ".monitoring-" + MonitoredSystem.ES.getSystem() + "-" + TEMPLATE_VERSION + "-"; @@ -167,7 +168,7 @@ public class OldMonitoringIndicesBackwardsCompatibilityTests extends AbstractOld if they have not been re created by some in flight monitoring bulk request */ internalCluster().getInstances(MonitoringService.class).forEach(MonitoringService::stop); - Settings.Builder settings = Settings.builder().put(MonitoringSettings.INTERVAL.getKey(), "-1"); + Settings.Builder settings = Settings.builder().put(MonitoringService.INTERVAL.getKey(), "-1"); if (httpExporter) { logger.info("--> Disabling http exporter after test"); setupHttpExporter(settings, null); diff --git a/plugin/src/test/java/org/elasticsearch/xpack/monitoring/cleaner/AbstractIndicesCleanerTestCase.java b/plugin/src/test/java/org/elasticsearch/xpack/monitoring/cleaner/AbstractIndicesCleanerTestCase.java index 219879b5ee5..835fd85558c 100644 --- a/plugin/src/test/java/org/elasticsearch/xpack/monitoring/cleaner/AbstractIndicesCleanerTestCase.java +++ b/plugin/src/test/java/org/elasticsearch/xpack/monitoring/cleaner/AbstractIndicesCleanerTestCase.java @@ -8,7 +8,8 @@ package org.elasticsearch.xpack.monitoring.cleaner; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.unit.TimeValue; import org.elasticsearch.test.ESIntegTestCase.ClusterScope; -import org.elasticsearch.xpack.monitoring.MonitoringSettings; +import org.elasticsearch.xpack.monitoring.Monitoring; +import org.elasticsearch.xpack.monitoring.MonitoringService; import org.elasticsearch.xpack.monitoring.exporter.Exporter; import org.elasticsearch.xpack.monitoring.exporter.Exporters; import org.elasticsearch.xpack.monitoring.exporter.MonitoringTemplateUtils; @@ -29,7 +30,7 @@ public abstract class AbstractIndicesCleanerTestCase extends MonitoringIntegTest protected Settings nodeSettings(int nodeOrdinal) { Settings.Builder settings = Settings.builder() .put(super.nodeSettings(nodeOrdinal)) - .put(MonitoringSettings.INTERVAL.getKey(), "-1"); + .put(MonitoringService.INTERVAL.getKey(), "-1"); return settings.build(); } @@ -151,7 +152,7 @@ public abstract class AbstractIndicesCleanerTestCase extends MonitoringIntegTest public void testRetentionAsGlobalSetting() throws Exception { final int max = 10; final int retention = randomIntBetween(1, max); - internalCluster().startNode(Settings.builder().put(MonitoringSettings.HISTORY_DURATION.getKey(), + internalCluster().startNode(Settings.builder().put(Monitoring.HISTORY_DURATION.getKey(), String.format(Locale.ROOT, "%dd", retention))); final DateTime now = now(); diff --git a/plugin/src/test/java/org/elasticsearch/xpack/monitoring/cleaner/CleanerServiceTests.java b/plugin/src/test/java/org/elasticsearch/xpack/monitoring/cleaner/CleanerServiceTests.java index 44dd709d0bf..96585f29753 100644 --- a/plugin/src/test/java/org/elasticsearch/xpack/monitoring/cleaner/CleanerServiceTests.java +++ b/plugin/src/test/java/org/elasticsearch/xpack/monitoring/cleaner/CleanerServiceTests.java @@ -12,7 +12,7 @@ import org.elasticsearch.license.XPackLicenseState; import org.elasticsearch.test.ESTestCase; import org.elasticsearch.threadpool.TestThreadPool; import org.elasticsearch.threadpool.ThreadPool; -import org.elasticsearch.xpack.monitoring.MonitoringSettings; +import org.elasticsearch.xpack.monitoring.Monitoring; import org.joda.time.DateTime; import org.joda.time.DateTimeZone; import org.junit.After; @@ -40,7 +40,7 @@ public class CleanerServiceTests extends ESTestCase { @Before public void start() { - clusterSettings = new ClusterSettings(Settings.EMPTY, Collections.singleton(MonitoringSettings.HISTORY_DURATION)); + clusterSettings = new ClusterSettings(Settings.EMPTY, Collections.singleton(Monitoring.HISTORY_DURATION)); threadPool = new TestThreadPool("CleanerServiceTests"); } @@ -54,14 +54,14 @@ public class CleanerServiceTests extends ESTestCase { expectedException.expect(IllegalArgumentException.class); TimeValue expected = TimeValue.timeValueHours(1); - Settings settings = Settings.builder().put(MonitoringSettings.HISTORY_DURATION.getKey(), expected.getStringRep()).build(); + Settings settings = Settings.builder().put(Monitoring.HISTORY_DURATION.getKey(), expected.getStringRep()).build(); new CleanerService(settings, clusterSettings, threadPool, licenseState); } public void testGetRetentionWithSettingWithUpdatesAllowed() { TimeValue expected = TimeValue.timeValueHours(25); - Settings settings = Settings.builder().put(MonitoringSettings.HISTORY_DURATION.getKey(), expected.getStringRep()).build(); + Settings settings = Settings.builder().put(Monitoring.HISTORY_DURATION.getKey(), expected.getStringRep()).build(); when(licenseState.isUpdateRetentionAllowed()).thenReturn(true); @@ -73,7 +73,7 @@ public class CleanerServiceTests extends ESTestCase { public void testGetRetentionDefaultValueWithNoSettings() { when(licenseState.isUpdateRetentionAllowed()).thenReturn(true); - assertEquals(MonitoringSettings.HISTORY_DURATION.get(Settings.EMPTY), + assertEquals(Monitoring.HISTORY_DURATION.get(Settings.EMPTY), new CleanerService(Settings.EMPTY, clusterSettings, threadPool, licenseState).getRetention()); verify(licenseState).isUpdateRetentionAllowed(); @@ -81,11 +81,11 @@ public class CleanerServiceTests extends ESTestCase { public void testGetRetentionDefaultValueWithSettingsButUpdatesNotAllowed() { TimeValue notExpected = TimeValue.timeValueHours(25); - Settings settings = Settings.builder().put(MonitoringSettings.HISTORY_DURATION.getKey(), notExpected.getStringRep()).build(); + Settings settings = Settings.builder().put(Monitoring.HISTORY_DURATION.getKey(), notExpected.getStringRep()).build(); when(licenseState.isUpdateRetentionAllowed()).thenReturn(false); - assertEquals(MonitoringSettings.HISTORY_DURATION.get(Settings.EMPTY), + assertEquals(Monitoring.HISTORY_DURATION.get(Settings.EMPTY), new CleanerService(settings, clusterSettings, threadPool, licenseState).getRetention()); verify(licenseState).isUpdateRetentionAllowed(); diff --git a/plugin/src/test/java/org/elasticsearch/xpack/monitoring/collector/BaseCollectorTestCase.java b/plugin/src/test/java/org/elasticsearch/xpack/monitoring/collector/BaseCollectorTestCase.java index 23a230175c2..f0a154a262c 100644 --- a/plugin/src/test/java/org/elasticsearch/xpack/monitoring/collector/BaseCollectorTestCase.java +++ b/plugin/src/test/java/org/elasticsearch/xpack/monitoring/collector/BaseCollectorTestCase.java @@ -12,12 +12,19 @@ import org.elasticsearch.cluster.metadata.MetaData; import org.elasticsearch.cluster.node.DiscoveryNode; import org.elasticsearch.cluster.node.DiscoveryNodes; import org.elasticsearch.cluster.service.ClusterService; +import org.elasticsearch.common.settings.ClusterSettings; +import org.elasticsearch.common.settings.Setting; +import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.transport.TransportAddress; +import org.elasticsearch.common.unit.TimeValue; +import org.elasticsearch.common.util.set.Sets; import org.elasticsearch.license.XPackLicenseState; import org.elasticsearch.test.ESTestCase; -import org.elasticsearch.xpack.monitoring.MonitoringSettings; +import org.elasticsearch.xpack.monitoring.Monitoring; import org.elasticsearch.xpack.security.InternalClient; +import java.util.function.Function; + import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @@ -28,9 +35,9 @@ public abstract class BaseCollectorTestCase extends ESTestCase { protected ClusterState clusterState; protected DiscoveryNodes nodes; protected MetaData metaData; - protected MonitoringSettings monitoringSettings; protected XPackLicenseState licenseState; protected InternalClient client; + protected Settings settings; @Override public void setUp() throws Exception { @@ -40,9 +47,9 @@ public abstract class BaseCollectorTestCase extends ESTestCase { clusterState = mock(ClusterState.class); nodes = mock(DiscoveryNodes.class); metaData = mock(MetaData.class); - monitoringSettings = mock(MonitoringSettings.class); licenseState = mock(XPackLicenseState.class); client = mock(InternalClient.class); + settings = Settings.EMPTY; } protected void whenLocalNodeElectedMaster(final boolean electedMaster) { @@ -63,6 +70,28 @@ public abstract class BaseCollectorTestCase extends ESTestCase { when(metaData.clusterUUID()).thenReturn(clusterUUID); } + protected void withCollectionTimeout(final Setting collectionTimeout, final TimeValue timeout) { + withCollectionSetting(builder -> builder.put(collectionTimeout.getKey(), timeout.getStringRep())); + } + + protected void withCollectionIndices(final String[] collectionIndices) { + final String key = Collector.INDICES.getKey(); + if (collectionIndices != null) { + withCollectionSetting(builder -> builder.putArray(key, collectionIndices)); + } else { + withCollectionSetting(builder -> builder.putNull(key)); + } + } + + protected void withCollectionSetting(final Function builder) { + settings = Settings.builder() + .put(settings) + .put(builder.apply(Settings.builder()).build()) + .build(); + when(clusterService.getClusterSettings()) + .thenReturn(new ClusterSettings(settings, Sets.newHashSet(new Monitoring(settings, licenseState).getSettings()))); + } + protected static DiscoveryNode localNode(final String uuid) { return new DiscoveryNode(uuid, new TransportAddress(TransportAddress.META_ADDRESS, 9300), Version.CURRENT); } diff --git a/plugin/src/test/java/org/elasticsearch/xpack/monitoring/collector/cluster/ClusterStatsCollectorTests.java b/plugin/src/test/java/org/elasticsearch/xpack/monitoring/collector/cluster/ClusterStatsCollectorTests.java index b44c3ff9299..1157c617d45 100644 --- a/plugin/src/test/java/org/elasticsearch/xpack/monitoring/collector/cluster/ClusterStatsCollectorTests.java +++ b/plugin/src/test/java/org/elasticsearch/xpack/monitoring/collector/cluster/ClusterStatsCollectorTests.java @@ -25,7 +25,6 @@ import org.elasticsearch.xpack.action.XPackUsageResponse; import org.elasticsearch.xpack.logstash.Logstash; import org.elasticsearch.xpack.logstash.LogstashFeatureSet; import org.elasticsearch.xpack.monitoring.MonitoredSystem; -import org.elasticsearch.xpack.monitoring.MonitoringTestUtils; import org.elasticsearch.xpack.monitoring.collector.BaseCollectorTestCase; import org.elasticsearch.xpack.monitoring.exporter.MonitoringDoc; @@ -64,7 +63,7 @@ public class ClusterStatsCollectorTests extends BaseCollectorTestCase { whenLocalNodeElectedMaster(false); final ClusterStatsCollector collector = - new ClusterStatsCollector(Settings.EMPTY, clusterService, monitoringSettings, licenseState, client, licenseService); + new ClusterStatsCollector(Settings.EMPTY, clusterService, licenseState, client, licenseService); assertThat(collector.shouldCollect(), is(false)); verify(nodes).isLocalNodeElectedMaster(); @@ -74,13 +73,16 @@ public class ClusterStatsCollectorTests extends BaseCollectorTestCase { whenLocalNodeElectedMaster(true); final ClusterStatsCollector collector = - new ClusterStatsCollector(Settings.EMPTY, clusterService, monitoringSettings, licenseState, client, licenseService); + new ClusterStatsCollector(Settings.EMPTY, clusterService, licenseState, client, licenseService); assertThat(collector.shouldCollect(), is(true)); verify(nodes).isLocalNodeElectedMaster(); } public void testDoCollect() throws Exception { + final TimeValue timeout = TimeValue.timeValueSeconds(randomIntBetween(1, 120)); + withCollectionTimeout(ClusterStatsCollector.CLUSTER_STATS_TIMEOUT, timeout); + whenLocalNodeElectedMaster(true); final String clusterName = randomAlphaOfLength(10); @@ -102,9 +104,6 @@ public class ClusterStatsCollectorTests extends BaseCollectorTestCase { .build(); when(licenseService.getLicense()).thenReturn(license); - final TimeValue timeout = mock(TimeValue.class); - when(monitoringSettings.clusterStatsTimeout()).thenReturn(timeout); - final ClusterStatsResponse mockClusterStatsResponse = mock(ClusterStatsResponse.class); final ClusterHealthStatus clusterStatus = randomFrom(ClusterHealthStatus.values()); @@ -137,7 +136,8 @@ public class ClusterStatsCollectorTests extends BaseCollectorTestCase { when(xPackUsageFuture.actionGet()).thenReturn(xPackUsageResponse); final ClusterStatsCollector collector = - new ClusterStatsCollector(Settings.EMPTY, clusterService, monitoringSettings, licenseState, client, licenseService); + new ClusterStatsCollector(Settings.EMPTY, clusterService, licenseState, client, licenseService); + assertEquals(timeout, collector.getCollectionTimeout()); final Collection results = collector.doCollect(node); assertEquals(1, results.size()); diff --git a/plugin/src/test/java/org/elasticsearch/xpack/monitoring/collector/indices/IndexRecoveryCollectorTests.java b/plugin/src/test/java/org/elasticsearch/xpack/monitoring/collector/indices/IndexRecoveryCollectorTests.java index 8ad669b25ba..2268c51814f 100644 --- a/plugin/src/test/java/org/elasticsearch/xpack/monitoring/collector/indices/IndexRecoveryCollectorTests.java +++ b/plugin/src/test/java/org/elasticsearch/xpack/monitoring/collector/indices/IndexRecoveryCollectorTests.java @@ -16,6 +16,7 @@ import org.elasticsearch.cluster.node.DiscoveryNode; import org.elasticsearch.cluster.routing.RecoverySource; import org.elasticsearch.cluster.routing.ShardRouting; import org.elasticsearch.cluster.routing.UnassignedInfo; +import org.elasticsearch.common.Strings; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.unit.TimeValue; import org.elasticsearch.index.shard.ShardId; @@ -53,8 +54,7 @@ public class IndexRecoveryCollectorTests extends BaseCollectorTestCase { when(licenseState.isMonitoringAllowed()).thenReturn(false); whenLocalNodeElectedMaster(randomBoolean()); - final IndexRecoveryCollector collector = - new IndexRecoveryCollector(Settings.EMPTY, clusterService, monitoringSettings, licenseState, client); + final IndexRecoveryCollector collector = new IndexRecoveryCollector(Settings.EMPTY, clusterService, licenseState, client); assertThat(collector.shouldCollect(), is(false)); verify(licenseState).isMonitoringAllowed(); @@ -65,8 +65,7 @@ public class IndexRecoveryCollectorTests extends BaseCollectorTestCase { // this controls the blockage whenLocalNodeElectedMaster(false); - final IndexRecoveryCollector collector = - new IndexRecoveryCollector(Settings.EMPTY, clusterService, monitoringSettings, licenseState, client); + final IndexRecoveryCollector collector = new IndexRecoveryCollector(Settings.EMPTY, clusterService, licenseState, client); assertThat(collector.shouldCollect(), is(false)); verify(licenseState).isMonitoringAllowed(); @@ -77,8 +76,7 @@ public class IndexRecoveryCollectorTests extends BaseCollectorTestCase { when(licenseState.isMonitoringAllowed()).thenReturn(true); whenLocalNodeElectedMaster(true); - final IndexRecoveryCollector collector = - new IndexRecoveryCollector(Settings.EMPTY, clusterService, monitoringSettings, licenseState, client); + final IndexRecoveryCollector collector = new IndexRecoveryCollector(Settings.EMPTY, clusterService, licenseState, client); assertThat(collector.shouldCollect(), is(true)); verify(licenseState).isMonitoringAllowed(); @@ -86,6 +84,9 @@ public class IndexRecoveryCollectorTests extends BaseCollectorTestCase { } public void testDoCollect() throws Exception { + final TimeValue timeout = TimeValue.timeValueSeconds(randomIntBetween(1, 120)); + withCollectionTimeout(IndexRecoveryCollector.INDEX_RECOVERY_TIMEOUT, timeout); + whenLocalNodeElectedMaster(true); final String clusterName = randomAlphaOfLength(10); @@ -100,19 +101,18 @@ public class IndexRecoveryCollectorTests extends BaseCollectorTestCase { final MonitoringDoc.Node node = randomMonitoringNode(random()); final boolean recoveryOnly = randomBoolean(); - when(monitoringSettings.recoveryActiveOnly()).thenReturn(recoveryOnly); + withCollectionSetting(builder -> builder.put(IndexRecoveryCollector.INDEX_RECOVERY_ACTIVE_ONLY.getKey(), recoveryOnly)); final String[] indices; if (randomBoolean()) { - indices = null; + indices = randomBoolean() ? null : Strings.EMPTY_ARRAY; } else { indices = new String[randomIntBetween(1, 5)]; for (int i = 0; i < indices.length; i++) { indices[i] = randomAlphaOfLengthBetween(5, 10); } } - when(monitoringSettings.indices()).thenReturn(indices); - when(monitoringSettings.recoveryTimeout()).thenReturn(TimeValue.timeValueSeconds(12)); + withCollectionIndices(indices); final int nbRecoveries = randomBoolean() ? 0 : randomIntBetween(1, 3); final Map> recoveryStates = new HashMap<>(); @@ -130,9 +130,6 @@ public class IndexRecoveryCollectorTests extends BaseCollectorTestCase { final RecoveryResponse recoveryResponse = new RecoveryResponse(randomInt(), randomInt(), randomInt(), randomBoolean(), recoveryStates, emptyList()); - final TimeValue timeout = mock(TimeValue.class); - when(monitoringSettings.recoveryTimeout()).thenReturn(timeout); - final RecoveryRequestBuilder recoveryRequestBuilder = spy(new RecoveryRequestBuilder(mock(ElasticsearchClient.class), RecoveryAction.INSTANCE)); doReturn(recoveryResponse).when(recoveryRequestBuilder).get(eq(timeout)); @@ -146,8 +143,17 @@ public class IndexRecoveryCollectorTests extends BaseCollectorTestCase { final Client client = mock(Client.class); when(client.admin()).thenReturn(adminClient); - final IndexRecoveryCollector collector = - new IndexRecoveryCollector(Settings.EMPTY, clusterService, monitoringSettings, licenseState, client); + final IndexRecoveryCollector collector = new IndexRecoveryCollector(Settings.EMPTY, clusterService, licenseState, client); + assertEquals(timeout, collector.getCollectionTimeout()); + assertEquals(recoveryOnly, collector.getActiveRecoveriesOnly()); + + if (indices != null) { + assertArrayEquals(indices, collector.getCollectionIndices()); + } else { + // Collection indices has a default value equals to emptyList(), + // so it won't return a null indices array + assertArrayEquals(Strings.EMPTY_ARRAY, collector.getCollectionIndices()); + } final Collection results = collector.doCollect(node); verify(indicesAdminClient).prepareRecoveries(); diff --git a/plugin/src/test/java/org/elasticsearch/xpack/monitoring/collector/indices/IndexStatsCollectorTests.java b/plugin/src/test/java/org/elasticsearch/xpack/monitoring/collector/indices/IndexStatsCollectorTests.java index 53986e84c27..db0e8ec4744 100644 --- a/plugin/src/test/java/org/elasticsearch/xpack/monitoring/collector/indices/IndexStatsCollectorTests.java +++ b/plugin/src/test/java/org/elasticsearch/xpack/monitoring/collector/indices/IndexStatsCollectorTests.java @@ -43,8 +43,7 @@ public class IndexStatsCollectorTests extends BaseCollectorTestCase { when(licenseState.isMonitoringAllowed()).thenReturn(false); whenLocalNodeElectedMaster(randomBoolean()); - final IndexStatsCollector collector = - new IndexStatsCollector(Settings.EMPTY, clusterService, monitoringSettings, licenseState, client); + final IndexStatsCollector collector = new IndexStatsCollector(Settings.EMPTY, clusterService, licenseState, client); assertThat(collector.shouldCollect(), is(false)); verify(licenseState).isMonitoringAllowed(); @@ -55,8 +54,7 @@ public class IndexStatsCollectorTests extends BaseCollectorTestCase { // this controls the blockage whenLocalNodeElectedMaster(false); - final IndexStatsCollector collector = - new IndexStatsCollector(Settings.EMPTY, clusterService, monitoringSettings, licenseState, client); + final IndexStatsCollector collector = new IndexStatsCollector(Settings.EMPTY, clusterService, licenseState, client); assertThat(collector.shouldCollect(), is(false)); verify(licenseState).isMonitoringAllowed(); @@ -67,8 +65,7 @@ public class IndexStatsCollectorTests extends BaseCollectorTestCase { when(licenseState.isMonitoringAllowed()).thenReturn(true); whenLocalNodeElectedMaster(true); - final IndexStatsCollector collector = - new IndexStatsCollector(Settings.EMPTY, clusterService, monitoringSettings, licenseState, client); + final IndexStatsCollector collector = new IndexStatsCollector(Settings.EMPTY, clusterService, licenseState, client); assertThat(collector.shouldCollect(), is(true)); verify(licenseState).isMonitoringAllowed(); @@ -76,6 +73,9 @@ public class IndexStatsCollectorTests extends BaseCollectorTestCase { } public void testDoCollect() throws Exception { + final TimeValue timeout = TimeValue.timeValueSeconds(randomIntBetween(1, 120)); + withCollectionTimeout(IndexStatsCollector.INDEX_STATS_TIMEOUT, timeout); + whenLocalNodeElectedMaster(true); final String clusterName = randomAlphaOfLength(10); @@ -86,9 +86,6 @@ public class IndexStatsCollectorTests extends BaseCollectorTestCase { final MonitoringDoc.Node node = randomMonitoringNode(random()); - final TimeValue timeout = mock(TimeValue.class); - when(monitoringSettings.indexStatsTimeout()).thenReturn(timeout); - final Map indicesStats = new HashMap<>(); final int indices = randomIntBetween(0, 10); for (int i = 0; i < indices; i++) { @@ -114,8 +111,8 @@ public class IndexStatsCollectorTests extends BaseCollectorTestCase { final Client client = mock(Client.class); when(client.admin()).thenReturn(adminClient); - final IndexStatsCollector collector = - new IndexStatsCollector(Settings.EMPTY, clusterService, monitoringSettings, licenseState, client); + final IndexStatsCollector collector = new IndexStatsCollector(Settings.EMPTY, clusterService, licenseState, client); + assertEquals(timeout, collector.getCollectionTimeout()); final Collection results = collector.doCollect(node); verify(indicesAdminClient).prepareStats(); diff --git a/plugin/src/test/java/org/elasticsearch/xpack/monitoring/collector/ml/JobStatsCollectorTests.java b/plugin/src/test/java/org/elasticsearch/xpack/monitoring/collector/ml/JobStatsCollectorTests.java index 03461eedcc1..70f9accc8cb 100644 --- a/plugin/src/test/java/org/elasticsearch/xpack/monitoring/collector/ml/JobStatsCollectorTests.java +++ b/plugin/src/test/java/org/elasticsearch/xpack/monitoring/collector/ml/JobStatsCollectorTests.java @@ -47,7 +47,7 @@ public class JobStatsCollectorTests extends BaseCollectorTestCase { when(licenseState.isMonitoringAllowed()).thenReturn(false); when(licenseState.isMachineLearningAllowed()).thenReturn(mlAllowed); - final JobStatsCollector collector = new JobStatsCollector(settings, clusterService, monitoringSettings, licenseState, client); + final JobStatsCollector collector = new JobStatsCollector(settings, clusterService, licenseState, client); assertThat(collector.shouldCollect(), is(false)); @@ -63,7 +63,7 @@ public class JobStatsCollectorTests extends BaseCollectorTestCase { // this controls the blockage whenLocalNodeElectedMaster(false); - final JobStatsCollector collector = new JobStatsCollector(settings, clusterService, monitoringSettings, licenseState, client); + final JobStatsCollector collector = new JobStatsCollector(settings, clusterService, licenseState, client); assertThat(collector.shouldCollect(), is(false)); @@ -78,7 +78,7 @@ public class JobStatsCollectorTests extends BaseCollectorTestCase { when(licenseState.isMachineLearningAllowed()).thenReturn(randomBoolean()); whenLocalNodeElectedMaster(randomBoolean()); - final JobStatsCollector collector = new JobStatsCollector(settings, clusterService, monitoringSettings, licenseState, client); + final JobStatsCollector collector = new JobStatsCollector(settings, clusterService, licenseState, client); assertThat(collector.shouldCollect(), is(false)); @@ -93,7 +93,7 @@ public class JobStatsCollectorTests extends BaseCollectorTestCase { when(licenseState.isMachineLearningAllowed()).thenReturn(false); whenLocalNodeElectedMaster(randomBoolean()); - final JobStatsCollector collector = new JobStatsCollector(settings, clusterService, monitoringSettings, licenseState, client); + final JobStatsCollector collector = new JobStatsCollector(settings, clusterService, licenseState, client); assertThat(collector.shouldCollect(), is(false)); @@ -107,7 +107,7 @@ public class JobStatsCollectorTests extends BaseCollectorTestCase { when(licenseState.isMachineLearningAllowed()).thenReturn(true); whenLocalNodeElectedMaster(true); - final JobStatsCollector collector = new JobStatsCollector(settings, clusterService, monitoringSettings, licenseState, client); + final JobStatsCollector collector = new JobStatsCollector(settings, clusterService, licenseState, client); assertThat(collector.shouldCollect(), is(true)); @@ -115,20 +115,20 @@ public class JobStatsCollectorTests extends BaseCollectorTestCase { } public void testDoCollect() throws Exception { - final TimeValue timeout = mock(TimeValue.class); final MetaData metaData = mock(MetaData.class); final String clusterUuid = randomAlphaOfLength(5); final MonitoringDoc.Node node = randomMonitoringNode(random()); final MachineLearningClient client = mock(MachineLearningClient.class); - when(monitoringSettings.jobStatsTimeout()).thenReturn(timeout); + final TimeValue timeout = TimeValue.timeValueSeconds(randomIntBetween(1, 120)); + withCollectionTimeout(JobStatsCollector.JOB_STATS_TIMEOUT, timeout); when(clusterService.state()).thenReturn(clusterState); when(clusterState.metaData()).thenReturn(metaData); when(metaData.clusterUUID()).thenReturn(clusterUuid); - final JobStatsCollector collector = - new JobStatsCollector(Settings.EMPTY, clusterService, monitoringSettings, licenseState, client); + final JobStatsCollector collector = new JobStatsCollector(Settings.EMPTY, clusterService, licenseState, client); + assertEquals(timeout, collector.getCollectionTimeout()); final List jobStats = mockJobStats(); diff --git a/plugin/src/test/java/org/elasticsearch/xpack/monitoring/collector/node/NodeStatsCollectorTests.java b/plugin/src/test/java/org/elasticsearch/xpack/monitoring/collector/node/NodeStatsCollectorTests.java index c7c67db14d7..66ba843e41f 100644 --- a/plugin/src/test/java/org/elasticsearch/xpack/monitoring/collector/node/NodeStatsCollectorTests.java +++ b/plugin/src/test/java/org/elasticsearch/xpack/monitoring/collector/node/NodeStatsCollectorTests.java @@ -15,6 +15,7 @@ import org.elasticsearch.client.AdminClient; import org.elasticsearch.client.Client; import org.elasticsearch.client.ClusterAdminClient; import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.common.unit.TimeValue; import org.elasticsearch.xpack.monitoring.MonitoredSystem; import org.elasticsearch.xpack.monitoring.collector.BaseCollectorTestCase; import org.elasticsearch.xpack.monitoring.exporter.MonitoringDoc; @@ -41,8 +42,7 @@ public class NodeStatsCollectorTests extends BaseCollectorTestCase { when(licenseState.isMonitoringAllowed()).thenReturn(false); whenLocalNodeElectedMaster(randomBoolean()); - final NodeStatsCollector collector = - new NodeStatsCollector(Settings.EMPTY, clusterService, monitoringSettings, licenseState, client); + final NodeStatsCollector collector = new NodeStatsCollector(Settings.EMPTY, clusterService, licenseState, client); assertThat(collector.shouldCollect(), is(false)); verify(licenseState).isMonitoringAllowed(); @@ -52,8 +52,7 @@ public class NodeStatsCollectorTests extends BaseCollectorTestCase { when(licenseState.isMonitoringAllowed()).thenReturn(true); whenLocalNodeElectedMaster(true); - final NodeStatsCollector collector = - new NodeStatsCollector(Settings.EMPTY, clusterService, monitoringSettings, licenseState, client); + final NodeStatsCollector collector = new NodeStatsCollector(Settings.EMPTY, clusterService, licenseState, client); assertThat(collector.shouldCollect(), is(true)); verify(licenseState).isMonitoringAllowed(); @@ -62,6 +61,9 @@ public class NodeStatsCollectorTests extends BaseCollectorTestCase { public void testDoCollectWithFailures() throws Exception { when(licenseState.isMonitoringAllowed()).thenReturn(true); + final TimeValue timeout = TimeValue.parseTimeValue(randomPositiveTimeValue(), NodeStatsCollectorTests.class.getName()); + withCollectionTimeout(NodeStatsCollector.NODE_STATS_TIMEOUT, timeout); + final NodesStatsResponse nodesStatsResponse = mock(NodesStatsResponse.class); when(nodesStatsResponse.hasFailures()).thenReturn(true); @@ -69,10 +71,10 @@ public class NodeStatsCollectorTests extends BaseCollectorTestCase { when(nodesStatsResponse.failures()).thenReturn(Collections.singletonList(exception)); final Client client = mock(Client.class); - thenReturnNodeStats(client, nodesStatsResponse); + thenReturnNodeStats(client, timeout, nodesStatsResponse); - final NodeStatsCollector collector = - new NodeStatsCollector(Settings.EMPTY, clusterService, monitoringSettings, licenseState, client); + final NodeStatsCollector collector = new NodeStatsCollector(Settings.EMPTY, clusterService, licenseState, client); + assertEquals(timeout, collector.getCollectionTimeout()); final FailedNodeException e = expectThrows(FailedNodeException.class, () -> collector.doCollect(randomMonitoringNode(random()))); assertEquals(exception, e); @@ -81,6 +83,9 @@ public class NodeStatsCollectorTests extends BaseCollectorTestCase { public void testDoCollect() throws Exception { when(licenseState.isMonitoringAllowed()).thenReturn(true); + final TimeValue timeout = TimeValue.timeValueSeconds(randomIntBetween(1, 120)); + withCollectionTimeout(NodeStatsCollector.NODE_STATS_TIMEOUT, timeout); + final boolean isMaster = randomBoolean(); whenLocalNodeElectedMaster(isMaster); @@ -99,10 +104,10 @@ public class NodeStatsCollectorTests extends BaseCollectorTestCase { when(nodeStats.getTimestamp()).thenReturn(timestamp); final Client client = mock(Client.class); - thenReturnNodeStats(client, nodesStatsResponse); + thenReturnNodeStats(client, timeout, nodesStatsResponse); - final NodeStatsCollector collector = - new NodeStatsCollector(Settings.EMPTY, clusterService, monitoringSettings, licenseState, client); + final NodeStatsCollector collector = new NodeStatsCollector(Settings.EMPTY, clusterService, licenseState, client); + assertEquals(timeout, collector.getCollectionTimeout()); final Collection results = collector.doCollect(node); assertEquals(1, results.size()); @@ -124,10 +129,10 @@ public class NodeStatsCollectorTests extends BaseCollectorTestCase { assertThat(document.isMlockall(), equalTo(BootstrapInfo.isMemoryLocked())); } - private void thenReturnNodeStats(final Client client, final NodesStatsResponse nodesStatsResponse) { + private void thenReturnNodeStats(final Client client, final TimeValue timeout, final NodesStatsResponse nodesStatsResponse) { @SuppressWarnings("unchecked") final ActionFuture future = (ActionFuture) mock(ActionFuture.class); - when(future.actionGet(eq(monitoringSettings.nodeStatsTimeout()))).thenReturn(nodesStatsResponse); + when(future.actionGet(eq(timeout))).thenReturn(nodesStatsResponse); final ClusterAdminClient clusterAdminClient = mock(ClusterAdminClient.class); when(clusterAdminClient.nodesStats(any(NodesStatsRequest.class))).thenReturn(future); diff --git a/plugin/src/test/java/org/elasticsearch/xpack/monitoring/collector/shards/ShardsCollectorTests.java b/plugin/src/test/java/org/elasticsearch/xpack/monitoring/collector/shards/ShardsCollectorTests.java index e817b2c2e18..682737ca0bb 100644 --- a/plugin/src/test/java/org/elasticsearch/xpack/monitoring/collector/shards/ShardsCollectorTests.java +++ b/plugin/src/test/java/org/elasticsearch/xpack/monitoring/collector/shards/ShardsCollectorTests.java @@ -47,7 +47,7 @@ public class ShardsCollectorTests extends BaseCollectorTestCase { when(licenseState.isMonitoringAllowed()).thenReturn(false); whenLocalNodeElectedMaster(randomBoolean()); - final ShardsCollector collector = new ShardsCollector(Settings.EMPTY, clusterService, monitoringSettings, licenseState); + final ShardsCollector collector = new ShardsCollector(Settings.EMPTY, clusterService, licenseState); assertThat(collector.shouldCollect(), is(false)); verify(licenseState).isMonitoringAllowed(); @@ -58,7 +58,7 @@ public class ShardsCollectorTests extends BaseCollectorTestCase { // this controls the blockage whenLocalNodeElectedMaster(false); - final ShardsCollector collector = new ShardsCollector(Settings.EMPTY, clusterService, monitoringSettings, licenseState); + final ShardsCollector collector = new ShardsCollector(Settings.EMPTY, clusterService, licenseState); assertThat(collector.shouldCollect(), is(false)); verify(licenseState).isMonitoringAllowed(); @@ -69,7 +69,7 @@ public class ShardsCollectorTests extends BaseCollectorTestCase { when(licenseState.isMonitoringAllowed()).thenReturn(true); whenLocalNodeElectedMaster(true); - final ShardsCollector collector = new ShardsCollector(Settings.EMPTY, clusterService, monitoringSettings, licenseState); + final ShardsCollector collector = new ShardsCollector(Settings.EMPTY, clusterService, licenseState); assertThat(collector.shouldCollect(), is(true)); verify(licenseState).isMonitoringAllowed(); @@ -79,7 +79,7 @@ public class ShardsCollectorTests extends BaseCollectorTestCase { public void testDoCollectWhenNoClusterState() throws Exception { when(clusterService.state()).thenReturn(null); - final ShardsCollector collector = new ShardsCollector(Settings.EMPTY, clusterService, monitoringSettings, licenseState); + final ShardsCollector collector = new ShardsCollector(Settings.EMPTY, clusterService, licenseState); final Collection results = collector.doCollect(randomMonitoringNode(random())); assertThat(results, notNullValue()); @@ -98,7 +98,7 @@ public class ShardsCollectorTests extends BaseCollectorTestCase { when(clusterState.stateUUID()).thenReturn(stateUUID); final String[] indices = randomFrom(NONE, Strings.EMPTY_ARRAY, new String[]{"_all"}, new String[]{"_index*"}); - when(monitoringSettings.indices()).thenReturn(indices); + withCollectionIndices(indices); final RoutingTable routingTable = mockRoutingTable(); when(clusterState.routingTable()).thenReturn(routingTable); @@ -108,7 +108,9 @@ public class ShardsCollectorTests extends BaseCollectorTestCase { when(nodes.get(eq("_current"))).thenReturn(localNode); when(clusterState.getNodes()).thenReturn(nodes); - final ShardsCollector collector = new ShardsCollector(Settings.EMPTY, clusterService, monitoringSettings, licenseState); + final ShardsCollector collector = new ShardsCollector(Settings.EMPTY, clusterService, licenseState); + assertNull(collector.getCollectionTimeout()); + assertArrayEquals(indices, collector.getCollectionIndices()); final Collection results = collector.doCollect(node); assertThat(results, notNullValue()); diff --git a/plugin/src/test/java/org/elasticsearch/xpack/monitoring/exporter/ExportersTests.java b/plugin/src/test/java/org/elasticsearch/xpack/monitoring/exporter/ExportersTests.java index 78b376cf906..004c1ef6ff1 100644 --- a/plugin/src/test/java/org/elasticsearch/xpack/monitoring/exporter/ExportersTests.java +++ b/plugin/src/test/java/org/elasticsearch/xpack/monitoring/exporter/ExportersTests.java @@ -19,8 +19,7 @@ import org.elasticsearch.license.XPackLicenseState; import org.elasticsearch.test.ESTestCase; import org.elasticsearch.threadpool.ThreadPool; import org.elasticsearch.xpack.monitoring.MonitoredSystem; -import org.elasticsearch.xpack.monitoring.MonitoringSettings; -import org.elasticsearch.xpack.monitoring.action.MonitoringBulkDoc; +import org.elasticsearch.xpack.monitoring.MonitoringService; import org.elasticsearch.xpack.monitoring.cleaner.CleanerService; import org.elasticsearch.xpack.monitoring.exporter.local.LocalExporter; import org.elasticsearch.xpack.security.InternalClient; @@ -40,6 +39,7 @@ import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicReference; +import static java.util.Collections.singleton; import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.empty; import static org.hamcrest.Matchers.equalTo; @@ -75,7 +75,7 @@ public class ExportersTests extends ESTestCase { // default state.version() will be 0, which is "valid" state = mock(ClusterState.class); clusterSettings = new ClusterSettings(Settings.EMPTY, - new HashSet<>(Arrays.asList(MonitoringSettings.INTERVAL, MonitoringSettings.EXPORTERS_SETTINGS))); + new HashSet<>(Arrays.asList(MonitoringService.INTERVAL, Exporters.EXPORTERS_SETTINGS))); when(clusterService.getClusterSettings()).thenReturn(clusterSettings); when(clusterService.state()).thenReturn(state); @@ -180,7 +180,7 @@ public class ExportersTests extends ESTestCase { .put("xpack.monitoring.exporters._name0.type", "_type") .put("xpack.monitoring.exporters._name1.type", "_type") .build(); - clusterSettings = new ClusterSettings(nodeSettings, new HashSet<>(Arrays.asList(MonitoringSettings.EXPORTERS_SETTINGS))); + clusterSettings = new ClusterSettings(nodeSettings, singleton(Exporters.EXPORTERS_SETTINGS)); when(clusterService.getClusterSettings()).thenReturn(clusterSettings); exporters = new Exporters(nodeSettings, factories, clusterService, licenseState, threadContext) { diff --git a/plugin/src/test/java/org/elasticsearch/xpack/monitoring/exporter/http/HttpExporterIT.java b/plugin/src/test/java/org/elasticsearch/xpack/monitoring/exporter/http/HttpExporterIT.java index bc1dcc40554..b75a3cd38e9 100644 --- a/plugin/src/test/java/org/elasticsearch/xpack/monitoring/exporter/http/HttpExporterIT.java +++ b/plugin/src/test/java/org/elasticsearch/xpack/monitoring/exporter/http/HttpExporterIT.java @@ -29,7 +29,7 @@ import org.elasticsearch.test.ESIntegTestCase.Scope; import org.elasticsearch.test.http.MockRequest; import org.elasticsearch.test.http.MockResponse; import org.elasticsearch.test.http.MockWebServer; -import org.elasticsearch.xpack.monitoring.MonitoringSettings; +import org.elasticsearch.xpack.monitoring.MonitoringService; import org.elasticsearch.xpack.monitoring.MonitoringTestUtils; import org.elasticsearch.xpack.monitoring.collector.indices.IndexRecoveryMonitoringDoc; import org.elasticsearch.xpack.monitoring.exporter.ClusterAlertsUtil; @@ -107,7 +107,7 @@ public class HttpExporterIT extends MonitoringIntegTestCase { // we make an exporter on demand per test return Settings.builder() .put(super.nodeSettings(nodeOrdinal)) - .put(MonitoringSettings.INTERVAL.getKey(), "-1") + .put(MonitoringService.INTERVAL.getKey(), "-1") .put("xpack.monitoring.exporters._http.type", "http") .put("xpack.monitoring.exporters._http.enabled", false) .build(); diff --git a/plugin/src/test/java/org/elasticsearch/xpack/monitoring/exporter/local/LocalExporterIntegTestCase.java b/plugin/src/test/java/org/elasticsearch/xpack/monitoring/exporter/local/LocalExporterIntegTestCase.java index e6c9e5aab46..b4373a43646 100644 --- a/plugin/src/test/java/org/elasticsearch/xpack/monitoring/exporter/local/LocalExporterIntegTestCase.java +++ b/plugin/src/test/java/org/elasticsearch/xpack/monitoring/exporter/local/LocalExporterIntegTestCase.java @@ -11,7 +11,7 @@ import org.elasticsearch.license.XPackLicenseState; import org.elasticsearch.threadpool.TestThreadPool; import org.elasticsearch.threadpool.ThreadPool; import org.elasticsearch.xpack.XPackSettings; -import org.elasticsearch.xpack.monitoring.MonitoringSettings; +import org.elasticsearch.xpack.monitoring.MonitoringService; import org.elasticsearch.xpack.monitoring.cleaner.CleanerService; import org.elasticsearch.xpack.monitoring.exporter.Exporter; import org.elasticsearch.xpack.monitoring.test.MonitoringIntegTestCase; @@ -65,7 +65,7 @@ public abstract class LocalExporterIntegTestCase extends MonitoringIntegTestCase protected Settings localExporterSettings() { return Settings.builder() - .put(MonitoringSettings.INTERVAL.getKey(), "-1") + .put(MonitoringService.INTERVAL.getKey(), "-1") .put("xpack.monitoring.exporters." + exporterName + ".type", LocalExporter.TYPE) .put("xpack.monitoring.exporters." + exporterName + ".enabled", false) .put("xpack.monitoring.exporters." + exporterName + "." + CLUSTER_ALERTS_MANAGEMENT_SETTING, useClusterAlerts()) diff --git a/plugin/src/test/java/org/elasticsearch/xpack/monitoring/exporter/local/LocalExporterIntegTests.java b/plugin/src/test/java/org/elasticsearch/xpack/monitoring/exporter/local/LocalExporterIntegTests.java index 637f083c794..e5a8dfc1f1c 100644 --- a/plugin/src/test/java/org/elasticsearch/xpack/monitoring/exporter/local/LocalExporterIntegTests.java +++ b/plugin/src/test/java/org/elasticsearch/xpack/monitoring/exporter/local/LocalExporterIntegTests.java @@ -26,7 +26,7 @@ import org.elasticsearch.search.aggregations.metrics.max.Max; import org.elasticsearch.test.ESIntegTestCase; import org.elasticsearch.xpack.XPackClient; import org.elasticsearch.xpack.monitoring.MonitoredSystem; -import org.elasticsearch.xpack.monitoring.MonitoringSettings; +import org.elasticsearch.xpack.monitoring.MonitoringService; import org.elasticsearch.xpack.monitoring.MonitoringTestUtils; import org.elasticsearch.xpack.monitoring.action.MonitoringBulkDoc; import org.elasticsearch.xpack.monitoring.action.MonitoringBulkRequestBuilder; @@ -71,7 +71,7 @@ public class LocalExporterIntegTests extends LocalExporterIntegTestCase { private void stopMonitoring() throws Exception { // Now disabling the monitoring service, so that no more collection are started assertAcked(client().admin().cluster().prepareUpdateSettings().setTransientSettings( - Settings.builder().putNull(MonitoringSettings.INTERVAL.getKey()) + Settings.builder().putNull(MonitoringService.INTERVAL.getKey()) .putNull("xpack.monitoring.exporters._local.enabled") .putNull("xpack.monitoring.exporters._local.index.name.time_format"))); } @@ -126,7 +126,7 @@ public class LocalExporterIntegTests extends LocalExporterIntegTestCase { // monitoring service is started exporterSettings = Settings.builder() - .put(MonitoringSettings.INTERVAL.getKey(), 3L, TimeUnit.SECONDS); + .put(MonitoringService.INTERVAL.getKey(), 3L, TimeUnit.SECONDS); assertAcked(client().admin().cluster().prepareUpdateSettings().setTransientSettings(exporterSettings)); final int numNodes = internalCluster().getNodeNames().length; diff --git a/plugin/src/test/java/org/elasticsearch/xpack/monitoring/security/MonitoringInternalClientTests.java b/plugin/src/test/java/org/elasticsearch/xpack/monitoring/security/MonitoringInternalClientTests.java index 7813035767c..fe78753ba30 100644 --- a/plugin/src/test/java/org/elasticsearch/xpack/monitoring/security/MonitoringInternalClientTests.java +++ b/plugin/src/test/java/org/elasticsearch/xpack/monitoring/security/MonitoringInternalClientTests.java @@ -10,7 +10,7 @@ import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.common.collect.Tuple; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.xcontent.XContentType; -import org.elasticsearch.xpack.monitoring.MonitoringSettings; +import org.elasticsearch.xpack.monitoring.MonitoringService; import org.elasticsearch.xpack.monitoring.test.MonitoringIntegTestCase; import org.elasticsearch.xpack.security.InternalClient; @@ -24,7 +24,7 @@ public class MonitoringInternalClientTests extends MonitoringIntegTestCase { protected Settings nodeSettings(int nodeOrdinal) { return Settings.builder() .put(super.nodeSettings(nodeOrdinal)) - .put(MonitoringSettings.INTERVAL.getKey(), "-1") + .put(MonitoringService.INTERVAL.getKey(), "-1") .build(); } diff --git a/plugin/src/test/java/org/elasticsearch/xpack/monitoring/security/MonitoringSettingsFilterTests.java b/plugin/src/test/java/org/elasticsearch/xpack/monitoring/security/MonitoringSettingsFilterTests.java index b9d2d836e34..1d95f694682 100644 --- a/plugin/src/test/java/org/elasticsearch/xpack/monitoring/security/MonitoringSettingsFilterTests.java +++ b/plugin/src/test/java/org/elasticsearch/xpack/monitoring/security/MonitoringSettingsFilterTests.java @@ -14,7 +14,7 @@ import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.xcontent.json.JsonXContent; import org.elasticsearch.plugins.Plugin; import org.elasticsearch.transport.Netty4Plugin; -import org.elasticsearch.xpack.monitoring.MonitoringSettings; +import org.elasticsearch.xpack.monitoring.MonitoringService; import org.elasticsearch.xpack.monitoring.test.MonitoringIntegTestCase; import java.util.ArrayList; @@ -35,7 +35,7 @@ public class MonitoringSettingsFilterTests extends MonitoringIntegTestCase { return Settings.builder() .put(super.nodeSettings(nodeOrdinal)) .put(NetworkModule.HTTP_ENABLED.getKey(), true) - .put(MonitoringSettings.INTERVAL.getKey(), "-1") + .put(MonitoringService.INTERVAL.getKey(), "-1") .put("xpack.monitoring.exporters._http.type", "http") .put("xpack.monitoring.exporters._http.enabled", false) .put("xpack.monitoring.exporters._http.auth.username", "_user") diff --git a/plugin/src/test/java/org/elasticsearch/xpack/monitoring/test/MonitoringIntegTestCase.java b/plugin/src/test/java/org/elasticsearch/xpack/monitoring/test/MonitoringIntegTestCase.java index 7be94498f76..f906a5a6607 100644 --- a/plugin/src/test/java/org/elasticsearch/xpack/monitoring/test/MonitoringIntegTestCase.java +++ b/plugin/src/test/java/org/elasticsearch/xpack/monitoring/test/MonitoringIntegTestCase.java @@ -34,7 +34,6 @@ import org.elasticsearch.xpack.XPackPlugin; import org.elasticsearch.xpack.XPackSettings; import org.elasticsearch.xpack.ml.MachineLearning; import org.elasticsearch.xpack.monitoring.MonitoringService; -import org.elasticsearch.xpack.monitoring.MonitoringSettings; import org.elasticsearch.xpack.monitoring.client.MonitoringClient; import org.elasticsearch.xpack.monitoring.exporter.ClusterAlertsUtil; import org.elasticsearch.xpack.monitoring.exporter.MonitoringTemplateUtils; @@ -404,7 +403,7 @@ public abstract class MonitoringIntegTestCase extends ESIntegTestCase { protected void updateMonitoringInterval(long value, TimeUnit timeUnit) { assertAcked(client().admin().cluster().prepareUpdateSettings().setTransientSettings( - Settings.builder().put(MonitoringSettings.INTERVAL.getKey(), value, timeUnit))); + Settings.builder().put(MonitoringService.INTERVAL.getKey(), value, timeUnit))); } /** security related settings */