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