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 java.io.IOException;
021import org.apache.hadoop.hbase.Server;
022import org.apache.hadoop.hbase.ServerName;
023import org.apache.hadoop.hbase.client.RegionInfo;
024import org.apache.hadoop.hbase.executor.EventHandler;
025import org.apache.hadoop.hbase.executor.EventType;
026import org.apache.hadoop.hbase.procedure2.Procedure;
027import org.apache.hadoop.hbase.regionserver.HRegion;
028import org.apache.hadoop.hbase.regionserver.RegionServerServices;
029import org.apache.hadoop.hbase.regionserver.RegionServerServices.RegionStateTransitionContext;
030import org.apache.yetus.audience.InterfaceAudience;
031import org.slf4j.Logger;
032import org.slf4j.LoggerFactory;
033
034import org.apache.hadoop.hbase.shaded.protobuf.generated.RegionServerStatusProtos.RegionStateTransition.TransitionCode;
035
036/**
037 * Handles closing of a region on a region server.
038 * <p/>
039 * In normal operation, we use {@link UnassignRegionHandler} closing Regions but when shutting down
040 * the region server and closing out Regions, we use this handler instead; it does not expect to be
041 * able to communicate the close back to the Master.
042 * <p>
043 * Expects that the close *has* been registered in the hosting RegionServer before submitting this
044 * Handler; i.e. <code>rss.getRegionsInTransitionInRS().putIfAbsent(
045 * this.regionInfo.getEncodedNameAsBytes(), Boolean.FALSE);</code> has been called first. In here
046 * when done, we do the deregister.
047 * </p>
048 * @see UnassignRegionHandler
049 */
050@InterfaceAudience.Private
051public class CloseRegionHandler extends EventHandler {
052  // NOTE on priorities shutting down. There are none for close. There are some
053  // for open. I think that is right. On shutdown, we want the meta to close
054  // after the user regions have closed. What
055  // about the case where master tells us to shutdown a catalog region and we
056  // have a running queue of user regions to close?
057  private static final Logger LOG = LoggerFactory.getLogger(CloseRegionHandler.class);
058
059  private final RegionServerServices rsServices;
060  private final RegionInfo regionInfo;
061
062  // If true, the hosting server is aborting. Region close process is different
063  // when we are aborting.
064  private final boolean abort;
065  private ServerName destination;
066
067  /**
068   * This method used internally by the RegionServer to close out regions.
069   * @param abort If the regionserver is aborting.
070   */
071  public CloseRegionHandler(final Server server, final RegionServerServices rsServices,
072    final RegionInfo regionInfo, final boolean abort, ServerName destination) {
073    this(server, rsServices, regionInfo, abort, EventType.M_RS_CLOSE_REGION, destination);
074  }
075
076  protected CloseRegionHandler(final Server server, final RegionServerServices rsServices,
077    RegionInfo regionInfo, boolean abort, EventType eventType, ServerName destination) {
078    super(server, eventType);
079    this.server = server;
080    this.rsServices = rsServices;
081    this.regionInfo = regionInfo;
082    this.abort = abort;
083    this.destination = destination;
084  }
085
086  public RegionInfo getRegionInfo() {
087    return regionInfo;
088  }
089
090  @Override
091  public void process() throws IOException {
092    String name = regionInfo.getEncodedName();
093    LOG.trace("Processing close of {}", name);
094    // Check that this region is being served here
095    HRegion region = (HRegion) rsServices.getRegion(name);
096    try {
097      if (region == null) {
098        LOG.warn("Received CLOSE for region {} but currently not serving - ignoring", name);
099        // TODO: do better than a simple warning
100        return;
101      }
102
103      // Close the region
104      if (region.close(abort, false, true) == null) {
105        // This region has already been closed. Should not happen (A unit test makes this
106        // happen as a side effect, TestRegionObserverInterface.testPreWALAppendNotCalledOnMetaEdit)
107        LOG.warn("Can't close {}; already closed", name);
108        return;
109      }
110
111      this.rsServices.removeRegion(region, destination);
112      // Report the region's durable flushed seqid on CLOSE so the master can seed its
113      // flushedSequenceIdByRegion watermark. This complements the OPEN-time seed (HBASE-30335)
114      // and narrows the graceful-close window where a subsequent WAL split of a crashed source
115      // RS could otherwise write orphaned recovered.edits for already-durable edits.
116      rsServices.reportRegionStateTransition(new RegionStateTransitionContext(TransitionCode.CLOSED,
117        region.getMaxFlushedSeqId(), Procedure.NO_PROC_ID, -1, regionInfo, -1));
118
119      // Done! Region is closed on this RS
120      LOG.debug("Closed {}", region.getRegionInfo().getRegionNameAsString());
121    } finally {
122      // Clear any reference in getServer().getRegionsInTransitionInRS() on success or failure,
123      // since a reference was added before this CRH was invoked. If we don't clear it, it can
124      // hold up regionserver abort on cluster shutdown. HBASE-23984.
125      this.rsServices.getRegionsInTransitionInRS().remove(regionInfo.getEncodedNameAsBytes());
126    }
127  }
128
129  @Override
130  protected void handleException(Throwable t) {
131    server.abort("Unrecoverable exception while closing " + this.regionInfo.getRegionNameAsString(),
132      t);
133  }
134}