ProcessInfo to implement Writeable rather than Streamable

This commit is contained in:
javanna 2016-09-01 18:41:04 +02:00 committed by Luca Cavanna
parent 2370c25fa4
commit 536d13ff11
9 changed files with 41 additions and 57 deletions

View File

@ -205,7 +205,7 @@ public class NodeInfo extends BaseNodeResponse {
os = new OsInfo(in);
}
if (in.readBoolean()) {
process = ProcessInfo.readProcessInfo(in);
process = new ProcessInfo(in);
}
if (in.readBoolean()) {
jvm = new JvmInfo(in);

View File

@ -21,26 +21,35 @@ package org.elasticsearch.monitor.process;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.io.stream.Streamable;
import org.elasticsearch.common.io.stream.Writeable;
import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.XContentBuilder;
import java.io.IOException;
public class ProcessInfo implements Streamable, ToXContent {
public class ProcessInfo implements Writeable, ToXContent {
long refreshInterval;
private final long refreshInterval;
private final long id;
private final boolean mlockall;
private long id;
private boolean mlockall;
ProcessInfo() {
}
public ProcessInfo(long id, boolean mlockall) {
public ProcessInfo(long id, boolean mlockall, long refreshInterval) {
this.id = id;
this.mlockall = mlockall;
this.refreshInterval = refreshInterval;
}
public ProcessInfo(StreamInput in) throws IOException {
refreshInterval = in.readLong();
id = in.readLong();
mlockall = in.readBoolean();
}
@Override
public void writeTo(StreamOutput out) throws IOException {
out.writeLong(refreshInterval);
out.writeLong(id);
out.writeBoolean(mlockall);
}
public long refreshInterval() {
@ -79,24 +88,4 @@ public class ProcessInfo implements Streamable, ToXContent {
builder.endObject();
return builder;
}
public static ProcessInfo readProcessInfo(StreamInput in) throws IOException {
ProcessInfo info = new ProcessInfo();
info.readFrom(in);
return info;
}
@Override
public void readFrom(StreamInput in) throws IOException {
refreshInterval = in.readLong();
id = in.readLong();
mlockall = in.readBoolean();
}
@Override
public void writeTo(StreamOutput out) throws IOException {
out.writeLong(refreshInterval);
out.writeLong(id);
out.writeBoolean(mlockall);
}
}

View File

@ -126,8 +126,8 @@ public class ProcessProbe {
return -1;
}
public ProcessInfo processInfo() {
return new ProcessInfo(jvmInfo().pid(), BootstrapInfo.isMemoryLocked());
public ProcessInfo processInfo(long refreshInterval) {
return new ProcessInfo(jvmInfo().pid(), BootstrapInfo.isMemoryLocked(), refreshInterval);
}
public ProcessStats processStats() {

View File

@ -42,11 +42,9 @@ public final class ProcessService extends AbstractComponent {
public ProcessService(Settings settings) {
super(settings);
this.probe = ProcessProbe.getInstance();
final TimeValue refreshInterval = REFRESH_INTERVAL_SETTING.get(settings);
processStatsCache = new ProcessStatsCache(refreshInterval, probe.processStats());
this.info = probe.processInfo();
this.info.refreshInterval = refreshInterval.millis();
this.info = probe.processInfo(refreshInterval.millis());
logger.debug("using refresh_interval [{}]", refreshInterval);
}

View File

@ -32,7 +32,7 @@ import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.lessThanOrEqualTo;
public class OsProbeTests extends ESTestCase {
private OsProbe probe = OsProbe.getInstance();
private final OsProbe probe = OsProbe.getInstance();
public void testOsInfo() {
int allocatedProcessors = randomIntBetween(1, Runtime.getRuntime().availableProcessors());
@ -40,11 +40,7 @@ public class OsProbeTests extends ESTestCase {
if (randomBoolean()) {
refreshInterval = -1;
} else {
refreshInterval = randomLong();
while (refreshInterval == Long.MIN_VALUE) {
refreshInterval = randomLong();
}
refreshInterval = Math.abs(refreshInterval);
refreshInterval = randomPositiveLong();
}
OsInfo info = probe.osInfo(refreshInterval, allocatedProcessors);
assertNotNull(info);

View File

@ -33,14 +33,15 @@ import static org.hamcrest.Matchers.lessThan;
import static org.hamcrest.Matchers.lessThanOrEqualTo;
public class ProcessProbeTests extends ESTestCase {
ProcessProbe probe = ProcessProbe.getInstance();
private final ProcessProbe probe = ProcessProbe.getInstance();
public void testProcessInfo() {
ProcessInfo info = probe.processInfo();
long refreshInterval = randomPositiveLong();
ProcessInfo info = probe.processInfo(refreshInterval);
assertNotNull(info);
assertThat(info.getRefreshInterval(), greaterThanOrEqualTo(0L));
assertThat(info.getId(), equalTo(jvmInfo().pid()));
assertThat(info.isMlockall(), equalTo(BootstrapInfo.isMemoryLocked()));
assertEquals(refreshInterval, info.getRefreshInterval());
assertEquals(jvmInfo().pid(), info.getId());
assertEquals(BootstrapInfo.isMemoryLocked(), info.isMlockall());
}
public void testProcessStats() {

View File

@ -128,7 +128,7 @@ public class NodeInfoStreamingTests extends ESTestCase {
serviceAttributes.put("test", "attribute");
Settings settings = Settings.builder().put("test", "setting").build();
OsInfo osInfo = DummyOsInfo.INSTANCE;
ProcessInfo process = new ProcessInfo(randomInt(), randomBoolean());
ProcessInfo process = new ProcessInfo(randomInt(), randomBoolean(), randomPositiveLong());
JvmInfo jvm = JvmInfo.jvmInfo();
List<ThreadPool.Info> threadPoolInfos = new ArrayList<>();
threadPoolInfos.add(new ThreadPool.Info("test_threadpool", ThreadPool.ThreadPoolType.FIXED, 5));

View File

@ -206,14 +206,6 @@ public class RoundTripTests extends ESTestCase {
emptyMap()); // Params
}
private long randomPositiveLong() {
long l;
do {
l = randomLong();
} while (l < 0);
return l;
}
private void assertResponseEquals(BulkIndexByScrollResponse expected, BulkIndexByScrollResponse actual) {
assertEquals(expected.getTook(), actual.getTook());
assertTaskStatusEquals(expected.getStatus(), actual.getStatus());

View File

@ -302,6 +302,14 @@ public abstract class ESTestCase extends LuceneTestCase {
return random().nextInt();
}
public static long randomPositiveLong() {
long positiveLong = randomLong();
while (positiveLong == Long.MIN_VALUE) {
positiveLong = randomLong();
}
return Math.abs(positiveLong);
}
public static float randomFloat() {
return random().nextFloat();
}