Skip to content

Commit a8be99b

Browse files
committed
Node Stats: Process / Network / Os should only refresh every 5 seconds (refresh_interval to set it), closes elastic#626.
1 parent 38d10d1 commit a8be99b

File tree

3 files changed

+59
-11
lines changed

3 files changed

+59
-11
lines changed

modules/elasticsearch/src/main/java/org/elasticsearch/monitor/network/NetworkService.java

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
import org.elasticsearch.common.component.AbstractComponent;
2323
import org.elasticsearch.common.inject.Inject;
2424
import org.elasticsearch.common.settings.Settings;
25+
import org.elasticsearch.common.unit.TimeValue;
26+
import org.elasticsearch.timer.TimerService;
2527

2628
import java.net.InetAddress;
2729
import java.net.NetworkInterface;
@@ -32,15 +34,27 @@
3234
*/
3335
public class NetworkService extends AbstractComponent {
3436

37+
private final TimerService timerService;
38+
3539
private final NetworkProbe probe;
3640

3741
private final NetworkInfo info;
3842

39-
@Inject public NetworkService(Settings settings, NetworkProbe probe) {
43+
private final TimeValue refreshInterval;
44+
45+
private NetworkStats cachedStats;
46+
47+
@Inject public NetworkService(Settings settings, NetworkProbe probe, TimerService timerService) {
4048
super(settings);
4149
this.probe = probe;
50+
this.timerService = timerService;
51+
52+
this.refreshInterval = componentSettings.getAsTime("refresh_interval", TimeValue.timeValueSeconds(5));
53+
54+
logger.debug("Using probe [{}] with refresh_interval [{}]", probe, refreshInterval);
4255

4356
this.info = probe.networkInfo();
57+
this.cachedStats = probe.networkStats();
4458

4559
if (logger.isDebugEnabled()) {
4660
StringBuilder netDebug = new StringBuilder("net_info");
@@ -77,8 +91,11 @@ public NetworkInfo info() {
7791
return this.info;
7892
}
7993

80-
public NetworkStats stats() {
81-
return probe.networkStats();
94+
public synchronized NetworkStats stats() {
95+
if ((timerService.estimatedTimeInMillis() - cachedStats.timestamp()) > refreshInterval.millis()) {
96+
cachedStats = probe.networkStats();
97+
}
98+
return cachedStats;
8299
}
83100

84101
public String ifconfig() {

modules/elasticsearch/src/main/java/org/elasticsearch/monitor/os/OsService.java

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,30 +22,45 @@
2222
import org.elasticsearch.common.component.AbstractComponent;
2323
import org.elasticsearch.common.inject.Inject;
2424
import org.elasticsearch.common.settings.Settings;
25+
import org.elasticsearch.common.unit.TimeValue;
26+
import org.elasticsearch.timer.TimerService;
2527

2628
/**
2729
* @author kimchy (shay.banon)
2830
*/
2931
public class OsService extends AbstractComponent {
3032

33+
private final TimerService timerService;
34+
3135
private final OsProbe probe;
3236

3337
private final OsInfo info;
3438

35-
@Inject public OsService(Settings settings, OsProbe probe) {
39+
private final TimeValue refreshInterval;
40+
41+
private OsStats cachedStats;
42+
43+
@Inject public OsService(Settings settings, OsProbe probe, TimerService timerService) {
3644
super(settings);
3745
this.probe = probe;
46+
this.timerService = timerService;
3847

3948
this.info = probe.osInfo();
4049

41-
logger.trace("Using probe [{}]", probe);
50+
this.refreshInterval = componentSettings.getAsTime("refresh_interval", TimeValue.timeValueSeconds(5));
51+
this.cachedStats = probe.osStats();
52+
53+
logger.debug("Using probe [{}] with refresh_interval [{}]", probe, refreshInterval);
4254
}
4355

4456
public OsInfo info() {
4557
return this.info;
4658
}
4759

48-
public OsStats stats() {
49-
return probe.osStats();
60+
public synchronized OsStats stats() {
61+
if ((timerService.estimatedTimeInMillis() - cachedStats.timestamp()) > refreshInterval.millis()) {
62+
cachedStats = probe.osStats();
63+
}
64+
return cachedStats;
5065
}
5166
}

modules/elasticsearch/src/main/java/org/elasticsearch/monitor/process/ProcessService.java

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,29 +22,45 @@
2222
import org.elasticsearch.common.component.AbstractComponent;
2323
import org.elasticsearch.common.inject.Inject;
2424
import org.elasticsearch.common.settings.Settings;
25+
import org.elasticsearch.common.unit.TimeValue;
26+
import org.elasticsearch.timer.TimerService;
2527

2628
/**
2729
* @author kimchy (shay.banon)
2830
*/
2931
public class ProcessService extends AbstractComponent {
3032

33+
private final TimerService timerService;
34+
3135
private final ProcessProbe probe;
3236

3337
private final ProcessInfo info;
3438

35-
@Inject public ProcessService(Settings settings, ProcessProbe probe) {
39+
private final TimeValue refreshInterval;
40+
41+
private ProcessStats cachedStats;
42+
43+
@Inject public ProcessService(Settings settings, ProcessProbe probe, TimerService timerService) {
3644
super(settings);
45+
this.timerService = timerService;
3746
this.probe = probe;
47+
48+
this.refreshInterval = componentSettings.getAsTime("refresh_interval", TimeValue.timeValueSeconds(5));
49+
3850
this.info = probe.processInfo();
51+
this.cachedStats = probe.processStats();
3952

40-
logger.trace("Using probe [{}]", probe);
53+
logger.debug("Using probe [{}] with refresh_interval [{}]", probe, refreshInterval);
4154
}
4255

4356
public ProcessInfo info() {
4457
return this.info;
4558
}
4659

47-
public ProcessStats stats() {
48-
return probe.processStats();
60+
public synchronized ProcessStats stats() {
61+
if ((timerService.estimatedTimeInMillis() - cachedStats.timestamp()) > refreshInterval.millis()) {
62+
cachedStats = probe.processStats();
63+
}
64+
return cachedStats;
4965
}
5066
}

0 commit comments

Comments
 (0)