chia7712 commented on code in PR #22454:
URL: https://github.com/apache/kafka/pull/22454#discussion_r3459952737
##########
server/src/main/java/org/apache/kafka/server/metrics/LinuxIoMetricsCollector.java:
##########
@@ -79,32 +141,61 @@ public long writeBytes() {
* read_bytes: 0
* write_bytes: 0
* cancelled_write_bytes: 0
+ *
+ * <p>Parses into local variables first, then publishes to the volatile
cached fields only
+ * after parsing succeeds. This prevents lock-free readers from observing
the transient
+ * -1L state during an update. {@code lastUpdateMs} is written last so any
thread observing
+ * the new timestamp via a volatile read also observes all updated cached
fields.
+ *
+ * <p>Caller must hold the monitor of this instance.
*/
private boolean updateValues(long now) {
- synchronized (this) {
- try {
- cachedReadBytes = -1L;
- cachedWriteBytes = -1L;
- List<String> lines = Files.readAllLines(path,
StandardCharsets.UTF_8);
- for (String line : lines) {
- if (line.startsWith(READ_BYTES_PREFIX)) {
- cachedReadBytes =
Long.parseLong(line.substring(READ_BYTES_PREFIX.length()));
- } else if (line.startsWith(WRITE_BYTES_PREFIX)) {
- cachedWriteBytes =
Long.parseLong(line.substring(WRITE_BYTES_PREFIX.length()));
- }
+ long readBytes = -1L;
+ long writeBytes = -1L;
+ long rchar = -1L;
+ long wchar = -1L;
+ long syscr = -1L;
+ long syscw = -1L;
+ long cancelledWriteBytes = -1L;
+ try {
+ List<String> lines = Files.readAllLines(path,
StandardCharsets.UTF_8);
+ for (String line : lines) {
+ if (line.startsWith(READ_BYTES_PREFIX)) {
+ readBytes =
Long.parseLong(line.substring(READ_BYTES_PREFIX.length()));
+ } else if (line.startsWith(WRITE_BYTES_PREFIX)) {
+ writeBytes =
Long.parseLong(line.substring(WRITE_BYTES_PREFIX.length()));
+ } else if (line.startsWith(RCHAR_PREFIX)) {
+ rchar =
Long.parseLong(line.substring(RCHAR_PREFIX.length()));
+ } else if (line.startsWith(WCHAR_PREFIX)) {
+ wchar =
Long.parseLong(line.substring(WCHAR_PREFIX.length()));
+ } else if (line.startsWith(SYSCR_PREFIX)) {
+ syscr =
Long.parseLong(line.substring(SYSCR_PREFIX.length()));
+ } else if (line.startsWith(SYSCW_PREFIX)) {
+ syscw =
Long.parseLong(line.substring(SYSCW_PREFIX.length()));
+ } else if (line.startsWith(CANCELLED_WRITE_BYTES_PREFIX)) {
+ cancelledWriteBytes =
Long.parseLong(line.substring(CANCELLED_WRITE_BYTES_PREFIX.length()));
}
- lastUpdateMs = now;
- return true;
- } catch (Throwable t) {
- LOG.warn("Unable to update IO metrics", t);
- return false;
}
+ } catch (Throwable t) {
+ LOG.warn("Unable to update IO metrics", t);
Review Comment:
Should we update `lastUpdateMs` here to avoid flooding the log?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]