Skip to content

Commit fca2528

Browse files
andreidaneasyice
andauthored
[7.x] Add fs iotime in Nodes Stats API (#67861) (#72754)
This adds io_time_in_millis to Nodes stats API (cherry picked from commit 71a9cfe) Signed-off-by: Andrei Dan <[email protected]> Co-authored-by: zhangchao <[email protected]>
1 parent 15888c2 commit fca2528

File tree

6 files changed

+246
-175
lines changed

6 files changed

+246
-175
lines changed

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

+48-2
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,8 @@ public static class DeviceStats implements Writeable, ToXContentFragment {
174174
final long previousWritesCompleted;
175175
final long currentSectorsWritten;
176176
final long previousSectorsWritten;
177+
final long currentIOTime;
178+
final long previousIOTime;
177179

178180
public DeviceStats(
179181
final int majorDeviceNumber,
@@ -183,6 +185,7 @@ public DeviceStats(
183185
final long currentSectorsRead,
184186
final long currentWritesCompleted,
185187
final long currentSectorsWritten,
188+
final long currentIOTime,
186189
final DeviceStats previousDeviceStats) {
187190
this(
188191
majorDeviceNumber,
@@ -195,7 +198,9 @@ public DeviceStats(
195198
currentSectorsRead,
196199
previousDeviceStats != null ? previousDeviceStats.currentSectorsRead : -1,
197200
currentWritesCompleted,
198-
previousDeviceStats != null ? previousDeviceStats.currentWritesCompleted : -1);
201+
previousDeviceStats != null ? previousDeviceStats.currentWritesCompleted : -1,
202+
currentIOTime,
203+
previousDeviceStats != null ? previousDeviceStats.currentIOTime : -1);
199204
}
200205

201206
private DeviceStats(
@@ -209,7 +214,9 @@ private DeviceStats(
209214
final long currentSectorsRead,
210215
final long previousSectorsRead,
211216
final long currentWritesCompleted,
212-
final long previousWritesCompleted) {
217+
final long previousWritesCompleted,
218+
final long currentIOTime,
219+
final long previousIOTime) {
213220
this.majorDeviceNumber = majorDeviceNumber;
214221
this.minorDeviceNumber = minorDeviceNumber;
215222
this.deviceName = deviceName;
@@ -221,6 +228,8 @@ private DeviceStats(
221228
this.previousSectorsRead = previousSectorsRead;
222229
this.currentSectorsWritten = currentSectorsWritten;
223230
this.previousSectorsWritten = previousSectorsWritten;
231+
this.currentIOTime = currentIOTime;
232+
this.previousIOTime = previousIOTime;
224233
}
225234

226235
public DeviceStats(StreamInput in) throws IOException {
@@ -235,6 +244,13 @@ public DeviceStats(StreamInput in) throws IOException {
235244
previousSectorsRead = in.readLong();
236245
currentSectorsWritten = in.readLong();
237246
previousSectorsWritten = in.readLong();
247+
if (in.getVersion().onOrAfter(Version.V_7_14_0)) {
248+
currentIOTime = in.readLong();
249+
previousIOTime = in.readLong();
250+
} else {
251+
currentIOTime = -1;
252+
previousIOTime = -1;
253+
}
238254
}
239255

240256
@Override
@@ -250,6 +266,10 @@ public void writeTo(StreamOutput out) throws IOException {
250266
out.writeLong(previousSectorsRead);
251267
out.writeLong(currentSectorsWritten);
252268
out.writeLong(previousSectorsWritten);
269+
if (out.getVersion().onOrAfter(Version.V_7_14_0)) {
270+
out.writeLong(currentIOTime);
271+
out.writeLong(previousIOTime);
272+
}
253273
}
254274

255275
public long operations() {
@@ -283,6 +303,12 @@ public long writeKilobytes() {
283303
return (currentSectorsWritten - previousSectorsWritten) / 2;
284304
}
285305

306+
public long ioTimeInMillis() {
307+
if (previousIOTime == -1) return -1;
308+
309+
return (currentIOTime - previousIOTime);
310+
}
311+
286312
@Override
287313
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
288314
builder.field("device_name", deviceName);
@@ -291,6 +317,7 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
291317
builder.field(IoStats.WRITE_OPERATIONS, writeOperations());
292318
builder.field(IoStats.READ_KILOBYTES, readKilobytes());
293319
builder.field(IoStats.WRITE_KILOBYTES, writeKilobytes());
320+
builder.field(IoStats.IO_TIMEMS, ioTimeInMillis());
294321
return builder;
295322
}
296323

@@ -303,13 +330,16 @@ public static class IoStats implements Writeable, ToXContentFragment {
303330
private static final String WRITE_OPERATIONS = "write_operations";
304331
private static final String READ_KILOBYTES = "read_kilobytes";
305332
private static final String WRITE_KILOBYTES = "write_kilobytes";
333+
private static final String IO_TIMEMS = "io_time_in_millis";
334+
306335

307336
final DeviceStats[] devicesStats;
308337
final long totalOperations;
309338
final long totalReadOperations;
310339
final long totalWriteOperations;
311340
final long totalReadKilobytes;
312341
final long totalWriteKilobytes;
342+
final long totalIOTimeInMillis;
313343

314344
public IoStats(final DeviceStats[] devicesStats) {
315345
this.devicesStats = devicesStats;
@@ -318,18 +348,21 @@ public IoStats(final DeviceStats[] devicesStats) {
318348
long totalWriteOperations = 0;
319349
long totalReadKilobytes = 0;
320350
long totalWriteKilobytes = 0;
351+
long totalIOTimeInMillis = 0;
321352
for (DeviceStats deviceStats : devicesStats) {
322353
totalOperations += deviceStats.operations() != -1 ? deviceStats.operations() : 0;
323354
totalReadOperations += deviceStats.readOperations() != -1 ? deviceStats.readOperations() : 0;
324355
totalWriteOperations += deviceStats.writeOperations() != -1 ? deviceStats.writeOperations() : 0;
325356
totalReadKilobytes += deviceStats.readKilobytes() != -1 ? deviceStats.readKilobytes() : 0;
326357
totalWriteKilobytes += deviceStats.writeKilobytes() != -1 ? deviceStats.writeKilobytes() : 0;
358+
totalIOTimeInMillis += deviceStats.ioTimeInMillis() != -1 ? deviceStats.ioTimeInMillis() : 0;
327359
}
328360
this.totalOperations = totalOperations;
329361
this.totalReadOperations = totalReadOperations;
330362
this.totalWriteOperations = totalWriteOperations;
331363
this.totalReadKilobytes = totalReadKilobytes;
332364
this.totalWriteKilobytes = totalWriteKilobytes;
365+
this.totalIOTimeInMillis = totalIOTimeInMillis;
333366
}
334367

335368
public IoStats(StreamInput in) throws IOException {
@@ -344,6 +377,11 @@ public IoStats(StreamInput in) throws IOException {
344377
this.totalWriteOperations = in.readLong();
345378
this.totalReadKilobytes = in.readLong();
346379
this.totalWriteKilobytes = in.readLong();
380+
if (in.getVersion().onOrAfter(Version.V_7_14_0)) {
381+
this.totalIOTimeInMillis = in.readLong();
382+
} else {
383+
this.totalIOTimeInMillis = -1;
384+
}
347385
}
348386

349387
@Override
@@ -357,6 +395,9 @@ public void writeTo(StreamOutput out) throws IOException {
357395
out.writeLong(totalWriteOperations);
358396
out.writeLong(totalReadKilobytes);
359397
out.writeLong(totalWriteKilobytes);
398+
if (out.getVersion().onOrAfter(Version.V_7_14_0)) {
399+
out.writeLong(totalIOTimeInMillis);
400+
}
360401
}
361402

362403
public DeviceStats[] getDevicesStats() {
@@ -383,6 +424,10 @@ public long getTotalWriteKilobytes() {
383424
return totalWriteKilobytes;
384425
}
385426

427+
public long getTotalIOTimeMillis() {
428+
return totalIOTimeInMillis;
429+
}
430+
386431
@Override
387432
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
388433
if (devicesStats.length > 0) {
@@ -400,6 +445,7 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
400445
builder.field(WRITE_OPERATIONS, totalWriteOperations);
401446
builder.field(READ_KILOBYTES, totalReadKilobytes);
402447
builder.field(WRITE_KILOBYTES, totalWriteKilobytes);
448+
builder.field(IO_TIMEMS, totalIOTimeInMillis);
403449
builder.endObject();
404450
}
405451
return builder;

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

+2
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ final FsInfo.IoStats ioStats(final Set<Tuple<Integer, Integer>> devicesNumbers,
8686
final long sectorsRead = Long.parseLong(fields[5]);
8787
final long writesCompleted = Long.parseLong(fields[7]);
8888
final long sectorsWritten = Long.parseLong(fields[9]);
89+
final long ioTime = Long.parseLong(fields[12]);
8990
final FsInfo.DeviceStats deviceStats =
9091
new FsInfo.DeviceStats(
9192
majorDeviceNumber,
@@ -95,6 +96,7 @@ final FsInfo.IoStats ioStats(final Set<Tuple<Integer, Integer>> devicesNumbers,
9596
sectorsRead,
9697
writesCompleted,
9798
sectorsWritten,
99+
ioTime,
98100
deviceMap.get(Tuple.tuple(majorDeviceNumber, minorDeviceNumber)));
99101
devicesStats.add(deviceStats);
100102
}

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

+6-2
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@ public void testSerialization() throws IOException {
189189
assertEquals(ioStats.getTotalReadOperations(), deserializedIoStats.getTotalReadOperations());
190190
assertEquals(ioStats.getTotalWriteKilobytes(), deserializedIoStats.getTotalWriteKilobytes());
191191
assertEquals(ioStats.getTotalWriteOperations(), deserializedIoStats.getTotalWriteOperations());
192+
assertEquals(ioStats.getTotalIOTimeMillis(), deserializedIoStats.getTotalIOTimeMillis());
192193
assertEquals(ioStats.getDevicesStats().length, deserializedIoStats.getDevicesStats().length);
193194
for (int i = 0; i < ioStats.getDevicesStats().length; i++) {
194195
FsInfo.DeviceStats deviceStats = ioStats.getDevicesStats()[i];
@@ -198,6 +199,7 @@ public void testSerialization() throws IOException {
198199
assertEquals(deviceStats.readOperations(), deserializedDeviceStats.readOperations());
199200
assertEquals(deviceStats.writeKilobytes(), deserializedDeviceStats.writeKilobytes());
200201
assertEquals(deviceStats.writeOperations(), deserializedDeviceStats.writeOperations());
202+
assertEquals(deviceStats.ioTimeInMillis(), deserializedDeviceStats.ioTimeInMillis());
201203
}
202204
}
203205
if (nodeStats.getTransport() == null) {
@@ -416,10 +418,12 @@ public static NodeStats createNodeStats() {
416418
for (int i = 0; i < numDeviceStats; i++) {
417419
FsInfo.DeviceStats previousDeviceStats = randomBoolean() ? null :
418420
new FsInfo.DeviceStats(randomInt(), randomInt(), randomAlphaOfLengthBetween(3, 10),
419-
randomNonNegativeLong(), randomNonNegativeLong(), randomNonNegativeLong(), randomNonNegativeLong(), null);
421+
randomNonNegativeLong(), randomNonNegativeLong(), randomNonNegativeLong(), randomNonNegativeLong(),
422+
randomNonNegativeLong(), null);
420423
deviceStatsArray[i] =
421424
new FsInfo.DeviceStats(randomInt(), randomInt(), randomAlphaOfLengthBetween(3, 10), randomNonNegativeLong(),
422-
randomNonNegativeLong(), randomNonNegativeLong(), randomNonNegativeLong(), previousDeviceStats);
425+
randomNonNegativeLong(), randomNonNegativeLong(), randomNonNegativeLong(), randomNonNegativeLong(),
426+
previousDeviceStats);
423427
}
424428
FsInfo.IoStats ioStats = new FsInfo.IoStats(deviceStatsArray);
425429
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)