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;
022
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
043  public static final HBaseClassTestRule CLASS_RULE =
044    HBaseClassTestRule.forClass(TestMetricsUserAggregate.class);
045
046  private static MetricsAssertHelper HELPER =
047    CompatibilityFactory.getInstance(MetricsAssertHelper.class);
048
049  private MetricsRegionServerWrapperStub wrapper;
050  private MetricsRegionServer rsm;
051  private MetricsUserAggregate userAgg;
052  private TableName tableName = TableName.valueOf("testUserAggregateMetrics");
053
054  @BeforeClass
055  public static void classSetUp() {
056    HELPER.init();
057  }
058
059  @Before
060  public void setUp() {
061    wrapper = new MetricsRegionServerWrapperStub();
062    Configuration conf = HBaseConfiguration.create();
063    rsm = new MetricsRegionServer(wrapper, conf, null);
064    userAgg = (MetricsUserAggregate) rsm.getMetricsUserAggregate();
065  }
066
067  private void doOperations() {
068    for (int i = 0; i < 10; i++) {
069      rsm.updateGet(tableName, 10);
070    }
071    for (int i = 0; i < 11; i++) {
072      rsm.updateScanTime(tableName, 11);
073    }
074    for (int i = 0; i < 12; i++) {
075      rsm.updatePut(tableName, 12);
076    }
077    for (int i = 0; i < 13; i++) {
078      rsm.updateDelete(tableName, 13);
079    }
080    for (int i = 0; i < 14; i++) {
081      rsm.updateIncrement(tableName, 14);
082    }
083    for (int i = 0; i < 15; i++) {
084      rsm.updateAppend(tableName, 15);
085    }
086    for (int i = 0; i < 16; i++) {
087      rsm.updateReplay(16);
088    }
089  }
090
091  @Test
092  public void testPerUserOperations() {
093    Configuration conf = HBaseConfiguration.create();
094    // If metrics for users is not enabled, this test doesn't make sense.
095    if (
096      !conf.getBoolean(MetricsUserAggregateFactory.METRIC_USER_ENABLED_CONF,
097        MetricsUserAggregateFactory.DEFAULT_METRIC_USER_ENABLED_CONF)
098    ) {
099      return;
100    }
101    User userFoo = User.createUserForTesting(conf, "FOO", new String[0]);
102    User userBar = User.createUserForTesting(conf, "BAR", new String[0]);
103
104    userFoo.getUGI().doAs(new PrivilegedAction<Void>() {
105      @Override
106      public Void run() {
107        doOperations();
108        return null;
109      }
110    });
111
112    userBar.getUGI().doAs(new PrivilegedAction<Void>() {
113      @Override
114      public Void run() {
115        doOperations();
116        return null;
117      }
118    });
119
120    HELPER.assertCounter("userfoometricgetnumops", 10, userAgg.getSource());
121    HELPER.assertCounter("userfoometricscantimenumops", 11, userAgg.getSource());
122    HELPER.assertCounter("userfoometricputnumops", 12, userAgg.getSource());
123    HELPER.assertCounter("userfoometricdeletenumops", 13, userAgg.getSource());
124    HELPER.assertCounter("userfoometricincrementnumops", 14, userAgg.getSource());
125    HELPER.assertCounter("userfoometricappendnumops", 15, userAgg.getSource());
126    HELPER.assertCounter("userfoometricreplaynumops", 16, userAgg.getSource());
127
128    HELPER.assertCounter("userbarmetricgetnumops", 10, userAgg.getSource());
129    HELPER.assertCounter("userbarmetricscantimenumops", 11, userAgg.getSource());
130    HELPER.assertCounter("userbarmetricputnumops", 12, userAgg.getSource());
131    HELPER.assertCounter("userbarmetricdeletenumops", 13, userAgg.getSource());
132    HELPER.assertCounter("userbarmetricincrementnumops", 14, userAgg.getSource());
133    HELPER.assertCounter("userbarmetricappendnumops", 15, userAgg.getSource());
134    HELPER.assertCounter("userbarmetricreplaynumops", 16, userAgg.getSource());
135  }
136
137  @Test
138  public void testLossyCountingOfUserMetrics() {
139    Configuration conf = HBaseConfiguration.create();
140    // If metrics for users is not enabled, this test doesn't make sense.
141    if (
142      !conf.getBoolean(MetricsUserAggregateFactory.METRIC_USER_ENABLED_CONF,
143        MetricsUserAggregateFactory.DEFAULT_METRIC_USER_ENABLED_CONF)
144    ) {
145      return;
146    }
147    int noOfUsers = 10000;
148    for (int i = 1; i <= noOfUsers; i++) {
149      User.createUserForTesting(conf, "FOO" + i, new String[0]).getUGI()
150        .doAs(new PrivilegedAction<Void>() {
151          @Override
152          public Void run() {
153            rsm.updateGet(tableName, 10);
154            return null;
155          }
156        });
157    }
158    assertTrue(((MetricsUserAggregateSourceImpl) userAgg.getSource()).getUserSources().size()
159        <= (noOfUsers / 10));
160    for (int i = 1; i <= noOfUsers / 10; i++) {
161      assertFalse(
162        HELPER.checkCounterExists("userfoo" + i + "metricgetnumops", userAgg.getSource()));
163    }
164    HELPER.assertCounter("userfoo" + noOfUsers + "metricgetnumops", 1, userAgg.getSource());
165  }
166}