Skip to content

Commit 71a9cfe

Browse files
authored
Add fs iotime in Nodes Stats API (#67861)
This adds io_time_in_millis to Nodes stats API
1 parent 05baf4c commit 71a9cfe

File tree

6 files changed

+117
-48
lines changed

6 files changed

+117
-48
lines changed

server/src/main/java/org/elasticsearch/monitor/fs/FsInfo.java

+49-2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
package org.elasticsearch.monitor.fs;
1010

11+
import org.elasticsearch.Version;
1112
import org.elasticsearch.common.Nullable;
1213
import org.elasticsearch.common.io.stream.StreamInput;
1314
import org.elasticsearch.common.io.stream.StreamOutput;
@@ -166,6 +167,8 @@ public static class DeviceStats implements Writeable, ToXContentFragment {
166167
final long previousWritesCompleted;
167168
final long currentSectorsWritten;
168169
final long previousSectorsWritten;
170+
final long currentIOTime;
171+
final long previousIOTime;
169172

170173
public DeviceStats(
171174
final int majorDeviceNumber,
@@ -175,6 +178,7 @@ public DeviceStats(
175178
final long currentSectorsRead,
176179
final long currentWritesCompleted,
177180
final long currentSectorsWritten,
181+
final long currentIOTime,
178182
final DeviceStats previousDeviceStats) {
179183
this(
180184
majorDeviceNumber,
@@ -187,7 +191,9 @@ public DeviceStats(
187191
currentSectorsRead,
188192
previousDeviceStats != null ? previousDeviceStats.currentSectorsRead : -1,
189193
currentWritesCompleted,
190-
previousDeviceStats != null ? previousDeviceStats.currentWritesCompleted : -1);
194+
previousDeviceStats != null ? previousDeviceStats.currentWritesCompleted : -1,
195+
currentIOTime,
196+
previousDeviceStats != null ? previousDeviceStats.currentIOTime : -1);
191197
}
192198

193199
private DeviceStats(
@@ -201,7 +207,9 @@ private DeviceStats(
201207
final long currentSectorsRead,
202208
final long previousSectorsRead,
203209
final long currentWritesCompleted,
204-
final long previousWritesCompleted) {
210+
final long previousWritesCompleted,
211+
final long currentIOTime,
212+
final long previousIOTime) {
205213
this.majorDeviceNumber = majorDeviceNumber;
206214
this.minorDeviceNumber = minorDeviceNumber;
207215
this.deviceName = deviceName;
@@ -213,6 +221,8 @@ private DeviceStats(
213221
this.previousSectorsRead = previousSectorsRead;
214222
this.currentSectorsWritten = currentSectorsWritten;
215223
this.previousSectorsWritten = previousSectorsWritten;
224+
this.currentIOTime = currentIOTime;
225+
this.previousIOTime = previousIOTime;
216226
}
217227

218228
public DeviceStats(StreamInput in) throws IOException {
@@ -227,6 +237,13 @@ public DeviceStats(StreamInput in) throws IOException {
227237
previousSectorsRead = in.readLong();
228238
currentSectorsWritten = in.readLong();
229239
previousSectorsWritten = in.readLong();
240+
if (in.getVersion().onOrAfter(Version.V_8_0_0)) {
241+
currentIOTime = in.readLong();
242+
previousIOTime = in.readLong();
243+
} else {
244+
currentIOTime = 0;
245+
previousIOTime = 0;
246+
}
230247
}
231248

232249
@Override
@@ -242,6 +259,10 @@ public void writeTo(StreamOutput out) throws IOException {
242259
out.writeLong(previousSectorsRead);
243260
out.writeLong(currentSectorsWritten);
244261
out.writeLong(previousSectorsWritten);
262+
if (out.getVersion().onOrAfter(Version.V_8_0_0)) {
263+
out.writeLong(currentIOTime);
264+
out.writeLong(previousIOTime);
265+
}
245266
}
246267

247268
public long operations() {
@@ -275,6 +296,12 @@ public long writeKilobytes() {
275296
return (currentSectorsWritten - previousSectorsWritten) / 2;
276297
}
277298

299+
public long ioTimeInMillis() {
300+
if (previousIOTime == -1) return -1;
301+
302+
return (currentIOTime - previousIOTime);
303+
}
304+
278305
@Override
279306
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
280307
builder.field("device_name", deviceName);
@@ -283,6 +310,7 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
283310
builder.field(IoStats.WRITE_OPERATIONS, writeOperations());
284311
builder.field(IoStats.READ_KILOBYTES, readKilobytes());
285312
builder.field(IoStats.WRITE_KILOBYTES, writeKilobytes());
313+
builder.field(IoStats.IO_TIMEMS, ioTimeInMillis());
286314
return builder;
287315
}
288316

@@ -295,13 +323,16 @@ public static class IoStats implements Writeable, ToXContentFragment {
295323
private static final String WRITE_OPERATIONS = "write_operations";
296324
private static final String READ_KILOBYTES = "read_kilobytes";
297325
private static final String WRITE_KILOBYTES = "write_kilobytes";
326+
private static final String IO_TIMEMS = "io_time_in_millis";
327+
298328

299329
final DeviceStats[] devicesStats;
300330
final long totalOperations;
301331
final long totalReadOperations;
302332
final long totalWriteOperations;
303333
final long totalReadKilobytes;
304334
final long totalWriteKilobytes;
335+
final long totalIOTimeInMillis;
305336

306337
public IoStats(final DeviceStats[] devicesStats) {
307338
this.devicesStats = devicesStats;
@@ -310,18 +341,21 @@ public IoStats(final DeviceStats[] devicesStats) {
310341
long totalWriteOperations = 0;
311342
long totalReadKilobytes = 0;
312343
long totalWriteKilobytes = 0;
344+
long totalIOTimeInMillis = 0;
313345
for (DeviceStats deviceStats : devicesStats) {
314346
totalOperations += deviceStats.operations() != -1 ? deviceStats.operations() : 0;
315347
totalReadOperations += deviceStats.readOperations() != -1 ? deviceStats.readOperations() : 0;
316348
totalWriteOperations += deviceStats.writeOperations() != -1 ? deviceStats.writeOperations() : 0;
317349
totalReadKilobytes += deviceStats.readKilobytes() != -1 ? deviceStats.readKilobytes() : 0;
318350
totalWriteKilobytes += deviceStats.writeKilobytes() != -1 ? deviceStats.writeKilobytes() : 0;
351+
totalIOTimeInMillis += deviceStats.ioTimeInMillis() != -1 ? deviceStats.ioTimeInMillis() : 0;
319352
}
320353
this.totalOperations = totalOperations;
321354
this.totalReadOperations = totalReadOperations;
322355
this.totalWriteOperations = totalWriteOperations;
323356
this.totalReadKilobytes = totalReadKilobytes;
324357
this.totalWriteKilobytes = totalWriteKilobytes;
358+
this.totalIOTimeInMillis = totalIOTimeInMillis;
325359
}
326360

327361
public IoStats(StreamInput in) throws IOException {
@@ -336,6 +370,11 @@ public IoStats(StreamInput in) throws IOException {
336370
this.totalWriteOperations = in.readLong();
337371
this.totalReadKilobytes = in.readLong();
338372
this.totalWriteKilobytes = in.readLong();
373+
if (in.getVersion().onOrAfter(Version.V_8_0_0)) {
374+
this.totalIOTimeInMillis = in.readLong();
375+
} else {
376+
this.totalIOTimeInMillis = 0;
377+
}
339378
}
340379

341380
@Override
@@ -349,6 +388,9 @@ public void writeTo(StreamOutput out) throws IOException {
349388
out.writeLong(totalWriteOperations);
350389
out.writeLong(totalReadKilobytes);
351390
out.writeLong(totalWriteKilobytes);
391+
if (out.getVersion().onOrAfter(Version.V_8_0_0)) {
392+
out.writeLong(totalIOTimeInMillis);
393+
}
352394
}
353395

354396
public DeviceStats[] getDevicesStats() {
@@ -375,6 +417,10 @@ public long getTotalWriteKilobytes() {
375417
return totalWriteKilobytes;
376418
}
377419

420+
public long getTotalIOTimeMillis() {
421+
return totalIOTimeInMillis;
422+
}
423+
378424
@Override
379425
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
380426
if (devicesStats.length > 0) {
@@ -392,6 +438,7 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
392438
builder.field(WRITE_OPERATIONS, totalWriteOperations);
393439
builder.field(READ_KILOBYTES, totalReadKilobytes);
394440
builder.field(WRITE_KILOBYTES, totalWriteKilobytes);
441+
builder.field(IO_TIMEMS, totalIOTimeInMillis);
395442
builder.endObject();
396443
}
397444
return builder;

server/src/main/java/org/elasticsearch/monitor/fs/FsProbe.java

+2
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ final FsInfo.IoStats ioStats(final Set<Tuple<Integer, Integer>> devicesNumbers,
8181
final long sectorsRead = Long.parseLong(fields[5]);
8282
final long writesCompleted = Long.parseLong(fields[7]);
8383
final long sectorsWritten = Long.parseLong(fields[9]);
84+
final long ioTime = Long.parseLong(fields[12]);
8485
final FsInfo.DeviceStats deviceStats =
8586
new FsInfo.DeviceStats(
8687
majorDeviceNumber,
@@ -90,6 +91,7 @@ final FsInfo.IoStats ioStats(final Set<Tuple<Integer, Integer>> devicesNumbers,
9091
sectorsRead,
9192
writesCompleted,
9293
sectorsWritten,
94+
ioTime,
9395
deviceMap.get(Tuple.tuple(majorDeviceNumber, minorDeviceNumber)));
9496
devicesStats.add(deviceStats);
9597
}

server/src/test/java/org/elasticsearch/action/admin/cluster/node/stats/NodeStatsTests.java

+6-2
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@ public void testSerialization() throws IOException {
191191
assertEquals(ioStats.getTotalReadOperations(), deserializedIoStats.getTotalReadOperations());
192192
assertEquals(ioStats.getTotalWriteKilobytes(), deserializedIoStats.getTotalWriteKilobytes());
193193
assertEquals(ioStats.getTotalWriteOperations(), deserializedIoStats.getTotalWriteOperations());
194+
assertEquals(ioStats.getTotalIOTimeMillis(), deserializedIoStats.getTotalIOTimeMillis());
194195
assertEquals(ioStats.getDevicesStats().length, deserializedIoStats.getDevicesStats().length);
195196
for (int i = 0; i < ioStats.getDevicesStats().length; i++) {
196197
FsInfo.DeviceStats deviceStats = ioStats.getDevicesStats()[i];
@@ -200,6 +201,7 @@ public void testSerialization() throws IOException {
200201
assertEquals(deviceStats.readOperations(), deserializedDeviceStats.readOperations());
201202
assertEquals(deviceStats.writeKilobytes(), deserializedDeviceStats.writeKilobytes());
202203
assertEquals(deviceStats.writeOperations(), deserializedDeviceStats.writeOperations());
204+
assertEquals(deviceStats.ioTimeInMillis(), deserializedDeviceStats.ioTimeInMillis());
203205
}
204206
}
205207
if (nodeStats.getTransport() == null) {
@@ -414,10 +416,12 @@ public static NodeStats createNodeStats() {
414416
for (int i = 0; i < numDeviceStats; i++) {
415417
FsInfo.DeviceStats previousDeviceStats = randomBoolean() ? null :
416418
new FsInfo.DeviceStats(randomInt(), randomInt(), randomAlphaOfLengthBetween(3, 10),
417-
randomNonNegativeLong(), randomNonNegativeLong(), randomNonNegativeLong(), randomNonNegativeLong(), null);
419+
randomNonNegativeLong(), randomNonNegativeLong(), randomNonNegativeLong(), randomNonNegativeLong(),
420+
randomNonNegativeLong(), null);
418421
deviceStatsArray[i] =
419422
new FsInfo.DeviceStats(randomInt(), randomInt(), randomAlphaOfLengthBetween(3, 10), randomNonNegativeLong(),
420-
randomNonNegativeLong(), randomNonNegativeLong(), randomNonNegativeLong(), previousDeviceStats);
423+
randomNonNegativeLong(), randomNonNegativeLong(), randomNonNegativeLong(), randomNonNegativeLong(),
424+
previousDeviceStats);
421425
}
422426
FsInfo.IoStats ioStats = new FsInfo.IoStats(deviceStatsArray);
423427
int numPaths = randomIntBetween(0, 10);

server/src/test/java/org/elasticsearch/monitor/fs/DeviceStatsTests.java

+4
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ public void testDeviceStats() {
2222
final int sectorsRead = randomIntBetween(8 * readsCompleted, 16 * readsCompleted);
2323
final int writesCompleted = randomIntBetween(1, 1 << 16);
2424
final int sectorsWritten = randomIntBetween(8 * writesCompleted, 16 * writesCompleted);
25+
final int ioTime = randomIntBetween(1, 1 << 16);
2526

2627
FsInfo.DeviceStats previous = new FsInfo.DeviceStats(
2728
majorDeviceNumber,
@@ -31,6 +32,7 @@ public void testDeviceStats() {
3132
sectorsRead,
3233
writesCompleted,
3334
sectorsWritten,
35+
ioTime,
3436
null);
3537
FsInfo.DeviceStats current = new FsInfo.DeviceStats(
3638
majorDeviceNumber,
@@ -40,12 +42,14 @@ public void testDeviceStats() {
4042
sectorsRead + 16384,
4143
writesCompleted + 2048,
4244
sectorsWritten + 32768,
45+
ioTime + 128,
4346
previous);
4447
assertThat(current.operations(), equalTo(1024L + 2048L));
4548
assertThat(current.readOperations(), equalTo(1024L));
4649
assertThat(current.writeOperations(), equalTo(2048L));
4750
assertThat(current.readKilobytes(), equalTo(16384L / 2));
4851
assertThat(current.writeKilobytes(), equalTo(32768L / 2));
52+
assertThat(current.ioTimeInMillis(), equalTo(128L));
4953
}
5054

5155
}

server/src/test/java/org/elasticsearch/monitor/fs/FsProbeTests.java

+11
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ public void testFsInfo() throws IOException {
6060
assertThat(deviceStats.previousWritesCompleted, equalTo(-1L));
6161
assertThat(deviceStats.currentSectorsWritten, greaterThanOrEqualTo(0L));
6262
assertThat(deviceStats.previousSectorsWritten, equalTo(-1L));
63+
assertThat(deviceStats.currentIOTime, greaterThanOrEqualTo(0L));
64+
assertThat(deviceStats.previousIOTime, equalTo(-1L));
6365
}
6466
} else {
6567
assertNull(stats.getIoStats());
@@ -178,6 +180,8 @@ List<String> readProcDiskStats() throws IOException {
178180
assertThat(first.devicesStats[0].previousWritesCompleted, equalTo(-1L));
179181
assertThat(first.devicesStats[0].currentSectorsWritten, equalTo(118857776L));
180182
assertThat(first.devicesStats[0].previousSectorsWritten, equalTo(-1L));
183+
assertThat(first.devicesStats[0].currentIOTime, equalTo(1918440L));
184+
assertThat(first.devicesStats[0].previousIOTime, equalTo(-1L));
181185
assertThat(first.devicesStats[1].majorDeviceNumber, equalTo(253));
182186
assertThat(first.devicesStats[1].minorDeviceNumber, equalTo(2));
183187
assertThat(first.devicesStats[1].deviceName, equalTo("dm-2"));
@@ -189,6 +193,8 @@ List<String> readProcDiskStats() throws IOException {
189193
assertThat(first.devicesStats[1].previousWritesCompleted, equalTo(-1L));
190194
assertThat(first.devicesStats[1].currentSectorsWritten, equalTo(64126096L));
191195
assertThat(first.devicesStats[1].previousSectorsWritten, equalTo(-1L));
196+
assertThat(first.devicesStats[1].currentIOTime, equalTo(1058193L));
197+
assertThat(first.devicesStats[1].previousIOTime, equalTo(-1L));
192198

193199
diskStats.set(Arrays.asList(
194200
" 259 0 nvme0n1 336870 0 7928397 82876 10264393 0 182986405 52451610 0 2971042 52536492",
@@ -213,6 +219,8 @@ List<String> readProcDiskStats() throws IOException {
213219
assertThat(second.devicesStats[0].previousWritesCompleted, equalTo(8398869L));
214220
assertThat(second.devicesStats[0].currentSectorsWritten, equalTo(118857776L));
215221
assertThat(second.devicesStats[0].previousSectorsWritten, equalTo(118857776L));
222+
assertThat(second.devicesStats[0].currentIOTime, equalTo(1918444L));
223+
assertThat(second.devicesStats[0].previousIOTime, equalTo(1918440L));
216224
assertThat(second.devicesStats[1].majorDeviceNumber, equalTo(253));
217225
assertThat(second.devicesStats[1].minorDeviceNumber, equalTo(2));
218226
assertThat(second.devicesStats[1].deviceName, equalTo("dm-2"));
@@ -224,12 +232,15 @@ List<String> readProcDiskStats() throws IOException {
224232
assertThat(second.devicesStats[1].previousWritesCompleted, equalTo(1371977L));
225233
assertThat(second.devicesStats[1].currentSectorsWritten, equalTo(64128568L));
226234
assertThat(second.devicesStats[1].previousSectorsWritten, equalTo(64126096L));
235+
assertThat(second.devicesStats[1].currentIOTime, equalTo(1058347L));
236+
assertThat(second.devicesStats[1].previousIOTime, equalTo(1058193L));
227237

228238
assertThat(second.totalOperations, equalTo(575L));
229239
assertThat(second.totalReadOperations, equalTo(261L));
230240
assertThat(second.totalWriteOperations, equalTo(314L));
231241
assertThat(second.totalReadKilobytes, equalTo(2392L));
232242
assertThat(second.totalWriteKilobytes, equalTo(1236L));
243+
assertThat(second.totalIOTimeInMillis, equalTo(158L));
233244
}
234245

235246
public void testAdjustForHugeFilesystems() throws Exception {

0 commit comments

Comments
 (0)