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.hbase.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.hbase.util.EnvironmentEdgeManager;
30 import org.apache.hadoop.hbase.util.Strings;
31 import org.apache.hadoop.util.StringUtils;
32
33 import com.google.common.base.Preconditions;
34
35
36
37
38 @InterfaceAudience.Private
39 class SplitRequest implements Runnable {
40 static final Log LOG = LogFactory.getLog(SplitRequest.class);
41 private final HRegion parent;
42 private final byte[] midKey;
43 private final HRegionServer server;
44 private TableLock tableLock;
45
46 SplitRequest(HRegion region, byte[] midKey, HRegionServer hrs) {
47 Preconditions.checkNotNull(hrs);
48 this.parent = region;
49 this.midKey = midKey;
50 this.server = hrs;
51 }
52
53 @Override
54 public String toString() {
55 return "regionName=" + parent + ", midKey=" + Bytes.toStringBinary(midKey);
56 }
57
58 @Override
59 public void run() {
60 if (this.server.isStopping() || this.server.isStopped()) {
61 LOG.debug("Skipping split because server is stopping=" +
62 this.server.isStopping() + " or stopped=" + this.server.isStopped());
63 return;
64 }
65 boolean success = false;
66 server.getMetrics().incrSplitRequest();
67 long startTime = EnvironmentEdgeManager.currentTimeMillis();
68 SplitTransaction st = new SplitTransaction(parent, midKey);
69 try {
70
71
72 tableLock = server.getTableLockManager().readLock(parent.getTableDesc().getTableName()
73 , "SPLIT_REGION:" + parent.getRegionNameAsString());
74 try {
75 tableLock.acquire();
76 } catch (IOException ex) {
77 tableLock = null;
78 throw ex;
79 }
80
81
82
83 if (!st.prepare()) return;
84 try {
85 st.execute(this.server, this.server);
86 success = true;
87 } catch (Exception e) {
88 if (this.server.isStopping() || this.server.isStopped()) {
89 LOG.info(
90 "Skip rollback/cleanup of failed split of "
91 + parent.getRegionNameAsString() + " because server is"
92 + (this.server.isStopping() ? " stopping" : " stopped"), e);
93 return;
94 }
95 try {
96 LOG.info("Running rollback/cleanup of failed split of " +
97 parent.getRegionNameAsString() + "; " + e.getMessage(), e);
98 if (st.rollback(this.server, this.server)) {
99 LOG.info("Successful rollback of failed split of " +
100 parent.getRegionNameAsString());
101 } else {
102 this.server.abort("Abort; we got an error after point-of-no-return");
103 }
104 } catch (RuntimeException ee) {
105 String msg = "Failed rollback of failed split of " +
106 parent.getRegionNameAsString() + " -- aborting server";
107
108 LOG.info(msg, ee);
109 this.server.abort(msg + " -- Cause: " + ee.getMessage());
110 }
111 return;
112 }
113 } catch (IOException ex) {
114 LOG.error("Split failed " + this, RemoteExceptionHandler.checkIOException(ex));
115 server.checkFileSystem();
116 } finally {
117 if (this.parent.getCoprocessorHost() != null) {
118 try {
119 this.parent.getCoprocessorHost().postCompleteSplit();
120 } catch (IOException io) {
121 LOG.error("Split failed " + this,
122 RemoteExceptionHandler.checkIOException(io));
123 }
124 }
125 if (parent.shouldForceSplit()) {
126 parent.clearSplit();
127 }
128 releaseTableLock();
129 long endTime = EnvironmentEdgeManager.currentTimeMillis();
130
131 server.getMetrics().updateSplitTime(endTime - startTime);
132 if (success) {
133 server.getMetrics().incrSplitSuccess();
134
135 LOG.info("Region split, hbase:meta updated, and report to master. Parent="
136 + parent.getRegionNameAsString() + ", new regions: "
137 + st.getFirstDaughter().getRegionNameAsString() + ", "
138 + st.getSecondDaughter().getRegionNameAsString() + ". Split took "
139 + StringUtils.formatTimeDiff(EnvironmentEdgeManager.currentTimeMillis(), startTime));
140 }
141
142 LOG.info("Split transaction journal:\n\t" + Strings.join("\n\t", st.getJournal()));
143 }
144 }
145
146 protected void releaseTableLock() {
147 if (this.tableLock != null) {
148 try {
149 this.tableLock.release();
150 } catch (IOException ex) {
151 LOG.error("Could not release the table lock (something is really wrong). "
152 + "Aborting this server to avoid holding the lock forever.");
153 this.server.abort("Abort; we got an error when releasing the table lock "
154 + "on " + parent.getRegionNameAsString());
155 }
156 }
157 }
158 }