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.apache.hadoop.hbase.HConstants.HIGH_QOS; 021import static org.apache.hadoop.hbase.HConstants.NORMAL_QOS; 022import static org.apache.hadoop.hbase.HConstants.SYSTEMTABLE_QOS; 023import static org.apache.hadoop.hbase.NamespaceDescriptor.SYSTEM_NAMESPACE_NAME_STR; 024import static org.mockito.ArgumentMatchers.any; 025import static org.mockito.ArgumentMatchers.argThat; 026import static org.mockito.Mockito.doAnswer; 027import static org.mockito.Mockito.mock; 028import static org.mockito.Mockito.times; 029import static org.mockito.Mockito.verify; 030 031import java.io.IOException; 032import java.util.concurrent.CompletableFuture; 033import org.apache.hadoop.conf.Configuration; 034import org.apache.hadoop.hbase.HBaseConfiguration; 035import org.apache.hadoop.hbase.ServerName; 036import org.apache.hadoop.hbase.TableName; 037import org.apache.hadoop.hbase.ipc.HBaseRpcController; 038import org.apache.hadoop.hbase.security.User; 039import org.apache.hadoop.hbase.security.UserProvider; 040import org.apache.hadoop.hbase.testclassification.ClientTests; 041import org.apache.hadoop.hbase.testclassification.SmallTests; 042import org.junit.jupiter.api.BeforeEach; 043import org.junit.jupiter.api.Tag; 044import org.junit.jupiter.api.Test; 045import org.junit.jupiter.api.TestInfo; 046import org.mockito.ArgumentMatcher; 047import org.mockito.invocation.InvocationOnMock; 048import org.mockito.stubbing.Answer; 049 050import org.apache.hbase.thirdparty.com.google.protobuf.RpcCallback; 051 052import org.apache.hadoop.hbase.shaded.protobuf.generated.AdminProtos.AdminService; 053import org.apache.hadoop.hbase.shaded.protobuf.generated.AdminProtos.AdminService.Interface; 054import org.apache.hadoop.hbase.shaded.protobuf.generated.AdminProtos.StopServerRequest; 055import org.apache.hadoop.hbase.shaded.protobuf.generated.AdminProtos.StopServerResponse; 056import org.apache.hadoop.hbase.shaded.protobuf.generated.MasterProtos.CreateTableRequest; 057import org.apache.hadoop.hbase.shaded.protobuf.generated.MasterProtos.CreateTableResponse; 058import org.apache.hadoop.hbase.shaded.protobuf.generated.MasterProtos.GetProcedureResultRequest; 059import org.apache.hadoop.hbase.shaded.protobuf.generated.MasterProtos.GetProcedureResultResponse; 060import org.apache.hadoop.hbase.shaded.protobuf.generated.MasterProtos.MasterService; 061import org.apache.hadoop.hbase.shaded.protobuf.generated.MasterProtos.ShutdownRequest; 062import org.apache.hadoop.hbase.shaded.protobuf.generated.MasterProtos.ShutdownResponse; 063import org.apache.hadoop.hbase.shaded.protobuf.generated.MasterProtos.StopMasterRequest; 064import org.apache.hadoop.hbase.shaded.protobuf.generated.MasterProtos.StopMasterResponse; 065 066/** 067 * Confirm that we will set the priority in {@link HBaseRpcController} for several admin operations. 068 */ 069@Tag(ClientTests.TAG) 070@Tag(SmallTests.TAG) 071public class TestAsyncAdminRpcPriority { 072 073 private static Configuration CONF = HBaseConfiguration.create(); 074 075 private MasterService.Interface masterStub; 076 077 private AdminService.Interface adminStub; 078 079 private AsyncConnection conn; 080 081 public String name; 082 083 @BeforeEach 084 public void setUp(TestInfo testInfo) throws IOException { 085 name = testInfo.getTestMethod().get().getName(); 086 masterStub = mock(MasterService.Interface.class); 087 adminStub = mock(AdminService.Interface.class); 088 doAnswer(new Answer<Void>() { 089 090 @Override 091 public Void answer(InvocationOnMock invocation) throws Throwable { 092 RpcCallback<GetProcedureResultResponse> done = invocation.getArgument(2); 093 done.run(GetProcedureResultResponse.newBuilder() 094 .setState(GetProcedureResultResponse.State.FINISHED).build()); 095 return null; 096 } 097 }).when(masterStub).getProcedureResult(any(HBaseRpcController.class), 098 any(GetProcedureResultRequest.class), any()); 099 doAnswer(new Answer<Void>() { 100 101 @Override 102 public Void answer(InvocationOnMock invocation) throws Throwable { 103 RpcCallback<CreateTableResponse> done = invocation.getArgument(2); 104 done.run(CreateTableResponse.newBuilder().setProcId(1L).build()); 105 return null; 106 } 107 }).when(masterStub).createTable(any(HBaseRpcController.class), any(CreateTableRequest.class), 108 any()); 109 doAnswer(new Answer<Void>() { 110 111 @Override 112 public Void answer(InvocationOnMock invocation) throws Throwable { 113 RpcCallback<ShutdownResponse> done = invocation.getArgument(2); 114 done.run(ShutdownResponse.getDefaultInstance()); 115 return null; 116 } 117 }).when(masterStub).shutdown(any(HBaseRpcController.class), any(ShutdownRequest.class), any()); 118 doAnswer(new Answer<Void>() { 119 120 @Override 121 public Void answer(InvocationOnMock invocation) throws Throwable { 122 RpcCallback<StopMasterResponse> done = invocation.getArgument(2); 123 done.run(StopMasterResponse.getDefaultInstance()); 124 return null; 125 } 126 }).when(masterStub).stopMaster(any(HBaseRpcController.class), any(StopMasterRequest.class), 127 any()); 128 129 doAnswer(new Answer<Void>() { 130 131 @Override 132 public Void answer(InvocationOnMock invocation) throws Throwable { 133 RpcCallback<StopServerResponse> done = invocation.getArgument(2); 134 done.run(StopServerResponse.getDefaultInstance()); 135 return null; 136 } 137 }).when(adminStub).stopServer(any(HBaseRpcController.class), any(StopServerRequest.class), 138 any()); 139 User user = UserProvider.instantiate(CONF).getCurrent(); 140 conn = new AsyncConnectionImpl(CONF, new DoNothingConnectionRegistry(CONF, user), "test", null, 141 user) { 142 143 @Override 144 CompletableFuture<MasterService.Interface> getMasterStub() { 145 return CompletableFuture.completedFuture(masterStub); 146 } 147 148 @Override 149 Interface getAdminStub(ServerName serverName) throws IOException { 150 return adminStub; 151 } 152 }; 153 } 154 155 private HBaseRpcController assertPriority(int priority) { 156 return argThat(new ArgumentMatcher<HBaseRpcController>() { 157 158 @Override 159 public boolean matches(HBaseRpcController controller) { 160 return controller.getPriority() == priority; 161 } 162 }); 163 } 164 165 @Test 166 public void testCreateNormalTable() { 167 conn.getAdmin().createTable(TableDescriptorBuilder.newBuilder(TableName.valueOf(name)) 168 .setColumnFamily(ColumnFamilyDescriptorBuilder.of("cf")).build()).join(); 169 verify(masterStub, times(1)).createTable(assertPriority(NORMAL_QOS), 170 any(CreateTableRequest.class), any()); 171 } 172 173 // a bit strange as we can not do this in production but anyway, just a client mock to confirm 174 // that we pass the correct priority 175 @Test 176 public void testCreateSystemTable() { 177 conn.getAdmin() 178 .createTable( 179 TableDescriptorBuilder.newBuilder(TableName.valueOf(SYSTEM_NAMESPACE_NAME_STR, name)) 180 .setColumnFamily(ColumnFamilyDescriptorBuilder.of("cf")).build()) 181 .join(); 182 verify(masterStub, times(1)).createTable(assertPriority(SYSTEMTABLE_QOS), 183 any(CreateTableRequest.class), any()); 184 } 185 186 // a bit strange as we can not do this in production but anyway, just a client mock to confirm 187 // that we pass the correct priority 188 @Test 189 public void testCreateMetaTable() { 190 conn.getAdmin().createTable(TableDescriptorBuilder.newBuilder(TableName.META_TABLE_NAME) 191 .setColumnFamily(ColumnFamilyDescriptorBuilder.of("cf")).build()).join(); 192 verify(masterStub, times(1)).createTable(assertPriority(SYSTEMTABLE_QOS), 193 any(CreateTableRequest.class), any()); 194 } 195 196 @Test 197 public void testShutdown() { 198 conn.getAdmin().shutdown().join(); 199 verify(masterStub, times(1)).shutdown(assertPriority(HIGH_QOS), any(ShutdownRequest.class), 200 any()); 201 } 202 203 @Test 204 public void testStopMaster() { 205 conn.getAdmin().stopMaster().join(); 206 verify(masterStub, times(1)).stopMaster(assertPriority(HIGH_QOS), any(StopMasterRequest.class), 207 any()); 208 } 209 210 @Test 211 public void testStopRegionServer() { 212 conn.getAdmin().stopRegionServer(ServerName.valueOf("rs", 16010, 12345)).join(); 213 verify(adminStub, times(1)).stopServer(assertPriority(HIGH_QOS), any(StopServerRequest.class), 214 any()); 215 } 216}