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 MetricsUserAggregateImpl 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 = (MetricsUserAggregateImpl)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    User userFoo = User.createUserForTesting(conf, "FOO", new String[0]);
094    User userBar = User.createUserForTesting(conf, "BAR", new String[0]);
095
096    userFoo.getUGI().doAs(new PrivilegedAction<Void>() {
097      @Override
098      public Void run() {
099        doOperations();
100        return null;
101      }
102    });
103
104    userBar.getUGI().doAs(new PrivilegedAction<Void>() {
105      @Override
106      public Void run() {
107        doOperations();
108        return null;
109      }
110    });
111
112    HELPER.assertCounter("userfoometricgetnumops", 10, userAgg.getSource());
113    HELPER.assertCounter("userfoometricscantimenumops", 11, userAgg.getSource());
114    HELPER.assertCounter("userfoometricputnumops", 12, userAgg.getSource());
115    HELPER.assertCounter("userfoometricdeletenumops", 13, userAgg.getSource());
116    HELPER.assertCounter("userfoometricincrementnumops", 14, userAgg.getSource());
117    HELPER.assertCounter("userfoometricappendnumops", 15, userAgg.getSource());
118    HELPER.assertCounter("userfoometricreplaynumops", 16, userAgg.getSource());
119
120    HELPER.assertCounter("userbarmetricgetnumops", 10, userAgg.getSource());
121    HELPER.assertCounter("userbarmetricscantimenumops", 11, userAgg.getSource());
122    HELPER.assertCounter("userbarmetricputnumops", 12, userAgg.getSource());
123    HELPER.assertCounter("userbarmetricdeletenumops", 13, userAgg.getSource());
124    HELPER.assertCounter("userbarmetricincrementnumops", 14, userAgg.getSource());
125    HELPER.assertCounter("userbarmetricappendnumops", 15, userAgg.getSource());
126    HELPER.assertCounter("userbarmetricreplaynumops", 16, userAgg.getSource());
127  }
128
129  @Test public void testLossyCountingOfUserMetrics() {
130    Configuration conf = HBaseConfiguration.create();
131    int noOfUsers = 10000;
132    for (int i = 1; i <= noOfUsers; i++) {
133      User.createUserForTesting(conf, "FOO" + i, new String[0]).getUGI()
134          .doAs(new PrivilegedAction<Void>() {
135            @Override public Void run() {
136              rsm.updateGet(tableName, 10);
137              return null;
138            }
139          });
140    }
141    assertTrue(
142        ((MetricsUserAggregateSourceImpl) userAgg.getSource()).getUserSources().size() <= (noOfUsers
143            / 10));
144    for (int i = 1; i <= noOfUsers / 10; i++) {
145      assertFalse(
146          HELPER.checkCounterExists("userfoo" + i + "metricgetnumops", userAgg.getSource()));
147    }
148    HELPER.assertCounter("userfoo" + noOfUsers + "metricgetnumops", 1, userAgg.getSource());
149  }
150}