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.coprocessor.example.row.stats;
019
020import static org.junit.Assert.assertEquals;
021import static org.mockito.ArgumentMatchers.any;
022import static org.mockito.Mockito.doNothing;
023import static org.mockito.Mockito.doThrow;
024import static org.mockito.Mockito.mock;
025import static org.mockito.Mockito.times;
026import static org.mockito.Mockito.verify;
027import static org.mockito.Mockito.when;
028
029import java.io.IOException;
030import org.apache.hadoop.hbase.client.BufferedMutator;
031import org.apache.hadoop.hbase.client.Put;
032import org.apache.hadoop.hbase.coprocessor.example.row.stats.ringbuffer.RowStatisticsEventHandler;
033import org.apache.hadoop.hbase.coprocessor.example.row.stats.ringbuffer.RowStatisticsRingBufferEnvelope;
034import org.apache.hadoop.hbase.coprocessor.example.row.stats.ringbuffer.RowStatisticsRingBufferPayload;
035import org.apache.hadoop.hbase.metrics.Counter;
036import org.apache.hadoop.hbase.metrics.impl.CounterImpl;
037import org.apache.hadoop.hbase.util.Bytes;
038import org.junit.Before;
039import org.junit.Test;
040
041public class TestRowStatisticsEventHandler {
042
043  private static final String REGION_STRING = "REGION_STRING";
044  private static final byte[] FULL_REGION = Bytes.toBytes("FULL_REGION_STRING");
045  private static final String JSON_STRING = "JSON_STRING";
046  private static final RowStatisticsRingBufferEnvelope EVENT =
047    new RowStatisticsRingBufferEnvelope();
048  private static final RowStatistics ROW_STATISTICS = mock(RowStatistics.class);
049  private BufferedMutator bufferedMutator;
050  private Counter failureCounter;
051  private RowStatisticsEventHandler eventHandler;
052
053  @Before
054  public void setup() {
055    bufferedMutator = mock(BufferedMutator.class);
056    failureCounter = new CounterImpl();
057    eventHandler = new RowStatisticsEventHandler(bufferedMutator, failureCounter);
058    when(ROW_STATISTICS.getRegion()).thenReturn(REGION_STRING);
059    when(ROW_STATISTICS.getJsonString()).thenReturn(JSON_STRING);
060  }
061
062  @Test
063  public void itPersistsRowStatistics() throws Exception {
064    EVENT.load(new RowStatisticsRingBufferPayload(ROW_STATISTICS, FULL_REGION));
065    doNothing().when(bufferedMutator).mutate(any(Put.class));
066    eventHandler.onEvent(EVENT, 0L, true);
067    verify(bufferedMutator, times(1)).mutate(any(Put.class));
068    assertEquals(failureCounter.getCount(), 0);
069  }
070
071  @Test
072  public void itDoesNotPublishNullRowStatistics() throws Exception {
073    EVENT.load(null);
074    eventHandler.onEvent(EVENT, 0L, true);
075    verify(bufferedMutator, times(0)).mutate(any(Put.class));
076    assertEquals(failureCounter.getCount(), 0);
077  }
078
079  @Test
080  public void itCountsFailedPersists() throws Exception {
081    EVENT.load(new RowStatisticsRingBufferPayload(ROW_STATISTICS, FULL_REGION));
082    doThrow(new IOException()).when(bufferedMutator).mutate(any(Put.class));
083    eventHandler.onEvent(EVENT, 0L, true);
084    verify(bufferedMutator, times(1)).mutate(any(Put.class));
085    assertEquals(failureCounter.getCount(), 1);
086  }
087}