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