Skip to content
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

Expose params to toXContentChunked as well as per-chunk #91771

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ private void assertMasterStability(Client client, HealthStatus expectedStatus, M

private String xContentToString(ChunkedToXContent xContent) throws IOException {
XContentBuilder builder = JsonXContent.contentBuilder();
xContent.toXContentChunked().forEachRemaining(xcontent -> {
xContent.toXContentChunked(ToXContent.EMPTY_PARAMS).forEachRemaining(xcontent -> {
try {
xcontent.toXContent(builder, ToXContent.EMPTY_PARAMS);
} catch (IOException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ public void writeTo(StreamOutput out) throws IOException {
}

@Override
public Iterator<ToXContent> toXContentChunked() {
public Iterator<ToXContent> toXContentChunked(ToXContent.Params params) {
return Iterators.concat(Iterators.single((b, p) -> {
b.startObject();
b.startArray("snapshots");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ public SnapshotStats getStats() {
private static final String INCLUDE_GLOBAL_STATE = "include_global_state";

@Override
public Iterator<? extends ToXContent> toXContentChunked() {
public Iterator<? extends ToXContent> toXContentChunked(ToXContent.Params params) {
return Iterators.concat(Iterators.single((ToXContent) (b, p) -> {
b.startObject()
.field(SNAPSHOT, snapshot.getSnapshotId().getName())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,13 @@ public int hashCode() {
}

@Override
public Iterator<? extends ToXContent> toXContentChunked() {
public Iterator<? extends ToXContent> toXContentChunked(ToXContent.Params params) {
return Iterators.concat(
Iterators.single((ToXContent) (b, p) -> b.startObject().startArray("snapshots")),
snapshots.stream()
.flatMap(s -> StreamSupport.stream(Spliterators.spliteratorUnknownSize(s.toXContentChunked(), Spliterator.ORDERED), false))
.flatMap(
s -> StreamSupport.stream(Spliterators.spliteratorUnknownSize(s.toXContentChunked(params), Spliterator.ORDERED), false)
)
.iterator(),
Iterators.single((b, p) -> b.endArray().endObject())
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public void writeTo(StreamOutput out) throws IOException {
}

@Override
public Iterator<ToXContent> toXContentChunked() {
public Iterator<ToXContent> toXContentChunked(ToXContent.Params outerParams) {
return Iterators.concat(
Iterators.single((b, p) -> b.startObject()),
getMappings().entrySet().stream().map(indexEntry -> (ToXContent) (builder, params) -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public Map<String, List<RecoveryState>> shardRecoveryStates() {
}

@Override
public Iterator<ToXContent> toXContentChunked() {
public Iterator<ToXContent> toXContentChunked(ToXContent.Params params) {
return Iterators.concat(
Iterators.single((b, p) -> b.startObject()),
shardRecoveryStates.entrySet()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public void writeTo(StreamOutput out) throws IOException {
}

@Override
public Iterator<? extends ToXContent> toXContentChunked() {
public Iterator<? extends ToXContent> toXContentChunked(ToXContent.Params outerParams) {
return Iterators.concat(Iterators.single(((builder, params) -> {
builder.startObject();
RestActions.buildBroadcastShardsHeader(builder, params, this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ public String toString() {
}

@Override
public Iterator<? extends ToXContent> toXContentChunked() {
public Iterator<? extends ToXContent> toXContentChunked(ToXContent.Params params) {
final boolean omitEmptySettings = indexToDefaultSettings.isEmpty();
return toXContentChunked(omitEmptySettings);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ private static void writeField(StreamOutput out, Map<String, FieldCapabilities>
}

@Override
public Iterator<? extends ToXContent> toXContentChunked() {
public Iterator<? extends ToXContent> toXContentChunked(ToXContent.Params params) {
if (indexResponses.size() > 0) {
throw new IllegalStateException("cannot serialize non-merged response");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ protected BaseNodesXContentResponse(StreamInput in) throws IOException {
}

@Override
public final Iterator<? extends ToXContent> toXContentChunked() {
public final Iterator<? extends ToXContent> toXContentChunked(ToXContent.Params params) {
return Iterators.concat(Iterators.single((b, p) -> {
b.startObject();
RestActions.buildNodesHeader(b, p, this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public interface ChunkedToXContent {
* {@link ToXContent.Params} for each call until it is fully drained.
* @return iterator over chunks of {@link ToXContent}
*/
Iterator<? extends ToXContent> toXContentChunked();
Iterator<? extends ToXContent> toXContentChunked(ToXContent.Params params);

/**
* Wraps the given instance in a {@link ToXContentObject} that will fully serialize the instance when serialized.
Expand All @@ -35,7 +35,7 @@ public interface ChunkedToXContent {
*/
static ToXContentObject wrapAsXContentObject(ChunkedToXContent chunkedToXContent) {
return (builder, params) -> {
Iterator<? extends ToXContent> serialization = chunkedToXContent.toXContentChunked();
Iterator<? extends ToXContent> serialization = chunkedToXContent.toXContentChunked(params);
while (serialization.hasNext()) {
serialization.next().toXContent(builder, params);
}
Expand Down
11 changes: 8 additions & 3 deletions server/src/main/java/org/elasticsearch/health/Diagnosis.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public Resource(Collection<DiscoveryNode> nodes) {
}

@Override
public Iterator<? extends ToXContent> toXContentChunked() {
public Iterator<? extends ToXContent> toXContentChunked(ToXContent.Params outerParams) {
Iterator<? extends ToXContent> valuesIterator;
if (nodes != null) {
valuesIterator = nodes.stream().map(node -> (ToXContent) (builder, params) -> {
Expand Down Expand Up @@ -147,11 +147,16 @@ public String getUniqueId() {
}

@Override
public Iterator<? extends ToXContent> toXContentChunked() {
public Iterator<? extends ToXContent> toXContentChunked(ToXContent.Params outerParams) {
Iterator<? extends ToXContent> resourcesIterator = Collections.emptyIterator();
if (affectedResources != null && affectedResources.size() > 0) {
resourcesIterator = affectedResources.stream()
.flatMap(s -> StreamSupport.stream(Spliterators.spliteratorUnknownSize(s.toXContentChunked(), Spliterator.ORDERED), false))
.flatMap(
s -> StreamSupport.stream(
Spliterators.spliteratorUnknownSize(s.toXContentChunked(outerParams), Spliterator.ORDERED),
false
)
)
.iterator();
}
return Iterators.concat(Iterators.single((ToXContent) (builder, params) -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public void writeTo(StreamOutput out) throws IOException {

@Override
@SuppressWarnings("unchecked")
public Iterator<? extends ToXContent> toXContentChunked() {
public Iterator<? extends ToXContent> toXContentChunked(ToXContent.Params outerParams) {
return Iterators.concat(Iterators.single((ToXContent) (builder, params) -> {
builder.startObject();
if (status != null) {
Expand All @@ -111,7 +111,7 @@ public Iterator<? extends ToXContent> toXContentChunked() {
// indicators however the affected resources which are the O(indices) fields are
// flat mapped over all diagnoses within the indicator
Iterators.single((ToXContent) (builder, params) -> builder.field(indicator.name())),
indicator.toXContentChunked()
indicator.toXContentChunked(outerParams)
)
)
.toArray(Iterator[]::new)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,16 @@ public record HealthIndicatorResult(
List<Diagnosis> diagnosisList
) implements ChunkedToXContent {
@Override
public Iterator<? extends ToXContent> toXContentChunked() {
public Iterator<? extends ToXContent> toXContentChunked(ToXContent.Params outerParams) {
Iterator<? extends ToXContent> diagnosisIterator = Collections.emptyIterator();
if (diagnosisList != null && diagnosisList.isEmpty() == false) {
diagnosisIterator = diagnosisList.stream()
.flatMap(s -> StreamSupport.stream(Spliterators.spliteratorUnknownSize(s.toXContentChunked(), Spliterator.ORDERED), false))
.flatMap(
s -> StreamSupport.stream(
Spliterators.spliteratorUnknownSize(s.toXContentChunked(outerParams), Spliterator.ORDERED),
false
)
)
.iterator();
}
return Iterators.concat(Iterators.single((ToXContent) (builder, params) -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public void write(byte[] b, int off, int len) throws IOException {
Streams.noCloseStream(out)
);

private final Iterator<? extends ToXContent> serialization = chunkedToXContent.toXContentChunked();
private final Iterator<? extends ToXContent> serialization = chunkedToXContent.toXContentChunked(params);

private BytesStream target;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@

import static org.elasticsearch.snapshots.SnapshotInfo.INDEX_DETAILS_XCONTENT_PARAM;
import static org.elasticsearch.test.AbstractXContentTestCase.chunkedXContentTester;
import static org.elasticsearch.xcontent.ToXContent.EMPTY_PARAMS;
import static org.hamcrest.CoreMatchers.containsString;

public class GetSnapshotsResponseTests extends ESTestCase {
Expand Down Expand Up @@ -179,7 +180,7 @@ public void testFromXContent() throws IOException {

public void testToChunkedXContent() {
final GetSnapshotsResponse response = createTestInstance();
final Iterator<ToXContent> serialization = response.toXContentChunked();
final Iterator<ToXContent> serialization = response.toXContentChunked(EMPTY_PARAMS);
int chunks = 0;
while (serialization.hasNext()) {
serialization.next();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
import java.util.List;
import java.util.function.Predicate;

import static org.elasticsearch.xcontent.ToXContent.EMPTY_PARAMS;

public class SnapshotsStatusResponseTests extends AbstractChunkedSerializingTestCase<SnapshotsStatusResponse> {

@Override
Expand Down Expand Up @@ -58,7 +60,7 @@ public void testChunkCount() {
// open and close chunk + one chunk per index
chunksExpected += 2 + snapshot.getIndices().size();
}
final var iterator = instance.toXContentChunked();
final var iterator = instance.toXContentChunked(EMPTY_PARAMS);
int chunksSeen = 0;
while (iterator.hasNext()) {
iterator.next();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import java.util.stream.Collectors;
import java.util.stream.IntStream;

import static org.elasticsearch.xcontent.ToXContent.EMPTY_PARAMS;

public class GetMappingsResponseTests extends AbstractWireSerializingTestCase<GetMappingsResponse> {

public void testCheckEqualsAndHashCode() {
Expand Down Expand Up @@ -73,7 +75,7 @@ public void testChunkedXContentUsesChunkPerIndex() {
.mapToObj(i -> "index-" + i)
.collect(Collectors.toUnmodifiableMap(Function.identity(), k -> createMappingsForIndex()))
);
final var chunks = response.toXContentChunked();
final var chunks = response.toXContentChunked(EMPTY_PARAMS);
int chunkCount = 0;
while (chunks.hasNext()) {
chunks.next();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

import static java.util.Collections.emptyMap;
import static java.util.Collections.emptySet;
import static org.elasticsearch.xcontent.ToXContent.EMPTY_PARAMS;

public class RecoveryResponseTests extends ESTestCase {

Expand Down Expand Up @@ -57,7 +58,7 @@ public void testChunkedToXContent() {
),
List.of()
);
final var iterator = recoveryResponse.toXContentChunked();
final var iterator = recoveryResponse.toXContentChunked(EMPTY_PARAMS);
int chunks = 0;
while (iterator.hasNext()) {
iterator.next();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.util.Collections;
import java.util.List;

import static org.elasticsearch.xcontent.ToXContent.EMPTY_PARAMS;
import static org.elasticsearch.xcontent.XContentFactory.jsonBuilder;

public class IndicesSegmentResponseTests extends ESTestCase {
Expand All @@ -42,7 +43,7 @@ public void testToXContentSerialiationWithSortedFields() throws Exception {
0,
Collections.emptyList()
);
var serialization = response.toXContentChunked();
var serialization = response.toXContentChunked(EMPTY_PARAMS);
try (XContentBuilder builder = jsonBuilder()) {
while (serialization.hasNext()) {
serialization.next().toXContent(builder, ToXContent.EMPTY_PARAMS);
Expand All @@ -68,7 +69,7 @@ public void testSerializesOneChunkPerIndex() {
Collections.emptyList()
);
int chunks = 0;
final var iterator = response.toXContentChunked();
final var iterator = response.toXContentChunked(EMPTY_PARAMS);
while (iterator.hasNext()) {
iterator.next();
chunks++;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import java.util.Set;
import java.util.function.Predicate;

import static org.elasticsearch.xcontent.ToXContent.EMPTY_PARAMS;

public class GetSettingsResponseTests extends AbstractChunkedSerializingTestCase<GetSettingsResponse> {

@Override
Expand Down Expand Up @@ -76,7 +78,7 @@ protected Predicate<String> getRandomFieldsExcludeFilter() {

public void testOneChunkPerIndex() {
final var instance = createTestInstance();
final var iterator = instance.toXContentChunked();
final var iterator = instance.toXContentChunked(EMPTY_PARAMS);
int chunks = 0;
while (iterator.hasNext()) {
chunks++;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import java.util.Map;
import java.util.function.Predicate;

import static org.elasticsearch.xcontent.ToXContent.EMPTY_PARAMS;

public class MergedFieldCapabilitiesResponseTests extends AbstractChunkedSerializingTestCase<FieldCapabilitiesResponse> {

@Override
Expand Down Expand Up @@ -207,7 +209,7 @@ private static FieldCapabilitiesResponse createSimpleResponse() {
public void testExpectedChunkSizes() {
{
final FieldCapabilitiesResponse instance = FieldCapabilitiesResponseTests.createResponseWithFailures();
final var iterator = instance.toXContentChunked();
final var iterator = instance.toXContentChunked(EMPTY_PARAMS);
int chunks = 0;
while (iterator.hasNext()) {
iterator.next();
Expand All @@ -221,7 +223,7 @@ public void testExpectedChunkSizes() {
}
{
final FieldCapabilitiesResponse instance = createTestInstance();
final var iterator = instance.toXContentChunked();
final var iterator = instance.toXContentChunked(EMPTY_PARAMS);
int chunks = 0;
while (iterator.hasNext()) {
iterator.next();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import org.elasticsearch.common.xcontent.XContentHelper;
import org.elasticsearch.health.GetHealthAction.Response;
import org.elasticsearch.test.ESTestCase;
import org.elasticsearch.xcontent.ToXContent;
import org.elasticsearch.xcontent.XContentBuilder;
import org.elasticsearch.xcontent.XContentFactory;

Expand All @@ -24,6 +23,7 @@
import java.util.Locale;
import java.util.Map;

import static org.elasticsearch.xcontent.ToXContent.EMPTY_PARAMS;
import static org.hamcrest.Matchers.is;

public class GetHealthResponseTests extends ESTestCase {
Expand All @@ -35,9 +35,9 @@ public void testToXContent() throws IOException {
Response response = new Response(ClusterName.DEFAULT, indicatorResults, true);

XContentBuilder builder = XContentFactory.jsonBuilder().prettyPrint();
response.toXContentChunked().forEachRemaining(xcontent -> {
response.toXContentChunked(EMPTY_PARAMS).forEachRemaining(xcontent -> {
try {
xcontent.toXContent(builder, ToXContent.EMPTY_PARAMS);
xcontent.toXContent(builder, EMPTY_PARAMS);
} catch (IOException e) {
logger.error(e.getMessage(), e);
fail(e.getMessage());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public void testToXContent() throws Exception {
HealthIndicatorResult result = new HealthIndicatorResult(name, status, symptom, details, impacts, diagnosisList);
XContentBuilder builder = XContentFactory.jsonBuilder().prettyPrint();

result.toXContentChunked().forEachRemaining(xcontent -> {
result.toXContentChunked(ToXContent.EMPTY_PARAMS).forEachRemaining(xcontent -> {
try {
xcontent.toXContent(builder, ToXContent.EMPTY_PARAMS);
} catch (IOException e) {
Expand Down Expand Up @@ -106,7 +106,7 @@ public void testToXContent() throws Exception {

if (diagnosis1.affectedResources() != null) {
XContentBuilder diagnosisXContent = XContentFactory.jsonBuilder().prettyPrint();
diagnosis1.toXContentChunked().forEachRemaining(xcontent -> {
diagnosis1.toXContentChunked(ToXContent.EMPTY_PARAMS).forEachRemaining(xcontent -> {
try {
xcontent.toXContent(diagnosisXContent, ToXContent.EMPTY_PARAMS);
} catch (IOException e) {
Expand All @@ -131,7 +131,7 @@ public void testToXContent() throws Exception {
expectedDiagnosis2.put("help_url", diagnosis2.definition().helpURL());
if (diagnosis2.affectedResources() != null) {
XContentBuilder diagnosisXContent = XContentFactory.jsonBuilder().prettyPrint();
diagnosis2.toXContentChunked().forEachRemaining(xcontent -> {
diagnosis2.toXContentChunked(ToXContent.EMPTY_PARAMS).forEachRemaining(xcontent -> {
try {
xcontent.toXContent(diagnosisXContent, ToXContent.EMPTY_PARAMS);
} catch (IOException e) {
Expand Down Expand Up @@ -199,7 +199,7 @@ public void testChunkCount() {
// -> each Diagnosis yields 5 chunks => 10 chunks from both diagnosis
// -> HealthIndicatorResult surrounds the diagnosis list by 2 chunks
int chunksExpected = 12;
var iterator = result.toXContentChunked();
var iterator = result.toXContentChunked(ToXContent.EMPTY_PARAMS);
int chunksSeen = 0;
while (iterator.hasNext()) {
iterator.next();
Expand Down
Loading