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 static org.junit.Assert.assertFalse;
22  import static org.junit.Assert.assertTrue;
23  
24  import java.security.PrivilegedAction;
25  
26  import org.apache.hadoop.conf.Configuration;
27  import org.apache.hadoop.hbase.CompatibilityFactory;
28  import org.apache.hadoop.hbase.HBaseConfiguration;
29  import org.apache.hadoop.hbase.TableName;
30  import org.apache.hadoop.hbase.security.User;
31  import org.apache.hadoop.hbase.test.MetricsAssertHelper;
32  import org.apache.hadoop.hbase.testclassification.RegionServerTests;
33  import org.apache.hadoop.hbase.testclassification.SmallTests;
34  import org.junit.Before;
35  import org.junit.BeforeClass;
36  import org.junit.Test;
37  import org.junit.experimental.categories.Category;
38  
39  @Category({RegionServerTests.class, SmallTests.class})
40  public class TestMetricsUserAggregate {
41  
42    public static MetricsAssertHelper HELPER =
43        CompatibilityFactory.getInstance(MetricsAssertHelper.class);
44  
45    private MetricsRegionServerWrapperStub wrapper;
46    private MetricsRegionServer rsm;
47    private MetricsUserAggregateImpl userAgg;
48    private TableName tableName = TableName.valueOf("testUserAggregateMetrics");
49  
50    @BeforeClass
51    public static void classSetUp() {
52      HELPER.init();
53    }
54  
55    @Before
56    public void setUp() {
57      wrapper = new MetricsRegionServerWrapperStub();
58      Configuration conf = HBaseConfiguration.create();
59      rsm = new MetricsRegionServer(wrapper, conf);
60      userAgg = (MetricsUserAggregateImpl)rsm.getMetricsUserAggregate();
61    }
62  
63    private void doOperations() {
64      for (int i=0; i < 10; i ++) {
65        rsm.updateGet(tableName,10);
66      }
67      for (int i=0; i < 11; i ++) {
68        rsm.updateScanTime(tableName,11);
69      }
70      for (int i=0; i < 12; i ++) {
71        rsm.updatePut(tableName,12);
72      }
73      for (int i=0; i < 13; i ++) {
74        rsm.updateDelete(tableName,13);
75      }
76      for (int i=0; i < 14; i ++) {
77        rsm.updateIncrement(tableName,14);
78      }
79      for (int i=0; i < 15; i ++) {
80        rsm.updateAppend(tableName,15);
81      }
82      for (int i=0; i < 16; i ++) {
83        rsm.updateReplay(16);
84      }
85    }
86  
87    @Test
88    public void testPerUserOperations() {
89      Configuration conf = HBaseConfiguration.create();
90      User userFoo = User.createUserForTesting(conf, "FOO", new String[0]);
91      User userBar = User.createUserForTesting(conf, "BAR", new String[0]);
92  
93      userFoo.getUGI().doAs(new PrivilegedAction<Void>() {
94        @Override
95        public Void run() {
96          doOperations();
97          return null;
98        }
99      });
100 
101     userBar.getUGI().doAs(new PrivilegedAction<Void>() {
102       @Override
103       public Void run() {
104         doOperations();
105         return null;
106       }
107     });
108 
109     HELPER.assertCounter("userfoometricgetnumops", 10, userAgg.getSource());
110     HELPER.assertCounter("userfoometricscantimenumops", 11, userAgg.getSource());
111     HELPER.assertCounter("userfoometricputnumops", 12, userAgg.getSource());
112     HELPER.assertCounter("userfoometricdeletenumops", 13, userAgg.getSource());
113     HELPER.assertCounter("userfoometricincrementnumops", 14, userAgg.getSource());
114     HELPER.assertCounter("userfoometricappendnumops", 15, userAgg.getSource());
115     HELPER.assertCounter("userfoometricreplaynumops", 16, userAgg.getSource());
116 
117     HELPER.assertCounter("userbarmetricgetnumops", 10, userAgg.getSource());
118     HELPER.assertCounter("userbarmetricscantimenumops", 11, userAgg.getSource());
119     HELPER.assertCounter("userbarmetricputnumops", 12, userAgg.getSource());
120     HELPER.assertCounter("userbarmetricdeletenumops", 13, userAgg.getSource());
121     HELPER.assertCounter("userbarmetricincrementnumops", 14, userAgg.getSource());
122     HELPER.assertCounter("userbarmetricappendnumops", 15, userAgg.getSource());
123     HELPER.assertCounter("userbarmetricreplaynumops", 16, userAgg.getSource());
124   }
125 
126   @Test public void testLossyCountingOfUserMetrics() {
127     Configuration conf = HBaseConfiguration.create();
128     int noOfUsers = 10000;
129     for (int i = 1; i <= noOfUsers; i++) {
130       User.createUserForTesting(conf, "FOO" + i, new String[0]).getUGI()
131           .doAs(new PrivilegedAction<Void>() {
132             @Override public Void run() {
133               rsm.updateGet(tableName, 10);
134               return null;
135             }
136           });
137     }
138     assertTrue(
139         ((MetricsUserAggregateSourceImpl) userAgg.getSource()).getUserSources().size() <= (noOfUsers
140             / 10));
141     for (int i = 1; i <= noOfUsers / 10; i++) {
142       assertFalse(
143           HELPER.checkCounterExists("userfoo" + i + "metricgetnumops", userAgg.getSource()));
144     }
145     HELPER.assertCounter("userfoo" + noOfUsers + "metricgetnumops", 1, userAgg.getSource());
146   }
147 }