From b7c4cf7129768c0312b186dfb94ba1beb891e2f3 Mon Sep 17 00:00:00 2001 From: Jitendra Pandey Date: Mon, 20 Jun 2016 16:00:13 -0700 Subject: [PATCH] HADOOP-13291. Probing stats in DFSOpsCountStatistics/S3AStorageStatistics should be correctly implemented. Contributed by Mingliang Liu. --- .../hadoop/hdfs/DFSOpsCountStatistics.java | 5 +- .../hdfs/TestDFSOpsCountStatistics.java | 108 ++++++++++++++++++ .../hadoop/fs/s3a/S3AStorageStatistics.java | 5 +- 3 files changed, 114 insertions(+), 4 deletions(-) create mode 100644 hadoop-hdfs-project/hadoop-hdfs-client/src/test/java/org/apache/hadoop/hdfs/TestDFSOpsCountStatistics.java diff --git a/hadoop-hdfs-project/hadoop-hdfs-client/src/main/java/org/apache/hadoop/hdfs/DFSOpsCountStatistics.java b/hadoop-hdfs-project/hadoop-hdfs-client/src/main/java/org/apache/hadoop/hdfs/DFSOpsCountStatistics.java index d58a59f0c0..a047d34084 100644 --- a/hadoop-hdfs-project/hadoop-hdfs-client/src/main/java/org/apache/hadoop/hdfs/DFSOpsCountStatistics.java +++ b/hadoop-hdfs-project/hadoop-hdfs-client/src/main/java/org/apache/hadoop/hdfs/DFSOpsCountStatistics.java @@ -139,7 +139,8 @@ public LongStatistic next() { throw new NoSuchElementException(); } final Entry entry = iterator.next(); - return new LongStatistic(entry.getKey().name(), entry.getValue().get()); + return new LongStatistic(entry.getKey().getSymbol(), + entry.getValue().get()); } @Override @@ -161,7 +162,7 @@ public Long getLong(String key) { @Override public boolean isTracked(String key) { - return OpType.fromSymbol(key) == null; + return OpType.fromSymbol(key) != null; } } diff --git a/hadoop-hdfs-project/hadoop-hdfs-client/src/test/java/org/apache/hadoop/hdfs/TestDFSOpsCountStatistics.java b/hadoop-hdfs-project/hadoop-hdfs-client/src/test/java/org/apache/hadoop/hdfs/TestDFSOpsCountStatistics.java new file mode 100644 index 0000000000..7c1b018ae9 --- /dev/null +++ b/hadoop-hdfs-project/hadoop-hdfs-client/src/test/java/org/apache/hadoop/hdfs/TestDFSOpsCountStatistics.java @@ -0,0 +1,108 @@ +/** + * 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.hdfs; + +import org.apache.commons.lang3.RandomUtils; +import org.apache.hadoop.fs.StorageStatistics.LongStatistic; + +import org.apache.hadoop.hdfs.DFSOpsCountStatistics.OpType; + +import org.junit.BeforeClass; +import org.junit.Rule; +import org.junit.Test; + +import org.junit.rules.ExpectedException; +import org.junit.rules.Timeout; + +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; + +/** + * This tests basic operations of {@link DFSOpsCountStatistics} class. + */ +public class TestDFSOpsCountStatistics { + + private static final DFSOpsCountStatistics STORAGE_STATISTICS = + new DFSOpsCountStatistics(); + private static final Map OP_COUNTER_MAP = new HashMap<>(); + private static final String NO_SUCH_OP = "no-such-dfs-operation-dude"; + + @Rule + public final Timeout globalTimeout = new Timeout(10 * 1000); + @Rule + public final ExpectedException exception = ExpectedException.none(); + + @BeforeClass + public static void setup() { + for (OpType opType : OpType.values()) { + final Long opCount = RandomUtils.nextLong(0, 100); + OP_COUNTER_MAP.put(opType.getSymbol(), opCount); + for (long i = 0; i < opCount; i++) { + STORAGE_STATISTICS.incrementOpCounter(opType); + } + } + } + + @Test + public void testGetLongStatistics() { + short iterations = 0; // number of the iter.hasNext() + final Iterator iter = STORAGE_STATISTICS.getLongStatistics(); + + while (iter.hasNext()) { + final LongStatistic longStat = iter.next(); + assertNotNull(longStat); + assertTrue(OP_COUNTER_MAP.containsKey(longStat.getName())); + assertEquals(OP_COUNTER_MAP.get(longStat.getName()).longValue(), + longStat.getValue()); + iterations++; + } + + // check that all the OpType enum entries are iterated via iter + assertEquals(OpType.values().length, iterations); + } + + @Test + public void testGetLong() { + assertNull(STORAGE_STATISTICS.getLong(NO_SUCH_OP)); + + for (OpType opType : OpType.values()) { + final String key = opType.getSymbol(); + assertEquals(OP_COUNTER_MAP.get(key), STORAGE_STATISTICS.getLong(key)); + } + } + + @Test + public void testIsTracked() { + assertFalse(STORAGE_STATISTICS.isTracked(NO_SUCH_OP)); + + final Iterator iter = STORAGE_STATISTICS.getLongStatistics(); + while (iter.hasNext()) { + final LongStatistic longStatistic = iter.next(); + assertTrue(STORAGE_STATISTICS.isTracked(longStatistic.getName())); + } + } + +} diff --git a/hadoop-tools/hadoop-aws/src/main/java/org/apache/hadoop/fs/s3a/S3AStorageStatistics.java b/hadoop-tools/hadoop-aws/src/main/java/org/apache/hadoop/fs/s3a/S3AStorageStatistics.java index f69159a0f5..a74b8648c1 100644 --- a/hadoop-tools/hadoop-aws/src/main/java/org/apache/hadoop/fs/s3a/S3AStorageStatistics.java +++ b/hadoop-tools/hadoop-aws/src/main/java/org/apache/hadoop/fs/s3a/S3AStorageStatistics.java @@ -76,7 +76,8 @@ public LongStatistic next() { throw new NoSuchElementException(); } final Map.Entry entry = iterator.next(); - return new LongStatistic(entry.getKey().name(), entry.getValue().get()); + return new LongStatistic(entry.getKey().getSymbol(), + entry.getValue().get()); } @Override @@ -98,7 +99,7 @@ public Long getLong(String key) { @Override public boolean isTracked(String key) { - return Statistic.fromSymbol(key) == null; + return Statistic.fromSymbol(key) != null; } }