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; 019 020import static org.junit.Assert.assertEquals; 021import static org.mockito.ArgumentMatchers.any; 022import static org.mockito.Mockito.clearInvocations; 023import static org.mockito.Mockito.doAnswer; 024import static org.mockito.Mockito.doReturn; 025import static org.mockito.Mockito.mock; 026import static org.mockito.Mockito.reset; 027import static org.mockito.Mockito.verify; 028import static org.mockito.Mockito.verifyNoMoreInteractions; 029 030import java.io.IOException; 031import java.util.List; 032import java.util.Map; 033import java.util.Optional; 034import org.apache.hadoop.conf.Configuration; 035import org.apache.hadoop.hbase.HBaseClassTestRule; 036import org.apache.hadoop.hbase.HBaseTestingUtil; 037import org.apache.hadoop.hbase.HRegionLocation; 038import org.apache.hadoop.hbase.RegionLocations; 039import org.apache.hadoop.hbase.ServerName; 040import org.apache.hadoop.hbase.client.AsyncConnectionImpl; 041import org.apache.hadoop.hbase.client.ConnectionFactory; 042import org.apache.hadoop.hbase.client.ConnectionRegistry; 043import org.apache.hadoop.hbase.client.RegionInfoBuilder; 044import org.apache.hadoop.hbase.master.HMaster; 045import org.apache.hadoop.hbase.master.MasterRpcServices; 046import org.apache.hadoop.hbase.testclassification.CoprocessorTests; 047import org.apache.hadoop.hbase.testclassification.MediumTests; 048import org.junit.AfterClass; 049import org.junit.Before; 050import org.junit.BeforeClass; 051import org.junit.ClassRule; 052import org.junit.Rule; 053import org.junit.Test; 054import org.junit.experimental.categories.Category; 055import org.junit.rules.TestName; 056import org.slf4j.Logger; 057import org.slf4j.LoggerFactory; 058 059/** 060 * Tests invocation of the {@link MasterObserver} interface hooks at all appropriate times during 061 * normal HMaster operations. 062 */ 063@Category({ CoprocessorTests.class, MediumTests.class }) 064public class TestClientMetaCoprocessor { 065 private static final Logger LOG = LoggerFactory.getLogger(TestClientMetaCoprocessor.class); 066 067 @ClassRule 068 public static final HBaseClassTestRule CLASS_RULE = 069 HBaseClassTestRule.forClass(TestClientMetaCoprocessor.class); 070 071 @Rule 072 public TestName name = new TestName(); 073 074 private static final HBaseTestingUtil UTIL = new HBaseTestingUtil(); 075 076 private static final ServerName SERVER_NAME = ServerName.valueOf("localhost", 1234, 12345); 077 078 public static class TestCoprocessor implements ClientMetaCoprocessor { 079 protected final ClientMetaObserver observer; 080 081 public TestCoprocessor() { 082 observer = mock(ClientMetaObserver.class); 083 resetMock(); 084 } 085 086 protected void resetMock() { 087 reset(observer); 088 089 try { 090 doAnswer(answer -> answer.getArgument(1, String.class)).when(observer) 091 .postGetClusterId(any(), any()); 092 doAnswer(answer -> { 093 return answer.getArgument(1, ServerName.class); 094 }).when(observer).postGetActiveMaster(any(), any()); 095 doAnswer(answer -> answer.getArgument(1, Map.class)).when(observer).postGetMasters(any(), 096 any()); 097 doAnswer(answer -> answer.getArgument(1, List.class)).when(observer) 098 .postGetBootstrapNodes(any(), any()); 099 doAnswer(answer -> answer.getArgument(1, List.class)).when(observer) 100 .postGetMetaLocations(any(), any()); 101 } catch (IOException e) { 102 throw new IllegalStateException("Could not setup observer mock.", e); 103 } 104 } 105 106 @Override 107 public Optional<ClientMetaObserver> getClientMetaObserver() { 108 return Optional.of(observer); 109 } 110 } 111 112 private static TestCoprocessor getCoprocessor() { 113 HMaster master = UTIL.getMiniHBaseCluster().getMaster(); 114 MasterRpcServices masterRpcServices = master.getMasterRpcServices(); 115 116 return masterRpcServices.getClientMetaCoprocessorHost().findCoprocessor(TestCoprocessor.class); 117 } 118 119 private static ClientMetaObserver getObserverMock() { 120 return getCoprocessor().getClientMetaObserver().get(); 121 } 122 123 private static void resetObserverMock() { 124 getCoprocessor().resetMock(); 125 } 126 127 @BeforeClass 128 public static void setupBeforeClass() throws Exception { 129 Configuration conf = UTIL.getConfiguration(); 130 conf.set(CoprocessorHost.CLIENT_META_COPROCESSOR_CONF_KEY, TestCoprocessor.class.getName()); 131 132 UTIL.startMiniCluster(); 133 } 134 135 @AfterClass 136 public static void tearDownAfterClass() throws Exception { 137 UTIL.shutdownMiniCluster(); 138 } 139 140 @Before 141 public void setupBefore() { 142 resetObserverMock(); 143 } 144 145 @Test 146 public void testGetClusterId() throws Exception { 147 ClientMetaObserver observer = getObserverMock(); 148 149 try (AsyncConnectionImpl asyncConnection = (AsyncConnectionImpl) ConnectionFactory 150 .createAsyncConnection(UTIL.getConfiguration()).get()) { 151 ConnectionRegistry connectionRegistry = asyncConnection.getConnectionRegistry(); 152 153 doReturn("cluster-id").when(observer).postGetClusterId(any(), any()); 154 clearInvocations(observer); 155 156 String clusterId = connectionRegistry.getClusterId().get(); 157 assertEquals("cluster-id", clusterId); 158 159 verify(observer).preGetClusterId(any()); 160 verify(observer).postGetClusterId(any(), any()); 161 verifyNoMoreInteractions(observer); 162 } 163 } 164 165 @Test 166 public void testGetActiveMaster() throws Exception { 167 ClientMetaObserver observer = getObserverMock(); 168 169 try (AsyncConnectionImpl asyncConnection = (AsyncConnectionImpl) ConnectionFactory 170 .createAsyncConnection(UTIL.getConfiguration()).get()) { 171 ConnectionRegistry connectionRegistry = asyncConnection.getConnectionRegistry(); 172 173 doReturn(SERVER_NAME).when(observer).postGetActiveMaster(any(), any()); 174 clearInvocations(observer); 175 176 ServerName activeMaster = connectionRegistry.getActiveMaster().get(); 177 assertEquals(SERVER_NAME, activeMaster); 178 179 verify(observer).preGetActiveMaster(any()); 180 verify(observer).postGetActiveMaster(any(), any()); 181 verifyNoMoreInteractions(observer); 182 } 183 } 184 185 @Test 186 public void testGetMetaRegionLocations() throws Exception { 187 ClientMetaObserver observer = getObserverMock(); 188 189 try (AsyncConnectionImpl asyncConnection = (AsyncConnectionImpl) ConnectionFactory 190 .createAsyncConnection(UTIL.getConfiguration()).get()) { 191 ConnectionRegistry connectionRegistry = asyncConnection.getConnectionRegistry(); 192 193 HRegionLocation metaRegionLocation = 194 new HRegionLocation(RegionInfoBuilder.FIRST_META_REGIONINFO, SERVER_NAME); 195 doReturn(List.of(metaRegionLocation)).when(observer).postGetMetaLocations(any(), any()); 196 clearInvocations(observer); 197 198 RegionLocations regionLocations = connectionRegistry.getMetaRegionLocations().get(); 199 HRegionLocation actualMetaRegionLocation = regionLocations.getDefaultRegionLocation(); 200 201 assertEquals(metaRegionLocation, actualMetaRegionLocation); 202 203 verify(observer).preGetMetaLocations(any()); 204 verify(observer).postGetMetaLocations(any(), any()); 205 verifyNoMoreInteractions(observer); 206 } 207 } 208}