From c610b603310fe16a78e0cb40fec41d46d82dc487 Mon Sep 17 00:00:00 2001 From: Colin Patrick Mccabe Date: Mon, 20 Jun 2016 10:43:07 -0700 Subject: [PATCH] =?UTF-8?q?HADOOP-13280.=20FileSystemStorageStatistics#get?= =?UTF-8?q?Long(=E2=80=9CreadOps=E2=80=9C)=20should=20return=20readOps=20+?= =?UTF-8?q?=20largeReadOps=20(Mingliang=20Liu=20via=20cmccabe)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit (cherry picked from commit 5370a6ffaec5227c0978f10c86a5811155271933) (cherry picked from commit 7db1e57cc160a902a688db7068b6e692d8263a65) Conflicts: hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/fs/TestFileSystemStorageStatistics.java --- .../fs/FileSystemStorageStatistics.java | 2 +- .../fs/TestFileSystemStorageStatistics.java | 118 ++++++++++++++++++ 2 files changed, 119 insertions(+), 1 deletion(-) create mode 100644 hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/fs/TestFileSystemStorageStatistics.java diff --git a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/FileSystemStorageStatistics.java b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/FileSystemStorageStatistics.java index c0b4409791f..98cb70aa1b2 100644 --- a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/FileSystemStorageStatistics.java +++ b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/FileSystemStorageStatistics.java @@ -83,7 +83,7 @@ public class FileSystemStorageStatistics extends StorageStatistics { case "bytesWritten": return data.getBytesWritten(); case "readOps": - return Long.valueOf(data.getReadOps()); + return (long) (data.getReadOps() + data.getLargeReadOps()); case "largeReadOps": return Long.valueOf(data.getLargeReadOps()); case "writeOps": diff --git a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/fs/TestFileSystemStorageStatistics.java b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/fs/TestFileSystemStorageStatistics.java new file mode 100644 index 00000000000..10ace083079 --- /dev/null +++ b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/fs/TestFileSystemStorageStatistics.java @@ -0,0 +1,118 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.hadoop.fs; + +import org.apache.commons.lang.math.RandomUtils; +import org.apache.hadoop.fs.StorageStatistics.LongStatistic; + +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; + +import org.junit.rules.ExpectedException; +import org.junit.rules.Timeout; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.Iterator; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +/** + * This tests basic operations of {@link FileSystemStorageStatistics} class. + */ +public class TestFileSystemStorageStatistics { + private static final Logger LOG = LoggerFactory.getLogger( + TestFileSystemStorageStatistics.class); + private static final String FS_STORAGE_STATISTICS_NAME = "test-fs-statistics"; + private static final String[] STATISTICS_KEYS = { + "bytesRead", + "bytesWritten", + "readOps", + "largeReadOps", + "writeOps" + }; + + private FileSystem.Statistics statistics = + new FileSystem.Statistics("test-scheme"); + private FileSystemStorageStatistics storageStatistics = + new FileSystemStorageStatistics(FS_STORAGE_STATISTICS_NAME, statistics); + + @Rule + public final Timeout globalTimeout = new Timeout(10 * 1000); + @Rule + public final ExpectedException exception = ExpectedException.none(); + + @Before + public void setup() { + statistics.incrementBytesRead(RandomUtils.nextInt(100)); + statistics.incrementBytesWritten(RandomUtils.nextInt(100)); + statistics.incrementLargeReadOps(RandomUtils.nextInt(100)); + statistics.incrementWriteOps(RandomUtils.nextInt(100)); + } + + @Test + public void testgetLongStatistics() { + Iterator iter = storageStatistics.getLongStatistics(); + while (iter.hasNext()) { + final LongStatistic longStat = iter.next(); + assertNotNull(longStat); + final long expectedStat = getStatisticsValue(longStat.getName()); + LOG.info("{}: FileSystem.Statistics={}, FileSystemStorageStatistics={}", + longStat.getName(), expectedStat, longStat.getValue()); + assertEquals(expectedStat, longStat.getValue()); + } + } + + @Test + public void testGetLong() { + for (String key : STATISTICS_KEYS) { + final long expectedStat = getStatisticsValue(key); + final long storageStat = storageStatistics.getLong(key); + LOG.info("{}: FileSystem.Statistics={}, FileSystemStorageStatistics={}", + key, expectedStat, storageStat); + assertEquals(expectedStat, storageStat); + } + } + + /** + * Helper method to retrieve the specific FileSystem.Statistics value by name. + * + * Basically, the {@link FileSystemStorageStatistics} should do this + * internally in a similar approach. + */ + private long getStatisticsValue(String name) { + switch (name) { + case "bytesRead": + return statistics.getBytesRead(); + case "bytesWritten": + return statistics.getBytesWritten(); + case "readOps": + return statistics.getReadOps(); + case "largeReadOps": + return statistics.getLargeReadOps(); + case "writeOps": + return statistics.getWriteOps(); + default: + return 0; + } + } + +}