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.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(HRegion a, HRegion b, HRegionServer hrs, boolean forcible) {
46      Preconditions.checkNotNull(hrs);
47      this.region_a = a;
48      this.region_b = 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.currentTimeMillis();
68        RegionMergeTransaction mt = new RegionMergeTransaction(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.getRegionNameAsString() + ", " + region_b.getRegionNameAsString());
75        try {
76          tableLock.acquire();
77        } catch (IOException ex) {
78          tableLock = null;
79          throw ex;
80        }
81  
82        // If prepare does not return true, for some reason -- logged inside in
83        // the prepare call -- we are not ready to merge just now. Just return.
84        if (!mt.prepare(this.server)) return;
85        try {
86          mt.execute(this.server, this.server);
87        } catch (Exception e) {
88          if (this.server.isStopping() || this.server.isStopped()) {
89            LOG.info(
90                "Skip rollback/cleanup of failed merge of " + region_a + " and "
91                    + region_b + " because server is"
92                    + (this.server.isStopping() ? " stopping" : " stopped"), e);
93            return;
94          }
95          try {
96            LOG.warn("Running rollback/cleanup of failed merge of "
97                    + region_a +" and "+ region_b + "; " + e.getMessage(), e);
98            if (mt.rollback(this.server, this.server)) {
99              LOG.info("Successful rollback of failed merge of "
100                 + region_a +" and "+ region_b);
101           } else {
102             this.server.abort("Abort; we got an error after point-of-no-return"
103                 + "when merging " + region_a + " and " + region_b);
104           }
105         } catch (RuntimeException ee) {
106           String msg = "Failed rollback of failed merge of "
107               + region_a +" and "+ region_b + " -- aborting server";
108           // If failed rollback, kill this server to avoid having a hole in
109           // table.
110           LOG.info(msg, ee);
111           this.server.abort(msg);
112         }
113         return;
114       }
115       LOG.info("Regions merged, hbase:meta updated, and report to master. region_a="
116           + region_a + ", region_b=" + region_b + ",merged region="
117           + mt.getMergedRegionInfo().getRegionNameAsString()
118           + ". Region merge took "
119           + StringUtils.formatTimeDiff(EnvironmentEdgeManager.currentTimeMillis(), startTime));
120     } catch (IOException ex) {
121       LOG.error("Merge failed " + this,
122           RemoteExceptionHandler.checkIOException(ex));
123       server.checkFileSystem();
124     } finally {
125       releaseTableLock();
126     }
127   }
128 
129   protected void releaseTableLock() {
130     if (this.tableLock != null) {
131       try {
132         this.tableLock.release();
133       } catch (IOException ex) {
134         LOG.error("Could not release the table lock (something is really wrong). " 
135            + "Aborting this server to avoid holding the lock forever.");
136         this.server.abort("Abort; we got an error when releasing the table lock "
137                          + "on " + region_a.getRegionNameAsString());
138       }
139     }
140   }
141 }