Skip to content

Commit fafeb3a

Browse files
committed
Introduce a common base response class to all single doc write ops
IndexResponse, DeleteResponse and UpdateResponse share some logic. This can be unified to a single DocWriteResponse base class. On top, some replication actions are now not about write operations anymore. This commit renames ActionWriteResponse to ReplicationResponse Last some toXContent is moved from the Rest layer to the actual response classes, for more code re-sharing. Closes #15334
1 parent fab4439 commit fafeb3a

26 files changed

+385
-423
lines changed
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
/*
2+
* Licensed to Elasticsearch under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.elasticsearch.action;
20+
21+
import org.elasticsearch.common.io.stream.StreamInput;
22+
import org.elasticsearch.common.io.stream.StreamOutput;
23+
import org.elasticsearch.common.xcontent.StatusToXContent;
24+
import org.elasticsearch.common.xcontent.ToXContent;
25+
import org.elasticsearch.common.xcontent.XContentBuilder;
26+
import org.elasticsearch.common.xcontent.XContentBuilderString;
27+
import org.elasticsearch.index.shard.ShardId;
28+
import org.elasticsearch.rest.RestStatus;
29+
30+
import java.io.IOException;
31+
32+
/**
33+
* A base class for the response of a write operation that involves a single doc
34+
*/
35+
public abstract class DocWriteResponse extends ReplicationResponse implements StatusToXContent {
36+
37+
private ShardId shardId;
38+
private String id;
39+
private String type;
40+
private long version;
41+
42+
public DocWriteResponse(ShardId shardId, String type, String id, long version) {
43+
this.shardId = shardId;
44+
this.type = type;
45+
this.id = id;
46+
this.version = version;
47+
}
48+
49+
// needed for deserialization
50+
protected DocWriteResponse() {
51+
}
52+
53+
/**
54+
* The index the document was changed in.
55+
*/
56+
public String getIndex() {
57+
return this.shardId.getIndex();
58+
}
59+
60+
61+
/**
62+
* The exact shard the document was changed in.
63+
*/
64+
public ShardId getShardId() {
65+
return this.shardId;
66+
}
67+
68+
/**
69+
* The type of the document changed.
70+
*/
71+
public String getType() {
72+
return this.type;
73+
}
74+
75+
/**
76+
* The id of the document changed.
77+
*/
78+
public String getId() {
79+
return this.id;
80+
}
81+
82+
/**
83+
* Returns the current version of the doc.
84+
*/
85+
public long getVersion() {
86+
return this.version;
87+
}
88+
89+
/** returns the rest status for this response (based on {@link ShardInfo#status()} */
90+
public RestStatus status() {
91+
return getShardInfo().status();
92+
}
93+
94+
95+
@Override
96+
public void readFrom(StreamInput in) throws IOException {
97+
super.readFrom(in);
98+
shardId = ShardId.readShardId(in);
99+
type = in.readString();
100+
id = in.readString();
101+
version = in.readZLong();
102+
}
103+
104+
@Override
105+
public void writeTo(StreamOutput out) throws IOException {
106+
super.writeTo(out);
107+
shardId.writeTo(out);
108+
out.writeString(type);
109+
out.writeString(id);
110+
out.writeZLong(version);
111+
}
112+
113+
static final class Fields {
114+
static final XContentBuilderString _INDEX = new XContentBuilderString("_index");
115+
static final XContentBuilderString _TYPE = new XContentBuilderString("_type");
116+
static final XContentBuilderString _ID = new XContentBuilderString("_id");
117+
static final XContentBuilderString _VERSION = new XContentBuilderString("_version");
118+
}
119+
120+
@Override
121+
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
122+
ReplicationResponse.ShardInfo shardInfo = getShardInfo();
123+
builder.field(Fields._INDEX, shardId.getIndex())
124+
.field(Fields._TYPE, type)
125+
.field(Fields._ID, id)
126+
.field(Fields._VERSION, version);
127+
shardInfo.toXContent(builder, params);
128+
return builder;
129+
}
130+
}

core/src/main/java/org/elasticsearch/action/ActionWriteResponse.java renamed to core/src/main/java/org/elasticsearch/action/ReplicationResponse.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121

2222
import org.elasticsearch.ElasticsearchException;
2323
import org.elasticsearch.ExceptionsHelper;
24-
import org.elasticsearch.bootstrap.Elasticsearch;
2524
import org.elasticsearch.common.Nullable;
2625
import org.elasticsearch.common.Strings;
2726
import org.elasticsearch.common.io.stream.StreamInput;
@@ -30,25 +29,23 @@
3029
import org.elasticsearch.common.xcontent.ToXContent;
3130
import org.elasticsearch.common.xcontent.XContentBuilder;
3231
import org.elasticsearch.common.xcontent.XContentBuilderString;
33-
import org.elasticsearch.common.xcontent.json.JsonXContent;
3432
import org.elasticsearch.rest.RestStatus;
3533

3634
import java.io.IOException;
37-
import java.util.Collections;
3835

3936
/**
4037
* Base class for write action responses.
4138
*/
42-
public class ActionWriteResponse extends ActionResponse {
39+
public class ReplicationResponse extends ActionResponse {
4340

44-
public final static ActionWriteResponse.ShardInfo.Failure[] EMPTY = new ActionWriteResponse.ShardInfo.Failure[0];
41+
public final static ReplicationResponse.ShardInfo.Failure[] EMPTY = new ReplicationResponse.ShardInfo.Failure[0];
4542

4643
private ShardInfo shardInfo;
4744

4845
@Override
4946
public void readFrom(StreamInput in) throws IOException {
5047
super.readFrom(in);
51-
shardInfo = ActionWriteResponse.ShardInfo.readShardInfo(in);
48+
shardInfo = ReplicationResponse.ShardInfo.readShardInfo(in);
5249
}
5350

5451
@Override

core/src/main/java/org/elasticsearch/action/admin/indices/flush/TransportFlushAction.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
package org.elasticsearch.action.admin.indices.flush;
2121

22-
import org.elasticsearch.action.ActionWriteResponse;
22+
import org.elasticsearch.action.ReplicationResponse;
2323
import org.elasticsearch.action.ShardOperationFailedException;
2424
import org.elasticsearch.action.support.ActionFilters;
2525
import org.elasticsearch.action.support.replication.TransportBroadcastReplicationAction;
@@ -36,7 +36,7 @@
3636
/**
3737
* Flush Action.
3838
*/
39-
public class TransportFlushAction extends TransportBroadcastReplicationAction<FlushRequest, FlushResponse, ShardFlushRequest, ActionWriteResponse> {
39+
public class TransportFlushAction extends TransportBroadcastReplicationAction<FlushRequest, FlushResponse, ShardFlushRequest, ReplicationResponse> {
4040

4141
@Inject
4242
public TransportFlushAction(Settings settings, ThreadPool threadPool, ClusterService clusterService,
@@ -47,8 +47,8 @@ public TransportFlushAction(Settings settings, ThreadPool threadPool, ClusterSer
4747
}
4848

4949
@Override
50-
protected ActionWriteResponse newShardResponse() {
51-
return new ActionWriteResponse();
50+
protected ReplicationResponse newShardResponse() {
51+
return new ReplicationResponse();
5252
}
5353

5454
@Override

core/src/main/java/org/elasticsearch/action/admin/indices/flush/TransportShardFlushAction.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
package org.elasticsearch.action.admin.indices.flush;
2121

22-
import org.elasticsearch.action.ActionWriteResponse;
22+
import org.elasticsearch.action.ReplicationResponse;
2323
import org.elasticsearch.action.support.ActionFilters;
2424
import org.elasticsearch.action.support.replication.TransportReplicationAction;
2525
import org.elasticsearch.cluster.ClusterService;
@@ -39,7 +39,7 @@
3939
/**
4040
*
4141
*/
42-
public class TransportShardFlushAction extends TransportReplicationAction<ShardFlushRequest, ShardFlushRequest, ActionWriteResponse> {
42+
public class TransportShardFlushAction extends TransportReplicationAction<ShardFlushRequest, ShardFlushRequest, ReplicationResponse> {
4343

4444
public static final String NAME = FlushAction.NAME + "[s]";
4545

@@ -53,16 +53,16 @@ public TransportShardFlushAction(Settings settings, TransportService transportSe
5353
}
5454

5555
@Override
56-
protected ActionWriteResponse newResponseInstance() {
57-
return new ActionWriteResponse();
56+
protected ReplicationResponse newResponseInstance() {
57+
return new ReplicationResponse();
5858
}
5959

6060
@Override
61-
protected Tuple<ActionWriteResponse, ShardFlushRequest> shardOperationOnPrimary(MetaData metaData, ShardFlushRequest shardRequest) throws Throwable {
61+
protected Tuple<ReplicationResponse, ShardFlushRequest> shardOperationOnPrimary(MetaData metaData, ShardFlushRequest shardRequest) throws Throwable {
6262
IndexShard indexShard = indicesService.indexServiceSafe(shardRequest.shardId().getIndex()).getShard(shardRequest.shardId().id());
6363
indexShard.flush(shardRequest.getRequest());
6464
logger.trace("{} flush request executed on primary", indexShard.shardId());
65-
return new Tuple<>(new ActionWriteResponse(), shardRequest);
65+
return new Tuple<>(new ReplicationResponse(), shardRequest);
6666
}
6767

6868
@Override

core/src/main/java/org/elasticsearch/action/admin/indices/refresh/TransportRefreshAction.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
package org.elasticsearch.action.admin.indices.refresh;
2121

22-
import org.elasticsearch.action.ActionWriteResponse;
22+
import org.elasticsearch.action.ReplicationResponse;
2323
import org.elasticsearch.action.ShardOperationFailedException;
2424
import org.elasticsearch.action.support.ActionFilters;
2525
import org.elasticsearch.action.support.replication.ReplicationRequest;
@@ -37,7 +37,7 @@
3737
/**
3838
* Refresh action.
3939
*/
40-
public class TransportRefreshAction extends TransportBroadcastReplicationAction<RefreshRequest, RefreshResponse, ReplicationRequest, ActionWriteResponse> {
40+
public class TransportRefreshAction extends TransportBroadcastReplicationAction<RefreshRequest, RefreshResponse, ReplicationRequest, ReplicationResponse> {
4141

4242
@Inject
4343
public TransportRefreshAction(Settings settings, ThreadPool threadPool, ClusterService clusterService,
@@ -48,8 +48,8 @@ public TransportRefreshAction(Settings settings, ThreadPool threadPool, ClusterS
4848
}
4949

5050
@Override
51-
protected ActionWriteResponse newShardResponse() {
52-
return new ActionWriteResponse();
51+
protected ReplicationResponse newShardResponse() {
52+
return new ReplicationResponse();
5353
}
5454

5555
@Override

core/src/main/java/org/elasticsearch/action/admin/indices/refresh/TransportShardRefreshAction.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
package org.elasticsearch.action.admin.indices.refresh;
2121

22-
import org.elasticsearch.action.ActionWriteResponse;
22+
import org.elasticsearch.action.ReplicationResponse;
2323
import org.elasticsearch.action.support.ActionFilters;
2424
import org.elasticsearch.action.support.replication.ReplicationRequest;
2525
import org.elasticsearch.action.support.replication.TransportReplicationAction;
@@ -41,7 +41,7 @@
4141
/**
4242
*
4343
*/
44-
public class TransportShardRefreshAction extends TransportReplicationAction<ReplicationRequest, ReplicationRequest, ActionWriteResponse> {
44+
public class TransportShardRefreshAction extends TransportReplicationAction<ReplicationRequest, ReplicationRequest, ReplicationResponse> {
4545

4646
public static final String NAME = RefreshAction.NAME + "[s]";
4747

@@ -55,16 +55,16 @@ public TransportShardRefreshAction(Settings settings, TransportService transport
5555
}
5656

5757
@Override
58-
protected ActionWriteResponse newResponseInstance() {
59-
return new ActionWriteResponse();
58+
protected ReplicationResponse newResponseInstance() {
59+
return new ReplicationResponse();
6060
}
6161

6262
@Override
63-
protected Tuple<ActionWriteResponse, ReplicationRequest> shardOperationOnPrimary(MetaData metaData, ReplicationRequest shardRequest) throws Throwable {
63+
protected Tuple<ReplicationResponse, ReplicationRequest> shardOperationOnPrimary(MetaData metaData, ReplicationRequest shardRequest) throws Throwable {
6464
IndexShard indexShard = indicesService.indexServiceSafe(shardRequest.shardId().getIndex()).getShard(shardRequest.shardId().id());
6565
indexShard.refresh("api");
6666
logger.trace("{} refresh request executed on primary", indexShard.shardId());
67-
return new Tuple<>(new ActionWriteResponse(), shardRequest);
67+
return new Tuple<>(new ReplicationResponse(), shardRequest);
6868
}
6969

7070
@Override

0 commit comments

Comments
 (0)