Cleanup: Removed config service infrastructure

Original commit: elastic/x-pack-elasticsearch@3f99c357c8
This commit is contained in:
Martijn van Groningen 2015-02-23 09:30:10 +01:00
parent 2136210711
commit 59f0883721
13 changed files with 0 additions and 583 deletions

View File

@ -48,7 +48,6 @@ public class AlertsModule extends AbstractModule implements SpawnModules {
bind(AlertsService.class).asEagerSingleton();
bind(AlertsStore.class).asEagerSingleton();
bind(TemplateUtils.class).asEagerSingleton();
bind(ConfigurationService.class).asEagerSingleton();
}

View File

@ -1,15 +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.alerts;
import org.elasticsearch.common.settings.Settings;
/**
* This interface allows a component to register itself for configuration updates
*/
public interface ConfigurableComponentListener {
void receiveConfigurationUpdate(Settings settings);
}

View File

@ -1,85 +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.alerts;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.alerts.support.init.proxy.ClientProxy;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.component.AbstractComponent;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.settings.ImmutableSettings;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.indices.IndexMissingException;
import java.io.IOException;
import java.util.concurrent.CopyOnWriteArrayList;
/**
* Simple service to get settings that are persisted in the a special document in the .alerts index.
* Also notifies known components about setting changes.
*
* The service requires on the fact that the alert service has been started.
*/
public class ConfigurationService extends AbstractComponent {
public static final String CONFIG_TYPE = "config";
public static final String GLOBAL_CONFIG_NAME = "global";
private final ClientProxy client;
private final CopyOnWriteArrayList<ConfigurableComponentListener> registeredComponents;
@Inject
public ConfigurationService(Settings settings, ClientProxy client) {
super(settings);
this.client = client;
registeredComponents = new CopyOnWriteArrayList<>();
}
/**
* This method gets the config
* @return The immutable settings loaded from the index
*/
public Settings getConfig() {
try {
client.admin().indices().prepareRefresh(AlertsStore.ALERT_INDEX).get();
} catch (IndexMissingException ime) {
logger.error("No index [" + AlertsStore.ALERT_INDEX + "] found");
return null;
}
GetResponse response = client.prepareGet(AlertsStore.ALERT_INDEX, CONFIG_TYPE, GLOBAL_CONFIG_NAME).get();
if (response.isExists()) {
return ImmutableSettings.settingsBuilder().loadFromSource(response.getSourceAsString()).build();
} else {
return null;
}
}
/**
* Notify the listeners of a new config
*/
public IndexResponse updateConfig(BytesReference settingsSource) throws IOException {
IndexResponse indexResponse = client.prepareIndex(AlertsStore.ALERT_INDEX, ConfigurationService.CONFIG_TYPE, ConfigurationService.GLOBAL_CONFIG_NAME)
.setSource(settingsSource).get();
Settings settings = ImmutableSettings.settingsBuilder().loadFromSource(settingsSource.toUtf8()).build();
for (ConfigurableComponentListener componentListener : registeredComponents) {
componentListener.receiveConfigurationUpdate(settings);
}
return indexResponse;
}
/**
* Registers an component to receive config updates
*/
public void registerListener(ConfigurableComponentListener configListener) {
if (!registeredComponents.contains(configListener)) {
registeredComponents.add(configListener);
}
}
}

View File

@ -11,10 +11,6 @@ import org.elasticsearch.alerts.transport.actions.ack.AckAlertAction;
import org.elasticsearch.alerts.transport.actions.ack.AckAlertRequest;
import org.elasticsearch.alerts.transport.actions.ack.AckAlertRequestBuilder;
import org.elasticsearch.alerts.transport.actions.ack.AckAlertResponse;
import org.elasticsearch.alerts.transport.actions.config.ConfigAlertAction;
import org.elasticsearch.alerts.transport.actions.config.ConfigAlertRequest;
import org.elasticsearch.alerts.transport.actions.config.ConfigAlertRequestBuilder;
import org.elasticsearch.alerts.transport.actions.config.ConfigAlertResponse;
import org.elasticsearch.alerts.transport.actions.delete.DeleteAlertAction;
import org.elasticsearch.alerts.transport.actions.delete.DeleteAlertRequest;
import org.elasticsearch.alerts.transport.actions.delete.DeleteAlertRequestBuilder;
@ -254,25 +250,4 @@ public class AlertsClient {
public ActionFuture<AlertsServiceResponse> alertService(AlertsServiceRequest request) {
return client.execute(AlertsServiceAction.INSTANCE, request);
}
/**
* Prepare make an alert config request.
*/
public ConfigAlertRequestBuilder prepareAlertConfig() {
return new ConfigAlertRequestBuilder(client);
}
/**
* Perform an config alert request
*/
public void alertConfig(ConfigAlertRequest request, ActionListener<ConfigAlertResponse> listener) {
client.execute(ConfigAlertAction.INSTANCE, request, listener);
}
/**
* Perform an config alert request
*/
public ActionFuture<ConfigAlertResponse> alertConfig(ConfigAlertRequest request) {
return client.execute(ConfigAlertAction.INSTANCE, request);
}
}

View File

@ -25,7 +25,6 @@ public class AlertsRestModule extends AbstractModule implements PreProcessModule
restModule.addRestAction(RestAlertsStatsAction.class);
restModule.addRestAction(RestGetAlertAction.class);
restModule.addRestAction(RestAlertServiceAction.class);
restModule.addRestAction(RestConfigAlertAction.class);
restModule.addRestAction(RestAckAlertAction.class);
}
}

View File

@ -1,63 +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.alerts.rest.action;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.alerts.AlertsStore;
import org.elasticsearch.alerts.ConfigurationService;
import org.elasticsearch.alerts.client.AlertsClient;
import org.elasticsearch.alerts.transport.actions.config.ConfigAlertRequest;
import org.elasticsearch.alerts.transport.actions.config.ConfigAlertResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.rest.*;
import org.elasticsearch.rest.action.support.RestBuilderListener;
import static org.elasticsearch.rest.RestStatus.CREATED;
import static org.elasticsearch.rest.RestStatus.OK;
/**
*/
public class RestConfigAlertAction extends BaseRestHandler {
private final AlertsClient alertsClient;
@Inject
protected RestConfigAlertAction(Settings settings, RestController controller, Client client, AlertsClient alertsClient) {
super(settings, controller, client);
this.alertsClient = alertsClient;
String path = AlertsStore.ALERT_INDEX + "/" + ConfigurationService.CONFIG_TYPE + "/" + ConfigurationService.GLOBAL_CONFIG_NAME;
controller.registerHandler(RestRequest.Method.PUT, path, this);
controller.registerHandler(RestRequest.Method.POST, path, this);
}
@Override
protected void handleRequest(RestRequest request, RestChannel channel, Client client) throws Exception {
ConfigAlertRequest configAlertRequest = new ConfigAlertRequest();
configAlertRequest.setConfigSource(request.content());
configAlertRequest.setConfigSourceUnsafe(request.contentUnsafe());
alertsClient.alertConfig(configAlertRequest, new RestBuilderListener<ConfigAlertResponse>(channel) {
@Override
public RestResponse buildResponse(ConfigAlertResponse response, XContentBuilder builder) throws Exception {
IndexResponse indexResponse = response.indexResponse();
builder.startObject()
.field("_index", indexResponse.getIndex())
.field("_type", indexResponse.getType())
.field("_id", indexResponse.getId())
.field("_version", indexResponse.getVersion())
.field("created", indexResponse.isCreated());
builder.endObject();
RestStatus status = OK;
if (indexResponse.isCreated()) {
status = CREATED;
}
return new BytesRestResponse(status, builder);
}
});
}
}

View File

@ -8,8 +8,6 @@ package org.elasticsearch.alerts.transport;
import org.elasticsearch.action.ActionModule;
import org.elasticsearch.alerts.transport.actions.ack.AckAlertAction;
import org.elasticsearch.alerts.transport.actions.ack.TransportAckAlertAction;
import org.elasticsearch.alerts.transport.actions.config.ConfigAlertAction;
import org.elasticsearch.alerts.transport.actions.config.TransportConfigAlertAction;
import org.elasticsearch.alerts.transport.actions.delete.DeleteAlertAction;
import org.elasticsearch.alerts.transport.actions.delete.TransportDeleteAlertAction;
import org.elasticsearch.alerts.transport.actions.get.GetAlertAction;
@ -39,8 +37,6 @@ public class AlertsTransportModule extends AbstractModule implements PreProcessM
actionModule.registerAction(AlertsStatsAction.INSTANCE, TransportAlertsStatsAction.class);
actionModule.registerAction(AckAlertAction.INSTANCE, TransportAckAlertAction.class);
actionModule.registerAction(AlertsServiceAction.INSTANCE, TransportAlertsServiceAction.class);
actionModule.registerAction(ConfigAlertAction.INSTANCE, TransportConfigAlertAction.class);
actionModule.registerAction(ConfigAlertAction.INSTANCE, TransportConfigAlertAction.class);
}
}

View File

@ -1,33 +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.alerts.transport.actions.config;
import org.elasticsearch.alerts.client.AlertsAction;
import org.elasticsearch.client.Client;
/**
* This action deletes an alert from in memory, the scheduler and the index
*/
public class ConfigAlertAction extends AlertsAction<ConfigAlertRequest, ConfigAlertResponse, ConfigAlertRequestBuilder> {
public static final ConfigAlertAction INSTANCE = new ConfigAlertAction();
public static final String NAME = "indices:data/write/alert/config";
private ConfigAlertAction() {
super(NAME);
}
@Override
public ConfigAlertResponse newResponse() {
return new ConfigAlertResponse();
}
@Override
public ConfigAlertRequestBuilder newRequestBuilder(Client client) {
return new ConfigAlertRequestBuilder(client);
}
}

View File

@ -1,100 +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.alerts.transport.actions.config;
import org.elasticsearch.action.ActionRequestValidationException;
import org.elasticsearch.action.ValidateActions;
import org.elasticsearch.action.support.master.MasterNodeOperationRequest;
import org.elasticsearch.alerts.AlertsStore;
import org.elasticsearch.alerts.ConfigurationService;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import java.io.IOException;
/**
* A delete alert request to delete an alert by name (id)
*/
public class ConfigAlertRequest extends MasterNodeOperationRequest<ConfigAlertRequest> {
private BytesReference configSource;
private boolean configSourceUnsafe;
public ConfigAlertRequest() {
}
/**
* @return The source of the config
*/
public BytesReference getConfigSource() {
return configSource;
}
/**
* Sets the source of the config document
*/
public void setConfigSource(BytesReference configSource) {
this.configSource = configSource;
this.configSourceUnsafe = false;
}
/**
* @return Whether the ByteRef configSource safe
*/
public boolean isConfigSourceUnsafe() {
return configSourceUnsafe;
}
public void setConfigSourceUnsafe(boolean configSourceUnsafe) {
this.configSourceUnsafe = configSourceUnsafe;
}
/**
* Set the source of the config with boolean to control source safety
*/
public void setConfigSource(BytesReference configSource, boolean configSourceUnsafe) {
this.configSource = configSource;
this.configSourceUnsafe = configSourceUnsafe;
}
public void beforeLocalFork() {
if (configSourceUnsafe) {
configSource = configSource.copyBytesArray();
configSourceUnsafe = false;
}
}
@Override
public ActionRequestValidationException validate() {
ActionRequestValidationException validationException = null;
if (configSource == null){
validationException = ValidateActions.addValidationError("configName is missing", validationException);
}
return validationException;
}
@Override
public void readFrom(StreamInput in) throws IOException {
super.readFrom(in);
configSource = in.readBytesReference();
configSourceUnsafe = false;
}
@Override
public void writeTo(StreamOutput out) throws IOException {
super.writeTo(out);
out.writeBytesReference(configSource);
}
@Override
public String toString() {
return "config {[" + AlertsStore.ALERT_INDEX + "][" + ConfigurationService.CONFIG_TYPE + "]}";
}
}

View File

@ -1,45 +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.alerts.transport.actions.config;
import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.support.master.MasterNodeOperationRequestBuilder;
import org.elasticsearch.alerts.client.AlertsClient;
import org.elasticsearch.client.Client;
import org.elasticsearch.common.bytes.BytesReference;
/**
* A alert config action request builder.
*/
public class ConfigAlertRequestBuilder extends MasterNodeOperationRequestBuilder<ConfigAlertRequest, ConfigAlertResponse, ConfigAlertRequestBuilder, Client> {
public ConfigAlertRequestBuilder(Client client) {
super(client, new ConfigAlertRequest());
}
/**
* Sets the source of the config to be modified
*/
public ConfigAlertRequestBuilder setConfigSource(BytesReference configSource) {
this.request().setConfigSource(configSource);
return this;
}
/**
* Sets the source of the config to be modified with boolean to control safety
*/
public ConfigAlertRequestBuilder setConfigSource(BytesReference configSource, boolean sourceUnsafe) {
this.request().setConfigSource(configSource);
this.request().setConfigSourceUnsafe(sourceUnsafe);
return this;
}
@Override
protected void doExecute(final ActionListener<ConfigAlertResponse> listener) {
new AlertsClient(client).alertConfig(request, listener);
}
}

View File

@ -1,50 +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.alerts.transport.actions.config;
import org.elasticsearch.action.ActionResponse;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.common.Nullable;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import java.io.IOException;
/**
*/
public class ConfigAlertResponse extends ActionResponse {
private IndexResponse indexResponse;
public ConfigAlertResponse() {
}
public ConfigAlertResponse(@Nullable IndexResponse indexResponse) {
this.indexResponse = indexResponse;
}
public IndexResponse indexResponse() {
return indexResponse;
}
@Override
public void readFrom(StreamInput in) throws IOException {
super.readFrom(in);
if (in.readBoolean()) {
indexResponse = new IndexResponse();
indexResponse.readFrom(in);
}
}
@Override
public void writeTo(StreamOutput out) throws IOException {
super.writeTo(out);
out.writeBoolean(indexResponse != null);
if (indexResponse != null) {
indexResponse.writeTo(out);
}
}
}

View File

@ -1,70 +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.alerts.transport.actions.config;
import org.elasticsearch.ElasticsearchException;
import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.support.ActionFilters;
import org.elasticsearch.action.support.master.TransportMasterNodeOperationAction;
import org.elasticsearch.alerts.AlertsStore;
import org.elasticsearch.alerts.ConfigurationService;
import org.elasticsearch.cluster.ClusterService;
import org.elasticsearch.cluster.ClusterState;
import org.elasticsearch.cluster.block.ClusterBlockException;
import org.elasticsearch.cluster.block.ClusterBlockLevel;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.threadpool.ThreadPool;
import org.elasticsearch.transport.TransportService;
/**
* Performs the config operation.
*/
public class TransportConfigAlertAction extends TransportMasterNodeOperationAction<ConfigAlertRequest, ConfigAlertResponse> {
private final ConfigurationService configManager;
@Inject
public TransportConfigAlertAction(Settings settings, TransportService transportService, ClusterService clusterService,
ThreadPool threadPool, ActionFilters actionFilters, ConfigurationService configManager) {
super(settings, ConfigAlertAction.NAME, transportService, clusterService, threadPool, actionFilters);
this.configManager = configManager;
}
@Override
protected String executor() {
return ThreadPool.Names.MANAGEMENT;
}
@Override
protected ConfigAlertRequest newRequest() {
return new ConfigAlertRequest();
}
@Override
protected ConfigAlertResponse newResponse() {
return new ConfigAlertResponse();
}
@Override
protected void masterOperation(ConfigAlertRequest request, ClusterState state, ActionListener<ConfigAlertResponse> listener) throws ElasticsearchException {
try {
IndexResponse indexResponse = configManager.updateConfig(request.getConfigSource());
listener.onResponse(new ConfigAlertResponse(indexResponse));
} catch (Exception e) {
listener.onFailure(e);
}
}
@Override
protected ClusterBlockException checkBlock(ConfigAlertRequest request, ClusterState state) {
request.beforeLocalFork(); // This is the best place to make the config source safe
return state.blocks().indexBlockedException(ClusterBlockLevel.WRITE, AlertsStore.ALERT_INDEX);
}
}

View File

@ -1,91 +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.alerts;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.alerts.support.init.proxy.ClientProxy;
import org.elasticsearch.common.settings.ImmutableSettings;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.test.ElasticsearchIntegrationTest;
import static org.hamcrest.core.IsEqual.equalTo;
/**
*/
public class ConfigTest extends ElasticsearchIntegrationTest {
class SettingsListener implements ConfigurableComponentListener {
Settings settings;
@Override
public void receiveConfigurationUpdate(Settings settings) {
this.settings = settings;
}
}
public void testListener() throws Exception {
TimeValue tv = TimeValue.timeValueMillis(10000);
Settings oldSettings = ImmutableSettings.builder()
.put("foo", tv)
.put("bar", 1)
.put("baz", true)
.build();
ConfigurationService configurationService = new ConfigurationService(oldSettings, ClientProxy.of(client()));
SettingsListener settingsListener = new SettingsListener();
configurationService.registerListener(settingsListener);
TimeValue tv2 = TimeValue.timeValueMillis(10);
XContentBuilder jsonSettings = XContentFactory.jsonBuilder();
jsonSettings.startObject();
jsonSettings.field("foo", tv2)
.field("bar", 100)
.field("baz", false);
jsonSettings.endObject();
configurationService.updateConfig(jsonSettings.bytes());
assertThat(settingsListener.settings.getAsTime("foo", new TimeValue(0)).getMillis(), equalTo(tv2.getMillis()));
assertThat(settingsListener.settings.getAsInt("bar", 0), equalTo(100));
assertThat(settingsListener.settings.getAsBoolean("baz", true), equalTo(false));
}
public void testLoadingSettings() throws Exception {
TimeValue tv = TimeValue.timeValueMillis(10000);
Settings oldSettings = ImmutableSettings.builder()
.put("foo", tv)
.put("bar", 1)
.put("baz", true)
.build();
TimeValue tv2 = TimeValue.timeValueMillis(10);
Settings newSettings = ImmutableSettings.builder()
.put("foo", tv2)
.put("bar", 100)
.put("baz", false)
.build();
XContentBuilder jsonBuilder = XContentFactory.jsonBuilder();
jsonBuilder.startObject();
newSettings.toXContent(jsonBuilder, ToXContent.EMPTY_PARAMS);
jsonBuilder.endObject();
IndexResponse indexResponse = client()
.prepareIndex(AlertsStore.ALERT_INDEX, ConfigurationService.CONFIG_TYPE, "global")
.setSource(jsonBuilder)
.get();
assertTrue(indexResponse.isCreated());
ConfigurationService configurationService = new ConfigurationService(oldSettings, ClientProxy.of(client()));
Settings loadedSettings = configurationService.getConfig();
assertThat(loadedSettings.get("foo"), equalTo(newSettings.get("foo")));
assertThat(loadedSettings.get("bar"), equalTo(newSettings.get("bar")));
assertThat(loadedSettings.get("baz"), equalTo(newSettings.get("baz")));
}
}