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