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