Cluster State API: Remove index template filtering

The possibility of filtering for index templates in the cluster state API
had been introduced before there was a dedicated index templates API. This
commit removes this support from the cluster state API, as it was not really
clean, requiring you to specify the metadata and the index templates.

Closes #4954
This commit is contained in:
Alexander Reelsen 2014-05-02 12:37:02 +02:00
parent 76e3e54c4a
commit d4fcf23057
11 changed files with 58 additions and 103 deletions

View File

@ -45,8 +45,6 @@ $ curl -XGET 'http://localhost:9200/_cluster/state/{metrics}/{indices}'
`blocks`::
Shows the `blocks` part of the response
In addition the `index_templates` parameter can be specified, which returns the specified index templates only. This works only if the `metadata` is asked to be returned.
A couple of example calls:
[source,js]
@ -59,9 +57,5 @@ $ curl -XGET 'http://localhost:9200/_cluster/state/_all/foo,bar'
# Return only blocks data
$ curl -XGET 'http://localhost:9200/_cluster/state/blocks'
# Return only metadata and a specific index_template
# You should use the dedicated template endpoint for this
$ curl -XGET 'http://localhost:9200/_cluster/state/metadata?index_templates=template_1'
--------------------------------------------------

View File

@ -88,11 +88,7 @@ curl -XGET localhost:9200/_template/temp*
curl -XGET localhost:9200/_template/template_1,template_2
--------------------------------------------------
To get list of all index templates you can use
<<cluster-state,Cluster State>> API
and check for the metadata/templates section of the response.
Or run:
To get list of all index templates you can run:
[source,js]
--------------------------------------------------

View File

@ -29,10 +29,6 @@
"type": "time",
"description": "Specify timeout for connection to master"
},
"index_templates": {
"type": "list",
"description": "A comma separated list to return specific index templates when returning metadata"
},
"flat_settings": {
"type": "boolean",
"description": "Return settings in flat format (default: false)"

View File

@ -79,46 +79,6 @@ setup:
- is_true: routing_table
- is_true: routing_nodes
---
"Filtering the cluster state for specific index templates should work ":
- do:
indices.put_template:
name: test1
body:
template: test-*
settings:
number_of_shards: 1
- do:
indices.put_template:
name: test2
body:
template: test-*
settings:
number_of_shards: 2
- do:
indices.put_template:
name: foo
body:
template: foo-*
settings:
number_of_shards: 3
- do:
cluster.state:
metric: [ metadata ]
index_templates: [ test1, test2 ]
- is_false: blocks
- is_false: nodes
- is_true: metadata
- is_false: routing_table
- is_false: routing_nodes
- is_true: metadata.templates.test1
- is_true: metadata.templates.test2
- is_false: metadata.templates.foo
---
"Filtering the cluster state by indices should work in routing table and metadata":
- do:

View File

@ -19,6 +19,7 @@
package org.elasticsearch.action.admin.cluster.state;
import org.elasticsearch.Version;
import org.elasticsearch.action.ActionRequestValidationException;
import org.elasticsearch.action.support.master.MasterNodeReadOperationRequest;
import org.elasticsearch.common.Strings;
@ -37,7 +38,6 @@ public class ClusterStateRequest extends MasterNodeReadOperationRequest<ClusterS
private boolean metaData = true;
private boolean blocks = true;
private String[] indices = Strings.EMPTY_ARRAY;
private String[] indexTemplates = Strings.EMPTY_ARRAY;
public ClusterStateRequest() {
}
@ -53,7 +53,6 @@ public class ClusterStateRequest extends MasterNodeReadOperationRequest<ClusterS
metaData = true;
blocks = true;
indices = Strings.EMPTY_ARRAY;
indexTemplates = Strings.EMPTY_ARRAY;
return this;
}
@ -63,7 +62,6 @@ public class ClusterStateRequest extends MasterNodeReadOperationRequest<ClusterS
metaData = false;
blocks = false;
indices = Strings.EMPTY_ARRAY;
indexTemplates = Strings.EMPTY_ARRAY;
return this;
}
@ -112,15 +110,6 @@ public class ClusterStateRequest extends MasterNodeReadOperationRequest<ClusterS
return this;
}
public String[] indexTemplates() {
return this.indexTemplates;
}
public ClusterStateRequest indexTemplates(String... indexTemplates) {
this.indexTemplates = indexTemplates;
return this;
}
@Override
public void readFrom(StreamInput in) throws IOException {
super.readFrom(in);
@ -129,7 +118,10 @@ public class ClusterStateRequest extends MasterNodeReadOperationRequest<ClusterS
metaData = in.readBoolean();
blocks = in.readBoolean();
indices = in.readStringArray();
indexTemplates = in.readStringArray();
// fake support for indices in pre 1.2.0 versions
if (in.getVersion().before(Version.V_1_2_0)) {
in.readStringArray();
}
readLocal(in);
}
@ -141,7 +133,10 @@ public class ClusterStateRequest extends MasterNodeReadOperationRequest<ClusterS
out.writeBoolean(metaData);
out.writeBoolean(blocks);
out.writeStringArray(indices);
out.writeStringArray(indexTemplates);
// fake support for indices in pre 1.2.0 versions
if (out.getVersion().before(Version.V_1_2_0)) {
out.writeStringArray(Strings.EMPTY_ARRAY);
}
writeLocal(out);
}
}

View File

@ -90,11 +90,6 @@ public class ClusterStateRequestBuilder extends MasterNodeReadOperationRequestBu
return this;
}
public ClusterStateRequestBuilder setIndexTemplates(String... templates) {
request.indexTemplates(templates);
return this;
}
@Override
protected void doExecute(ActionListener<ClusterStateResponse> listener) {
((ClusterAdminClient) client).state(request, listener);

View File

@ -19,6 +19,7 @@
package org.elasticsearch.action.admin.cluster.state;
import com.carrotsearch.hppc.cursors.ObjectCursor;
import org.elasticsearch.ElasticsearchException;
import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.support.master.TransportMasterNodeReadOperationAction;
@ -96,7 +97,7 @@ public class TransportClusterStateAction extends TransportMasterNodeReadOperatio
}
if (request.metaData()) {
MetaData.Builder mdBuilder;
if (request.indices().length == 0 && request.indexTemplates().length == 0) {
if (request.indices().length == 0) {
mdBuilder = MetaData.builder(currentState.metaData());
} else {
mdBuilder = MetaData.builder();
@ -112,15 +113,6 @@ public class TransportClusterStateAction extends TransportMasterNodeReadOperatio
}
}
if (request.indexTemplates().length > 0) {
for (String templateName : request.indexTemplates()) {
IndexTemplateMetaData indexTemplateMetaData = currentState.metaData().templates().get(templateName);
if (indexTemplateMetaData != null) {
mdBuilder.put(indexTemplateMetaData);
}
}
}
builder.metaData(mdBuilder);
}
listener.onResponse(new ClusterStateResponse(clusterName, builder.build()));

View File

@ -74,7 +74,6 @@ public class RestClusterStateAction extends BaseRestHandler {
clusterStateRequest.routingTable(metrics.contains("routing_table"));
clusterStateRequest.metaData(metrics.contains("metadata"));
clusterStateRequest.blocks(metrics.contains("blocks"));
clusterStateRequest.indexTemplates(request.paramAsStringArray("index_templates", Strings.EMPTY_ARRAY));
}
client.admin().cluster().state(clusterStateRequest, new RestBuilderListener<ClusterStateResponse>(channel) {

View File

@ -20,13 +20,16 @@
package org.elasticsearch.cluster;
import org.elasticsearch.action.admin.cluster.state.ClusterStateResponse;
import org.elasticsearch.action.admin.indices.template.get.GetIndexTemplatesResponse;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.test.ElasticsearchIntegrationTest;
import org.elasticsearch.test.hamcrest.CollectionAssertions;
import org.junit.Before;
import org.junit.Test;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertIndexTemplateExists;
import static org.hamcrest.Matchers.greaterThanOrEqualTo;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.is;
/**
@ -98,8 +101,8 @@ public class SimpleClusterStateTests extends ElasticsearchIntegrationTest {
ClusterStateResponse clusterStateResponseUnfiltered = client().admin().cluster().prepareState().get();
assertThat(clusterStateResponseUnfiltered.getState().metaData().templates().size(), is(greaterThanOrEqualTo(2)));
ClusterStateResponse clusterStateResponse = client().admin().cluster().prepareState().clear().setMetaData(true).setIndexTemplates("foo_template").get();
assertThat(clusterStateResponse.getState().metaData().templates().size(), is(1));
GetIndexTemplatesResponse getIndexTemplatesResponse = client().admin().indices().prepareGetTemplates("foo_template").get();
assertIndexTemplateExists(getIndexTemplatesResponse, "foo_template");
}
@Test

View File

@ -32,13 +32,13 @@ import org.elasticsearch.action.admin.cluster.snapshots.restore.RestoreSnapshotR
import org.elasticsearch.action.admin.cluster.snapshots.status.*;
import org.elasticsearch.action.admin.cluster.state.ClusterStateResponse;
import org.elasticsearch.action.admin.indices.settings.get.GetSettingsResponse;
import org.elasticsearch.action.admin.indices.template.get.GetIndexTemplatesResponse;
import org.elasticsearch.action.count.CountResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.cluster.ClusterState;
import org.elasticsearch.cluster.metadata.MappingMetaData;
import org.elasticsearch.cluster.metadata.SnapshotMetaData;
import org.elasticsearch.cluster.routing.allocation.decider.FilterAllocationDecider;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.collect.ImmutableOpenMap;
import org.elasticsearch.common.settings.ImmutableSettings;
import org.elasticsearch.common.settings.Settings;
@ -53,7 +53,7 @@ import org.junit.Test;
import java.io.File;
import static org.elasticsearch.cluster.metadata.IndexMetaData.*;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.*;
import static org.hamcrest.Matchers.*;
@Slow
@ -227,8 +227,8 @@ public class SharedClusterSnapshotRestoreTests extends AbstractSnapshotTests {
logger.info("--> delete test template");
assertThat(client.admin().indices().prepareDeleteTemplate("test-template").get().isAcknowledged(), equalTo(true));
ClusterStateResponse clusterStateResponse = client.admin().cluster().prepareState().setRoutingTable(false).setNodes(false).setIndexTemplates("test-template").setIndices(Strings.EMPTY_ARRAY).get();
assertThat(clusterStateResponse.getState().getMetaData().templates().containsKey("test-template"), equalTo(false));
GetIndexTemplatesResponse getIndexTemplatesResponse = client().admin().indices().prepareGetTemplates().get();
assertIndexTemplateMissing(getIndexTemplatesResponse, "test-template");
logger.info("--> restore cluster state");
RestoreSnapshotResponse restoreSnapshotResponse = client.admin().cluster().prepareRestoreSnapshot("test-repo", "test-snap").setWaitForCompletion(true).setRestoreGlobalState(true).execute().actionGet();
@ -236,8 +236,9 @@ public class SharedClusterSnapshotRestoreTests extends AbstractSnapshotTests {
assertThat(restoreSnapshotResponse.getRestoreInfo().totalShards(), equalTo(0));
logger.info("--> check that template is restored");
clusterStateResponse = client.admin().cluster().prepareState().setRoutingTable(false).setNodes(false).setIndexTemplates("test-template").setIndices(Strings.EMPTY_ARRAY).get();
assertThat(clusterStateResponse.getState().getMetaData().templates().containsKey("test-template"), equalTo(true));
getIndexTemplatesResponse = client().admin().indices().prepareGetTemplates().get();
assertIndexTemplateExists(getIndexTemplatesResponse, "test-template");
}
@Test
@ -266,24 +267,24 @@ public class SharedClusterSnapshotRestoreTests extends AbstractSnapshotTests {
logger.info("--> delete test template");
immutableCluster().wipeTemplates("test-template");
ClusterStateResponse clusterStateResponse = client.admin().cluster().prepareState().setRoutingTable(false).setNodes(false).setIndexTemplates("test-template").setIndices().get();
assertThat(clusterStateResponse.getState().getMetaData().templates().containsKey("test-template"), equalTo(false));
GetIndexTemplatesResponse getIndexTemplatesResponse = client().admin().indices().prepareGetTemplates().get();
assertIndexTemplateMissing(getIndexTemplatesResponse, "test-template");
logger.info("--> try restoring cluster state from snapshot without global state");
RestoreSnapshotResponse restoreSnapshotResponse = client.admin().cluster().prepareRestoreSnapshot("test-repo", "test-snap-no-global-state").setWaitForCompletion(true).setRestoreGlobalState(true).execute().actionGet();
assertThat(restoreSnapshotResponse.getRestoreInfo().totalShards(), equalTo(0));
logger.info("--> check that template wasn't restored");
clusterStateResponse = client.admin().cluster().prepareState().setRoutingTable(false).setNodes(false).setIndexTemplates("test-template").setIndices().get();
assertThat(clusterStateResponse.getState().getMetaData().templates().containsKey("test-template"), equalTo(false));
getIndexTemplatesResponse = client().admin().indices().prepareGetTemplates().get();
assertIndexTemplateMissing(getIndexTemplatesResponse, "test-template");
logger.info("--> restore cluster state");
restoreSnapshotResponse = client.admin().cluster().prepareRestoreSnapshot("test-repo", "test-snap-with-global-state").setWaitForCompletion(true).setRestoreGlobalState(true).execute().actionGet();
assertThat(restoreSnapshotResponse.getRestoreInfo().totalShards(), equalTo(0));
logger.info("--> check that template is restored");
clusterStateResponse = client.admin().cluster().prepareState().setRoutingTable(false).setNodes(false).setIndexTemplates("test-template").setIndices().get();
assertThat(clusterStateResponse.getState().getMetaData().templates().containsKey("test-template"), equalTo(true));
getIndexTemplatesResponse = client().admin().indices().prepareGetTemplates().get();
assertIndexTemplateExists(getIndexTemplatesResponse, "test-template");
createIndex("test-idx");
ensureGreen();
@ -304,8 +305,8 @@ public class SharedClusterSnapshotRestoreTests extends AbstractSnapshotTests {
logger.info("--> delete test template and index ");
immutableCluster().wipeIndices("test-idx");
immutableCluster().wipeTemplates("test-template");
clusterStateResponse = client.admin().cluster().prepareState().setRoutingTable(false).setNodes(false).setIndexTemplates("test-template").setIndices().get();
assertThat(clusterStateResponse.getState().getMetaData().templates().containsKey("test-template"), equalTo(false));
getIndexTemplatesResponse = client().admin().indices().prepareGetTemplates().get();
assertIndexTemplateMissing(getIndexTemplatesResponse, "test-template");
logger.info("--> try restoring index and cluster state from snapshot without global state");
restoreSnapshotResponse = client.admin().cluster().prepareRestoreSnapshot("test-repo", "test-snap-no-global-state-with-index").setWaitForCompletion(true).setRestoreGlobalState(true).execute().actionGet();
@ -314,8 +315,8 @@ public class SharedClusterSnapshotRestoreTests extends AbstractSnapshotTests {
ensureGreen();
logger.info("--> check that template wasn't restored but index was");
clusterStateResponse = client.admin().cluster().prepareState().setRoutingTable(false).setNodes(false).setIndexTemplates("test-template").setIndices().get();
assertThat(clusterStateResponse.getState().getMetaData().templates().containsKey("test-template"), equalTo(false));
getIndexTemplatesResponse = client().admin().indices().prepareGetTemplates().get();
assertIndexTemplateMissing(getIndexTemplatesResponse, "test-template");
assertThat(client.prepareCount("test-idx").get().getCount(), equalTo(100L));
}

View File

@ -32,6 +32,7 @@ import org.elasticsearch.action.admin.cluster.health.ClusterHealthRequestBuilder
import org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequestBuilder;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexResponse;
import org.elasticsearch.action.admin.indices.template.get.GetIndexTemplatesResponse;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.count.CountResponse;
import org.elasticsearch.action.get.GetResponse;
@ -43,6 +44,7 @@ import org.elasticsearch.action.search.ShardSearchFailure;
import org.elasticsearch.action.support.broadcast.BroadcastOperationResponse;
import org.elasticsearch.action.support.master.AcknowledgedRequestBuilder;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.cluster.metadata.IndexTemplateMetaData;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.io.stream.BytesStreamInput;
import org.elasticsearch.common.io.stream.BytesStreamOutput;
@ -330,6 +332,28 @@ public class ElasticsearchAssertions {
}
}
/**
* Assert that an index template is missing
*/
public static void assertIndexTemplateMissing(GetIndexTemplatesResponse templatesResponse, String name) {
List<String> templateNames = new ArrayList<>();
for (IndexTemplateMetaData indexTemplateMetaData : templatesResponse.getIndexTemplates()) {
templateNames.add(indexTemplateMetaData.name());
}
assertThat(templateNames, not(hasItem(name)));
}
/**
* Assert that an index template exists
*/
public static void assertIndexTemplateExists(GetIndexTemplatesResponse templatesResponse, String name) {
List<String> templateNames = new ArrayList<>();
for (IndexTemplateMetaData indexTemplateMetaData : templatesResponse.getIndexTemplates()) {
templateNames.add(indexTemplateMetaData.name());
}
assertThat(templateNames, hasItem(name));
}
/*
* matchers
*/