Skip to content

Add action to decommission legacy monitoring cluster alerts #64373

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 21 commits into from
Dec 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
6d15cb2
Basic action definitions made
jbaiera Sep 24, 2020
fd7bc4a
Add ability to specify a consumer for when setup is completed by asyn…
jbaiera Sep 29, 2020
76e4303
Refactor HttpResource to return more information on its success or fa…
jbaiera Oct 5, 2020
9060cfb
Transport action to remove existing cluster alerts from configured ex…
jbaiera Oct 26, 2020
b0c5638
satisfy precommit
jbaiera Oct 26, 2020
63704e5
Fix duplicate setting
jbaiera Oct 28, 2020
a9e7413
Add preliminary rest handler
jbaiera Oct 28, 2020
dfa0877
Some serious test cleanup, including a fix for a testing bug
jbaiera Oct 29, 2020
d9c6f00
The infinite struggle of forgetting to run precommit
jbaiera Oct 29, 2020
6c3355b
The struggle continues
jbaiera Oct 29, 2020
7881f15
Use a semaphore to lock the exporters out of running their setup unti…
jbaiera Nov 11, 2020
25633f1
Merge branch 'master' into monitoring-decommission-watch-action
elasticmachine Nov 12, 2020
0996f6e
PR feedback
jbaiera Dec 7, 2020
8b52a5b
precommit
jbaiera Dec 7, 2020
16065ed
Merge branch 'master' into monitoring-decommission-watch-action
elasticmachine Dec 7, 2020
1642f52
Wrap mocked listeners to fix calls to ActionListener.map
jbaiera Dec 8, 2020
e911d7a
overzealous with one of my reverts
jbaiera Dec 9, 2020
9942a25
Merge branch 'master' into monitoring-decommission-watch-action
elasticmachine Dec 10, 2020
6a3e472
Add alert migration API to non operator actions
jbaiera Dec 10, 2020
0622599
Merge branch 'master' into monitoring-decommission-watch-action
elasticmachine Dec 13, 2020
ae5be4c
PR comments
jbaiera Dec 14, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
* 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.core.monitoring.action;

import org.elasticsearch.action.ActionType;

public class MonitoringMigrateAlertsAction extends ActionType<MonitoringMigrateAlertsResponse> {

public static final MonitoringMigrateAlertsAction INSTANCE = new MonitoringMigrateAlertsAction();
public static final String NAME = "cluster:admin/xpack/monitoring/migrate/alerts";

public MonitoringMigrateAlertsAction() {
super(NAME, MonitoringMigrateAlertsResponse::new);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* 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.core.monitoring.action;

import org.elasticsearch.action.ActionRequestValidationException;
import org.elasticsearch.action.support.master.MasterNodeRequest;
import org.elasticsearch.common.io.stream.StreamInput;

import java.io.IOException;

public class MonitoringMigrateAlertsRequest extends MasterNodeRequest<MonitoringMigrateAlertsRequest> {

public MonitoringMigrateAlertsRequest() {}

public MonitoringMigrateAlertsRequest(StreamInput in) throws IOException {
super(in);
}

@Override
public ActionRequestValidationException validate() {
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
/*
* 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.core.monitoring.action;

import org.elasticsearch.ElasticsearchException;
import org.elasticsearch.action.ActionResponse;
import org.elasticsearch.common.Nullable;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.io.stream.Writeable;
import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.ToXContentObject;
import org.elasticsearch.common.xcontent.XContentBuilder;

import java.io.IOException;
import java.util.List;
import java.util.Objects;

public class MonitoringMigrateAlertsResponse extends ActionResponse implements ToXContentObject {

private final List<ExporterMigrationResult> exporters;

public MonitoringMigrateAlertsResponse(List<ExporterMigrationResult> exporters) {
this.exporters = exporters;
}

public MonitoringMigrateAlertsResponse(StreamInput in) throws IOException {
super(in);
this.exporters = in.readList(ExporterMigrationResult::new);
}

@Override
public void writeTo(StreamOutput out) throws IOException {
out.writeList(exporters);
}

@Override
public XContentBuilder toXContent(XContentBuilder builder, ToXContent.Params params) throws IOException {
return builder.startObject()
.array("exporters", exporters)
.endObject();
}

public List<ExporterMigrationResult> getExporters() {
return exporters;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
MonitoringMigrateAlertsResponse response = (MonitoringMigrateAlertsResponse) o;
return Objects.equals(exporters, response.exporters);
}

@Override
public int hashCode() {
return Objects.hash(exporters);
}

@Override
public String toString() {
return "MonitoringMigrateAlertsResponse{" +
"exporters=" + exporters +
'}';
}

public static class ExporterMigrationResult implements Writeable, ToXContentObject {

private final String name;
private final String type;
private final boolean migrationComplete;
private final Exception reason;

public ExporterMigrationResult(String name, String type, boolean migrationComplete, Exception reason) {
this.name = name;
this.type = type;
this.migrationComplete = migrationComplete;
this.reason = reason;
}

public ExporterMigrationResult(StreamInput in) throws IOException {
this.name = in.readString();
this.type = in.readString();
this.migrationComplete = in.readBoolean();
this.reason = in.readException();
}

@Override
public void writeTo(StreamOutput out) throws IOException {
out.writeString(name);
out.writeString(type);
out.writeBoolean(migrationComplete);
out.writeException(reason);
}

@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject();
{
builder.field("name", name);
builder.field("type", type);
builder.field("migration_complete", migrationComplete);
if (reason != null) {
builder.startObject("reason");
ElasticsearchException.generateThrowableXContent(builder, params, reason);
builder.endObject();
}
}
return builder.endObject();
}

public String getName() {
return name;
}

public String getType() {
return type;
}

public boolean isMigrationComplete() {
return migrationComplete;
}

@Nullable
public Exception getReason() {
return reason;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
ExporterMigrationResult that = (ExporterMigrationResult) o;
return migrationComplete == that.migrationComplete &&
Objects.equals(name, that.name) &&
Objects.equals(type, that.type);
}

@Override
public int hashCode() {
return Objects.hash(name, type, migrationComplete);
}

@Override
public String toString() {
return "ExporterMigrationResult{" +
"name='" + name + '\'' +
", type='" + type + '\'' +
", migrationComplete=" + migrationComplete +
", reason=" + reason +
'}';
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
import org.elasticsearch.xpack.monitoring.exporter.ClusterAlertsUtil;
import org.elasticsearch.xpack.monitoring.exporter.ExportBulk;
import org.elasticsearch.xpack.monitoring.exporter.Exporter;
import org.elasticsearch.xpack.monitoring.exporter.MonitoringMigrationCoordinator;
import org.elasticsearch.xpack.monitoring.test.MonitoringIntegTestCase;
import org.junit.After;
import org.junit.Before;
Expand Down Expand Up @@ -99,6 +100,7 @@ public class HttpExporterIT extends MonitoringIntegTestCase {
private final boolean watcherAlreadyExists = randomBoolean();
private final Environment environment = TestEnvironment.newEnvironment(Settings.builder().put("path.home", createTempDir()).build());
private final String userName = "elasticuser";
private final MonitoringMigrationCoordinator coordinator = new MonitoringMigrationCoordinator();

private MockWebServer webServer;

Expand Down Expand Up @@ -651,7 +653,7 @@ private HttpExporter createHttpExporter(final Settings settings) {
new Exporter.Config("_http", "http", settings, clusterService(), TestUtils.newTestLicenseState());

final Environment env = TestEnvironment.newEnvironment(buildEnvSettings(settings));
return new HttpExporter(config, new SSLService(env), new ThreadContext(settings));
return new HttpExporter(config, new SSLService(env), new ThreadContext(settings), coordinator);
}

private void export(final Settings settings, final Collection<MonitoringDoc> docs) throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,10 @@
import org.elasticsearch.xpack.core.action.XPackUsageFeatureAction;
import org.elasticsearch.xpack.core.monitoring.MonitoringField;
import org.elasticsearch.xpack.core.monitoring.action.MonitoringBulkAction;
import org.elasticsearch.xpack.core.monitoring.action.MonitoringMigrateAlertsAction;
import org.elasticsearch.xpack.core.ssl.SSLService;
import org.elasticsearch.xpack.monitoring.action.TransportMonitoringBulkAction;
import org.elasticsearch.xpack.monitoring.action.TransportMonitoringMigrateAlertsAction;
import org.elasticsearch.xpack.monitoring.cleaner.CleanerService;
import org.elasticsearch.xpack.monitoring.collector.Collector;
import org.elasticsearch.xpack.monitoring.collector.ccr.StatsCollector;
Expand All @@ -50,9 +52,11 @@
import org.elasticsearch.xpack.monitoring.collector.shards.ShardsCollector;
import org.elasticsearch.xpack.monitoring.exporter.Exporter;
import org.elasticsearch.xpack.monitoring.exporter.Exporters;
import org.elasticsearch.xpack.monitoring.exporter.MonitoringMigrationCoordinator;
import org.elasticsearch.xpack.monitoring.exporter.http.HttpExporter;
import org.elasticsearch.xpack.monitoring.exporter.local.LocalExporter;
import org.elasticsearch.xpack.monitoring.rest.action.RestMonitoringBulkAction;
import org.elasticsearch.xpack.monitoring.rest.action.RestMonitoringMigrateAlertsAction;

import java.util.ArrayList;
import java.util.Arrays;
Expand All @@ -65,7 +69,6 @@
import java.util.Set;
import java.util.function.Supplier;

import static java.util.Collections.singletonList;
import static org.elasticsearch.common.settings.Setting.boolSetting;

public class Monitoring extends Plugin implements ActionPlugin, ReloadablePlugin {
Expand Down Expand Up @@ -103,10 +106,12 @@ public Collection<Object> createComponents(Client client, ClusterService cluster
final ClusterSettings clusterSettings = clusterService.getClusterSettings();
final CleanerService cleanerService = new CleanerService(settings, clusterSettings, threadPool, getLicenseState());
final SSLService dynamicSSLService = getSslService().createDynamicSSLService();
final MonitoringMigrationCoordinator migrationCoordinator = new MonitoringMigrationCoordinator();

Map<String, Exporter.Factory> exporterFactories = new HashMap<>();
exporterFactories.put(HttpExporter.TYPE, config -> new HttpExporter(config, dynamicSSLService, threadPool.getThreadContext()));
exporterFactories.put(LocalExporter.TYPE, config -> new LocalExporter(config, client, cleanerService));
exporterFactories.put(HttpExporter.TYPE, config -> new HttpExporter(config, dynamicSSLService, threadPool.getThreadContext(),
migrationCoordinator));
exporterFactories.put(LocalExporter.TYPE, config -> new LocalExporter(config, client, migrationCoordinator, cleanerService));
exporters = new Exporters(settings, exporterFactories, clusterService, getLicenseState(), threadPool.getThreadContext(),
dynamicSSLService);

Expand All @@ -124,7 +129,7 @@ public Collection<Object> createComponents(Client client, ClusterService cluster
final MonitoringService monitoringService = new MonitoringService(settings, clusterService, threadPool, collectors, exporters);

var usageServices = new MonitoringUsageServices(monitoringService, exporters);
return Arrays.asList(monitoringService, exporters, cleanerService, usageServices);
return Arrays.asList(monitoringService, exporters, migrationCoordinator, cleanerService, usageServices);
}

@Override
Expand All @@ -133,6 +138,7 @@ public Collection<Object> createComponents(Client client, ClusterService cluster
var infoAction = new ActionHandler<>(XPackInfoFeatureAction.MONITORING, MonitoringInfoTransportAction.class);
return Arrays.asList(
new ActionHandler<>(MonitoringBulkAction.INSTANCE, TransportMonitoringBulkAction.class),
new ActionHandler<>(MonitoringMigrateAlertsAction.INSTANCE, TransportMonitoringMigrateAlertsAction.class),
usageAction,
infoAction);
}
Expand All @@ -141,15 +147,14 @@ public Collection<Object> createComponents(Client client, ClusterService cluster
public List<RestHandler> getRestHandlers(Settings settings, RestController restController, ClusterSettings clusterSettings,
IndexScopedSettings indexScopedSettings, SettingsFilter settingsFilter, IndexNameExpressionResolver indexNameExpressionResolver,
Supplier<DiscoveryNodes> nodesInCluster) {
return singletonList(new RestMonitoringBulkAction());
return List.of(new RestMonitoringBulkAction(), new RestMonitoringMigrateAlertsAction());
}

@Override
public List<Setting<?>> getSettings() {
List<Setting<?>> settings = new ArrayList<>();
settings.add(MonitoringField.HISTORY_DURATION);
settings.add(CLEAN_WATCHER_HISTORY);
settings.add(MIGRATION_DECOMMISSION_ALERTS);
settings.add(MonitoringService.ENABLED);
settings.add(MonitoringService.ELASTICSEARCH_COLLECTION_ENABLED);
settings.add(MonitoringService.INTERVAL);
Expand All @@ -163,6 +168,7 @@ public List<Setting<?>> getSettings() {
settings.add(NodeStatsCollector.NODE_STATS_TIMEOUT);
settings.add(EnrichStatsCollector.STATS_TIMEOUT);
settings.addAll(Exporters.getSettings());
settings.add(Monitoring.MIGRATION_DECOMMISSION_ALERTS);
return Collections.unmodifiableList(settings);
}

Expand Down
Loading