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.client;
019
020import static org.junit.Assert.assertEquals;
021
022import org.apache.hadoop.conf.Configuration;
023import org.apache.hadoop.hbase.HBaseClassTestRule;
024import org.apache.hadoop.hbase.HBaseTestingUtility;
025import org.apache.hadoop.hbase.TableName;
026import org.apache.hadoop.hbase.testclassification.ClientTests;
027import org.apache.hadoop.hbase.testclassification.SmallTests;
028import org.apache.hadoop.hbase.util.Bytes;
029import org.junit.AfterClass;
030import org.junit.BeforeClass;
031import org.junit.ClassRule;
032import org.junit.Test;
033import org.junit.experimental.categories.Category;
034
035@Category({ SmallTests.class, ClientTests.class })
036public class TestMultiActionMetricsFromClient {
037
038  @ClassRule
039  public static final HBaseClassTestRule CLASS_RULE =
040    HBaseClassTestRule.forClass(TestMultiActionMetricsFromClient.class);
041
042  private final static HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
043  private static final TableName TABLE_NAME = TableName.valueOf("test_table");
044  private static final byte[] FAMILY = Bytes.toBytes("fam1");
045  private static final byte[] QUALIFIER = Bytes.toBytes("qual");
046
047  @BeforeClass
048  public static void setUpBeforeClass() throws Exception {
049    TEST_UTIL.startMiniCluster(1);
050    TEST_UTIL.getHBaseCluster().waitForActiveAndReadyMaster();
051    TEST_UTIL.waitUntilAllRegionsAssigned(TABLE_NAME.META_TABLE_NAME);
052    TEST_UTIL.createTable(TABLE_NAME, FAMILY);
053  }
054
055  @AfterClass
056  public static void tearDownAfterClass() throws Exception {
057    TEST_UTIL.shutdownMiniCluster();
058  }
059
060  @Test
061  public void testMultiMetrics() throws Exception {
062    Configuration conf = new Configuration(TEST_UTIL.getConfiguration());
063    conf.set(MetricsConnection.CLIENT_SIDE_METRICS_ENABLED_KEY, "true");
064    ConnectionImplementation conn =
065      (ConnectionImplementation) ConnectionFactory.createConnection(conf);
066
067    try {
068      BufferedMutator mutator = conn.getBufferedMutator(TABLE_NAME);
069      byte[][] keys = { Bytes.toBytes("aaa"), Bytes.toBytes("mmm"), Bytes.toBytes("zzz") };
070      for (byte[] key : keys) {
071        Put p = new Put(key);
072        p.addColumn(FAMILY, QUALIFIER, Bytes.toBytes(10));
073        mutator.mutate(p);
074      }
075
076      mutator.flush();
077      mutator.close();
078
079      MetricsConnection metrics = conn.getConnectionMetrics();
080      assertEquals(1, metrics.getMultiTracker().reqHist.getCount());
081      assertEquals(3, metrics.getNumActionsPerServerHist().getSnapshot().getMean(), 1e-15);
082      assertEquals(1, metrics.getNumActionsPerServerHist().getCount());
083    } finally {
084      conn.close();
085    }
086  }
087}