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.util.Bytes;
30  import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
31  import org.apache.hadoop.hbase.util.Strings;
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    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 TableLock tableLock;
46  
47    SplitRequest(HRegion region, byte[] midKey, HRegionServer hrs) {
48      Preconditions.checkNotNull(hrs);
49      this.parent = region;
50      this.midKey = midKey;
51      this.server = hrs;
52    }
53  
54    @Override
55    public String toString() {
56      return "regionName=" + parent + ", midKey=" + Bytes.toStringBinary(midKey);
57    }
58  
59    @Override
60    public void run() {
61      if (this.server.isStopping() || this.server.isStopped()) {
62        LOG.debug("Skipping split because server is stopping=" +
63          this.server.isStopping() + " or stopped=" + this.server.isStopped());
64        return;
65      }
66      boolean success = false;
67      server.getMetrics().incrSplitRequest();
68      long startTime = EnvironmentEdgeManager.currentTimeMillis();
69      SplitTransaction st = new SplitTransaction(parent, midKey);
70      try {
71        //acquire a shared read lock on the table, so that table schema modifications
72        //do not happen concurrently
73        tableLock = server.getTableLockManager().readLock(parent.getTableDesc().getTableName()
74            , "SPLIT_REGION:" + parent.getRegionNameAsString());
75        try {
76          tableLock.acquire();
77        } catch (IOException ex) {
78          tableLock = null;
79          throw ex;
80        }
81  
82        // If prepare does not return true, for some reason -- logged inside in
83        // the prepare call -- we are not ready to split just now. Just return.
84        if (!st.prepare()) return;
85        try {
86          st.execute(this.server, this.server);
87          success = true;
88        } catch (Exception e) {
89          if (this.server.isStopping() || this.server.isStopped()) {
90            LOG.info(
91                "Skip rollback/cleanup of failed split of "
92                    + parent.getRegionNameAsString() + " because server is"
93                    + (this.server.isStopping() ? " stopping" : " stopped"), e);
94            return;
95          }
96          if (e instanceof DroppedSnapshotException) {
97            server.abort("Replay of WAL required. Forcing server shutdown", e);
98            return;
99          }
100         try {
101           LOG.info("Running rollback/cleanup of failed split of " +
102             parent.getRegionNameAsString() + "; " + e.getMessage(), e);
103           if (st.rollback(this.server, this.server)) {
104             LOG.info("Successful rollback of failed split of " +
105               parent.getRegionNameAsString());
106           } else {
107             this.server.abort("Abort; we got an error after point-of-no-return");
108           }
109         } catch (RuntimeException ee) {
110           String msg = "Failed rollback of failed split of " +
111             parent.getRegionNameAsString() + " -- aborting server";
112           // If failed rollback, kill this server to avoid having a hole in table.
113           LOG.info(msg, ee);
114           this.server.abort(msg + " -- Cause: " + ee.getMessage());
115         }
116         return;
117       }
118     } catch (IOException ex) {
119       LOG.error("Split failed " + this, RemoteExceptionHandler.checkIOException(ex));
120       server.checkFileSystem();
121     } finally {
122       if (this.parent.getCoprocessorHost() != null) {
123         try {
124           this.parent.getCoprocessorHost().postCompleteSplit();
125         } catch (IOException io) {
126           LOG.error("Split failed " + this,
127               RemoteExceptionHandler.checkIOException(io));
128         }
129       }
130       if (parent.shouldForceSplit()) {
131         parent.clearSplit();
132       }
133       releaseTableLock();
134       long endTime = EnvironmentEdgeManager.currentTimeMillis();
135       // Update regionserver metrics with the split transaction total running time
136       server.getMetrics().updateSplitTime(endTime - startTime);
137       if (success) {
138         server.getMetrics().incrSplitSuccess();
139         // Log success
140         LOG.info("Region split, hbase:meta updated, and report to master. Parent="
141             + parent.getRegionNameAsString() + ", new regions: "
142             + st.getFirstDaughter().getRegionNameAsString() + ", "
143             + st.getSecondDaughter().getRegionNameAsString() + ". Split took "
144             + StringUtils.formatTimeDiff(EnvironmentEdgeManager.currentTimeMillis(), startTime));
145       }
146       // Always log the split transaction journal
147       LOG.info("Split transaction journal:\n\t" + Strings.join("\n\t", st.getJournal()));
148     }
149   }
150 
151   protected void releaseTableLock() {
152     if (this.tableLock != null) {
153       try {
154         this.tableLock.release();
155       } catch (IOException ex) {
156         LOG.error("Could not release the table lock (something is really wrong). "
157            + "Aborting this server to avoid holding the lock forever.");
158         this.server.abort("Abort; we got an error when releasing the table lock "
159                          + "on " + parent.getRegionNameAsString());
160       }
161     }
162   }
163 }