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 */
018package org.apache.hadoop.hbase.regionserver;
019
020import static org.junit.Assert.assertFalse;
021import static org.junit.Assert.assertTrue;
022import static org.mockito.Mockito.mock;
023import static org.mockito.Mockito.when;
024
025import java.security.PrivilegedAction;
026import org.apache.hadoop.conf.Configuration;
027import org.apache.hadoop.hbase.CompatibilityFactory;
028import org.apache.hadoop.hbase.HBaseClassTestRule;
029import org.apache.hadoop.hbase.HBaseConfiguration;
030import org.apache.hadoop.hbase.TableName;
031import org.apache.hadoop.hbase.regionserver.metrics.MetricsTableRequests;
032import org.apache.hadoop.hbase.security.User;
033import org.apache.hadoop.hbase.test.MetricsAssertHelper;
034import org.apache.hadoop.hbase.testclassification.LargeTests;
035import org.apache.hadoop.hbase.testclassification.RegionServerTests;
036import org.junit.Before;
037import org.junit.BeforeClass;
038import org.junit.ClassRule;
039import org.junit.Test;
040import org.junit.experimental.categories.Category;
041
042@Category({ RegionServerTests.class, LargeTests.class })
043public class TestMetricsUserAggregate {
044
045  @ClassRule
046  public static final HBaseClassTestRule CLASS_RULE =
047    HBaseClassTestRule.forClass(TestMetricsUserAggregate.class);
048
049  private static MetricsAssertHelper HELPER =
050    CompatibilityFactory.getInstance(MetricsAssertHelper.class);
051
052  private MetricsRegionServerWrapperStub wrapper;
053  private MetricsRegionServer rsm;
054  private MetricsUserAggregate userAgg;
055  private TableName tableName = TableName.valueOf("testUserAggregateMetrics");
056
057  @BeforeClass
058  public static void classSetUp() {
059    HELPER.init();
060  }
061
062  @Before
063  public void setUp() {
064    wrapper = new MetricsRegionServerWrapperStub();
065    Configuration conf = HBaseConfiguration.create();
066    conf.setBoolean(MetricsUserAggregateFactory.METRIC_USER_ENABLED_CONF, true);
067    rsm = new MetricsRegionServer(wrapper, conf, null);
068    userAgg = (MetricsUserAggregate) rsm.getMetricsUserAggregate();
069  }
070
071  private void doOperations() {
072    HRegion region = mock(HRegion.class);
073    MetricsTableRequests metricsTableRequests = mock(MetricsTableRequests.class);
074    when(region.getMetricsTableRequests()).thenReturn(metricsTableRequests);
075    when(metricsTableRequests.isEnableTableLatenciesMetrics()).thenReturn(false);
076    when(metricsTableRequests.isEnabTableQueryMeterMetrics()).thenReturn(false);
077    for (int i = 0; i < 10; i++) {
078      rsm.updateGet(region, 10, 10);
079    }
080    for (int i = 0; i < 11; i++) {
081      rsm.updateScan(region, 11, 111, 1111);
082    }
083    for (int i = 0; i < 12; i++) {
084      rsm.updatePut(region, 12);
085    }
086    for (int i = 0; i < 13; i++) {
087      rsm.updateDelete(region, 13);
088    }
089    for (int i = 0; i < 14; i++) {
090      rsm.updateIncrement(region, 14, 140);
091    }
092    for (int i = 0; i < 15; i++) {
093      rsm.updateAppend(region, 15, 150);
094    }
095    for (int i = 0; i < 16; i++) {
096      rsm.updateReplay(16);
097    }
098  }
099
100  @Test
101  public void testPerUserOperations() {
102    Configuration conf = HBaseConfiguration.create();
103    User userFoo = User.createUserForTesting(conf, "FOO", new String[0]);
104    User userBar = User.createUserForTesting(conf, "BAR", new String[0]);
105
106    userFoo.getUGI().doAs(new PrivilegedAction<Void>() {
107      @Override
108      public Void run() {
109        doOperations();
110        return null;
111      }
112    });
113
114    userBar.getUGI().doAs(new PrivilegedAction<Void>() {
115      @Override
116      public Void run() {
117        doOperations();
118        return null;
119      }
120    });
121
122    HELPER.assertCounter("userfoometricgetnumops", 10, userAgg.getSource());
123    HELPER.assertCounter("userfoometricscantimenumops", 11, userAgg.getSource());
124    HELPER.assertCounter("userfoometricputnumops", 12, userAgg.getSource());
125    HELPER.assertCounter("userfoometricdeletenumops", 13, userAgg.getSource());
126    HELPER.assertCounter("userfoometricincrementnumops", 14, userAgg.getSource());
127    HELPER.assertCounter("userfoometricappendnumops", 15, userAgg.getSource());
128    HELPER.assertCounter("userfoometricreplaynumops", 16, userAgg.getSource());
129    HELPER.assertCounter("userfoometricblockbytesscannedcount", 16531, userAgg.getSource());
130
131    HELPER.assertCounter("userbarmetricgetnumops", 10, userAgg.getSource());
132    HELPER.assertCounter("userbarmetricscantimenumops", 11, userAgg.getSource());
133    HELPER.assertCounter("userbarmetricputnumops", 12, userAgg.getSource());
134    HELPER.assertCounter("userbarmetricdeletenumops", 13, userAgg.getSource());
135    HELPER.assertCounter("userbarmetricincrementnumops", 14, userAgg.getSource());
136    HELPER.assertCounter("userbarmetricappendnumops", 15, userAgg.getSource());
137    HELPER.assertCounter("userbarmetricreplaynumops", 16, userAgg.getSource());
138    HELPER.assertCounter("userbarmetricblockbytesscannedcount", 16531, userAgg.getSource());
139  }
140
141  @Test
142  public void testLossyCountingOfUserMetrics() {
143    Configuration conf = HBaseConfiguration.create();
144    int noOfUsers = 10000;
145    for (int i = 1; i <= noOfUsers; i++) {
146      User.createUserForTesting(conf, "FOO" + i, new String[0]).getUGI()
147        .doAs(new PrivilegedAction<Void>() {
148          @Override
149          public Void run() {
150            HRegion region = mock(HRegion.class);
151            MetricsTableRequests metricsTableRequests = mock(MetricsTableRequests.class);
152            when(region.getMetricsTableRequests()).thenReturn(metricsTableRequests);
153            when(metricsTableRequests.isEnableTableLatenciesMetrics()).thenReturn(false);
154            when(metricsTableRequests.isEnabTableQueryMeterMetrics()).thenReturn(false);
155            rsm.updateGet(region, 10, 100);
156            return null;
157          }
158        });
159    }
160    assertTrue(((MetricsUserAggregateSourceImpl) userAgg.getSource()).getUserSources().size()
161        <= (noOfUsers / 10));
162    for (int i = 1; i <= noOfUsers / 10; i++) {
163      assertFalse(
164        HELPER.checkCounterExists("userfoo" + i + "metricgetnumops", userAgg.getSource()));
165      assertFalse(HELPER.checkCounterExists("userfoo" + i + "metricblockbytesscannedcount",
166        userAgg.getSource()));
167    }
168    HELPER.assertCounter("userfoo" + noOfUsers + "metricgetnumops", 1, userAgg.getSource());
169    HELPER.assertCounter("userfoo" + noOfUsers + "metricblockbytesscannedcount", 100,
170      userAgg.getSource());
171  }
172}