http://git-wip-us.apache.org/repos/asf/hbase-site/blob/323b17d9/devapidocs/src-html/org/apache/hadoop/hbase/procedure2/ProcedureExecutor.WorkerMonitor.html ---------------------------------------------------------------------- diff --git a/devapidocs/src-html/org/apache/hadoop/hbase/procedure2/ProcedureExecutor.WorkerMonitor.html b/devapidocs/src-html/org/apache/hadoop/hbase/procedure2/ProcedureExecutor.WorkerMonitor.html index bdfc3f8..151ab8e 100644 --- a/devapidocs/src-html/org/apache/hadoop/hbase/procedure2/ProcedureExecutor.WorkerMonitor.html +++ b/devapidocs/src-html/org/apache/hadoop/hbase/procedure2/ProcedureExecutor.WorkerMonitor.html @@ -1062,1140 +1062,1159 @@ <span class="sourceLineNo">1054</span> }<a name="line.1054"></a> <span class="sourceLineNo">1055</span><a name="line.1055"></a> <span class="sourceLineNo">1056</span> boolean bypassProcedure(long pid, long lockWait, boolean force) throws IOException {<a name="line.1056"></a> -<span class="sourceLineNo">1057</span> Procedure<TEnvironment> procedure = getProcedure(pid);<a name="line.1057"></a> -<span class="sourceLineNo">1058</span> if (procedure == null) {<a name="line.1058"></a> -<span class="sourceLineNo">1059</span> LOG.debug("Procedure with id={} does not exist, skipping bypass", pid);<a name="line.1059"></a> -<span class="sourceLineNo">1060</span> return false;<a name="line.1060"></a> -<span class="sourceLineNo">1061</span> }<a name="line.1061"></a> -<span class="sourceLineNo">1062</span><a name="line.1062"></a> -<span class="sourceLineNo">1063</span> LOG.debug("Begin bypass {} with lockWait={}, force={}", procedure, lockWait, force);<a name="line.1063"></a> -<span class="sourceLineNo">1064</span> IdLock.Entry lockEntry = procExecutionLock.tryLockEntry(procedure.getProcId(), lockWait);<a name="line.1064"></a> -<span class="sourceLineNo">1065</span> if (lockEntry == null && !force) {<a name="line.1065"></a> -<span class="sourceLineNo">1066</span> LOG.debug("Waited {} ms, but {} is still running, skipping bypass with force={}",<a name="line.1066"></a> -<span class="sourceLineNo">1067</span> lockWait, procedure, force);<a name="line.1067"></a> -<span class="sourceLineNo">1068</span> return false;<a name="line.1068"></a> -<span class="sourceLineNo">1069</span> } else if (lockEntry == null) {<a name="line.1069"></a> -<span class="sourceLineNo">1070</span> LOG.debug("Waited {} ms, but {} is still running, begin bypass with force={}",<a name="line.1070"></a> -<span class="sourceLineNo">1071</span> lockWait, procedure, force);<a name="line.1071"></a> -<span class="sourceLineNo">1072</span> }<a name="line.1072"></a> -<span class="sourceLineNo">1073</span> try {<a name="line.1073"></a> -<span class="sourceLineNo">1074</span> // check whether the procedure is already finished<a name="line.1074"></a> -<span class="sourceLineNo">1075</span> if (procedure.isFinished()) {<a name="line.1075"></a> -<span class="sourceLineNo">1076</span> LOG.debug("{} is already finished, skipping bypass", procedure);<a name="line.1076"></a> -<span class="sourceLineNo">1077</span> return false;<a name="line.1077"></a> -<span class="sourceLineNo">1078</span> }<a name="line.1078"></a> -<span class="sourceLineNo">1079</span><a name="line.1079"></a> -<span class="sourceLineNo">1080</span> if (procedure.hasChildren()) {<a name="line.1080"></a> -<span class="sourceLineNo">1081</span> LOG.debug("{} has children, skipping bypass", procedure);<a name="line.1081"></a> -<span class="sourceLineNo">1082</span> return false;<a name="line.1082"></a> -<span class="sourceLineNo">1083</span> }<a name="line.1083"></a> -<span class="sourceLineNo">1084</span><a name="line.1084"></a> -<span class="sourceLineNo">1085</span> // If the procedure has no parent or no child, we are safe to bypass it in whatever state<a name="line.1085"></a> -<span class="sourceLineNo">1086</span> if (procedure.hasParent() && procedure.getState() != ProcedureState.RUNNABLE<a name="line.1086"></a> -<span class="sourceLineNo">1087</span> && procedure.getState() != ProcedureState.WAITING<a name="line.1087"></a> -<span class="sourceLineNo">1088</span> && procedure.getState() != ProcedureState.WAITING_TIMEOUT) {<a name="line.1088"></a> -<span class="sourceLineNo">1089</span> LOG.debug("Bypassing procedures in RUNNABLE, WAITING and WAITING_TIMEOUT states "<a name="line.1089"></a> -<span class="sourceLineNo">1090</span> + "(with no parent), {}",<a name="line.1090"></a> -<span class="sourceLineNo">1091</span> procedure);<a name="line.1091"></a> -<span class="sourceLineNo">1092</span> return false;<a name="line.1092"></a> -<span class="sourceLineNo">1093</span> }<a name="line.1093"></a> -<span class="sourceLineNo">1094</span><a name="line.1094"></a> -<span class="sourceLineNo">1095</span> // Now, the procedure is not finished, and no one can execute it since we take the lock now<a name="line.1095"></a> -<span class="sourceLineNo">1096</span> // And we can be sure that its ancestor is not running too, since their child has not<a name="line.1096"></a> -<span class="sourceLineNo">1097</span> // finished yet<a name="line.1097"></a> -<span class="sourceLineNo">1098</span> Procedure<TEnvironment> current = procedure;<a name="line.1098"></a> -<span class="sourceLineNo">1099</span> while (current != null) {<a name="line.1099"></a> -<span class="sourceLineNo">1100</span> LOG.debug("Bypassing {}", current);<a name="line.1100"></a> -<span class="sourceLineNo">1101</span> current.bypass();<a name="line.1101"></a> -<span class="sourceLineNo">1102</span> store.update(procedure);<a name="line.1102"></a> -<span class="sourceLineNo">1103</span> long parentID = current.getParentProcId();<a name="line.1103"></a> -<span class="sourceLineNo">1104</span> current = getProcedure(parentID);<a name="line.1104"></a> -<span class="sourceLineNo">1105</span> }<a name="line.1105"></a> -<span class="sourceLineNo">1106</span><a name="line.1106"></a> -<span class="sourceLineNo">1107</span> //wake up waiting procedure, already checked there is no child<a name="line.1107"></a> -<span class="sourceLineNo">1108</span> if (procedure.getState() == ProcedureState.WAITING) {<a name="line.1108"></a> -<span class="sourceLineNo">1109</span> procedure.setState(ProcedureState.RUNNABLE);<a name="line.1109"></a> -<span class="sourceLineNo">1110</span> store.update(procedure);<a name="line.1110"></a> -<span class="sourceLineNo">1111</span> }<a name="line.1111"></a> -<span class="sourceLineNo">1112</span><a name="line.1112"></a> -<span class="sourceLineNo">1113</span> // If we don't have the lock, we can't re-submit the queue,<a name="line.1113"></a> -<span class="sourceLineNo">1114</span> // since it is already executing. To get rid of the stuck situation, we<a name="line.1114"></a> -<span class="sourceLineNo">1115</span> // need to restart the master. With the procedure set to bypass, the procedureExecutor<a name="line.1115"></a> -<span class="sourceLineNo">1116</span> // will bypass it and won't get stuck again.<a name="line.1116"></a> -<span class="sourceLineNo">1117</span> if (lockEntry != null) {<a name="line.1117"></a> -<span class="sourceLineNo">1118</span> // add the procedure to run queue,<a name="line.1118"></a> -<span class="sourceLineNo">1119</span> scheduler.addFront(procedure);<a name="line.1119"></a> -<span class="sourceLineNo">1120</span> LOG.debug("Bypassing {} and its ancestors successfully, adding to queue", procedure);<a name="line.1120"></a> -<span class="sourceLineNo">1121</span> } else {<a name="line.1121"></a> -<span class="sourceLineNo">1122</span> LOG.debug("Bypassing {} and its ancestors successfully, but since it is already running, "<a name="line.1122"></a> -<span class="sourceLineNo">1123</span> + "skipping add to queue", procedure);<a name="line.1123"></a> -<span class="sourceLineNo">1124</span> }<a name="line.1124"></a> -<span class="sourceLineNo">1125</span> return true;<a name="line.1125"></a> -<span class="sourceLineNo">1126</span><a name="line.1126"></a> -<span class="sourceLineNo">1127</span> } finally {<a name="line.1127"></a> -<span class="sourceLineNo">1128</span> if (lockEntry != null) {<a name="line.1128"></a> -<span class="sourceLineNo">1129</span> procExecutionLock.releaseLockEntry(lockEntry);<a name="line.1129"></a> -<span class="sourceLineNo">1130</span> }<a name="line.1130"></a> -<span class="sourceLineNo">1131</span> }<a name="line.1131"></a> -<span class="sourceLineNo">1132</span> }<a name="line.1132"></a> -<span class="sourceLineNo">1133</span><a name="line.1133"></a> -<span class="sourceLineNo">1134</span> /**<a name="line.1134"></a> -<span class="sourceLineNo">1135</span> * Add a new root-procedure to the executor.<a name="line.1135"></a> -<span class="sourceLineNo">1136</span> * @param proc the new procedure to execute.<a name="line.1136"></a> -<span class="sourceLineNo">1137</span> * @param nonceKey the registered unique identifier for this operation from the client or process.<a name="line.1137"></a> -<span class="sourceLineNo">1138</span> * @return the procedure id, that can be used to monitor the operation<a name="line.1138"></a> -<span class="sourceLineNo">1139</span> */<a name="line.1139"></a> -<span class="sourceLineNo">1140</span> @edu.umd.cs.findbugs.annotations.SuppressWarnings(value="NP_NULL_ON_SOME_PATH",<a name="line.1140"></a> -<span class="sourceLineNo">1141</span> justification = "FindBugs is blind to the check-for-null")<a name="line.1141"></a> -<span class="sourceLineNo">1142</span> public long submitProcedure(Procedure<TEnvironment> proc, NonceKey nonceKey) {<a name="line.1142"></a> -<span class="sourceLineNo">1143</span> Preconditions.checkArgument(lastProcId.get() >= 0);<a name="line.1143"></a> -<span class="sourceLineNo">1144</span><a name="line.1144"></a> -<span class="sourceLineNo">1145</span> prepareProcedure(proc);<a name="line.1145"></a> -<span class="sourceLineNo">1146</span><a name="line.1146"></a> -<span class="sourceLineNo">1147</span> final Long currentProcId;<a name="line.1147"></a> -<span class="sourceLineNo">1148</span> if (nonceKey != null) {<a name="line.1148"></a> -<span class="sourceLineNo">1149</span> currentProcId = nonceKeysToProcIdsMap.get(nonceKey);<a name="line.1149"></a> -<span class="sourceLineNo">1150</span> Preconditions.checkArgument(currentProcId != null,<a name="line.1150"></a> -<span class="sourceLineNo">1151</span> "Expected nonceKey=" + nonceKey + " to be reserved, use registerNonce(); proc=" + proc);<a name="line.1151"></a> -<span class="sourceLineNo">1152</span> } else {<a name="line.1152"></a> -<span class="sourceLineNo">1153</span> currentProcId = nextProcId();<a name="line.1153"></a> -<span class="sourceLineNo">1154</span> }<a name="line.1154"></a> -<span class="sourceLineNo">1155</span><a name="line.1155"></a> -<span class="sourceLineNo">1156</span> // Initialize the procedure<a name="line.1156"></a> -<span class="sourceLineNo">1157</span> proc.setNonceKey(nonceKey);<a name="line.1157"></a> -<span class="sourceLineNo">1158</span> proc.setProcId(currentProcId.longValue());<a name="line.1158"></a> -<span class="sourceLineNo">1159</span><a name="line.1159"></a> -<span class="sourceLineNo">1160</span> // Commit the transaction<a name="line.1160"></a> -<span class="sourceLineNo">1161</span> store.insert(proc, null);<a name="line.1161"></a> -<span class="sourceLineNo">1162</span> LOG.debug("Stored {}", proc);<a name="line.1162"></a> -<span class="sourceLineNo">1163</span><a name="line.1163"></a> -<span class="sourceLineNo">1164</span> // Add the procedure to the executor<a name="line.1164"></a> -<span class="sourceLineNo">1165</span> return pushProcedure(proc);<a name="line.1165"></a> -<span class="sourceLineNo">1166</span> }<a name="line.1166"></a> -<span class="sourceLineNo">1167</span><a name="line.1167"></a> -<span class="sourceLineNo">1168</span> /**<a name="line.1168"></a> -<span class="sourceLineNo">1169</span> * Add a set of new root-procedure to the executor.<a name="line.1169"></a> -<span class="sourceLineNo">1170</span> * @param procs the new procedures to execute.<a name="line.1170"></a> -<span class="sourceLineNo">1171</span> */<a name="line.1171"></a> -<span class="sourceLineNo">1172</span> // TODO: Do we need to take nonces here?<a name="line.1172"></a> -<span class="sourceLineNo">1173</span> public void submitProcedures(Procedure<TEnvironment>[] procs) {<a name="line.1173"></a> -<span class="sourceLineNo">1174</span> Preconditions.checkArgument(lastProcId.get() >= 0);<a name="line.1174"></a> -<span class="sourceLineNo">1175</span> if (procs == null || procs.length <= 0) {<a name="line.1175"></a> -<span class="sourceLineNo">1176</span> return;<a name="line.1176"></a> -<span class="sourceLineNo">1177</span> }<a name="line.1177"></a> -<span class="sourceLineNo">1178</span><a name="line.1178"></a> -<span class="sourceLineNo">1179</span> // Prepare procedure<a name="line.1179"></a> -<span class="sourceLineNo">1180</span> for (int i = 0; i < procs.length; ++i) {<a name="line.1180"></a> -<span class="sourceLineNo">1181</span> prepareProcedure(procs[i]).setProcId(nextProcId());<a name="line.1181"></a> -<span class="sourceLineNo">1182</span> }<a name="line.1182"></a> -<span class="sourceLineNo">1183</span><a name="line.1183"></a> -<span class="sourceLineNo">1184</span> // Commit the transaction<a name="line.1184"></a> -<span class="sourceLineNo">1185</span> store.insert(procs);<a name="line.1185"></a> -<span class="sourceLineNo">1186</span> if (LOG.isDebugEnabled()) {<a name="line.1186"></a> -<span class="sourceLineNo">1187</span> LOG.debug("Stored " + Arrays.toString(procs));<a name="line.1187"></a> -<span class="sourceLineNo">1188</span> }<a name="line.1188"></a> -<span class="sourceLineNo">1189</span><a name="line.1189"></a> -<span class="sourceLineNo">1190</span> // Add the procedure to the executor<a name="line.1190"></a> -<span class="sourceLineNo">1191</span> for (int i = 0; i < procs.length; ++i) {<a name="line.1191"></a> -<span class="sourceLineNo">1192</span> pushProcedure(procs[i]);<a name="line.1192"></a> -<span class="sourceLineNo">1193</span> }<a name="line.1193"></a> -<span class="sourceLineNo">1194</span> }<a name="line.1194"></a> -<span class="sourceLineNo">1195</span><a name="line.1195"></a> -<span class="sourceLineNo">1196</span> private Procedure<TEnvironment> prepareProcedure(Procedure<TEnvironment> proc) {<a name="line.1196"></a> -<span class="sourceLineNo">1197</span> Preconditions.checkArgument(proc.getState() == ProcedureState.INITIALIZING);<a name="line.1197"></a> -<span class="sourceLineNo">1198</span> Preconditions.checkArgument(!proc.hasParent(), "unexpected parent", proc);<a name="line.1198"></a> -<span class="sourceLineNo">1199</span> if (this.checkOwnerSet) {<a name="line.1199"></a> -<span class="sourceLineNo">1200</span> Preconditions.checkArgument(proc.hasOwner(), "missing owner");<a name="line.1200"></a> -<span class="sourceLineNo">1201</span> }<a name="line.1201"></a> -<span class="sourceLineNo">1202</span> return proc;<a name="line.1202"></a> -<span class="sourceLineNo">1203</span> }<a name="line.1203"></a> -<span class="sourceLineNo">1204</span><a name="line.1204"></a> -<span class="sourceLineNo">1205</span> private long pushProcedure(Procedure<TEnvironment> proc) {<a name="line.1205"></a> -<span class="sourceLineNo">1206</span> final long currentProcId = proc.getProcId();<a name="line.1206"></a> -<span class="sourceLineNo">1207</span><a name="line.1207"></a> -<span class="sourceLineNo">1208</span> // Update metrics on start of a procedure<a name="line.1208"></a> -<span class="sourceLineNo">1209</span> proc.updateMetricsOnSubmit(getEnvironment());<a name="line.1209"></a> -<span class="sourceLineNo">1210</span><a name="line.1210"></a> -<span class="sourceLineNo">1211</span> // Create the rollback stack for the procedure<a name="line.1211"></a> -<span class="sourceLineNo">1212</span> RootProcedureState<TEnvironment> stack = new RootProcedureState<>();<a name="line.1212"></a> -<span class="sourceLineNo">1213</span> rollbackStack.put(currentProcId, stack);<a name="line.1213"></a> -<span class="sourceLineNo">1214</span><a name="line.1214"></a> -<span class="sourceLineNo">1215</span> // Submit the new subprocedures<a name="line.1215"></a> -<span class="sourceLineNo">1216</span> assert !procedures.containsKey(currentProcId);<a name="line.1216"></a> -<span class="sourceLineNo">1217</span> procedures.put(currentProcId, proc);<a name="line.1217"></a> -<span class="sourceLineNo">1218</span> sendProcedureAddedNotification(currentProcId);<a name="line.1218"></a> -<span class="sourceLineNo">1219</span> scheduler.addBack(proc);<a name="line.1219"></a> -<span class="sourceLineNo">1220</span> return proc.getProcId();<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> * Send an abort notification the specified procedure.<a name="line.1224"></a> -<span class="sourceLineNo">1225</span> * Depending on the procedure implementation the abort can be considered or ignored.<a name="line.1225"></a> -<span class="sourceLineNo">1226</span> * @param procId the procedure to abort<a name="line.1226"></a> -<span class="sourceLineNo">1227</span> * @return true if the procedure exists and has received the abort, otherwise false.<a name="line.1227"></a> -<span class="sourceLineNo">1228</span> */<a name="line.1228"></a> -<span class="sourceLineNo">1229</span> public boolean abort(long procId) {<a name="line.1229"></a> -<span class="sourceLineNo">1230</span> return abort(procId, true);<a name="line.1230"></a> -<span class="sourceLineNo">1231</span> }<a name="line.1231"></a> -<span class="sourceLineNo">1232</span><a name="line.1232"></a> -<span class="sourceLineNo">1233</span> /**<a name="line.1233"></a> -<span class="sourceLineNo">1234</span> * Send an abort notification to the specified procedure.<a name="line.1234"></a> -<span class="sourceLineNo">1235</span> * Depending on the procedure implementation, the abort can be considered or ignored.<a name="line.1235"></a> -<span class="sourceLineNo">1236</span> * @param procId the procedure to abort<a name="line.1236"></a> -<span class="sourceLineNo">1237</span> * @param mayInterruptIfRunning if the proc completed at least one step, should it be aborted?<a name="line.1237"></a> -<span class="sourceLineNo">1238</span> * @return true if the procedure exists and has received the abort, otherwise false.<a name="line.1238"></a> -<span class="sourceLineNo">1239</span> */<a name="line.1239"></a> -<span class="sourceLineNo">1240</span> public boolean abort(long procId, boolean mayInterruptIfRunning) {<a name="line.1240"></a> -<span class="sourceLineNo">1241</span> Procedure<TEnvironment> proc = procedures.get(procId);<a name="line.1241"></a> -<span class="sourceLineNo">1242</span> if (proc != null) {<a name="line.1242"></a> -<span class="sourceLineNo">1243</span> if (!mayInterruptIfRunning && proc.wasExecuted()) {<a name="line.1243"></a> -<span class="sourceLineNo">1244</span> return false;<a name="line.1244"></a> -<span class="sourceLineNo">1245</span> }<a name="line.1245"></a> -<span class="sourceLineNo">1246</span> return proc.abort(getEnvironment());<a name="line.1246"></a> -<span class="sourceLineNo">1247</span> }<a name="line.1247"></a> -<span class="sourceLineNo">1248</span> return false;<a name="line.1248"></a> -<span class="sourceLineNo">1249</span> }<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> // Executor query helpers<a name="line.1252"></a> -<span class="sourceLineNo">1253</span> // ==========================================================================<a name="line.1253"></a> -<span class="sourceLineNo">1254</span> public Procedure<TEnvironment> getProcedure(final long procId) {<a name="line.1254"></a> -<span class="sourceLineNo">1255</span> return procedures.get(procId);<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> public <T extends Procedure<TEnvironment>> T getProcedure(Class<T> clazz, long procId) {<a name="line.1258"></a> -<span class="sourceLineNo">1259</span> Procedure<TEnvironment> proc = getProcedure(procId);<a name="line.1259"></a> -<span class="sourceLineNo">1260</span> if (clazz.isInstance(proc)) {<a name="line.1260"></a> -<span class="sourceLineNo">1261</span> return clazz.cast(proc);<a name="line.1261"></a> -<span class="sourceLineNo">1262</span> }<a name="line.1262"></a> -<span class="sourceLineNo">1263</span> return null;<a name="line.1263"></a> -<span class="sourceLineNo">1264</span> }<a name="line.1264"></a> -<span class="sourceLineNo">1265</span><a name="line.1265"></a> -<span class="sourceLineNo">1266</span> public Procedure<TEnvironment> getResult(long procId) {<a name="line.1266"></a> -<span class="sourceLineNo">1267</span> CompletedProcedureRetainer<TEnvironment> retainer = completed.get(procId);<a name="line.1267"></a> -<span class="sourceLineNo">1268</span> if (retainer == null) {<a name="line.1268"></a> -<span class="sourceLineNo">1269</span> return null;<a name="line.1269"></a> -<span class="sourceLineNo">1270</span> } else {<a name="line.1270"></a> -<span class="sourceLineNo">1271</span> return retainer.getProcedure();<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> * Return true if the procedure is finished.<a name="line.1276"></a> -<span class="sourceLineNo">1277</span> * The state may be "completed successfully" or "failed and rolledback".<a name="line.1277"></a> -<span class="sourceLineNo">1278</span> * Use getResult() to check the state or get the result data.<a name="line.1278"></a> -<span class="sourceLineNo">1279</span> * @param procId the ID of the procedure to check<a name="line.1279"></a> -<span class="sourceLineNo">1280</span> * @return true if the procedure execution is finished, otherwise false.<a name="line.1280"></a> -<span class="sourceLineNo">1281</span> */<a name="line.1281"></a> -<span class="sourceLineNo">1282</span> public boolean isFinished(final long procId) {<a name="line.1282"></a> -<span class="sourceLineNo">1283</span> return !procedures.containsKey(procId);<a name="line.1283"></a> -<span class="sourceLineNo">1284</span> }<a name="line.1284"></a> -<span class="sourceLineNo">1285</span><a name="line.1285"></a> -<span class="sourceLineNo">1286</span> /**<a name="line.1286"></a> -<span class="sourceLineNo">1287</span> * Return true if the procedure is started.<a name="line.1287"></a> -<span class="sourceLineNo">1288</span> * @param procId the ID of the procedure to check<a name="line.1288"></a> -<span class="sourceLineNo">1289</span> * @return true if the procedure execution is started, otherwise false.<a name="line.1289"></a> -<span class="sourceLineNo">1290</span> */<a name="line.1290"></a> -<span class="sourceLineNo">1291</span> public boolean isStarted(long procId) {<a name="line.1291"></a> -<span class="sourceLineNo">1292</span> Procedure<?> proc = procedures.get(procId);<a name="line.1292"></a> -<span class="sourceLineNo">1293</span> if (proc == null) {<a name="line.1293"></a> -<span class="sourceLineNo">1294</span> return completed.get(procId) != null;<a name="line.1294"></a> -<span class="sourceLineNo">1295</span> }<a name="line.1295"></a> -<span class="sourceLineNo">1296</span> return proc.wasExecuted();<a name="line.1296"></a> -<span class="sourceLineNo">1297</span> }<a name="line.1297"></a> -<span class="sourceLineNo">1298</span><a name="line.1298"></a> -<span class="sourceLineNo">1299</span> /**<a name="line.1299"></a> -<span class="sourceLineNo">1300</span> * Mark the specified completed procedure, as ready to remove.<a name="line.1300"></a> -<span class="sourceLineNo">1301</span> * @param procId the ID of the procedure to remove<a name="line.1301"></a> -<span class="sourceLineNo">1302</span> */<a name="line.1302"></a> -<span class="sourceLineNo">1303</span> public void removeResult(long procId) {<a name="line.1303"></a> -<span class="sourceLineNo">1304</span> CompletedProcedureRetainer<TEnvironment> retainer = completed.get(procId);<a name="line.1304"></a> -<span class="sourceLineNo">1305</span> if (retainer == null) {<a name="line.1305"></a> -<span class="sourceLineNo">1306</span> assert !procedures.containsKey(procId) : "pid=" + procId + " is still running";<a name="line.1306"></a> -<span class="sourceLineNo">1307</span> LOG.debug("pid={} already removed by the cleaner.", procId);<a name="line.1307"></a> -<span class="sourceLineNo">1308</span> return;<a name="line.1308"></a> -<span class="sourceLineNo">1309</span> }<a name="line.1309"></a> -<span class="sourceLineNo">1310</span><a name="line.1310"></a> -<span class="sourceLineNo">1311</span> // The CompletedProcedureCleaner will take care of deletion, once the TTL is expired.<a name="line.1311"></a> -<span class="sourceLineNo">1312</span> retainer.setClientAckTime(EnvironmentEdgeManager.currentTime());<a name="line.1312"></a> -<span class="sourceLineNo">1313</span> }<a name="line.1313"></a> -<span class="sourceLineNo">1314</span><a name="line.1314"></a> -<span class="sourceLineNo">1315</span> public Procedure<TEnvironment> getResultOrProcedure(long procId) {<a name="line.1315"></a> -<span class="sourceLineNo">1316</span> CompletedProcedureRetainer<TEnvironment> retainer = completed.get(procId);<a name="line.1316"></a> -<span class="sourceLineNo">1317</span> if (retainer == null) {<a name="line.1317"></a> -<span class="sourceLineNo">1318</span> return procedures.get(procId);<a name="line.1318"></a> -<span class="sourceLineNo">1319</span> } else {<a name="line.1319"></a> -<span class="sourceLineNo">1320</span> return retainer.getProcedure();<a name="line.1320"></a> -<span class="sourceLineNo">1321</span> }<a name="line.1321"></a> -<span class="sourceLineNo">1322</span> }<a name="line.1322"></a> -<span class="sourceLineNo">1323</span><a name="line.1323"></a> -<span class="sourceLineNo">1324</span> /**<a name="line.1324"></a> -<span class="sourceLineNo">1325</span> * Check if the user is this procedure's owner<a name="line.1325"></a> -<span class="sourceLineNo">1326</span> * @param procId the target procedure<a name="line.1326"></a> -<span class="sourceLineNo">1327</span> * @param user the user<a name="line.1327"></a> -<span class="sourceLineNo">1328</span> * @return true if the user is the owner of the procedure,<a name="line.1328"></a> -<span class="sourceLineNo">1329</span> * false otherwise or the owner is unknown.<a name="line.1329"></a> -<span class="sourceLineNo">1330</span> */<a name="line.1330"></a> -<span class="sourceLineNo">1331</span> public boolean isProcedureOwner(long procId, User user) {<a name="line.1331"></a> -<span class="sourceLineNo">1332</span> if (user == null) {<a name="line.1332"></a> -<span class="sourceLineNo">1333</span> return false;<a name="line.1333"></a> -<span class="sourceLineNo">1334</span> }<a name="line.1334"></a> -<span class="sourceLineNo">1335</span> final Procedure<TEnvironment> runningProc = procedures.get(procId);<a name="line.1335"></a> -<span class="sourceLineNo">1336</span> if (runningProc != null) {<a name="line.1336"></a> -<span class="sourceLineNo">1337</span> return runningProc.getOwner().equals(user.getShortName());<a name="line.1337"></a> -<span class="sourceLineNo">1338</span> }<a name="line.1338"></a> -<span class="sourceLineNo">1339</span><a name="line.1339"></a> -<span class="sourceLineNo">1340</span> final CompletedProcedureRetainer<TEnvironment> retainer = completed.get(procId);<a name="line.1340"></a> -<span class="sourceLineNo">1341</span> if (retainer != null) {<a name="line.1341"></a> -<span class="sourceLineNo">1342</span> return retainer.getProcedure().getOwner().equals(user.getShortName());<a name="line.1342"></a> -<span class="sourceLineNo">1343</span> }<a name="line.1343"></a> -<span class="sourceLineNo">1344</span><a name="line.1344"></a> -<span class="sourceLineNo">1345</span> // Procedure either does not exist or has already completed and got cleaned up.<a name="line.1345"></a> -<span class="sourceLineNo">1346</span> // At this time, we cannot check the owner of the procedure<a name="line.1346"></a> -<span class="sourceLineNo">1347</span> return false;<a name="line.1347"></a> -<span class="sourceLineNo">1348</span> }<a name="line.1348"></a> -<span class="sourceLineNo">1349</span><a name="line.1349"></a> -<span class="sourceLineNo">1350</span> /**<a name="line.1350"></a> -<span class="sourceLineNo">1351</span> * Should only be used when starting up, where the procedure workers have not been started.<a name="line.1351"></a> -<span class="sourceLineNo">1352</span> * <p/><a name="line.1352"></a> -<span class="sourceLineNo">1353</span> * If the procedure works has been started, the return values maybe changed when you are<a name="line.1353"></a> -<span class="sourceLineNo">1354</span> * processing it so usually this is not safe. Use {@link #getProcedures()} below for most cases as<a name="line.1354"></a> -<span class="sourceLineNo">1355</span> * it will do a copy, and also include the finished procedures.<a name="line.1355"></a> -<span class="sourceLineNo">1356</span> */<a name="line.1356"></a> -<span class="sourceLineNo">1357</span> public Collection<Procedure<TEnvironment>> getActiveProceduresNoCopy() {<a name="line.1357"></a> -<span class="sourceLineNo">1358</span> return procedures.values();<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> /**<a name="line.1361"></a> -<span class="sourceLineNo">1362</span> * Get procedures.<a name="line.1362"></a> -<span class="sourceLineNo">1363</span> * @return the procedures in a list<a name="line.1363"></a> -<span class="sourceLineNo">1364</span> */<a name="line.1364"></a> -<span class="sourceLineNo">1365</span> public List<Procedure<TEnvironment>> getProcedures() {<a name="line.1365"></a> -<span class="sourceLineNo">1366</span> List<Procedure<TEnvironment>> procedureList =<a name="line.1366"></a> -<span class="sourceLineNo">1367</span> new ArrayList<>(procedures.size() + completed.size());<a name="line.1367"></a> -<span class="sourceLineNo">1368</span> procedureList.addAll(procedures.values());<a name="line.1368"></a> -<span class="sourceLineNo">1369</span> // Note: The procedure could show up twice in the list with different state, as<a name="line.1369"></a> -<span class="sourceLineNo">1370</span> // it could complete after we walk through procedures list and insert into<a name="line.1370"></a> -<span class="sourceLineNo">1371</span> // procedureList - it is ok, as we will use the information in the Procedure<a name="line.1371"></a> -<span class="sourceLineNo">1372</span> // to figure it out; to prevent this would increase the complexity of the logic.<a name="line.1372"></a> -<span class="sourceLineNo">1373</span> completed.values().stream().map(CompletedProcedureRetainer::getProcedure)<a name="line.1373"></a> -<span class="sourceLineNo">1374</span> .forEach(procedureList::add);<a name="line.1374"></a> -<span class="sourceLineNo">1375</span> return procedureList;<a name="line.1375"></a> -<span class="sourceLineNo">1376</span> }<a name="line.1376"></a> -<span class="sourceLineNo">1377</span><a name="line.1377"></a> -<span class="sourceLineNo">1378</span> // ==========================================================================<a name="line.1378"></a> -<span class="sourceLineNo">1379</span> // Listeners helpers<a name="line.1379"></a> -<span class="sourceLineNo">1380</span> // ==========================================================================<a name="line.1380"></a> -<span class="sourceLineNo">1381</span> public void registerListener(ProcedureExecutorListener listener) {<a name="line.1381"></a> -<span class="sourceLineNo">1382</span> this.listeners.add(listener);<a name="line.1382"></a> -<span class="sourceLineNo">1383</span> }<a name="line.1383"></a> -<span class="sourceLineNo">1384</span><a name="line.1384"></a> -<span class="sourceLineNo">1385</span> public boolean unregisterListener(ProcedureExecutorListener listener) {<a name="line.1385"></a> -<span class="sourceLineNo">1386</span> return this.listeners.remove(listener);<a name="line.1386"></a> -<span class="sourceLineNo">1387</span> }<a name="line.1387"></a> -<span class="sourceLineNo">1388</span><a name="line.1388"></a> -<span class="sourceLineNo">1389</span> private void sendProcedureLoadedNotification(final long procId) {<a name="line.1389"></a> -<span class="sourceLineNo">1390</span> if (!this.listeners.isEmpty()) {<a name="line.1390"></a> -<span class="sourceLineNo">1391</span> for (ProcedureExecutorListener listener: this.listeners) {<a name="line.1391"></a> -<span class="sourceLineNo">1392</span> try {<a name="line.1392"></a> -<span class="sourceLineNo">1393</span> listener.procedureLoaded(procId);<a name="line.1393"></a> -<span class="sourceLineNo">1394</span> } catch (Throwable e) {<a name="line.1394"></a> -<span class="sourceLineNo">1395</span> LOG.error("Listener " + listener + " had an error: " + e.getMessage(), e);<a name="line.1395"></a> -<span class="sourceLineNo">1396</span> }<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><a name="line.1400"></a> -<span class="sourceLineNo">1401</span> private void sendProcedureAddedNotification(final long procId) {<a name="line.1401"></a> -<span class="sourceLineNo">1402</span> if (!this.listeners.isEmpty()) {<a name="line.1402"></a> -<span class="sourceLineNo">1403</span> for (ProcedureExecutorListener listener: this.listeners) {<a name="line.1403"></a> -<span class="sourceLineNo">1404</span> try {<a name="line.1404"></a> -<span class="sourceLineNo">1405</span> listener.procedureAdded(procId);<a name="line.1405"></a> -<span class="sourceLineNo">1406</span> } catch (Throwable e) {<a name="line.1406"></a> -<span class="sourceLineNo">1407</span> LOG.error("Listener " + listener + " had an error: " + e.getMessage(), e);<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> }<a name="line.1411"></a> -<span class="sourceLineNo">1412</span><a name="line.1412"></a> -<span class="sourceLineNo">1413</span> private void sendProcedureFinishedNotification(final long procId) {<a name="line.1413"></a> -<span class="sourceLineNo">1414</span> if (!this.listeners.isEmpty()) {<a name="line.1414"></a> -<span class="sourceLineNo">1415</span> for (ProcedureExecutorListener listener: this.listeners) {<a name="line.1415"></a> -<span class="sourceLineNo">1416</span> try {<a name="line.1416"></a> -<span class="sourceLineNo">1417</span> listener.procedureFinished(procId);<a name="line.1417"></a> -<span class="sourceLineNo">1418</span> } catch (Throwable e) {<a name="line.1418"></a> -<span class="sourceLineNo">1419</span> LOG.error("Listener " + listener + " had an error: " + e.getMessage(), e);<a name="line.1419"></a> -<span class="sourceLineNo">1420</span> }<a name="line.1420"></a> -<span class="sourceLineNo">1421</span> }<a name="line.1421"></a> -<span class="sourceLineNo">1422</span> }<a name="line.1422"></a> -<span class="sourceLineNo">1423</span> }<a name="line.1423"></a> -<span class="sourceLineNo">1424</span><a name="line.1424"></a> -<span class="sourceLineNo">1425</span> // ==========================================================================<a name="line.1425"></a> -<span class="sourceLineNo">1426</span> // Procedure IDs helpers<a name="line.1426"></a> -<span class="sourceLineNo">1427</span> // ==========================================================================<a name="line.1427"></a> -<span class="sourceLineNo">1428</span> private long nextProcId() {<a name="line.1428"></a> -<span class="sourceLineNo">1429</span> long procId = lastProcId.incrementAndGet();<a name="line.1429"></a> -<span class="sourceLineNo">1430</span> if (procId < 0) {<a name="line.1430"></a> -<span class="sourceLineNo">1431</span> while (!lastProcId.compareAndSet(procId, 0)) {<a name="line.1431"></a> -<span class="sourceLineNo">1432</span> procId = lastProcId.get();<a name="line.1432"></a> -<span class="sourceLineNo">1433</span> if (procId >= 0)<a name="line.1433"></a> -<span class="sourceLineNo">1434</span> break;<a name="line.1434"></a> -<span class="sourceLineNo">1435</span> }<a name="line.1435"></a> -<span class="sourceLineNo">1436</span> while (procedures.containsKey(procId)) {<a name="line.1436"></a> -<span class="sourceLineNo">1437</span> procId = lastProcId.incrementAndGet();<a name="line.1437"></a> -<span class="sourceLineNo">1438</span> }<a name="line.1438"></a> -<span class="sourceLineNo">1439</span> }<a name="line.1439"></a> -<span class="sourceLineNo">1440</span> assert procId >= 0 : "Invalid procId " + procId;<a name="line.1440"></a> -<span class="sourceLineNo">1441</span> return procId;<a name="line.1441"></a> -<span class="sourceLineNo">1442</span> }<a name="line.1442"></a> -<span class="sourceLineNo">1443</span><a name="line.1443"></a> -<span class="sourceLineNo">1444</span> @VisibleForTesting<a name="line.1444"></a> -<span class="sourceLineNo">1445</span> protected long getLastProcId() {<a name="line.1445"></a> -<span class="sourceLineNo">1446</span> return lastProcId.get();<a name="line.1446"></a> -<span class="sourceLineNo">1447</span> }<a name="line.1447"></a> -<span class="sourceLineNo">1448</span><a name="line.1448"></a> -<span class="sourceLineNo">1449</span> @VisibleForTesting<a name="line.1449"></a> -<span class="sourceLineNo">1450</span> public Set<Long> getActiveProcIds() {<a name="line.1450"></a> -<span class="sourceLineNo">1451</span> return procedures.keySet();<a name="line.1451"></a> -<span class="sourceLineNo">1452</span> }<a name="line.1452"></a> -<span class="sourceLineNo">1453</span><a name="line.1453"></a> -<span class="sourceLineNo">1454</span> Long getRootProcedureId(Procedure<TEnvironment> proc) {<a name="line.1454"></a> -<span class="sourceLineNo">1455</span> return Procedure.getRootProcedureId(procedures, proc);<a name="line.1455"></a> -<span class="sourceLineNo">1456</span> }<a name="line.1456"></a> -<span class="sourceLineNo">1457</span><a name="line.1457"></a> -<span class="sourceLineNo">1458</span> // ==========================================================================<a name="line.1458"></a> -<span class="sourceLineNo">1459</span> // Executions<a name="line.1459"></a> -<span class="sourceLineNo">1460</span> // ==========================================================================<a name="line.1460"></a> -<span class="sourceLineNo">1461</span> private void executeProcedure(Procedure<TEnvironment> proc) {<a name="line.1461"></a> -<span class="sourceLineNo">1462</span> if (proc.isFinished()) {<a name="line.1462"></a> -<span class="sourceLineNo">1463</span> LOG.debug("{} is already finished, skipping execution", proc);<a name="line.1463"></a> -<span class="sourceLineNo">1464</span> return;<a name="line.1464"></a> -<span class="sourceLineNo">1465</span> }<a name="line.1465"></a> -<span class="sourceLineNo">1466</span> final Long rootProcId = getRootProcedureId(proc);<a name="line.1466"></a> -<span class="sourceLineNo">1467</span> if (rootProcId == null) {<a name="line.1467"></a> -<span class="sourceLineNo">1468</span> // The 'proc' was ready to run but the root procedure was rolledback<a name="line.1468"></a> -<span class="sourceLineNo">1469</span> LOG.warn("Rollback because parent is done/rolledback proc=" + proc);<a name="line.1469"></a> -<span class="sourceLineNo">1470</span> executeRollback(proc);<a name="line.1470"></a> -<span class="sourceLineNo">1471</span> return;<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> RootProcedureState<TEnvironment> procStack = rollbackStack.get(rootProcId);<a name="line.1474"></a> -<span class="sourceLineNo">1475</span> if (procStack == null) {<a name="line.1475"></a> -<span class="sourceLineNo">1476</span> LOG.warn("RootProcedureState is null for " + proc.getProcId());<a name="line.1476"></a> -<span class="sourceLineNo">1477</span> return;<a name="line.1477"></a> -<span class="sourceLineNo">1478</span> }<a name="line.1478"></a> -<span class="sourceLineNo">1479</span> do {<a name="line.1479"></a> -<span class="sourceLineNo">1480</span> // Try to acquire the execution<a name="line.1480"></a> -<span class="sourceLineNo">1481</span> if (!procStack.acquire(proc)) {<a name="line.1481"></a> -<span class="sourceLineNo">1482</span> if (procStack.setRollback()) {<a name="line.1482"></a> -<span class="sourceLineNo">1483</span> // we have the 'rollback-lock' we can start rollingback<a name="line.1483"></a> -<span class="sourceLineNo">1484</span> switch (executeRollback(rootProcId, procStack)) {<a name="line.1484"></a> -<span class="sourceLineNo">1485</span> case LOCK_ACQUIRED:<a name="line.1485"></a> -<span class="sourceLineNo">1486</span> break;<a name="line.1486"></a> -<span class="sourceLineNo">1487</span> case LOCK_YIELD_WAIT:<a name="line.1487"></a> -<span class="sourceLineNo">1488</span> procStack.unsetRollback();<a name="line.1488"></a> -<span class="sourceLineNo">1489</span> scheduler.yield(proc);<a name="line.1489"></a> -<span class="sourceLineNo">1490</span> break;<a name="line.1490"></a> -<span class="sourceLineNo">1491</span> case LOCK_EVENT_WAIT:<a name="line.1491"></a> -<span class="sourceLineNo">1492</span> LOG.info("LOCK_EVENT_WAIT rollback..." + proc);<a name="line.1492"></a> -<span class="sourceLineNo">1493</span> procStack.unsetRollback();<a name="line.1493"></a> -<span class="sourceLineNo">1494</span> break;<a name="line.1494"></a> -<span class="sourceLineNo">1495</span> default:<a name="line.1495"></a> -<span class="sourceLineNo">1496</span> throw new UnsupportedOperationException();<a name="line.1496"></a> -<span class="sourceLineNo">1497</span> }<a name="line.1497"></a> -<span class="sourceLineNo">1498</span> } else {<a name="line.1498"></a> -<span class="sourceLineNo">1499</span> // if we can't rollback means that some child is still running.<a name="line.1499"></a> -<span class="sourceLineNo">1500</span> // the rollback will be executed after all the children are done.<a name="line.1500"></a> -<span class="sourceLineNo">1501</span> // If the procedure was never executed, remove and mark it as rolledback.<a name="line.1501"></a> -<span class="sourceLineNo">1502</span> if (!proc.wasExecuted()) {<a name="line.1502"></a> -<span class="sourceLineNo">1503</span> switch (executeRollback(proc)) {<a name="line.1503"></a> -<span class="sourceLineNo">1504</span> case LOCK_ACQUIRED:<a name="line.1504"></a> -<span class="sourceLineNo">1505</span> break;<a name="line.1505"></a> -<span class="sourceLineNo">1506</span> case LOCK_YIELD_WAIT:<a name="line.1506"></a> -<span class="sourceLineNo">1507</span> scheduler.yield(proc);<a name="line.1507"></a> -<span class="sourceLineNo">1508</span> break;<a name="line.1508"></a> -<span class="sourceLineNo">1509</span> case LOCK_EVENT_WAIT:<a name="line.1509"></a> -<span class="sourceLineNo">1510</span> LOG.info("LOCK_EVENT_WAIT can't rollback child running?..." + proc);<a name="line.1510"></a> -<span class="sourceLineNo">1511</span> break;<a name="line.1511"></a> -<span class="sourceLineNo">1512</span> default:<a name="line.1512"></a> -<span class="sourceLineNo">1513</span> throw new UnsupportedOperationException();<a name="line.1513"></a> -<span class="sourceLineNo">1514</span> }<a name="line.1514"></a> -<span class="sourceLineNo">1515</span> }<a name="line.1515"></a> -<span class="sourceLineNo">1516</span> }<a name="line.1516"></a> -<span class="sourceLineNo">1517</span> break;<a name="line.1517"></a> -<span class="sourceLineNo">1518</span> }<a name="line.1518"></a> -<span class="sourceLineNo">1519</span><a name="line.1519"></a> -<span class="sourceLineNo">1520</span> // Execute the procedure<a name="line.1520"></a> -<span class="sourceLineNo">1521</span> assert proc.getState() == ProcedureState.RUNNABLE : proc;<a name="line.1521"></a> -<span class="sourceLineNo">1522</span> // Note that lock is NOT about concurrency but rather about ensuring<a name="line.1522"></a> -<span class="sourceLineNo">1523</span> // ownership of a procedure of an entity such as a region or table<a name="line.1523"></a> -<span class="sourceLineNo">1524</span> LockState lockState = acquireLock(proc);<a name="line.1524"></a> -<span class="sourceLineNo">1525</span> switch (lockState) {<a name="line.1525"></a> -<span class="sourceLineNo">1526</span> case LOCK_ACQUIRED:<a name="line.1526"></a> -<span class="sourceLineNo">1527</span> execProcedure(procStack, proc);<a name="line.1527"></a> -<span class="sourceLineNo">1528</span> break;<a name="line.1528"></a> -<span class="sourceLineNo">1529</span> case LOCK_YIELD_WAIT:<a name="line.1529"></a> -<span class="sourceLineNo">1530</span> LOG.info(lockState + " " + proc);<a name="line.1530"></a> -<span class="sourceLineNo">1531</span> scheduler.yield(proc);<a name="line.1531"></a> -<span class="sourceLineNo">1532</span> break;<a name="line.1532"></a> -<span class="sourceLineNo">1533</span> case LOCK_EVENT_WAIT:<a name="line.1533"></a> -<span class="sourceLineNo">1534</span> // Someone will wake us up when the lock is available<a name="line.1534"></a> -<span class="sourceLineNo">1535</span> LOG.debug(lockState + " " + proc);<a name="line.1535"></a> -<span class="sourceLineNo">1536</span> break;<a name="line.1536"></a> -<span class="sourceLineNo">1537</span> default:<a name="line.1537"></a> -<span class="sourceLineNo">1538</span> throw new UnsupportedOperationException();<a name="line.1538"></a> -<span class="sourceLineNo">1539</span> }<a name="line.1539"></a> -<span class="sourceLineNo">1540</span> procStack.release(proc);<a name="line.1540"></a> -<span class="sourceLineNo">1541</span><a name="line.1541"></a> -<span class="sourceLineNo">1542</span> if (proc.isSuccess()) {<a name="line.1542"></a> -<span class="sourceLineNo">1543</span> // update metrics on finishing the procedure<a name="line.1543"></a> -<span class="sourceLineNo">1544</span> proc.updateMetricsOnFinish(getEnvironment(), proc.elapsedTime(), true);<a name="line.1544"></a> -<span class="sourceLineNo">1545</span> LOG.info("Finished " + proc + " in " + StringUtils.humanTimeDiff(proc.elapsedTime()));<a name="line.1545"></a> -<span class="sourceLineNo">1546</span> // Finalize the procedure state<a name="line.1546"></a> -<span class="sourceLineNo">1547</span> if (proc.getProcId() == rootProcId) {<a name="line.1547"></a> -<span class="sourceLineNo">1548</span> procedureFinished(proc);<a name="line.1548"></a> -<span class="sourceLineNo">1549</span> } else {<a name="line.1549"></a> -<span class="sourceLineNo">1550</span> execCompletionCleanup(proc);<a name="line.1550"></a> -<span class="sourceLineNo">1551</span> }<a name="line.1551"></a> -<span class="sourceLineNo">1552</span> break;<a name="line.1552"></a> -<span class="sourceLineNo">1553</span> }<a name="line.1553"></a> -<span class="sourceLineNo">1554</span> } while (procStack.isFailed());<a name="line.1554"></a> -<span class="sourceLineNo">1555</span> }<a name="line.1555"></a> -<span class="sourceLineNo">1556</span><a name="line.1556"></a> -<span class="sourceLineNo">1557</span> private LockState acquireLock(Procedure<TEnvironment> proc) {<a name="line.1557"></a> -<span class="sourceLineNo">1558</span> TEnvironment env = getEnvironment();<a name="line.1558"></a> -<span class="sourceLineNo">1559</span> // if holdLock is true, then maybe we already have the lock, so just return LOCK_ACQUIRED if<a name="line.1559"></a> -<span class="sourceLineNo">1560</span> // hasLock is true.<a name="line.1560"></a> -<span class="sourceLineNo">1561</span> if (proc.hasLock()) {<a name="line.1561"></a> -<span class="sourceLineNo">1562</span> return LockState.LOCK_ACQUIRED;<a name="line.1562"></a> -<span class="sourceLineNo">1563</span> }<a name="line.1563"></a> -<span class="sourceLineNo">1564</span> return proc.doAcquireLock(env, store);<a name="line.1564"></a> -<span class="sourceLineNo">1565</span> }<a name="line.1565"></a> -<span class="sourceLineNo">1566</span><a name="line.1566"></a> -<span class="sourceLineNo">1567</span> private void releaseLock(Procedure<TEnvironment> proc, boolean force) {<a name="line.1567"></a> -<span class="sourceLineNo">1568</span> TEnvironment env = getEnvironment();<a name="line.1568"></a> -<span class="sourceLineNo">1569</span> // For how the framework works, we know that we will always have the lock<a name="line.1569"></a> -<span class="sourceLineNo">1570</span> // when we call releaseLock(), so we can avoid calling proc.hasLock()<a name="line.1570"></a> -<span class="sourceLineNo">1571</span> if (force || !proc.holdLock(env) || proc.isFinished()) {<a name="line.1571"></a> -<span class="sourceLineNo">1572</span> proc.doReleaseLock(env, store);<a name="line.1572"></a> -<span class="sourceLineNo">1573</span> }<a name="line.1573"></a> -<span class="sourceLineNo">1574</span> }<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> * Execute the rollback of the full procedure stack. Once the procedure is rolledback, the<a name="line.1577"></a> -<span class="sourceLineNo">1578</span> * root-procedure will be visible as finished to user, and the result will be the fatal exception.<a name="line.1578"></a> -<span class="sourceLineNo">1579</span> */<a name="line.1579"></a> -<span class="sourceLineNo">1580</span> private LockState executeRollback(long rootProcId, RootProcedureState<TEnvironment> procStack) {<a name="line.1580"></a> -<span class="sourceLineNo">1581</span> Procedure<TEnvironment> rootProc = procedures.get(rootProcId);<a name="line.1581"></a> -<span class="sourceLineNo">1582</span> RemoteProcedureException exception = rootProc.getException();<a name="line.1582"></a> -<span class="sourceLineNo">1583</span> // TODO: This needs doc. The root proc doesn't have an exception. Maybe we are<a name="line.1583"></a> -<span class="sourceLineNo">1584</span> // rolling back because the subprocedure does. Clarify.<a name="line.1584"></a> -<span class="sourceLineNo">1585</span> if (exception == null) {<a name="line.1585"></a> -<span class="sourceLineNo">1586</span> exception = procStack.getException();<a name="line.1586"></a> -<span class="sourceLineNo">1587</span> rootProc.setFailure(exception);<a name="line.1587"></a> -<span class="sourceLineNo">1588</span> store.update(rootProc);<a name="line.1588"></a> -<span class="sourceLineNo">1589</span> }<a name="line.1589"></a> -<span class="sourceLineNo">1590</span><a name="line.1590"></a> -<span class="sourceLineNo">1591</span> List<Procedure<TEnvironment>> subprocStack = procStack.getSubproceduresStack();<a name="line.1591"></a> -<span class="sourceLineNo">1592</span> assert subprocStack != null : "Called rollback with no steps executed rootProc=" + rootProc;<a name="line.1592"></a> -<span class="sourceLineNo">1593</span><a name="line.1593"></a> -<span class="sourceLineNo">1594</span> int stackTail = subprocStack.size();<a name="line.1594"></a> -<span class="sourceLineNo">1595</span> while (stackTail-- > 0) {<a name="line.1595"></a> -<span class="sourceLineNo">1596</span> Procedure<TEnvironment> proc = subprocStack.get(stackTail);<a name="line.1596"></a> -<span class="sourceLineNo">1597</span><a name="line.1597"></a> -<span class="sourceLineNo">1598</span> LockState lockState = acquireLock(proc);<a name="line.1598"></a> -<span class="sourceLineNo">1599</span> if (lockState != LockState.LOCK_ACQUIRED) {<a name="line.1599"></a> -<span class="sourceLineNo">1600</span> // can't take a lock on the procedure, add the root-proc back on the<a name="line.1600"></a> -<span class="sourceLineNo">1601</span> // queue waiting for the lock availability<a name="line.1601"></a> -<span class="sourceLineNo">1602</span> return lockState;<a name="line.1602"></a> -<span class="sourceLineNo">1603</span> }<a name="line.1603"></a> -<span class="sourceLineNo">1604</span><a name="line.1604"></a> -<span class="sourceLineNo">1605</span> lockState = executeRollback(proc);<a name="line.1605"></a> -<span class="sourceLineNo">1606</span> releaseLock(proc, false);<a name="line.1606"></a> -<span class="sourceLineNo">1607</span> boolean abortRollback = lockState != LockState.LOCK_ACQUIRED;<a name="line.1607"></a> -<span class="sourceLineNo">1608</span> abortRollback |= !isRunning() || !store.isRunning();<a name="line.1608"></a> -<span class="sourceLineNo">1609</span><a name="line.1609"></a> -<span class="sourceLineNo">1610</span> // allows to kill the executor before something is stored to the wal.<a name="line.1610"></a> -<span class="sourceLineNo">1611</span> // useful to test the procedure recovery.<a name="line.1611"></a> -<span class="sourceLineNo">1612</span> if (abortRollback) {<a name="line.1612"></a> -<span class="sourceLineNo">1613</span> return lockState;<a name="line.1613"></a> -<span class="sourceLineNo">1614</span> }<a name="line.1614"></a> -<span class="sourceLineNo">1615</span><a name="line.1615"></a> -<span class="sourceLineNo">1616</span> subprocStack.remove(stackTail);<a name="line.1616"></a> +<span class="sourceLineNo">1057</span> Preconditions.checkArgument(lockWait > 0, "lockWait should be positive");<a name="line.1057"></a> +<span class="sourceLineNo">1058</span> Procedure<TEnvironment> procedure = getProcedure(pid);<a name="line.1058"></a> +<span class="sourceLineNo">1059</span> if (procedure == null) {<a name="line.1059"></a> +<span class="sourceLineNo">1060</span> LOG.debug("Procedure with id={} does not exist, skipping bypass", pid);<a name="line.1060"></a> +<span class="sourceLineNo">1061</span> return false;<a name="line.1061"></a> +<span class="sourceLineNo">1062</span> }<a name="line.1062"></a> +<span class="sourceLineNo">1063</span><a name="line.1063"></a> +<span class="sourceLineNo">1064</span> LOG.debug("Begin bypass {} with lockWait={}, force={}", procedure, lockWait, force);<a name="line.1064"></a> +<span class="sourceLineNo">1065</span> IdLock.Entry lockEntry = procExecutionLock.tryLockEntry(procedure.getProcId(), lockWait);<a name="line.1065"></a> +<span class="sourceLineNo">1066</span> if (lockEntry == null && !force) {<a name="line.1066"></a> +<span class="sourceLineNo">1067</span> LOG.debug("Waited {} ms, but {} is still running, skipping bypass with force={}",<a name="line.1067"></a> +<span class="sourceLineNo">1068</span> lockWait, procedure, force);<a name="line.1068"></a> +<span class="sourceLineNo">1069</span> return false;<a name="line.1069"></a> +<span class="sourceLineNo">1070</span> } else if (lockEntry == null) {<a name="line.1070"></a> +<span class="sourceLineNo">1071</span> LOG.debug("Waited {} ms, but {} is still running, begin bypass with force={}",<a name="line.1071"></a> +<span class="sourceLineNo">1072</span> lockWait, procedure, force);<a name="line.1072"></a> +<span class="sourceLineNo">1073</span> }<a name="line.1073"></a> +<span class="sourceLineNo">1074</span> try {<a name="line.1074"></a> +<span class="sourceLineNo">1075</span> // check whether the procedure is already finished<a name="line.1075"></a> +<span class="sourceLineNo">1076</span> if (procedure.isFinished()) {<a name="line.1076"></a> +<span class="sourceLineNo">1077</span> LOG.debug("{} is already finished, skipping bypass", procedure);<a name="line.1077"></a> +<span class="sourceLineNo">1078</span> return false;<a name="line.1078"></a> +<span class="sourceLineNo">1079</span> }<a name="line.1079"></a> +<span class="sourceLineNo">1080</span><a name="line.1080"></a> +<span class="sourceLineNo">1081</span> if (procedure.hasChildren()) {<a name="line.1081"></a> +<span class="sourceLineNo">1082</span> LOG.debug("{} has children, skipping bypass", procedure);<a name="line.1082"></a> +<span class="sourceLineNo">1083</span> return false;<a name="line.1083"></a> +<span class="sourceLineNo">1084</span> }<a name="line.1084"></a> +<span class="sourceLineNo">1085</span><a name="line.1085"></a> +<span class="sourceLineNo">1086</span> // If the procedure has no parent or no child, we are safe to bypass it in whatever state<a name="line.1086"></a> +<span class="sourceLineNo">1087</span> if (procedure.hasParent() && procedure.getState() != ProcedureState.RUNNABLE<a name="line.1087"></a> +<span class="sourceLineNo">1088</span> && procedure.getState() != ProcedureState.WAITING<a name="line.1088"></a> +<span class="sourceLineNo">1089</span> && procedure.getState() != ProcedureState.WAITING_TIMEOUT) {<a name="line.1089"></a> +<span class="sourceLineNo">1090</span> LOG.debug("Bypassing procedures in RUNNABLE, WAITING and WAITING_TIMEOUT states "<a name="line.1090"></a> +<span class="sourceLineNo">1091</span> + "(with no parent), {}",<a name="line.1091"></a> +<span class="sourceLineNo">1092</span> procedure);<a name="line.1092"></a> +<span class="sourceLineNo">1093</span> return false;<a name="line.1093"></a> +<span class="sourceLineNo">1094</span> }<a name="line.1094"></a> +<span class="sourceLineNo">1095</span><a name="line.1095"></a> +<span class="sourceLineNo">1096</span> // Now, the procedure is not finished, and no one can execute it since we take the lock now<a name="line.1096"></a> +<span class="sourceLineNo">1097</span> // And we can be sure that its ancestor is not running too, since their child has not<a name="line.1097"></a> +<span class="sourceLineNo">1098</span> // finished yet<a name="line.1098"></a> +<span class="sourceLineNo">1099</span> Procedure<TEnvironment> current = procedure;<a name="line.1099"></a> +<span class="sourceLineNo">1100</span> while (current != null) {<a name="line.1100"></a> +<span class="sourceLineNo">1101</span> LOG.debug("Bypassing {}", current);<a name="line.1101"></a> +<span class="sourceLineNo">1102</span> current.bypass();<a name="line.1102"></a> +<span class="sourceLineNo">1103</span> store.update(procedure);<a name="line.1103"></a> +<span class="sourceLineNo">1104</span> long parentID = current.getParentProcId();<a name="line.1104"></a> +<span class="sourceLineNo">1105</span> current = getProcedure(parentID);<a name="line.1105"></a> +<span class="sourceLineNo">1106</span> }<a name="line.1106"></a> +<span class="sourceLineNo">1107</span><a name="line.1107"></a> +<span class="sourceLineNo">1108</span> //wake up waiting procedure, already checked there is no child<a name="line.1108"></a> +<span class="sourceLineNo">1109</span> if (procedure.getState() == ProcedureState.WAITING) {<a name="line.1109"></a> +<span class="sourceLineNo">1110</span> procedure.setState(ProcedureState.RUNNABLE);<a name="line.1110"></a> +<span class="sourceLineNo">1111</span> store.update(procedure);<a name="line.1111"></a> +<span class="sourceLineNo">1112</span> }<a name="line.1112"></a> +<span class="sourceLineNo">1113</span><a name="line.1113"></a> +<span class="sourceLineNo">1114</span> // If we don't have the lock, we can't re-submit the queue,<a name="line.1114"></a> +<span class="sourceLineNo">1115</span> // since it is already executing. To get rid of the stuck situation, we<a name="line.1115"></a> +<span class="sourceLineNo">1116</span> // need to restart the master. With the procedure set to bypass, the procedureExecutor<a name="line.1116"></a> +<span class="sourceLineNo">1117</span> // will bypass it and won't get stuck again.<a name="line.1117"></a> +<span class="sourceLineNo">1118</span> if (lockEntry != null) {<a name="line.1118"></a> +<span class="sourceLineNo">1119</span> // add the procedure to run queue,<a name="line.1119"></a> +<span class="sourceLineNo">1120</span> scheduler.addFront(procedure);<a name="line.1120"></a> +<span class="sourceLineNo">1121</span> LOG.debug("Bypassing {} and its ancestors successfully, adding to queue", procedure);<a name="line.1121"></a> +<span class="sourceLineNo">1122</span> } else {<a name="line.1122"></a> +<span class="sourceLineNo">1123</span> LOG.debug("Bypassing {} and its ancestors successfully, but since it is already running, "<a name="line.1123"></a> +<span class="sourceLineNo">1124</span> + "skipping add to queue", procedure);<a name="line.1124"></a> +<span class="sourceLineNo">1125</span> }<a name="line.1125"></a> +<span class="sourceLineNo">1126</span> return true;<a name="line.1126"></a> +<span class="sourceLineNo">1127</span><a name="line.1127"></a> +<span class="sourceLineNo">1128</span> } finally {<a name="line.1128"></a> +<span class="sourceLineNo">1129</span> if (lockEntry != null) {<a name="line.1129"></a> +<span class="sourceLineNo">1130</span> procExecutionLock.releaseLockEntry(lockEntry);<a name="line.1130"></a> +<span class="sourceLineNo">1131</span> }<a name="line.1131"></a> +<span class="sourceLineNo">1132</span> }<a name="line.1132"></a> +<span class="sourceLineNo">1133</span> }<a name="line.1133"></a> +<span class="sourceLineNo">1134</span><a name="line.1134"></a> +<span class="sourceLineNo">1135</span> /**<a name="line.1135"></a> +<span class="sourceLineNo">1136</span> * Add a new root-procedure to the executor.<a name="line.1136"></a> +<span class="sourceLineNo">1137</span> * @param proc the new procedure to execute.<a name="line.1137"></a> +<span class="sourceLineNo">1138</span> * @param nonceKey the registered unique identifier for this operation from the client or process.<a name="line.1138"></a> +<span class="sourceLineNo">1139</span> * @return the procedure id, that can be used to monitor the operation<a name="line.1139"></a> +<span class="sourceLineNo">1140</span> */<a name="line.1140"></a> +<span class="sourceLineNo">1141</span> @edu.umd.cs.findbugs.annotations.SuppressWarnings(value="NP_NULL_ON_SOME_PATH",<a name="line.1141"></a> +<span class="sourceLineNo">1142</span> justification = "FindBugs is blind to the check-for-null")<a name="line.1142"></a> +<span class="sourceLineNo">1143</span> public long submitProcedure(Procedure<TEnvironment> proc, NonceKey nonceKey) {<a name="line.1143"></a> +<span class="sourceLineNo">1144</span> Preconditions.checkArgument(lastProcId.get() >= 0);<a name="line.1144"></a> +<span class="sourceLineNo">1145</span><a name="line.1145"></a> +<span class="sourceLineNo">1146</span> prepareProcedure(proc);<a name="line.1146"></a> +<span class="sourceLineNo">1147</span><a name="line.1147"></a> +<span class="sourceLineNo">1148</span> final Long currentProcId;<a name="line.1148"></a> +<span class="sourceLineNo">1149</span> if (nonceKey != null) {<a name="line.1149"></a> +<span class="sourceLineNo">1150</span> currentProcId = nonceKeysToProcIdsMap.get(nonceKey);<a name="line.1150"></a> +<span class="sourceLineNo">1151</span> Preconditions.checkArgument(currentProcId != null,<a name="line.1151"></a> +<span class="sourceLineNo">1152</span> "Expected nonceKey=" + nonceKey + " to be reserved, use registerNonce(); proc=" + proc);<a name="line.1152"></a> +<span class="sourceLineNo">1153</span> } else {<a name="line.1153"></a> +<span class="sourceLineNo">1154</span> currentProcId = nextProcId();<a name="line.1154"></a> +<span class="sourceLineNo">1155</span> }<a name="line.1155"></a> +<span class="sourceLineNo">1156</span><a name="line.1156"></a> +<span class="sourceLineNo">1157</span> // Initialize the procedure<a name="line.1157"></a> +<span class="sourceLineNo">1158</span> proc.setNonceKey(nonceKey);<a name="line.1158"></a> +<span class="sourceLineNo">1159</span> proc.setProcId(currentProcId.longValue());<a name="line.1159"></a> +<span class="sourceLineNo">1160</span><a name="line.1160"></a> +<span class="sourceLineNo">1161</span> // Commit the transaction<a name="line.1161"></a> +<span class="sourceLineNo">1162</span> store.insert(proc, null);<a name="line.1162"></a> +<span class="sourceLineNo">1163</span> LOG.debug("Stored {}", proc);<a name="line.1163"></a> +<span class="sourceLineNo">1164</span><a name="line.1164"></a> +<span class="sourceLineNo">1165</span> // Add the procedure to the executor<a name="line.1165"></a> +<span class="sourceLineNo">1166</span> return pushProcedure(proc);<a name="line.1166"></a> +<span class="sourceLineNo">1167</span> }<a name="line.1167"></a> +<span class="sourceLineNo">1168</span><a name="line.1168"></a> +<span class="sourceLineNo">1169</span> /**<a name="line.1169"></a> +<span class="sourceLineNo">1170</span> * Add a set of new root-procedure to the executor.<a name="line.1170"></a> +<span class="sourceLineNo">1171</span> * @param procs the new procedures to execute.<a name="line.1171"></a> +<span class="sourceLineNo">1172</span> */<a name="line.1172"></a> +<span class="sourceLineNo">1173</span> // TODO: Do we need to take nonces here?<a name="line.1173"></a> +<span class="sourceLineNo">1174</span> public void submitProcedures(Procedure<TEnvironment>[] procs) {<a name="line.1174"></a> +<span class="sourceLineNo">1175</span> Preconditions.checkArgument(lastProcId.get() >= 0);<a name="line.1175"></a> +<span class="sourceLineNo">1176</span> if (procs == null || procs.length <= 0) {<a name="line.1176"></a> +<span class="sourceLineNo">1177</span> return;<a name="line.1177"></a> +<span class="sourceLineNo">1178</span> }<a name="line.1178"></a> +<span class="sourceLineNo">1179</span><a name="line.1179"></a> +<span class="sourceLineNo">1180</span> // Prepare procedure<a name="line.1180"></a> +<span class="sourceLineNo">1181</span> for (int i = 0; i < procs.length; ++i) {<a name="line.1181"></a> +<span class="sourceLineNo">1182</span> prepareProcedure(procs[i]).setProcId(nextProcId());<a name="line.1182"></a> +<span class="sourceLineNo">1183</span> }<a name="line.1183"></a> +<span class="sourceLineNo">1184</span><a name="line.1184"></a> +<span class="sourceLineNo">1185</span> // Commit the transaction<a name="line.1185"></a> +<span class="sourceLineNo">1186</span> store.insert(procs);<a name="line.1186"></a> +<span class="sourceLineNo">1187</span> if (LOG.isDebugEnabled()) {<a name="line.1187"></a> +<span class="sourceLineNo">1188</span> LOG.debug("Stored " + Arrays.toString(procs));<a name="line.1188"></a> +<span class="sourceLineNo">1189</span> }<a name="line.1189"></a> +<span class="sourceLineNo">1190</span><a name="line.1190"></a> +<span class="sourceLineNo">1191</span> // Add the procedure to the executor<a name="line.1191"></a> +<span class="sourceLineNo">1192</span> for (int i = 0; i < procs.length; ++i) {<a name="line.1192"></a> +<span class="sourceLineNo">1193</span> pushProcedure(procs[i]);<a name="line.1193"></a> +<span class="sourceLineNo">1194</span> }<a name="line.1194"></a> +<span class="sourceLineNo">1195</span> }<a name="line.1195"></a> +<span class="sourceLineNo">1196</span><a name="line.1196"></a> +<span class="sourceLineNo">1197</span> private Procedure<TEnvironment> prepareProcedure(Procedure<TEnvironment> proc) {<a name="line.1197"></a> +<span class="sourceLineNo">1198</span> Preconditions.checkArgument(proc.getState() == ProcedureState.INITIALIZING);<a name="line.1198"></a> +<span class="sourceLineNo">1199</span> Preconditions.checkArgument(!proc.hasParent(), "unexpected parent", proc);<a name="line.1199"></a> +<span class="sourceLineNo">1200</span> if (this.checkOwnerSet) {<a name="line.1200"></a> +<span class="sourceLineNo">1201</span> Preconditions.checkArgument(proc.hasOwner(), "missing owner");<a name="line.1201"></a> +<span class="sourceLineNo">1202</span> }<a name="line.1202"></a> +<span class="sourceLineNo">1203</span> return proc;<a name="line.1203"></a> +<span class="sourceLineNo">1204</span> }<a name="line.1204"></a> +<span class="sourceLineNo">1205</span><a name="line.1205"></a> +<span class="sourceLineNo">1206</span> private long pushProcedure(Procedure<TEnvironment> proc) {<a name="line.1206"></a> +<span class="sourceLineNo">1207</span> final long currentProcId = proc.getProcId();<a name="line.1207"></a> +<span class="sourceLineNo">1208</span><a name="line.1208"></a> +<span class="sourceLineNo">1209</span> // Update metrics on start of a procedure<a name="line.1209"></a> +<span class="sourceLineNo">1210</span> proc.updateMetricsOnSubmit(getEnvironment());<a name="line.1210"></a> +<span class="sourceLineNo">1211</span><a name="line.1211"></a> +<span class="sourceLineNo">1212</span> // Create the rollback stack for the procedure<a name="line.1212"></a> +<span class="sourceLineNo">1213</span> RootProcedureState<TEnvironment> stack = new RootProcedureState<>();<a name="line.1213"></a> +<span class="sourceLineNo">1214</span> rollbackStack.put(currentProcId, stack);<a name="line.1214"></a> +<span class="sourceLineNo">1215</span><a name="line.1215"></a> +<span class="sourceLineNo">1216</span> // Submit the new subprocedures<a name="line.1216"></a> +<span class="sourceLineNo">1217</span> assert !procedures.containsKey(currentProcId);<a name="line.1217"></a> +<span class="sourceLineNo">1218</span> procedures.put(currentProcId, proc);<a name="line.1218"></a> +<span class="sourceLineNo">1219</span> sendProcedureAddedNotification(currentProcId);<a name="line.1219"></a> +<span class="sourceLineNo">1220</span> scheduler.addBack(proc);<a name="line.1220"></a> +<span class="sourceLineNo">1221</span> return proc.getProcId();<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> /**<a name="line.1224"></a> +<span class="sourceLineNo">1225</span> * Send an abort notification the specified procedure.<a name="line.1225"></a> +<span class="sourceLineNo">1226</span> * Depending on the procedure implementation the abort can be considered or ignored.<a name="line.1226"></a> +<span class="sourceLineNo">1227</span> * @param procId the procedure to abort<a name="line.1227"></a> +<span class="sourceLineNo">1228</span> * @return true if the procedure exists and has received the abort, otherwise false.<a name="line.1228"></a> +<span class="sourceLineNo">1229</span> */<a name="line.1229"></a> +<span class="sourceLineNo">1230</span> public boolean abort(long procId) {<a name="line.1230"></a> +<span class="sourceLineNo">1231</span> return abort(procId, true);<a name="line.1231"></a> +<span class="sourceLineNo">1232</span> }<a name="line.1232"></a> +<span class="sourceLineNo">1233</span><a name="line.1233"></a> +<span class="sourceLineNo">1234</span> /**<a name="line.1234"></a> +<span class="sourceLineNo">1235</span> * Send an abort notification to the specified procedure.<a name="line.1235"></a> +<span class="sourceLineNo">1236</span> * Depending on the procedure implementation, the abort can be considered or ignored.<a name="line.1236"></a> +<span class="sourceLineNo">1237</span> * @param procId the procedure to abort<a name="line.1237"></a> +<span class="sourceLineNo">1238</span> * @param mayInterruptIfRunning if the proc completed at least one step, should it be aborted?<a name="line.1238"></a> +<span class="sourceLineNo">1239</span> * @return true if the procedure exists and has received the abort, otherwise false.<a name="line.1239"></a> +<span class="sourceLineNo">1240</span> */<a name="line.1240"></a> +<span class="sourceLineNo">1241</span> public boolean abort(long procId, boolean mayInterruptIfRunning) {<a name="line.1241"></a> +<span class="sourceLineNo">1242</span> Procedure<TEnvironment> proc = procedures.get(procId);<a name="line.1242"></a> +<span class="sourceLineNo">1243</span> if (proc != null) {<a name="line.1243"></a> +<span class="sourceLineNo">1244</span> if (!mayInterruptIfRunning && proc.wasExecuted()) {<a name="line.1244"></a> +<span class="sourceLineNo">1245</span> return false;<a name="line.1245"></a> +<span class="sourceLineNo">1246</span> }<a name="line.1246"></a> +<span class="sourceLineNo">1247</span> return proc.abort(getEnvironment());<a name="line.1247"></a> +<span class="sourceLineNo">1248</span> }<a name="line.1248"></a> +<span class="sourceLineNo">1249</span> return false;<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> // ==========================================================================<a name="line.1252"></a> +<span class="sourceLineNo">1253</span> // Executor query helpers<a name="line.1253"></a> +<span class="sourceLineNo">1254</span> // ==========================================================================<a name="line.1254"></a> +<span class="sourceLineNo">1255</span> public Procedure<TEnvironment> getProcedure(final long procId) {<a name="line.1255"></a> +<span class="sourceLineNo">1256</span> return procedures.get(procId);<a name="line.1256"></a> +<span class="sourceLineNo">1257</span> }<a name="line.1257"></a> +<span class="sourceLineNo">1258</span><a name="line.1258"></a> +<span class="sourceLineNo">1259</span> public <T extends Procedure<TEnvironment>> T getProcedure(Class<T> clazz, long procId) {<a name="line.1259"></a> +<span class="sourceLineNo">1260</span> Procedure<TEnvironment> proc = getProcedure(procId);<a name="line.1260"></a> +<span class="sourceLineNo">1261</span> if (clazz.isInstance(proc)) {<a name="line.1261"></a> +<span class="sourceLineNo">1262</span> return clazz.cast(proc);<a name="line.1262"></a> +<span class="sourceLineNo">1263</span> }<a name="line.1263"></a> +<span class="sourceLineNo">1264</span> return null;<a name="line.1264"></a> +<span class="sourceLineNo">1265</span> }<a name="line.1265"></a> +<span class="sourceLineNo">1266</span><a name="line.1266"></a> +<span class="sourceLineNo">1267</span> public Procedure<TEnvironment> getResult(long procId) {<a name="line.1267"></a> +<span class="sourceLineNo">1268</span> CompletedProcedureRetainer<TEnvironment> retainer = completed.get(procId);<a name="line.1268"></a> +<span class="sourceLineNo">1269</span> if (retainer == null) {<a name="line.1269"></a> +<span class="sourceLineNo">1270</span> return null;<a name="line.1270"></a> +<span class="sourceLineNo">1271</span> } else {<a name="line.1271"></a> +<span class="sourceLineNo">1272</span> return retainer.getProcedure();<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> * Return true if the procedure is finished.<a name="line.1277"></a> +<span class="sourceLineNo">1278</span> * The state may be "completed successfully" or "failed and rolledback".<a name="line.1278"></a> +<span class="sourceLineNo">1279</span> * Use getResult() to check the state or get the result data.<a name="line.1279"></a> +<span class="sourceLineNo">1280</span> * @param procId the ID of the procedure to check<a name="line.1280"></a> +<span class="sourceLineNo">1281</span> * @return true if the procedure execution is finished, otherwise false.<a name="line.1281"></a> +<span class="sourceLineNo">1282</span> */<a name="line.1282"></a> +<span class="sourceLineNo">1283</span> public boolean isFinished(final long procId) {<a name="line.1283"></a> +<span class="sourceLineNo">1284</span> return !procedures.containsKey(procId);<a name="line.1284"></a> +<span class="sourceLineNo">1285</span> }<a name="line.1285"></a> +<span class="sourceLineNo">1286</span><a name="line.1286"></a> +<span class="sourceLineNo">1287</span> /**<a name="line.1287"></a> +<span class="sourceLineNo">1288</span> * Return true if the procedure is started.<a name="line.1288"></a> +<span class="sourceLineNo">1289</span> * @param procId the ID of the procedure to check<a name="line.1289"></a> +<span class="sourceLineNo">1290</span> * @return true if the procedure execution is started, otherwise false.<a name="line.1290"></a> +<span class="sourceLineNo">1291</span> */<a name="line.1291"></a> +<span class="sourceLineNo">1292</span> public boolean isStarted(long procId) {<a name="line.1292"></a> +<span class="sourceLineNo">1293</span> Procedure<?> proc = procedures.get(procId);<a name="line.1293"></a> +<span class="sourceLineNo">1294</span> if (proc == null) {<a name="line.1294"></a> +<span class="sourceLineNo">1295</span> return completed.get(procId) != null;<a name="line.1295"></a> +<span class="sourceLineNo">1296</span> }<a name="line.1296"></a> +<span class="sourceLineNo">1297</span> return proc.wasExecuted();<a name="line.1297"></a> +<span class="sourceLineNo">1298</span> }<a name="line.1298"></a> +<span class="sourceLineNo">1299</span><a name="line.1299"></a> +<span class="sourceLineNo">1300</span> /**<a name="line.1300"></a> +<span class="sourceLineNo">1301</span> * Mark the specified completed procedure, as ready to remove.<a name="line.1301"></a> +<span class="sourceLineNo">1302</span> * @param procId the ID of the procedure to remove<a name="line.1302"></a> +<span class="sourceLineNo">1303</span> */<a name="line.1303"></a> +<span class="sourceLineNo">1304</span> public void removeResult(long procId) {<a name="line.1304"></a> +<span class="sourceLineNo">1305</span> CompletedProcedureRetainer<TEnvironment> retainer = completed.get(procId);<a name="line.1305"></a> +<span class="sourceLineNo">1306</span> if (retainer == null) {<a name="line.1306"></a> +<span class="sourceLineNo">1307</span> assert !procedures.containsKey(procId) : "pid=" + procId + " is still running";<a name="line.1307"></a> +<span class="sourceLineNo">1308</span> LOG.debug("pid={} already removed by the cleaner.", procId);<a name="line.1308"></a> +<span class="sourceLineNo">1309</span> return;<a name="line.1309"></a> +<span class="sourceLineNo">1310</span> }<a name="line.1310"></a> +<span class="sourceLineNo">1311</span><a name="line.1311"></a> +<span class="sourceLineNo">1312</span> // The CompletedProcedureCleaner will take care of deletion, once the TTL is expired.<a name="line.1312"></a> +<span class="sourceLineNo">1313</span> retainer.setClientAckTime(EnvironmentEdgeManager.currentTime());<a name="line.1313"></a> +<span class="sourceLineNo">1314</span> }<a name="line.1314"></a> +<span class="sourceLineNo">1315</span><a name="line.1315"></a> +<span class="sourceLineNo">1316</span> public Procedure<TEnvironment> getResultOrProcedure(long procId) {<a name="line.1316"></a> +<span class="sourceLineNo">1317</span> CompletedProcedureRetainer<TEnvironment> retainer = completed.get(procId);<a name="line.1317"></a> +<span class="sourceLineNo">1318</span> if (retainer == null) {<a name="line.1318"></a> +<span class="sourceLineNo">1319</span> return procedures.get(procId);<a name="line.1319"></a> +<span class="sourceLineNo">1320</span> } else {<a name="line.1320"></a> +<span class="sourceLineNo">1321</span> return retainer.getProcedure();<a name="line.1321"></a> +<span class="sourceLineNo">1322</span> }<a name="line.1322"></a> +<span class="sourceLineNo">1323</span> }<a name="line.1323"></a> +<span class="sourceLineNo">1324</span><a name="line.1324"></a> +<span class="sourceLineNo">1325</span> /**<a name="line.1325"></a> +<span class="sourceLineNo">1326</span> * Check if the user is this procedure's owner<a name="line.1326"></a> +<span class="sourceLineNo">1327</span> * @param procId the target procedure<a name="line.1327"></a> +<span class="sourceLineNo">1328</span> * @param user the user<a name="line.1328"></a> +<span class="sourceLineNo">1329</span> * @return true if the user is the owner of the procedure,<a name="line.1329"></a> +<span class="sourceLineNo">1330</span> * false otherwise or the owner is unknown.<a name="line.1330"></a> +<span class="sourceLineNo">1331</span> */<a name="line.1331"></a> +<span class="sourceLineNo">1332</span> public boolean isProcedureOwner(long procId, User user) {<a name="line.1332"></a> +<span class="sourceLineNo">1333</span> if (user == null) {<a name="line.1333"></a> +<span class="sourceLineNo">1334</span> return false;<a name="line.1334"></a> +<span class="sourceLineNo">1335</span> }<a name="line.1335"></a> +<span class="sourceLineNo">1336</span> final Procedure<TEnvironment> runningProc = procedures.get(procId);<a name="line.1336"></a> +<span class="sourceLineNo">1337</span> if (runningProc != null) {<a name="line.1337"></a> +<span class="sourceLineNo">1338</span> return runningProc.getOwner().equals(user.getShortName());<a name="line.1338"></a> +<span class="sourceLineNo">1339</span> }<a name="line.1339"></a> +<span class="sourceLineNo">1340</span><a name="line.1340"></a> +<span class="sourceLineNo">1341</span> final CompletedProcedureRetainer<TEnvironment> retainer = completed.get(procId);<a name="line.1341"></a> +<span class="sourceLineNo">1342</span> if (retainer != null) {<a name="line.1342"></a> +<span class="sourceLineNo">1343</span> return retainer.getProcedure().getOwner().equals(user.getShortName());<a name="line.1343"></a> +<span class="sourceLineNo">1344</span> }<a name="line.1344"></a> +<span class="sourceLineNo">1345</span><a name="line.1345"></a> +<span class="sourceLineNo">1346</span> // Procedure either does not exist or has already completed and got cleaned up.<a name="line.1346"></a> +<span class="sourceLineNo">1347</span> // At this time, we cannot check the owner of the procedure<a name="line.1347"></a> +<span class="sourceLineNo">1348</span> return false;<a name="line.1348"></a> +<span class="sourceLineNo">1349</span> }<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> * Should only be used when starting up, where the procedure workers have not been started.<a name="line.1352"></a> +<span class="sourceLineNo">1353</span> * <p/><a name="line.1353"></a> +<span class="sourceLineNo">1354</span> * If the procedure works has been started, the return values maybe changed when you are<a name="line.1354"></a> +<span class="sourceLineNo">1355</span> * processing it so usually this is not safe. Use {@link #getProcedures()} below for most cases as<a name="line.1355"></a> +<span class="sourceLineNo">1356</span> * it will do a copy, and also include the finished procedures.<a name="line.1356"></a> +<span class="sourceLineNo">1357</span> */<a name="line.1357"></a> +<span class="sourceLineNo">1358</span> public Collection<Procedure<TEnvironment>> getActiveProceduresNoCopy() {<a name="line.1358"></a> +<span class="sourceLineNo">1359</span> return procedures.values();<a name="line.1359"></a> +<span class="sourceLineNo">1360</span> }<a name="line.1360"></a> +<span class="sourceLineNo">1361</span><a name="line.1361"></a> +<span class="sourceLineNo">1362</span> /**<a name="line.1362"></a> +<span class="sourceLineNo">1363</span> * Get procedures.<a name="line.1363"></a> +<span class="sourceLineNo">1364</span> * @return the procedures in a list<a name="line.1364"></a> +<span class="sourceLineNo">1365</span> */<a name="line.1365"></a> +<span class="sourceLineNo">1366</span> public List<Procedure<TEnvironment>> getProcedures() {<a name="line.1366"></a> +<span class="sourceLineNo">1367</span> List<Procedure<TEnvironment>> procedureList =<a name="line.1367"></a> +<span class="sourceLineNo">1368</span> new ArrayList<>(procedures.size() + completed.size());<a name="line.1368"></a> +<span class="sourceLineNo">1369</span> procedureList.addAll(procedures.values());<a name="line.1369"></a> +<span class="sourceLineNo">1370</span> // Note: The procedure could show up twice in the list with different state, as<a name="line.1370"></a> +<span class="sourceLineNo">1371</span> // it could complete after we walk through procedures list and insert into<a name="line.1371"></a> +<span class="sourceLineNo">1372</span> // procedureList - it is ok, as we will use the information in the Procedure<a name="line.1372"></a> +<span class="sourceLineNo">1373</span> // to figure it out; to prevent this would increase the complexity of the logic.<a name="line.1373"></a> +<span class="sourceLineNo">1374</span> completed.values().stream().map(CompletedProcedureRetainer::getProcedure)<a name="line.1374"></a> +<span class="sourceLineNo">1375</span> .forEach(procedureList::add);<a name="line.1375"></a> +<span class="sourceLineNo">1376</span> return procedureList;<a name="line.1376"></a> +<span class="sourceLineNo">1377</span> }<a name="line.1377"></a> +<span class="sourceLineNo">1378</span><a name="line.1378"></a> +<span class="sourceLineNo">1379</span> // ==========================================================================<a name="line.1379"></a> +<span class="sourceLineNo">1380</span> // Listeners helpers<a name="line.1380"></a> +<span class="sourceLineNo">1381</span> // ==========================================================================
<TRUNCATED>