Skip to content

Commit

Permalink
Use reflect to read HDFS ReadStatistics
Browse files Browse the repository at this point in the history
  • Loading branch information
cxzl25 committed Dec 12, 2022
1 parent ed53549 commit 2defbab
Showing 1 changed file with 26 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.concurrent.atomic.AtomicInteger;
import org.apache.hadoop.fs.CanUnbuffer;
import org.apache.hadoop.fs.FSDataInputStream;
Expand Down Expand Up @@ -244,14 +246,30 @@ private void updateInputStreamStatistics(FSDataInputStream stream) {
*/
HdfsDataInputStream hdfsDataInputStream = (HdfsDataInputStream) stream;
synchronized (readStatistics) {
readStatistics.totalBytesRead +=
hdfsDataInputStream.getReadStatistics().getTotalBytesRead();
readStatistics.totalLocalBytesRead +=
hdfsDataInputStream.getReadStatistics().getTotalLocalBytesRead();
readStatistics.totalShortCircuitBytesRead +=
hdfsDataInputStream.getReadStatistics().getTotalShortCircuitBytesRead();
readStatistics.totalZeroCopyBytesRead +=
hdfsDataInputStream.getReadStatistics().getTotalZeroCopyBytesRead();
try {
Method getReadStatisticsMethod =
hdfsDataInputStream.getClass().getMethod("getReadStatistics");
Object getReadStatisticsObj = getReadStatisticsMethod.invoke(hdfsDataInputStream);
Method getTotalBytesReadMethod =
getReadStatisticsObj.getClass().getMethod("getTotalBytesRead");
Method getTotalLocalBytesReadMethod =
getReadStatisticsObj.getClass().getMethod("getTotalLocalBytesRead");
Method getTotalShortCircuitBytesReadMethod =
getReadStatisticsObj.getClass().getMethod("getTotalShortCircuitBytesRead");
Method getTotalZeroCopyBytesReadMethod =
getReadStatisticsObj.getClass().getMethod("getTotalZeroCopyBytesRead");

readStatistics.totalBytesRead +=
(long) getTotalBytesReadMethod.invoke(getReadStatisticsObj);
readStatistics.totalLocalBytesRead +=
(long) getTotalLocalBytesReadMethod.invoke(getReadStatisticsObj);
readStatistics.totalShortCircuitBytesRead +=
(long) getTotalShortCircuitBytesReadMethod.invoke(getReadStatisticsObj);
readStatistics.totalZeroCopyBytesRead +=
(long) getTotalZeroCopyBytesReadMethod.invoke(getReadStatisticsObj);
} catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) {
throw new RuntimeException(e);
}
}
}
}
Expand Down

0 comments on commit 2defbab

Please sign in to comment.