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.conf.Configuration; 024import org.apache.hadoop.hbase.HConstants; 025import org.apache.hadoop.hbase.TableName; 026import org.apache.hadoop.hbase.client.RegionInfo; 027import org.apache.hadoop.hbase.client.RegionReplicaUtil; 028import org.apache.hadoop.hbase.client.TableDescriptor; 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.PostOpenDeployContext; 035import org.apache.hadoop.hbase.regionserver.RegionServerServices.RegionStateTransitionContext; 036import org.apache.hadoop.hbase.util.RetryCounter; 037import org.apache.hadoop.hbase.util.ServerRegionReplicaUtil; 038import org.apache.yetus.audience.InterfaceAudience; 039import org.slf4j.Logger; 040import org.slf4j.LoggerFactory; 041 042import org.apache.hadoop.hbase.shaded.protobuf.generated.RegionServerStatusProtos.RegionStateTransition.TransitionCode; 043 044/** 045 * Handles opening of a region on a region server. 046 * <p/> 047 * Just done the same thing with the old {@link OpenRegionHandler}, with some modifications on 048 * fencing and retrying. But we need to keep the {@link OpenRegionHandler} 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 AssignRegionHandler extends EventHandler { 053 054 private static final Logger LOG = LoggerFactory.getLogger(AssignRegionHandler.class); 055 056 private final RegionInfo regionInfo; 057 058 private final long openProcId; 059 060 private final TableDescriptor tableDesc; 061 062 private final long masterSystemTime; 063 064 private final RetryCounter retryCounter; 065 066 public AssignRegionHandler(HRegionServer server, RegionInfo regionInfo, long openProcId, 067 @Nullable TableDescriptor tableDesc, long masterSystemTime, EventType eventType) { 068 super(server, eventType); 069 this.regionInfo = regionInfo; 070 this.openProcId = openProcId; 071 this.tableDesc = tableDesc; 072 this.masterSystemTime = masterSystemTime; 073 this.retryCounter = HandlerUtil.getRetryCounter(); 074 } 075 076 private HRegionServer getServer() { 077 return (HRegionServer) server; 078 } 079 080 private void cleanUpAndReportFailure(IOException error) throws IOException { 081 LOG.warn("Failed to open region {}, will report to master", regionInfo.getRegionNameAsString(), 082 error); 083 HRegionServer rs = getServer(); 084 rs.getRegionsInTransitionInRS().remove(regionInfo.getEncodedNameAsBytes(), Boolean.TRUE); 085 if ( 086 !rs.reportRegionStateTransition(new RegionStateTransitionContext(TransitionCode.FAILED_OPEN, 087 HConstants.NO_SEQNUM, openProcId, masterSystemTime, regionInfo)) 088 ) { 089 throw new IOException( 090 "Failed to report failed open to master: " + regionInfo.getRegionNameAsString()); 091 } 092 } 093 094 @Override 095 public void process() throws IOException { 096 HRegionServer rs = getServer(); 097 String encodedName = regionInfo.getEncodedName(); 098 byte[] encodedNameBytes = regionInfo.getEncodedNameAsBytes(); 099 String regionName = regionInfo.getRegionNameAsString(); 100 Region onlineRegion = rs.getRegion(encodedName); 101 if (onlineRegion != null) { 102 LOG.warn("Received OPEN for {} which is already online", regionName); 103 // Just follow the old behavior, do we need to call reportRegionStateTransition? Maybe not? 104 // For normal case, it could happen that the rpc call to schedule this handler is succeeded, 105 // but before returning to master the connection is broken. And when master tries again, we 106 // have already finished the opening. For this case we do not need to call 107 // reportRegionStateTransition any more. 108 return; 109 } 110 Boolean previous = rs.getRegionsInTransitionInRS().putIfAbsent(encodedNameBytes, Boolean.TRUE); 111 if (previous != null) { 112 if (previous) { 113 // The region is opening and this maybe a retry on the rpc call, it is safe to ignore it. 114 LOG.info("Receiving OPEN for {} which we are already trying to OPEN" 115 + " - ignoring this new request for this region.", regionName); 116 } else { 117 // The region is closing. This is possible as we will update the region state to CLOSED when 118 // calling reportRegionStateTransition, so the HMaster will think the region is offline, 119 // before we actually close the region, as reportRegionStateTransition is part of the 120 // closing process. 121 long backoff = retryCounter.getBackoffTimeAndIncrementAttempts(); 122 LOG.info("Receiving OPEN for {} which we are trying to close, try again after {}ms", 123 regionName, backoff); 124 rs.getExecutorService().delayedSubmit(this, backoff, TimeUnit.MILLISECONDS); 125 } 126 return; 127 } 128 LOG.info("Open {}", regionName); 129 HRegion region; 130 try { 131 TableDescriptor htd = 132 tableDesc != null ? tableDesc : rs.getTableDescriptors().get(regionInfo.getTable()); 133 if (htd == null) { 134 throw new IOException("Missing table descriptor for " + regionName); 135 } 136 // pass null for the last parameter, which used to be a CancelableProgressable, as now the 137 // opening can not be interrupted by a close request any more. 138 Configuration conf = rs.getConfiguration(); 139 TableName tn = htd.getTableName(); 140 if (ServerRegionReplicaUtil.isMetaRegionReplicaReplicationEnabled(conf, tn)) { 141 if (RegionReplicaUtil.isDefaultReplica(this.regionInfo.getReplicaId())) { 142 // Add the hbase:meta replication source on replica zero/default. 143 rs.getReplicationSourceService().getReplicationManager() 144 .addCatalogReplicationSource(this.regionInfo); 145 } 146 } 147 region = HRegion.openHRegion(regionInfo, htd, rs.getWAL(regionInfo), conf, rs, null); 148 } catch (IOException e) { 149 cleanUpAndReportFailure(e); 150 return; 151 } 152 // From here on out, this is PONR. We can not revert back. The only way to address an 153 // exception from here on out is to abort the region server. 154 rs.postOpenDeployTasks(new PostOpenDeployContext(region, openProcId, masterSystemTime)); 155 rs.addRegion(region); 156 LOG.info("Opened {}", regionName); 157 // Cache the open region procedure id after report region transition succeed. 158 rs.finishRegionProcedure(openProcId); 159 Boolean current = rs.getRegionsInTransitionInRS().remove(regionInfo.getEncodedNameAsBytes()); 160 if (current == null) { 161 // Should NEVER happen, but let's be paranoid. 162 LOG.error("Bad state: we've just opened {} which was NOT in transition", regionName); 163 } else if (!current) { 164 // Should NEVER happen, but let's be paranoid. 165 LOG.error("Bad state: we've just opened {} which was closing", regionName); 166 } 167 } 168 169 @Override 170 protected void handleException(Throwable t) { 171 LOG.warn("Fatal error occurred while opening region {}, aborting...", 172 regionInfo.getRegionNameAsString(), t); 173 // Clear any reference in getServer().getRegionsInTransitionInRS() otherwise can hold up 174 // regionserver abort on cluster shutdown. HBASE-23984. 175 getServer().getRegionsInTransitionInRS().remove(regionInfo.getEncodedNameAsBytes()); 176 getServer().abort( 177 "Failed to open region " + regionInfo.getRegionNameAsString() + " and can not recover", t); 178 } 179 180 public static AssignRegionHandler create(HRegionServer server, RegionInfo regionInfo, 181 long openProcId, TableDescriptor tableDesc, long masterSystemTime) { 182 EventType eventType; 183 if (regionInfo.isMetaRegion()) { 184 eventType = EventType.M_RS_OPEN_META; 185 } else if ( 186 regionInfo.getTable().isSystemTable() 187 || (tableDesc != null && tableDesc.getPriority() >= HConstants.ADMIN_QOS) 188 ) { 189 eventType = EventType.M_RS_OPEN_PRIORITY_REGION; 190 } else { 191 eventType = EventType.M_RS_OPEN_REGION; 192 } 193 return new AssignRegionHandler(server, regionInfo, openProcId, tableDesc, masterSystemTime, 194 eventType); 195 } 196}