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