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.master;
019
020import org.apache.hadoop.hbase.HConstants;
021import org.apache.hadoop.hbase.TableName;
022import org.apache.hadoop.hbase.regionserver.AnnotationReadingPriorityFunction;
023import org.apache.hadoop.hbase.regionserver.RSRpcServices;
024import org.apache.hadoop.hbase.security.User;
025import org.apache.yetus.audience.InterfaceAudience;
026
027import org.apache.hbase.thirdparty.com.google.protobuf.Message;
028
029import org.apache.hadoop.hbase.shaded.protobuf.ProtobufUtil;
030import org.apache.hadoop.hbase.shaded.protobuf.generated.HBaseProtos;
031import org.apache.hadoop.hbase.shaded.protobuf.generated.RPCProtos;
032import org.apache.hadoop.hbase.shaded.protobuf.generated.RegionServerStatusProtos;
033
034/**
035 * Priority function specifically for the master.
036 *
037 * This doesn't make the super users always priority since that would make everything
038 * to the master into high priority.
039 *
040 * Specifically when reporting that a region is in transition master will try and edit the meta
041 * table. That edit will block the thread until successful. However if at the same time meta is
042 * also moving then we need to ensure that the regular region that's moving isn't blocking
043 * processing of the request to online meta. To accomplish this this priority function makes sure
044 * that all requests to transition meta are handled in different threads from other report region
045 * in transition calls.
046 */
047@InterfaceAudience.Private
048public class MasterAnnotationReadingPriorityFunction extends AnnotationReadingPriorityFunction {
049  public MasterAnnotationReadingPriorityFunction(final RSRpcServices rpcServices) {
050    this(rpcServices, rpcServices.getClass());
051  }
052
053
054  public MasterAnnotationReadingPriorityFunction(RSRpcServices rpcServices,
055                                          Class<? extends RSRpcServices> clz) {
056    super(rpcServices, clz);
057  }
058
059  @Override
060  public int getPriority(RPCProtos.RequestHeader header, Message param, User user) {
061    // Yes this is copy pasted from the base class but it keeps from having to look in the
062    // annotatedQos table twice something that could get costly since this is called for
063    // every single RPC request.
064    int priorityByAnnotation = getAnnotatedPriority(header);
065    if (priorityByAnnotation >= 0) {
066      return priorityByAnnotation;
067    }
068
069    // If meta is moving then all the other of reports of state transitions will be
070    // un able to edit meta. Those blocked reports should not keep the report that opens meta from
071    // running. Hence all reports of meta transitioning should always be in a different thread.
072    // This keeps from deadlocking the cluster.
073    if (param instanceof RegionServerStatusProtos.ReportRegionStateTransitionRequest) {
074      // Regions are moving. Lets see which ones.
075      RegionServerStatusProtos.ReportRegionStateTransitionRequest
076          tRequest = (RegionServerStatusProtos.ReportRegionStateTransitionRequest) param;
077      for (RegionServerStatusProtos.RegionStateTransition rst : tRequest.getTransitionList()) {
078        if (rst.getRegionInfoList() != null) {
079          for (HBaseProtos.RegionInfo info : rst.getRegionInfoList()) {
080            TableName tn = ProtobufUtil.toTableName(info.getTableName());
081            if (tn.isSystemTable()) {
082              return HConstants.SYSTEMTABLE_QOS;
083            }
084          }
085        }
086      }
087      return HConstants.NORMAL_QOS;
088    }
089
090    // Handle the rest of the different reasons to change priority.
091    return getBasePriority(header, param);
092  }
093}