View Javadoc

1   /**
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  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,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations 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.master.TableLockManager.TableLock;
27  import org.apache.hadoop.hbase.util.Bytes;
28  import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
29  import org.apache.hadoop.ipc.RemoteException;
30  import org.apache.hadoop.util.StringUtils;
31  
32  import com.google.common.base.Preconditions;
33  
34  /**
35   * Handles processing region splits. Put in a queue, owned by HRegionServer.
36   */
37  @InterfaceAudience.Private
38  class SplitRequest implements Runnable {
39    static final Log LOG = LogFactory.getLog(SplitRequest.class);
40    private final HRegion parent;
41    private final byte[] midKey;
42    private final HRegionServer server;
43    private TableLock tableLock;
44  
45    SplitRequest(HRegion region, byte[] midKey, HRegionServer hrs) {
46      Preconditions.checkNotNull(hrs);
47      this.parent = region;
48      this.midKey = midKey;
49      this.server = hrs;
50    }
51  
52    @Override
53    public String toString() {
54      return "regionName=" + parent + ", midKey=" + Bytes.toStringBinary(midKey);
55    }
56  
57    @Override
58    public void run() {
59      if (this.server.isStopping() || this.server.isStopped()) {
60        LOG.debug("Skipping split because server is stopping=" +
61          this.server.isStopping() + " or stopped=" + this.server.isStopped());
62        return;
63      }
64      boolean success = false;
65      long startTime = EnvironmentEdgeManager.currentTime();
66      SplitTransaction st = new SplitTransaction(parent, midKey);
67      try {
68        //acquire a shared read lock on the table, so that table schema modifications
69        //do not happen concurrently
70        tableLock = server.getTableLockManager().readLock(parent.getTableDesc().getTableName()
71            , "SPLIT_REGION:" + parent.getRegionNameAsString());
72        try {
73          tableLock.acquire();
74        } catch (IOException ex) {
75          tableLock = null;
76          throw ex;
77        }
78  
79        // If prepare does not return true, for some reason -- logged inside in
80        // the prepare call -- we are not ready to split just now. Just return.
81        if (!st.prepare()) return;
82        try {
83          st.execute(this.server, this.server);
84          success = true;
85        } catch (Exception e) {
86          if (this.server.isStopping() || this.server.isStopped()) {
87            LOG.info(
88                "Skip rollback/cleanup of failed split of "
89                    + parent.getRegionNameAsString() + " because server is"
90                    + (this.server.isStopping() ? " stopping" : " stopped"), e);
91            return;
92          }
93          try {
94            LOG.info("Running rollback/cleanup of failed split of " +
95              parent.getRegionNameAsString() + "; " + e.getMessage(), e);
96            if (st.rollback(this.server, this.server)) {
97              LOG.info("Successful rollback of failed split of " +
98                parent.getRegionNameAsString());
99            } else {
100             this.server.abort("Abort; we got an error after point-of-no-return");
101           }
102         } catch (RuntimeException ee) {
103           String msg = "Failed rollback of failed split of " +
104             parent.getRegionNameAsString() + " -- aborting server";
105           // If failed rollback, kill this server to avoid having a hole in table.
106           LOG.info(msg, ee);
107           this.server.abort(msg + " -- Cause: " + ee.getMessage());
108         }
109         return;
110       }
111     } catch (IOException ex) {
112       ex = ex instanceof RemoteException ? ((RemoteException) ex).unwrapRemoteException() : ex;
113       LOG.error("Split failed " + this, ex);
114       server.checkFileSystem();
115     } finally {
116       if (this.parent.getCoprocessorHost() != null) {
117         try {
118           this.parent.getCoprocessorHost().postCompleteSplit();
119         } catch (IOException io) {
120           LOG.error("Split failed " + this,
121             io instanceof RemoteException ? ((RemoteException) io).unwrapRemoteException() : io);
122         }
123       }
124       if (parent.shouldForceSplit()) {
125         parent.clearSplit();
126       }
127       releaseTableLock();
128       long endTime = EnvironmentEdgeManager.currentTime();
129       // Update regionserver metrics with the split transaction total running time
130       server.metricsRegionServer.updateSplitTime(endTime - startTime);
131       if (success) {
132         // Log success
133         LOG.info("Region split, hbase:meta updated, and report to master. Parent="
134             + parent.getRegionNameAsString() + ", new regions: "
135             + st.getFirstDaughter().getRegionNameAsString() + ", "
136             + st.getSecondDaughter().getRegionNameAsString() + ". Split took "
137             + StringUtils.formatTimeDiff(EnvironmentEdgeManager.currentTime(), startTime));
138       }
139       // Always log the split transaction journal
140       LOG.info("Split transaction journal:\n\t" + StringUtils.join("\n\t", st.getJournal()));
141     }
142   }
143 
144   protected void releaseTableLock() {
145     if (this.tableLock != null) {
146       try {
147         this.tableLock.release();
148       } catch (IOException ex) {
149         LOG.error("Could not release the table lock (something is really wrong). " 
150            + "Aborting this server to avoid holding the lock forever.");
151         this.server.abort("Abort; we got an error when releasing the table lock "
152                          + "on " + parent.getRegionNameAsString());
153       }
154     }
155   }
156 }