View Javadoc

1   /**
2    * Copyright 2011 The Apache Software Foundation
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
19   */
20  package org.apache.hadoop.hbase.regionserver;
21  
22  import java.io.IOException;
23  
24  import org.apache.commons.logging.Log;
25  import org.apache.commons.logging.LogFactory;
26  import org.apache.hadoop.hbase.RemoteExceptionHandler;
27  import org.apache.hadoop.hbase.util.Bytes;
28  import org.apache.hadoop.util.StringUtils;
29  
30  import com.google.common.base.Preconditions;
31  
32  /**
33   * Handles processing region splits. Put in a queue, owned by HRegionServer.
34   */
35  class SplitRequest implements Runnable {
36    static final Log LOG = LogFactory.getLog(SplitRequest.class);
37    private final HRegion parent;
38    private final byte[] midKey;
39    private final HRegionServer server;
40  
41    SplitRequest(HRegion region, byte[] midKey, HRegionServer hrs) {
42      Preconditions.checkNotNull(hrs);
43      this.parent = region;
44      this.midKey = midKey;
45      this.server = hrs;
46    }
47  
48    @Override
49    public String toString() {
50      return "regionName=" + parent + ", midKey=" + Bytes.toStringBinary(midKey);
51    }
52  
53    @Override
54    public void run() {
55      if (this.server.isStopping() || this.server.isStopped()) {
56        LOG.debug("Skipping split because server is stopping=" +
57          this.server.isStopping() + " or stopped=" + this.server.isStopped());
58        return;
59      }
60      try {
61        final long startTime = System.currentTimeMillis();
62        SplitTransaction st = new SplitTransaction(parent, midKey);
63        // If prepare does not return true, for some reason -- logged inside in
64        // the prepare call -- we are not ready to split just now. Just return.
65        if (!st.prepare()) return;
66        try {
67          st.execute(this.server, this.server);
68          this.server.getMetrics().incrementSplitSuccessCount();
69        } catch (Exception e) {
70          if (this.server.isStopping() || this.server.isStopped()) {
71            LOG.info(
72                "Skip rollback/cleanup of failed split of "
73                    + parent.getRegionNameAsString() + " because server is"
74                    + (this.server.isStopping() ? " stopping" : " stopped"), e);
75            return;
76          }
77          try {
78            LOG.info("Running rollback/cleanup of failed split of " +
79              parent.getRegionNameAsString() + "; " + e.getMessage(), e);
80            if (st.rollback(this.server, this.server)) {
81              LOG.info("Successful rollback of failed split of " +
82                parent.getRegionNameAsString());
83              this.server.getMetrics().incrementSplitFailureCount();
84            } else {
85              this.server.abort("Abort; we got an error after point-of-no-return");
86            }
87          } catch (RuntimeException ee) {
88            String msg = "Failed rollback of failed split of " +
89              parent.getRegionNameAsString() + " -- aborting server";
90            // If failed rollback, kill this server to avoid having a hole in table.
91            LOG.info(msg, ee);
92            this.server.abort(msg + " -- Cause: " + ee.getMessage());
93          }
94          return;
95        }
96        LOG.info("Region split, META updated, and report to master. Parent="
97            + parent.getRegionInfo().getRegionNameAsString() + ", new regions: "
98            + st.getFirstDaughter().getRegionNameAsString() + ", "
99            + st.getSecondDaughter().getRegionNameAsString() + ". Split took "
100           + StringUtils.formatTimeDiff(System.currentTimeMillis(), startTime));
101     } catch (IOException ex) {
102       LOG.error("Split failed " + this, RemoteExceptionHandler
103           .checkIOException(ex));
104       this.server.getMetrics().incrementSplitFailureCount();
105       server.checkFileSystem();
106     }
107   }
108 }