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.regionserver.handler; 019 020import static org.apache.hadoop.hbase.io.hfile.CacheConfig.DEFAULT_EVICT_ON_CLOSE; 021import static org.apache.hadoop.hbase.io.hfile.CacheConfig.DEFAULT_EVICT_ON_SPLIT; 022import static org.apache.hadoop.hbase.io.hfile.CacheConfig.EVICT_BLOCKS_ON_CLOSE_KEY; 023import static org.apache.hadoop.hbase.io.hfile.CacheConfig.EVICT_BLOCKS_ON_SPLIT_KEY; 024 025import edu.umd.cs.findbugs.annotations.Nullable; 026import java.io.IOException; 027import java.util.concurrent.TimeUnit; 028import org.apache.hadoop.hbase.ServerName; 029import org.apache.hadoop.hbase.executor.EventHandler; 030import org.apache.hadoop.hbase.executor.EventType; 031import org.apache.hadoop.hbase.regionserver.HRegion; 032import org.apache.hadoop.hbase.regionserver.HRegionServer; 033import org.apache.hadoop.hbase.regionserver.Region; 034import org.apache.hadoop.hbase.regionserver.RegionServerServices.RegionStateTransitionContext; 035import org.apache.hadoop.hbase.util.Bytes; 036import org.apache.hadoop.hbase.util.RetryCounter; 037import org.apache.yetus.audience.InterfaceAudience; 038import org.slf4j.Logger; 039import org.slf4j.LoggerFactory; 040import org.slf4j.MDC; 041 042import org.apache.hadoop.hbase.shaded.protobuf.generated.RegionServerStatusProtos.RegionStateTransition.TransitionCode; 043 044/** 045 * Handles closing of a region on a region server. 046 * <p/> 047 * Just done the same thing with the old {@link CloseRegionHandler}, with some modifications on 048 * fencing and retrying. But we need to keep the {@link CloseRegionHandler} as is to keep compatible 049 * with the zk less assignment for 1.x, otherwise it is not possible to do rolling upgrade. 050 */ 051@InterfaceAudience.Private 052public class UnassignRegionHandler extends EventHandler { 053 054 private static final Logger LOG = LoggerFactory.getLogger(UnassignRegionHandler.class); 055 056 private final String encodedName; 057 058 private final long closeProcId; 059 // If true, the hosting server is aborting. Region close process is different 060 // when we are aborting. 061 // TODO: not used yet, we still use the old CloseRegionHandler when aborting 062 private final boolean abort; 063 064 private final ServerName destination; 065 066 private final RetryCounter retryCounter; 067 068 private boolean isSplit; 069 070 // active time of the master that sent this unassign request, used for fencing 071 private final long initiatingMasterActiveTime; 072 073 public UnassignRegionHandler(HRegionServer server, String encodedName, long closeProcId, 074 boolean abort, @Nullable ServerName destination, EventType eventType, 075 long initiatingMasterActiveTime, boolean isSplit) { 076 super(server, eventType); 077 this.encodedName = encodedName; 078 this.closeProcId = closeProcId; 079 this.abort = abort; 080 this.destination = destination; 081 this.retryCounter = HandlerUtil.getRetryCounter(); 082 this.isSplit = isSplit; 083 this.initiatingMasterActiveTime = initiatingMasterActiveTime; 084 } 085 086 private HRegionServer getServer() { 087 return (HRegionServer) server; 088 } 089 090 @Override 091 public void process() throws IOException { 092 MDC.put("pid", Long.toString(closeProcId)); 093 HRegionServer rs = getServer(); 094 byte[] encodedNameBytes = Bytes.toBytes(encodedName); 095 Boolean previous = rs.getRegionsInTransitionInRS().putIfAbsent(encodedNameBytes, Boolean.FALSE); 096 if (previous != null) { 097 if (previous) { 098 // This could happen as we will update the region state to OPEN when calling 099 // reportRegionStateTransition, so the HMaster will think the region is online, before we 100 // actually open the region, as reportRegionStateTransition is part of the opening process. 101 long backoff = retryCounter.getBackoffTimeAndIncrementAttempts(); 102 LOG.warn( 103 "Received CLOSE for {} which we are already " + "trying to OPEN; try again after {}ms", 104 encodedName, backoff); 105 rs.getExecutorService().delayedSubmit(this, backoff, TimeUnit.MILLISECONDS); 106 } else { 107 LOG.info( 108 "Received CLOSE for {} which we are already trying to CLOSE," + " but not completed yet", 109 encodedName); 110 } 111 return; 112 } 113 HRegion region = rs.getRegion(encodedName); 114 if (region == null) { 115 LOG.debug("Received CLOSE for {} which is not ONLINE and we're not opening/closing.", 116 encodedName); 117 rs.getRegionsInTransitionInRS().remove(encodedNameBytes, Boolean.FALSE); 118 return; 119 } 120 String regionName = region.getRegionInfo().getEncodedName(); 121 LOG.info("Close {}", regionName); 122 if (region.getCoprocessorHost() != null) { 123 // XXX: The behavior is a bit broken. At master side there is no FAILED_CLOSE state, so if 124 // there are exception thrown from the CP, we can not report the error to master, and if 125 // here we just return without calling reportRegionStateTransition, the TRSP at master side 126 // will hang there for ever. So here if the CP throws an exception out, the only way is to 127 // abort the RS... 128 region.getCoprocessorHost().preClose(abort); 129 } 130 // This should be true only in the case of splits/merges closing the parent regions, as 131 // there's no point on keep blocks for those region files. 132 final boolean evictCacheOnClose = isSplit 133 ? server.getConfiguration().getBoolean(EVICT_BLOCKS_ON_SPLIT_KEY, DEFAULT_EVICT_ON_SPLIT) 134 : server.getConfiguration().getBoolean(EVICT_BLOCKS_ON_CLOSE_KEY, DEFAULT_EVICT_ON_CLOSE); 135 LOG.debug("Unassign region: split region: {}: evictCache: {}", isSplit, evictCacheOnClose); 136 region.getStores().forEach(s -> s.getCacheConfig().setEvictOnClose(evictCacheOnClose)); 137 138 if (region.close(abort) == null) { 139 // XXX: Is this still possible? The old comment says about split, but now split is done at 140 // master side, so... 141 LOG.warn("Can't close {}, already closed during close()", regionName); 142 rs.getRegionsInTransitionInRS().remove(encodedNameBytes, Boolean.FALSE); 143 return; 144 } 145 146 rs.removeRegion(region, destination); 147 // Report the region's durable flushed seqid on CLOSE so the master can seed its 148 // flushedSequenceIdByRegion watermark. This complements the OPEN-time seed (HBASE-30335) 149 // and narrows the graceful-close window where a subsequent WAL split of a crashed source 150 // RS could otherwise write orphaned recovered.edits for already-durable edits. 151 if ( 152 !rs.reportRegionStateTransition( 153 new RegionStateTransitionContext(TransitionCode.CLOSED, region.getMaxFlushedSeqId(), 154 closeProcId, -1, region.getRegionInfo(), initiatingMasterActiveTime)) 155 ) { 156 throw new IOException("Failed to report close to master: " + regionName); 157 } 158 // Cache the close region procedure id after report region transition succeed. 159 rs.finishRegionProcedure(closeProcId); 160 rs.getRegionsInTransitionInRS().remove(encodedNameBytes, Boolean.FALSE); 161 LOG.info("Closed {}", regionName); 162 } 163 164 @Override 165 protected void handleException(Throwable t) { 166 LOG.warn("Fatal error occurred while closing region {}, aborting...", encodedName, t); 167 // Clear any reference in getServer().getRegionsInTransitionInRS() otherwise can hold up 168 // regionserver abort on cluster shutdown. HBASE-23984. 169 getServer().getRegionsInTransitionInRS().remove(Bytes.toBytes(this.encodedName)); 170 getServer().abort("Failed to close region " + encodedName + " and can not recover", t); 171 } 172 173 public static UnassignRegionHandler create(HRegionServer server, String encodedName, 174 long closeProcId, boolean abort, @Nullable ServerName destination, boolean evictCache, 175 long initiatingMasterActiveTime) { 176 // Just try our best to determine whether it is for closing meta. It is not the end of the world 177 // if we put the handler into a wrong executor. 178 Region region = server.getRegion(encodedName); 179 EventType eventType = region != null && region.getRegionInfo().isMetaRegion() 180 ? EventType.M_RS_CLOSE_META 181 : EventType.M_RS_CLOSE_REGION; 182 return new UnassignRegionHandler(server, encodedName, closeProcId, abort, destination, 183 eventType, initiatingMasterActiveTime, evictCache); 184 } 185}