1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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.classification.InterfaceAudience;
26 import org.apache.hadoop.hbase.RemoteExceptionHandler;
27 import org.apache.hadoop.hbase.master.TableLockManager.TableLock;
28 import org.apache.hadoop.hbase.util.Bytes;
29 import org.apache.hadoop.util.StringUtils;
30
31 import com.google.common.base.Preconditions;
32
33
34
35
36 @InterfaceAudience.Private
37 class SplitRequest implements Runnable {
38 static final Log LOG = LogFactory.getLog(SplitRequest.class);
39 private final HRegion parent;
40 private final byte[] midKey;
41 private final HRegionServer server;
42 private TableLock tableLock;
43
44 SplitRequest(HRegion region, byte[] midKey, HRegionServer hrs) {
45 Preconditions.checkNotNull(hrs);
46 this.parent = region;
47 this.midKey = midKey;
48 this.server = hrs;
49 }
50
51 @Override
52 public String toString() {
53 return "regionName=" + parent + ", midKey=" + Bytes.toStringBinary(midKey);
54 }
55
56 @Override
57 public void run() {
58 if (this.server.isStopping() || this.server.isStopped()) {
59 LOG.debug("Skipping split because server is stopping=" +
60 this.server.isStopping() + " or stopped=" + this.server.isStopped());
61 return;
62 }
63 try {
64 final long startTime = System.currentTimeMillis();
65 SplitTransaction st = new SplitTransaction(parent, midKey);
66
67
68
69 tableLock = server.getTableLockManager().readLock(parent.getTableDesc().getTableName()
70 , "SPLIT_REGION:" + parent.getRegionNameAsString());
71 try {
72 tableLock.acquire();
73 } catch (IOException ex) {
74 tableLock = null;
75 throw ex;
76 }
77
78
79
80 if (!st.prepare()) return;
81 try {
82 st.execute(this.server, this.server);
83 } catch (Exception e) {
84 if (this.server.isStopping() || this.server.isStopped()) {
85 LOG.info(
86 "Skip rollback/cleanup of failed split of "
87 + parent.getRegionNameAsString() + " because server is"
88 + (this.server.isStopping() ? " stopping" : " stopped"), e);
89 return;
90 }
91 try {
92 LOG.info("Running rollback/cleanup of failed split of " +
93 parent.getRegionNameAsString() + "; " + e.getMessage(), e);
94 if (st.rollback(this.server, this.server)) {
95 LOG.info("Successful rollback of failed split of " +
96 parent.getRegionNameAsString());
97 } else {
98 this.server.abort("Abort; we got an error after point-of-no-return");
99 }
100 } catch (RuntimeException ee) {
101 String msg = "Failed rollback of failed split of " +
102 parent.getRegionNameAsString() + " -- aborting server";
103
104 LOG.info(msg, ee);
105 this.server.abort(msg + " -- Cause: " + ee.getMessage());
106 }
107 return;
108 }
109 LOG.info("Region split, hbase:meta updated, and report to master. Parent="
110 + parent.getRegionNameAsString() + ", new regions: "
111 + st.getFirstDaughter().getRegionNameAsString() + ", "
112 + st.getSecondDaughter().getRegionNameAsString() + ". Split took "
113 + StringUtils.formatTimeDiff(System.currentTimeMillis(), startTime));
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 releaseTableLock();
127 }
128 }
129
130 protected void releaseTableLock() {
131 if (this.tableLock != null) {
132 try {
133 this.tableLock.release();
134 } catch (IOException ex) {
135 LOG.error("Could not release the table lock (something is really wrong). "
136 + "Aborting this server to avoid holding the lock forever.");
137 this.server.abort("Abort; we got an error when releasing the table lock "
138 + "on " + parent.getRegionNameAsString());
139 }
140 }
141 }
142 }