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.handler;
20
21 import java.io.IOException;
22 import java.io.InterruptedIOException;
23
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
26 import org.apache.hadoop.conf.Configuration;
27 import org.apache.hadoop.hbase.HConstants;
28 import org.apache.hadoop.hbase.Server;
29 import org.apache.hadoop.hbase.TableNotFoundException;
30 import org.apache.hadoop.hbase.classification.InterfaceAudience;
31 import org.apache.hadoop.hbase.client.ClusterConnection;
32 import org.apache.hadoop.hbase.client.FlushRegionCallable;
33 import org.apache.hadoop.hbase.client.RegionReplicaUtil;
34 import org.apache.hadoop.hbase.client.RpcRetryingCallerFactory;
35 import org.apache.hadoop.hbase.executor.EventHandler;
36 import org.apache.hadoop.hbase.executor.EventType;
37 import org.apache.hadoop.hbase.ipc.RpcControllerFactory;
38 import org.apache.hadoop.hbase.protobuf.generated.AdminProtos.FlushRegionResponse;
39 import org.apache.hadoop.hbase.regionserver.HRegion;
40 import org.apache.hadoop.hbase.util.RetryCounter;
41 import org.apache.hadoop.hbase.util.RetryCounterFactory;
42 import org.apache.hadoop.hbase.util.ServerRegionReplicaUtil;
43
44
45
46
47
48
49
50
51
52
53
54 @InterfaceAudience.Private
55 public class RegionReplicaFlushHandler extends EventHandler {
56
57 private static final Log LOG = LogFactory.getLog(RegionReplicaFlushHandler.class);
58
59 private final ClusterConnection connection;
60 private final RpcRetryingCallerFactory rpcRetryingCallerFactory;
61 private final RpcControllerFactory rpcControllerFactory;
62 private final int operationTimeout;
63 private final HRegion region;
64
65 public RegionReplicaFlushHandler(Server server, ClusterConnection connection,
66 RpcRetryingCallerFactory rpcRetryingCallerFactory, RpcControllerFactory rpcControllerFactory,
67 int operationTimeout, HRegion region) {
68 super(server, EventType.RS_REGION_REPLICA_FLUSH);
69 this.connection = connection;
70 this.rpcRetryingCallerFactory = rpcRetryingCallerFactory;
71 this.rpcControllerFactory = rpcControllerFactory;
72 this.operationTimeout = operationTimeout;
73 this.region = region;
74 }
75
76 @Override
77 public void process() throws IOException {
78 triggerFlushInPrimaryRegion(region);
79 }
80
81 @Override
82 protected void handleException(Throwable t) {
83 super.handleException(t);
84
85 if (t instanceof InterruptedIOException || t instanceof InterruptedException) {
86
87 } else if (t instanceof RuntimeException) {
88 server.abort("ServerAborting because a runtime exception was thrown", t);
89 } else {
90
91
92
93 server.abort("ServerAborting because an exception was thrown", t);
94 }
95 }
96
97 private int getRetriesCount(Configuration conf) {
98 int numRetries = conf.getInt(HConstants.HBASE_CLIENT_RETRIES_NUMBER,
99 HConstants.DEFAULT_HBASE_CLIENT_RETRIES_NUMBER);
100 if (numRetries > 10) {
101 int mult = conf.getInt("hbase.client.serverside.retries.multiplier", 10);
102 numRetries = numRetries / mult;
103 }
104 return numRetries;
105 }
106
107 void triggerFlushInPrimaryRegion(final HRegion region) throws IOException, RuntimeException {
108 long pause = connection.getConfiguration().getLong(HConstants.HBASE_CLIENT_PAUSE,
109 HConstants.DEFAULT_HBASE_CLIENT_PAUSE);
110
111 int maxAttempts = getRetriesCount(connection.getConfiguration());
112 RetryCounter counter = new RetryCounterFactory(maxAttempts, (int)pause).create();
113
114 if (LOG.isDebugEnabled()) {
115 LOG.debug("Attempting to do an RPC to the primary region replica " + ServerRegionReplicaUtil
116 .getRegionInfoForDefaultReplica(region.getRegionInfo()).getEncodedName() + " of region "
117 + region.getRegionInfo().getEncodedName() + " to trigger a flush");
118 }
119 while (!region.isClosing() && !region.isClosed()
120 && !server.isAborted() && !server.isStopped()) {
121 FlushRegionCallable flushCallable = new FlushRegionCallable(
122 connection, rpcControllerFactory,
123 RegionReplicaUtil.getRegionInfoForDefaultReplica(region.getRegionInfo()), true);
124
125
126
127 FlushRegionResponse response = null;
128 try {
129 response = rpcRetryingCallerFactory.<FlushRegionResponse>newCaller()
130 .callWithRetries(flushCallable, this.operationTimeout);
131 } catch (IOException ex) {
132 if (ex instanceof TableNotFoundException
133 || connection.isTableDisabled(region.getRegionInfo().getTable())) {
134 return;
135 }
136 throw ex;
137 }
138
139 if (response.getFlushed()) {
140
141
142 if (LOG.isDebugEnabled()) {
143 LOG.debug("Successfully triggered a flush of primary region replica "
144 + ServerRegionReplicaUtil
145 .getRegionInfoForDefaultReplica(region.getRegionInfo()).getEncodedName()
146 + " of region " + region.getRegionInfo().getEncodedName()
147 + " Now waiting and blocking reads until observing a full flush cycle");
148 }
149 break;
150 } else {
151 if (response.hasWroteFlushWalMarker()) {
152 if(response.getWroteFlushWalMarker()) {
153 if (LOG.isDebugEnabled()) {
154 LOG.debug("Successfully triggered an empty flush marker(memstore empty) of primary "
155 + "region replica " + ServerRegionReplicaUtil
156 .getRegionInfoForDefaultReplica(region.getRegionInfo()).getEncodedName()
157 + " of region " + region.getRegionInfo().getEncodedName() + " Now waiting and "
158 + "blocking reads until observing a flush marker");
159 }
160 break;
161 } else {
162
163
164 if (!counter.shouldRetry()) {
165 throw new IOException("Cannot cause primary to flush or drop a wal marker after " +
166 "retries. Failing opening of this region replica "
167 + region.getRegionInfo().getEncodedName());
168 }
169 }
170 } else {
171
172 LOG.warn("Was not able to trigger a flush from primary region due to old server version? "
173 + "Continuing to open the secondary region replica: "
174 + region.getRegionInfo().getEncodedName());
175 region.setReadsEnabled(true);
176 break;
177 }
178 }
179 try {
180 counter.sleepUntilNextRetry();
181 } catch (InterruptedException e) {
182 throw new InterruptedIOException(e.getMessage());
183 }
184 }
185 }
186
187 }