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.RemoteExceptionHandler;
27  import org.apache.hadoop.hbase.master.TableLockManager.TableLock;
28  import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
29  import org.apache.hadoop.util.StringUtils;
30  
31  import com.google.common.base.Preconditions;
32  
33  /**
34   * Handles processing region merges. Put in a queue, owned by HRegionServer.
35   */
36  @InterfaceAudience.Private
37  class RegionMergeRequest implements Runnable {
38    static final Log LOG = LogFactory.getLog(RegionMergeRequest.class);
39    private final HRegion region_a;
40    private final HRegion region_b;
41    private final HRegionServer server;
42    private final boolean forcible;
43    private TableLock tableLock;
44  
45    RegionMergeRequest(Region a, Region b, HRegionServer hrs, boolean forcible) {
46      Preconditions.checkNotNull(hrs);
47      this.region_a = (HRegion)a;
48      this.region_b = (HRegion)b;
49      this.server = hrs;
50      this.forcible = forcible;
51    }
52  
53    @Override
54    public String toString() {
55      return "MergeRequest,regions:" + region_a + ", " + region_b + ", forcible="
56          + forcible;
57    }
58  
59    @Override
60    public void run() {
61      if (this.server.isStopping() || this.server.isStopped()) {
62        LOG.debug("Skipping merge because server is stopping="
63            + this.server.isStopping() + " or stopped=" + this.server.isStopped());
64        return;
65      }
66      try {
67        final long startTime = EnvironmentEdgeManager.currentTime();
68        RegionMergeTransactionImpl mt = new RegionMergeTransactionImpl(region_a,
69            region_b, forcible);
70  
71        //acquire a shared read lock on the table, so that table schema modifications
72        //do not happen concurrently
73        tableLock = server.getTableLockManager().readLock(region_a.getTableDesc().getTableName()
74            , "MERGE_REGIONS:" + region_a.getRegionInfo().getRegionNameAsString() + ", " +
75                region_b.getRegionInfo().getRegionNameAsString());
76        try {
77          tableLock.acquire();
78        } catch (IOException ex) {
79          tableLock = null;
80          throw ex;
81        }
82  
83        // If prepare does not return true, for some reason -- logged inside in
84        // the prepare call -- we are not ready to merge just now. Just return.
85        if (!mt.prepare(this.server)) return;
86        try {
87          mt.execute(this.server, this.server);
88        } catch (Exception e) {
89          if (this.server.isStopping() || this.server.isStopped()) {
90            LOG.info(
91                "Skip rollback/cleanup of failed merge of " + region_a + " and "
92                    + region_b + " because server is"
93                    + (this.server.isStopping() ? " stopping" : " stopped"), e);
94            return;
95          }
96          try {
97            LOG.warn("Running rollback/cleanup of failed merge of "
98                    + region_a +" and "+ region_b + "; " + e.getMessage(), e);
99            if (mt.rollback(this.server, this.server)) {
100             LOG.info("Successful rollback of failed merge of "
101                 + region_a +" and "+ region_b);
102           } else {
103             this.server.abort("Abort; we got an error after point-of-no-return"
104                 + "when merging " + region_a + " and " + region_b);
105           }
106         } catch (RuntimeException ee) {
107           String msg = "Failed rollback of failed merge of "
108               + region_a +" and "+ region_b + " -- aborting server";
109           // If failed rollback, kill this server to avoid having a hole in
110           // table.
111           LOG.info(msg, ee);
112           this.server.abort(msg);
113         }
114         return;
115       }
116       LOG.info("Regions merged, hbase:meta updated, and report to master. region_a="
117           + region_a + ", region_b=" + region_b + ",merged region="
118           + mt.getMergedRegionInfo().getRegionNameAsString()
119           + ". Region merge took "
120           + StringUtils.formatTimeDiff(EnvironmentEdgeManager.currentTime(), startTime));
121     } catch (IOException ex) {
122       LOG.error("Merge failed " + this,
123           RemoteExceptionHandler.checkIOException(ex));
124       server.checkFileSystem();
125     } finally {
126       releaseTableLock();
127     }
128   }
129 
130   protected void releaseTableLock() {
131     if (this.tableLock != null) {
132       try {
133         this.tableLock.release();
134       } catch (IOException ex) {
135         LOG.error("Could not release the table lock (something is really wrong). " 
136            + "Aborting this server to avoid holding the lock forever.");
137         this.server.abort("Abort; we got an error when releasing the table lock "
138                          + "on " + region_a.getRegionInfo().getRegionNameAsString());
139       }
140     }
141   }
142 }