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