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

[TEST] Improve HotThreadsRequest BWC test (#77818) #77886

Merged
merged 1 commit into from
Sep 16, 2021
Merged
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 @@ -8,77 +8,61 @@

package org.elasticsearch.action.admin.cluster.hotthreads;

import org.elasticsearch.Version;
import org.elasticsearch.action.admin.cluster.node.hotthreads.NodesHotThreadsRequest;
import org.elasticsearch.action.support.nodes.BaseNodesRequest;
import org.elasticsearch.common.io.stream.BytesStreamOutput;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.core.TimeValue;
import org.elasticsearch.monitor.jvm.HotThreads;
import org.elasticsearch.test.ESTestCase;
import org.elasticsearch.test.VersionUtils;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

public class NodesHotThreadsRequestTests extends ESTestCase {

/** Simple override of BaseNodesRequest to ensure we read the
* common fields of the nodes request.
*/
static class NodesHotThreadsRequestHelper extends BaseNodesRequest<NodesHotThreadsRequestHelper> {
NodesHotThreadsRequestHelper(StreamInput in) throws IOException {
super(in);
}

NodesHotThreadsRequestHelper(String... nodesIds) {
super(nodesIds);
}

@Override
public void writeTo(StreamOutput out) throws IOException {
super.writeTo(out);
}
}

public void testBWCSerialization() throws IOException {
BytesStreamOutput out = new BytesStreamOutput();

TimeValue sampleInterval = new TimeValue(50, TimeUnit.MINUTES);

NodesHotThreadsRequestHelper outHelper = new NodesHotThreadsRequestHelper("123");

outHelper.writeTo(out);
// Write manually some values that differ from the defaults
// in NodesHotThreadsRequest
out.writeInt(4); // threads
out.writeBoolean(false); // ignoreIdleThreads
out.writeString("block"); // type
out.writeTimeValue(sampleInterval); // interval
out.writeInt(3); // snapshots

NodesHotThreadsRequest inRequest = new NodesHotThreadsRequest(out.bytes().streamInput());

assertEquals(4, inRequest.threads());
assertFalse(inRequest.ignoreIdleThreads());
assertEquals(HotThreads.ReportType.BLOCK, inRequest.type());
assertEquals(sampleInterval, inRequest.interval());
assertEquals(3, inRequest.snapshots());

// Change the report type enum
inRequest.type(HotThreads.ReportType.WAIT);

BytesStreamOutput writeOut = new BytesStreamOutput();
inRequest.writeTo(writeOut);

StreamInput whatWeWrote = writeOut.bytes().streamInput();
NodesHotThreadsRequest request = new NodesHotThreadsRequest("123");
request.threads(4);
request.ignoreIdleThreads(false);
request.type(HotThreads.ReportType.BLOCK);
request.interval(sampleInterval);
request.snapshots(3);

Version latest = Version.CURRENT;
Version previous = VersionUtils.randomVersionBetween(random(),
VersionUtils.getFirstVersion(), VersionUtils.getPreviousVersion(Version.CURRENT));

try (BytesStreamOutput out = new BytesStreamOutput()) {
out.setVersion(latest);
request.writeTo(out);
try (StreamInput in = out.bytes().streamInput()) {
in.setVersion(previous);
NodesHotThreadsRequest deserialized = new NodesHotThreadsRequest(in);
assertEquals(request.threads(), deserialized.threads());
assertEquals(request.ignoreIdleThreads(), deserialized.ignoreIdleThreads());
assertEquals(request.type(), deserialized.type());
assertEquals(request.interval(), deserialized.interval());
assertEquals(request.snapshots(), deserialized.snapshots());

}
}

// We construct the helper to read the common serialized fields from the in.
new NodesHotThreadsRequestHelper(whatWeWrote);
// Make sure we serialized in the following format
assertEquals(4, whatWeWrote.readInt());
assertFalse(whatWeWrote.readBoolean());
assertEquals("wait", whatWeWrote.readString()); // lowercase enum value, not label
assertEquals(sampleInterval, whatWeWrote.readTimeValue());
assertEquals(3, whatWeWrote.readInt());
try (BytesStreamOutput out = new BytesStreamOutput()) {
out.setVersion(previous);
request.writeTo(out);
try (StreamInput in = out.bytes().streamInput()) {
in.setVersion(latest);
NodesHotThreadsRequest deserialized = new NodesHotThreadsRequest(in);
assertEquals(request.threads(), deserialized.threads());
assertEquals(request.ignoreIdleThreads(), deserialized.ignoreIdleThreads());
assertEquals(request.type(), deserialized.type());
assertEquals(request.interval(), deserialized.interval());
assertEquals(request.snapshots(), deserialized.snapshots());
}
}
}
}