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