Skip to content

Commit c0a4076

Browse files
authored
Fix and re-enable two monitoring migrate alerts test on 7.x branch (#69326)
Re-enabled TransportMonitoringMigrateAlertsActionTests#testLocalAlertsRemoval and TransportMonitoringMigrateAlertsActionTests#testRepeatedLocalAlertsRemoval on 7.x branch. Includes changes from #69139 and #68752 Relates to #66586 Included commits: * Add more trace logging when installing monitor watches and (#68752) unmute TransportMonitoringMigrateAlertsActionTests#testLocalAlertsRemoval and TransportMonitoringMigrateAlertsActionTests#testRepeatedLocalAlertsRemoval tests Somehow during these tests the monitor watches are not installed. Both tests use the local exporter and this exporter only installs the watches under specific conditions via the elected master node. I suspect the conditions are never met. The http exporter is more relaxed when attempting to install monitor watches and the tests using the http exporter seem not to be prone by the fact that tests fail because monitor watches have not been installed. Relates to #66586 * Manually trigger local exporter to open a bulk in some monitor tests. (#69139) Change tests to use monitor bulk api on elected master node before verifying watcher index exists. Sometimes the monitor service on the elected master doesn't yet export monitor documents resulting in tests using the `ensureInitialLocalResources(...)` method to fail. Cluster alerts watcher are only installed when local exporter tries to resolve local bulk. Relates to #66586
1 parent 3e4b0aa commit c0a4076

File tree

3 files changed

+37
-2
lines changed

3 files changed

+37
-2
lines changed

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,9 +430,20 @@ private void setupClusterAlertsTasks(ClusterState clusterState, boolean clusterS
430430
if (watches != null && watches.allPrimaryShardsActive() == false) {
431431
logger.trace("cannot manage cluster alerts because [.watches] index is not allocated");
432432
} else if ((watches == null || indexExists) && watcherSetup.compareAndSet(false, true)) {
433+
logger.trace("installing monitoring watches");
433434
getClusterAlertsInstallationAsyncActions(indexExists, asyncActions, pendingResponses);
435+
} else {
436+
logger.trace("skipping installing monitoring watches, watches=[{}], indexExists=[{}], watcherSetup=[{}]",
437+
watches, indexExists, watcherSetup.get());
434438
}
439+
} else {
440+
logger.trace("watches shouldn't be setup, because state=[{}] and clusterStateChange=[{}]", state.get(), clusterStateChange);
435441
}
442+
} else {
443+
logger.trace("watches can't be used, because xpack.watcher.enabled=[{}] and " +
444+
"xpack.monitoring.exporters._local.cluster_alerts.management.enabled=[{}]",
445+
XPackSettings.WATCHER_ENABLED.get(config.settings()),
446+
CLUSTER_ALERTS_MANAGEMENT_SETTING.getConcreteSettingForNamespace(config.name()).get(config.settings()));
436447
}
437448
}
438449

@@ -581,6 +592,7 @@ private void getClusterAlertsInstallationAsyncActions(final boolean indexExists,
581592
new ResponseActionListener<>("watch", uniqueWatchId, pendingResponses)));
582593
}
583594
} else if (addWatch) {
595+
logger.trace("adding monitoring watch [{}]", uniqueWatchId);
584596
asyncActions.add(() -> putWatch(watcher, watchId, uniqueWatchId, pendingResponses));
585597
}
586598
}

x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/action/TransportMonitoringMigrateAlertsActionTests.java

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,16 @@
3131
import org.elasticsearch.common.collect.Tuple;
3232
import org.elasticsearch.common.settings.Settings;
3333
import org.elasticsearch.plugins.Plugin;
34+
import org.elasticsearch.rest.RestStatus;
3435
import org.elasticsearch.test.ESIntegTestCase;
3536
import org.elasticsearch.test.http.MockRequest;
3637
import org.elasticsearch.test.http.MockResponse;
3738
import org.elasticsearch.test.http.MockWebServer;
39+
import org.elasticsearch.test.junit.annotations.TestLogging;
3840
import org.elasticsearch.xpack.core.XPackSettings;
41+
import org.elasticsearch.xpack.core.monitoring.action.MonitoringBulkAction;
42+
import org.elasticsearch.xpack.core.monitoring.action.MonitoringBulkRequest;
43+
import org.elasticsearch.xpack.core.monitoring.action.MonitoringBulkResponse;
3944
import org.elasticsearch.xpack.core.monitoring.action.MonitoringMigrateAlertsAction;
4045
import org.elasticsearch.xpack.core.monitoring.action.MonitoringMigrateAlertsRequest;
4146
import org.elasticsearch.xpack.core.monitoring.action.MonitoringMigrateAlertsResponse;
@@ -49,6 +54,7 @@
4954
import org.elasticsearch.xpack.monitoring.exporter.ClusterAlertsUtil;
5055
import org.elasticsearch.xpack.monitoring.exporter.http.HttpExporter;
5156
import org.elasticsearch.xpack.monitoring.exporter.local.LocalExporter;
57+
import org.elasticsearch.xpack.monitoring.exporter.local.LocalExporterIntegTests;
5258
import org.elasticsearch.xpack.monitoring.test.MonitoringIntegTestCase;
5359
import org.junit.After;
5460
import org.junit.Before;
@@ -124,7 +130,9 @@ private void stopMonitoring() {
124130
));
125131
}
126132

127-
@AwaitsFix(bugUrl = "https://github.com/elastic/elasticsearch/issues/66586")
133+
@TestLogging(
134+
value = "org.elasticsearch.xpack.monitoring.exporter.local:trace",
135+
reason = "to ensure we log local exporter on trace level")
128136
public void testLocalAlertsRemoval() throws Exception {
129137
try {
130138
// start monitoring service
@@ -159,6 +167,9 @@ public void testLocalAlertsRemoval() throws Exception {
159167
}
160168
}
161169

170+
@TestLogging(
171+
value = "org.elasticsearch.xpack.monitoring.exporter.local:trace",
172+
reason = "to ensure we log local exporter on trace level")
162173
public void testRepeatedLocalAlertsRemoval() throws Exception {
163174
try {
164175
// start monitoring service
@@ -474,6 +485,18 @@ public void testRemoteAlertsRemoteDisallowsWatcher() throws Exception {
474485
}
475486

476487
private void ensureInitialLocalResources() throws Exception {
488+
// Should trigger setting up alert watches via LocalExporter#openBulk(...) and
489+
// then eventually to LocalExporter#setupIfElectedMaster(...)
490+
// Sometimes this last method doesn't install watches, because elected master node doesn't export monitor documents.
491+
// and then these assertions here fail.
492+
{
493+
MonitoringBulkRequest request = new MonitoringBulkRequest();
494+
request.add(LocalExporterIntegTests.createMonitoringBulkDoc());
495+
String masterNode = internalCluster().getMasterName();
496+
MonitoringBulkResponse response = client(masterNode).execute(MonitoringBulkAction.INSTANCE, request).actionGet();
497+
assertThat(response.status(), equalTo(RestStatus.OK));
498+
}
499+
477500
waitForWatcherIndices();
478501
assertBusy(() -> {
479502
assertThat(indexExists(".monitoring-*"), is(true));

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ private void checkMonitoringDocs() {
303303
}
304304
}
305305

306-
private static MonitoringBulkDoc createMonitoringBulkDoc() throws IOException {
306+
public static MonitoringBulkDoc createMonitoringBulkDoc() throws IOException {
307307
final MonitoredSystem system = randomFrom(BEATS, KIBANA, LOGSTASH);
308308
final XContentType xContentType = randomFrom(XContentType.values());
309309
final BytesReference source;

0 commit comments

Comments
 (0)