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.testclassification.SmallTests; 038import org.apache.hadoop.hbase.util.Bytes; 039import org.junit.Before; 040import org.junit.Test; 041import org.junit.jupiter.api.Tag; 042 043@Tag(SmallTests.TAG) 044public class TestRowStatisticsEventHandler { 045 046 private static final String REGION_STRING = "REGION_STRING"; 047 private static final byte[] FULL_REGION = Bytes.toBytes("FULL_REGION_STRING"); 048 private static final String JSON_STRING = "JSON_STRING"; 049 private static final RowStatisticsRingBufferEnvelope EVENT = 050 new RowStatisticsRingBufferEnvelope(); 051 private static final RowStatistics ROW_STATISTICS = mock(RowStatistics.class); 052 private BufferedMutator bufferedMutator; 053 private Counter failureCounter; 054 private RowStatisticsEventHandler eventHandler; 055 056 @Before 057 public void setup() { 058 bufferedMutator = mock(BufferedMutator.class); 059 failureCounter = new CounterImpl(); 060 eventHandler = new RowStatisticsEventHandler(bufferedMutator, failureCounter); 061 when(ROW_STATISTICS.getRegion()).thenReturn(REGION_STRING); 062 when(ROW_STATISTICS.getJsonString()).thenReturn(JSON_STRING); 063 } 064 065 @Test 066 public void itPersistsRowStatistics() throws Exception { 067 EVENT.load(new RowStatisticsRingBufferPayload(ROW_STATISTICS, FULL_REGION)); 068 doNothing().when(bufferedMutator).mutate(any(Put.class)); 069 eventHandler.onEvent(EVENT, 0L, true); 070 verify(bufferedMutator, times(1)).mutate(any(Put.class)); 071 assertEquals(failureCounter.getCount(), 0); 072 } 073 074 @Test 075 public void itDoesNotPublishNullRowStatistics() throws Exception { 076 EVENT.load(null); 077 eventHandler.onEvent(EVENT, 0L, true); 078 verify(bufferedMutator, times(0)).mutate(any(Put.class)); 079 assertEquals(failureCounter.getCount(), 0); 080 } 081 082 @Test 083 public void itCountsFailedPersists() throws Exception { 084 EVENT.load(new RowStatisticsRingBufferPayload(ROW_STATISTICS, FULL_REGION)); 085 doThrow(new IOException()).when(bufferedMutator).mutate(any(Put.class)); 086 eventHandler.onEvent(EVENT, 0L, true); 087 verify(bufferedMutator, times(1)).mutate(any(Put.class)); 088 assertEquals(failureCounter.getCount(), 1); 089 } 090}