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;
019
020import static org.junit.Assert.assertTrue;
021
022import java.io.IOException;
023import java.util.Arrays;
024import java.util.List;
025import java.util.concurrent.CompletableFuture;
026import org.apache.hadoop.hbase.TestMetaTableAccessor.SpyingRpcScheduler;
027import org.apache.hadoop.hbase.TestMetaTableAccessor.SpyingRpcSchedulerFactory;
028import org.apache.hadoop.hbase.client.AsyncTable;
029import org.apache.hadoop.hbase.client.Delete;
030import org.apache.hadoop.hbase.client.Mutation;
031import org.apache.hadoop.hbase.client.Put;
032import org.apache.hadoop.hbase.client.RegionInfo;
033import org.apache.hadoop.hbase.client.RegionInfoBuilder;
034import org.apache.hadoop.hbase.regionserver.HRegionServer;
035import org.apache.hadoop.hbase.regionserver.RSRpcServices;
036import org.apache.hadoop.hbase.testclassification.MediumTests;
037import org.apache.hadoop.hbase.testclassification.MiscTests;
038import org.apache.hadoop.hbase.util.Bytes;
039import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
040import org.apache.hadoop.hbase.util.FutureUtils;
041import org.junit.AfterClass;
042import org.junit.BeforeClass;
043import org.junit.ClassRule;
044import org.junit.Test;
045import org.junit.experimental.categories.Category;
046
047import org.apache.hadoop.hbase.shaded.protobuf.ProtobufUtil;
048import org.apache.hadoop.hbase.shaded.protobuf.generated.ClientProtos;
049import org.apache.hadoop.hbase.shaded.protobuf.generated.MultiRowMutationProtos.MultiRowMutationService;
050import org.apache.hadoop.hbase.shaded.protobuf.generated.MultiRowMutationProtos.MutateRowsRequest;
051import org.apache.hadoop.hbase.shaded.protobuf.generated.MultiRowMutationProtos.MutateRowsResponse;
052
053@Category({ MiscTests.class, MediumTests.class })
054public class TestMetaUpdatesGoToPriorityQueue {
055
056  @ClassRule
057  public static final HBaseClassTestRule CLASS_RULE =
058    HBaseClassTestRule.forClass(TestMetaUpdatesGoToPriorityQueue.class);
059
060  private static final HBaseTestingUtil UTIL = new HBaseTestingUtil();
061
062  @BeforeClass
063  public static void beforeClass() throws Exception {
064    // This test has to be end-to-end, and do the verification from the server side
065    UTIL.getConfiguration().set(RSRpcServices.REGION_SERVER_RPC_SCHEDULER_FACTORY_CLASS,
066      SpyingRpcSchedulerFactory.class.getName());
067    UTIL.startMiniCluster();
068  }
069
070  @AfterClass
071  public static void afterClass() throws Exception {
072    UTIL.shutdownMiniCluster();
073  }
074
075  private void multiMutate(byte[] row, List<Mutation> mutations) throws IOException {
076    MutateRowsRequest.Builder builder = MutateRowsRequest.newBuilder();
077    for (Mutation mutation : mutations) {
078      if (mutation instanceof Put) {
079        builder.addMutationRequest(
080          ProtobufUtil.toMutation(ClientProtos.MutationProto.MutationType.PUT, mutation));
081      } else if (mutation instanceof Delete) {
082        builder.addMutationRequest(
083          ProtobufUtil.toMutation(ClientProtos.MutationProto.MutationType.DELETE, mutation));
084      } else {
085        throw new DoNotRetryIOException(
086          "multi in MetaEditor doesn't support " + mutation.getClass().getName());
087      }
088    }
089    MutateRowsRequest request = builder.build();
090    AsyncTable<?> table = UTIL.getAsyncConnection().getTable(TableName.META_TABLE_NAME);
091    CompletableFuture<MutateRowsResponse> future = table.<MultiRowMutationService,
092      MutateRowsResponse> coprocessorService(MultiRowMutationService::newStub,
093        (stub, controller, done) -> stub.mutateRows(controller, request, done), row);
094    FutureUtils.get(future);
095  }
096
097  @Test
098  public void test() throws IOException, InterruptedException {
099    TableName tableName = TableName.valueOf(getClass().getSimpleName());
100    // create a table and prepare for a manual split
101    UTIL.createTable(tableName, "cf1");
102    UTIL.waitTableAvailable(tableName);
103    RegionInfo parent = UTIL.getAdmin().getRegions(tableName).get(0);
104    long rid = 1000;
105    byte[] splitKey = Bytes.toBytes("a");
106    RegionInfo splitA =
107      RegionInfoBuilder.newBuilder(parent.getTable()).setStartKey(parent.getStartKey())
108        .setEndKey(splitKey).setSplit(false).setRegionId(rid).build();
109    RegionInfo splitB = RegionInfoBuilder.newBuilder(parent.getTable()).setStartKey(splitKey)
110      .setEndKey(parent.getEndKey()).setSplit(false).setRegionId(rid).build();
111
112    // find the meta server
113    SingleProcessHBaseCluster cluster = UTIL.getMiniHBaseCluster();
114    int rsIndex = cluster.getServerWithMeta();
115    assertTrue(rsIndex >= 0);
116    HRegionServer rs = cluster.getRegionServer(rsIndex);
117    SpyingRpcScheduler scheduler = (SpyingRpcScheduler) rs.getRpcServer().getScheduler();
118    long prevCalls = scheduler.numPriorityCalls;
119    long time = EnvironmentEdgeManager.currentTime();
120    Put putParent = MetaTableAccessor.makePutFromRegionInfo(
121      RegionInfoBuilder.newBuilder(parent).setOffline(true).setSplit(true).build(), time);
122    MetaTableAccessor.addDaughtersToPut(putParent, splitA, splitB);
123    Put putA = MetaTableAccessor.makePutFromRegionInfo(splitA, time);
124    Put putB = MetaTableAccessor.makePutFromRegionInfo(splitB, time);
125    multiMutate(putParent.getRow(), Arrays.asList(putParent, putA, putB));
126
127    assertTrue(prevCalls < scheduler.numPriorityCalls);
128  }
129}