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.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.Bytes;
31  import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
32  import org.apache.hadoop.util.StringUtils;
33  
34  import com.google.common.base.Preconditions;
35  
36  /**
37   * Handles processing region splits. Put in a queue, owned by HRegionServer.
38   */
39  @InterfaceAudience.Private
40  class SplitRequest implements Runnable {
41    private static final Log LOG = LogFactory.getLog(SplitRequest.class);
42    private final HRegion parent;
43    private final byte[] midKey;
44    private final HRegionServer server;
45    private final User user;
46    private TableLock tableLock;
47  
48    SplitRequest(Region region, byte[] midKey, HRegionServer hrs, User user) {
49      Preconditions.checkNotNull(hrs);
50      this.parent = (HRegion)region;
51      this.midKey = midKey;
52      this.server = hrs;
53      this.user = user;
54    }
55  
56    @Override
57    public String toString() {
58      return "regionName=" + parent + ", midKey=" + Bytes.toStringBinary(midKey);
59    }
60  
61    private void doSplitting(User user) {
62      boolean success = false;
63      server.metricsRegionServer.incrSplitRequest();
64      long startTime = EnvironmentEdgeManager.currentTime();
65      SplitTransactionImpl st = new SplitTransactionImpl(parent, midKey);
66      try {
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.getRegionInfo().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, user);
83          success = true;
84        } catch (Exception e) {
85          if (this.server.isStopping() || this.server.isStopped()) {
86            LOG.info(
87                "Skip rollback/cleanup of failed split of "
88                    + parent.getRegionInfo().getRegionNameAsString() + " because server is"
89                    + (this.server.isStopping() ? " stopping" : " stopped"), e);
90            return;
91          }
92          if (e instanceof DroppedSnapshotException) {
93            server.abort("Replay of WAL required. Forcing server shutdown", e);
94            return;
95          }
96          try {
97            LOG.info("Running rollback/cleanup of failed split of " +
98              parent.getRegionInfo().getRegionNameAsString() + "; " + e.getMessage(), e);
99            if (st.rollback(this.server, this.server)) {
100             LOG.info("Successful rollback of failed split of " +
101               parent.getRegionInfo().getRegionNameAsString());
102           } else {
103             this.server.abort("Abort; we got an error after point-of-no-return");
104           }
105         } catch (RuntimeException ee) {
106           String msg = "Failed rollback of failed split of " +
107             parent.getRegionInfo().getRegionNameAsString() + " -- aborting server";
108           // If failed rollback, kill this server to avoid having a hole in table.
109           LOG.info(msg, ee);
110           this.server.abort(msg + " -- Cause: " + ee.getMessage());
111         }
112         return;
113       }
114     } catch (IOException ex) {
115       LOG.error("Split failed " + this, RemoteExceptionHandler.checkIOException(ex));
116       server.checkFileSystem();
117     } finally {
118       if (this.parent.getCoprocessorHost() != null) {
119         try {
120           this.parent.getCoprocessorHost().postCompleteSplit();
121         } catch (IOException io) {
122           LOG.error("Split failed " + this,
123               RemoteExceptionHandler.checkIOException(io));
124         }
125       }
126       if (parent.shouldForceSplit()) {
127         parent.clearSplit();
128       }
129       releaseTableLock();
130       long endTime = EnvironmentEdgeManager.currentTime();
131       // Update regionserver metrics with the split transaction total running time
132       server.metricsRegionServer.updateSplitTime(endTime - startTime);
133       if (success) {
134         server.metricsRegionServer.incrSplitSuccess();
135         // Log success
136         LOG.info("Region split, hbase:meta updated, and report to master. Parent="
137             + parent.getRegionInfo().getRegionNameAsString() + ", new regions: "
138             + st.getFirstDaughter().getRegionNameAsString() + ", "
139             + st.getSecondDaughter().getRegionNameAsString() + ". Split took "
140             + StringUtils.formatTimeDiff(EnvironmentEdgeManager.currentTime(), startTime));
141       }
142       // Always log the split transaction journal
143       LOG.info("Split transaction journal:\n\t" + StringUtils.join("\n\t", st.getJournal()));
144     }
145   }
146 
147   @Override
148   public void run() {
149     if (this.server.isStopping() || this.server.isStopped()) {
150       LOG.debug("Skipping split because server is stopping=" +
151         this.server.isStopping() + " or stopped=" + this.server.isStopped());
152       return;
153     }
154     doSplitting(user);
155   }
156 
157   protected void releaseTableLock() {
158     if (this.tableLock != null) {
159       try {
160         this.tableLock.release();
161       } catch (IOException ex) {
162         LOG.error("Could not release the table lock (something is really wrong). "
163            + "Aborting this server to avoid holding the lock forever.");
164         this.server.abort("Abort; we got an error when releasing the table lock "
165                          + "on " + parent.getRegionInfo().getRegionNameAsString());
166       }
167     }
168   }
169 }