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; 022 023import org.apache.hadoop.hbase.HConstants; 024import org.apache.hadoop.hbase.Server; 025import org.apache.hadoop.hbase.ServerName; 026import org.apache.hadoop.hbase.client.RegionInfo; 027import org.apache.hadoop.hbase.executor.EventHandler; 028import org.apache.hadoop.hbase.executor.EventType; 029import org.apache.hadoop.hbase.procedure2.Procedure; 030import org.apache.hadoop.hbase.regionserver.HRegion; 031import org.apache.hadoop.hbase.regionserver.RegionServerServices; 032import org.apache.hadoop.hbase.regionserver.RegionServerServices.RegionStateTransitionContext; 033import org.apache.yetus.audience.InterfaceAudience; 034import org.slf4j.Logger; 035import org.slf4j.LoggerFactory; 036import org.apache.hadoop.hbase.shaded.protobuf.generated.RegionServerStatusProtos.RegionStateTransition.TransitionCode; 037 038/** 039 * Handles closing of a region on a region server. 040 * <p/> 041 * Now for regular close region request, we will use {@link UnassignRegionHandler} instead. But when 042 * shutting down the region server, will also close regions and the related methods still use this 043 * class so we keep it here. 044 * @see UnassignRegionHandler 045 */ 046@InterfaceAudience.Private 047public class CloseRegionHandler extends EventHandler { 048 // NOTE on priorities shutting down. There are none for close. There are some 049 // for open. I think that is right. On shutdown, we want the meta to close 050 // after the user regions have closed. What 051 // about the case where master tells us to shutdown a catalog region and we 052 // have a running queue of user regions to close? 053 private static final Logger LOG = LoggerFactory.getLogger(CloseRegionHandler.class); 054 055 private final RegionServerServices rsServices; 056 private final RegionInfo regionInfo; 057 058 // If true, the hosting server is aborting. Region close process is different 059 // when we are aborting. 060 private final boolean abort; 061 private ServerName destination; 062 063 /** 064 * This method used internally by the RegionServer to close out regions. 065 * @param server 066 * @param rsServices 067 * @param regionInfo 068 * @param abort If the regionserver is aborting. 069 * @param destination 070 */ 071 public CloseRegionHandler(final Server server, 072 final RegionServerServices rsServices, 073 final RegionInfo regionInfo, final boolean abort, 074 ServerName destination) { 075 this(server, rsServices, regionInfo, abort, 076 EventType.M_RS_CLOSE_REGION, destination); 077 } 078 079 protected CloseRegionHandler(final Server server, 080 final RegionServerServices rsServices, RegionInfo regionInfo, 081 boolean abort, EventType eventType, ServerName destination) { 082 super(server, eventType); 083 this.server = server; 084 this.rsServices = rsServices; 085 this.regionInfo = regionInfo; 086 this.abort = abort; 087 this.destination = destination; 088 } 089 090 public RegionInfo getRegionInfo() { 091 return regionInfo; 092 } 093 094 @Override 095 public void process() { 096 try { 097 String name = regionInfo.getEncodedName(); 098 LOG.trace("Processing close of {}", name); 099 String encodedRegionName = regionInfo.getEncodedName(); 100 // Check that this region is being served here 101 HRegion region = (HRegion)rsServices.getRegion(encodedRegionName); 102 if (region == null) { 103 LOG.warn("Received CLOSE for region {} but currently not serving - ignoring", name); 104 // TODO: do better than a simple warning 105 return; 106 } 107 108 // Close the region 109 try { 110 if (region.close(abort) == null) { 111 // This region got closed. Most likely due to a split. 112 // The split message will clean up the master state. 113 LOG.warn("Can't close region {}, was already closed during close()", name); 114 return; 115 } 116 } catch (IOException ioe) { 117 // An IOException here indicates that we couldn't successfully flush the 118 // memstore before closing. So, we need to abort the server and allow 119 // the master to split our logs in order to recover the data. 120 server.abort("Unrecoverable exception while closing region " + 121 regionInfo.getRegionNameAsString() + ", still finishing close", ioe); 122 throw new RuntimeException(ioe); 123 } 124 125 this.rsServices.removeRegion(region, destination); 126 rsServices.reportRegionStateTransition(new RegionStateTransitionContext(TransitionCode.CLOSED, 127 HConstants.NO_SEQNUM, Procedure.NO_PROC_ID, -1, regionInfo)); 128 129 // Done! Region is closed on this RS 130 LOG.debug("Closed " + region.getRegionInfo().getRegionNameAsString()); 131 } finally { 132 this.rsServices.getRegionsInTransitionInRS(). 133 remove(this.regionInfo.getEncodedNameAsBytes(), Boolean.FALSE); 134 } 135 } 136}