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.DroppedSnapshotException;
27 import org.apache.hadoop.hbase.RemoteExceptionHandler;
28 import org.apache.hadoop.hbase.master.TableLockManager.TableLock;
29 import org.apache.hadoop.hbase.security.User;
30 import org.apache.hadoop.hbase.util.Bytes;
31 import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
32 import org.apache.hadoop.util.StringUtils;
33
34 import com.google.common.base.Preconditions;
35
36
37
38
39 @InterfaceAudience.Private
40 class SplitRequest implements Runnable {
41 private 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 final User user;
46 private TableLock tableLock;
47
48 SplitRequest(Region region, byte[] midKey, HRegionServer hrs, User user) {
49 Preconditions.checkNotNull(hrs);
50 this.parent = (HRegion)region;
51 this.midKey = midKey;
52 this.server = hrs;
53 this.user = user;
54 }
55
56 @Override
57 public String toString() {
58 return "regionName=" + parent + ", midKey=" + Bytes.toStringBinary(midKey);
59 }
60
61 private void doSplitting(User user) {
62 boolean success = false;
63 server.metricsRegionServer.incrSplitRequest();
64 long startTime = EnvironmentEdgeManager.currentTime();
65 SplitTransactionImpl st = new SplitTransactionImpl(parent, midKey);
66 try {
67
68
69 tableLock = server.getTableLockManager().readLock(parent.getTableDesc().getTableName()
70 , "SPLIT_REGION:" + parent.getRegionInfo().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, user);
83 success = true;
84 } catch (Exception e) {
85 if (this.server.isStopping() || this.server.isStopped()) {
86 LOG.info(
87 "Skip rollback/cleanup of failed split of "
88 + parent.getRegionInfo().getRegionNameAsString() + " because server is"
89 + (this.server.isStopping() ? " stopping" : " stopped"), e);
90 return;
91 }
92 if (e instanceof DroppedSnapshotException) {
93 server.abort("Replay of WAL required. Forcing server shutdown", e);
94 return;
95 }
96 try {
97 LOG.info("Running rollback/cleanup of failed split of " +
98 parent.getRegionInfo().getRegionNameAsString() + "; " + e.getMessage(), e);
99 if (st.rollback(this.server, this.server)) {
100 LOG.info("Successful rollback of failed split of " +
101 parent.getRegionInfo().getRegionNameAsString());
102 } else {
103 this.server.abort("Abort; we got an error after point-of-no-return");
104 }
105 } catch (RuntimeException ee) {
106 String msg = "Failed rollback of failed split of " +
107 parent.getRegionInfo().getRegionNameAsString() + " -- aborting server";
108
109 LOG.info(msg, ee);
110 this.server.abort(msg + " -- Cause: " + ee.getMessage());
111 }
112 return;
113 }
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 if (parent.shouldForceSplit()) {
127 parent.clearSplit();
128 }
129 releaseTableLock();
130 long endTime = EnvironmentEdgeManager.currentTime();
131
132 server.metricsRegionServer.updateSplitTime(endTime - startTime);
133 if (success) {
134 server.metricsRegionServer.incrSplitSuccess();
135
136 LOG.info("Region split, hbase:meta updated, and report to master. Parent="
137 + parent.getRegionInfo().getRegionNameAsString() + ", new regions: "
138 + st.getFirstDaughter().getRegionNameAsString() + ", "
139 + st.getSecondDaughter().getRegionNameAsString() + ". Split took "
140 + StringUtils.formatTimeDiff(EnvironmentEdgeManager.currentTime(), startTime));
141 }
142
143 LOG.info("Split transaction journal:\n\t" + StringUtils.join("\n\t", st.getJournal()));
144 }
145 }
146
147 @Override
148 public void run() {
149 if (this.server.isStopping() || this.server.isStopped()) {
150 LOG.debug("Skipping split because server is stopping=" +
151 this.server.isStopping() + " or stopped=" + this.server.isStopped());
152 return;
153 }
154 doSplitting(user);
155 }
156
157 protected void releaseTableLock() {
158 if (this.tableLock != null) {
159 try {
160 this.tableLock.release();
161 } catch (IOException ex) {
162 LOG.error("Could not release the table lock (something is really wrong). "
163 + "Aborting this server to avoid holding the lock forever.");
164 this.server.abort("Abort; we got an error when releasing the table lock "
165 + "on " + parent.getRegionInfo().getRegionNameAsString());
166 }
167 }
168 }
169 }