Skip to content

Chunk profiling stacktrace response #96340

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

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
5 changes: 5 additions & 0 deletions docs/changelog/96340.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pr: 96340
summary: Chunk profiling stacktrace response
area: Application
type: enhancement
issues: []
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@

package org.elasticsearch.xpack.profiler;

import org.elasticsearch.rest.RestStatus;

import java.util.List;

public class GetProfilingActionIT extends ProfilingTestCase {
Expand All @@ -20,7 +18,6 @@ protected boolean useOnlyAllEvents() {
public void testGetProfilingDataUnfiltered() throws Exception {
GetProfilingRequest request = new GetProfilingRequest(1, null);
GetProfilingResponse response = client().execute(GetProfilingAction.INSTANCE, request).get();
assertEquals(RestStatus.OK, response.status());
assertEquals(1, response.getTotalFrames());
assertNotNull(response.getStackTraces());
StackTrace stackTrace = response.getStackTraces().get("QjoLteG7HX3VUUXr-J4kHQ");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,23 @@
package org.elasticsearch.xpack.profiler;

import org.elasticsearch.ElasticsearchException;
import org.elasticsearch.ExceptionsHelper;
import org.elasticsearch.action.ActionResponse;
import org.elasticsearch.common.collect.Iterators;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.xcontent.StatusToXContentObject;
import org.elasticsearch.common.xcontent.ChunkedToXContentHelper;
import org.elasticsearch.common.xcontent.ChunkedToXContentObject;
import org.elasticsearch.core.Nullable;
import org.elasticsearch.rest.RestStatus;
import org.elasticsearch.xcontent.XContentBuilder;
import org.elasticsearch.xcontent.ToXContent;

import java.io.IOException;
import java.util.Collections;
import java.util.Iterator;
import java.util.Map;
import java.util.Objects;
import java.util.function.BiFunction;

import static org.elasticsearch.rest.RestStatus.OK;

public class GetProfilingResponse extends ActionResponse implements StatusToXContentObject {
public class GetProfilingResponse extends ActionResponse implements ChunkedToXContentObject {
@Nullable
private final Map<String, StackTrace> stackTraces;
@Nullable
Expand Down Expand Up @@ -139,11 +140,6 @@ public void writeTo(StreamOutput out) throws IOException {
}
}

@Override
public RestStatus status() {
return error != null ? ExceptionsHelper.status(ExceptionsHelper.unwrapCause(error)) : OK;
}

public Map<String, StackTrace> getStackTraces() {
return stackTraces;
}
Expand All @@ -169,36 +165,32 @@ public Exception getError() {
}

@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject();
if (stackTraces != null) {
builder.startObject("stack_traces");
builder.mapContents(stackTraces);
builder.endObject();
}
if (stackFrames != null) {
builder.startObject("stack_frames");
builder.mapContents(stackFrames);
builder.endObject();
}
if (executables != null) {
builder.startObject("executables");
builder.mapContents(executables);
builder.endObject();
}
if (stackTraceEvents != null) {
builder.startObject("stack_trace_events");
builder.mapContents(stackTraceEvents);
builder.endObject();
}
builder.field("total_frames", totalFrames);
public Iterator<? extends ToXContent> toXContentChunked(ToXContent.Params params) {
if (error != null) {
builder.startObject("error");
ElasticsearchException.generateThrowableXContent(builder, params, error);
builder.endObject();
return Iterators.concat(
ChunkedToXContentHelper.startObject(),
Iterators.single((b, p) -> ElasticsearchException.generateFailureXContent(b, params, error, true)),
ChunkedToXContentHelper.endObject()
);
} else {
return Iterators.concat(
ChunkedToXContentHelper.startObject(),
optional("stack_traces", stackTraces, ChunkedToXContentHelper::xContentValuesMap),
optional("stack_frames", stackFrames, ChunkedToXContentHelper::xContentValuesMap),
optional("executables", executables, ChunkedToXContentHelper::map),
optional("stack_trace_events", stackTraceEvents, ChunkedToXContentHelper::map),
Iterators.single((b, p) -> b.field("total_frames", totalFrames)),
ChunkedToXContentHelper.endObject()
);
}
builder.endObject();
return builder;
}

private <T> Iterator<? extends ToXContent> optional(
String name,
Map<String, T> values,
BiFunction<String, Map<String, T>, Iterator<? extends ToXContent>> supplier
) {
return (values != null) ? supplier.apply(name, values) : Collections.emptyIterator();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@
import org.elasticsearch.client.internal.node.NodeClient;
import org.elasticsearch.rest.BaseRestHandler;
import org.elasticsearch.rest.RestRequest;
import org.elasticsearch.rest.action.RestActionListener;
import org.elasticsearch.rest.action.RestCancellableNodeClient;
import org.elasticsearch.rest.action.RestStatusToXContentListener;
import org.elasticsearch.rest.action.RestChunkedToXContentListener;

import java.io.IOException;
import java.util.List;
Expand All @@ -30,7 +31,7 @@ protected RestChannelConsumer prepareRequest(RestRequest request, NodeClient cli
request.applyContentParser(getProfilingRequest::parseXContent);

return channel -> {
RestStatusToXContentListener<GetProfilingResponse> listener = new RestStatusToXContentListener<>(channel);
RestActionListener<GetProfilingResponse> listener = new RestChunkedToXContentListener<>(channel);
RestCancellableNodeClient cancelClient = new RestCancellableNodeClient(client, request.getHttpChannel());
cancelClient.execute(GetProfilingAction.INSTANCE, getProfilingRequest, listener);
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,13 @@
package org.elasticsearch.xpack.profiler;

import org.elasticsearch.common.io.stream.Writeable;
import org.elasticsearch.test.AbstractChunkedSerializingTestCase;
import org.elasticsearch.test.AbstractWireSerializingTestCase;

import java.util.List;
import java.util.Map;
import java.util.function.Supplier;

public class GetProfilingResponseTests extends AbstractWireSerializingTestCase<GetProfilingResponse> {
private <T> T randomNullable(Supplier<T> v) {
return randomBoolean() ? v.get() : null;
}

private <T> T randomNullable(T v) {
return randomBoolean() ? v : null;
}
Expand Down Expand Up @@ -60,4 +56,21 @@ protected GetProfilingResponse mutateInstance(GetProfilingResponse instance) {
protected Writeable.Reader<GetProfilingResponse> instanceReader() {
return GetProfilingResponse::new;
}

public void testChunking() {
AbstractChunkedSerializingTestCase.assertChunkCount(createTestInstance(), instance -> {
// start, end, total_frames
int chunks = 3;
chunks += size(instance.getExecutables());
chunks += size(instance.getStackFrames());
chunks += size(instance.getStackTraces());
chunks += size(instance.getStackTraceEvents());
return chunks;
});
}

private int size(Map<?, ?> m) {
// if there is a map, we also need to take into account start and end object
return m != null ? 2 + m.size() : 0;
}
}