Refactor and rename transport layer

This commit removes the update action and renames the create action to index.
It also hides the Alert objects behind the interface rather than exposing them.
This means that all interactions should be via BytesReferences.

Original commit: elastic/x-pack-elasticsearch@87ac377d3e
This commit is contained in:
Brian Murphy 2014-11-06 11:08:57 +00:00
parent 0f78028190
commit bce97abc1d
21 changed files with 196 additions and 532 deletions

View File

@ -66,7 +66,7 @@ public class IndexAlertAction implements AlertAction, ToXContent {
try {
XContentBuilder resultBuilder = XContentFactory.jsonBuilder().prettyPrint();
resultBuilder.startObject();
//resultBuilder = alertResult.searchResponse.toXContent(resultBuilder, ToXContent.EMPTY_PARAMS);
resultBuilder = alertResult.getSearchResponse().toXContent(resultBuilder, ToXContent.EMPTY_PARAMS);
resultBuilder.field("timestamp", alertResult.getFireTime());
resultBuilder.endObject();
indexRequest.source(resultBuilder);

View File

@ -8,13 +8,11 @@ package org.elasticsearch.alerts.client;
import org.elasticsearch.action.*;
import org.elasticsearch.action.support.ActionFilters;
import org.elasticsearch.action.support.TransportAction;
import org.elasticsearch.action.update.TransportUpdateAction;
import org.elasticsearch.alerts.Alert;
import org.elasticsearch.alerts.AlertManager;
import org.elasticsearch.alerts.transport.actions.create.*;
import org.elasticsearch.alerts.transport.actions.index.*;
import org.elasticsearch.alerts.transport.actions.delete.*;
import org.elasticsearch.alerts.transport.actions.get.*;
import org.elasticsearch.alerts.transport.actions.update.*;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.support.Headers;
import org.elasticsearch.cluster.ClusterService;
import org.elasticsearch.common.inject.Inject;
@ -36,22 +34,21 @@ public class AlertsClient implements AlertsClientInterface {
Settings settings,
Headers headers,
ActionFilters filters,
TransportService transportService, ClusterService clusterService, AlertManager alertManager) {
TransportService transportService, ClusterService clusterService, AlertManager alertManager,
Client client) {
this.headers = headers;
internalActions = new HashMap<>();
this.threadPool = threadPool;
internalActions.put(CreateAlertAction.INSTANCE, new TransportCreateAlertAction(settings,
CreateAlertAction.NAME, transportService, clusterService, threadPool, filters, alertManager));
internalActions.put(IndexAlertAction.INSTANCE, new TransportIndexAlertAction(settings,
IndexAlertAction.NAME, transportService, clusterService, threadPool, filters, alertManager));
internalActions.put(GetAlertAction.INSTANCE, new TransportGetAlertAction(settings,
GetAlertAction.NAME, transportService, clusterService, threadPool, filters, alertManager));
GetAlertAction.NAME, threadPool, filters, client));
internalActions.put(DeleteAlertAction.INSTANCE, new TransportDeleteAlertAction(settings,
DeleteAlertAction.NAME, transportService, clusterService, threadPool, filters, alertManager));
internalActions.put(UpdateAlertAction.INSTANCE, new TransportUpdateAlertAction(settings,
UpdateAlertAction.NAME, transportService, clusterService, threadPool, filters, alertManager));
}
@ -99,47 +96,25 @@ public class AlertsClient implements AlertsClientInterface {
@Override
public CreateAlertRequestBuilder prepareCreateAlert(Alert alert) {
return new CreateAlertRequestBuilder(this, alert);
public IndexAlertRequestBuilder prepareCreateAlert(String alertName) {
return new IndexAlertRequestBuilder(this, alertName);
}
@Override
public CreateAlertRequestBuilder prepareCreateAlert() {
return new CreateAlertRequestBuilder(this, null);
public IndexAlertRequestBuilder prepareCreateAlert() {
return new IndexAlertRequestBuilder(this, null);
}
@Override
public void createAlert(CreateAlertRequest request, ActionListener<CreateAlertResponse> response) {
execute(CreateAlertAction.INSTANCE, request, response);
public void createAlert(IndexAlertRequest request, ActionListener<IndexAlertResponse> response) {
execute(IndexAlertAction.INSTANCE, request, response);
}
@Override
public ActionFuture<CreateAlertResponse> createAlert(CreateAlertRequest request) {
return execute(CreateAlertAction.INSTANCE, request);
public ActionFuture<IndexAlertResponse> createAlert(IndexAlertRequest request) {
return execute(IndexAlertAction.INSTANCE, request);
}
@Override
public UpdateAlertRequestBuilder prepareUpdateAlert(Alert alert) {
return new UpdateAlertRequestBuilder(this, alert);
}
@Override
public UpdateAlertRequestBuilder prepareUpdateAlert() {
return new UpdateAlertRequestBuilder(this);
}
@Override
public void updateAlert(UpdateAlertRequest request, ActionListener<UpdateAlertResponse> response) {
execute(UpdateAlertAction.INSTANCE, request, response);
}
@Override
public ActionFuture<UpdateAlertResponse> updateAlert(UpdateAlertRequest request) {
return execute(UpdateAlertAction.INSTANCE, request);
}
@SuppressWarnings("unchecked")
@Override
public <Request extends ActionRequest, Response extends ActionResponse, RequestBuilder extends ActionRequestBuilder<Request, Response, RequestBuilder, AlertsClientInterface>> ActionFuture<Response> execute(Action<Request, Response, RequestBuilder, AlertsClientInterface> action, Request request) {

View File

@ -6,23 +6,16 @@
package org.elasticsearch.alerts.client;
import org.elasticsearch.action.*;
import org.elasticsearch.action.admin.indices.template.get.GetIndexTemplatesResponse;
import org.elasticsearch.alerts.Alert;
import org.elasticsearch.alerts.transport.actions.create.CreateAlertRequest;
import org.elasticsearch.alerts.transport.actions.create.CreateAlertRequestBuilder;
import org.elasticsearch.alerts.transport.actions.create.CreateAlertResponse;
import org.elasticsearch.alerts.transport.actions.index.IndexAlertRequest;
import org.elasticsearch.alerts.transport.actions.index.IndexAlertRequestBuilder;
import org.elasticsearch.alerts.transport.actions.index.IndexAlertResponse;
import org.elasticsearch.alerts.transport.actions.delete.DeleteAlertRequest;
import org.elasticsearch.alerts.transport.actions.delete.DeleteAlertRequestBuilder;
import org.elasticsearch.alerts.transport.actions.delete.DeleteAlertResponse;
import org.elasticsearch.alerts.transport.actions.get.GetAlertRequest;
import org.elasticsearch.alerts.transport.actions.get.GetAlertRequestBuilder;
import org.elasticsearch.alerts.transport.actions.get.GetAlertResponse;
import org.elasticsearch.alerts.transport.actions.update.UpdateAlertRequest;
import org.elasticsearch.alerts.transport.actions.update.UpdateAlertRequestBuilder;
import org.elasticsearch.alerts.transport.actions.update.UpdateAlertResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.ElasticsearchClient;
import org.elasticsearch.common.lease.Releasable;
/**
*/
@ -46,24 +39,13 @@ public interface AlertsClientInterface extends ElasticsearchClient<AlertsClientI
ActionFuture<DeleteAlertResponse> deleteAlert(DeleteAlertRequest request);
CreateAlertRequestBuilder prepareCreateAlert(Alert alert);
IndexAlertRequestBuilder prepareCreateAlert(String alertName);
CreateAlertRequestBuilder prepareCreateAlert();
public void createAlert(CreateAlertRequest request, ActionListener<CreateAlertResponse> response);
ActionFuture<CreateAlertResponse> createAlert(CreateAlertRequest request);
UpdateAlertRequestBuilder prepareUpdateAlert(Alert alert);
UpdateAlertRequestBuilder prepareUpdateAlert();
public void updateAlert(UpdateAlertRequest request, ActionListener<UpdateAlertResponse> response);
ActionFuture<UpdateAlertResponse> updateAlert(UpdateAlertRequest request);
IndexAlertRequestBuilder prepareCreateAlert();
public void createAlert(IndexAlertRequest request, ActionListener<IndexAlertResponse> response);
ActionFuture<IndexAlertResponse> createAlert(IndexAlertRequest request);
}

View File

@ -1,35 +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.create;
import org.elasticsearch.action.ClientAction;
import org.elasticsearch.alerts.client.AlertsClient;
import org.elasticsearch.alerts.client.AlertsClientAction;
import org.elasticsearch.alerts.client.AlertsClientInterface;
import org.elasticsearch.client.Client;
/**
*/
public class CreateAlertAction extends AlertsClientAction<CreateAlertRequest, CreateAlertResponse, CreateAlertRequestBuilder> {
public static final CreateAlertAction INSTANCE = new CreateAlertAction();
public static final String NAME = "indices:data/write/alert/create";
private CreateAlertAction() {
super(NAME);
}
@Override
public CreateAlertRequestBuilder newRequestBuilder(AlertsClientInterface client) {
return new CreateAlertRequestBuilder(client);
}
@Override
public CreateAlertResponse newResponse() {
return new CreateAlertResponse();
}
}

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.create;
import org.elasticsearch.action.ActionRequestValidationException;
import org.elasticsearch.action.ValidateActions;
import org.elasticsearch.action.support.master.MasterNodeOperationRequest;
import org.elasticsearch.alerts.Alert;
import org.elasticsearch.alerts.triggers.AlertTrigger;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.joda.time.DateTime;
import org.elasticsearch.common.lucene.uid.Versions;
import org.elasticsearch.common.unit.TimeValue;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
/**
*/
public class CreateAlertRequest extends MasterNodeOperationRequest<CreateAlertRequest> {
private Alert alert;
@Override
public ActionRequestValidationException validate() {
ActionRequestValidationException validationException = null;
if (alert == null) {
validationException = ValidateActions.addValidationError("alert is missing", validationException);
}
return validationException;
}
CreateAlertRequest() {
}
public CreateAlertRequest(Alert alert) {
this.alert = alert;
}
@Override
public void readFrom(StreamInput in) throws IOException {
super.readFrom(in);
alert = new Alert();
alert.readFrom(in);
}
@Override
public void writeTo(StreamOutput out) throws IOException {
super.writeTo(out);
alert.writeTo(out);
}
public Alert alert() {
return alert;
}
public void Alert(Alert alert) {
this.alert = alert;
}
}

View File

@ -1,37 +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.create;
import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.ActionRequestBuilder;
import org.elasticsearch.action.admin.indices.create.CreateIndexResponse;
import org.elasticsearch.action.support.master.MasterNodeOperationRequestBuilder;
import org.elasticsearch.alerts.Alert;
import org.elasticsearch.alerts.client.AlertsClient;
import org.elasticsearch.alerts.client.AlertsClientInterface;
import org.elasticsearch.client.Client;
/**
*/
public class CreateAlertRequestBuilder
extends MasterNodeOperationRequestBuilder<CreateAlertRequest, CreateAlertResponse,
CreateAlertRequestBuilder, AlertsClientInterface> {
public CreateAlertRequestBuilder(AlertsClientInterface client) {
super(client, new CreateAlertRequest(null));
}
public CreateAlertRequestBuilder(AlertsClientInterface client, Alert alert) {
super(client, new CreateAlertRequest(alert));
}
@Override
protected void doExecute(ActionListener<CreateAlertResponse> listener) {
client.createAlert(request, listener);
}
}

View File

@ -1,80 +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.create;
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.HandledTransportAction;
import org.elasticsearch.action.support.master.TransportMasterNodeOperationAction;
import org.elasticsearch.alerts.Alert;
import org.elasticsearch.alerts.AlertManager;
import org.elasticsearch.alerts.AlertsStore;
import org.elasticsearch.alerts.actions.AlertAction;
import org.elasticsearch.alerts.actions.AlertActionManager;
import org.elasticsearch.alerts.triggers.TriggerManager;
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;
import org.quartz.Trigger;
import java.util.ArrayList;
/**
*/
public class TransportCreateAlertAction extends TransportMasterNodeOperationAction<CreateAlertRequest, CreateAlertResponse> {
private final AlertManager alertManager;
@Inject
public TransportCreateAlertAction(Settings settings, String actionName, TransportService transportService,
ClusterService clusterService, ThreadPool threadPool, ActionFilters actionFilters,
AlertManager alertManager) {
super(settings, actionName, transportService, clusterService, threadPool, actionFilters);
this.alertManager = alertManager;
}
@Override
protected String executor() {
return ThreadPool.Names.MANAGEMENT;
}
@Override
protected CreateAlertRequest newRequest() {
return new CreateAlertRequest();
}
@Override
protected CreateAlertResponse newResponse() {
return new CreateAlertResponse();
}
@Override
protected void masterOperation(CreateAlertRequest request, ClusterState state, ActionListener<CreateAlertResponse> listener) throws ElasticsearchException {
try {
IndexResponse indexResponse = alertManager.addAlert(request.alert());
listener.onResponse(new CreateAlertResponse(indexResponse));
} catch (Exception e) {
listener.onFailure(e);
}
}
@Override
protected ClusterBlockException checkBlock(CreateAlertRequest request, ClusterState state) {
if (!alertManager.isStarted()) {
return new ClusterBlockException(null);
}
return state.blocks().indicesBlockedException(ClusterBlockLevel.WRITE, new String[]{AlertsStore.ALERT_INDEX, AlertActionManager.ALERT_HISTORY_INDEX});
}
}

View File

@ -22,7 +22,7 @@ import java.io.IOException;
/**
*/
public class GetAlertRequest extends MasterNodeOperationRequest<GetAlertRequest> implements IndicesRequest {
public class GetAlertRequest extends ActionRequest<GetAlertRequest> implements IndicesRequest {
private String alertName;
private long version = Versions.MATCH_ANY;

View File

@ -17,7 +17,7 @@ import org.elasticsearch.index.VersionType;
* A delete document action request builder.
*/
public class GetAlertRequestBuilder
extends MasterNodeOperationRequestBuilder<GetAlertRequest, GetAlertResponse, GetAlertRequestBuilder, AlertsClientInterface> {
extends ActionRequestBuilder<GetAlertRequest, GetAlertResponse, GetAlertRequestBuilder, AlertsClientInterface> {
public GetAlertRequestBuilder(AlertsClientInterface client, String alertName) {

View File

@ -6,6 +6,7 @@
package org.elasticsearch.alerts.transport.actions.get;
import org.elasticsearch.action.ActionResponse;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.alerts.Alert;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
@ -16,44 +17,38 @@ import java.io.IOException;
*/
public class GetAlertResponse extends ActionResponse {
private boolean found = false;
private Alert alert = null;
private GetResponse getResponse;
public GetAlertResponse() {
}
public boolean found() {
return this.found;
public GetAlertResponse(GetResponse getResponse) {
this.getResponse = getResponse;
}
public void found(boolean found) {
this.found = found;
public void getResponse(GetResponse getResponse) {
this.getResponse = getResponse;
}
public Alert alert() {
return alert;
}
public void alert(Alert alert){
this.alert = alert;
public GetResponse getResponse() {
return this.getResponse;
}
@Override
public void readFrom(StreamInput in) throws IOException {
super.readFrom(in);
found = in.readBoolean();
if (found) {
alert = new Alert();
alert.readFrom(in);
if (in.readBoolean()) {
getResponse = GetResponse.readGetResponse(in);
}
}
@Override
public void writeTo(StreamOutput out) throws IOException {
super.writeTo(out);
out.writeBoolean(found);
if (found && alert != null){
alert.writeTo(out);
out.writeBoolean(getResponse != null);
if (getResponse != null) {
getResponse.writeTo(out);
}
}
}

View File

@ -7,7 +7,10 @@ package org.elasticsearch.alerts.transport.actions.get;
import org.elasticsearch.ElasticsearchException;
import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.support.ActionFilters;
import org.elasticsearch.action.support.TransportAction;
import org.elasticsearch.action.support.master.TransportMasterNodeOperationAction;
import org.elasticsearch.alerts.Alert;
import org.elasticsearch.alerts.AlertManager;
@ -15,6 +18,7 @@ import org.elasticsearch.alerts.AlertsStore;
import org.elasticsearch.alerts.actions.AlertActionManager;
import org.elasticsearch.alerts.transport.actions.delete.DeleteAlertRequest;
import org.elasticsearch.alerts.transport.actions.delete.DeleteAlertResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.cluster.ClusterService;
import org.elasticsearch.cluster.ClusterState;
import org.elasticsearch.cluster.block.ClusterBlockException;
@ -27,53 +31,27 @@ import org.elasticsearch.transport.TransportService;
/**
* Performs the delete operation.
*/
public class TransportGetAlertAction extends TransportMasterNodeOperationAction<GetAlertRequest, GetAlertResponse> {
public class TransportGetAlertAction extends TransportAction<GetAlertRequest, GetAlertResponse> {
private final AlertManager alertManager;
private final Client client;
@Inject
public TransportGetAlertAction(Settings settings, String actionName, TransportService transportService,
ClusterService clusterService, ThreadPool threadPool, ActionFilters actionFilters,
AlertManager alertManager) {
super(settings, actionName, transportService, clusterService, threadPool, actionFilters);
this.alertManager = alertManager;
public TransportGetAlertAction(Settings settings, String actionName, ThreadPool threadPool,
ActionFilters actionFilters, Client client) {
super(settings, actionName, threadPool, actionFilters);
this.client = client;
}
@Override
protected String executor() {
return ThreadPool.Names.MANAGEMENT;
}
@Override
protected GetAlertRequest newRequest() {
return new GetAlertRequest();
}
@Override
protected GetAlertResponse newResponse() {
return new GetAlertResponse();
}
@Override
protected void masterOperation(GetAlertRequest request, ClusterState state, ActionListener<GetAlertResponse> listener) throws ElasticsearchException {
protected void doExecute(GetAlertRequest request, ActionListener<GetAlertResponse> listener) {
try {
Alert alert = alertManager.getAlert(request.alertName());
GetAlertResponse response = new GetAlertResponse();
response.found(alert != null);
response.alert(alert);
GetResponse getResponse = client.prepareGet(AlertsStore.ALERT_INDEX, AlertsStore.ALERT_TYPE, request.alertName())
.setVersion(request.version())
.setVersionType(request.versionType()).execute().actionGet();
GetAlertResponse response = new GetAlertResponse(getResponse);
listener.onResponse(response);
} catch (Exception e) {
listener.onFailure(e);
}
}
@Override
protected ClusterBlockException checkBlock(GetAlertRequest request, ClusterState state) {
if (!alertManager.isStarted()) {
return new ClusterBlockException(null);
}
return state.blocks().indicesBlockedException(ClusterBlockLevel.WRITE, new String[]{AlertsStore.ALERT_INDEX, AlertActionManager.ALERT_HISTORY_INDEX});
}
}

View File

@ -0,0 +1,32 @@
/*
* 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.index;
import org.elasticsearch.alerts.client.AlertsClientAction;
import org.elasticsearch.alerts.client.AlertsClientInterface;
/**
*/
public class IndexAlertAction extends AlertsClientAction<IndexAlertRequest, IndexAlertResponse, IndexAlertRequestBuilder> {
public static final IndexAlertAction INSTANCE = new IndexAlertAction();
public static final String NAME = "indices:data/write/alert/create";
private IndexAlertAction() {
super(NAME);
}
@Override
public IndexAlertRequestBuilder newRequestBuilder(AlertsClientInterface client) {
return new IndexAlertRequestBuilder(client);
}
@Override
public IndexAlertResponse newResponse() {
return new IndexAlertResponse();
}
}

View File

@ -3,12 +3,13 @@
* 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.update;
package org.elasticsearch.alerts.transport.actions.index;
import org.elasticsearch.action.ActionRequestValidationException;
import org.elasticsearch.action.ValidateActions;
import org.elasticsearch.action.support.master.MasterNodeOperationRequest;
import org.elasticsearch.alerts.Alert;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
@ -16,49 +17,58 @@ import java.io.IOException;
/**
*/
public class UpdateAlertRequest extends MasterNodeOperationRequest<UpdateAlertRequest> {
private Alert alert;
public class IndexAlertRequest extends MasterNodeOperationRequest<IndexAlertRequest> {
private BytesReference alertSource;
private String alertName;
@Override
public ActionRequestValidationException validate() {
ActionRequestValidationException validationException = null;
if (alert == null) {
validationException = ValidateActions.addValidationError("alert is missing", validationException);
if (alertName == null) {
validationException = ValidateActions.addValidationError("alertName is missing", validationException);
}
if (alertSource == null) {
validationException = ValidateActions.addValidationError("alertSource is missing", validationException);
}
return validationException;
}
UpdateAlertRequest() {
IndexAlertRequest() {
}
public UpdateAlertRequest(Alert alert) {
this.alert = alert;
public IndexAlertRequest(BytesReference alertSource) {
this.alertSource = alertSource;
}
@Override
public void readFrom(StreamInput in) throws IOException {
super.readFrom(in);
alert = new Alert();
alert.readFrom(in);
alertSource = in.readBytesReference();
}
@Override
public void writeTo(StreamOutput out) throws IOException {
super.writeTo(out);
alert.writeTo(out);
alertSource.writeTo(out);
}
public Alert alert() {
return alert;
public String alertName() {
return alertName;
}
public void Alert(Alert alert) {
this.alert = alert;
public void alertName(String alertName) {
this.alertName = alertName;
}
public BytesReference alertSource() {
return alertSource;
}
public void alertSource(BytesReference alertSource) {
this.alertSource = alertSource;
}
}

View File

@ -0,0 +1,45 @@
/*
* 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.index;
import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.support.master.MasterNodeOperationRequestBuilder;
import org.elasticsearch.alerts.client.AlertsClientInterface;
import org.elasticsearch.common.bytes.BytesReference;
/**
*/
public class IndexAlertRequestBuilder
extends MasterNodeOperationRequestBuilder<IndexAlertRequest, IndexAlertResponse,
IndexAlertRequestBuilder, AlertsClientInterface> {
public IndexAlertRequestBuilder(AlertsClientInterface client) {
super(client, new IndexAlertRequest());
}
public IndexAlertRequestBuilder(AlertsClientInterface client, String alertName) {
super(client, new IndexAlertRequest());
request.alertName(alertName);
}
public IndexAlertRequestBuilder setAlertName(String alertName){
request.alertName(alertName);
return this;
}
public IndexAlertRequestBuilder setAlertSource(BytesReference alertSource) {
request.alertSource(alertSource);
return this;
}
@Override
protected void doExecute(ActionListener<IndexAlertResponse> listener) {
client.createAlert(request, listener);
}
}

View File

@ -3,7 +3,7 @@
* 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.create;
package org.elasticsearch.alerts.transport.actions.index;
import org.elasticsearch.action.ActionResponse;
import org.elasticsearch.action.index.IndexResponse;
@ -14,14 +14,14 @@ import java.io.IOException;
/**
*/
public class CreateAlertResponse extends ActionResponse {
public class IndexAlertResponse extends ActionResponse {
private IndexResponse indexResponse;
public CreateAlertResponse(IndexResponse indexResponse) {
public IndexAlertResponse(IndexResponse indexResponse) {
this.indexResponse = indexResponse;
}
public CreateAlertResponse() {
public IndexAlertResponse() {
indexResponse = null;
}

View File

@ -3,7 +3,7 @@
* 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.update;
package org.elasticsearch.alerts.transport.actions.index;
import org.elasticsearch.ElasticsearchException;
import org.elasticsearch.action.ActionListener;
@ -17,19 +17,21 @@ 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;
/**
*/
public class TransportUpdateAlertAction extends TransportMasterNodeOperationAction<UpdateAlertRequest, UpdateAlertResponse> {
public class TransportIndexAlertAction extends TransportMasterNodeOperationAction<IndexAlertRequest, IndexAlertResponse> {
private final AlertManager alertManager;
public TransportUpdateAlertAction(Settings settings, String actionName, TransportService transportService,
ClusterService clusterService, ThreadPool threadPool, ActionFilters actionFilters,
AlertManager alertManager) {
@Inject
public TransportIndexAlertAction(Settings settings, String actionName, TransportService transportService,
ClusterService clusterService, ThreadPool threadPool, ActionFilters actionFilters,
AlertManager alertManager) {
super(settings, actionName, transportService, clusterService, threadPool, actionFilters);
this.alertManager = alertManager;
}
@ -40,31 +42,32 @@ public class TransportUpdateAlertAction extends TransportMasterNodeOperationActi
}
@Override
protected UpdateAlertRequest newRequest() {
return new UpdateAlertRequest();
protected IndexAlertRequest newRequest() {
return new IndexAlertRequest();
}
@Override
protected UpdateAlertResponse newResponse() {
return new UpdateAlertResponse();
protected IndexAlertResponse newResponse() {
return new IndexAlertResponse();
}
@Override
protected void masterOperation(UpdateAlertRequest request, ClusterState state, ActionListener<UpdateAlertResponse> listener) throws ElasticsearchException {
protected void masterOperation(IndexAlertRequest request, ClusterState state, ActionListener<IndexAlertResponse> listener) throws ElasticsearchException {
try {
IndexResponse indexResponse = alertManager.updateAlert(request.alert(), true);
listener.onResponse(new UpdateAlertResponse(indexResponse));
IndexResponse indexResponse = alertManager.addAlert(request.alertName(), request.alertSource());
listener.onResponse(new IndexAlertResponse(indexResponse));
} catch (Exception e) {
listener.onFailure(e);
}
}
@Override
protected ClusterBlockException checkBlock(UpdateAlertRequest request, ClusterState state) {
protected ClusterBlockException checkBlock(IndexAlertRequest request, ClusterState state) {
if (!alertManager.isStarted()) {
return new ClusterBlockException(null);
}
return state.blocks().indicesBlockedException(ClusterBlockLevel.WRITE, new String[]{AlertsStore.ALERT_INDEX, AlertActionManager.ALERT_HISTORY_INDEX});
}
}

View File

@ -1,35 +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.update;
import org.elasticsearch.action.ClientAction;
import org.elasticsearch.alerts.client.AlertsClientAction;
import org.elasticsearch.alerts.client.AlertsClientInterface;
import org.elasticsearch.client.Client;
/**
*/
public class UpdateAlertAction extends AlertsClientAction<UpdateAlertRequest, UpdateAlertResponse, UpdateAlertRequestBuilder> {
public static final UpdateAlertAction INSTANCE = new UpdateAlertAction();
public static final String NAME = "indices:data/write/alert/update";
private UpdateAlertAction() {
super(NAME);
}
@Override
public UpdateAlertRequestBuilder newRequestBuilder(AlertsClientInterface client) {
return new UpdateAlertRequestBuilder(client);
}
@Override
public UpdateAlertResponse newResponse() {
return new UpdateAlertResponse();
}
}

View File

@ -1,34 +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.update;
import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.ActionRequestBuilder;
import org.elasticsearch.action.support.master.MasterNodeOperationRequestBuilder;
import org.elasticsearch.alerts.Alert;
import org.elasticsearch.alerts.client.AlertsClient;
import org.elasticsearch.alerts.client.AlertsClientInterface;
import org.elasticsearch.client.Client;
/**
*/
public class UpdateAlertRequestBuilder extends MasterNodeOperationRequestBuilder
<UpdateAlertRequest, UpdateAlertResponse, UpdateAlertRequestBuilder, AlertsClientInterface> {
public UpdateAlertRequestBuilder(AlertsClientInterface client) {
super(client, new UpdateAlertRequest());
}
public UpdateAlertRequestBuilder(AlertsClientInterface client, Alert alert) {
super(client, new UpdateAlertRequest(alert));
}
@Override
protected void doExecute(ActionListener<UpdateAlertResponse> listener) {
client.updateAlert(request, listener);
}
}

View File

@ -1,56 +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.update;
import org.elasticsearch.action.ActionResponse;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import java.io.IOException;
/**
*/
public class UpdateAlertResponse extends ActionResponse {
private IndexResponse indexResponse;
public UpdateAlertResponse() {
indexResponse = null;
}
public UpdateAlertResponse(IndexResponse indexResponse) {
this.indexResponse = indexResponse;
}
public IndexResponse updateResponse() {
return indexResponse;
}
public void indexResponse(IndexResponse indexResponse) {
this.indexResponse = 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

@ -10,8 +10,8 @@ import org.elasticsearch.alerts.actions.*;
import org.elasticsearch.alerts.client.AlertsClient;
import org.elasticsearch.alerts.client.AlertsClientInterface;
import org.elasticsearch.alerts.plugin.AlertsPlugin;
import org.elasticsearch.alerts.transport.actions.create.CreateAlertRequest;
import org.elasticsearch.alerts.transport.actions.create.CreateAlertResponse;
import org.elasticsearch.alerts.transport.actions.index.IndexAlertRequest;
import org.elasticsearch.alerts.transport.actions.index.IndexAlertResponse;
import org.elasticsearch.alerts.transport.actions.delete.DeleteAlertRequest;
import org.elasticsearch.alerts.transport.actions.delete.DeleteAlertResponse;
import org.elasticsearch.alerts.triggers.AlertTrigger;
@ -20,7 +20,9 @@ import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.settings.ImmutableSettings;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.script.ScriptService;
@ -124,10 +126,11 @@ public class BasicAlertingTest extends ElasticsearchIntegrationTest {
);
AlertsClientInterface alertsClient = internalCluster().getInstance(AlertsClient.class, internalCluster().getMasterName());
XContentBuilder jsonBuilder = XContentFactory.jsonBuilder();
alert.toXContent(jsonBuilder, ToXContent.EMPTY_PARAMS);
//alertManager.addAlert("my-first-alert", jsonBuilder().value(alert).bytes());
CreateAlertRequest alertRequest = new CreateAlertRequest(alert);
CreateAlertResponse alertsResponse = alertsClient.createAlert(alertRequest).actionGet();
IndexAlertRequest alertRequest = alertsClient.prepareCreateAlert().setAlertName("my-first-alert").setAlertSource(jsonBuilder.bytes()).request();
IndexAlertResponse alertsResponse = alertsClient.createAlert(alertRequest).actionGet();
assertNotNull(alertsResponse.indexResponse());
assertTrue(alertsResponse.indexResponse().isCreated());

View File

@ -15,14 +15,12 @@ import org.elasticsearch.alerts.AlertsStore;
import org.elasticsearch.alerts.client.AlertsClient;
import org.elasticsearch.alerts.client.AlertsClientInterface;
import org.elasticsearch.alerts.plugin.AlertsPlugin;
import org.elasticsearch.alerts.transport.actions.create.CreateAlertRequest;
import org.elasticsearch.alerts.transport.actions.create.CreateAlertResponse;
import org.elasticsearch.alerts.transport.actions.index.IndexAlertRequest;
import org.elasticsearch.alerts.transport.actions.index.IndexAlertResponse;
import org.elasticsearch.alerts.transport.actions.delete.DeleteAlertRequest;
import org.elasticsearch.alerts.transport.actions.delete.DeleteAlertResponse;
import org.elasticsearch.alerts.transport.actions.get.GetAlertRequest;
import org.elasticsearch.alerts.transport.actions.get.GetAlertResponse;
import org.elasticsearch.alerts.transport.actions.update.UpdateAlertRequest;
import org.elasticsearch.alerts.transport.actions.update.UpdateAlertResponse;
import org.elasticsearch.alerts.triggers.AlertTrigger;
import org.elasticsearch.alerts.triggers.ScriptedAlertTrigger;
import org.elasticsearch.common.io.stream.BytesStreamOutput;
@ -31,8 +29,7 @@ import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.joda.FormatDateTimeFormatter;
import org.elasticsearch.common.joda.time.DateTime;
import org.elasticsearch.common.joda.time.DateTimeZone;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.common.xcontent.*;
import org.elasticsearch.index.mapper.core.DateFieldMapper;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.script.ScriptService;
@ -41,13 +38,11 @@ import org.elasticsearch.search.internal.InternalSearchHits;
import org.elasticsearch.search.internal.InternalSearchResponse;
import org.elasticsearch.common.settings.ImmutableSettings;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.test.ElasticsearchIntegrationTest;
import org.junit.Test;
import java.io.IOException;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import static org.hamcrest.core.Is.is;
import java.util.ArrayList;
@ -211,25 +206,20 @@ public class AlertActionsTest extends ElasticsearchIntegrationTest {
true
);
XContentBuilder jsonBuilder = XContentFactory.jsonBuilder();
alert.toXContent(jsonBuilder, ToXContent.EMPTY_PARAMS);
AlertsClientInterface alertsClient = internalCluster().getInstance(AlertsClient.class, internalCluster().getMasterName());
CreateAlertRequest alertRequest = new CreateAlertRequest(alert);
CreateAlertResponse alertsResponse = alertsClient.createAlert(alertRequest).actionGet();
IndexAlertRequest alertRequest = alertsClient.prepareCreateAlert().setAlertName("my-first-alert").setAlertSource(jsonBuilder.bytes()).request();
IndexAlertResponse alertsResponse = alertsClient.createAlert(alertRequest).actionGet();
assertNotNull(alertsResponse.indexResponse());
assertTrue(alertsResponse.indexResponse().isCreated());
GetAlertRequest getAlertRequest = new GetAlertRequest(alert.alertName());
GetAlertResponse getAlertResponse = alertsClient.getAlert(getAlertRequest).actionGet();
assertTrue(getAlertResponse.found());
assertEquals(alert.schedule(), getAlertResponse.alert().schedule());
String schedule = "0/10 * * * * ? *";
alert.schedule(schedule);
UpdateAlertRequest updateAlertRequest = new UpdateAlertRequest(alert);
UpdateAlertResponse updateAlertResponse = alertsClient.updateAlert(updateAlertRequest).actionGet();
assertNotNull(updateAlertResponse.updateResponse());
assertFalse(updateAlertResponse.updateResponse().isCreated());
assertTrue(getAlertResponse.getResponse().isExists());
assertEquals(getAlertResponse.getResponse().getSourceAsMap().get("schedule").toString(), "0/5 * * * * ? *");
DeleteAlertRequest deleteAlertRequest = new DeleteAlertRequest(alert.alertName());
DeleteAlertResponse deleteAlertResponse = alertsClient.deleteAlert(deleteAlertRequest).actionGet();
@ -237,10 +227,8 @@ public class AlertActionsTest extends ElasticsearchIntegrationTest {
assertTrue(deleteAlertResponse.deleteResponse().isFound());
getAlertResponse = alertsClient.getAlert(getAlertRequest).actionGet();
assertFalse(getAlertResponse.found());
assertFalse(getAlertResponse.getResponse().isExists());
updateAlertResponse = alertsClient.updateAlert(updateAlertRequest).actionGet();
assertNull(updateAlertResponse.updateResponse());
}
}