Return the index name on a create index response

This commit modifies the create index response so that it includes the
index name.

Relates #25139
This commit is contained in:
Sergey Novikov 2017-06-09 19:47:47 +02:00 committed by Jason Tedor
parent 5997e4a39d
commit 7c8657df0e
9 changed files with 114 additions and 8 deletions

View File

@ -19,6 +19,7 @@
package org.elasticsearch.action.admin.indices.create; package org.elasticsearch.action.admin.indices.create;
import org.elasticsearch.Version;
import org.elasticsearch.action.support.master.AcknowledgedResponse; import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.StreamOutput;
@ -32,14 +33,16 @@ import java.io.IOException;
public class CreateIndexResponse extends AcknowledgedResponse { public class CreateIndexResponse extends AcknowledgedResponse {
private boolean shardsAcked; private boolean shardsAcked;
private String index;
protected CreateIndexResponse() { protected CreateIndexResponse() {
} }
protected CreateIndexResponse(boolean acknowledged, boolean shardsAcked) { protected CreateIndexResponse(boolean acknowledged, boolean shardsAcked, String index) {
super(acknowledged); super(acknowledged);
assert acknowledged || shardsAcked == false; // if its not acknowledged, then shards acked should be false too assert acknowledged || shardsAcked == false; // if its not acknowledged, then shards acked should be false too
this.shardsAcked = shardsAcked; this.shardsAcked = shardsAcked;
this.index = index;
} }
@Override @Override
@ -47,6 +50,9 @@ public class CreateIndexResponse extends AcknowledgedResponse {
super.readFrom(in); super.readFrom(in);
readAcknowledged(in); readAcknowledged(in);
shardsAcked = in.readBoolean(); shardsAcked = in.readBoolean();
if (in.getVersion().onOrAfter(Version.V_6_0_0_alpha3)) {
index = in.readString();
}
} }
@Override @Override
@ -54,6 +60,9 @@ public class CreateIndexResponse extends AcknowledgedResponse {
super.writeTo(out); super.writeTo(out);
writeAcknowledged(out); writeAcknowledged(out);
out.writeBoolean(shardsAcked); out.writeBoolean(shardsAcked);
if (out.getVersion().onOrAfter(Version.V_6_0_0_alpha3)) {
out.writeString(index);
}
} }
/** /**
@ -65,7 +74,12 @@ public class CreateIndexResponse extends AcknowledgedResponse {
return shardsAcked; return shardsAcked;
} }
public String index() {
return index;
}
public void addCustomFields(XContentBuilder builder) throws IOException { public void addCustomFields(XContentBuilder builder) throws IOException {
builder.field("shards_acknowledged", isShardsAcked()); builder.field("shards_acknowledged", isShardsAcked());
builder.field("index", index());
} }
} }

View File

@ -79,7 +79,7 @@ public class TransportCreateIndexAction extends TransportMasterNodeAction<Create
.waitForActiveShards(request.waitForActiveShards()); .waitForActiveShards(request.waitForActiveShards());
createIndexService.createIndex(updateRequest, ActionListener.wrap(response -> createIndexService.createIndex(updateRequest, ActionListener.wrap(response ->
listener.onResponse(new CreateIndexResponse(response.isAcknowledged(), response.isShardsAcked())), listener.onResponse(new CreateIndexResponse(response.isAcknowledged(), response.isShardsAcked(), indexName)),
listener::onFailure)); listener::onFailure));
} }

View File

@ -25,7 +25,7 @@ public final class ShrinkResponse extends CreateIndexResponse {
ShrinkResponse() { ShrinkResponse() {
} }
ShrinkResponse(boolean acknowledged, boolean shardsAcked) { ShrinkResponse(boolean acknowledged, boolean shardsAcked, String index) {
super(acknowledged, shardsAcked); super(acknowledged, shardsAcked, index);
} }
} }

View File

@ -91,8 +91,13 @@ public class TransportShrinkAction extends TransportMasterNodeAction<ShrinkReque
IndexShardStats shard = indicesStatsResponse.getIndex(sourceIndex).getIndexShards().get(i); IndexShardStats shard = indicesStatsResponse.getIndex(sourceIndex).getIndexShards().get(i);
return shard == null ? null : shard.getPrimary().getDocs(); return shard == null ? null : shard.getPrimary().getDocs();
}, indexNameExpressionResolver); }, indexNameExpressionResolver);
createIndexService.createIndex(updateRequest, ActionListener.wrap(response -> createIndexService.createIndex(
listener.onResponse(new ShrinkResponse(response.isAcknowledged(), response.isShardsAcked())), listener::onFailure)); updateRequest,
ActionListener.wrap(response ->
listener.onResponse(new ShrinkResponse(response.isAcknowledged(), response.isShardsAcked(), updateRequest.index())),
listener::onFailure
)
);
} }
@Override @Override

View File

@ -335,4 +335,12 @@ public class CreateIndexIT extends ESIntegTestCase {
assertTrue(createPartitionedIndex.apply(1, 1)); assertTrue(createPartitionedIndex.apply(1, 1));
} }
public void testIndexNameInResponse() {
CreateIndexResponse response = prepareCreate("foo")
.setSettings(Settings.builder().build())
.get();
assertEquals("Should have index name in response", "foo", response.index());
}
} }

View File

@ -0,0 +1,65 @@
/*
* Licensed to Elasticsearch under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.elasticsearch.action.admin.indices.create;
import org.elasticsearch.Version;
import org.elasticsearch.common.io.stream.BytesStreamOutput;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.test.ESTestCase;
import java.io.IOException;
public class CreateIndexResponseTests extends ESTestCase {
public void testSerialization() throws IOException {
CreateIndexResponse response = new CreateIndexResponse(true, true, "foo");
try (BytesStreamOutput output = new BytesStreamOutput()) {
response.writeTo(output);
try (StreamInput in = output.bytes().streamInput()) {
CreateIndexResponse serialized = new CreateIndexResponse();
serialized.readFrom(in);
assertEquals(response.isShardsAcked(), serialized.isShardsAcked());
assertEquals(response.isAcknowledged(), serialized.isAcknowledged());
assertEquals(response.index(), serialized.index());
}
}
}
public void testSerializationWithOldVersion() throws IOException {
Version oldVersion = Version.V_5_4_0;
CreateIndexResponse response = new CreateIndexResponse(true, true, "foo");
try (BytesStreamOutput output = new BytesStreamOutput()) {
output.setVersion(oldVersion);
response.writeTo(output);
try (StreamInput in = output.bytes().streamInput()) {
in.setVersion(oldVersion);
CreateIndexResponse serialized = new CreateIndexResponse();
serialized.readFrom(in);
assertEquals(response.isShardsAcked(), serialized.isShardsAcked());
assertEquals(response.isAcknowledged(), serialized.isAcknowledged());
assertNull(serialized.index());
}
}
}
}

View File

@ -118,7 +118,7 @@ public class TransportBulkActionIndicesThatCannotBeCreatedTests extends ESTestCa
@Override @Override
void createIndex(String index, TimeValue timeout, ActionListener<CreateIndexResponse> listener) { void createIndex(String index, TimeValue timeout, ActionListener<CreateIndexResponse> listener) {
// If we try to create an index just immediately assume it worked // If we try to create an index just immediately assume it worked
listener.onResponse(new CreateIndexResponse(true, true) {}); listener.onResponse(new CreateIndexResponse(true, true, index) {});
} }
}; };
action.doExecute(null, bulkRequest, null); action.doExecute(null, bulkRequest, null);

View File

@ -129,7 +129,8 @@ what happened:
-------------------------------------------------- --------------------------------------------------
{ {
"acknowledged": true, "acknowledged": true,
"shards_acknowledged": true "shards_acknowledged": true,
"index": "test"
} }
-------------------------------------------------- --------------------------------------------------
// TESTRESPONSE // TESTRESPONSE

View File

@ -30,6 +30,19 @@
- match: { test_index.settings.index.number_of_replicas: "0"} - match: { test_index.settings.index.number_of_replicas: "0"}
---
"Create index":
- skip:
version: " - 5.99.99"
reason: create index response contains index name since 5.6.0
- do:
indices.create:
index: test_index
- match: { acknowledged: true }
- match: { index: "test_index"}
--- ---
"Create index with wait_for_active_shards set to all": "Create index with wait_for_active_shards set to all":