1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements.  See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership.  The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License.  You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11 
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 package org.apache.hadoop.hdfs.server.namenode.metrics;
19 
20 import static org.apache.hadoop.test.MetricsAsserts.assertCounter;
21 import static org.apache.hadoop.test.MetricsAsserts.getMetrics;
22 
23 import java.io.IOException;
24 import java.util.Random;
25 
26 import org.apache.hadoop.conf.Configuration;
27 import org.apache.hadoop.fs.Path;
28 import org.apache.hadoop.hdfs.DFSConfigKeys;
29 import org.apache.hadoop.hdfs.DFSTestUtil;
30 import org.apache.hadoop.hdfs.DistributedFileSystem;
31 import org.apache.hadoop.hdfs.HdfsConfiguration;
32 import org.apache.hadoop.hdfs.MiniDFSCluster;
33 import org.apache.hadoop.hdfs.protocol.HdfsFileStatus;
34 import org.junit.After;
35 import org.junit.Before;
36 import org.junit.Test;
37 
38 /**
39  * Test case for FilesInGetListingOps metric in Namenode
40  */
41 public class TestNNMetricFilesInGetListingOps {
42   private static final Configuration CONF = new HdfsConfiguration();
43   private static final String NN_METRICS = "NameNodeActivity";
44   static {
CONF.setLong(DFSConfigKeys.DFS_BLOCK_SIZE_KEY, 100)45     CONF.setLong(DFSConfigKeys.DFS_BLOCK_SIZE_KEY, 100);
CONF.setInt(DFSConfigKeys.DFS_BYTES_PER_CHECKSUM_KEY, 1)46     CONF.setInt(DFSConfigKeys.DFS_BYTES_PER_CHECKSUM_KEY, 1);
CONF.setLong(DFSConfigKeys.DFS_HEARTBEAT_INTERVAL_KEY, 1L)47     CONF.setLong(DFSConfigKeys.DFS_HEARTBEAT_INTERVAL_KEY, 1L);
CONF.setInt(DFSConfigKeys.DFS_NAMENODE_REPLICATION_INTERVAL_KEY, 1)48     CONF.setInt(DFSConfigKeys.DFS_NAMENODE_REPLICATION_INTERVAL_KEY, 1);
49   }
50 
51   private MiniDFSCluster cluster;
52   private DistributedFileSystem fs;
53   private final Random rand = new Random();
54 
55   @Before
setUp()56   public void setUp() throws Exception {
57     cluster = new MiniDFSCluster.Builder(CONF).build();
58     cluster.waitActive();
59     cluster.getNameNode();
60     fs = cluster.getFileSystem();
61   }
62 
63   @After
tearDown()64   public void tearDown() throws Exception {
65     cluster.shutdown();
66   }
67 
68   /** create a file with a length of <code>fileLen</code> */
createFile(String fileName, long fileLen, short replicas)69   private void createFile(String fileName, long fileLen, short replicas) throws IOException {
70     Path filePath = new Path(fileName);
71     DFSTestUtil.createFile(fs, filePath, fileLen, replicas, rand.nextLong());
72   }
73 
74 
75   @Test
testFilesInGetListingOps()76   public void testFilesInGetListingOps() throws Exception {
77     createFile("/tmp1/t1", 3200, (short)3);
78     createFile("/tmp1/t2", 3200, (short)3);
79     createFile("/tmp2/t1", 3200, (short)3);
80     createFile("/tmp2/t2", 3200, (short)3);
81     cluster.getNameNodeRpc().getListing("/tmp1", HdfsFileStatus.EMPTY_NAME, false);
82     assertCounter("FilesInGetListingOps", 2L, getMetrics(NN_METRICS));
83     cluster.getNameNodeRpc().getListing("/tmp2", HdfsFileStatus.EMPTY_NAME, false);
84     assertCounter("FilesInGetListingOps", 4L, getMetrics(NN_METRICS));
85   }
86 }
87 
88