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