Skip to content
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

[7.x] Add action to decommission legacy monitoring cluster alerts (#64373) #66309

Merged
merged 2 commits into from
Dec 15, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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) {
final Exporter.Config config =
new Exporter.Config("_http", "http", settings, clusterService(), TestUtils.newTestLicenseState());

return new HttpExporter(config, new SSLService(settings, environment), new ThreadContext(settings));
return new HttpExporter(config, new SSLService(settings, environment), 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.XPackPlugin;
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;

/**
Expand Down Expand Up @@ -127,10 +130,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 @@ -147,27 +152,29 @@ public Collection<Object> createComponents(Client client, ClusterService cluster

final MonitoringService monitoringService = new MonitoringService(settings, clusterService, threadPool, collectors, exporters);

return Arrays.asList(monitoringService, exporters, cleanerService);
return Arrays.asList(monitoringService, exporters, migrationCoordinator, cleanerService);
}

@Override
public List<ActionHandler<? extends ActionRequest, ? extends ActionResponse>> getActions() {
return singletonList(new ActionHandler<>(MonitoringBulkAction.INSTANCE, TransportMonitoringBulkAction.class));
return Arrays.asList(
new ActionHandler<>(MonitoringBulkAction.INSTANCE, TransportMonitoringBulkAction.class),
new ActionHandler<>(MonitoringMigrateAlertsAction.INSTANCE, TransportMonitoringMigrateAlertsAction.class)
);
}

@Override
public List<RestHandler> getRestHandlers(Settings settings, RestController restController, ClusterSettings clusterSettings,
IndexScopedSettings indexScopedSettings, SettingsFilter settingsFilter, IndexNameExpressionResolver indexNameExpressionResolver,
Supplier<DiscoveryNodes> nodesInCluster) {
return singletonList(new RestMonitoringBulkAction());
return Arrays.asList(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 @@ -181,6 +188,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