Skip to content

Commit b2bbd8e

Browse files
committed
Ensure type exists for all monitoring configuration (elastic#57399)
elastic#47711 and elastic#47246 helped to validate that monitoring settings are rejected at time of setting the monitoring settings. Else an invalid monitoring setting can find it's way into the cluster state and result in an exception thrown [1] on the cluster state application (there by causing significant issues). Some additional monitoring settings have been identified that can result in invalid cluster state that also result in exceptions thrown on cluster state application. All settings require a type of either http or local to be applicable. When a setting is changed, the exporters are automatically updated with the new settings. However, if the old or new settings lack of a type setting an exception will be thrown (since exporters are always of type 'http' or 'local'). Arguably we shouldn't blindly create and destroy new exporters on each monitoring setting update, but the lifecycle of the exporters is abit out the scope this PR is trying to address. This commit introduces a similar methodology to check for validity as elastic#47711 and elastic#47246 but this time for ALL (including non-http) settings. Monitoring settings are not useful unless there an exporter with a type defined. The type is used as dependent setting, such that it must exist to set the value. This ensures that when any monitoring settings changes that they can only get added to cluster state if the type exists. If the type exists (and the other validations pass) then the exporters will get re-built and the cluster state remains valid. Tests have been included to ensure that all dynamic monitoring settings have the type as dependent settings. [1] org.elasticsearch.common.settings.SettingsException: missing exporter type for [found-user-defined] exporter at org.elasticsearch.xpack.monitoring.exporter.Exporters.initExporters(Exporters.java:126) ~[?:?]
1 parent 025682d commit b2bbd8e

File tree

8 files changed

+84
-20
lines changed

8 files changed

+84
-20
lines changed

server/src/main/java/org/elasticsearch/common/settings/Setting.java

+9
Original file line numberDiff line numberDiff line change
@@ -742,6 +742,15 @@ private Stream<String> matchStream(Settings settings) {
742742
return settings.keySet().stream().filter(this::match).map(key::getConcreteString);
743743
}
744744

745+
/**
746+
* Get the raw list of dependencies. This method is exposed for testing purposes and {@link #getSettingsDependencies(String)}
747+
* should be preferred for most all cases.
748+
* @return the raw list of dependencies for this setting
749+
*/
750+
public Set<AffixSettingDependency> getDependencies() {
751+
return Collections.unmodifiableSet(dependencies);
752+
}
753+
745754
@Override
746755
public Set<SettingDependency> getSettingsDependencies(String settingsKey) {
747756
if (dependencies.isEmpty()) {

x-pack/plugin/monitoring/src/internalClusterTest/java/org/elasticsearch/xpack/monitoring/integration/MonitoringIT.java

+2
Original file line numberDiff line numberDiff line change
@@ -596,6 +596,7 @@ public void enableMonitoring() throws Exception {
596596

597597
final Settings settings = Settings.builder()
598598
.put("xpack.monitoring.collection.enabled", true)
599+
.put("xpack.monitoring.exporters._local.type", "local")
599600
.put("xpack.monitoring.exporters._local.enabled", true)
600601
.build();
601602

@@ -622,6 +623,7 @@ public void enableMonitoring() throws Exception {
622623
public void disableMonitoring() throws Exception {
623624
final Settings settings = Settings.builder()
624625
.putNull("xpack.monitoring.collection.enabled")
626+
.putNull("xpack.monitoring.exporters._local.type")
625627
.putNull("xpack.monitoring.exporters._local.enabled")
626628
.putNull("cluster.metadata.display_name")
627629
.build();

x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/exporter/Exporter.java

+8-5
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,11 @@
2525

2626
public abstract class Exporter implements AutoCloseable {
2727

28+
public static Setting.AffixSettingDependency TYPE_DEPENDENCY = () -> Exporter.TYPE_SETTING;
29+
2830
private static final Setting.AffixSetting<Boolean> ENABLED_SETTING =
2931
Setting.affixKeySetting("xpack.monitoring.exporters.","enabled",
30-
key -> Setting.boolSetting(key, true, Property.Dynamic, Property.NodeScope));
32+
key -> Setting.boolSetting(key, true, Property.Dynamic, Property.NodeScope), TYPE_DEPENDENCY);
3133

3234
public static final Setting.AffixSetting<String> TYPE_SETTING = Setting.affixKeySetting(
3335
"xpack.monitoring.exporters.",
@@ -83,21 +85,22 @@ public Iterator<Setting<?>> settings() {
8385
*/
8486
public static final Setting.AffixSetting<Boolean> USE_INGEST_PIPELINE_SETTING =
8587
Setting.affixKeySetting("xpack.monitoring.exporters.","use_ingest",
86-
key -> Setting.boolSetting(key, true, Property.Dynamic, Property.NodeScope));
88+
key -> Setting.boolSetting(key, true, Property.Dynamic, Property.NodeScope), TYPE_DEPENDENCY);
8789
/**
8890
* Every {@code Exporter} allows users to explicitly disable cluster alerts.
8991
*/
9092
public static final Setting.AffixSetting<Boolean> CLUSTER_ALERTS_MANAGEMENT_SETTING =
9193
Setting.affixKeySetting("xpack.monitoring.exporters.", "cluster_alerts.management.enabled",
92-
key -> Setting.boolSetting(key, true, Property.Dynamic, Property.NodeScope));
94+
key -> Setting.boolSetting(key, true, Property.Dynamic, Property.NodeScope), TYPE_DEPENDENCY);
9395
/**
9496
* Every {@code Exporter} allows users to explicitly disable specific cluster alerts.
9597
* <p>
9698
* When cluster alerts management is enabled, this should delete anything blacklisted here in addition to not creating it.
9799
*/
98100
public static final Setting.AffixSetting<List<String>> CLUSTER_ALERTS_BLACKLIST_SETTING = Setting
99101
.affixKeySetting("xpack.monitoring.exporters.", "cluster_alerts.management.blacklist",
100-
key -> Setting.listSetting(key, Collections.emptyList(), Function.identity(), Property.Dynamic, Property.NodeScope));
102+
key -> Setting.listSetting(key, Collections.emptyList(), Function.identity(), Property.Dynamic, Property.NodeScope),
103+
TYPE_DEPENDENCY);
101104

102105
/**
103106
* Every {@code Exporter} allows users to use a different index time format.
@@ -109,7 +112,7 @@ public Iterator<Setting<?>> settings() {
109112
Exporter.INDEX_FORMAT,
110113
DateFormatter::forPattern,
111114
Property.Dynamic,
112-
Property.NodeScope));
115+
Property.NodeScope), TYPE_DEPENDENCY);
113116

114117
private static final String INDEX_FORMAT = "yyyy.MM.dd";
115118

x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/exporter/http/HttpExporter.java

+16-14
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public class HttpExporter extends Exporter {
8181

8282
public static final String TYPE = "http";
8383

84-
private static Setting.AffixSettingDependency TYPE_DEPENDENCY = new Setting.AffixSettingDependency() {
84+
private static Setting.AffixSettingDependency HTTP_TYPE_DEPENDENCY = new Setting.AffixSettingDependency() {
8585
@Override
8686
public Setting.AffixSetting<String> getSetting() {
8787
return Exporter.TYPE_SETTING;
@@ -168,30 +168,32 @@ public Iterator<Setting<?>> settings() {
168168
},
169169
Property.Dynamic,
170170
Property.NodeScope),
171-
TYPE_DEPENDENCY);
171+
HTTP_TYPE_DEPENDENCY);
172172

173173
/**
174174
* Master timeout associated with bulk requests.
175175
*/
176176
public static final Setting.AffixSetting<TimeValue> BULK_TIMEOUT_SETTING =
177177
Setting.affixKeySetting("xpack.monitoring.exporters.","bulk.timeout",
178-
(key) -> Setting.timeSetting(key, TimeValue.MINUS_ONE, Property.Dynamic, Property.NodeScope), TYPE_DEPENDENCY);
178+
(key) -> Setting.timeSetting(key, TimeValue.MINUS_ONE, Property.Dynamic, Property.NodeScope), HTTP_TYPE_DEPENDENCY);
179179
/**
180180
* Timeout used for initiating a connection.
181181
*/
182182
public static final Setting.AffixSetting<TimeValue> CONNECTION_TIMEOUT_SETTING =
183183
Setting.affixKeySetting(
184184
"xpack.monitoring.exporters.",
185185
"connection.timeout",
186-
(key) -> Setting.timeSetting(key, TimeValue.timeValueSeconds(6), Property.Dynamic, Property.NodeScope), TYPE_DEPENDENCY);
186+
(key) -> Setting.timeSetting(key, TimeValue.timeValueSeconds(6), Property.Dynamic, Property.NodeScope),
187+
HTTP_TYPE_DEPENDENCY);
187188
/**
188189
* Timeout used for reading from the connection.
189190
*/
190191
public static final Setting.AffixSetting<TimeValue> CONNECTION_READ_TIMEOUT_SETTING =
191192
Setting.affixKeySetting(
192193
"xpack.monitoring.exporters.",
193194
"connection.read_timeout",
194-
(key) -> Setting.timeSetting(key, TimeValue.timeValueSeconds(60), Property.Dynamic, Property.NodeScope), TYPE_DEPENDENCY);
195+
(key) -> Setting.timeSetting(key, TimeValue.timeValueSeconds(60), Property.Dynamic, Property.NodeScope),
196+
HTTP_TYPE_DEPENDENCY);
195197
/**
196198
* Username for basic auth.
197199
*/
@@ -242,7 +244,7 @@ public Iterator<Setting<?>> settings() {
242244
Property.Dynamic,
243245
Property.NodeScope,
244246
Property.Filtered),
245-
TYPE_DEPENDENCY);
247+
HTTP_TYPE_DEPENDENCY);
246248
/**
247249
* Password for basic auth.
248250
*/
@@ -297,7 +299,7 @@ public Iterator<Setting<?>> settings() {
297299
"xpack.monitoring.exporters.",
298300
"auth.secure_password",
299301
key -> SecureSetting.secureString(key, null),
300-
TYPE_DEPENDENCY);
302+
HTTP_TYPE_DEPENDENCY);
301303
/**
302304
* The SSL settings.
303305
*
@@ -308,7 +310,7 @@ public Iterator<Setting<?>> settings() {
308310
"xpack.monitoring.exporters.",
309311
"ssl",
310312
(key) -> Setting.groupSetting(key + ".", Property.Dynamic, Property.NodeScope, Property.Filtered),
311-
TYPE_DEPENDENCY);
313+
HTTP_TYPE_DEPENDENCY);
312314

313315
/**
314316
* Proxy setting to allow users to send requests to a remote cluster that requires a proxy base path.
@@ -329,13 +331,13 @@ public Iterator<Setting<?>> settings() {
329331
},
330332
Property.Dynamic,
331333
Property.NodeScope),
332-
TYPE_DEPENDENCY);
334+
HTTP_TYPE_DEPENDENCY);
333335
/**
334336
* A boolean setting to enable or disable sniffing for extra connections.
335337
*/
336338
public static final Setting.AffixSetting<Boolean> SNIFF_ENABLED_SETTING =
337339
Setting.affixKeySetting("xpack.monitoring.exporters.","sniff.enabled",
338-
(key) -> Setting.boolSetting(key, false, Property.Dynamic, Property.NodeScope), TYPE_DEPENDENCY);
340+
(key) -> Setting.boolSetting(key, false, Property.Dynamic, Property.NodeScope), HTTP_TYPE_DEPENDENCY);
339341
/**
340342
* A parent setting to header key/value pairs, whose names are user defined.
341343
*/
@@ -358,7 +360,7 @@ public Iterator<Setting<?>> settings() {
358360
},
359361
Property.Dynamic,
360362
Property.NodeScope),
361-
TYPE_DEPENDENCY);
363+
HTTP_TYPE_DEPENDENCY);
362364
/**
363365
* Blacklist of headers that the user is not allowed to set.
364366
* <p>
@@ -370,19 +372,19 @@ public Iterator<Setting<?>> settings() {
370372
*/
371373
public static final Setting.AffixSetting<TimeValue> TEMPLATE_CHECK_TIMEOUT_SETTING =
372374
Setting.affixKeySetting("xpack.monitoring.exporters.","index.template.master_timeout",
373-
(key) -> Setting.timeSetting(key, TimeValue.MINUS_ONE, Property.Dynamic, Property.NodeScope), TYPE_DEPENDENCY);
375+
(key) -> Setting.timeSetting(key, TimeValue.MINUS_ONE, Property.Dynamic, Property.NodeScope), HTTP_TYPE_DEPENDENCY);
374376
/**
375377
* A boolean setting to enable or disable whether to create placeholders for the old templates.
376378
*/
377379
public static final Setting.AffixSetting<Boolean> TEMPLATE_CREATE_LEGACY_VERSIONS_SETTING =
378380
Setting.affixKeySetting("xpack.monitoring.exporters.","index.template.create_legacy_templates",
379-
(key) -> Setting.boolSetting(key, true, Property.Dynamic, Property.NodeScope), TYPE_DEPENDENCY);
381+
(key) -> Setting.boolSetting(key, true, Property.Dynamic, Property.NodeScope), HTTP_TYPE_DEPENDENCY);
380382
/**
381383
* ES level timeout used when checking and writing pipelines (used to speed up tests)
382384
*/
383385
public static final Setting.AffixSetting<TimeValue> PIPELINE_CHECK_TIMEOUT_SETTING =
384386
Setting.affixKeySetting("xpack.monitoring.exporters.","index.pipeline.master_timeout",
385-
(key) -> Setting.timeSetting(key, TimeValue.MINUS_ONE, Property.Dynamic, Property.NodeScope), TYPE_DEPENDENCY);
387+
(key) -> Setting.timeSetting(key, TimeValue.MINUS_ONE, Property.Dynamic, Property.NodeScope), HTTP_TYPE_DEPENDENCY);
386388

387389
/**
388390
* Minimum supported version of the remote monitoring cluster (same major).

x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/exporter/local/LocalExporter.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public class LocalExporter extends Exporter implements ClusterStateListener, Cle
9696
public static final Setting.AffixSetting<TimeValue> WAIT_MASTER_TIMEOUT_SETTING = Setting.affixKeySetting(
9797
"xpack.monitoring.exporters.",
9898
"wait_master.timeout",
99-
(key) -> Setting.timeSetting(key, TimeValue.timeValueSeconds(30), Property.Dynamic, Property.NodeScope)
99+
(key) -> Setting.timeSetting(key, TimeValue.timeValueSeconds(30), Property.Dynamic, Property.NodeScope), TYPE_DEPENDENCY
100100
);
101101

102102
private final Client client;

x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/exporter/ExportersTests.java

+45
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,9 @@
4949
import java.util.concurrent.TimeUnit;
5050
import java.util.concurrent.atomic.AtomicInteger;
5151
import java.util.concurrent.atomic.AtomicReference;
52+
import java.util.stream.Collectors;
5253

54+
import static org.hamcrest.Matchers.contains;
5355
import static org.hamcrest.Matchers.containsString;
5456
import static org.hamcrest.Matchers.empty;
5557
import static org.hamcrest.Matchers.equalTo;
@@ -337,6 +339,49 @@ public void testConcurrentExports() throws Exception {
337339
exporters.close();
338340
}
339341

342+
/**
343+
* All dynamic monitoring settings should have dependency on type.
344+
*/
345+
public void testSettingsDependency() {
346+
List<Setting.AffixSetting<?>> settings = Exporters.getSettings().stream().filter(Setting::isDynamic).collect(Collectors.toList());
347+
settings.stream().filter(s -> s.getKey().equals("xpack.monitoring.exporters.*.type") == false)
348+
.forEach(setting -> assertThat(setting.getKey() + " does not have a dependency on type",
349+
setting.getDependencies().stream().map(Setting.AffixSettingDependency::getSetting).distinct().collect(Collectors.toList()),
350+
contains(Exporter.TYPE_SETTING)));
351+
}
352+
353+
/**
354+
* This is a variation of testing that all settings validated at cluster state update ensure that the type is set. This workflow
355+
* emulates adding valid settings, then attempting to remove the type. This should never be allowed since type if type is null
356+
* then any associated settings are extraneous and thus invalid (and can cause validation issues on cluster state application).
357+
*/
358+
public void testRemoveType() {
359+
//run the update for all dynamic settings and ensure that they correctly throw an exception
360+
List<Setting.AffixSetting<?>> settings = Exporters.getSettings().stream().filter(Setting::isDynamic).collect(Collectors.toList());
361+
settings.stream().filter(s -> s.getKey().equals("xpack.monitoring.exporters.*.type") == false)
362+
.forEach(setting -> {
363+
String fullSettingName = setting.getKey().replace("*", "foobar");
364+
Settings nodeSettings = Settings.builder()
365+
.put("xpack.monitoring.exporters.foobar.type", randomFrom("local, http")) //actual type should not matter
366+
.put(fullSettingName, "")
367+
.build();
368+
369+
clusterSettings = new ClusterSettings(nodeSettings, new HashSet<>(Exporters.getSettings()));
370+
when(clusterService.getClusterSettings()).thenReturn(clusterSettings);
371+
372+
Settings update = Settings.builder()
373+
.put("xpack.monitoring.exporters.foobar.type", (String) null)
374+
.build();
375+
376+
Settings.Builder target = Settings.builder().put(nodeSettings);
377+
clusterSettings.updateDynamicSettings(update, target, Settings.builder(), "persistent");
378+
IllegalArgumentException e = expectThrows(IllegalArgumentException.class,
379+
() -> clusterSettings.validate(target.build(), true));
380+
assertThat(e.getMessage(),
381+
containsString("missing required setting [xpack.monitoring.exporters.foobar.type] for setting [" + fullSettingName));
382+
});
383+
}
384+
340385
/**
341386
* Attempt to export a random number of documents via {@code exporters} from multiple threads.
342387
*

x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/exporter/local/LocalExporterIntegTests.java

+2
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ private void stopMonitoring() {
6666
// Now disabling the monitoring service, so that no more collection are started
6767
assertAcked(client().admin().cluster().prepareUpdateSettings().setTransientSettings(
6868
Settings.builder().putNull(MonitoringService.ENABLED.getKey())
69+
.putNull("xpack.monitoring.exporters._local.type")
6970
.putNull("xpack.monitoring.exporters._local.enabled")
7071
.putNull("xpack.monitoring.exporters._local.cluster_alerts.management.enabled")
7172
.putNull("xpack.monitoring.exporters._local.index.name.time_format")));
@@ -86,6 +87,7 @@ public void testExport() throws Exception {
8687
// start the monitoring service so that /_monitoring/bulk is not ignored
8788
final Settings.Builder exporterSettings = Settings.builder()
8889
.put(MonitoringService.ENABLED.getKey(), true)
90+
.put("xpack.monitoring.exporters._local.type", LocalExporter.TYPE)
8991
.put("xpack.monitoring.exporters._local.enabled", true)
9092
.put("xpack.monitoring.exporters._local.cluster_alerts.management.enabled", false);
9193

x-pack/plugin/src/test/java/org/elasticsearch/xpack/test/rest/XPackRestIT.java

+1
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ private void enableMonitoring() throws Exception {
123123
final Map<String, Object> settings = new HashMap<>();
124124
settings.put("xpack.monitoring.collection.enabled", true);
125125
settings.put("xpack.monitoring.collection.interval", "1s");
126+
settings.put("xpack.monitoring.exporters._local.type", "local");
126127
settings.put("xpack.monitoring.exporters._local.enabled", true);
127128

128129
awaitCallApi("cluster.put_settings", emptyMap(),

0 commit comments

Comments
 (0)