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        } catch (Exception e) {
69          try {
70            LOG.info("Running rollback/cleanup of failed split of " +
71              parent.getRegionNameAsString() + "; " + e.getMessage());
72            if (st.rollback(this.server, this.server)) {
73              LOG.info("Successful rollback of failed split of " +
74                parent.getRegionNameAsString());
75            } else {
76              this.server.abort("Abort; we got an error after point-of-no-return");
77            }
78          } catch (RuntimeException ee) {
79            String msg = "Failed rollback of failed split of " +
80              parent.getRegionNameAsString() + " -- aborting server";
81            // If failed rollback, kill this server to avoid having a hole in table.
82            LOG.info(msg, ee);
83            this.server.abort(msg);
84          }
85          return;
86        }
87        LOG.info("Region split, META updated, and report to master. Parent="
88            + parent.getRegionInfo().getRegionNameAsString() + ", new regions: "
89            + st.getFirstDaughter().getRegionNameAsString() + ", "
90            + st.getSecondDaughter().getRegionNameAsString() + ". Split took "
91            + StringUtils.formatTimeDiff(System.currentTimeMillis(), startTime));
92      } catch (IOException ex) {
93        LOG.error("Split failed " + this, RemoteExceptionHandler
94            .checkIOException(ex));
95        server.checkFileSystem();
96      }
97    }
98  }