http://git-wip-us.apache.org/repos/asf/hbase-site/blob/f30982bd/devapidocs/src-html/org/apache/hadoop/hbase/client/AsyncProcess.BatchErrors.html ---------------------------------------------------------------------- diff --git a/devapidocs/src-html/org/apache/hadoop/hbase/client/AsyncProcess.BatchErrors.html b/devapidocs/src-html/org/apache/hadoop/hbase/client/AsyncProcess.BatchErrors.html index ed7bc7f..e65df71 100644 --- a/devapidocs/src-html/org/apache/hadoop/hbase/client/AsyncProcess.BatchErrors.html +++ b/devapidocs/src-html/org/apache/hadoop/hbase/client/AsyncProcess.BatchErrors.html @@ -1198,681 +1198,699 @@ <span class="sourceLineNo">1190</span> byte[] row = e.getValue().iterator().next().getAction().getRow();<a name="line.1190"></a> <span class="sourceLineNo">1191</span> // Do not use the exception for updating cache because it might be coming from<a name="line.1191"></a> <span class="sourceLineNo">1192</span> // any of the regions in the MultiAction.<a name="line.1192"></a> -<span class="sourceLineNo">1193</span> if (tableName != null) {<a name="line.1193"></a> -<span class="sourceLineNo">1194</span> connection.updateCachedLocations(tableName, regionName, row,<a name="line.1194"></a> -<span class="sourceLineNo">1195</span> ClientExceptionsUtil.isMetaClearingException(t) ? null : t, server);<a name="line.1195"></a> -<span class="sourceLineNo">1196</span> }<a name="line.1196"></a> -<span class="sourceLineNo">1197</span> for (Action<Row> action : e.getValue()) {<a name="line.1197"></a> -<span class="sourceLineNo">1198</span> Retry retry = manageError(<a name="line.1198"></a> -<span class="sourceLineNo">1199</span> action.getOriginalIndex(), action.getAction(), canRetry, t, server);<a name="line.1199"></a> -<span class="sourceLineNo">1200</span> if (retry == Retry.YES) {<a name="line.1200"></a> -<span class="sourceLineNo">1201</span> toReplay.add(action);<a name="line.1201"></a> -<span class="sourceLineNo">1202</span> } else if (retry == Retry.NO_OTHER_SUCCEEDED) {<a name="line.1202"></a> -<span class="sourceLineNo">1203</span> ++stopped;<a name="line.1203"></a> -<span class="sourceLineNo">1204</span> } else {<a name="line.1204"></a> -<span class="sourceLineNo">1205</span> ++failed;<a name="line.1205"></a> -<span class="sourceLineNo">1206</span> }<a name="line.1206"></a> -<span class="sourceLineNo">1207</span> }<a name="line.1207"></a> -<span class="sourceLineNo">1208</span> }<a name="line.1208"></a> -<span class="sourceLineNo">1209</span><a name="line.1209"></a> -<span class="sourceLineNo">1210</span> if (toReplay.isEmpty()) {<a name="line.1210"></a> -<span class="sourceLineNo">1211</span> logNoResubmit(server, numAttempt, rsActions.size(), t, failed, stopped);<a name="line.1211"></a> -<span class="sourceLineNo">1212</span> } else {<a name="line.1212"></a> -<span class="sourceLineNo">1213</span> resubmit(server, toReplay, numAttempt, rsActions.size(), t);<a name="line.1213"></a> +<span class="sourceLineNo">1193</span> try {<a name="line.1193"></a> +<span class="sourceLineNo">1194</span> if (tableName != null) {<a name="line.1194"></a> +<span class="sourceLineNo">1195</span> connection.updateCachedLocations(tableName, regionName, row,<a name="line.1195"></a> +<span class="sourceLineNo">1196</span> ClientExceptionsUtil.isMetaClearingException(t) ? null : t, server);<a name="line.1196"></a> +<span class="sourceLineNo">1197</span> }<a name="line.1197"></a> +<span class="sourceLineNo">1198</span> } catch (Throwable ex) {<a name="line.1198"></a> +<span class="sourceLineNo">1199</span> // That should never happen, but if it did, we want to make sure<a name="line.1199"></a> +<span class="sourceLineNo">1200</span> // we still process errors<a name="line.1200"></a> +<span class="sourceLineNo">1201</span> LOG.error("Couldn't update cached region locations: " + ex);<a name="line.1201"></a> +<span class="sourceLineNo">1202</span> }<a name="line.1202"></a> +<span class="sourceLineNo">1203</span> for (Action<Row> action : e.getValue()) {<a name="line.1203"></a> +<span class="sourceLineNo">1204</span> Retry retry = manageError(<a name="line.1204"></a> +<span class="sourceLineNo">1205</span> action.getOriginalIndex(), action.getAction(), canRetry, t, server);<a name="line.1205"></a> +<span class="sourceLineNo">1206</span> if (retry == Retry.YES) {<a name="line.1206"></a> +<span class="sourceLineNo">1207</span> toReplay.add(action);<a name="line.1207"></a> +<span class="sourceLineNo">1208</span> } else if (retry == Retry.NO_OTHER_SUCCEEDED) {<a name="line.1208"></a> +<span class="sourceLineNo">1209</span> ++stopped;<a name="line.1209"></a> +<span class="sourceLineNo">1210</span> } else {<a name="line.1210"></a> +<span class="sourceLineNo">1211</span> ++failed;<a name="line.1211"></a> +<span class="sourceLineNo">1212</span> }<a name="line.1212"></a> +<span class="sourceLineNo">1213</span> }<a name="line.1213"></a> <span class="sourceLineNo">1214</span> }<a name="line.1214"></a> -<span class="sourceLineNo">1215</span> }<a name="line.1215"></a> -<span class="sourceLineNo">1216</span><a name="line.1216"></a> -<span class="sourceLineNo">1217</span> /**<a name="line.1217"></a> -<span class="sourceLineNo">1218</span> * Log as much info as possible, and, if there is something to replay,<a name="line.1218"></a> -<span class="sourceLineNo">1219</span> * submit it again after a back off sleep.<a name="line.1219"></a> -<span class="sourceLineNo">1220</span> */<a name="line.1220"></a> -<span class="sourceLineNo">1221</span> private void resubmit(ServerName oldServer, List<Action<Row>> toReplay,<a name="line.1221"></a> -<span class="sourceLineNo">1222</span> int numAttempt, int failureCount, Throwable throwable) {<a name="line.1222"></a> -<span class="sourceLineNo">1223</span> // We have something to replay. We're going to sleep a little before.<a name="line.1223"></a> -<span class="sourceLineNo">1224</span><a name="line.1224"></a> -<span class="sourceLineNo">1225</span> // We have two contradicting needs here:<a name="line.1225"></a> -<span class="sourceLineNo">1226</span> // 1) We want to get the new location after having slept, as it may change.<a name="line.1226"></a> -<span class="sourceLineNo">1227</span> // 2) We want to take into account the location when calculating the sleep time.<a name="line.1227"></a> -<span class="sourceLineNo">1228</span> // 3) If all this is just because the response needed to be chunked try again FAST.<a name="line.1228"></a> -<span class="sourceLineNo">1229</span> // It should be possible to have some heuristics to take the right decision. Short term,<a name="line.1229"></a> -<span class="sourceLineNo">1230</span> // we go for one.<a name="line.1230"></a> -<span class="sourceLineNo">1231</span> boolean retryImmediately = throwable instanceof RetryImmediatelyException;<a name="line.1231"></a> -<span class="sourceLineNo">1232</span> int nextAttemptNumber = retryImmediately ? numAttempt : numAttempt + 1;<a name="line.1232"></a> -<span class="sourceLineNo">1233</span> long backOffTime = retryImmediately ? 0 :<a name="line.1233"></a> -<span class="sourceLineNo">1234</span> errorsByServer.calculateBackoffTime(oldServer, pause);<a name="line.1234"></a> -<span class="sourceLineNo">1235</span> if (numAttempt > startLogErrorsCnt) {<a name="line.1235"></a> -<span class="sourceLineNo">1236</span> // We use this value to have some logs when we have multiple failures, but not too many<a name="line.1236"></a> -<span class="sourceLineNo">1237</span> // logs, as errors are to be expected when a region moves, splits and so on<a name="line.1237"></a> -<span class="sourceLineNo">1238</span> LOG.info(createLog(numAttempt, failureCount, toReplay.size(),<a name="line.1238"></a> -<span class="sourceLineNo">1239</span> oldServer, throwable, backOffTime, true, null, -1, -1));<a name="line.1239"></a> -<span class="sourceLineNo">1240</span> }<a name="line.1240"></a> -<span class="sourceLineNo">1241</span><a name="line.1241"></a> -<span class="sourceLineNo">1242</span> try {<a name="line.1242"></a> -<span class="sourceLineNo">1243</span> if (backOffTime > 0) {<a name="line.1243"></a> -<span class="sourceLineNo">1244</span> Thread.sleep(backOffTime);<a name="line.1244"></a> -<span class="sourceLineNo">1245</span> }<a name="line.1245"></a> -<span class="sourceLineNo">1246</span> } catch (InterruptedException e) {<a name="line.1246"></a> -<span class="sourceLineNo">1247</span> LOG.warn("#" + id + ", not sent: " + toReplay.size() + " operations, " + oldServer, e);<a name="line.1247"></a> -<span class="sourceLineNo">1248</span> Thread.currentThread().interrupt();<a name="line.1248"></a> -<span class="sourceLineNo">1249</span> return;<a name="line.1249"></a> -<span class="sourceLineNo">1250</span> }<a name="line.1250"></a> -<span class="sourceLineNo">1251</span><a name="line.1251"></a> -<span class="sourceLineNo">1252</span> groupAndSendMultiAction(toReplay, nextAttemptNumber);<a name="line.1252"></a> -<span class="sourceLineNo">1253</span> }<a name="line.1253"></a> -<span class="sourceLineNo">1254</span><a name="line.1254"></a> -<span class="sourceLineNo">1255</span> private void logNoResubmit(ServerName oldServer, int numAttempt,<a name="line.1255"></a> -<span class="sourceLineNo">1256</span> int failureCount, Throwable throwable, int failed, int stopped) {<a name="line.1256"></a> -<span class="sourceLineNo">1257</span> if (failureCount != 0 || numAttempt > startLogErrorsCnt + 1) {<a name="line.1257"></a> -<span class="sourceLineNo">1258</span> String timeStr = new Date(errorsByServer.getStartTrackingTime()).toString();<a name="line.1258"></a> -<span class="sourceLineNo">1259</span> String logMessage = createLog(numAttempt, failureCount, 0, oldServer,<a name="line.1259"></a> -<span class="sourceLineNo">1260</span> throwable, -1, false, timeStr, failed, stopped);<a name="line.1260"></a> -<span class="sourceLineNo">1261</span> if (failed != 0) {<a name="line.1261"></a> -<span class="sourceLineNo">1262</span> // Only log final failures as warning<a name="line.1262"></a> -<span class="sourceLineNo">1263</span> LOG.warn(logMessage);<a name="line.1263"></a> -<span class="sourceLineNo">1264</span> } else {<a name="line.1264"></a> -<span class="sourceLineNo">1265</span> LOG.info(logMessage);<a name="line.1265"></a> -<span class="sourceLineNo">1266</span> }<a name="line.1266"></a> -<span class="sourceLineNo">1267</span> }<a name="line.1267"></a> -<span class="sourceLineNo">1268</span> }<a name="line.1268"></a> -<span class="sourceLineNo">1269</span><a name="line.1269"></a> -<span class="sourceLineNo">1270</span> /**<a name="line.1270"></a> -<span class="sourceLineNo">1271</span> * Called when we receive the result of a server query.<a name="line.1271"></a> -<span class="sourceLineNo">1272</span> *<a name="line.1272"></a> -<span class="sourceLineNo">1273</span> * @param multiAction - the multiAction we sent<a name="line.1273"></a> -<span class="sourceLineNo">1274</span> * @param server - the location. It's used as a server name.<a name="line.1274"></a> -<span class="sourceLineNo">1275</span> * @param responses - the response, if any<a name="line.1275"></a> -<span class="sourceLineNo">1276</span> * @param numAttempt - the attempt<a name="line.1276"></a> -<span class="sourceLineNo">1277</span> */<a name="line.1277"></a> -<span class="sourceLineNo">1278</span> private void receiveMultiAction(MultiAction<Row> multiAction,<a name="line.1278"></a> -<span class="sourceLineNo">1279</span> ServerName server, MultiResponse responses, int numAttempt) {<a name="line.1279"></a> -<span class="sourceLineNo">1280</span> assert responses != null;<a name="line.1280"></a> -<span class="sourceLineNo">1281</span><a name="line.1281"></a> -<span class="sourceLineNo">1282</span> // Success or partial success<a name="line.1282"></a> -<span class="sourceLineNo">1283</span> // Analyze detailed results. We can still have individual failures to be redo.<a name="line.1283"></a> -<span class="sourceLineNo">1284</span> // two specific throwables are managed:<a name="line.1284"></a> -<span class="sourceLineNo">1285</span> // - DoNotRetryIOException: we continue to retry for other actions<a name="line.1285"></a> -<span class="sourceLineNo">1286</span> // - RegionMovedException: we update the cache with the new region location<a name="line.1286"></a> +<span class="sourceLineNo">1215</span><a name="line.1215"></a> +<span class="sourceLineNo">1216</span> if (toReplay.isEmpty()) {<a name="line.1216"></a> +<span class="sourceLineNo">1217</span> logNoResubmit(server, numAttempt, rsActions.size(), t, failed, stopped);<a name="line.1217"></a> +<span class="sourceLineNo">1218</span> } else {<a name="line.1218"></a> +<span class="sourceLineNo">1219</span> resubmit(server, toReplay, numAttempt, rsActions.size(), t);<a name="line.1219"></a> +<span class="sourceLineNo">1220</span> }<a name="line.1220"></a> +<span class="sourceLineNo">1221</span> }<a name="line.1221"></a> +<span class="sourceLineNo">1222</span><a name="line.1222"></a> +<span class="sourceLineNo">1223</span> /**<a name="line.1223"></a> +<span class="sourceLineNo">1224</span> * Log as much info as possible, and, if there is something to replay,<a name="line.1224"></a> +<span class="sourceLineNo">1225</span> * submit it again after a back off sleep.<a name="line.1225"></a> +<span class="sourceLineNo">1226</span> */<a name="line.1226"></a> +<span class="sourceLineNo">1227</span> private void resubmit(ServerName oldServer, List<Action<Row>> toReplay,<a name="line.1227"></a> +<span class="sourceLineNo">1228</span> int numAttempt, int failureCount, Throwable throwable) {<a name="line.1228"></a> +<span class="sourceLineNo">1229</span> // We have something to replay. We're going to sleep a little before.<a name="line.1229"></a> +<span class="sourceLineNo">1230</span><a name="line.1230"></a> +<span class="sourceLineNo">1231</span> // We have two contradicting needs here:<a name="line.1231"></a> +<span class="sourceLineNo">1232</span> // 1) We want to get the new location after having slept, as it may change.<a name="line.1232"></a> +<span class="sourceLineNo">1233</span> // 2) We want to take into account the location when calculating the sleep time.<a name="line.1233"></a> +<span class="sourceLineNo">1234</span> // 3) If all this is just because the response needed to be chunked try again FAST.<a name="line.1234"></a> +<span class="sourceLineNo">1235</span> // It should be possible to have some heuristics to take the right decision. Short term,<a name="line.1235"></a> +<span class="sourceLineNo">1236</span> // we go for one.<a name="line.1236"></a> +<span class="sourceLineNo">1237</span> boolean retryImmediately = throwable instanceof RetryImmediatelyException;<a name="line.1237"></a> +<span class="sourceLineNo">1238</span> int nextAttemptNumber = retryImmediately ? numAttempt : numAttempt + 1;<a name="line.1238"></a> +<span class="sourceLineNo">1239</span> long backOffTime = retryImmediately ? 0 :<a name="line.1239"></a> +<span class="sourceLineNo">1240</span> errorsByServer.calculateBackoffTime(oldServer, pause);<a name="line.1240"></a> +<span class="sourceLineNo">1241</span> if (numAttempt > startLogErrorsCnt) {<a name="line.1241"></a> +<span class="sourceLineNo">1242</span> // We use this value to have some logs when we have multiple failures, but not too many<a name="line.1242"></a> +<span class="sourceLineNo">1243</span> // logs, as errors are to be expected when a region moves, splits and so on<a name="line.1243"></a> +<span class="sourceLineNo">1244</span> LOG.info(createLog(numAttempt, failureCount, toReplay.size(),<a name="line.1244"></a> +<span class="sourceLineNo">1245</span> oldServer, throwable, backOffTime, true, null, -1, -1));<a name="line.1245"></a> +<span class="sourceLineNo">1246</span> }<a name="line.1246"></a> +<span class="sourceLineNo">1247</span><a name="line.1247"></a> +<span class="sourceLineNo">1248</span> try {<a name="line.1248"></a> +<span class="sourceLineNo">1249</span> if (backOffTime > 0) {<a name="line.1249"></a> +<span class="sourceLineNo">1250</span> Thread.sleep(backOffTime);<a name="line.1250"></a> +<span class="sourceLineNo">1251</span> }<a name="line.1251"></a> +<span class="sourceLineNo">1252</span> } catch (InterruptedException e) {<a name="line.1252"></a> +<span class="sourceLineNo">1253</span> LOG.warn("#" + id + ", not sent: " + toReplay.size() + " operations, " + oldServer, e);<a name="line.1253"></a> +<span class="sourceLineNo">1254</span> Thread.currentThread().interrupt();<a name="line.1254"></a> +<span class="sourceLineNo">1255</span> return;<a name="line.1255"></a> +<span class="sourceLineNo">1256</span> }<a name="line.1256"></a> +<span class="sourceLineNo">1257</span><a name="line.1257"></a> +<span class="sourceLineNo">1258</span> groupAndSendMultiAction(toReplay, nextAttemptNumber);<a name="line.1258"></a> +<span class="sourceLineNo">1259</span> }<a name="line.1259"></a> +<span class="sourceLineNo">1260</span><a name="line.1260"></a> +<span class="sourceLineNo">1261</span> private void logNoResubmit(ServerName oldServer, int numAttempt,<a name="line.1261"></a> +<span class="sourceLineNo">1262</span> int failureCount, Throwable throwable, int failed, int stopped) {<a name="line.1262"></a> +<span class="sourceLineNo">1263</span> if (failureCount != 0 || numAttempt > startLogErrorsCnt + 1) {<a name="line.1263"></a> +<span class="sourceLineNo">1264</span> String timeStr = new Date(errorsByServer.getStartTrackingTime()).toString();<a name="line.1264"></a> +<span class="sourceLineNo">1265</span> String logMessage = createLog(numAttempt, failureCount, 0, oldServer,<a name="line.1265"></a> +<span class="sourceLineNo">1266</span> throwable, -1, false, timeStr, failed, stopped);<a name="line.1266"></a> +<span class="sourceLineNo">1267</span> if (failed != 0) {<a name="line.1267"></a> +<span class="sourceLineNo">1268</span> // Only log final failures as warning<a name="line.1268"></a> +<span class="sourceLineNo">1269</span> LOG.warn(logMessage);<a name="line.1269"></a> +<span class="sourceLineNo">1270</span> } else {<a name="line.1270"></a> +<span class="sourceLineNo">1271</span> LOG.info(logMessage);<a name="line.1271"></a> +<span class="sourceLineNo">1272</span> }<a name="line.1272"></a> +<span class="sourceLineNo">1273</span> }<a name="line.1273"></a> +<span class="sourceLineNo">1274</span> }<a name="line.1274"></a> +<span class="sourceLineNo">1275</span><a name="line.1275"></a> +<span class="sourceLineNo">1276</span> /**<a name="line.1276"></a> +<span class="sourceLineNo">1277</span> * Called when we receive the result of a server query.<a name="line.1277"></a> +<span class="sourceLineNo">1278</span> *<a name="line.1278"></a> +<span class="sourceLineNo">1279</span> * @param multiAction - the multiAction we sent<a name="line.1279"></a> +<span class="sourceLineNo">1280</span> * @param server - the location. It's used as a server name.<a name="line.1280"></a> +<span class="sourceLineNo">1281</span> * @param responses - the response, if any<a name="line.1281"></a> +<span class="sourceLineNo">1282</span> * @param numAttempt - the attempt<a name="line.1282"></a> +<span class="sourceLineNo">1283</span> */<a name="line.1283"></a> +<span class="sourceLineNo">1284</span> private void receiveMultiAction(MultiAction<Row> multiAction,<a name="line.1284"></a> +<span class="sourceLineNo">1285</span> ServerName server, MultiResponse responses, int numAttempt) {<a name="line.1285"></a> +<span class="sourceLineNo">1286</span> assert responses != null;<a name="line.1286"></a> <span class="sourceLineNo">1287</span><a name="line.1287"></a> -<span class="sourceLineNo">1288</span> List<Action<Row>> toReplay = new ArrayList<Action<Row>>();<a name="line.1288"></a> -<span class="sourceLineNo">1289</span> Throwable throwable = null;<a name="line.1289"></a> -<span class="sourceLineNo">1290</span> int failureCount = 0;<a name="line.1290"></a> -<span class="sourceLineNo">1291</span> boolean canRetry = true;<a name="line.1291"></a> -<span class="sourceLineNo">1292</span><a name="line.1292"></a> -<span class="sourceLineNo">1293</span> Map<byte[], MultiResponse.RegionResult> results = responses.getResults();<a name="line.1293"></a> -<span class="sourceLineNo">1294</span> updateStats(server, results);<a name="line.1294"></a> -<span class="sourceLineNo">1295</span><a name="line.1295"></a> -<span class="sourceLineNo">1296</span> int failed = 0, stopped = 0;<a name="line.1296"></a> -<span class="sourceLineNo">1297</span> // Go by original action.<a name="line.1297"></a> -<span class="sourceLineNo">1298</span> for (Map.Entry<byte[], List<Action<Row>>> regionEntry : multiAction.actions.entrySet()) {<a name="line.1298"></a> -<span class="sourceLineNo">1299</span> byte[] regionName = regionEntry.getKey();<a name="line.1299"></a> -<span class="sourceLineNo">1300</span> Map<Integer, Object> regionResults = results.get(regionName) == null<a name="line.1300"></a> -<span class="sourceLineNo">1301</span> ? null : results.get(regionName).result;<a name="line.1301"></a> -<span class="sourceLineNo">1302</span> if (regionResults == null) {<a name="line.1302"></a> -<span class="sourceLineNo">1303</span> if (!responses.getExceptions().containsKey(regionName)) {<a name="line.1303"></a> -<span class="sourceLineNo">1304</span> LOG.error("Server sent us neither results nor exceptions for "<a name="line.1304"></a> -<span class="sourceLineNo">1305</span> + Bytes.toStringBinary(regionName));<a name="line.1305"></a> -<span class="sourceLineNo">1306</span> responses.getExceptions().put(regionName, new RuntimeException("Invalid response"));<a name="line.1306"></a> -<span class="sourceLineNo">1307</span> }<a name="line.1307"></a> -<span class="sourceLineNo">1308</span> continue;<a name="line.1308"></a> -<span class="sourceLineNo">1309</span> }<a name="line.1309"></a> -<span class="sourceLineNo">1310</span> boolean regionFailureRegistered = false;<a name="line.1310"></a> -<span class="sourceLineNo">1311</span> for (Action<Row> sentAction : regionEntry.getValue()) {<a name="line.1311"></a> -<span class="sourceLineNo">1312</span> Object result = regionResults.get(sentAction.getOriginalIndex());<a name="line.1312"></a> -<span class="sourceLineNo">1313</span> // Failure: retry if it's make sense else update the errors lists<a name="line.1313"></a> -<span class="sourceLineNo">1314</span> if (result == null || result instanceof Throwable) {<a name="line.1314"></a> -<span class="sourceLineNo">1315</span> Row row = sentAction.getAction();<a name="line.1315"></a> -<span class="sourceLineNo">1316</span> throwable = ClientExceptionsUtil.findException(result);<a name="line.1316"></a> -<span class="sourceLineNo">1317</span> // Register corresponding failures once per server/once per region.<a name="line.1317"></a> -<span class="sourceLineNo">1318</span> if (!regionFailureRegistered) {<a name="line.1318"></a> -<span class="sourceLineNo">1319</span> regionFailureRegistered = true;<a name="line.1319"></a> -<span class="sourceLineNo">1320</span> connection.updateCachedLocations(<a name="line.1320"></a> -<span class="sourceLineNo">1321</span> tableName, regionName, row.getRow(), result, server);<a name="line.1321"></a> -<span class="sourceLineNo">1322</span> }<a name="line.1322"></a> -<span class="sourceLineNo">1323</span> if (failureCount == 0) {<a name="line.1323"></a> -<span class="sourceLineNo">1324</span> errorsByServer.reportServerError(server);<a name="line.1324"></a> -<span class="sourceLineNo">1325</span> // We determine canRetry only once for all calls, after reporting server failure.<a name="line.1325"></a> -<span class="sourceLineNo">1326</span> canRetry = errorsByServer.canTryMore(numAttempt);<a name="line.1326"></a> -<span class="sourceLineNo">1327</span> }<a name="line.1327"></a> -<span class="sourceLineNo">1328</span> ++failureCount;<a name="line.1328"></a> -<span class="sourceLineNo">1329</span> Retry retry = manageError(sentAction.getOriginalIndex(), row,<a name="line.1329"></a> -<span class="sourceLineNo">1330</span> canRetry ? Retry.YES : Retry.NO_RETRIES_EXHAUSTED, (Throwable) result, server);<a name="line.1330"></a> -<span class="sourceLineNo">1331</span> if (retry == Retry.YES) {<a name="line.1331"></a> -<span class="sourceLineNo">1332</span> toReplay.add(sentAction);<a name="line.1332"></a> -<span class="sourceLineNo">1333</span> } else if (retry == Retry.NO_OTHER_SUCCEEDED) {<a name="line.1333"></a> -<span class="sourceLineNo">1334</span> ++stopped;<a name="line.1334"></a> -<span class="sourceLineNo">1335</span> } else {<a name="line.1335"></a> -<span class="sourceLineNo">1336</span> ++failed;<a name="line.1336"></a> -<span class="sourceLineNo">1337</span> }<a name="line.1337"></a> -<span class="sourceLineNo">1338</span> } else {<a name="line.1338"></a> -<span class="sourceLineNo">1339</span> if (callback != null) {<a name="line.1339"></a> -<span class="sourceLineNo">1340</span> try {<a name="line.1340"></a> -<span class="sourceLineNo">1341</span> //noinspection unchecked<a name="line.1341"></a> -<span class="sourceLineNo">1342</span> // TODO: would callback expect a replica region name if it gets one?<a name="line.1342"></a> -<span class="sourceLineNo">1343</span> this.callback.update(regionName, sentAction.getAction().getRow(), (CResult) result);<a name="line.1343"></a> -<span class="sourceLineNo">1344</span> } catch (Throwable t) {<a name="line.1344"></a> -<span class="sourceLineNo">1345</span> LOG.error("User callback threw an exception for "<a name="line.1345"></a> -<span class="sourceLineNo">1346</span> + Bytes.toStringBinary(regionName) + ", ignoring", t);<a name="line.1346"></a> -<span class="sourceLineNo">1347</span> }<a name="line.1347"></a> -<span class="sourceLineNo">1348</span> }<a name="line.1348"></a> -<span class="sourceLineNo">1349</span> setResult(sentAction, result);<a name="line.1349"></a> -<span class="sourceLineNo">1350</span> }<a name="line.1350"></a> -<span class="sourceLineNo">1351</span> }<a name="line.1351"></a> -<span class="sourceLineNo">1352</span> }<a name="line.1352"></a> -<span class="sourceLineNo">1353</span><a name="line.1353"></a> -<span class="sourceLineNo">1354</span> // The failures global to a region. We will use for multiAction we sent previously to find the<a name="line.1354"></a> -<span class="sourceLineNo">1355</span> // actions to replay.<a name="line.1355"></a> -<span class="sourceLineNo">1356</span> for (Map.Entry<byte[], Throwable> throwableEntry : responses.getExceptions().entrySet()) {<a name="line.1356"></a> -<span class="sourceLineNo">1357</span> throwable = throwableEntry.getValue();<a name="line.1357"></a> -<span class="sourceLineNo">1358</span> byte[] region = throwableEntry.getKey();<a name="line.1358"></a> -<span class="sourceLineNo">1359</span> List<Action<Row>> actions = multiAction.actions.get(region);<a name="line.1359"></a> -<span class="sourceLineNo">1360</span> if (actions == null || actions.isEmpty()) {<a name="line.1360"></a> -<span class="sourceLineNo">1361</span> throw new IllegalStateException("Wrong response for the region: " +<a name="line.1361"></a> -<span class="sourceLineNo">1362</span> HRegionInfo.encodeRegionName(region));<a name="line.1362"></a> +<span class="sourceLineNo">1288</span> // Success or partial success<a name="line.1288"></a> +<span class="sourceLineNo">1289</span> // Analyze detailed results. We can still have individual failures to be redo.<a name="line.1289"></a> +<span class="sourceLineNo">1290</span> // two specific throwables are managed:<a name="line.1290"></a> +<span class="sourceLineNo">1291</span> // - DoNotRetryIOException: we continue to retry for other actions<a name="line.1291"></a> +<span class="sourceLineNo">1292</span> // - RegionMovedException: we update the cache with the new region location<a name="line.1292"></a> +<span class="sourceLineNo">1293</span><a name="line.1293"></a> +<span class="sourceLineNo">1294</span> List<Action<Row>> toReplay = new ArrayList<Action<Row>>();<a name="line.1294"></a> +<span class="sourceLineNo">1295</span> Throwable throwable = null;<a name="line.1295"></a> +<span class="sourceLineNo">1296</span> int failureCount = 0;<a name="line.1296"></a> +<span class="sourceLineNo">1297</span> boolean canRetry = true;<a name="line.1297"></a> +<span class="sourceLineNo">1298</span><a name="line.1298"></a> +<span class="sourceLineNo">1299</span> Map<byte[], MultiResponse.RegionResult> results = responses.getResults();<a name="line.1299"></a> +<span class="sourceLineNo">1300</span> updateStats(server, results);<a name="line.1300"></a> +<span class="sourceLineNo">1301</span><a name="line.1301"></a> +<span class="sourceLineNo">1302</span> int failed = 0, stopped = 0;<a name="line.1302"></a> +<span class="sourceLineNo">1303</span> // Go by original action.<a name="line.1303"></a> +<span class="sourceLineNo">1304</span> for (Map.Entry<byte[], List<Action<Row>>> regionEntry : multiAction.actions.entrySet()) {<a name="line.1304"></a> +<span class="sourceLineNo">1305</span> byte[] regionName = regionEntry.getKey();<a name="line.1305"></a> +<span class="sourceLineNo">1306</span> Map<Integer, Object> regionResults = results.get(regionName) == null<a name="line.1306"></a> +<span class="sourceLineNo">1307</span> ? null : results.get(regionName).result;<a name="line.1307"></a> +<span class="sourceLineNo">1308</span> if (regionResults == null) {<a name="line.1308"></a> +<span class="sourceLineNo">1309</span> if (!responses.getExceptions().containsKey(regionName)) {<a name="line.1309"></a> +<span class="sourceLineNo">1310</span> LOG.error("Server sent us neither results nor exceptions for "<a name="line.1310"></a> +<span class="sourceLineNo">1311</span> + Bytes.toStringBinary(regionName));<a name="line.1311"></a> +<span class="sourceLineNo">1312</span> responses.getExceptions().put(regionName, new RuntimeException("Invalid response"));<a name="line.1312"></a> +<span class="sourceLineNo">1313</span> }<a name="line.1313"></a> +<span class="sourceLineNo">1314</span> continue;<a name="line.1314"></a> +<span class="sourceLineNo">1315</span> }<a name="line.1315"></a> +<span class="sourceLineNo">1316</span> boolean regionFailureRegistered = false;<a name="line.1316"></a> +<span class="sourceLineNo">1317</span> for (Action<Row> sentAction : regionEntry.getValue()) {<a name="line.1317"></a> +<span class="sourceLineNo">1318</span> Object result = regionResults.get(sentAction.getOriginalIndex());<a name="line.1318"></a> +<span class="sourceLineNo">1319</span> // Failure: retry if it's make sense else update the errors lists<a name="line.1319"></a> +<span class="sourceLineNo">1320</span> if (result == null || result instanceof Throwable) {<a name="line.1320"></a> +<span class="sourceLineNo">1321</span> Row row = sentAction.getAction();<a name="line.1321"></a> +<span class="sourceLineNo">1322</span> throwable = ClientExceptionsUtil.findException(result);<a name="line.1322"></a> +<span class="sourceLineNo">1323</span> // Register corresponding failures once per server/once per region.<a name="line.1323"></a> +<span class="sourceLineNo">1324</span> if (!regionFailureRegistered) {<a name="line.1324"></a> +<span class="sourceLineNo">1325</span> regionFailureRegistered = true;<a name="line.1325"></a> +<span class="sourceLineNo">1326</span> try {<a name="line.1326"></a> +<span class="sourceLineNo">1327</span> connection.updateCachedLocations(<a name="line.1327"></a> +<span class="sourceLineNo">1328</span> tableName, regionName, row.getRow(), result, server);<a name="line.1328"></a> +<span class="sourceLineNo">1329</span> } catch (Throwable ex) {<a name="line.1329"></a> +<span class="sourceLineNo">1330</span> // That should never happen, but if it did, we want to make sure<a name="line.1330"></a> +<span class="sourceLineNo">1331</span> // we still process errors<a name="line.1331"></a> +<span class="sourceLineNo">1332</span> LOG.error("Couldn't update cached region locations: " + ex);<a name="line.1332"></a> +<span class="sourceLineNo">1333</span> }<a name="line.1333"></a> +<span class="sourceLineNo">1334</span> }<a name="line.1334"></a> +<span class="sourceLineNo">1335</span> if (failureCount == 0) {<a name="line.1335"></a> +<span class="sourceLineNo">1336</span> errorsByServer.reportServerError(server);<a name="line.1336"></a> +<span class="sourceLineNo">1337</span> // We determine canRetry only once for all calls, after reporting server failure.<a name="line.1337"></a> +<span class="sourceLineNo">1338</span> canRetry = errorsByServer.canTryMore(numAttempt);<a name="line.1338"></a> +<span class="sourceLineNo">1339</span> }<a name="line.1339"></a> +<span class="sourceLineNo">1340</span> ++failureCount;<a name="line.1340"></a> +<span class="sourceLineNo">1341</span> Retry retry = manageError(sentAction.getOriginalIndex(), row,<a name="line.1341"></a> +<span class="sourceLineNo">1342</span> canRetry ? Retry.YES : Retry.NO_RETRIES_EXHAUSTED, (Throwable) result, server);<a name="line.1342"></a> +<span class="sourceLineNo">1343</span> if (retry == Retry.YES) {<a name="line.1343"></a> +<span class="sourceLineNo">1344</span> toReplay.add(sentAction);<a name="line.1344"></a> +<span class="sourceLineNo">1345</span> } else if (retry == Retry.NO_OTHER_SUCCEEDED) {<a name="line.1345"></a> +<span class="sourceLineNo">1346</span> ++stopped;<a name="line.1346"></a> +<span class="sourceLineNo">1347</span> } else {<a name="line.1347"></a> +<span class="sourceLineNo">1348</span> ++failed;<a name="line.1348"></a> +<span class="sourceLineNo">1349</span> }<a name="line.1349"></a> +<span class="sourceLineNo">1350</span> } else {<a name="line.1350"></a> +<span class="sourceLineNo">1351</span> if (callback != null) {<a name="line.1351"></a> +<span class="sourceLineNo">1352</span> try {<a name="line.1352"></a> +<span class="sourceLineNo">1353</span> //noinspection unchecked<a name="line.1353"></a> +<span class="sourceLineNo">1354</span> // TODO: would callback expect a replica region name if it gets one?<a name="line.1354"></a> +<span class="sourceLineNo">1355</span> this.callback.update(regionName, sentAction.getAction().getRow(), (CResult) result);<a name="line.1355"></a> +<span class="sourceLineNo">1356</span> } catch (Throwable t) {<a name="line.1356"></a> +<span class="sourceLineNo">1357</span> LOG.error("User callback threw an exception for "<a name="line.1357"></a> +<span class="sourceLineNo">1358</span> + Bytes.toStringBinary(regionName) + ", ignoring", t);<a name="line.1358"></a> +<span class="sourceLineNo">1359</span> }<a name="line.1359"></a> +<span class="sourceLineNo">1360</span> }<a name="line.1360"></a> +<span class="sourceLineNo">1361</span> setResult(sentAction, result);<a name="line.1361"></a> +<span class="sourceLineNo">1362</span> }<a name="line.1362"></a> <span class="sourceLineNo">1363</span> }<a name="line.1363"></a> -<span class="sourceLineNo">1364</span><a name="line.1364"></a> -<span class="sourceLineNo">1365</span> if (failureCount == 0) {<a name="line.1365"></a> -<span class="sourceLineNo">1366</span> errorsByServer.reportServerError(server);<a name="line.1366"></a> -<span class="sourceLineNo">1367</span> canRetry = errorsByServer.canTryMore(numAttempt);<a name="line.1367"></a> -<span class="sourceLineNo">1368</span> }<a name="line.1368"></a> -<span class="sourceLineNo">1369</span> if (null == tableName && ClientExceptionsUtil.isMetaClearingException(throwable)) {<a name="line.1369"></a> -<span class="sourceLineNo">1370</span> // For multi-actions, we don't have a table name, but we want to make sure to clear the<a name="line.1370"></a> -<span class="sourceLineNo">1371</span> // cache in case there were location-related exceptions. We don't to clear the cache<a name="line.1371"></a> -<span class="sourceLineNo">1372</span> // for every possible exception that comes through, however.<a name="line.1372"></a> -<span class="sourceLineNo">1373</span> connection.clearCaches(server);<a name="line.1373"></a> -<span class="sourceLineNo">1374</span> } else {<a name="line.1374"></a> -<span class="sourceLineNo">1375</span> connection.updateCachedLocations(<a name="line.1375"></a> -<span class="sourceLineNo">1376</span> tableName, region, actions.get(0).getAction().getRow(), throwable, server);<a name="line.1376"></a> -<span class="sourceLineNo">1377</span> }<a name="line.1377"></a> -<span class="sourceLineNo">1378</span> failureCount += actions.size();<a name="line.1378"></a> -<span class="sourceLineNo">1379</span><a name="line.1379"></a> -<span class="sourceLineNo">1380</span> for (Action<Row> action : actions) {<a name="line.1380"></a> -<span class="sourceLineNo">1381</span> Row row = action.getAction();<a name="line.1381"></a> -<span class="sourceLineNo">1382</span> Retry retry = manageError(action.getOriginalIndex(), row,<a name="line.1382"></a> -<span class="sourceLineNo">1383</span> canRetry ? Retry.YES : Retry.NO_RETRIES_EXHAUSTED, throwable, server);<a name="line.1383"></a> -<span class="sourceLineNo">1384</span> if (retry == Retry.YES) {<a name="line.1384"></a> -<span class="sourceLineNo">1385</span> toReplay.add(action);<a name="line.1385"></a> -<span class="sourceLineNo">1386</span> } else if (retry == Retry.NO_OTHER_SUCCEEDED) {<a name="line.1386"></a> -<span class="sourceLineNo">1387</span> ++stopped;<a name="line.1387"></a> -<span class="sourceLineNo">1388</span> } else {<a name="line.1388"></a> -<span class="sourceLineNo">1389</span> ++failed;<a name="line.1389"></a> -<span class="sourceLineNo">1390</span> }<a name="line.1390"></a> -<span class="sourceLineNo">1391</span> }<a name="line.1391"></a> -<span class="sourceLineNo">1392</span> }<a name="line.1392"></a> -<span class="sourceLineNo">1393</span> if (toReplay.isEmpty()) {<a name="line.1393"></a> -<span class="sourceLineNo">1394</span> logNoResubmit(server, numAttempt, failureCount, throwable, failed, stopped);<a name="line.1394"></a> -<span class="sourceLineNo">1395</span> } else {<a name="line.1395"></a> -<span class="sourceLineNo">1396</span> resubmit(server, toReplay, numAttempt, failureCount, throwable);<a name="line.1396"></a> -<span class="sourceLineNo">1397</span> }<a name="line.1397"></a> -<span class="sourceLineNo">1398</span> }<a name="line.1398"></a> -<span class="sourceLineNo">1399</span><a name="line.1399"></a> -<span class="sourceLineNo">1400</span> private String createLog(int numAttempt, int failureCount, int replaySize, ServerName sn,<a name="line.1400"></a> -<span class="sourceLineNo">1401</span> Throwable error, long backOffTime, boolean willRetry, String startTime,<a name="line.1401"></a> -<span class="sourceLineNo">1402</span> int failed, int stopped) {<a name="line.1402"></a> -<span class="sourceLineNo">1403</span> StringBuilder sb = new StringBuilder();<a name="line.1403"></a> -<span class="sourceLineNo">1404</span> sb.append("#").append(id).append(", table=").append(tableName).append(", ")<a name="line.1404"></a> -<span class="sourceLineNo">1405</span> .append("attempt=").append(numAttempt)<a name="line.1405"></a> -<span class="sourceLineNo">1406</span> .append("/").append(numTries).append(" ");<a name="line.1406"></a> -<span class="sourceLineNo">1407</span><a name="line.1407"></a> -<span class="sourceLineNo">1408</span> if (failureCount > 0 || error != null){<a name="line.1408"></a> -<span class="sourceLineNo">1409</span> sb.append("failed=").append(failureCount).append("ops").append(", last exception: ").<a name="line.1409"></a> -<span class="sourceLineNo">1410</span> append(error == null ? "null" : error);<a name="line.1410"></a> -<span class="sourceLineNo">1411</span> } else {<a name="line.1411"></a> -<span class="sourceLineNo">1412</span> sb.append("succeeded");<a name="line.1412"></a> -<span class="sourceLineNo">1413</span> }<a name="line.1413"></a> -<span class="sourceLineNo">1414</span><a name="line.1414"></a> -<span class="sourceLineNo">1415</span> sb.append(" on ").append(sn).append(", tracking started ").append(startTime);<a name="line.1415"></a> -<span class="sourceLineNo">1416</span><a name="line.1416"></a> -<span class="sourceLineNo">1417</span> if (willRetry) {<a name="line.1417"></a> -<span class="sourceLineNo">1418</span> sb.append(", retrying after=").append(backOffTime).append("ms").<a name="line.1418"></a> -<span class="sourceLineNo">1419</span> append(", replay=").append(replaySize).append("ops");<a name="line.1419"></a> -<span class="sourceLineNo">1420</span> } else if (failureCount > 0) {<a name="line.1420"></a> -<span class="sourceLineNo">1421</span> if (stopped > 0) {<a name="line.1421"></a> -<span class="sourceLineNo">1422</span> sb.append("; not retrying ").append(stopped).append(" due to success from other replica");<a name="line.1422"></a> -<span class="sourceLineNo">1423</span> }<a name="line.1423"></a> -<span class="sourceLineNo">1424</span> if (failed > 0) {<a name="line.1424"></a> -<span class="sourceLineNo">1425</span> sb.append("; not retrying ").append(failed).append(" - final failure");<a name="line.1425"></a> -<span class="sourceLineNo">1426</span> }<a name="line.1426"></a> -<span class="sourceLineNo">1427</span><a name="line.1427"></a> -<span class="sourceLineNo">1428</span> }<a name="line.1428"></a> -<span class="sourceLineNo">1429</span><a name="line.1429"></a> -<span class="sourceLineNo">1430</span> return sb.toString();<a name="line.1430"></a> -<span class="sourceLineNo">1431</span> }<a name="line.1431"></a> +<span class="sourceLineNo">1364</span> }<a name="line.1364"></a> +<span class="sourceLineNo">1365</span><a name="line.1365"></a> +<span class="sourceLineNo">1366</span> // The failures global to a region. We will use for multiAction we sent previously to find the<a name="line.1366"></a> +<span class="sourceLineNo">1367</span> // actions to replay.<a name="line.1367"></a> +<span class="sourceLineNo">1368</span> for (Map.Entry<byte[], Throwable> throwableEntry : responses.getExceptions().entrySet()) {<a name="line.1368"></a> +<span class="sourceLineNo">1369</span> throwable = throwableEntry.getValue();<a name="line.1369"></a> +<span class="sourceLineNo">1370</span> byte[] region = throwableEntry.getKey();<a name="line.1370"></a> +<span class="sourceLineNo">1371</span> List<Action<Row>> actions = multiAction.actions.get(region);<a name="line.1371"></a> +<span class="sourceLineNo">1372</span> if (actions == null || actions.isEmpty()) {<a name="line.1372"></a> +<span class="sourceLineNo">1373</span> throw new IllegalStateException("Wrong response for the region: " +<a name="line.1373"></a> +<span class="sourceLineNo">1374</span> HRegionInfo.encodeRegionName(region));<a name="line.1374"></a> +<span class="sourceLineNo">1375</span> }<a name="line.1375"></a> +<span class="sourceLineNo">1376</span><a name="line.1376"></a> +<span class="sourceLineNo">1377</span> if (failureCount == 0) {<a name="line.1377"></a> +<span class="sourceLineNo">1378</span> errorsByServer.reportServerError(server);<a name="line.1378"></a> +<span class="sourceLineNo">1379</span> canRetry = errorsByServer.canTryMore(numAttempt);<a name="line.1379"></a> +<span class="sourceLineNo">1380</span> }<a name="line.1380"></a> +<span class="sourceLineNo">1381</span> if (null == tableName && ClientExceptionsUtil.isMetaClearingException(throwable)) {<a name="line.1381"></a> +<span class="sourceLineNo">1382</span> // For multi-actions, we don't have a table name, but we want to make sure to clear the<a name="line.1382"></a> +<span class="sourceLineNo">1383</span> // cache in case there were location-related exceptions. We don't to clear the cache<a name="line.1383"></a> +<span class="sourceLineNo">1384</span> // for every possible exception that comes through, however.<a name="line.1384"></a> +<span class="sourceLineNo">1385</span> connection.clearCaches(server);<a name="line.1385"></a> +<span class="sourceLineNo">1386</span> } else {<a name="line.1386"></a> +<span class="sourceLineNo">1387</span> try {<a name="line.1387"></a> +<span class="sourceLineNo">1388</span> connection.updateCachedLocations(<a name="line.1388"></a> +<span class="sourceLineNo">1389</span> tableName, region, actions.get(0).getAction().getRow(), throwable, server);<a name="line.1389"></a> +<span class="sourceLineNo">1390</span> } catch (Throwable ex) {<a name="line.1390"></a> +<span class="sourceLineNo">1391</span> // That should never happen, but if it did, we want to make sure<a name="line.1391"></a> +<span class="sourceLineNo">1392</span> // we still process errors<a name="line.1392"></a> +<span class="sourceLineNo">1393</span> LOG.error("Couldn't update cached region locations: " + ex);<a name="line.1393"></a> +<span class="sourceLineNo">1394</span> }<a name="line.1394"></a> +<span class="sourceLineNo">1395</span> }<a name="line.1395"></a> +<span class="sourceLineNo">1396</span> failureCount += actions.size();<a name="line.1396"></a> +<span class="sourceLineNo">1397</span><a name="line.1397"></a> +<span class="sourceLineNo">1398</span> for (Action<Row> action : actions) {<a name="line.1398"></a> +<span class="sourceLineNo">1399</span> Row row = action.getAction();<a name="line.1399"></a> +<span class="sourceLineNo">1400</span> Retry retry = manageError(action.getOriginalIndex(), row,<a name="line.1400"></a> +<span class="sourceLineNo">1401</span> canRetry ? Retry.YES : Retry.NO_RETRIES_EXHAUSTED, throwable, server);<a name="line.1401"></a> +<span class="sourceLineNo">1402</span> if (retry == Retry.YES) {<a name="line.1402"></a> +<span class="sourceLineNo">1403</span> toReplay.add(action);<a name="line.1403"></a> +<span class="sourceLineNo">1404</span> } else if (retry == Retry.NO_OTHER_SUCCEEDED) {<a name="line.1404"></a> +<span class="sourceLineNo">1405</span> ++stopped;<a name="line.1405"></a> +<span class="sourceLineNo">1406</span> } else {<a name="line.1406"></a> +<span class="sourceLineNo">1407</span> ++failed;<a name="line.1407"></a> +<span class="sourceLineNo">1408</span> }<a name="line.1408"></a> +<span class="sourceLineNo">1409</span> }<a name="line.1409"></a> +<span class="sourceLineNo">1410</span> }<a name="line.1410"></a> +<span class="sourceLineNo">1411</span> if (toReplay.isEmpty()) {<a name="line.1411"></a> +<span class="sourceLineNo">1412</span> logNoResubmit(server, numAttempt, failureCount, throwable, failed, stopped);<a name="line.1412"></a> +<span class="sourceLineNo">1413</span> } else {<a name="line.1413"></a> +<span class="sourceLineNo">1414</span> resubmit(server, toReplay, numAttempt, failureCount, throwable);<a name="line.1414"></a> +<span class="sourceLineNo">1415</span> }<a name="line.1415"></a> +<span class="sourceLineNo">1416</span> }<a name="line.1416"></a> +<span class="sourceLineNo">1417</span><a name="line.1417"></a> +<span class="sourceLineNo">1418</span> private String createLog(int numAttempt, int failureCount, int replaySize, ServerName sn,<a name="line.1418"></a> +<span class="sourceLineNo">1419</span> Throwable error, long backOffTime, boolean willRetry, String startTime,<a name="line.1419"></a> +<span class="sourceLineNo">1420</span> int failed, int stopped) {<a name="line.1420"></a> +<span class="sourceLineNo">1421</span> StringBuilder sb = new StringBuilder();<a name="line.1421"></a> +<span class="sourceLineNo">1422</span> sb.append("#").append(id).append(", table=").append(tableName).append(", ")<a name="line.1422"></a> +<span class="sourceLineNo">1423</span> .append("attempt=").append(numAttempt)<a name="line.1423"></a> +<span class="sourceLineNo">1424</span> .append("/").append(numTries).append(" ");<a name="line.1424"></a> +<span class="sourceLineNo">1425</span><a name="line.1425"></a> +<span class="sourceLineNo">1426</span> if (failureCount > 0 || error != null){<a name="line.1426"></a> +<span class="sourceLineNo">1427</span> sb.append("failed=").append(failureCount).append("ops").append(", last exception: ").<a name="line.1427"></a> +<span class="sourceLineNo">1428</span> append(error == null ? "null" : error);<a name="line.1428"></a> +<span class="sourceLineNo">1429</span> } else {<a name="line.1429"></a> +<span class="sourceLineNo">1430</span> sb.append("succeeded");<a name="line.1430"></a> +<span class="sourceLineNo">1431</span> }<a name="line.1431"></a> <span class="sourceLineNo">1432</span><a name="line.1432"></a> -<span class="sourceLineNo">1433</span> /**<a name="line.1433"></a> -<span class="sourceLineNo">1434</span> * Sets the non-error result from a particular action.<a name="line.1434"></a> -<span class="sourceLineNo">1435</span> * @param action Action (request) that the server responded to.<a name="line.1435"></a> -<span class="sourceLineNo">1436</span> * @param result The result.<a name="line.1436"></a> -<span class="sourceLineNo">1437</span> */<a name="line.1437"></a> -<span class="sourceLineNo">1438</span> private void setResult(Action<Row> action, Object result) {<a name="line.1438"></a> -<span class="sourceLineNo">1439</span> if (result == null) {<a name="line.1439"></a> -<span class="sourceLineNo">1440</span> throw new RuntimeException("Result cannot be null");<a name="line.1440"></a> -<span class="sourceLineNo">1441</span> }<a name="line.1441"></a> -<span class="sourceLineNo">1442</span> ReplicaResultState state = null;<a name="line.1442"></a> -<span class="sourceLineNo">1443</span> boolean isStale = !RegionReplicaUtil.isDefaultReplica(action.getReplicaId());<a name="line.1443"></a> -<span class="sourceLineNo">1444</span> int index = action.getOriginalIndex();<a name="line.1444"></a> -<span class="sourceLineNo">1445</span> if (results == null) {<a name="line.1445"></a> -<span class="sourceLineNo">1446</span> decActionCounter(index);<a name="line.1446"></a> -<span class="sourceLineNo">1447</span> return; // Simple case, no replica requests.<a name="line.1447"></a> -<span class="sourceLineNo">1448</span> }<a name="line.1448"></a> -<span class="sourceLineNo">1449</span> state = trySetResultSimple(index, action.getAction(), false, result, null, isStale);<a name="line.1449"></a> -<span class="sourceLineNo">1450</span> if (state == null) {<a name="line.1450"></a> -<span class="sourceLineNo">1451</span> return; // Simple case, no replica requests.<a name="line.1451"></a> -<span class="sourceLineNo">1452</span> }<a name="line.1452"></a> -<span class="sourceLineNo">1453</span> // At this point we know that state is set to replica tracking class.<a name="line.1453"></a> -<span class="sourceLineNo">1454</span> // It could be that someone else is also looking at it; however, we know there can<a name="line.1454"></a> -<span class="sourceLineNo">1455</span> // only be one state object, and only one thread can set callCount to 0. Other threads<a name="line.1455"></a> -<span class="sourceLineNo">1456</span> // will either see state with callCount 0 after locking it; or will not see state at all<a name="line.1456"></a> -<span class="sourceLineNo">1457</span> // we will replace it with the result.<a name="line.1457"></a> -<span class="sourceLineNo">1458</span> synchronized (state) {<a name="line.1458"></a> -<span class="sourceLineNo">1459</span> if (state.callCount == 0) {<a name="line.1459"></a> -<span class="sourceLineNo">1460</span> return; // someone already set the result<a name="line.1460"></a> -<span class="sourceLineNo">1461</span> }<a name="line.1461"></a> -<span class="sourceLineNo">1462</span> state.callCount = 0;<a name="line.1462"></a> -<span class="sourceLineNo">1463</span> }<a name="line.1463"></a> -<span class="sourceLineNo">1464</span> synchronized (replicaResultLock) {<a name="line.1464"></a> -<span class="sourceLineNo">1465</span> if (results[index] != state) {<a name="line.1465"></a> -<span class="sourceLineNo">1466</span> throw new AssertionError("We set the callCount but someone else replaced the result");<a name="line.1466"></a> -<span class="sourceLineNo">1467</span> }<a name="line.1467"></a> -<span class="sourceLineNo">1468</span> results[index] = result;<a name="line.1468"></a> -<span class="sourceLineNo">1469</span> }<a name="line.1469"></a> -<span class="sourceLineNo">1470</span><a name="line.1470"></a> -<span class="sourceLineNo">1471</span> decActionCounter(index);<a name="line.1471"></a> -<span class="sourceLineNo">1472</span> }<a name="line.1472"></a> -<span class="sourceLineNo">1473</span><a name="line.1473"></a> -<span class="sourceLineNo">1474</span> /**<a name="line.1474"></a> -<span class="sourceLineNo">1475</span> * Sets the error from a particular action.<a name="line.1475"></a> -<span class="sourceLineNo">1476</span> * @param index Original action index.<a name="line.1476"></a> -<span class="sourceLineNo">1477</span> * @param row Original request.<a name="line.1477"></a> -<span class="sourceLineNo">1478</span> * @param throwable The resulting error.<a name="line.1478"></a> -<span class="sourceLineNo">1479</span> * @param server The source server.<a name="line.1479"></a> -<span class="sourceLineNo">1480</span> */<a name="line.1480"></a> -<span class="sourceLineNo">1481</span> private void setError(int index, Row row, Throwable throwable, ServerName server) {<a name="line.1481"></a> -<span class="sourceLineNo">1482</span> ReplicaResultState state = null;<a name="line.1482"></a> -<span class="sourceLineNo">1483</span> if (results == null) {<a name="line.1483"></a> -<span class="sourceLineNo">1484</span> // Note that we currently cannot have replica requests with null results. So it shouldn't<a name="line.1484"></a> -<span class="sourceLineNo">1485</span> // happen that multiple replica calls will call dAC for same actions with results == null.<a name="line.1485"></a> -<span class="sourceLineNo">1486</span> // Only one call per action should be present in this case.<a name="line.1486"></a> -<span class="sourceLineNo">1487</span> errors.add(throwable, row, server);<a name="line.1487"></a> -<span class="sourceLineNo">1488</span> decActionCounter(index);<a name="line.1488"></a> -<span class="sourceLineNo">1489</span> return; // Simple case, no replica requests.<a name="line.1489"></a> -<span class="sourceLineNo">1490</span> }<a name="line.1490"></a> -<span class="sourceLineNo">1491</span> state = trySetResultSimple(index, row, true, throwable, server, false);<a name="line.1491"></a> -<span class="sourceLineNo">1492</span> if (state == null) {<a name="line.1492"></a> -<span class="sourceLineNo">1493</span> return; // Simple case, no replica requests.<a name="line.1493"></a> -<span class="sourceLineNo">1494</span> }<a name="line.1494"></a> -<span class="sourceLineNo">1495</span> BatchErrors target = null; // Error will be added to final errors, or temp replica errors.<a name="line.1495"></a> -<span class="sourceLineNo">1496</span> boolean isActionDone = false;<a name="line.1496"></a> -<span class="sourceLineNo">1497</span> synchronized (state) {<a name="line.1497"></a> -<span class="sourceLineNo">1498</span> switch (state.callCount) {<a name="line.1498"></a> -<span class="sourceLineNo">1499</span> case 0: return; // someone already set the result<a name="line.1499"></a> -<span class="sourceLineNo">1500</span> case 1: { // All calls failed, we are the last error.<a name="line.1500"></a> -<span class="sourceLineNo">1501</span> target = errors;<a name="line.1501"></a> -<span class="sourceLineNo">1502</span> isActionDone = true;<a name="line.1502"></a> -<span class="sourceLineNo">1503</span> break;<a name="line.1503"></a> -<span class="sourceLineNo">1504</span> }<a name="line.1504"></a> -<span class="sourceLineNo">1505</span> default: {<a name="line.1505"></a> -<span class="sourceLineNo">1506</span> assert state.callCount > 1;<a name="line.1506"></a> -<span class="sourceLineNo">1507</span> if (state.replicaErrors == null) {<a name="line.1507"></a> -<span class="sourceLineNo">1508</span> state.replicaErrors = new BatchErrors();<a name="line.1508"></a> -<span class="sourceLineNo">1509</span> }<a name="line.1509"></a> -<span class="sourceLineNo">1510</span> target = state.replicaErrors;<a name="line.1510"></a> -<span class="sourceLineNo">1511</span> break;<a name="line.1511"></a> -<span class="sourceLineNo">1512</span> }<a name="line.1512"></a> -<span class="sourceLineNo">1513</span> }<a name="line.1513"></a> -<span class="sourceLineNo">1514</span> --state.callCount;<a name="line.1514"></a> -<span class="sourceLineNo">1515</span> }<a name="line.1515"></a> -<span class="sourceLineNo">1516</span> target.add(throwable, row, server);<a name="line.1516"></a> -<span class="sourceLineNo">1517</span> if (isActionDone) {<a name="line.1517"></a> -<span class="sourceLineNo">1518</span> if (state.replicaErrors != null) { // last call, no need to lock<a name="line.1518"></a> -<span class="sourceLineNo">1519</span> errors.merge(state.replicaErrors);<a name="line.1519"></a> -<span class="sourceLineNo">1520</span> }<a name="line.1520"></a> -<span class="sourceLineNo">1521</span> // See setResult for explanations.<a name="line.1521"></a> -<span class="sourceLineNo">1522</span> synchronized (replicaResultLock) {<a name="line.1522"></a> -<span class="sourceLineNo">1523</span> if (results[index] != state) {<a name="line.1523"></a> -<span class="sourceLineNo">1524</span> throw new AssertionError("We set the callCount but someone else replaced the result");<a name="line.1524"></a> -<span class="sourceLineNo">1525</span> }<a name="line.1525"></a> -<span class="sourceLineNo">1526</span> results[index] = throwable;<a name="line.1526"></a> -<span class="sourceLineNo">1527</span> }<a name="line.1527"></a> -<span class="sourceLineNo">1528</span> decActionCounter(index);<a name="line.1528"></a> -<span class="sourceLineNo">1529</span> }<a name="line.1529"></a> -<span class="sourceLineNo">1530</span> }<a name="line.1530"></a> -<span class="sourceLineNo">1531</span><a name="line.1531"></a> -<span class="sourceLineNo">1532</span> /**<a name="line.1532"></a> -<span class="sourceLineNo">1533</span> * Checks if the action is complete; used on error to prevent needless retries.<a name="line.1533"></a> -<span class="sourceLineNo">1534</span> * Does not synchronize, assuming element index/field accesses are atomic.<a name="line.1534"></a> -<span class="sourceLineNo">1535</span> * This is an opportunistic optimization check, doesn't have to be strict.<a name="line.1535"></a> -<span class="sourceLineNo">1536</span> * @param index Original action index.<a name="line.1536"></a> -<span class="sourceLineNo">1537</span> * @param row Original request.<a name="line.1537"></a> -<span class="sourceLineNo">1538</span> */<a name="line.1538"></a> -<span class="sourceLineNo">1539</span> private boolean isActionComplete(int index, Row row) {<a name="line.1539"></a> -<span class="sourceLineNo">1540</span> if (!isReplicaGet(row)) return false;<a name="line.1540"></a> -<span class="sourceLineNo">1541</span> Object resObj = results[index];<a name="line.1541"></a> -<span class="sourceLineNo">1542</span> return (resObj != null) && (!(resObj instanceof ReplicaResultState)<a name="line.1542"></a> -<span class="sourceLineNo">1543</span> || ((ReplicaResultState)resObj).callCount == 0);<a name="line.1543"></a> -<span class="sourceLineNo">1544</span> }<a name="line.1544"></a> -<span class="sourceLineNo">1545</span><a name="line.1545"></a> -<span class="sourceLineNo">1546</span> /**<a name="line.1546"></a> -<span class="sourceLineNo">1547</span> * Tries to set the result or error for a particular action as if there were no replica calls.<a name="line.1547"></a> -<span class="sourceLineNo">1548</span> * @return null if successful; replica state if there were in fact replica calls.<a name="line.1548"></a> -<span class="sourceLineNo">1549</span> */<a name="line.1549"></a> -<span class="sourceLineNo">1550</span> private ReplicaResultState trySetResultSimple(int index, Row row, boolean isError,<a name="line.1550"></a> -<span class="sourceLineNo">1551</span> Object result, ServerName server, boolean isFromReplica) {<a name="line.1551"></a> -<span class="sourceLineNo">1552</span> Object resObj = null;<a name="line.1552"></a> -<span class="sourceLineNo">1553</span> if (!isReplicaGet(row)) {<a name="line.1553"></a> -<span class="sourceLineNo">1554</span> if (isFromReplica) {<a name="line.1554"></a> -<span class="sourceLineNo">1555</span> throw new AssertionError("Unexpected stale result for " + row);<a name="line.1555"></a> -<span class="sourceLineNo">1556</span> }<a name="line.1556"></a> -<span class="sourceLineNo">1557</span> results[index] = result;<a name="line.1557"></a> -<span class="sourceLineNo">1558</span> } else {<a name="line.1558"></a> -<span class="sourceLineNo">1559</span> synchronized (replicaResultLock) {<a name="line.1559"></a> -<span class="sourceLineNo">1560</span> resObj = results[index];<a name="line.1560"></a> -<span class="sourceLineNo">1561</span> if (resObj == null) {<a name="line.1561"></a> -<span class="sourceLineNo">1562</span> if (isFromReplica) {<a name="line.1562"></a> -<span class="sourceLineNo">1563</span> throw new AssertionError("Unexpected stale result for " + row);<a name="line.1563"></a> -<span class="sourceLineNo">1564</span> }<a name="line.1564"></a> -<span class="sourceLineNo">1565</span> results[index] = result;<a name="line.1565"></a> -<span class="sourceLineNo">1566</span> }<a name="line.1566"></a> -<span class="sourceLineNo">1567</span> }<a name="line.1567"></a> -<span class="sourceLineNo">1568</span> }<a name="line.1568"></a> -<span class="sourceLineNo">1569</span><a name="line.1569"></a> -<span class="sourceLineNo">1570</span> ReplicaResultState rrs =<a name="line.1570"></a> -<span class="sourceLineNo">1571</span> (resObj instanceof ReplicaResultState) ? (ReplicaResultState)resObj : null;<a name="line.1571"></a> -<span class="sourceLineNo">1572</span> if (rrs == null && isError) {<a name="line.1572"></a> -<span class="sourceLineNo">1573</span> // The resObj is not replica state (null or already set).<a name="line.1573"></a> -<span class="sourceLineNo">1574</span> errors.add((Throwable)result, row, server);<a name="line.1574"></a> -<span class="sourceLineNo">1575</span> }<a name="line.1575"></a> -<span class="sourceLineNo">1576</span><a name="line.1576"></a> -<span class="sourceLineNo">1577</span> if (resObj == null) {<a name="line.1577"></a> -<span class="sourceLineNo">1578</span> // resObj is null - no replica calls were made.<a name="line.1578"></a> -<span class="sourceLineNo">1579</span> decActionCounter(index);<a name="line.1579"></a> -<span class="sourceLineNo">1580</span> return null;<a name="line.1580"></a> -<span class="sourceLineNo">1581</span> }<a name="line.1581"></a> -<span class="sourceLineNo">1582</span> return rrs;<a name="line.1582"></a> -<span class="sourceLineNo">1583</span> }<a name="line.1583"></a> -<span class="sourceLineNo">1584</span><a name="line.1584"></a> -<span class="sourceLineNo">1585</span> private void decActionCounter(int index) {<a name="line.1585"></a> -<span class="sourceLineNo">1586</span> long actionsRemaining = actionsInProgress.decrementAndGet();<a name="line.1586"></a> -<span class="sourceLineNo">1587</span> if (actionsRemaining < 0) {<a name="line.1587"></a> -<span class="sourceLineNo">1588</span> String error = buildDetailedErrorMsg("Incorrect actions in progress", index);<a name="line.1588"></a> -<span class="sourceLineNo">1589</span> throw new AssertionError(error);<a name="line.1589"></a> -<span class="sourceLineNo">1590</span> } else if (actionsRemaining == 0) {<a name="line.1590"></a> -<span class="sourceLineNo">1591</span> synchronized (actionsInProgress) {<a name="line.1591"></a> -<span class="sourceLineNo">1592</span> actionsInProgress.notifyAll();<a name="line.1592"></a> -<span class="sourceLineNo">1593</span> }<a name="line.1593"></a> -<span class="sourceLineNo">1594</span> }<a name="line.1594"></a> -<span class="sourceLineNo">1595</span> }<a name="line.1595"></a> -<span class="sourceLineNo">1596</span><a name="line.1596"></a> -<span class="sourceLineNo">1597</span> private String buildDetailedErrorMsg(String string, int index) {<a name="line.1597"></a> -<span class="sourceLineNo">1598</span> StringBuilder error = new StringBuilder(128);<a name="line.1598"></a> -<span class="sourceLineNo">1599</span> error.append(string).append("; called for ").append(index).append(", actionsInProgress ")<a name="line.1599"></a> -<span class="sourceLineNo">1600</span> .append(actionsInProgress.get()).append("; replica gets: ");<a name="line.1600"></a> -<span class="sourceLineNo">1601</span> if (replicaGetIndices != null) {<a name="line.1601"></a> -<span class="sourceLineNo">1602</span> for (int i = 0; i < replicaGetIndices.length; ++i) {<a name="line.1602"></a> -<span class="sourceLineNo">1603</span> error.append(replicaGetIndices[i]).append(", ");<a name="line.1603"></a> -<span class="sourceLineNo">1604</span> }<a name="line.1604"></a> -<span class="sourceLineNo">1605</span> } else {<a name="line.1605"></a> -<span class="sourceLineNo">1606</span> error.append(hasAnyReplicaGets ? "all" : "none");<a name="line.1606"></a> -<span class="sourceLineNo">1607</span> }<a name="line.1607"></a> -<span class="sourceLineNo">1608</span> error.append("; results ");<a name="line.1608"></a> -<span class="sourceLineNo">1609</span> if (results != null) {<a name="line.1609"></a> -<span class="sourceLineNo">1610</span> for (int i = 0; i < results.length; ++i) {<a name="line.1610"></a> -<span class="sourceLineNo">1611</span> Object o = results[i];<a name="line.1611"></a> -<span class="sourceLineNo">1612</span> error.append(((o == null) ? "null" : o.toString())).append(", ");<a name="line.1612"></a> -<span class="sourceLineNo">1613</span> }<a name="line.1613"></a> -<span class="sourceLineNo">1614</span> }<a name="line.1614"></a> -<span class="sourceLineNo">1615</span> return error.toString();<a name="line.1615"></a> -<span class="sourceLineNo">1616</span> }<a name="line.1616"></a> -<span class="sourceLineNo">1617</span><a name="line.1617"></a> -<span class="sourceLineNo">1618</span> @Override<a name="line.1618"></a> -<span class="sourceLineNo">1619</span> public void waitUntilDone() throws InterruptedIOException {<a name="line.1619"></a> -<span class="sourceLineNo">1620</span> try {<a name="line.1620"></a> -<span class="sourceLineNo">1621</span> waitUntilDone(Long.MAX_VALUE);<a name="line.1621"></a> -<span class="sourceLineNo">1622</span> } catch (InterruptedException iex) {<a name="line.1622"></a> -<span class="sourceLineNo">1623</span> throw new InterruptedIOException(iex.getMessage());<a name="line.1623"></a> -<span class="sourceLineNo">1624</span> } finally {<a name="line.1624"></a> -<span class="sourceLineNo">1625</span> if (callsInProgress != null) {<a name="line.1625"></a> -<span class="sourceLineNo">1626</span> for (PayloadCarryingServerCallable clb : callsInProgress) {<a name="line.1626"></a> -<span class="sourceLineNo">1627</span> clb.cancel();<a name="line.1627"></a> -<span class="sourceLineNo">1628</span> }<a name="line.1628"></a> -<span class="sourceLineNo">1629</span> }<a name="line.1629"></a> -<span class="sourceLineNo">1630</span> }<a name="line.1630"></a> -<span class="sourceLineNo">1631</span> }<a name="line.1631"></a> -<span class="sourceLineNo">1632</span><a name="line.1632"></a> -<span class="sourceLineNo">1633</span> private boolean waitUntilDone(long cutoff) throws InterruptedException {<a name="line.1633"></a> -<span class="sourceLineNo">1634</span> boolean hasWait = cutoff != Long.MAX_VALUE;<a name="line.1634"></a> -<span class="sourceLineNo">1635</span> long lastLog = EnvironmentEdgeManager.currentTime();<a name="line.1635"></a> -<span class="sourceLineNo">1636</span> long currentInProgress;<a name="line.1636"></a> -<span class="sourceLineNo">1637</span> while (0 != (currentInProgress = actionsInProgress.get())) {<a name="line.1637"></a> -<span class="sourceLineNo">1638</span> long now = EnvironmentEdgeManager.currentTime();<a name="line.1638"></a> -<span class="sourceLineNo">1639</span> if (hasWait && (now * 1000L) > cutoff) {<a name="line.1639"></a> -<span class="sourceLineNo">1640</span> return false;<a name="line.1640"></a> -<span class="sourceLineNo">1641</span> }<a name="line.1641"></a> -<span class="sourceLineNo">1642</span> if (!hasWait) { // Only log if wait is infinite.<a name="line.1642"></a> -<span class="sourceLineNo">1643</span> if (now > lastLog + 10000) {<a name="line.1643"></a> -<span class="sourceLineNo">1644</span> lastLog = now;<a name="line.1644"></a> -<span class="sourceLineNo">1645</span> LOG.info("#" + id + ", waiting for " + currentInProgress + " actions to finish");<a name="line.1645"></a> +<span class="sourceLineNo">1433</span> sb.append(" on ").append(sn).append(", tracking started ").append(startTime);<a name="line.1433"></a> +<span class="sourceLineNo">1434</span><a name="line.1434"></a> +<span class="sourceLineNo">1435</span> if (willRetry) {<a name="line.1435"></a> +<span class="sourceLineNo">1436</span> sb.append(", retrying after=").append(backOffTime).append("ms").<a name="line.1436"></a> +<span class="sourceLineNo">1437</span> append(", replay=").append(replaySize).append("ops");<a name="line.1437"></a> +<span class="sourceLineNo">1438</span> } else if (failureCount > 0) {<a name="line.1438"></a> +<span class="sourceLineNo">1439</span> if (stopped > 0) {<a name="line.1439"></a> +<span class="sourceLineNo">1440</span> sb.append("; not retrying ").append(stopped).append(" due to success from other replica");<a name="line.1440"></a> +<span class="sourceLineNo">1441</span> }<a name="line.1441"></a> +<span class="sourceLineNo">1442</span> if (failed > 0) {<a name="line.1442"></a> +<span class="sourceLineNo">1443</span> sb.append("; not retrying ").append(failed).append(" - final failure");<a name="line.1443"></a> +<span class="sourceLineNo">1444</span> }<a name="line.1444"></a> +<span class="sourceLineNo">1445</span><a name="line.1445"></a> +<span class="sourceLineNo">1446</span> }<a name="line.1446"></a> +<span class="sourceLineNo">1447</span><a name="line.1447"></a> +<span class="sourceLineNo">1448</span> return sb.toString();<a name="line.1448"></a> +<span class="sourceLineNo">1449</span> }<a name="line.1449"></a> +<span class="sourceLineNo">1450</span><a name="line.1450"></a> +<span class="sourceLineNo">1451</span> /**<a name="line.1451"></a> +<span class="sourceLineNo">1452</span> * Sets the non-error result from a particular action.<a name="line.1452"></a> +<span class="sourceLineNo">1453</span> * @param action Action (request) that the server responded to.<a name="line.1453"></a> +<span class="sourceLineNo">1454</span> * @param result The result.<a name="line.1454"></a> +<span class="sourceLineNo">1455</span> */<a name="line.1455"></a> +<span class="sourceLineNo">1456</span> private void setResult(Action<Row> action, Object result) {<a name="line.1456"></a> +<span class="sourceLineNo">1457</span> if (result == null) {<a name="line.1457"></a> +<span class="sourceLineNo">1458</span> throw new RuntimeException("Result cannot be null");<a name="line.1458"></a> +<span class="sourceLineNo">1459</span> }<a name="line.1459"></a> +<span class="sourceLineNo">1460</span> ReplicaResultState state = null;<a name="line.1460"></a> +<span class="sourceLineNo">1461</span> boolean isStale = !RegionReplicaUtil.isDefaultReplica(action.getReplicaId());<a name="line.1461"></a> +<span class="sourceLineNo">1462</span> int index = action.getOriginalIndex();<a name="line.1462"></a> +<span class="sourceLineNo">1463</span> if (results == null) {<a name="line.1463"></a> +<span class="sourceLineNo">1464</span> decActionCounter(index);<a name="line.1464"></a> +<span class="sourceLineNo">1465</span> return; // Simple case, no replica requests.<a name="line.1465"></a> +<span class="sourceLineNo">1466</span> }<a name="line.1466"></a> +<span class="sourceLineNo">1467</span> state = trySetResultSimple(index, action.getAction(), false, result, null, isStale);<a name="line.1467"></a> +<span class="sourceLineNo">1468</span> if (state == null) {<a name="line.1468"></a> +<span class="sourceLineNo">1469</span> return; // Simple case, no replica requests.<a name="line.1469"></a> +<span class="sourceLineNo">1470</span> }<a name="line.1470"></a> +<span class="sourceLineNo">1471</span> // At this point we know that state is set to replica tracking class.<a name="line.1471"></a> +<span class="sourceLineNo">1472</span> // It could be that someone else is also looking at it; however, we know there can<a name="line.1472"></a> +<span class="sourceLineNo">1473</span> // only be one state object, and only one thread can set callCount to 0. Other threads<a name="line.1473"></a> +<span class="sourceLineNo">1474</span> // will either see state with callCount 0 after locking it; or will not see state at all<a name="line.1474"></a> +<span class="sourceLineNo">1475</span> // we will replace it with the result.<a name="line.1475"></a> +<span class="sourceLineNo">1476</span> synchronized (state) {<a name="line.1476"></a> +<span class="sourceLineNo">1477</span> if (state.callCount == 0) {<a name="line.1477"></a> +<span class="sourceLineNo">1478</span> return; // someone already set the result<a name="line.1478"></a> +<span class="sourceLineNo">1479</span> }<a name="line.1479"></a> +<span class="sourceLineNo">1480</span> state.callCount = 0;<a name="line.1480"></a> +<span class="sourceLineNo">1481</span> }<a name="line.1481"></a> +<span class="sourceLineNo">1482</span> synchronized (replicaResultLock) {<a name="line.1482"></a> +<span class="sourceLineNo">1483</span> if (results[index] != state) {<a name="line.1483"></a> +<span class="sourceLineNo">1484</span> throw new AssertionError("We set the callCount but someone else replaced the result");<a name="line.1484"></a> +<span class="sourceLineNo">1485</span> }<a name="line.1485"></a> +<span class="sourceLineNo">1486</span> results[index] = result;<a name="line.1486"></a> +<span class="sourceLineNo">1487</span> }<a name="line.1487"></a> +<span class="sourceLineNo">1488</span><a name="line.1488"></a> +<span class="sourceLineNo">1489</span> decActionCounter(index);<a name="line.1489"></a> +<span class="sourceLineNo">1490</span> }<a name="line.1490"></a> +<span class="sourceLineNo">1491</span><a name="line.1491"></a> +<span class="sourceLineNo">1492</span> /**<a name="line.1492"></a> +<span class="sourceLineNo">1493</span> * Sets the error from a particular action.<a name="line.1493"></a> +<span class="sourceLineNo">1494</span> * @param index Original action index.<a name="line.1494"></a> +<span class="sourceLineNo">1495</span> * @param row Original request.<a name="line.1495"></a> +<span class="sourceLineNo">1496</span> * @param throwable The resulting error.<a name="line.1496"></a> +<span class="sourceLineNo">1497</span> * @param server The source server.<a name="line.1497"></a> +<span class="sourceLineNo">1498</span> */<a name="line.1498"></a> +<span class="sourceLineNo">1499</span> private void setError(int index, Row row, Throwable throwable, ServerName server) {<a name="line.1499"></a> +<span class="sourceLineNo">1500</span> ReplicaResultState state = null;<a name="line.1500"></a> +<span class="sourceLineNo">1501</span> if (results == null) {<a name="line.1501"></a> +<span class="sourceLineNo">1502</span> // Note that we currently cannot have replica requests with null results. So it shouldn't<a name="line.1502"></a> +<span class="sourceLineNo">1503</span> // happen that multiple replica calls will call dAC for same actions with results == null.<a name="line.1503"></a> +<span class="sourceLineNo">1504</span> // Only one call per action should be present in this case.<a name="line.1504"></a> +<span class="sourceLineNo">1505</span> errors.add(throwable, row, server);<a name="line.1505"></a> +<span class="sourceLineNo">1506</span> decActionCounter(index);<a name="line.1506"></a> +<span class="sourceLineNo">1507</span> return; // Simple case, no replica requests.<a name="line.1507"></a> +<span class="sourceLineNo">1508</span> }<a name="line.1508"></a> +<span class="sourceLineNo">1509</span> state = trySetResultSimple(index, row, true, throwable, server, false);<a name="line.1509"></a> +<span class="sourceLineNo">1510</span> if (state == null) {<a name="line.1510"></a> +<span class="sourceLineNo">1511</span> return; // Simple case, no replica requests.<a name="line.1511"></a> +<span class="sourceLineNo">1512</span> }<a name="line.1512"></a> +<span class="sourceLineNo">1513</span> BatchErrors target = null; // Error will be added to final errors, or temp replica errors.<a name="line.1513"></a> +<span class="sourceLineNo">1514</span> boolean isActionDone = false;<a name="line.1514"></a> +<span class="sourceLineNo">1515</span> synchronized (state) {<a name="line.1515"></a> +<span class="sourceLineNo">1516</span> switch (state.callCount) {<a name="line.1516"></a> +<span class="sourceLineNo">1517</span> case 0: return; // someone already set the result<a name="line.1517"></a> +<span class="sourceLineNo">1518</span> case 1: { // All calls failed, we are the last error.<a name="line.1518"></a> +<span class="sourceLineNo">1519</span> target = errors;<a name="line.1519"></a> +<span class="sourceLineNo">1520</span> isActionDone = true;<a name="line.1520"></a> +<span class="sourceLineNo">1521</span> break;<a name="line.1521"></a> +<span class="sourceLineNo">1522</span> }<a name="line.1522"></a> +<span class="sourceLineNo">1523</span> default: {<a name="line.1523"></a> +<span class="sourceLineNo">1524</span> assert state.callCount > 1;<a name="line.1524"></a> +<span class="sourceLineNo">1525</span> if (state.replicaErrors == null) {<a name="line.1525"></a> +<span class="sourceLineNo">1526</span> state.replicaErrors = new BatchErrors();<a name="line.1526"></a> +<span class="sourceLineNo">1527</span> }<a name="line.1527"></a> +<span class="sourceLineNo">1528</span> target = state.replicaErrors;<a name="line.1528"></a> +<span class="sourceLineNo">1529</span> break;<a name="line.1529"></a> +<span class="sourceLineNo">1530</span> }<a name="line.1530"></a> +<span class="sourceLineNo">1531</span> }<a name="line.1531"></a> +<span class="sourceLineNo">1532</span> --state.callCount;<a name="line.1532"></a> +<span class="sourceLineNo">1533</span> }<a name="line.1533"></a> +<span class="sourceLineNo">1534</span> target.add(throwable, row, server);<a name="line.1534"></a> +<span class="sourceLineNo">1535</span> if (isActionDone) {<a name="line.1535"></a> +<span class="sourceLineNo">1536</span> if (state.replicaErrors != null) { // last call, no need to lock<a name="line.1536"></a> +<span class="sourceLineNo">1537</span> errors.merge(state.replicaErrors);<a name="line.1537"></a> +<span class="sourceLineNo">1538</span> }<a name="line.1538"></a> +<span class="sourceLineNo">1539</span> // See setResult for explanations.<a name="line.1539"></a> +<span class="sourceLineNo">1540</span> synchronized (replicaResultLock) {<a name="line.1540"></a> +<span class="sourceLineNo">1541</span> if (results[index] != state) {<a name="line.1541"></a> +<span class="sourceLineNo">1542</span> throw new AssertionError("We set the callCount but someone else replaced the result");<a name="line.1542"></a> +<span class="sourceLineNo">1543</span> }<a name="line.1543"></a> +<span class="sourceLineNo">1544</span> results[index] = throwable;<a name="line.1544"></a> +<span class="sourceLineNo">1545</span> }<a name="line.1545"></a> +<span class="sourceLineNo">1546</span> decActionCounter(index);<a name="line.1546"></a> +<span class="sourceLineNo">1547</span> }<a name="line.1547"></a> +<span class="sourceLineNo">1548</span> }<a name="line.1548"></a> +<span class="sourceLineNo">1549</span><a name="line.1549"></a> +<span class="sourceLineNo">1550</span> /**<a name="line.1550"></a> +<span class="sourceLineNo">1551</span> * Checks if the action is complete; used on error to prevent needless retries.<a name="line.1551"></a> +<span class="sourceLineNo">1552</span> * Does not synchronize, assuming element index/field accesses are atomic.<a name="line.1552"></a> +<span class="sourceLineNo">1553</span> * This is an opportunistic optimization check, doesn't have to be strict.<a name="line.1553"></a> +<span class="sourceLineNo">1554</span> * @param index Original action index.<a name="line.1554"></a> +<span class="sourceLineNo">1555</span> * @param row Original request.<a name="line.1555"></a> +<span class="sourceLineNo">1556</span> */<a name="line.1556"></a> +<span class="sourceLineNo">1557</span> private boolean isActionComplete(int index, Row row) {<a name="line.1557"></a> +<span class="sourceLineNo">1558</span> if (!isReplicaGet(row)) return false;<a name="line.1558"></a> +<span class="sourceLineNo">1559</span> Object resObj = results[index];<a name="line.1559"></a> +<span class="sourceLineNo">1560</span> return (resObj != null) && (!(resObj instanceof ReplicaResultState)<a name="line.1560"></a> +<span class="sourceLineNo">1561</span> || ((ReplicaResultState)resObj).callCount == 0);<a name="line.1561"></a> +<span class="sourceLineNo">1562</span> }<a name="line.1562"></a> +<span class="sourceLineNo">1563</span><a name="line.1563"></a> +<span class="sourceLineNo">1564</span> /**<a name="line.1564"></a> +<span class="sourceLineNo">1565</span> * Tries to set the result or error for a particular action as if there were no replica calls.<a name="line.1565"></a> +<span class="sourceLineNo">1566</span> * @return null if successful; replica state if there were in fact replica calls.<a name="line.1566"></a> +<span class="sourceLineNo">1567</span> */<a name="line.1567"></a> +<span class="sourceLineNo">1568</span> private ReplicaResultState trySetResultSimple(int index, Row row, boolean isError,<a name="line.1568"></a> +<span class="sourceLineNo">1569</span> Object result, ServerName server, boolean isFromReplica) {<a name="line.1569"></a> +<span class="sourceLineNo">1570</span> Object resObj = null;<a name="line.1570"></a> +<span class="sourceLineNo">1571</span> if (!isReplicaGet(row)) {<a name="line.1571"></a> +<span class="sourceLineNo">1572</span> if (isFromReplica) {<a name="line.1572"></a> +<span class="sourceLineNo">1573</span> throw new AssertionError("Unexpected stale result for " + row);<a name="line.1573"></a> +<span class="sourceLineNo">1574</span> }<a name="line.1574"></a> +<span class="sourceLineNo">1575</span> results[index] = result;<a name="line.1575"></a> +<span class="sourceLineNo">1576</span> } else {<a name="line.1576"></a> +<span class="sourceLineNo">1577</span> synchronized (replicaResultLock) {<a name="line.1577"></a> +<span class="sourceLineNo">1578</span> resObj = results[index];<a name="line.1578"></a> +<span class="sourceLineNo">1579</span> if (resObj == null) {<a name="line.1579"></a> +<span class="sourceLineNo">1580</span> if (isFromReplica) {<a name="line.1580"></a> +<span class="sourceLineNo">1581</span> throw new AssertionError("Unexpected stale result for " + row);<a name="line.1581"></a> +<span class="sourceLineNo">1582</span> }<a name="line.1582"></a> +<span class="sourceLineNo">1583</span> results[index] = result;<a name="line.1583"></a> +<span class="sourceLineNo">1584</span> }<a name="line.1584"></a> +<span class="sourceLineNo">1585</span> }<a name="line.1585"></a> +<span class="sourceLineNo">1586</span> }<a name="line.1586"></a> +<span class="sourceLineNo">1587</span><a name="line.1587"></a> +<span class="sourceLineNo">1588</span> ReplicaResultState rrs =<a name="line.1588"></a> +<span class="sourceLineNo">1589</span> (resObj instanceof Rep
<TRUNCATED>