From 536d13ff1131cb2a11aff7d0f7a1c3a84f4e7265 Mon Sep 17 00:00:00 2001 From: javanna Date: Thu, 1 Sep 2016 18:41:04 +0200 Subject: [PATCH] ProcessInfo to implement Writeable rather than Streamable --- .../admin/cluster/node/info/NodeInfo.java | 2 +- .../monitor/process/ProcessInfo.java | 51 ++++++++----------- .../monitor/process/ProcessProbe.java | 4 +- .../monitor/process/ProcessService.java | 4 +- .../monitor/os/OsProbeTests.java | 8 +-- .../monitor/process/ProcessProbeTests.java | 11 ++-- .../nodesinfo/NodeInfoStreamingTests.java | 2 +- .../index/reindex/RoundTripTests.java | 8 --- .../org/elasticsearch/test/ESTestCase.java | 8 +++ 9 files changed, 41 insertions(+), 57 deletions(-) diff --git a/core/src/main/java/org/elasticsearch/action/admin/cluster/node/info/NodeInfo.java b/core/src/main/java/org/elasticsearch/action/admin/cluster/node/info/NodeInfo.java index d697236472f..ef229bb34bd 100644 --- a/core/src/main/java/org/elasticsearch/action/admin/cluster/node/info/NodeInfo.java +++ b/core/src/main/java/org/elasticsearch/action/admin/cluster/node/info/NodeInfo.java @@ -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); diff --git a/core/src/main/java/org/elasticsearch/monitor/process/ProcessInfo.java b/core/src/main/java/org/elasticsearch/monitor/process/ProcessInfo.java index cf9c9e63b87..a0e3e7a70f2 100644 --- a/core/src/main/java/org/elasticsearch/monitor/process/ProcessInfo.java +++ b/core/src/main/java/org/elasticsearch/monitor/process/ProcessInfo.java @@ -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); - } } diff --git a/core/src/main/java/org/elasticsearch/monitor/process/ProcessProbe.java b/core/src/main/java/org/elasticsearch/monitor/process/ProcessProbe.java index b19b54a9478..e4307f724c5 100644 --- a/core/src/main/java/org/elasticsearch/monitor/process/ProcessProbe.java +++ b/core/src/main/java/org/elasticsearch/monitor/process/ProcessProbe.java @@ -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() { diff --git a/core/src/main/java/org/elasticsearch/monitor/process/ProcessService.java b/core/src/main/java/org/elasticsearch/monitor/process/ProcessService.java index 38593534480..99593003b34 100644 --- a/core/src/main/java/org/elasticsearch/monitor/process/ProcessService.java +++ b/core/src/main/java/org/elasticsearch/monitor/process/ProcessService.java @@ -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); } diff --git a/core/src/test/java/org/elasticsearch/monitor/os/OsProbeTests.java b/core/src/test/java/org/elasticsearch/monitor/os/OsProbeTests.java index ea15487a3b4..2674f726428 100644 --- a/core/src/test/java/org/elasticsearch/monitor/os/OsProbeTests.java +++ b/core/src/test/java/org/elasticsearch/monitor/os/OsProbeTests.java @@ -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); diff --git a/core/src/test/java/org/elasticsearch/monitor/process/ProcessProbeTests.java b/core/src/test/java/org/elasticsearch/monitor/process/ProcessProbeTests.java index 8e6016f6f98..5423242ccd8 100644 --- a/core/src/test/java/org/elasticsearch/monitor/process/ProcessProbeTests.java +++ b/core/src/test/java/org/elasticsearch/monitor/process/ProcessProbeTests.java @@ -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() { diff --git a/core/src/test/java/org/elasticsearch/nodesinfo/NodeInfoStreamingTests.java b/core/src/test/java/org/elasticsearch/nodesinfo/NodeInfoStreamingTests.java index 7cd4e355218..7a3f8706dad 100644 --- a/core/src/test/java/org/elasticsearch/nodesinfo/NodeInfoStreamingTests.java +++ b/core/src/test/java/org/elasticsearch/nodesinfo/NodeInfoStreamingTests.java @@ -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 threadPoolInfos = new ArrayList<>(); threadPoolInfos.add(new ThreadPool.Info("test_threadpool", ThreadPool.ThreadPoolType.FIXED, 5)); diff --git a/modules/reindex/src/test/java/org/elasticsearch/index/reindex/RoundTripTests.java b/modules/reindex/src/test/java/org/elasticsearch/index/reindex/RoundTripTests.java index b0fc9b428ba..1a262a32d3d 100644 --- a/modules/reindex/src/test/java/org/elasticsearch/index/reindex/RoundTripTests.java +++ b/modules/reindex/src/test/java/org/elasticsearch/index/reindex/RoundTripTests.java @@ -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()); diff --git a/test/framework/src/main/java/org/elasticsearch/test/ESTestCase.java b/test/framework/src/main/java/org/elasticsearch/test/ESTestCase.java index b0575d74817..f8098420d54 100644 --- a/test/framework/src/main/java/org/elasticsearch/test/ESTestCase.java +++ b/test/framework/src/main/java/org/elasticsearch/test/ESTestCase.java @@ -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(); }