001/* 002 * 003 * Licensed to the Apache Software Foundation (ASF) under one 004 * or more contributor license agreements. See the NOTICE file 005 * distributed with this work for additional information 006 * regarding copyright ownership. The ASF licenses this file 007 * to you under the Apache License, Version 2.0 (the 008 * "License"); you may not use this file except in compliance 009 * with the License. You may obtain a copy of the License at 010 * 011 * http://www.apache.org/licenses/LICENSE-2.0 012 * 013 * Unless required by applicable law or agreed to in writing, software 014 * distributed under the License is distributed on an "AS IS" BASIS, 015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 016 * See the License for the specific language governing permissions and 017 * limitations under the License. 018 */ 019package org.apache.hadoop.hbase.regionserver.handler; 020 021import java.io.IOException; 022import org.apache.hadoop.hbase.HConstants; 023import org.apache.hadoop.hbase.Server; 024import org.apache.hadoop.hbase.ServerName; 025import org.apache.hadoop.hbase.client.RegionInfo; 026import org.apache.hadoop.hbase.executor.EventHandler; 027import org.apache.hadoop.hbase.executor.EventType; 028import org.apache.hadoop.hbase.procedure2.Procedure; 029import org.apache.hadoop.hbase.regionserver.HRegion; 030import org.apache.hadoop.hbase.regionserver.RegionServerServices; 031import org.apache.hadoop.hbase.regionserver.RegionServerServices.RegionStateTransitionContext; 032import org.apache.yetus.audience.InterfaceAudience; 033import org.slf4j.Logger; 034import org.slf4j.LoggerFactory; 035import org.apache.hadoop.hbase.shaded.protobuf.generated.RegionServerStatusProtos.RegionStateTransition.TransitionCode; 036 037/** 038 * Handles closing of a region on a region server. 039 * <p/> 040 * In normal operation, we use {@link UnassignRegionHandler} closing Regions but when shutting down 041 * the region server and closing out Regions, we use this handler instead; it does not expect to 042 * be able to communicate the close back to the Master. 043 * <p>Expects that the close *has* been registered in the hosting RegionServer before 044 * submitting this Handler; i.e. <code>rss.getRegionsInTransitionInRS().putIfAbsent( 045 * this.regionInfo.getEncodedNameAsBytes(), Boolean.FALSE);</code> has been called first. 046 * In here when done, we do the deregister.</p> 047 * @see UnassignRegionHandler 048 */ 049@InterfaceAudience.Private 050public class CloseRegionHandler extends EventHandler { 051 // NOTE on priorities shutting down. There are none for close. There are some 052 // for open. I think that is right. On shutdown, we want the meta to close 053 // after the user regions have closed. What 054 // about the case where master tells us to shutdown a catalog region and we 055 // have a running queue of user regions to close? 056 private static final Logger LOG = LoggerFactory.getLogger(CloseRegionHandler.class); 057 058 private final RegionServerServices rsServices; 059 private final RegionInfo regionInfo; 060 061 // If true, the hosting server is aborting. Region close process is different 062 // when we are aborting. 063 private final boolean abort; 064 private ServerName destination; 065 066 /** 067 * This method used internally by the RegionServer to close out regions. 068 * @param abort If the regionserver is aborting. 069 */ 070 public CloseRegionHandler(final Server server, 071 final RegionServerServices rsServices, 072 final RegionInfo regionInfo, final boolean abort, 073 ServerName destination) { 074 this(server, rsServices, regionInfo, abort, 075 EventType.M_RS_CLOSE_REGION, destination); 076 } 077 078 protected CloseRegionHandler(final Server server, 079 final RegionServerServices rsServices, RegionInfo regionInfo, 080 boolean abort, EventType eventType, ServerName destination) { 081 super(server, eventType); 082 this.server = server; 083 this.rsServices = rsServices; 084 this.regionInfo = regionInfo; 085 this.abort = abort; 086 this.destination = destination; 087 } 088 089 public RegionInfo getRegionInfo() { 090 return regionInfo; 091 } 092 093 @Override 094 public void process() throws IOException { 095 String name = regionInfo.getEncodedName(); 096 LOG.trace("Processing close of {}", name); 097 String encodedRegionName = regionInfo.getEncodedName(); 098 // Check that this region is being served here 099 HRegion region = (HRegion)rsServices.getRegion(encodedRegionName); 100 try { 101 if (region == null) { 102 LOG.warn("Received CLOSE for region {} but currently not serving - ignoring", name); 103 // TODO: do better than a simple warning 104 return; 105 } 106 107 // Close the region 108 if (region.close(abort) == null) { 109 // This region has already been closed. Should not happen (A unit test makes this 110 // happen as a side effect, TestRegionObserverInterface.testPreWALAppendNotCalledOnMetaEdit) 111 LOG.warn("Can't close {}; already closed", name); 112 return; 113 } 114 115 this.rsServices.removeRegion(region, destination); 116 rsServices.reportRegionStateTransition(new RegionStateTransitionContext(TransitionCode.CLOSED, 117 HConstants.NO_SEQNUM, Procedure.NO_PROC_ID, -1, regionInfo)); 118 119 // Done! Region is closed on this RS 120 LOG.debug("Closed {}", region.getRegionInfo().getRegionNameAsString()); 121 } finally { 122 // Clear any reference in getServer().getRegionsInTransitionInRS() on success or failure, 123 // since a reference was added before this CRH was invoked. If we don't clear it, it can 124 // hold up regionserver abort on cluster shutdown. HBASE-23984. 125 this.rsServices.getRegionsInTransitionInRS().remove(regionInfo.getEncodedNameAsBytes()); 126 } 127 } 128 129 @Override protected void handleException(Throwable t) { 130 server.abort("Unrecoverable exception while closing " + 131 this.regionInfo.getRegionNameAsString(), t); 132 } 133}