-
Notifications
You must be signed in to change notification settings - Fork 25.2k
Change BroadcastResponse from ToXContentFragment to ToXContentObject #28878
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
826c79c
1413d0c
3b5a67d
c24f791
19c5bf8
24c18f2
685b94c
e59cc42
a9ecd59
20a49cd
e25a6ac
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,7 +24,6 @@ | |
import org.elasticsearch.common.Strings; | ||
import org.elasticsearch.common.io.stream.StreamInput; | ||
import org.elasticsearch.common.io.stream.StreamOutput; | ||
import org.elasticsearch.common.xcontent.ToXContentFragment; | ||
import org.elasticsearch.common.xcontent.XContentBuilder; | ||
import org.elasticsearch.indices.recovery.RecoveryState; | ||
|
||
|
@@ -37,7 +36,7 @@ | |
/** | ||
* Information regarding the recovery state of indices and their associated shards. | ||
*/ | ||
public class RecoveryResponse extends BroadcastResponse implements ToXContentFragment { | ||
public class RecoveryResponse extends BroadcastResponse { | ||
|
||
private boolean detailed = false; | ||
private Map<String, List<RecoveryState>> shardRecoveryStates = new HashMap<>(); | ||
|
@@ -87,6 +86,7 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws | |
if (recoveryStates == null || recoveryStates.size() == 0) { | ||
continue; | ||
} | ||
builder.startObject(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Doesn't this result in an empty builder when There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @olcbean Yes, you are right. The |
||
builder.startObject(index); | ||
builder.startArray("shards"); | ||
for (RecoveryState recoveryState : recoveryStates) { | ||
|
@@ -96,6 +96,7 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws | |
} | ||
builder.endArray(); | ||
builder.endObject(); | ||
builder.endObject(); | ||
} | ||
} | ||
return builder; | ||
|
@@ -133,4 +134,4 @@ public void readFrom(StreamInput in) throws IOException { | |
public String toString() { | ||
return Strings.toString(this, true, true); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,9 +29,9 @@ | |
import org.elasticsearch.common.io.stream.StreamInput; | ||
import org.elasticsearch.common.io.stream.StreamOutput; | ||
import org.elasticsearch.common.unit.ByteSizeValue; | ||
import org.elasticsearch.common.xcontent.ToXContentFragment; | ||
import org.elasticsearch.common.xcontent.XContentBuilder; | ||
import org.elasticsearch.index.engine.Segment; | ||
import org.elasticsearch.rest.action.RestActions; | ||
|
||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
|
@@ -43,7 +43,7 @@ | |
import java.util.Map; | ||
import java.util.Set; | ||
|
||
public class IndicesSegmentResponse extends BroadcastResponse implements ToXContentFragment { | ||
public class IndicesSegmentResponse extends BroadcastResponse { | ||
|
||
private ShardSegments[] shards; | ||
|
||
|
@@ -104,6 +104,8 @@ public void writeTo(StreamOutput out) throws IOException { | |
|
||
@Override | ||
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { | ||
builder.startObject(); | ||
RestActions.buildBroadcastShardsHeader(builder, params, this); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. given that in most cases we have the shards header, how about doing this in
subclasses can override this additional method to add their own fields rather than repeating the shards header every time. There is always the possibility to also override toXContent for content where the shards header is not printed out. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Exactly ! |
||
builder.startObject(Fields.INDICES); | ||
|
||
for (IndexSegments indexSegments : getIndices().values()) { | ||
|
@@ -172,6 +174,7 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws | |
builder.endObject(); | ||
} | ||
|
||
builder.endObject(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. could you also make the two toXContent static methods private? |
||
builder.endObject(); | ||
return builder; | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,9 +24,9 @@ | |
import org.elasticsearch.cluster.routing.ShardRouting; | ||
import org.elasticsearch.common.io.stream.StreamInput; | ||
import org.elasticsearch.common.io.stream.StreamOutput; | ||
import org.elasticsearch.common.xcontent.ToXContentFragment; | ||
import org.elasticsearch.common.xcontent.XContentBuilder; | ||
import org.elasticsearch.common.xcontent.XContentFactory; | ||
import org.elasticsearch.rest.action.RestActions; | ||
|
||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
|
@@ -38,7 +38,7 @@ | |
|
||
import static java.util.Collections.unmodifiableMap; | ||
|
||
public class IndicesStatsResponse extends BroadcastResponse implements ToXContentFragment { | ||
public class IndicesStatsResponse extends BroadcastResponse { | ||
|
||
private ShardStats[] shards; | ||
|
||
|
@@ -154,7 +154,8 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws | |
throw new IllegalArgumentException("level parameter must be one of [cluster] or [indices] or [shards] but was [" + level + "]"); | ||
} | ||
|
||
|
||
builder.startObject(); | ||
RestActions.buildBroadcastShardsHeader(builder, params, this); | ||
builder.startObject("_all"); | ||
|
||
builder.startObject("primaries"); | ||
|
@@ -197,7 +198,7 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws | |
} | ||
builder.endObject(); | ||
} | ||
|
||
builder.endObject(); | ||
return builder; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. would you mind also changing the toString method here to call Strings.toString instead? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Of course not :) |
||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,6 +23,8 @@ | |
import org.elasticsearch.action.support.broadcast.BroadcastResponse; | ||
import org.elasticsearch.common.io.stream.StreamInput; | ||
import org.elasticsearch.common.io.stream.StreamOutput; | ||
import org.elasticsearch.common.xcontent.XContentBuilder; | ||
import org.elasticsearch.rest.action.RestActions; | ||
|
||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
|
@@ -38,8 +40,15 @@ | |
*/ | ||
public class ValidateQueryResponse extends BroadcastResponse { | ||
|
||
public static final String INDEX_FIELD = "index"; | ||
public static final String SHARD_FIELD = "shard"; | ||
public static final String VALID_FIELD = "valid"; | ||
public static final String EXPLANATIONS_FIELD = "explanations"; | ||
public static final String ERROR_FIELD = "error"; | ||
public static final String EXPLANATION_FIELD = "explanation"; | ||
|
||
private boolean valid; | ||
|
||
private List<QueryExplanation> queryExplanations; | ||
|
||
ValidateQueryResponse() { | ||
|
@@ -96,4 +105,34 @@ public void writeTo(StreamOutput out) throws IOException { | |
} | ||
|
||
} | ||
|
||
@Override | ||
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { | ||
builder.startObject(); | ||
builder.field(VALID_FIELD, isValid()); | ||
RestActions.buildBroadcastShardsHeader(builder, params, this); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. shall we leverage addCustomFields here too and just move the valid field after the shards header? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I feel like we are putting the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see, but you can't rely on keys ordering when parsing json... we are free to change order as that is not guaranteed. Maybe we should then update the docs as well accordingly just to make sure that nobody thinks we guarantee that valid comes first. |
||
if (getQueryExplanation() != null && !getQueryExplanation().isEmpty()) { | ||
builder.startArray(EXPLANATIONS_FIELD); | ||
for (QueryExplanation explanation : getQueryExplanation()) { | ||
builder.startObject(); | ||
if (explanation.getIndex() != null) { | ||
builder.field(INDEX_FIELD, explanation.getIndex()); | ||
} | ||
if(explanation.getShard() >= 0) { | ||
builder.field(SHARD_FIELD, explanation.getShard()); | ||
} | ||
builder.field(VALID_FIELD, explanation.isValid()); | ||
if (explanation.getError() != null) { | ||
builder.field(ERROR_FIELD, explanation.getError()); | ||
} | ||
if (explanation.getExplanation() != null) { | ||
builder.field(EXPLANATION_FIELD, explanation.getExplanation()); | ||
} | ||
builder.endObject(); | ||
} | ||
builder.endArray(); | ||
} | ||
builder.endObject(); | ||
return builder; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just figured this flag is never read, we can rather remove it and simplify the rest action. We should also remove it from the request as it does nothing, and adjust the docs. The correct parameter is
details
and notdetailed
, but we don't accept it anymore since we introduced params validation a while ago. The flag doesn't need to be serialized either, as it only affects the xcontent output, is is simply a REST param passed through to the response like we do in other places. Would you like to open a follow-up PR to fix this?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, cool ! of course :) I'll look at it after finishing these PRs, and it's like this it changes a little whlie working on Add Broadcast API 😄
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I opened #28910 to keep track of this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what do you think of removing the flag already from the response given that it does nothing? I think it would simplify the rest action