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