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.hamcrest.MatcherAssert.assertThat; 021import static org.hamcrest.Matchers.containsString; 022import static org.hamcrest.Matchers.startsWith; 023import static org.junit.Assert.assertEquals; 024import static org.mockito.ArgumentMatchers.any; 025import static org.mockito.Mockito.doAnswer; 026import static org.mockito.Mockito.mock; 027import static org.mockito.Mockito.never; 028import static org.mockito.Mockito.times; 029import static org.mockito.Mockito.verify; 030import static org.mockito.Mockito.when; 031 032import java.io.IOException; 033import java.util.IdentityHashMap; 034import java.util.concurrent.atomic.AtomicReference; 035import org.apache.hadoop.hbase.HBaseClassTestRule; 036import org.apache.hadoop.hbase.HRegionLocation; 037import org.apache.hadoop.hbase.ServerName; 038import org.apache.hadoop.hbase.TableName; 039import org.apache.hadoop.hbase.client.AsyncBatchRpcRetryingCaller.RegionRequest; 040import org.apache.hadoop.hbase.testclassification.ClientTests; 041import org.apache.hadoop.hbase.testclassification.SmallTests; 042import org.apache.hadoop.hbase.util.Bytes; 043import org.apache.hadoop.hbase.util.EnvironmentEdgeManager; 044import org.junit.After; 045import org.junit.Before; 046import org.junit.ClassRule; 047import org.junit.Test; 048import org.junit.experimental.categories.Category; 049import org.mockito.invocation.InvocationOnMock; 050import org.mockito.stubbing.Answer; 051 052@Category({ ClientTests.class, SmallTests.class }) 053public class TestAsyncBatchRpcRetryingCaller { 054 055 @ClassRule 056 public static final HBaseClassTestRule CLASS_RULE = 057 HBaseClassTestRule.forClass(TestAsyncBatchRpcRetryingCaller.class); 058 059 private org.apache.logging.log4j.core.Appender mockAppender; 060 061 @Before 062 public void setUp() { 063 mockAppender = mock(org.apache.logging.log4j.core.Appender.class); 064 when(mockAppender.getName()).thenReturn("mockAppender"); 065 when(mockAppender.isStarted()).thenReturn(true); 066 ((org.apache.logging.log4j.core.Logger) org.apache.logging.log4j.LogManager 067 .getLogger(AsyncBatchRpcRetryingCaller.class)).addAppender(mockAppender); 068 069 } 070 071 @After 072 public void tearDown() { 073 ((org.apache.logging.log4j.core.Logger) org.apache.logging.log4j.LogManager 074 .getLogger(AsyncBatchRpcRetryingCaller.class)).removeAppender(mockAppender); 075 } 076 077 @Test 078 public void testLogAction() { 079 AtomicReference<org.apache.logging.log4j.Level> level = new AtomicReference<>(); 080 AtomicReference<String> msg = new AtomicReference<String>(); 081 doAnswer(new Answer<Void>() { 082 083 @Override 084 public Void answer(InvocationOnMock invocation) throws Throwable { 085 org.apache.logging.log4j.core.LogEvent logEvent = 086 invocation.getArgument(0, org.apache.logging.log4j.core.LogEvent.class); 087 level.set(logEvent.getLevel()); 088 msg.set(logEvent.getMessage().getFormattedMessage()); 089 return null; 090 } 091 }).when(mockAppender).append(any(org.apache.logging.log4j.core.LogEvent.class)); 092 TableName tn = TableName.valueOf("async"); 093 ServerName sn = ServerName.valueOf("host", 12345, EnvironmentEdgeManager.currentTime()); 094 RegionRequest request = 095 new RegionRequest(new HRegionLocation(RegionInfoBuilder.newBuilder(tn).build(), sn)); 096 Action put = new Action(new Put(Bytes.toBytes("a")), 0); 097 Action get = new Action(new Get(Bytes.toBytes("b")), 1); 098 Action incr = new Action(new Increment(Bytes.toBytes("c")), 2); 099 Action del = new Action(new Delete(Bytes.toBytes("d")), 3); 100 request.actions.add(put); 101 request.actions.add(get); 102 request.actions.add(incr); 103 request.actions.add(del); 104 IdentityHashMap<Action, Throwable> action2Error = new IdentityHashMap<>(); 105 AsyncBatchRpcRetryingCaller.logActionsException(1, 2, request, action2Error, sn); 106 verify(mockAppender, never()).append(any()); 107 AsyncBatchRpcRetryingCaller.logActionsException(5, 4, request, action2Error, sn); 108 verify(mockAppender, never()).append(any()); 109 110 action2Error.put(get, new IOException("get error")); 111 action2Error.put(incr, new IOException("incr error")); 112 AsyncBatchRpcRetryingCaller.logActionsException(5, 4, request, action2Error, sn); 113 verify(mockAppender, times(1)).append(any()); 114 assertEquals(org.apache.logging.log4j.Level.WARN, level.get()); 115 116 String logMsg = msg.get(); 117 assertThat(logMsg, 118 startsWith("Process batch for " + request.loc.getRegion().getRegionNameAsString() + " on " 119 + sn.toString() + ", 2/4 actions failed, tries=5, sampled 2 errors:")); 120 assertThat(logMsg, containsString("=> java.io.IOException: get error")); 121 assertThat(logMsg, containsString("=> java.io.IOException: incr error")); 122 } 123}