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