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.assignment; 019 020import java.io.IOException; 021import org.apache.hadoop.hbase.ServerName; 022import org.apache.hadoop.hbase.client.RegionInfo; 023import org.apache.hadoop.hbase.exceptions.UnexpectedStateException; 024import org.apache.hadoop.hbase.master.RegionState.State; 025import org.apache.hadoop.hbase.master.procedure.MasterProcedureEnv; 026import org.apache.hadoop.hbase.master.procedure.RSProcedureDispatcher.RegionOpenOperation; 027import org.apache.hadoop.hbase.procedure2.ProcedureMetrics; 028import org.apache.hadoop.hbase.procedure2.ProcedureStateSerializer; 029import org.apache.hadoop.hbase.procedure2.RemoteProcedureDispatcher.RemoteOperation; 030import org.apache.yetus.audience.InterfaceAudience; 031import org.slf4j.Logger; 032import org.slf4j.LoggerFactory; 033 034import org.apache.hadoop.hbase.shaded.protobuf.generated.MasterProcedureProtos.OpenRegionProcedureStateData; 035import org.apache.hadoop.hbase.shaded.protobuf.generated.RegionServerStatusProtos.RegionStateTransition.TransitionCode; 036 037/** 038 * The remote procedure used to open a region. 039 */ 040@InterfaceAudience.Private 041public class OpenRegionProcedure extends RegionRemoteProcedureBase { 042 043 private static final Logger LOG = LoggerFactory.getLogger(OpenRegionProcedure.class); 044 045 public OpenRegionProcedure() { 046 super(); 047 } 048 049 public OpenRegionProcedure(TransitRegionStateProcedure parent, RegionInfo region, 050 ServerName targetServer) { 051 super(parent, region, targetServer); 052 } 053 054 @Override 055 public TableOperationType getTableOperationType() { 056 return TableOperationType.REGION_ASSIGN; 057 } 058 059 @Override 060 public RemoteOperation newRemoteOperation(MasterProcedureEnv env) { 061 return new RegionOpenOperation(this, region, getProcId(), 062 env.getMasterServices().getMasterActiveTime()); 063 } 064 065 @Override 066 protected void serializeStateData(ProcedureStateSerializer serializer) throws IOException { 067 super.serializeStateData(serializer); 068 serializer.serialize(OpenRegionProcedureStateData.getDefaultInstance()); 069 } 070 071 @Override 072 protected void deserializeStateData(ProcedureStateSerializer serializer) throws IOException { 073 super.deserializeStateData(serializer); 074 serializer.deserialize(OpenRegionProcedureStateData.class); 075 } 076 077 @Override 078 protected ProcedureMetrics getProcedureMetrics(MasterProcedureEnv env) { 079 return env.getAssignmentManager().getAssignmentManagerMetrics().getOpenProcMetrics(); 080 } 081 082 private void regionOpenedWithoutPersistingToMeta(AssignmentManager am, RegionStateNode regionNode, 083 TransitionCode transitionCode, long openSeqNum) throws IOException { 084 if (openSeqNum < regionNode.getOpenSeqNum()) { 085 LOG.warn( 086 "Received report {} transition from {} for {}, pid={} but the new openSeqNum {}" 087 + " is less than the current one {}, ignoring...", 088 transitionCode, targetServer, regionNode, getProcId(), openSeqNum, 089 regionNode.getOpenSeqNum()); 090 } else { 091 regionNode.setOpenSeqNum(openSeqNum); 092 } 093 am.regionOpenedWithoutPersistingToMeta(regionNode); 094 } 095 096 @Override 097 protected void checkTransition(RegionStateNode regionNode, TransitionCode transitionCode, 098 long openSeqNum) throws UnexpectedStateException { 099 switch (transitionCode) { 100 case OPENED: 101 if (openSeqNum < 0) { 102 throw new UnexpectedStateException("Received report unexpected " + TransitionCode.OPENED 103 + " transition openSeqNum=" + openSeqNum + ", " + regionNode + ", proc=" + this); 104 } 105 break; 106 case FAILED_OPEN: 107 break; 108 default: 109 throw new UnexpectedStateException( 110 "Received report unexpected " + transitionCode + " transition, " 111 + regionNode.toShortString() + ", " + this + ", expected OPENED or FAILED_OPEN."); 112 } 113 } 114 115 @Override 116 protected void updateTransitionWithoutPersistingToMeta(MasterProcedureEnv env, 117 RegionStateNode regionNode, TransitionCode transitionCode, long openSeqNum) throws IOException { 118 if (transitionCode == TransitionCode.OPENED) { 119 regionOpenedWithoutPersistingToMeta(env.getAssignmentManager(), regionNode, transitionCode, 120 openSeqNum); 121 } else { 122 assert transitionCode == TransitionCode.FAILED_OPEN; 123 // will not persist to meta if giveUp is false 124 env.getAssignmentManager().regionFailedOpen(regionNode, false); 125 } 126 } 127 128 @Override 129 protected void restoreSucceedState(AssignmentManager am, RegionStateNode regionNode, 130 long openSeqNum) throws IOException { 131 if (regionNode.getState() == State.OPEN) { 132 // should have already been persisted, ignore 133 return; 134 } 135 regionOpenedWithoutPersistingToMeta(am, regionNode, TransitionCode.OPENED, openSeqNum); 136 } 137}