View Javadoc

1   /**
2    * Copyright The Apache Software Foundation
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one or more
5    * contributor license agreements. See the NOTICE file distributed with this
6    * work for additional information regarding copyright ownership. The ASF
7    * licenses this file to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance with the License.
9    * You may obtain a copy of the License at
10   *
11   * http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
16   * License for the specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.hadoop.hbase.regionserver;
20  
21  import java.io.IOException;
22  
23  import org.apache.commons.logging.Log;
24  import org.apache.commons.logging.LogFactory;
25  import org.apache.hadoop.hbase.classification.InterfaceAudience;
26  import org.apache.hadoop.hbase.DroppedSnapshotException;
27  import org.apache.hadoop.hbase.RemoteExceptionHandler;
28  import org.apache.hadoop.hbase.master.TableLockManager.TableLock;
29  import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
30  import org.apache.hadoop.util.StringUtils;
31  
32  import com.google.common.base.Preconditions;
33  
34  /**
35   * Handles processing region merges. Put in a queue, owned by HRegionServer.
36   */
37  @InterfaceAudience.Private
38  class RegionMergeRequest implements Runnable {
39    static final Log LOG = LogFactory.getLog(RegionMergeRequest.class);
40    private final HRegion region_a;
41    private final HRegion region_b;
42    private final HRegionServer server;
43    private final boolean forcible;
44    private TableLock tableLock;
45    private final long masterSystemTime;
46  
47    RegionMergeRequest(HRegion a, HRegion b, HRegionServer hrs, boolean forcible,
48      long masterSystemTime) {
49      Preconditions.checkNotNull(hrs);
50      this.region_a = a;
51      this.region_b = b;
52      this.server = hrs;
53      this.forcible = forcible;
54      this.masterSystemTime = masterSystemTime;
55    }
56  
57    @Override
58    public String toString() {
59      return "MergeRequest,regions:" + region_a + ", " + region_b + ", forcible="
60          + forcible;
61    }
62  
63    @Override
64    public void run() {
65      if (this.server.isStopping() || this.server.isStopped()) {
66        LOG.debug("Skipping merge because server is stopping="
67            + this.server.isStopping() + " or stopped=" + this.server.isStopped());
68        return;
69      }
70      try {
71        final long startTime = EnvironmentEdgeManager.currentTimeMillis();
72        RegionMergeTransaction mt = new RegionMergeTransaction(region_a,
73            region_b, forcible, masterSystemTime);
74  
75        //acquire a shared read lock on the table, so that table schema modifications
76        //do not happen concurrently
77        tableLock = server.getTableLockManager().readLock(region_a.getTableDesc().getTableName()
78            , "MERGE_REGIONS:" + region_a.getRegionNameAsString() + ", " + region_b.getRegionNameAsString());
79        try {
80          tableLock.acquire();
81        } catch (IOException ex) {
82          tableLock = null;
83          throw ex;
84        }
85  
86        // If prepare does not return true, for some reason -- logged inside in
87        // the prepare call -- we are not ready to merge just now. Just return.
88        if (!mt.prepare(this.server)) return;
89        try {
90          mt.execute(this.server, this.server);
91        } catch (Exception e) {
92          if (this.server.isStopping() || this.server.isStopped()) {
93            LOG.info(
94                "Skip rollback/cleanup of failed merge of " + region_a + " and "
95                    + region_b + " because server is"
96                    + (this.server.isStopping() ? " stopping" : " stopped"), e);
97            return;
98          }
99          if (e instanceof DroppedSnapshotException) {
100           server.abort("Replay of WAL required. Forcing server shutdown", e);
101           return;
102         }
103         try {
104           LOG.warn("Running rollback/cleanup of failed merge of "
105                   + region_a +" and "+ region_b + "; " + e.getMessage(), e);
106           if (mt.rollback(this.server, this.server)) {
107             LOG.info("Successful rollback of failed merge of "
108                 + region_a +" and "+ region_b);
109           } else {
110             this.server.abort("Abort; we got an error after point-of-no-return"
111                 + "when merging " + region_a + " and " + region_b);
112           }
113         } catch (RuntimeException ee) {
114           String msg = "Failed rollback of failed merge of "
115               + region_a +" and "+ region_b + " -- aborting server";
116           // If failed rollback, kill this server to avoid having a hole in
117           // table.
118           LOG.info(msg, ee);
119           this.server.abort(msg);
120         }
121         return;
122       }
123       LOG.info("Regions merged, hbase:meta updated, and report to master. region_a="
124           + region_a + ", region_b=" + region_b + ",merged region="
125           + mt.getMergedRegionInfo().getRegionNameAsString()
126           + ". Region merge took "
127           + StringUtils.formatTimeDiff(EnvironmentEdgeManager.currentTimeMillis(), startTime));
128     } catch (IOException ex) {
129       LOG.error("Merge failed " + this,
130           RemoteExceptionHandler.checkIOException(ex));
131       server.checkFileSystem();
132     } finally {
133       releaseTableLock();
134     }
135   }
136 
137   protected void releaseTableLock() {
138     if (this.tableLock != null) {
139       try {
140         this.tableLock.release();
141       } catch (IOException ex) {
142         LOG.error("Could not release the table lock (something is really wrong). "
143            + "Aborting this server to avoid holding the lock forever.");
144         this.server.abort("Abort; we got an error when releasing the table lock "
145                          + "on " + region_a.getRegionNameAsString());
146       }
147     }
148   }
149 }