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