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.master.TableLockManager.TableLock;
28  import org.apache.hadoop.hbase.security.User;
29  import org.apache.hadoop.hbase.util.Bytes;
30  import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
31  import org.apache.hadoop.ipc.RemoteException;
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       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       long endTime = EnvironmentEdgeManager.currentTime();
132       // Update regionserver metrics with the split transaction total running time
133       server.metricsRegionServer.updateSplitTime(endTime - startTime);
134       if (success) {
135         server.metricsRegionServer.incrSplitSuccess();
136         // Log success
137         LOG.info("Region split, hbase:meta updated, and report to master. Parent="
138             + parent.getRegionInfo().getRegionNameAsString() + ", new regions: "
139             + st.getFirstDaughter().getRegionNameAsString() + ", "
140             + st.getSecondDaughter().getRegionNameAsString() + ". Split took "
141             + StringUtils.formatTimeDiff(EnvironmentEdgeManager.currentTime(), startTime));
142       }
143       // Always log the split transaction journal
144       LOG.info("Split transaction journal:\n\t" + StringUtils.join("\n\t", st.getJournal()));
145     }
146   }
147 
148   @Override
149   public void run() {
150     if (this.server.isStopping() || this.server.isStopped()) {
151       LOG.debug("Skipping split because server is stopping=" +
152         this.server.isStopping() + " or stopped=" + this.server.isStopped());
153       return;
154     }
155     doSplitting(user);
156   }
157 
158   protected void releaseTableLock() {
159     if (this.tableLock != null) {
160       try {
161         this.tableLock.release();
162       } catch (IOException ex) {
163         LOG.error("Could not release the table lock (something is really wrong). "
164            + "Aborting this server to avoid holding the lock forever.");
165         this.server.abort("Abort; we got an error when releasing the table lock "
166                          + "on " + parent.getRegionInfo().getRegionNameAsString());
167       }
168     }
169   }
170 }