Skip to content

Commit

Permalink
Added action.destructive_requires_name that controls whether wildca…
Browse files Browse the repository at this point in the history
…rd expressions and `_all` is allowed to be used for destructive operat Also the delete index api requires always an index to be specified (either concrete index, alias or wildcard expression)

Closes #4549 #4481
  • Loading branch information
martijnvg committed Jan 9, 2014
1 parent 7042a9a commit eb63bb2
Show file tree
Hide file tree
Showing 18 changed files with 397 additions and 124 deletions.
12 changes: 7 additions & 5 deletions docs/reference/indices/delete-index.asciidoc
Expand Up @@ -8,10 +8,12 @@ The delete index API allows to delete an existing index.
$ curl -XDELETE 'http://localhost:9200/twitter/'
--------------------------------------------------

The above example deletes an index called `twitter`.
The above example deletes an index called `twitter`. Specifying an index,
alias or wildcard expression is required.

The delete index API can also be applied to more than one index, or on
`_all` indices (be careful!). All indices will also be deleted when no
specific index is provided. In order to disable allowing to delete all
indices, set `action.disable_delete_all_indices` setting in the config
to `true`.
all indices (be careful!) by using `_all` or `*` as index.

In order to disable allowing to delete indices via wildcards or `_all`,
set `action.destructive_requires_name` setting in the config to `true`.
This setting can also be changed via the cluster update settings api.
5 changes: 3 additions & 2 deletions docs/reference/indices/open-close.asciidoc
Expand Up @@ -23,6 +23,7 @@ disabled using the `ignore_unavailable=true` parameter.

All indices can be opened or closed at once using `_all` as the index name
or specifying patterns that identify them all (e.g. `*`).
Closing all indices can be disabled by setting the `action.disable_close_all_indices`
flag in the config file to `true`.

Identifying indices via wildcards or `_all` can be disabled by setting the
`action.destructive_requires_name` flag in the config file to `true`.
This setting can also be changed via the cluster update settings api.
2 changes: 1 addition & 1 deletion rest-api-spec/api/indices.delete.json
Expand Up @@ -8,7 +8,7 @@
"parts": {
"index": {
"type" : "list",
"description" : "A comma-separated list of indices to delete; use `_all` or empty string to delete all indices"
"description" : "A comma-separated list of indices to delete; use `_all` or `*` string to delete all indices"
}
},
"params": {
Expand Down
Expand Up @@ -20,8 +20,8 @@
package org.elasticsearch.action.admin.indices.close;

import org.elasticsearch.ElasticsearchException;
import org.elasticsearch.ElasticsearchIllegalArgumentException;
import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.support.DestructiveOperations;
import org.elasticsearch.action.support.master.TransportMasterNodeOperationAction;
import org.elasticsearch.cluster.ClusterService;
import org.elasticsearch.cluster.ClusterState;
Expand All @@ -32,6 +32,7 @@
import org.elasticsearch.cluster.metadata.MetaDataIndexStateService;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.node.settings.NodeSettingsService;
import org.elasticsearch.threadpool.ThreadPool;
import org.elasticsearch.transport.TransportService;

Expand All @@ -41,15 +42,14 @@
public class TransportCloseIndexAction extends TransportMasterNodeOperationAction<CloseIndexRequest, CloseIndexResponse> {

private final MetaDataIndexStateService indexStateService;
private final boolean disableCloseAllIndices;

private final DestructiveOperations destructiveOperations;

@Inject
public TransportCloseIndexAction(Settings settings, TransportService transportService, ClusterService clusterService,
ThreadPool threadPool, MetaDataIndexStateService indexStateService) {
ThreadPool threadPool, MetaDataIndexStateService indexStateService, NodeSettingsService nodeSettingsService) {
super(settings, transportService, clusterService, threadPool);
this.indexStateService = indexStateService;
this.disableCloseAllIndices = settings.getAsBoolean("action.disable_close_all_indices", false);
this.destructiveOperations = new DestructiveOperations(logger, settings, nodeSettingsService);
}

@Override
Expand All @@ -75,17 +75,7 @@ protected CloseIndexResponse newResponse() {

@Override
protected void doExecute(CloseIndexRequest request, ActionListener<CloseIndexResponse> listener) {
ClusterState state = clusterService.state();
String[] indicesOrAliases = request.indices();
request.indices(state.metaData().concreteIndices(indicesOrAliases, request.indicesOptions()));

if (disableCloseAllIndices) {
if (state.metaData().isExplicitAllIndices(indicesOrAliases) ||
state.metaData().isPatternMatchingAllIndices(indicesOrAliases, request.indices())) {
throw new ElasticsearchIllegalArgumentException("closing all indices is disabled");
}
}

destructiveOperations.failDestructive(request.indices());
super.doExecute(request, listener);
}

Expand All @@ -96,7 +86,7 @@ protected ClusterBlockException checkBlock(CloseIndexRequest request, ClusterSta

@Override
protected void masterOperation(final CloseIndexRequest request, final ClusterState state, final ActionListener<CloseIndexResponse> listener) throws ElasticsearchException {

request.indices(state.metaData().concreteIndices(request.indices(), request.indicesOptions()));
CloseIndexClusterStateUpdateRequest updateRequest = new CloseIndexClusterStateUpdateRequest()
.ackTimeout(request.timeout()).masterNodeTimeout(request.masterNodeTimeout())
.indices(request.indices());
Expand Down
Expand Up @@ -68,7 +68,7 @@ public DeleteIndexRequest indicesOptions(IndicesOptions indicesOptions) {
@Override
public ActionRequestValidationException validate() {
ActionRequestValidationException validationException = null;
if (indices == null) {
if (indices == null || indices.length == 0) {
validationException = addValidationError("index / indices is missing", validationException);
}
return validationException;
Expand Down Expand Up @@ -114,25 +114,15 @@ public DeleteIndexRequest timeout(String timeout) {
@Override
public void readFrom(StreamInput in) throws IOException {
super.readFrom(in);
indices = new String[in.readVInt()];
for (int i = 0; i < indices.length; i++) {
indices[i] = in.readString();
}
indices = in.readStringArray();
indicesOptions = IndicesOptions.readIndicesOptions(in);
timeout = readTimeValue(in);
}

@Override
public void writeTo(StreamOutput out) throws IOException {
super.writeTo(out);
if (indices == null) {
out.writeVInt(0);
} else {
out.writeVInt(indices.length);
for (String index : indices) {
out.writeString(index);
}
}
out.writeStringArray(indices);
indicesOptions.writeIndicesOptions(out);
timeout.writeTo(out);
}
Expand Down
Expand Up @@ -20,9 +20,8 @@
package org.elasticsearch.action.admin.indices.delete;

import org.elasticsearch.ElasticsearchException;
import org.elasticsearch.ElasticsearchIllegalArgumentException;
import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.admin.indices.mapping.delete.TransportDeleteMappingAction;
import org.elasticsearch.action.support.DestructiveOperations;
import org.elasticsearch.action.support.master.TransportMasterNodeOperationAction;
import org.elasticsearch.cluster.ClusterService;
import org.elasticsearch.cluster.ClusterState;
Expand All @@ -32,6 +31,7 @@
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.util.concurrent.CountDown;
import org.elasticsearch.node.settings.NodeSettingsService;
import org.elasticsearch.threadpool.ThreadPool;
import org.elasticsearch.transport.TransportService;

Expand All @@ -41,19 +41,15 @@
public class TransportDeleteIndexAction extends TransportMasterNodeOperationAction<DeleteIndexRequest, DeleteIndexResponse> {

private final MetaDataDeleteIndexService deleteIndexService;

private final TransportDeleteMappingAction deleteMappingAction;

private final boolean disableDeleteAllIndices;
private final DestructiveOperations destructiveOperations;

@Inject
public TransportDeleteIndexAction(Settings settings, TransportService transportService, ClusterService clusterService,
ThreadPool threadPool, MetaDataDeleteIndexService deleteIndexService, TransportDeleteMappingAction deleteMappingAction) {
ThreadPool threadPool, MetaDataDeleteIndexService deleteIndexService,
NodeSettingsService nodeSettingsService) {
super(settings, transportService, clusterService, threadPool);
this.deleteIndexService = deleteIndexService;
this.deleteMappingAction = deleteMappingAction;

this.disableDeleteAllIndices = settings.getAsBoolean("action.disable_delete_all_indices", false);
this.destructiveOperations = new DestructiveOperations(logger, settings, nodeSettingsService);
}

@Override
Expand All @@ -78,17 +74,7 @@ protected DeleteIndexResponse newResponse() {

@Override
protected void doExecute(DeleteIndexRequest request, ActionListener<DeleteIndexResponse> listener) {
ClusterState state = clusterService.state();
String[] indicesOrAliases = request.indices();

request.indices(state.metaData().concreteIndices(request.indices(), request.indicesOptions()));

if (disableDeleteAllIndices) {
if (state.metaData().isAllIndices(indicesOrAliases) ||
state.metaData().isPatternMatchingAllIndices(indicesOrAliases, request.indices())) {
throw new ElasticsearchIllegalArgumentException("deleting all indices is disabled");
}
}
destructiveOperations.failDestructive(request.indices());
super.doExecute(request, listener);
}

Expand All @@ -99,6 +85,7 @@ protected ClusterBlockException checkBlock(DeleteIndexRequest request, ClusterSt

@Override
protected void masterOperation(final DeleteIndexRequest request, final ClusterState state, final ActionListener<DeleteIndexResponse> listener) throws ElasticsearchException {
request.indices(state.metaData().concreteIndices(request.indices(), request.indicesOptions()));
if (request.indices().length == 0) {
listener.onResponse(new DeleteIndexResponse(true));
return;
Expand Down
Expand Up @@ -27,6 +27,7 @@
import org.elasticsearch.action.admin.indices.refresh.TransportRefreshAction;
import org.elasticsearch.action.deletebyquery.DeleteByQueryResponse;
import org.elasticsearch.action.deletebyquery.TransportDeleteByQueryAction;
import org.elasticsearch.action.support.DestructiveOperations;
import org.elasticsearch.action.support.QuerySourceBuilder;
import org.elasticsearch.action.support.master.TransportMasterNodeOperationAction;
import org.elasticsearch.client.Requests;
Expand All @@ -41,6 +42,7 @@
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.index.query.FilterBuilders;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.node.settings.NodeSettingsService;
import org.elasticsearch.threadpool.ThreadPool;
import org.elasticsearch.transport.TransportService;

Expand All @@ -53,16 +55,19 @@ public class TransportDeleteMappingAction extends TransportMasterNodeOperationAc
private final TransportFlushAction flushAction;
private final TransportDeleteByQueryAction deleteByQueryAction;
private final TransportRefreshAction refreshAction;
private final DestructiveOperations destructiveOperations;

@Inject
public TransportDeleteMappingAction(Settings settings, TransportService transportService, ClusterService clusterService,
ThreadPool threadPool, MetaDataMappingService metaDataMappingService,
TransportDeleteByQueryAction deleteByQueryAction, TransportRefreshAction refreshAction, TransportFlushAction flushAction) {
TransportDeleteByQueryAction deleteByQueryAction, TransportRefreshAction refreshAction,
TransportFlushAction flushAction, NodeSettingsService nodeSettingsService) {
super(settings, transportService, clusterService, threadPool);
this.metaDataMappingService = metaDataMappingService;
this.deleteByQueryAction = deleteByQueryAction;
this.refreshAction = refreshAction;
this.flushAction = flushAction;
this.destructiveOperations = new DestructiveOperations(logger, settings, nodeSettingsService);
}

@Override
Expand All @@ -88,7 +93,7 @@ protected DeleteMappingResponse newResponse() {

@Override
protected void doExecute(DeleteMappingRequest request, ActionListener<DeleteMappingResponse> listener) {
request.indices(clusterService.state().metaData().concreteIndices(request.indices(), request.indicesOptions()));
destructiveOperations.failDestructive(request.indices());
super.doExecute(request, listener);
}

Expand All @@ -99,6 +104,7 @@ protected ClusterBlockException checkBlock(DeleteMappingRequest request, Cluster

@Override
protected void masterOperation(final DeleteMappingRequest request, final ClusterState state, final ActionListener<DeleteMappingResponse> listener) throws ElasticsearchException {
request.indices(state.metaData().concreteIndices(request.indices(), request.indicesOptions()));
flushAction.execute(Requests.flushRequest(request.indices()), new ActionListener<FlushResponse>() {
@Override
public void onResponse(FlushResponse flushResponse) {
Expand Down
Expand Up @@ -21,6 +21,7 @@

import org.elasticsearch.ElasticsearchException;
import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.support.DestructiveOperations;
import org.elasticsearch.action.support.master.TransportMasterNodeOperationAction;
import org.elasticsearch.cluster.ClusterService;
import org.elasticsearch.cluster.ClusterState;
Expand All @@ -31,6 +32,7 @@
import org.elasticsearch.cluster.metadata.MetaDataIndexStateService;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.node.settings.NodeSettingsService;
import org.elasticsearch.threadpool.ThreadPool;
import org.elasticsearch.transport.TransportService;

Expand All @@ -40,12 +42,14 @@
public class TransportOpenIndexAction extends TransportMasterNodeOperationAction<OpenIndexRequest, OpenIndexResponse> {

private final MetaDataIndexStateService indexStateService;
private final DestructiveOperations destructiveOperations;

@Inject
public TransportOpenIndexAction(Settings settings, TransportService transportService, ClusterService clusterService,
ThreadPool threadPool, MetaDataIndexStateService indexStateService) {
ThreadPool threadPool, MetaDataIndexStateService indexStateService, NodeSettingsService nodeSettingsService) {
super(settings, transportService, clusterService, threadPool);
this.indexStateService = indexStateService;
this.destructiveOperations = new DestructiveOperations(logger, settings, nodeSettingsService);
}

@Override
Expand All @@ -71,7 +75,7 @@ protected OpenIndexResponse newResponse() {

@Override
protected void doExecute(OpenIndexRequest request, ActionListener<OpenIndexResponse> listener) {
request.indices(clusterService.state().metaData().concreteIndices(request.indices(), request.indicesOptions()));
destructiveOperations.failDestructive(request.indices());
super.doExecute(request, listener);
}

Expand All @@ -82,7 +86,7 @@ protected ClusterBlockException checkBlock(OpenIndexRequest request, ClusterStat

@Override
protected void masterOperation(final OpenIndexRequest request, final ClusterState state, final ActionListener<OpenIndexResponse> listener) throws ElasticsearchException {

request.indices(state.metaData().concreteIndices(request.indices(), request.indicesOptions()));
OpenIndexClusterStateUpdateRequest updateRequest = new OpenIndexClusterStateUpdateRequest()
.ackTimeout(request.timeout()).masterNodeTimeout(request.masterNodeTimeout())
.indices(request.indices());
Expand Down
Expand Up @@ -20,13 +20,16 @@
package org.elasticsearch.action.deletebyquery;

import org.elasticsearch.ElasticsearchException;
import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.support.DestructiveOperations;
import org.elasticsearch.action.support.replication.TransportIndicesReplicationOperationAction;
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.node.settings.NodeSettingsService;
import org.elasticsearch.threadpool.ThreadPool;
import org.elasticsearch.transport.TransportService;

Expand All @@ -38,10 +41,20 @@
*/
public class TransportDeleteByQueryAction extends TransportIndicesReplicationOperationAction<DeleteByQueryRequest, DeleteByQueryResponse, IndexDeleteByQueryRequest, IndexDeleteByQueryResponse, ShardDeleteByQueryRequest, ShardDeleteByQueryRequest, ShardDeleteByQueryResponse> {

private final DestructiveOperations destructiveOperations;

@Inject
public TransportDeleteByQueryAction(Settings settings, ClusterService clusterService, TransportService transportService,
ThreadPool threadPool, TransportIndexDeleteByQueryAction indexDeleteByQueryAction) {
ThreadPool threadPool, TransportIndexDeleteByQueryAction indexDeleteByQueryAction,
NodeSettingsService nodeSettingsService) {
super(settings, transportService, clusterService, threadPool, indexDeleteByQueryAction);
this.destructiveOperations = new DestructiveOperations(logger, settings, nodeSettingsService);
}

@Override
protected void doExecute(DeleteByQueryRequest request, ActionListener<DeleteByQueryResponse> listener) {
destructiveOperations.failDestructive(request.indices());
super.doExecute(request, listener);
}

@Override
Expand Down Expand Up @@ -82,7 +95,7 @@ protected ClusterBlockException checkGlobalBlock(ClusterState state, DeleteByQue
}

@Override
protected ClusterBlockException checkRequestBlock(ClusterState state, DeleteByQueryRequest replicationPingRequest, String[] concreteIndices) {
protected ClusterBlockException checkRequestBlock(ClusterState state, DeleteByQueryRequest request, String[] concreteIndices) {
return state.blocks().indicesBlockedException(ClusterBlockLevel.WRITE, concreteIndices);
}

Expand Down

0 comments on commit eb63bb2

Please sign in to comment.