From 946cbfb9979ebcc10b5bb91f6aef7d277de99c46 Mon Sep 17 00:00:00 2001 From: Chris Earle Date: Fri, 13 May 2016 19:09:25 -0400 Subject: [PATCH] Remove default index code from _x-pack/monitoring/_bulk With the API no longer accepting {index}, we no longer need the code that supported it. Original commit: elastic/x-pack-elasticsearch@5d0ae37caa5b7ad989de808123e6cedae96980c1 --- .../marvel/action/MonitoringBulkRequest.java | 14 ++----- .../action/MonitoringBulkRequestBuilder.java | 6 +-- .../rest/action/RestMonitoringBulkAction.java | 3 +- .../action/MonitoringBulkRequestTests.java | 40 ++++++++++++++----- 4 files changed, 37 insertions(+), 26 deletions(-) diff --git a/elasticsearch/x-pack/marvel/src/main/java/org/elasticsearch/marvel/action/MonitoringBulkRequest.java b/elasticsearch/x-pack/marvel/src/main/java/org/elasticsearch/marvel/action/MonitoringBulkRequest.java index 7ada7f86a25..d113efaf1e0 100644 --- a/elasticsearch/x-pack/marvel/src/main/java/org/elasticsearch/marvel/action/MonitoringBulkRequest.java +++ b/elasticsearch/x-pack/marvel/src/main/java/org/elasticsearch/marvel/action/MonitoringBulkRequest.java @@ -85,10 +85,10 @@ public class MonitoringBulkRequest extends ActionRequest * Parses a monitoring bulk request and builds the list of documents to be indexed. */ public MonitoringBulkRequest add(BytesReference content, String defaultMonitoringId, String defaultMonitoringVersion, - String defaultIndex, String defaultType) throws Exception { + String defaultType) throws Exception { // MonitoringBulkRequest accepts a body request that has the same format as the BulkRequest: // instead of duplicating the parsing logic here we use a new BulkRequest instance to parse the content. - BulkRequest bulkRequest = Requests.bulkRequest().add(content, defaultIndex, defaultType); + BulkRequest bulkRequest = Requests.bulkRequest().add(content, null, defaultType); for (ActionRequest request : bulkRequest.requests()) { if (request instanceof IndexRequest) { @@ -114,18 +114,12 @@ public class MonitoringBulkRequest extends ActionRequest @Override public void readFrom(StreamInput in) throws IOException { super.readFrom(in); - int size = in.readVInt(); - for (int i = 0; i < size; i++) { - add(new MonitoringBulkDoc(in)); - } + docs.addAll(in.readList(MonitoringBulkDoc::new)); } @Override public void writeTo(StreamOutput out) throws IOException { super.writeTo(out); - out.writeVInt(docs.size()); - for (MonitoringBulkDoc doc : docs) { - doc.writeTo(out); - } + out.writeList(docs); } } diff --git a/elasticsearch/x-pack/marvel/src/main/java/org/elasticsearch/marvel/action/MonitoringBulkRequestBuilder.java b/elasticsearch/x-pack/marvel/src/main/java/org/elasticsearch/marvel/action/MonitoringBulkRequestBuilder.java index 112c6bf4551..edbe3837030 100644 --- a/elasticsearch/x-pack/marvel/src/main/java/org/elasticsearch/marvel/action/MonitoringBulkRequestBuilder.java +++ b/elasticsearch/x-pack/marvel/src/main/java/org/elasticsearch/marvel/action/MonitoringBulkRequestBuilder.java @@ -21,9 +21,9 @@ public class MonitoringBulkRequestBuilder return this; } - public MonitoringBulkRequestBuilder add(BytesReference content, String defaultId, String defaultVersion, String defaultIndex, - String defaultType) throws Exception { - request.add(content, defaultId, defaultVersion, defaultIndex, defaultType); + public MonitoringBulkRequestBuilder add(BytesReference content, String defaultId, String defaultVersion, String defaultType) + throws Exception { + request.add(content, defaultId, defaultVersion, defaultType); return this; } } diff --git a/elasticsearch/x-pack/marvel/src/main/java/org/elasticsearch/marvel/rest/action/RestMonitoringBulkAction.java b/elasticsearch/x-pack/marvel/src/main/java/org/elasticsearch/marvel/rest/action/RestMonitoringBulkAction.java index 0e0e2afd0b2..22be3e57bd7 100644 --- a/elasticsearch/x-pack/marvel/src/main/java/org/elasticsearch/marvel/rest/action/RestMonitoringBulkAction.java +++ b/elasticsearch/x-pack/marvel/src/main/java/org/elasticsearch/marvel/rest/action/RestMonitoringBulkAction.java @@ -42,7 +42,6 @@ public class RestMonitoringBulkAction extends MonitoringRestHandler { @Override protected void handleRequest(RestRequest request, RestChannel channel, MonitoringClient client) throws Exception { - String defaultIndex = request.param("index"); String defaultType = request.param("type"); String id = request.param(MONITORING_ID); @@ -59,7 +58,7 @@ public class RestMonitoringBulkAction extends MonitoringRestHandler { } MonitoringBulkRequestBuilder requestBuilder = client.prepareMonitoringBulk(); - requestBuilder.add(request.content(), id, version, defaultIndex, defaultType); + requestBuilder.add(request.content(), id, version, defaultType); requestBuilder.execute(new RestBuilderListener(channel) { @Override public RestResponse buildResponse(MonitoringBulkResponse response, XContentBuilder builder) throws Exception { diff --git a/elasticsearch/x-pack/marvel/src/test/java/org/elasticsearch/marvel/action/MonitoringBulkRequestTests.java b/elasticsearch/x-pack/marvel/src/test/java/org/elasticsearch/marvel/action/MonitoringBulkRequestTests.java index 3f67e00c0c3..a027f38110a 100644 --- a/elasticsearch/x-pack/marvel/src/test/java/org/elasticsearch/marvel/action/MonitoringBulkRequestTests.java +++ b/elasticsearch/x-pack/marvel/src/test/java/org/elasticsearch/marvel/action/MonitoringBulkRequestTests.java @@ -13,7 +13,6 @@ import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.test.ESTestCase; -import org.hamcrest.CoreMatchers; import org.hamcrest.Matcher; import java.io.IOException; @@ -109,12 +108,26 @@ public class MonitoringBulkRequestTests extends ESTestCase { public void testAddMultipleDocs() throws Exception { final int nbDocs = randomIntBetween(3, 20); + final MonitoringIndex[] indices = new MonitoringIndex[nbDocs]; + final String[] types = new String[nbDocs]; final XContentType xContentType = XContentType.JSON; + int i; try (BytesStreamOutput content = new BytesStreamOutput()) { try (XContentBuilder builder = XContentFactory.contentBuilder(xContentType, content)) { - for (int i = 0; i < nbDocs; i++) { - builder.startObject().startObject("index").endObject().endObject().flush(); + for (i = 0; i < nbDocs; i++) { + builder.startObject().startObject("index"); + if (rarely()) { + indices[i] = MonitoringIndex.DATA; + builder.field("_index", "_data"); + } else { + indices[i] = MonitoringIndex.TIMESTAMPED; + } + if (randomBoolean()) { + types[i] = randomAsciiOfLength(5); + builder.field("_type", types[i]); + } + builder.endObject().endObject().flush(); content.write(xContentType.xContent().streamSeparator()); builder.startObject().field("foo").value(i).endObject().flush(); content.write(xContentType.xContent().streamSeparator()); @@ -123,20 +136,23 @@ public class MonitoringBulkRequestTests extends ESTestCase { String defaultMonitoringId = randomBoolean() ? randomAsciiOfLength(2) : null; String defaultMonitoringVersion = randomBoolean() ? randomAsciiOfLength(3) : null; - String defaultIndex = randomFrom("_data", null); - String defaultType = randomBoolean() ? randomAsciiOfLength(4) : null; - - MonitoringIndex index = MonitoringIndex.from(defaultIndex); + String defaultType = rarely() ? randomAsciiOfLength(4) : null; MonitoringBulkRequest request = new MonitoringBulkRequest(); - request.add(content.bytes(), defaultMonitoringId, defaultMonitoringVersion, defaultIndex, defaultType); + request.add(content.bytes(), defaultMonitoringId, defaultMonitoringVersion, defaultType); assertThat(request.getDocs(), hasSize(nbDocs)); + i = 0; + for (MonitoringBulkDoc doc : request.getDocs()) { + String expectedType = types[i] != null ? types[i] : defaultType; + assertThat(doc.getMonitoringId(), equalTo(defaultMonitoringId)); assertThat(doc.getMonitoringVersion(), equalTo(defaultMonitoringVersion)); - assertThat(doc.getIndex(), sameInstance(index)); - assertThat(doc.getType(), equalTo(defaultType)); + assertThat(doc.getIndex(), sameInstance(indices[i])); + assertThat(doc.getType(), equalTo(expectedType)); + + ++i; } } } @@ -170,7 +186,9 @@ public class MonitoringBulkRequestTests extends ESTestCase { MonitoringBulkRequest request2 = new MonitoringBulkRequest(); request2.readFrom(in); - assertThat(request2.docs.size(), CoreMatchers.equalTo(request.docs.size())); + assertThat(in.available(), equalTo(0)); + assertThat(request2.docs.size(), equalTo(request.docs.size())); + for (int i = 0; i < request2.docs.size(); i++) { MonitoringBulkDoc doc = request.docs.get(i); MonitoringBulkDoc doc2 = request2.docs.get(i);