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.EnvironmentEdgeManager;
31 import org.apache.hadoop.util.StringUtils;
32
33 import com.google.common.base.Preconditions;
34
35
36
37
38 @InterfaceAudience.Private
39 class RegionMergeRequest implements Runnable {
40 private static final Log LOG = LogFactory.getLog(RegionMergeRequest.class);
41 private final HRegion region_a;
42 private final HRegion region_b;
43 private final HRegionServer server;
44 private final boolean forcible;
45 private TableLock tableLock;
46 private final long masterSystemTime;
47 private final User user;
48
49 RegionMergeRequest(Region a, Region b, HRegionServer hrs, boolean forcible,
50 long masterSystemTime, User user) {
51 Preconditions.checkNotNull(hrs);
52 this.region_a = (HRegion)a;
53 this.region_b = (HRegion)b;
54 this.server = hrs;
55 this.forcible = forcible;
56 this.masterSystemTime = masterSystemTime;
57 this.user = user;
58 }
59
60 @Override
61 public String toString() {
62 return "MergeRequest,regions:" + region_a + ", " + region_b + ", forcible="
63 + forcible;
64 }
65
66 @Override
67 public void run() {
68 if (this.server.isStopping() || this.server.isStopped()) {
69 LOG.debug("Skipping merge because server is stopping="
70 + this.server.isStopping() + " or stopped=" + this.server.isStopped());
71 return;
72 }
73 try {
74 final long startTime = EnvironmentEdgeManager.currentTime();
75 RegionMergeTransactionImpl mt = new RegionMergeTransactionImpl(region_a,
76 region_b, forcible, masterSystemTime);
77
78
79
80 tableLock = server.getTableLockManager().readLock(region_a.getTableDesc().getTableName()
81 , "MERGE_REGIONS:" + region_a.getRegionInfo().getRegionNameAsString() + ", " +
82 region_b.getRegionInfo().getRegionNameAsString());
83 try {
84 tableLock.acquire();
85 } catch (IOException ex) {
86 tableLock = null;
87 throw ex;
88 }
89
90
91
92 if (!mt.prepare(this.server)) return;
93 try {
94 mt.execute(this.server, this.server, this.user);
95 } catch (Exception e) {
96 if (this.server.isStopping() || this.server.isStopped()) {
97 LOG.info(
98 "Skip rollback/cleanup of failed merge of " + region_a + " and "
99 + region_b + " because server is"
100 + (this.server.isStopping() ? " stopping" : " stopped"), e);
101 return;
102 }
103 if (e instanceof DroppedSnapshotException) {
104 server.abort("Replay of WAL required. Forcing server shutdown", e);
105 return;
106 }
107 try {
108 LOG.warn("Running rollback/cleanup of failed merge of "
109 + region_a +" and "+ region_b + "; " + e.getMessage(), e);
110 if (mt.rollback(this.server, this.server)) {
111 LOG.info("Successful rollback of failed merge of "
112 + region_a +" and "+ region_b);
113 } else {
114 this.server.abort("Abort; we got an error after point-of-no-return"
115 + "when merging " + region_a + " and " + region_b);
116 }
117 } catch (RuntimeException ee) {
118 String msg = "Failed rollback of failed merge of "
119 + region_a +" and "+ region_b + " -- aborting server";
120
121
122 LOG.info(msg, ee);
123 this.server.abort(msg);
124 }
125 return;
126 }
127 LOG.info("Regions merged, hbase:meta updated, and report to master. region_a="
128 + region_a + ", region_b=" + region_b + ",merged region="
129 + mt.getMergedRegionInfo().getRegionNameAsString()
130 + ". Region merge took "
131 + StringUtils.formatTimeDiff(EnvironmentEdgeManager.currentTime(), startTime));
132 } catch (IOException ex) {
133 LOG.error("Merge failed " + this,
134 RemoteExceptionHandler.checkIOException(ex));
135 server.checkFileSystem();
136 } finally {
137 releaseTableLock();
138 }
139 }
140
141 protected void releaseTableLock() {
142 if (this.tableLock != null) {
143 try {
144 this.tableLock.release();
145 } catch (IOException ex) {
146 LOG.error("Could not release the table lock (something is really wrong). "
147 + "Aborting this server to avoid holding the lock forever.");
148 this.server.abort("Abort; we got an error when releasing the table lock "
149 + "on " + region_a.getRegionInfo().getRegionNameAsString());
150 }
151 }
152 }
153 }