View Javadoc

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  
19  package org.apache.hadoop.hbase.regionserver;
20  
21  import java.security.PrivilegedAction;
22  
23  import org.apache.hadoop.conf.Configuration;
24  import org.apache.hadoop.hbase.CompatibilityFactory;
25  import org.apache.hadoop.hbase.HBaseConfiguration;
26  import org.apache.hadoop.hbase.security.User;
27  import org.apache.hadoop.hbase.test.MetricsAssertHelper;
28  import org.apache.hadoop.hbase.testclassification.SmallTests;
29  import org.junit.Before;
30  import org.junit.BeforeClass;
31  import org.junit.Test;
32  import org.junit.experimental.categories.Category;
33  
34  @Category({SmallTests.class})
35  public class TestMetricsUserAggregate {
36  
37    public static MetricsAssertHelper HELPER =
38        CompatibilityFactory.getInstance(MetricsAssertHelper.class);
39  
40    private MetricsRegionServerWrapperStub wrapper;
41    private MetricsRegionServer rsm;
42    private MetricsUserAggregate userAgg;
43  
44    @BeforeClass
45    public static void classSetUp() {
46      HELPER.init();
47    }
48  
49    @Before
50    public void setUp() {
51      wrapper = new MetricsRegionServerWrapperStub();
52      rsm = new MetricsRegionServer(HBaseConfiguration.create(), wrapper, null);
53      userAgg = rsm.getMetricsUserAggregate();
54    }
55  
56    private void doOperations() {
57      for (int i=0; i < 10; i ++) {
58        rsm.updateGet(null, 10);
59      }
60      for (int i=0; i < 11; i ++) {
61        rsm.updateScanTime(null, 11);
62      }
63      for (int i=0; i < 12; i ++) {
64        rsm.updatePut(null, 12);
65      }
66      for (int i=0; i < 13; i ++) {
67        rsm.updateDelete(null, 13);
68      }
69      for (int i=0; i < 14; i ++) {
70        rsm.updateIncrement(null, 14);
71      }
72      for (int i=0; i < 15; i ++) {
73        rsm.updateAppend(null, 15);
74      }
75      for (int i=0; i < 16; i ++) {
76        rsm.updateReplay(16);
77      }
78    }
79  
80    @Test
81    public void testPerUserOperations() {
82      Configuration conf = HBaseConfiguration.create();
83      User userFoo = User.createUserForTesting(conf, "FOO", new String[0]);
84      User userBar = User.createUserForTesting(conf, "BAR", new String[0]);
85  
86      userFoo.getUGI().doAs(new PrivilegedAction<Void>() {
87        @Override
88        public Void run() {
89          doOperations();
90          return null;
91        }
92      });
93  
94      userBar.getUGI().doAs(new PrivilegedAction<Void>() {
95        @Override
96        public Void run() {
97          doOperations();
98          return null;
99        }
100     });
101 
102     HELPER.assertCounter("userfoometricgetnumops", 10, userAgg.getSource());
103     HELPER.assertCounter("userfoometricscantimenumops", 11, userAgg.getSource());
104     HELPER.assertCounter("userfoometricmutatenumops", 12, userAgg.getSource());
105     HELPER.assertCounter("userfoometricdeletenumops", 13, userAgg.getSource());
106     HELPER.assertCounter("userfoometricincrementnumops", 14, userAgg.getSource());
107     HELPER.assertCounter("userfoometricappendnumops", 15, userAgg.getSource());
108     HELPER.assertCounter("userfoometricreplaynumops", 16, userAgg.getSource());
109 
110     HELPER.assertCounter("userbarmetricgetnumops", 10, userAgg.getSource());
111     HELPER.assertCounter("userbarmetricscantimenumops", 11, userAgg.getSource());
112     HELPER.assertCounter("userbarmetricmutatenumops", 12, userAgg.getSource());
113     HELPER.assertCounter("userbarmetricdeletenumops", 13, userAgg.getSource());
114     HELPER.assertCounter("userbarmetricincrementnumops", 14, userAgg.getSource());
115     HELPER.assertCounter("userbarmetricappendnumops", 15, userAgg.getSource());
116     HELPER.assertCounter("userbarmetricreplaynumops", 16, userAgg.getSource());
117   }
118 }