Use a list for JvmStats memoryPools rather than an array

This commit is contained in:
javanna 2016-09-06 15:55:03 +02:00 committed by Luca Cavanna
parent 42f88406ee
commit a035ca102f
2 changed files with 11 additions and 11 deletions

View File

@ -19,7 +19,7 @@
package org.elasticsearch.monitor.jvm;
import org.elasticsearch.common.inject.internal.Nullable;
import org.elasticsearch.common.Nullable;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.io.stream.Writeable;
@ -40,6 +40,7 @@ import java.lang.management.RuntimeMXBean;
import java.lang.management.ThreadMXBean;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.TimeUnit;
@ -88,8 +89,7 @@ public class JvmStats implements Writeable, ToXContent {
* we just omit the pool in that case!*/
}
}
MemoryPool[] memoryPools = pools.toArray(new MemoryPool[pools.size()]);
Mem mem = new Mem(heapCommitted, heapUsed, heapMax, nonHeapCommitted, nonHeapUsed, memoryPools);
Mem mem = new Mem(heapCommitted, heapUsed, heapMax, nonHeapCommitted, nonHeapUsed, Collections.unmodifiableList(pools));
Threads threads = new Threads(threadMXBean.getThreadCount(), threadMXBean.getPeakThreadCount());
List<GarbageCollectorMXBean> gcMxBeans = ManagementFactory.getGarbageCollectorMXBeans();
@ -474,9 +474,9 @@ public class JvmStats implements Writeable, ToXContent {
private final long heapMax;
private final long nonHeapCommitted;
private final long nonHeapUsed;
private final MemoryPool[] pools;
private final List<MemoryPool> pools;
public Mem(long heapCommitted, long heapUsed, long heapMax, long nonHeapCommitted, long nonHeapUsed, MemoryPool[] pools) {
public Mem(long heapCommitted, long heapUsed, long heapMax, long nonHeapCommitted, long nonHeapUsed, List<MemoryPool> pools) {
this.heapCommitted = heapCommitted;
this.heapUsed = heapUsed;
this.heapMax = heapMax;
@ -491,7 +491,7 @@ public class JvmStats implements Writeable, ToXContent {
nonHeapCommitted = in.readVLong();
nonHeapUsed = in.readVLong();
heapMax = in.readVLong();
pools = in.readArray(MemoryPool::new, MemoryPool[]::new);
pools = in.readList(MemoryPool::new);
}
@Override
@ -501,12 +501,12 @@ public class JvmStats implements Writeable, ToXContent {
out.writeVLong(nonHeapCommitted);
out.writeVLong(nonHeapUsed);
out.writeVLong(heapMax);
out.writeArray(pools);
out.writeList(pools);
}
@Override
public Iterator<MemoryPool> iterator() {
return Arrays.stream(pools).iterator();
return pools.iterator();
}
public ByteSizeValue getHeapCommitted() {

View File

@ -280,10 +280,10 @@ public class NodeStatsTests extends ESTestCase {
JvmStats jvmStats = null;
if (frequently()) {
int numMemoryPools = randomIntBetween(0, 10);
JvmStats.MemoryPool[] memoryPools = new JvmStats.MemoryPool[numMemoryPools];
List<JvmStats.MemoryPool> memoryPools = new ArrayList<>(numMemoryPools);
for (int i = 0; i < numMemoryPools; i++) {
memoryPools[i] = new JvmStats.MemoryPool(randomAsciiOfLengthBetween(3, 10), randomPositiveLong(),
randomPositiveLong(), randomPositiveLong(), randomPositiveLong());
memoryPools.add(new JvmStats.MemoryPool(randomAsciiOfLengthBetween(3, 10), randomPositiveLong(),
randomPositiveLong(), randomPositiveLong(), randomPositiveLong()));
}
JvmStats.Threads threads = new JvmStats.Threads(randomIntBetween(1, 1000), randomIntBetween(1, 1000));
int numGarbageCollectors = randomIntBetween(0, 10);