001/** 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018 019package org.apache.hadoop.hbase.regionserver; 020 021import static org.junit.Assert.assertFalse; 022import static org.junit.Assert.assertTrue; 023import java.security.PrivilegedAction; 024import org.apache.hadoop.conf.Configuration; 025import org.apache.hadoop.hbase.CompatibilityFactory; 026import org.apache.hadoop.hbase.HBaseClassTestRule; 027import org.apache.hadoop.hbase.HBaseConfiguration; 028import org.apache.hadoop.hbase.TableName; 029import org.apache.hadoop.hbase.security.User; 030import org.apache.hadoop.hbase.test.MetricsAssertHelper; 031import org.apache.hadoop.hbase.testclassification.LargeTests; 032import org.apache.hadoop.hbase.testclassification.RegionServerTests; 033import org.junit.Before; 034import org.junit.BeforeClass; 035import org.junit.ClassRule; 036import org.junit.Test; 037import org.junit.experimental.categories.Category; 038 039@Category({RegionServerTests.class, LargeTests.class}) 040public class TestMetricsUserAggregate { 041 042 @ClassRule public static final HBaseClassTestRule CLASS_RULE = 043 HBaseClassTestRule.forClass(TestMetricsUserAggregate.class); 044 045 private static MetricsAssertHelper HELPER = 046 CompatibilityFactory.getInstance(MetricsAssertHelper.class); 047 048 private MetricsRegionServerWrapperStub wrapper; 049 private MetricsRegionServer rsm; 050 private MetricsUserAggregate userAgg; 051 private TableName tableName = TableName.valueOf("testUserAggregateMetrics"); 052 053 @BeforeClass 054 public static void classSetUp() { 055 HELPER.init(); 056 } 057 058 @Before 059 public void setUp() { 060 wrapper = new MetricsRegionServerWrapperStub(); 061 Configuration conf = HBaseConfiguration.create(); 062 rsm = new MetricsRegionServer(wrapper,conf , null); 063 userAgg = (MetricsUserAggregate)rsm.getMetricsUserAggregate(); 064 } 065 066 private void doOperations() { 067 for (int i=0; i < 10; i ++) { 068 rsm.updateGet(tableName,10); 069 } 070 for (int i=0; i < 11; i ++) { 071 rsm.updateScanTime(tableName,11); 072 } 073 for (int i=0; i < 12; i ++) { 074 rsm.updatePut(tableName,12); 075 } 076 for (int i=0; i < 13; i ++) { 077 rsm.updateDelete(tableName,13); 078 } 079 for (int i=0; i < 14; i ++) { 080 rsm.updateIncrement(tableName,14); 081 } 082 for (int i=0; i < 15; i ++) { 083 rsm.updateAppend(tableName,15); 084 } 085 for (int i=0; i < 16; i ++) { 086 rsm.updateReplay(16); 087 } 088 } 089 090 @Test 091 public void testPerUserOperations() { 092 Configuration conf = HBaseConfiguration.create(); 093 // If metrics for users is not enabled, this test doesn't make sense. 094 if (!conf.getBoolean(MetricsUserAggregateFactory.METRIC_USER_ENABLED_CONF, 095 MetricsUserAggregateFactory.DEFAULT_METRIC_USER_ENABLED_CONF)) { 096 return; 097 } 098 User userFoo = User.createUserForTesting(conf, "FOO", new String[0]); 099 User userBar = User.createUserForTesting(conf, "BAR", new String[0]); 100 101 userFoo.getUGI().doAs(new PrivilegedAction<Void>() { 102 @Override 103 public Void run() { 104 doOperations(); 105 return null; 106 } 107 }); 108 109 userBar.getUGI().doAs(new PrivilegedAction<Void>() { 110 @Override 111 public Void run() { 112 doOperations(); 113 return null; 114 } 115 }); 116 117 HELPER.assertCounter("userfoometricgetnumops", 10, userAgg.getSource()); 118 HELPER.assertCounter("userfoometricscantimenumops", 11, userAgg.getSource()); 119 HELPER.assertCounter("userfoometricputnumops", 12, userAgg.getSource()); 120 HELPER.assertCounter("userfoometricdeletenumops", 13, userAgg.getSource()); 121 HELPER.assertCounter("userfoometricincrementnumops", 14, userAgg.getSource()); 122 HELPER.assertCounter("userfoometricappendnumops", 15, userAgg.getSource()); 123 HELPER.assertCounter("userfoometricreplaynumops", 16, userAgg.getSource()); 124 125 HELPER.assertCounter("userbarmetricgetnumops", 10, userAgg.getSource()); 126 HELPER.assertCounter("userbarmetricscantimenumops", 11, userAgg.getSource()); 127 HELPER.assertCounter("userbarmetricputnumops", 12, userAgg.getSource()); 128 HELPER.assertCounter("userbarmetricdeletenumops", 13, userAgg.getSource()); 129 HELPER.assertCounter("userbarmetricincrementnumops", 14, userAgg.getSource()); 130 HELPER.assertCounter("userbarmetricappendnumops", 15, userAgg.getSource()); 131 HELPER.assertCounter("userbarmetricreplaynumops", 16, userAgg.getSource()); 132 } 133 134 @Test public void testLossyCountingOfUserMetrics() { 135 Configuration conf = HBaseConfiguration.create(); 136 // If metrics for users is not enabled, this test doesn't make sense. 137 if (!conf.getBoolean(MetricsUserAggregateFactory.METRIC_USER_ENABLED_CONF, 138 MetricsUserAggregateFactory.DEFAULT_METRIC_USER_ENABLED_CONF)) { 139 return; 140 } 141 int noOfUsers = 10000; 142 for (int i = 1; i <= noOfUsers; i++) { 143 User.createUserForTesting(conf, "FOO" + i, new String[0]).getUGI() 144 .doAs(new PrivilegedAction<Void>() { 145 @Override public Void run() { 146 rsm.updateGet(tableName, 10); 147 return null; 148 } 149 }); 150 } 151 assertTrue( 152 ((MetricsUserAggregateSourceImpl) userAgg.getSource()).getUserSources().size() <= (noOfUsers 153 / 10)); 154 for (int i = 1; i <= noOfUsers / 10; i++) { 155 assertFalse( 156 HELPER.checkCounterExists("userfoo" + i + "metricgetnumops", userAgg.getSource())); 157 } 158 HELPER.assertCounter("userfoo" + noOfUsers + "metricgetnumops", 1, userAgg.getSource()); 159 } 160}