http://git-wip-us.apache.org/repos/asf/hbase-site/blob/a949969b/devapidocs/src-html/org/apache/hadoop/hbase/procedure2/ProcedureExecutor.Testing.html ---------------------------------------------------------------------- diff --git a/devapidocs/src-html/org/apache/hadoop/hbase/procedure2/ProcedureExecutor.Testing.html b/devapidocs/src-html/org/apache/hadoop/hbase/procedure2/ProcedureExecutor.Testing.html index a1495ff..0c11099 100644 --- a/devapidocs/src-html/org/apache/hadoop/hbase/procedure2/ProcedureExecutor.Testing.html +++ b/devapidocs/src-html/org/apache/hadoop/hbase/procedure2/ProcedureExecutor.Testing.html @@ -839,1019 +839,1030 @@ <span class="sourceLineNo">831</span> private long pushProcedure(final Procedure proc) {<a name="line.831"></a> <span class="sourceLineNo">832</span> final long currentProcId = proc.getProcId();<a name="line.832"></a> <span class="sourceLineNo">833</span><a name="line.833"></a> -<span class="sourceLineNo">834</span> // Create the rollback stack for the procedure<a name="line.834"></a> -<span class="sourceLineNo">835</span> RootProcedureState stack = new RootProcedureState();<a name="line.835"></a> -<span class="sourceLineNo">836</span> rollbackStack.put(currentProcId, stack);<a name="line.836"></a> -<span class="sourceLineNo">837</span><a name="line.837"></a> -<span class="sourceLineNo">838</span> // Submit the new subprocedures<a name="line.838"></a> -<span class="sourceLineNo">839</span> assert !procedures.containsKey(currentProcId);<a name="line.839"></a> -<span class="sourceLineNo">840</span> procedures.put(currentProcId, proc);<a name="line.840"></a> -<span class="sourceLineNo">841</span> sendProcedureAddedNotification(currentProcId);<a name="line.841"></a> -<span class="sourceLineNo">842</span> scheduler.addBack(proc);<a name="line.842"></a> -<span class="sourceLineNo">843</span> return proc.getProcId();<a name="line.843"></a> -<span class="sourceLineNo">844</span> }<a name="line.844"></a> -<span class="sourceLineNo">845</span><a name="line.845"></a> -<span class="sourceLineNo">846</span> /**<a name="line.846"></a> -<span class="sourceLineNo">847</span> * Send an abort notification the specified procedure.<a name="line.847"></a> -<span class="sourceLineNo">848</span> * Depending on the procedure implementation the abort can be considered or ignored.<a name="line.848"></a> -<span class="sourceLineNo">849</span> * @param procId the procedure to abort<a name="line.849"></a> -<span class="sourceLineNo">850</span> * @return true if the procedure exist and has received the abort, otherwise false.<a name="line.850"></a> -<span class="sourceLineNo">851</span> */<a name="line.851"></a> -<span class="sourceLineNo">852</span> public boolean abort(final long procId) {<a name="line.852"></a> -<span class="sourceLineNo">853</span> return abort(procId, true);<a name="line.853"></a> -<span class="sourceLineNo">854</span> }<a name="line.854"></a> -<span class="sourceLineNo">855</span><a name="line.855"></a> -<span class="sourceLineNo">856</span> /**<a name="line.856"></a> -<span class="sourceLineNo">857</span> * Send an abort notification the specified procedure.<a name="line.857"></a> -<span class="sourceLineNo">858</span> * Depending on the procedure implementation the abort can be considered or ignored.<a name="line.858"></a> -<span class="sourceLineNo">859</span> * @param procId the procedure to abort<a name="line.859"></a> -<span class="sourceLineNo">860</span> * @param mayInterruptIfRunning if the proc completed at least one step, should it be aborted?<a name="line.860"></a> -<span class="sourceLineNo">861</span> * @return true if the procedure exist and has received the abort, otherwise false.<a name="line.861"></a> -<span class="sourceLineNo">862</span> */<a name="line.862"></a> -<span class="sourceLineNo">863</span> public boolean abort(final long procId, final boolean mayInterruptIfRunning) {<a name="line.863"></a> -<span class="sourceLineNo">864</span> final Procedure proc = procedures.get(procId);<a name="line.864"></a> -<span class="sourceLineNo">865</span> if (proc != null) {<a name="line.865"></a> -<span class="sourceLineNo">866</span> if (!mayInterruptIfRunning && proc.wasExecuted()) {<a name="line.866"></a> -<span class="sourceLineNo">867</span> return false;<a name="line.867"></a> -<span class="sourceLineNo">868</span> }<a name="line.868"></a> -<span class="sourceLineNo">869</span> return proc.abort(getEnvironment());<a name="line.869"></a> -<span class="sourceLineNo">870</span> }<a name="line.870"></a> -<span class="sourceLineNo">871</span> return false;<a name="line.871"></a> -<span class="sourceLineNo">872</span> }<a name="line.872"></a> -<span class="sourceLineNo">873</span><a name="line.873"></a> -<span class="sourceLineNo">874</span> // ==========================================================================<a name="line.874"></a> -<span class="sourceLineNo">875</span> // Executor query helpers<a name="line.875"></a> -<span class="sourceLineNo">876</span> // ==========================================================================<a name="line.876"></a> -<span class="sourceLineNo">877</span> public Procedure getProcedure(final long procId) {<a name="line.877"></a> -<span class="sourceLineNo">878</span> return procedures.get(procId);<a name="line.878"></a> -<span class="sourceLineNo">879</span> }<a name="line.879"></a> -<span class="sourceLineNo">880</span><a name="line.880"></a> -<span class="sourceLineNo">881</span> public <T extends Procedure> T getProcedure(final Class<T> clazz, final long procId) {<a name="line.881"></a> -<span class="sourceLineNo">882</span> final Procedure proc = getProcedure(procId);<a name="line.882"></a> -<span class="sourceLineNo">883</span> if (clazz.isInstance(proc)) {<a name="line.883"></a> -<span class="sourceLineNo">884</span> return (T)proc;<a name="line.884"></a> -<span class="sourceLineNo">885</span> }<a name="line.885"></a> -<span class="sourceLineNo">886</span> return null;<a name="line.886"></a> -<span class="sourceLineNo">887</span> }<a name="line.887"></a> -<span class="sourceLineNo">888</span><a name="line.888"></a> -<span class="sourceLineNo">889</span> public ProcedureInfo getResult(final long procId) {<a name="line.889"></a> -<span class="sourceLineNo">890</span> return completed.get(procId);<a name="line.890"></a> -<span class="sourceLineNo">891</span> }<a name="line.891"></a> -<span class="sourceLineNo">892</span><a name="line.892"></a> -<span class="sourceLineNo">893</span> /**<a name="line.893"></a> -<span class="sourceLineNo">894</span> * Return true if the procedure is finished.<a name="line.894"></a> -<span class="sourceLineNo">895</span> * The state may be "completed successfully" or "failed and rolledback".<a name="line.895"></a> -<span class="sourceLineNo">896</span> * Use getResult() to check the state or get the result data.<a name="line.896"></a> -<span class="sourceLineNo">897</span> * @param procId the ID of the procedure to check<a name="line.897"></a> -<span class="sourceLineNo">898</span> * @return true if the procedure execution is finished, otherwise false.<a name="line.898"></a> -<span class="sourceLineNo">899</span> */<a name="line.899"></a> -<span class="sourceLineNo">900</span> public boolean isFinished(final long procId) {<a name="line.900"></a> -<span class="sourceLineNo">901</span> return !procedures.containsKey(procId);<a name="line.901"></a> -<span class="sourceLineNo">902</span> }<a name="line.902"></a> -<span class="sourceLineNo">903</span><a name="line.903"></a> -<span class="sourceLineNo">904</span> /**<a name="line.904"></a> -<span class="sourceLineNo">905</span> * Return true if the procedure is started.<a name="line.905"></a> -<span class="sourceLineNo">906</span> * @param procId the ID of the procedure to check<a name="line.906"></a> -<span class="sourceLineNo">907</span> * @return true if the procedure execution is started, otherwise false.<a name="line.907"></a> -<span class="sourceLineNo">908</span> */<a name="line.908"></a> -<span class="sourceLineNo">909</span> public boolean isStarted(final long procId) {<a name="line.909"></a> -<span class="sourceLineNo">910</span> final Procedure proc = procedures.get(procId);<a name="line.910"></a> -<span class="sourceLineNo">911</span> if (proc == null) {<a name="line.911"></a> -<span class="sourceLineNo">912</span> return completed.get(procId) != null;<a name="line.912"></a> -<span class="sourceLineNo">913</span> }<a name="line.913"></a> -<span class="sourceLineNo">914</span> return proc.wasExecuted();<a name="line.914"></a> -<span class="sourceLineNo">915</span> }<a name="line.915"></a> -<span class="sourceLineNo">916</span><a name="line.916"></a> -<span class="sourceLineNo">917</span> /**<a name="line.917"></a> -<span class="sourceLineNo">918</span> * Mark the specified completed procedure, as ready to remove.<a name="line.918"></a> -<span class="sourceLineNo">919</span> * @param procId the ID of the procedure to remove<a name="line.919"></a> -<span class="sourceLineNo">920</span> */<a name="line.920"></a> -<span class="sourceLineNo">921</span> public void removeResult(final long procId) {<a name="line.921"></a> -<span class="sourceLineNo">922</span> final ProcedureInfo result = completed.get(procId);<a name="line.922"></a> -<span class="sourceLineNo">923</span> if (result == null) {<a name="line.923"></a> -<span class="sourceLineNo">924</span> assert !procedures.containsKey(procId) : "procId=" + procId + " is still running";<a name="line.924"></a> -<span class="sourceLineNo">925</span> if (LOG.isDebugEnabled()) {<a name="line.925"></a> -<span class="sourceLineNo">926</span> LOG.debug("procId=" + procId + " already removed by the cleaner.");<a name="line.926"></a> -<span class="sourceLineNo">927</span> }<a name="line.927"></a> -<span class="sourceLineNo">928</span> return;<a name="line.928"></a> -<span class="sourceLineNo">929</span> }<a name="line.929"></a> -<span class="sourceLineNo">930</span><a name="line.930"></a> -<span class="sourceLineNo">931</span> // The CompletedProcedureCleaner will take care of deletion, once the TTL is expired.<a name="line.931"></a> -<span class="sourceLineNo">932</span> result.setClientAckTime(EnvironmentEdgeManager.currentTime());<a name="line.932"></a> -<span class="sourceLineNo">933</span> }<a name="line.933"></a> -<span class="sourceLineNo">934</span><a name="line.934"></a> -<span class="sourceLineNo">935</span> public Pair<ProcedureInfo, Procedure> getResultOrProcedure(final long procId) {<a name="line.935"></a> -<span class="sourceLineNo">936</span> ProcedureInfo result = completed.get(procId);<a name="line.936"></a> -<span class="sourceLineNo">937</span> Procedure proc = null;<a name="line.937"></a> -<span class="sourceLineNo">938</span> if (result == null) {<a name="line.938"></a> -<span class="sourceLineNo">939</span> proc = procedures.get(procId);<a name="line.939"></a> -<span class="sourceLineNo">940</span> if (proc == null) {<a name="line.940"></a> -<span class="sourceLineNo">941</span> result = completed.get(procId);<a name="line.941"></a> -<span class="sourceLineNo">942</span> }<a name="line.942"></a> -<span class="sourceLineNo">943</span> }<a name="line.943"></a> -<span class="sourceLineNo">944</span> return new Pair(result, proc);<a name="line.944"></a> -<span class="sourceLineNo">945</span> }<a name="line.945"></a> -<span class="sourceLineNo">946</span><a name="line.946"></a> -<span class="sourceLineNo">947</span> /**<a name="line.947"></a> -<span class="sourceLineNo">948</span> * Check if the user is this procedure's owner<a name="line.948"></a> -<span class="sourceLineNo">949</span> * @param procId the target procedure<a name="line.949"></a> -<span class="sourceLineNo">950</span> * @param user the user<a name="line.950"></a> -<span class="sourceLineNo">951</span> * @return true if the user is the owner of the procedure,<a name="line.951"></a> -<span class="sourceLineNo">952</span> * false otherwise or the owner is unknown.<a name="line.952"></a> -<span class="sourceLineNo">953</span> */<a name="line.953"></a> -<span class="sourceLineNo">954</span> public boolean isProcedureOwner(final long procId, final User user) {<a name="line.954"></a> -<span class="sourceLineNo">955</span> if (user == null) return false;<a name="line.955"></a> -<span class="sourceLineNo">956</span><a name="line.956"></a> -<span class="sourceLineNo">957</span> final Procedure proc = procedures.get(procId);<a name="line.957"></a> -<span class="sourceLineNo">958</span> if (proc != null) {<a name="line.958"></a> -<span class="sourceLineNo">959</span> return proc.getOwner().equals(user.getShortName());<a name="line.959"></a> -<span class="sourceLineNo">960</span> }<a name="line.960"></a> -<span class="sourceLineNo">961</span><a name="line.961"></a> -<span class="sourceLineNo">962</span> final ProcedureInfo procInfo = completed.get(procId);<a name="line.962"></a> -<span class="sourceLineNo">963</span> if (procInfo == null) {<a name="line.963"></a> -<span class="sourceLineNo">964</span> // Procedure either does not exist or has already completed and got cleaned up.<a name="line.964"></a> -<span class="sourceLineNo">965</span> // At this time, we cannot check the owner of the procedure<a name="line.965"></a> -<span class="sourceLineNo">966</span> return false;<a name="line.966"></a> -<span class="sourceLineNo">967</span> }<a name="line.967"></a> -<span class="sourceLineNo">968</span> return ProcedureInfo.isProcedureOwner(procInfo, user);<a name="line.968"></a> -<span class="sourceLineNo">969</span> }<a name="line.969"></a> -<span class="sourceLineNo">970</span><a name="line.970"></a> -<span class="sourceLineNo">971</span> /**<a name="line.971"></a> -<span class="sourceLineNo">972</span> * List procedures.<a name="line.972"></a> -<span class="sourceLineNo">973</span> * @return the procedures in a list<a name="line.973"></a> -<span class="sourceLineNo">974</span> */<a name="line.974"></a> -<span class="sourceLineNo">975</span> public List<ProcedureInfo> listProcedures() {<a name="line.975"></a> -<span class="sourceLineNo">976</span> final List<ProcedureInfo> procedureLists = new ArrayList<>(procedures.size() + completed.size());<a name="line.976"></a> -<span class="sourceLineNo">977</span> for (Map.Entry<Long, Procedure> p: procedures.entrySet()) {<a name="line.977"></a> -<span class="sourceLineNo">978</span> procedureLists.add(ProcedureUtil.convertToProcedureInfo(p.getValue()));<a name="line.978"></a> -<span class="sourceLineNo">979</span> }<a name="line.979"></a> -<span class="sourceLineNo">980</span> for (Map.Entry<Long, ProcedureInfo> e: completed.entrySet()) {<a name="line.980"></a> -<span class="sourceLineNo">981</span> // Note: The procedure could show up twice in the list with different state, as<a name="line.981"></a> -<span class="sourceLineNo">982</span> // it could complete after we walk through procedures list and insert into<a name="line.982"></a> -<span class="sourceLineNo">983</span> // procedureList - it is ok, as we will use the information in the ProcedureInfo<a name="line.983"></a> -<span class="sourceLineNo">984</span> // to figure it out; to prevent this would increase the complexity of the logic.<a name="line.984"></a> -<span class="sourceLineNo">985</span> procedureLists.add(e.getValue());<a name="line.985"></a> -<span class="sourceLineNo">986</span> }<a name="line.986"></a> -<span class="sourceLineNo">987</span> return procedureLists;<a name="line.987"></a> -<span class="sourceLineNo">988</span> }<a name="line.988"></a> -<span class="sourceLineNo">989</span><a name="line.989"></a> -<span class="sourceLineNo">990</span> // ==========================================================================<a name="line.990"></a> -<span class="sourceLineNo">991</span> // Listeners helpers<a name="line.991"></a> -<span class="sourceLineNo">992</span> // ==========================================================================<a name="line.992"></a> -<span class="sourceLineNo">993</span> public void registerListener(ProcedureExecutorListener listener) {<a name="line.993"></a> -<span class="sourceLineNo">994</span> this.listeners.add(listener);<a name="line.994"></a> -<span class="sourceLineNo">995</span> }<a name="line.995"></a> -<span class="sourceLineNo">996</span><a name="line.996"></a> -<span class="sourceLineNo">997</span> public boolean unregisterListener(ProcedureExecutorListener listener) {<a name="line.997"></a> -<span class="sourceLineNo">998</span> return this.listeners.remove(listener);<a name="line.998"></a> -<span class="sourceLineNo">999</span> }<a name="line.999"></a> -<span class="sourceLineNo">1000</span><a name="line.1000"></a> -<span class="sourceLineNo">1001</span> private void sendProcedureLoadedNotification(final long procId) {<a name="line.1001"></a> -<span class="sourceLineNo">1002</span> if (!this.listeners.isEmpty()) {<a name="line.1002"></a> -<span class="sourceLineNo">1003</span> for (ProcedureExecutorListener listener: this.listeners) {<a name="line.1003"></a> -<span class="sourceLineNo">1004</span> try {<a name="line.1004"></a> -<span class="sourceLineNo">1005</span> listener.procedureLoaded(procId);<a name="line.1005"></a> -<span class="sourceLineNo">1006</span> } catch (Throwable e) {<a name="line.1006"></a> -<span class="sourceLineNo">1007</span> LOG.error("Listener " + listener + " had an error: " + e.getMessage(), e);<a name="line.1007"></a> -<span class="sourceLineNo">1008</span> }<a name="line.1008"></a> -<span class="sourceLineNo">1009</span> }<a name="line.1009"></a> -<span class="sourceLineNo">1010</span> }<a name="line.1010"></a> -<span class="sourceLineNo">1011</span> }<a name="line.1011"></a> -<span class="sourceLineNo">1012</span><a name="line.1012"></a> -<span class="sourceLineNo">1013</span> private void sendProcedureAddedNotification(final long procId) {<a name="line.1013"></a> -<span class="sourceLineNo">1014</span> if (!this.listeners.isEmpty()) {<a name="line.1014"></a> -<span class="sourceLineNo">1015</span> for (ProcedureExecutorListener listener: this.listeners) {<a name="line.1015"></a> -<span class="sourceLineNo">1016</span> try {<a name="line.1016"></a> -<span class="sourceLineNo">1017</span> listener.procedureAdded(procId);<a name="line.1017"></a> -<span class="sourceLineNo">1018</span> } catch (Throwable e) {<a name="line.1018"></a> -<span class="sourceLineNo">1019</span> LOG.error("Listener " + listener + " had an error: " + e.getMessage(), e);<a name="line.1019"></a> -<span class="sourceLineNo">1020</span> }<a name="line.1020"></a> -<span class="sourceLineNo">1021</span> }<a name="line.1021"></a> -<span class="sourceLineNo">1022</span> }<a name="line.1022"></a> -<span class="sourceLineNo">1023</span> }<a name="line.1023"></a> -<span class="sourceLineNo">1024</span><a name="line.1024"></a> -<span class="sourceLineNo">1025</span> private void sendProcedureFinishedNotification(final long procId) {<a name="line.1025"></a> -<span class="sourceLineNo">1026</span> if (!this.listeners.isEmpty()) {<a name="line.1026"></a> -<span class="sourceLineNo">1027</span> for (ProcedureExecutorListener listener: this.listeners) {<a name="line.1027"></a> -<span class="sourceLineNo">1028</span> try {<a name="line.1028"></a> -<span class="sourceLineNo">1029</span> listener.procedureFinished(procId);<a name="line.1029"></a> -<span class="sourceLineNo">1030</span> } catch (Throwable e) {<a name="line.1030"></a> -<span class="sourceLineNo">1031</span> LOG.error("Listener " + listener + " had an error: " + e.getMessage(), e);<a name="line.1031"></a> -<span class="sourceLineNo">1032</span> }<a name="line.1032"></a> -<span class="sourceLineNo">1033</span> }<a name="line.1033"></a> -<span class="sourceLineNo">1034</span> }<a name="line.1034"></a> -<span class="sourceLineNo">1035</span> }<a name="line.1035"></a> -<span class="sourceLineNo">1036</span><a name="line.1036"></a> -<span class="sourceLineNo">1037</span> // ==========================================================================<a name="line.1037"></a> -<span class="sourceLineNo">1038</span> // Procedure IDs helpers<a name="line.1038"></a> -<span class="sourceLineNo">1039</span> // ==========================================================================<a name="line.1039"></a> -<span class="sourceLineNo">1040</span> private long nextProcId() {<a name="line.1040"></a> -<span class="sourceLineNo">1041</span> long procId = lastProcId.incrementAndGet();<a name="line.1041"></a> -<span class="sourceLineNo">1042</span> if (procId < 0) {<a name="line.1042"></a> -<span class="sourceLineNo">1043</span> while (!lastProcId.compareAndSet(procId, 0)) {<a name="line.1043"></a> -<span class="sourceLineNo">1044</span> procId = lastProcId.get();<a name="line.1044"></a> -<span class="sourceLineNo">1045</span> if (procId >= 0)<a name="line.1045"></a> -<span class="sourceLineNo">1046</span> break;<a name="line.1046"></a> -<span class="sourceLineNo">1047</span> }<a name="line.1047"></a> -<span class="sourceLineNo">1048</span> while (procedures.containsKey(procId)) {<a name="line.1048"></a> -<span class="sourceLineNo">1049</span> procId = lastProcId.incrementAndGet();<a name="line.1049"></a> +<span class="sourceLineNo">834</span> // Update metrics on start of a procedure<a name="line.834"></a> +<span class="sourceLineNo">835</span> proc.updateMetricsOnSubmit(getEnvironment());<a name="line.835"></a> +<span class="sourceLineNo">836</span><a name="line.836"></a> +<span class="sourceLineNo">837</span> // Create the rollback stack for the procedure<a name="line.837"></a> +<span class="sourceLineNo">838</span> RootProcedureState stack = new RootProcedureState();<a name="line.838"></a> +<span class="sourceLineNo">839</span> rollbackStack.put(currentProcId, stack);<a name="line.839"></a> +<span class="sourceLineNo">840</span><a name="line.840"></a> +<span class="sourceLineNo">841</span> // Submit the new subprocedures<a name="line.841"></a> +<span class="sourceLineNo">842</span> assert !procedures.containsKey(currentProcId);<a name="line.842"></a> +<span class="sourceLineNo">843</span> procedures.put(currentProcId, proc);<a name="line.843"></a> +<span class="sourceLineNo">844</span> sendProcedureAddedNotification(currentProcId);<a name="line.844"></a> +<span class="sourceLineNo">845</span> scheduler.addBack(proc);<a name="line.845"></a> +<span class="sourceLineNo">846</span> return proc.getProcId();<a name="line.846"></a> +<span class="sourceLineNo">847</span> }<a name="line.847"></a> +<span class="sourceLineNo">848</span><a name="line.848"></a> +<span class="sourceLineNo">849</span> /**<a name="line.849"></a> +<span class="sourceLineNo">850</span> * Send an abort notification the specified procedure.<a name="line.850"></a> +<span class="sourceLineNo">851</span> * Depending on the procedure implementation the abort can be considered or ignored.<a name="line.851"></a> +<span class="sourceLineNo">852</span> * @param procId the procedure to abort<a name="line.852"></a> +<span class="sourceLineNo">853</span> * @return true if the procedure exist and has received the abort, otherwise false.<a name="line.853"></a> +<span class="sourceLineNo">854</span> */<a name="line.854"></a> +<span class="sourceLineNo">855</span> public boolean abort(final long procId) {<a name="line.855"></a> +<span class="sourceLineNo">856</span> return abort(procId, true);<a name="line.856"></a> +<span class="sourceLineNo">857</span> }<a name="line.857"></a> +<span class="sourceLineNo">858</span><a name="line.858"></a> +<span class="sourceLineNo">859</span> /**<a name="line.859"></a> +<span class="sourceLineNo">860</span> * Send an abort notification the specified procedure.<a name="line.860"></a> +<span class="sourceLineNo">861</span> * Depending on the procedure implementation the abort can be considered or ignored.<a name="line.861"></a> +<span class="sourceLineNo">862</span> * @param procId the procedure to abort<a name="line.862"></a> +<span class="sourceLineNo">863</span> * @param mayInterruptIfRunning if the proc completed at least one step, should it be aborted?<a name="line.863"></a> +<span class="sourceLineNo">864</span> * @return true if the procedure exist and has received the abort, otherwise false.<a name="line.864"></a> +<span class="sourceLineNo">865</span> */<a name="line.865"></a> +<span class="sourceLineNo">866</span> public boolean abort(final long procId, final boolean mayInterruptIfRunning) {<a name="line.866"></a> +<span class="sourceLineNo">867</span> final Procedure proc = procedures.get(procId);<a name="line.867"></a> +<span class="sourceLineNo">868</span> if (proc != null) {<a name="line.868"></a> +<span class="sourceLineNo">869</span> if (!mayInterruptIfRunning && proc.wasExecuted()) {<a name="line.869"></a> +<span class="sourceLineNo">870</span> return false;<a name="line.870"></a> +<span class="sourceLineNo">871</span> }<a name="line.871"></a> +<span class="sourceLineNo">872</span> return proc.abort(getEnvironment());<a name="line.872"></a> +<span class="sourceLineNo">873</span> }<a name="line.873"></a> +<span class="sourceLineNo">874</span> return false;<a name="line.874"></a> +<span class="sourceLineNo">875</span> }<a name="line.875"></a> +<span class="sourceLineNo">876</span><a name="line.876"></a> +<span class="sourceLineNo">877</span> // ==========================================================================<a name="line.877"></a> +<span class="sourceLineNo">878</span> // Executor query helpers<a name="line.878"></a> +<span class="sourceLineNo">879</span> // ==========================================================================<a name="line.879"></a> +<span class="sourceLineNo">880</span> public Procedure getProcedure(final long procId) {<a name="line.880"></a> +<span class="sourceLineNo">881</span> return procedures.get(procId);<a name="line.881"></a> +<span class="sourceLineNo">882</span> }<a name="line.882"></a> +<span class="sourceLineNo">883</span><a name="line.883"></a> +<span class="sourceLineNo">884</span> public <T extends Procedure> T getProcedure(final Class<T> clazz, final long procId) {<a name="line.884"></a> +<span class="sourceLineNo">885</span> final Procedure proc = getProcedure(procId);<a name="line.885"></a> +<span class="sourceLineNo">886</span> if (clazz.isInstance(proc)) {<a name="line.886"></a> +<span class="sourceLineNo">887</span> return (T)proc;<a name="line.887"></a> +<span class="sourceLineNo">888</span> }<a name="line.888"></a> +<span class="sourceLineNo">889</span> return null;<a name="line.889"></a> +<span class="sourceLineNo">890</span> }<a name="line.890"></a> +<span class="sourceLineNo">891</span><a name="line.891"></a> +<span class="sourceLineNo">892</span> public ProcedureInfo getResult(final long procId) {<a name="line.892"></a> +<span class="sourceLineNo">893</span> return completed.get(procId);<a name="line.893"></a> +<span class="sourceLineNo">894</span> }<a name="line.894"></a> +<span class="sourceLineNo">895</span><a name="line.895"></a> +<span class="sourceLineNo">896</span> /**<a name="line.896"></a> +<span class="sourceLineNo">897</span> * Return true if the procedure is finished.<a name="line.897"></a> +<span class="sourceLineNo">898</span> * The state may be "completed successfully" or "failed and rolledback".<a name="line.898"></a> +<span class="sourceLineNo">899</span> * Use getResult() to check the state or get the result data.<a name="line.899"></a> +<span class="sourceLineNo">900</span> * @param procId the ID of the procedure to check<a name="line.900"></a> +<span class="sourceLineNo">901</span> * @return true if the procedure execution is finished, otherwise false.<a name="line.901"></a> +<span class="sourceLineNo">902</span> */<a name="line.902"></a> +<span class="sourceLineNo">903</span> public boolean isFinished(final long procId) {<a name="line.903"></a> +<span class="sourceLineNo">904</span> return !procedures.containsKey(procId);<a name="line.904"></a> +<span class="sourceLineNo">905</span> }<a name="line.905"></a> +<span class="sourceLineNo">906</span><a name="line.906"></a> +<span class="sourceLineNo">907</span> /**<a name="line.907"></a> +<span class="sourceLineNo">908</span> * Return true if the procedure is started.<a name="line.908"></a> +<span class="sourceLineNo">909</span> * @param procId the ID of the procedure to check<a name="line.909"></a> +<span class="sourceLineNo">910</span> * @return true if the procedure execution is started, otherwise false.<a name="line.910"></a> +<span class="sourceLineNo">911</span> */<a name="line.911"></a> +<span class="sourceLineNo">912</span> public boolean isStarted(final long procId) {<a name="line.912"></a> +<span class="sourceLineNo">913</span> final Procedure proc = procedures.get(procId);<a name="line.913"></a> +<span class="sourceLineNo">914</span> if (proc == null) {<a name="line.914"></a> +<span class="sourceLineNo">915</span> return completed.get(procId) != null;<a name="line.915"></a> +<span class="sourceLineNo">916</span> }<a name="line.916"></a> +<span class="sourceLineNo">917</span> return proc.wasExecuted();<a name="line.917"></a> +<span class="sourceLineNo">918</span> }<a name="line.918"></a> +<span class="sourceLineNo">919</span><a name="line.919"></a> +<span class="sourceLineNo">920</span> /**<a name="line.920"></a> +<span class="sourceLineNo">921</span> * Mark the specified completed procedure, as ready to remove.<a name="line.921"></a> +<span class="sourceLineNo">922</span> * @param procId the ID of the procedure to remove<a name="line.922"></a> +<span class="sourceLineNo">923</span> */<a name="line.923"></a> +<span class="sourceLineNo">924</span> public void removeResult(final long procId) {<a name="line.924"></a> +<span class="sourceLineNo">925</span> final ProcedureInfo result = completed.get(procId);<a name="line.925"></a> +<span class="sourceLineNo">926</span> if (result == null) {<a name="line.926"></a> +<span class="sourceLineNo">927</span> assert !procedures.containsKey(procId) : "procId=" + procId + " is still running";<a name="line.927"></a> +<span class="sourceLineNo">928</span> if (LOG.isDebugEnabled()) {<a name="line.928"></a> +<span class="sourceLineNo">929</span> LOG.debug("procId=" + procId + " already removed by the cleaner.");<a name="line.929"></a> +<span class="sourceLineNo">930</span> }<a name="line.930"></a> +<span class="sourceLineNo">931</span> return;<a name="line.931"></a> +<span class="sourceLineNo">932</span> }<a name="line.932"></a> +<span class="sourceLineNo">933</span><a name="line.933"></a> +<span class="sourceLineNo">934</span> // The CompletedProcedureCleaner will take care of deletion, once the TTL is expired.<a name="line.934"></a> +<span class="sourceLineNo">935</span> result.setClientAckTime(EnvironmentEdgeManager.currentTime());<a name="line.935"></a> +<span class="sourceLineNo">936</span> }<a name="line.936"></a> +<span class="sourceLineNo">937</span><a name="line.937"></a> +<span class="sourceLineNo">938</span> public Pair<ProcedureInfo, Procedure> getResultOrProcedure(final long procId) {<a name="line.938"></a> +<span class="sourceLineNo">939</span> ProcedureInfo result = completed.get(procId);<a name="line.939"></a> +<span class="sourceLineNo">940</span> Procedure proc = null;<a name="line.940"></a> +<span class="sourceLineNo">941</span> if (result == null) {<a name="line.941"></a> +<span class="sourceLineNo">942</span> proc = procedures.get(procId);<a name="line.942"></a> +<span class="sourceLineNo">943</span> if (proc == null) {<a name="line.943"></a> +<span class="sourceLineNo">944</span> result = completed.get(procId);<a name="line.944"></a> +<span class="sourceLineNo">945</span> }<a name="line.945"></a> +<span class="sourceLineNo">946</span> }<a name="line.946"></a> +<span class="sourceLineNo">947</span> return new Pair(result, proc);<a name="line.947"></a> +<span class="sourceLineNo">948</span> }<a name="line.948"></a> +<span class="sourceLineNo">949</span><a name="line.949"></a> +<span class="sourceLineNo">950</span> /**<a name="line.950"></a> +<span class="sourceLineNo">951</span> * Check if the user is this procedure's owner<a name="line.951"></a> +<span class="sourceLineNo">952</span> * @param procId the target procedure<a name="line.952"></a> +<span class="sourceLineNo">953</span> * @param user the user<a name="line.953"></a> +<span class="sourceLineNo">954</span> * @return true if the user is the owner of the procedure,<a name="line.954"></a> +<span class="sourceLineNo">955</span> * false otherwise or the owner is unknown.<a name="line.955"></a> +<span class="sourceLineNo">956</span> */<a name="line.956"></a> +<span class="sourceLineNo">957</span> public boolean isProcedureOwner(final long procId, final User user) {<a name="line.957"></a> +<span class="sourceLineNo">958</span> if (user == null) return false;<a name="line.958"></a> +<span class="sourceLineNo">959</span><a name="line.959"></a> +<span class="sourceLineNo">960</span> final Procedure proc = procedures.get(procId);<a name="line.960"></a> +<span class="sourceLineNo">961</span> if (proc != null) {<a name="line.961"></a> +<span class="sourceLineNo">962</span> return proc.getOwner().equals(user.getShortName());<a name="line.962"></a> +<span class="sourceLineNo">963</span> }<a name="line.963"></a> +<span class="sourceLineNo">964</span><a name="line.964"></a> +<span class="sourceLineNo">965</span> final ProcedureInfo procInfo = completed.get(procId);<a name="line.965"></a> +<span class="sourceLineNo">966</span> if (procInfo == null) {<a name="line.966"></a> +<span class="sourceLineNo">967</span> // Procedure either does not exist or has already completed and got cleaned up.<a name="line.967"></a> +<span class="sourceLineNo">968</span> // At this time, we cannot check the owner of the procedure<a name="line.968"></a> +<span class="sourceLineNo">969</span> return false;<a name="line.969"></a> +<span class="sourceLineNo">970</span> }<a name="line.970"></a> +<span class="sourceLineNo">971</span> return ProcedureInfo.isProcedureOwner(procInfo, user);<a name="line.971"></a> +<span class="sourceLineNo">972</span> }<a name="line.972"></a> +<span class="sourceLineNo">973</span><a name="line.973"></a> +<span class="sourceLineNo">974</span> /**<a name="line.974"></a> +<span class="sourceLineNo">975</span> * List procedures.<a name="line.975"></a> +<span class="sourceLineNo">976</span> * @return the procedures in a list<a name="line.976"></a> +<span class="sourceLineNo">977</span> */<a name="line.977"></a> +<span class="sourceLineNo">978</span> public List<ProcedureInfo> listProcedures() {<a name="line.978"></a> +<span class="sourceLineNo">979</span> final List<ProcedureInfo> procedureLists = new ArrayList<>(procedures.size() + completed.size());<a name="line.979"></a> +<span class="sourceLineNo">980</span> for (Map.Entry<Long, Procedure> p: procedures.entrySet()) {<a name="line.980"></a> +<span class="sourceLineNo">981</span> procedureLists.add(ProcedureUtil.convertToProcedureInfo(p.getValue()));<a name="line.981"></a> +<span class="sourceLineNo">982</span> }<a name="line.982"></a> +<span class="sourceLineNo">983</span> for (Map.Entry<Long, ProcedureInfo> e: completed.entrySet()) {<a name="line.983"></a> +<span class="sourceLineNo">984</span> // Note: The procedure could show up twice in the list with different state, as<a name="line.984"></a> +<span class="sourceLineNo">985</span> // it could complete after we walk through procedures list and insert into<a name="line.985"></a> +<span class="sourceLineNo">986</span> // procedureList - it is ok, as we will use the information in the ProcedureInfo<a name="line.986"></a> +<span class="sourceLineNo">987</span> // to figure it out; to prevent this would increase the complexity of the logic.<a name="line.987"></a> +<span class="sourceLineNo">988</span> procedureLists.add(e.getValue());<a name="line.988"></a> +<span class="sourceLineNo">989</span> }<a name="line.989"></a> +<span class="sourceLineNo">990</span> return procedureLists;<a name="line.990"></a> +<span class="sourceLineNo">991</span> }<a name="line.991"></a> +<span class="sourceLineNo">992</span><a name="line.992"></a> +<span class="sourceLineNo">993</span> // ==========================================================================<a name="line.993"></a> +<span class="sourceLineNo">994</span> // Listeners helpers<a name="line.994"></a> +<span class="sourceLineNo">995</span> // ==========================================================================<a name="line.995"></a> +<span class="sourceLineNo">996</span> public void registerListener(ProcedureExecutorListener listener) {<a name="line.996"></a> +<span class="sourceLineNo">997</span> this.listeners.add(listener);<a name="line.997"></a> +<span class="sourceLineNo">998</span> }<a name="line.998"></a> +<span class="sourceLineNo">999</span><a name="line.999"></a> +<span class="sourceLineNo">1000</span> public boolean unregisterListener(ProcedureExecutorListener listener) {<a name="line.1000"></a> +<span class="sourceLineNo">1001</span> return this.listeners.remove(listener);<a name="line.1001"></a> +<span class="sourceLineNo">1002</span> }<a name="line.1002"></a> +<span class="sourceLineNo">1003</span><a name="line.1003"></a> +<span class="sourceLineNo">1004</span> private void sendProcedureLoadedNotification(final long procId) {<a name="line.1004"></a> +<span class="sourceLineNo">1005</span> if (!this.listeners.isEmpty()) {<a name="line.1005"></a> +<span class="sourceLineNo">1006</span> for (ProcedureExecutorListener listener: this.listeners) {<a name="line.1006"></a> +<span class="sourceLineNo">1007</span> try {<a name="line.1007"></a> +<span class="sourceLineNo">1008</span> listener.procedureLoaded(procId);<a name="line.1008"></a> +<span class="sourceLineNo">1009</span> } catch (Throwable e) {<a name="line.1009"></a> +<span class="sourceLineNo">1010</span> LOG.error("Listener " + listener + " had an error: " + e.getMessage(), e);<a name="line.1010"></a> +<span class="sourceLineNo">1011</span> }<a name="line.1011"></a> +<span class="sourceLineNo">1012</span> }<a name="line.1012"></a> +<span class="sourceLineNo">1013</span> }<a name="line.1013"></a> +<span class="sourceLineNo">1014</span> }<a name="line.1014"></a> +<span class="sourceLineNo">1015</span><a name="line.1015"></a> +<span class="sourceLineNo">1016</span> private void sendProcedureAddedNotification(final long procId) {<a name="line.1016"></a> +<span class="sourceLineNo">1017</span> if (!this.listeners.isEmpty()) {<a name="line.1017"></a> +<span class="sourceLineNo">1018</span> for (ProcedureExecutorListener listener: this.listeners) {<a name="line.1018"></a> +<span class="sourceLineNo">1019</span> try {<a name="line.1019"></a> +<span class="sourceLineNo">1020</span> listener.procedureAdded(procId);<a name="line.1020"></a> +<span class="sourceLineNo">1021</span> } catch (Throwable e) {<a name="line.1021"></a> +<span class="sourceLineNo">1022</span> LOG.error("Listener " + listener + " had an error: " + e.getMessage(), e);<a name="line.1022"></a> +<span class="sourceLineNo">1023</span> }<a name="line.1023"></a> +<span class="sourceLineNo">1024</span> }<a name="line.1024"></a> +<span class="sourceLineNo">1025</span> }<a name="line.1025"></a> +<span class="sourceLineNo">1026</span> }<a name="line.1026"></a> +<span class="sourceLineNo">1027</span><a name="line.1027"></a> +<span class="sourceLineNo">1028</span> private void sendProcedureFinishedNotification(final long procId) {<a name="line.1028"></a> +<span class="sourceLineNo">1029</span> if (!this.listeners.isEmpty()) {<a name="line.1029"></a> +<span class="sourceLineNo">1030</span> for (ProcedureExecutorListener listener: this.listeners) {<a name="line.1030"></a> +<span class="sourceLineNo">1031</span> try {<a name="line.1031"></a> +<span class="sourceLineNo">1032</span> listener.procedureFinished(procId);<a name="line.1032"></a> +<span class="sourceLineNo">1033</span> } catch (Throwable e) {<a name="line.1033"></a> +<span class="sourceLineNo">1034</span> LOG.error("Listener " + listener + " had an error: " + e.getMessage(), e);<a name="line.1034"></a> +<span class="sourceLineNo">1035</span> }<a name="line.1035"></a> +<span class="sourceLineNo">1036</span> }<a name="line.1036"></a> +<span class="sourceLineNo">1037</span> }<a name="line.1037"></a> +<span class="sourceLineNo">1038</span> }<a name="line.1038"></a> +<span class="sourceLineNo">1039</span><a name="line.1039"></a> +<span class="sourceLineNo">1040</span> // ==========================================================================<a name="line.1040"></a> +<span class="sourceLineNo">1041</span> // Procedure IDs helpers<a name="line.1041"></a> +<span class="sourceLineNo">1042</span> // ==========================================================================<a name="line.1042"></a> +<span class="sourceLineNo">1043</span> private long nextProcId() {<a name="line.1043"></a> +<span class="sourceLineNo">1044</span> long procId = lastProcId.incrementAndGet();<a name="line.1044"></a> +<span class="sourceLineNo">1045</span> if (procId < 0) {<a name="line.1045"></a> +<span class="sourceLineNo">1046</span> while (!lastProcId.compareAndSet(procId, 0)) {<a name="line.1046"></a> +<span class="sourceLineNo">1047</span> procId = lastProcId.get();<a name="line.1047"></a> +<span class="sourceLineNo">1048</span> if (procId >= 0)<a name="line.1048"></a> +<span class="sourceLineNo">1049</span> break;<a name="line.1049"></a> <span class="sourceLineNo">1050</span> }<a name="line.1050"></a> -<span class="sourceLineNo">1051</span> }<a name="line.1051"></a> -<span class="sourceLineNo">1052</span> assert procId >= 0 : "Invalid procId " + procId;<a name="line.1052"></a> -<span class="sourceLineNo">1053</span> return procId;<a name="line.1053"></a> -<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> @VisibleForTesting<a name="line.1056"></a> -<span class="sourceLineNo">1057</span> protected long getLastProcId() {<a name="line.1057"></a> -<span class="sourceLineNo">1058</span> return lastProcId.get();<a name="line.1058"></a> -<span class="sourceLineNo">1059</span> }<a name="line.1059"></a> -<span class="sourceLineNo">1060</span><a name="line.1060"></a> -<span class="sourceLineNo">1061</span> @VisibleForTesting<a name="line.1061"></a> -<span class="sourceLineNo">1062</span> public Set<Long> getActiveProcIds() {<a name="line.1062"></a> -<span class="sourceLineNo">1063</span> return procedures.keySet();<a name="line.1063"></a> -<span class="sourceLineNo">1064</span> }<a name="line.1064"></a> -<span class="sourceLineNo">1065</span><a name="line.1065"></a> -<span class="sourceLineNo">1066</span> private Long getRootProcedureId(Procedure proc) {<a name="line.1066"></a> -<span class="sourceLineNo">1067</span> return Procedure.getRootProcedureId(procedures, proc);<a name="line.1067"></a> -<span class="sourceLineNo">1068</span> }<a name="line.1068"></a> -<span class="sourceLineNo">1069</span><a name="line.1069"></a> -<span class="sourceLineNo">1070</span> // ==========================================================================<a name="line.1070"></a> -<span class="sourceLineNo">1071</span> // Executions<a name="line.1071"></a> -<span class="sourceLineNo">1072</span> // ==========================================================================<a name="line.1072"></a> -<span class="sourceLineNo">1073</span> private void executeProcedure(final Procedure proc) {<a name="line.1073"></a> -<span class="sourceLineNo">1074</span> final Long rootProcId = getRootProcedureId(proc);<a name="line.1074"></a> -<span class="sourceLineNo">1075</span> if (rootProcId == null) {<a name="line.1075"></a> -<span class="sourceLineNo">1076</span> // The 'proc' was ready to run but the root procedure was rolledback<a name="line.1076"></a> -<span class="sourceLineNo">1077</span> executeRollback(proc);<a name="line.1077"></a> -<span class="sourceLineNo">1078</span> return;<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> final RootProcedureState procStack = rollbackStack.get(rootProcId);<a name="line.1081"></a> -<span class="sourceLineNo">1082</span> if (procStack == null) return;<a name="line.1082"></a> +<span class="sourceLineNo">1051</span> while (procedures.containsKey(procId)) {<a name="line.1051"></a> +<span class="sourceLineNo">1052</span> procId = lastProcId.incrementAndGet();<a name="line.1052"></a> +<span class="sourceLineNo">1053</span> }<a name="line.1053"></a> +<span class="sourceLineNo">1054</span> }<a name="line.1054"></a> +<span class="sourceLineNo">1055</span> assert procId >= 0 : "Invalid procId " + procId;<a name="line.1055"></a> +<span class="sourceLineNo">1056</span> return procId;<a name="line.1056"></a> +<span class="sourceLineNo">1057</span> }<a name="line.1057"></a> +<span class="sourceLineNo">1058</span><a name="line.1058"></a> +<span class="sourceLineNo">1059</span> @VisibleForTesting<a name="line.1059"></a> +<span class="sourceLineNo">1060</span> protected long getLastProcId() {<a name="line.1060"></a> +<span class="sourceLineNo">1061</span> return lastProcId.get();<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> @VisibleForTesting<a name="line.1064"></a> +<span class="sourceLineNo">1065</span> public Set<Long> getActiveProcIds() {<a name="line.1065"></a> +<span class="sourceLineNo">1066</span> return procedures.keySet();<a name="line.1066"></a> +<span class="sourceLineNo">1067</span> }<a name="line.1067"></a> +<span class="sourceLineNo">1068</span><a name="line.1068"></a> +<span class="sourceLineNo">1069</span> private Long getRootProcedureId(Procedure proc) {<a name="line.1069"></a> +<span class="sourceLineNo">1070</span> return Procedure.getRootProcedureId(procedures, proc);<a name="line.1070"></a> +<span class="sourceLineNo">1071</span> }<a name="line.1071"></a> +<span class="sourceLineNo">1072</span><a name="line.1072"></a> +<span class="sourceLineNo">1073</span> // ==========================================================================<a name="line.1073"></a> +<span class="sourceLineNo">1074</span> // Executions<a name="line.1074"></a> +<span class="sourceLineNo">1075</span> // ==========================================================================<a name="line.1075"></a> +<span class="sourceLineNo">1076</span> private void executeProcedure(final Procedure proc) {<a name="line.1076"></a> +<span class="sourceLineNo">1077</span> final Long rootProcId = getRootProcedureId(proc);<a name="line.1077"></a> +<span class="sourceLineNo">1078</span> if (rootProcId == null) {<a name="line.1078"></a> +<span class="sourceLineNo">1079</span> // The 'proc' was ready to run but the root procedure was rolledback<a name="line.1079"></a> +<span class="sourceLineNo">1080</span> executeRollback(proc);<a name="line.1080"></a> +<span class="sourceLineNo">1081</span> return;<a name="line.1081"></a> +<span class="sourceLineNo">1082</span> }<a name="line.1082"></a> <span class="sourceLineNo">1083</span><a name="line.1083"></a> -<span class="sourceLineNo">1084</span> do {<a name="line.1084"></a> -<span class="sourceLineNo">1085</span> // Try to acquire the execution<a name="line.1085"></a> -<span class="sourceLineNo">1086</span> if (!procStack.acquire(proc)) {<a name="line.1086"></a> -<span class="sourceLineNo">1087</span> if (procStack.setRollback()) {<a name="line.1087"></a> -<span class="sourceLineNo">1088</span> // we have the 'rollback-lock' we can start rollingback<a name="line.1088"></a> -<span class="sourceLineNo">1089</span> switch (executeRollback(rootProcId, procStack)) {<a name="line.1089"></a> -<span class="sourceLineNo">1090</span> case LOCK_ACQUIRED:<a name="line.1090"></a> -<span class="sourceLineNo">1091</span> break;<a name="line.1091"></a> -<span class="sourceLineNo">1092</span> case LOCK_YIELD_WAIT:<a name="line.1092"></a> -<span class="sourceLineNo">1093</span> procStack.unsetRollback();<a name="line.1093"></a> -<span class="sourceLineNo">1094</span> scheduler.yield(proc);<a name="line.1094"></a> -<span class="sourceLineNo">1095</span> break;<a name="line.1095"></a> -<span class="sourceLineNo">1096</span> case LOCK_EVENT_WAIT:<a name="line.1096"></a> -<span class="sourceLineNo">1097</span> procStack.unsetRollback();<a name="line.1097"></a> +<span class="sourceLineNo">1084</span> final RootProcedureState procStack = rollbackStack.get(rootProcId);<a name="line.1084"></a> +<span class="sourceLineNo">1085</span> if (procStack == null) return;<a name="line.1085"></a> +<span class="sourceLineNo">1086</span><a name="line.1086"></a> +<span class="sourceLineNo">1087</span> do {<a name="line.1087"></a> +<span class="sourceLineNo">1088</span> // Try to acquire the execution<a name="line.1088"></a> +<span class="sourceLineNo">1089</span> if (!procStack.acquire(proc)) {<a name="line.1089"></a> +<span class="sourceLineNo">1090</span> if (procStack.setRollback()) {<a name="line.1090"></a> +<span class="sourceLineNo">1091</span> // we have the 'rollback-lock' we can start rollingback<a name="line.1091"></a> +<span class="sourceLineNo">1092</span> switch (executeRollback(rootProcId, procStack)) {<a name="line.1092"></a> +<span class="sourceLineNo">1093</span> case LOCK_ACQUIRED:<a name="line.1093"></a> +<span class="sourceLineNo">1094</span> break;<a name="line.1094"></a> +<span class="sourceLineNo">1095</span> case LOCK_YIELD_WAIT:<a name="line.1095"></a> +<span class="sourceLineNo">1096</span> procStack.unsetRollback();<a name="line.1096"></a> +<span class="sourceLineNo">1097</span> scheduler.yield(proc);<a name="line.1097"></a> <span class="sourceLineNo">1098</span> break;<a name="line.1098"></a> -<span class="sourceLineNo">1099</span> default:<a name="line.1099"></a> -<span class="sourceLineNo">1100</span> throw new UnsupportedOperationException();<a name="line.1100"></a> -<span class="sourceLineNo">1101</span> }<a name="line.1101"></a> -<span class="sourceLineNo">1102</span> } else {<a name="line.1102"></a> -<span class="sourceLineNo">1103</span> // if we can't rollback means that some child is still running.<a name="line.1103"></a> -<span class="sourceLineNo">1104</span> // the rollback will be executed after all the children are done.<a name="line.1104"></a> -<span class="sourceLineNo">1105</span> // If the procedure was never executed, remove and mark it as rolledback.<a name="line.1105"></a> -<span class="sourceLineNo">1106</span> if (!proc.wasExecuted()) {<a name="line.1106"></a> -<span class="sourceLineNo">1107</span> switch (executeRollback(proc)) {<a name="line.1107"></a> -<span class="sourceLineNo">1108</span> case LOCK_ACQUIRED:<a name="line.1108"></a> -<span class="sourceLineNo">1109</span> break;<a name="line.1109"></a> -<span class="sourceLineNo">1110</span> case LOCK_YIELD_WAIT:<a name="line.1110"></a> -<span class="sourceLineNo">1111</span> scheduler.yield(proc);<a name="line.1111"></a> +<span class="sourceLineNo">1099</span> case LOCK_EVENT_WAIT:<a name="line.1099"></a> +<span class="sourceLineNo">1100</span> procStack.unsetRollback();<a name="line.1100"></a> +<span class="sourceLineNo">1101</span> break;<a name="line.1101"></a> +<span class="sourceLineNo">1102</span> default:<a name="line.1102"></a> +<span class="sourceLineNo">1103</span> throw new UnsupportedOperationException();<a name="line.1103"></a> +<span class="sourceLineNo">1104</span> }<a name="line.1104"></a> +<span class="sourceLineNo">1105</span> } else {<a name="line.1105"></a> +<span class="sourceLineNo">1106</span> // if we can't rollback means that some child is still running.<a name="line.1106"></a> +<span class="sourceLineNo">1107</span> // the rollback will be executed after all the children are done.<a name="line.1107"></a> +<span class="sourceLineNo">1108</span> // If the procedure was never executed, remove and mark it as rolledback.<a name="line.1108"></a> +<span class="sourceLineNo">1109</span> if (!proc.wasExecuted()) {<a name="line.1109"></a> +<span class="sourceLineNo">1110</span> switch (executeRollback(proc)) {<a name="line.1110"></a> +<span class="sourceLineNo">1111</span> case LOCK_ACQUIRED:<a name="line.1111"></a> <span class="sourceLineNo">1112</span> break;<a name="line.1112"></a> -<span class="sourceLineNo">1113</span> case LOCK_EVENT_WAIT:<a name="line.1113"></a> -<span class="sourceLineNo">1114</span> break;<a name="line.1114"></a> -<span class="sourceLineNo">1115</span> default:<a name="line.1115"></a> -<span class="sourceLineNo">1116</span> throw new UnsupportedOperationException();<a name="line.1116"></a> -<span class="sourceLineNo">1117</span> }<a name="line.1117"></a> -<span class="sourceLineNo">1118</span> }<a name="line.1118"></a> -<span class="sourceLineNo">1119</span> }<a name="line.1119"></a> -<span class="sourceLineNo">1120</span> break;<a name="line.1120"></a> -<span class="sourceLineNo">1121</span> }<a name="line.1121"></a> -<span class="sourceLineNo">1122</span><a name="line.1122"></a> -<span class="sourceLineNo">1123</span> // Execute the procedure<a name="line.1123"></a> -<span class="sourceLineNo">1124</span> assert proc.getState() == ProcedureState.RUNNABLE : proc;<a name="line.1124"></a> -<span class="sourceLineNo">1125</span> switch (acquireLock(proc)) {<a name="line.1125"></a> -<span class="sourceLineNo">1126</span> case LOCK_ACQUIRED:<a name="line.1126"></a> -<span class="sourceLineNo">1127</span> execProcedure(procStack, proc);<a name="line.1127"></a> -<span class="sourceLineNo">1128</span> releaseLock(proc, false);<a name="line.1128"></a> -<span class="sourceLineNo">1129</span> break;<a name="line.1129"></a> -<span class="sourceLineNo">1130</span> case LOCK_YIELD_WAIT:<a name="line.1130"></a> -<span class="sourceLineNo">1131</span> scheduler.yield(proc);<a name="line.1131"></a> +<span class="sourceLineNo">1113</span> case LOCK_YIELD_WAIT:<a name="line.1113"></a> +<span class="sourceLineNo">1114</span> scheduler.yield(proc);<a name="line.1114"></a> +<span class="sourceLineNo">1115</span> break;<a name="line.1115"></a> +<span class="sourceLineNo">1116</span> case LOCK_EVENT_WAIT:<a name="line.1116"></a> +<span class="sourceLineNo">1117</span> break;<a name="line.1117"></a> +<span class="sourceLineNo">1118</span> default:<a name="line.1118"></a> +<span class="sourceLineNo">1119</span> throw new UnsupportedOperationException();<a name="line.1119"></a> +<span class="sourceLineNo">1120</span> }<a name="line.1120"></a> +<span class="sourceLineNo">1121</span> }<a name="line.1121"></a> +<span class="sourceLineNo">1122</span> }<a name="line.1122"></a> +<span class="sourceLineNo">1123</span> break;<a name="line.1123"></a> +<span class="sourceLineNo">1124</span> }<a name="line.1124"></a> +<span class="sourceLineNo">1125</span><a name="line.1125"></a> +<span class="sourceLineNo">1126</span> // Execute the procedure<a name="line.1126"></a> +<span class="sourceLineNo">1127</span> assert proc.getState() == ProcedureState.RUNNABLE : proc;<a name="line.1127"></a> +<span class="sourceLineNo">1128</span> switch (acquireLock(proc)) {<a name="line.1128"></a> +<span class="sourceLineNo">1129</span> case LOCK_ACQUIRED:<a name="line.1129"></a> +<span class="sourceLineNo">1130</span> execProcedure(procStack, proc);<a name="line.1130"></a> +<span class="sourceLineNo">1131</span> releaseLock(proc, false);<a name="line.1131"></a> <span class="sourceLineNo">1132</span> break;<a name="line.1132"></a> -<span class="sourceLineNo">1133</span> case LOCK_EVENT_WAIT:<a name="line.1133"></a> -<span class="sourceLineNo">1134</span> // someone will wake us up when the lock is available<a name="line.1134"></a> +<span class="sourceLineNo">1133</span> case LOCK_YIELD_WAIT:<a name="line.1133"></a> +<span class="sourceLineNo">1134</span> scheduler.yield(proc);<a name="line.1134"></a> <span class="sourceLineNo">1135</span> break;<a name="line.1135"></a> -<span class="sourceLineNo">1136</span> default:<a name="line.1136"></a> -<span class="sourceLineNo">1137</span> throw new UnsupportedOperationException();<a name="line.1137"></a> -<span class="sourceLineNo">1138</span> }<a name="line.1138"></a> -<span class="sourceLineNo">1139</span> procStack.release(proc);<a name="line.1139"></a> -<span class="sourceLineNo">1140</span><a name="line.1140"></a> -<span class="sourceLineNo">1141</span> // allows to kill the executor before something is stored to the wal.<a name="line.1141"></a> -<span class="sourceLineNo">1142</span> // useful to test the procedure recovery.<a name="line.1142"></a> -<span class="sourceLineNo">1143</span> if (testing != null && !isRunning()) {<a name="line.1143"></a> -<span class="sourceLineNo">1144</span> break;<a name="line.1144"></a> -<span class="sourceLineNo">1145</span> }<a name="line.1145"></a> -<span class="sourceLineNo">1146</span><a name="line.1146"></a> -<span class="sourceLineNo">1147</span> if (proc.isSuccess()) {<a name="line.1147"></a> -<span class="sourceLineNo">1148</span> if (LOG.isDebugEnabled()) {<a name="line.1148"></a> -<span class="sourceLineNo">1149</span> LOG.debug("Finished " + proc + " in " + StringUtils.humanTimeDiff(proc.elapsedTime()));<a name="line.1149"></a> -<span class="sourceLineNo">1150</span> }<a name="line.1150"></a> -<span class="sourceLineNo">1151</span> // Finalize the procedure state<a name="line.1151"></a> -<span class="sourceLineNo">1152</span> if (proc.getProcId() == rootProcId) {<a name="line.1152"></a> -<span class="sourceLineNo">1153</span> procedureFinished(proc);<a name="line.1153"></a> -<span class="sourceLineNo">1154</span> } else {<a name="line.1154"></a> -<span class="sourceLineNo">1155</span> execCompletionCleanup(proc);<a name="line.1155"></a> +<span class="sourceLineNo">1136</span> case LOCK_EVENT_WAIT:<a name="line.1136"></a> +<span class="sourceLineNo">1137</span> // someone will wake us up when the lock is available<a name="line.1137"></a> +<span class="sourceLineNo">1138</span> break;<a name="line.1138"></a> +<span class="sourceLineNo">1139</span> default:<a name="line.1139"></a> +<span class="sourceLineNo">1140</span> throw new UnsupportedOperationException();<a name="line.1140"></a> +<span class="sourceLineNo">1141</span> }<a name="line.1141"></a> +<span class="sourceLineNo">1142</span> procStack.release(proc);<a name="line.1142"></a> +<span class="sourceLineNo">1143</span><a name="line.1143"></a> +<span class="sourceLineNo">1144</span> // allows to kill the executor before something is stored to the wal.<a name="line.1144"></a> +<span class="sourceLineNo">1145</span> // useful to test the procedure recovery.<a name="line.1145"></a> +<span class="sourceLineNo">1146</span> if (testing != null && !isRunning()) {<a name="line.1146"></a> +<span class="sourceLineNo">1147</span> break;<a name="line.1147"></a> +<span class="sourceLineNo">1148</span> }<a name="line.1148"></a> +<span class="sourceLineNo">1149</span><a name="line.1149"></a> +<span class="sourceLineNo">1150</span> if (proc.isSuccess()) {<a name="line.1150"></a> +<span class="sourceLineNo">1151</span> // update metrics on finishing the procedure<a name="line.1151"></a> +<span class="sourceLineNo">1152</span> proc.updateMetricsOnFinish(getEnvironment(), proc.elapsedTime(), true);<a name="line.1152"></a> +<span class="sourceLineNo">1153</span><a name="line.1153"></a> +<span class="sourceLineNo">1154</span> if (LOG.isDebugEnabled()) {<a name="line.1154"></a> +<span class="sourceLineNo">1155</span> LOG.debug("Finished " + proc + " in " + StringUtils.humanTimeDiff(proc.elapsedTime()));<a name="line.1155"></a> <span class="sourceLineNo">1156</span> }<a name="line.1156"></a> -<span class="sourceLineNo">1157</span> break;<a name="line.1157"></a> -<span class="sourceLineNo">1158</span> }<a name="line.1158"></a> -<span class="sourceLineNo">1159</span> } while (procStack.isFailed());<a name="line.1159"></a> -<span class="sourceLineNo">1160</span> }<a name="line.1160"></a> -<span class="sourceLineNo">1161</span><a name="line.1161"></a> -<span class="sourceLineNo">1162</span> private LockState acquireLock(final Procedure proc) {<a name="line.1162"></a> -<span class="sourceLineNo">1163</span> final TEnvironment env = getEnvironment();<a name="line.1163"></a> -<span class="sourceLineNo">1164</span> // hasLock() is used in conjunction with holdLock().<a name="line.1164"></a> -<span class="sourceLineNo">1165</span> // This allows us to not rewrite or carry around the hasLock() flag<a name="line.1165"></a> -<span class="sourceLineNo">1166</span> // for every procedure. the hasLock() have meaning only if holdLock() is true.<a name="line.1166"></a> -<span class="sourceLineNo">1167</span> if (proc.holdLock(env) && proc.hasLock(env)) {<a name="line.1167"></a> -<span class="sourceLineNo">1168</span> return LockState.LOCK_ACQUIRED;<a name="line.1168"></a> -<span class="sourceLineNo">1169</span> }<a name="line.1169"></a> -<span class="sourceLineNo">1170</span> return proc.doAcquireLock(env);<a name="line.1170"></a> -<span class="sourceLineNo">1171</span> }<a name="line.1171"></a> -<span class="sourceLineNo">1172</span><a name="line.1172"></a> -<span class="sourceLineNo">1173</span> private void releaseLock(final Procedure proc, final boolean force) {<a name="line.1173"></a> -<span class="sourceLineNo">1174</span> final TEnvironment env = getEnvironment();<a name="line.1174"></a> -<span class="sourceLineNo">1175</span> // for how the framework works, we know that we will always have the lock<a name="line.1175"></a> -<span class="sourceLineNo">1176</span> // when we call releaseLock(), so we can avoid calling proc.hasLock()<a name="line.1176"></a> -<span class="sourceLineNo">1177</span> if (force || !proc.holdLock(env)) {<a name="line.1177"></a> -<span class="sourceLineNo">1178</span> proc.doReleaseLock(env);<a name="line.1178"></a> -<span class="sourceLineNo">1179</span> }<a name="line.1179"></a> -<span class="sourceLineNo">1180</span> }<a name="line.1180"></a> -<span class="sourceLineNo">1181</span><a name="line.1181"></a> -<span class="sourceLineNo">1182</span> /**<a name="line.1182"></a> -<span class="sourceLineNo">1183</span> * Execute the rollback of the full procedure stack.<a name="line.1183"></a> -<span class="sourceLineNo">1184</span> * Once the procedure is rolledback, the root-procedure will be visible as<a name="line.1184"></a> -<span class="sourceLineNo">1185</span> * finished to user, and the result will be the fatal exception.<a name="line.1185"></a> -<span class="sourceLineNo">1186</span> */<a name="line.1186"></a> -<span class="sourceLineNo">1187</span> private LockState executeRollback(final long rootProcId, final RootProcedureState procStack) {<a name="line.1187"></a> -<span class="sourceLineNo">1188</span> final Procedure rootProc = procedures.get(rootProcId);<a name="line.1188"></a> -<span class="sourceLineNo">1189</span> RemoteProcedureException exception = rootProc.getException();<a name="line.1189"></a> -<span class="sourceLineNo">1190</span> if (exception == null) {<a name="line.1190"></a> -<span class="sourceLineNo">1191</span> exception = procStack.getException();<a name="line.1191"></a> -<span class="sourceLineNo">1192</span> rootProc.setFailure(exception);<a name="line.1192"></a> -<span class="sourceLineNo">1193</span> store.update(rootProc);<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> final List<Procedure> subprocStack = procStack.getSubproceduresStack();<a name="line.1196"></a> -<span class="sourceLineNo">1197</span> assert subprocStack != null : "Called rollback with no steps executed rootProc=" + rootProc;<a name="line.1197"></a> -<span class="sourceLineNo">1198</span><a name="line.1198"></a> -<span class="sourceLineNo">1199</span> int stackTail = subprocStack.size();<a name="line.1199"></a> -<span class="sourceLineNo">1200</span> boolean reuseLock = false;<a name="line.1200"></a> -<span class="sourceLineNo">1201</span> while (stackTail --> 0) {<a name="line.1201"></a> -<span class="sourceLineNo">1202</span> final Procedure proc = subprocStack.get(stackTail);<a name="line.1202"></a> -<span class="sourceLineNo">1203</span><a name="line.1203"></a> -<span class="sourceLineNo">1204</span> LockState lockState;<a name="line.1204"></a> -<span class="sourceLineNo">1205</span> if (!reuseLock && (lockState = acquireLock(proc)) != LockState.LOCK_ACQUIRED) {<a name="line.1205"></a> -<span class="sourceLineNo">1206</span> // can't take a lock on the procedure, add the root-proc back on the<a name="line.1206"></a> -<span class="sourceLineNo">1207</span> // queue waiting for the lock availability<a name="line.1207"></a> -<span class="sourceLineNo">1208</span> return lockState;<a name="line.1208"></a> -<span class="sourceLineNo">1209</span> }<a name="line.1209"></a> -<span class="sourceLineNo">1210</span><a name="line.1210"></a> -<span class="sourceLineNo">1211</span> lockState = executeRollback(proc);<a name="line.1211"></a> -<span class="sourceLineNo">1212</span> boolean abortRollback = lockState != LockState.LOCK_ACQUIRED;<a name="line.1212"></a> -<span class="sourceLineNo">1213</span> abortRollback |= !isRunning() || !store.isRunning();<a name="line.1213"></a> -<span class="sourceLineNo">1214</span><a name="line.1214"></a> -<span class="sourceLineNo">1215</span> // If the next procedure is the same to this one<a name="line.1215"></a> -<span class="sourceLineNo">1216</span> // (e.g. StateMachineProcedure reuse the same instance)<a name="line.1216"></a> -<span class="sourceLineNo">1217</span> // we can avoid to lock/unlock each step<a name="line.1217"></a> -<span class="sourceLineNo">1218</span> reuseLock = stackTail > 0 && (subprocStack.get(stackTail - 1) == proc) && !abortRollback;<a name="line.1218"></a> -<span class="sourceLineNo">1219</span> if (!reuseLock) {<a name="line.1219"></a> -<span class="sourceLineNo">1220</span> releaseLock(proc, false);<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> // allows to kill the executor before something is stored to the wal.<a name="line.1223"></a> -<span class="sourceLineNo">1224</span> // useful to test the procedure recovery.<a name="line.1224"></a> -<span class="sourceLineNo">1225</span> if (abortRollback) {<a name="line.1225"></a> -<span class="sourceLineNo">1226</span> return lockState;<a name="line.1226"></a> +<span class="sourceLineNo">1157</span> // Finalize the procedure state<a name="line.1157"></a> +<span class="sourceLineNo">1158</span> if (proc.getProcId() == rootProcId) {<a name="line.1158"></a> +<span class="sourceLineNo">1159</span> procedureFinished(proc);<a name="line.1159"></a> +<span class="sourceLineNo">1160</span> } else {<a name="line.1160"></a> +<span class="sourceLineNo">1161</span> execCompletionCleanup(proc);<a name="line.1161"></a> +<span class="sourceLineNo">1162</span> }<a name="line.1162"></a> +<span class="sourceLineNo">1163</span> break;<a name="line.1163"></a> +<span class="sourceLineNo">1164</span> }<a name="line.1164"></a> +<span class="sourceLineNo">1165</span> } while (procStack.isFailed());<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> private LockState acquireLock(final Procedure proc) {<a name="line.1168"></a> +<span class="sourceLineNo">1169</span> final TEnvironment env = getEnvironment();<a name="line.1169"></a> +<span class="sourceLineNo">1170</span> // hasLock() is used in conjunction with holdLock().<a name="line.1170"></a> +<span class="sourceLineNo">1171</span> // This allows us to not rewrite or carry around the hasLock() flag<a name="line.1171"></a> +<span class="sourceLineNo">1172</span> // for every procedure. the hasLock() have meaning only if holdLock() is true.<a name="line.1172"></a> +<span class="sourceLineNo">1173</span> if (proc.holdLock(env) && proc.hasLock(env)) {<a name="line.1173"></a> +<span class="sourceLineNo">1174</span> return LockState.LOCK_ACQUIRED;<a name="line.1174"></a> +<span class="sourceLineNo">1175</span> }<a name="line.1175"></a> +<span class="sourceLineNo">1176</span> return proc.doAcquireLock(env);<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> private void releaseLock(final Procedure proc, final boolean force) {<a name="line.1179"></a> +<span class="sourceLineNo">1180</span> final TEnvironment env = getEnvironment();<a name="line.1180"></a> +<span class="sourceLineNo">1181</span> // for how the framework works, we know that we will always have the lock<a name="line.1181"></a> +<span class="sourceLineNo">1182</span> // when we call releaseLock(), so we can avoid calling proc.hasLock()<a name="line.1182"></a> +<span class="sourceLineNo">1183</span> if (force || !proc.holdLock(env)) {<a name="line.1183"></a> +<span class="sourceLineNo">1184</span> proc.doReleaseLock(env);<a name="line.1184"></a> +<span class="sourceLineNo">1185</span> }<a name="line.1185"></a> +<span class="sourceLineNo">1186</span> }<a name="line.1186"></a> +<span class="sourceLineNo">1187</span><a name="line.1187"></a> +<span class="sourceLineNo">1188</span> /**<a name="line.1188"></a> +<span class="sourceLineNo">1189</span> * Execute the rollback of the full procedure stack.<a name="line.1189"></a> +<span class="sourceLineNo">1190</span> * Once the procedure is rolledback, the root-procedure will be visible as<a name="line.1190"></a> +<span class="sourceLineNo">1191</span> * finished to user, and the result will be the fatal exception.<a name="line.1191"></a> +<span class="sourceLineNo">1192</span> */<a name="line.1192"></a> +<span class="sourceLineNo">1193</span> private LockState executeRollback(final long rootProcId, final RootProcedureState procStack) {<a name="line.1193"></a> +<span class="sourceLineNo">1194</span> final Procedure rootProc = procedures.get(rootProcId);<a name="line.1194"></a> +<span class="sourceLineNo">1195</span> RemoteProcedureException exception = rootProc.getException();<a name="line.1195"></a> +<span class="sourceLineNo">1196</span> if (exception == null) {<a name="line.1196"></a> +<span class="sourceLineNo">1197</span> exception = procStack.getException();<a name="line.1197"></a> +<span class="sourceLineNo">1198</span> rootProc.setFailure(exception);<a name="line.1198"></a> +<span class="sourceLineNo">1199</span> store.update(rootProc);<a name="line.1199"></a> +<span class="sourceLineNo">1200</span> }<a name="line.1200"></a> +<span class="sourceLineNo">1201</span><a name="line.1201"></a> +<span class="sourceLineNo">1202</span> final List<Procedure> subprocStack = procStack.getSubproceduresStack();<a name="line.1202"></a> +<span class="sourceLineNo">1203</span> assert subprocStack != null : "Called rollback with no steps executed rootProc=" + rootProc;<a name="line.1203"></a> +<span class="sourceLineNo">1204</span><a name="line.1204"></a> +<span class="sourceLineNo">1205</span> int stackTail = subprocStack.size();<a name="line.1205"></a> +<span class="sourceLineNo">1206</span> boolean reuseLock = false;<a name="line.1206"></a> +<span class="sourceLineNo">1207</span> while (stackTail --> 0) {<a name="line.1207"></a> +<span class="sourceLineNo">1208</span> final Procedure proc = subprocStack.get(stackTail);<a name="line.1208"></a> +<span class="sourceLineNo">1209</span><a name="line.1209"></a> +<span class="sourceLineNo">1210</span> LockState lockState;<a name="line.1210"></a> +<span class="sourceLineNo">1211</span> if (!reuseLock && (lockState = acquireLock(proc)) != LockState.LOCK_ACQUIRED) {<a name="line.1211"></a> +<span class="sourceLineNo">1212</span> // can't take a lock on the procedure, add the root-proc back on the<a name="line.1212"></a> +<span class="sourceLineNo">1213</span> // queue waiting for the lock availability<a name="line.1213"></a> +<span class="sourceLineNo">1214</span> return lockState;<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> lockState = executeRollback(proc);<a name="line.1217"></a> +<span class="sourceLineNo">1218</span> boolean abortRollback = lockState != LockState.LOCK_ACQUIRED;<a name="line.1218"></a> +<span class="sourceLineNo">1219</span> abortRollback |= !isRunning() || !store.isRunning();<a name="line.1219"></a> +<span class="sourceLineNo">1220</span><a name="line.1220"></a> +<span class="sourceLineNo">1221</span> // If the next procedure is the same to this one<a name="line.1221"></a> +<span class="sourceLineNo">1222</span> // (e.g. StateMachineProcedure reuse the same instance)<a name="line.1222"></a> +<span class="sourceLineNo">1223</span> // we can avoid to lock/unlock each step<a name="line.1223"></a> +<span class="sourceLineNo">1224</span> reuseLock = stackTail > 0 && (subprocStack.get(stackTail - 1) == proc) && !abortRollback;<a name="line.1224"></a> +<span class="sourceLineNo">1225</span> if (!reuseLock) {<a name="line.1225"></a> +<span class="sourceLineNo">1226</span> releaseLock(proc, false);<a name="line.1226"></a> <span class="sourceLineNo">1227</span> }<a name="line.1227"></a> <span class="sourceLineNo">1228</span><a name="line.1228"></a> -<span class="sourceLineNo">1229</span> subprocStack.remove(stackTail);<a name="line.1229"></a> -<span class="sourceLineNo">1230</span><a name="line.1230"></a> -<span class="sourceLineNo">1231</span> // if the procedure is kind enough to pass the slot to someone else, yield<a name="line.1231"></a> -<span class="sourceLineNo">1232</span> if (proc.isYieldAfterExecutionStep(getEnvironment())) {<a name="line.1232"></a> -<span class="sourceLineNo">1233</span> return LockState.LOCK_YIELD_WAIT;<a name="line.1233"></a> -<span class="sourceLineNo">1234</span> }<a name="line.1234"></a> -<span class="sourceLineNo">1235</span><a name="line.1235"></a> -<span class="sourceLineNo">1236</span> if (proc != rootProc) {<a name="line.1236"></a> -<span class="sourceLineNo">1237</span> execCompletionCleanup(proc);<a name="line.1237"></a> -<span class="sourceLineNo">1238</span> }<a name="line.1238"></a> -<span class="sourceLineNo">1239</span> }<a name="line.1239"></a> -<span class="sourceLineNo">1240</span><a name="line.1240"></a> -<span class="sourceLineNo">1241</span> // Finalize the procedure state<a name="line.1241"></a> -<span class="sourceLineNo">1242</span> LOG.info("Rolled back " + rootProc +<a name="line.1242"></a> -<span class="sourceLineNo">1243</span> " exec-time=" + StringUtils.humanTimeDiff(rootProc.elapsedTime()));<a name="line.1243"></a> -<span class="sourceLineNo">1244</span> procedureFinished(rootProc);<a name="line.1244"></a> -<span class="sourceLineNo">1245</span> return LockState.LOCK_ACQUIRED;<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> /**<a name="line.1248"></a> -<span class="sourceLineNo">1249</span> * Execute the rollback of the procedure step.<a name="line.1249"></a> -<span class="sourceLineNo">1250</span> * It updates the store with the new state (stack index)<a name="line.1250"></a> -<span class="sourceLineNo">1251</span> * or will remove completly the procedure in case it is a child.<a name="line.1251"></a> -<span class="sourceLineNo">1252</span> */<a name="line.1252"></a> -<span class="sourceLineNo">1253</span> private LockState executeRollback(final Procedure proc) {<a name="line.1253"></a> -<span class="sourceLineNo">1254</span> try {<a name="line.1254"></a> -<span class="sourceLineNo">1255</span> proc.doRollback(getEnvironment());<a name="line.1255"></a> -<span class="sourceLineNo">1256</span> } catch (IOException e) {<a name="line.1256"></a> -<span class="sourceLineNo">1257</span> if (LOG.isDebugEnabled()) {<a name="line.1257"></a> -<span class="sourceLineNo">1258</span> LOG.debug("Roll back attempt failed for " + proc, e);<a name="line.1258"></a> -<span class="sourceLineNo">1259</span> }<a name="line.1259"></a> -<span class="sourceLineNo">1260</span> return LockState.LOCK_YIELD_WAIT;<a name="line.1260"></a> -<span class="sourceLineNo">1261</span> } catch (InterruptedException e) {<a name="line.1261"></a> -<span class="sourceLineNo">1262</span> handleInterruptedException(proc, e);<a name="line.1262"></a> -<span class="sourceLineNo">1263</span> return LockState.LOCK_YIELD_WAIT;<a name="line.1263"></a> -<span class="sourceLineNo">1264</span> } catch (Throwable e) {<a name="line.1264"></a> -<span class="sourceLineNo">1265</span> // Catch NullPointerExceptions or similar errors...<a name="line.1265"></a> -<span class="sourceLineNo">1266</span> LOG.fatal("CODE-BUG: Uncatched runtime exception for procedure: " + proc, e);<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> // allows to kill the executor before something is stored to the wal.<a name="line.1269"></a> -<span class="sourceLineNo">1270</span> // useful to test the procedure recovery.<a name="line.1270"></a> -<span class="sourceLineNo">1271</span> if (testing != null && testing.shouldKillBeforeStoreUpdate()) {<a name="line.1271"></a> -<span class="sourceLineNo">1272</span> LOG.debug("TESTING: Kill before store update");<a name="line.1272"></a> -<span class="sourceLineNo">1273</span> stop();<a name="line.1273"></a> -<span class="sourceLineNo">1274</span> return LockState.LOCK_YIELD_WAIT;<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> if (proc.removeStackIndex()) {<a name="line.1277"></a> -<span class="sourceLineNo">1278</span> proc.setState(ProcedureState.ROLLEDBACK);<a name="line.1278"></a> -<span class="sourceLineNo">1279</span> if (proc.hasParent()) {<a name="line.1279"></a> -<span class="sourceLineNo">1280</span> store.delete(proc.getProcId());<a name="line.1280"></a> -<span class="sourceLineNo">1281</span> procedures.remove(proc.getProcId());<a name="line.1281"></a> -<span class="sourceLineNo">1282</span> } else {<a name="line.1282"></a> -<span class="sourceLineNo">1283</span> final long[] childProcIds = rollbackStack.get(proc.getProcId()).getSubprocedureIds();<a name="line.1283"></a> -<span class="sourceLineNo">1284</span> if (childProcIds != null) {<a name="line.1284"></a> -<span class="sourceLineNo">1285</span> store.delete(proc, childProcIds);<a name="line.1285"></a> -<span class="sourceLineNo">1286</span> } else {<a name="line.1286"></a> -<span class="sourceLineNo">1287</span> store.update(proc);<a name="line.1287"></a> -<span class="sourceLineNo">1288</span> }<a name="line.1288"></a> -<span class="sourceLineNo">1289</span> }<a name="line.1289"></a> -<span class="sourceLineNo">1290</span> } else {<a name="line.1290"></a> -<span class="sourceLineNo">1291</span> store.update(proc);<a name="line.1291"></a> -<span class="sourceLineNo">1292</span> }<a name="line.1292"></a> -<span class="sourceLineNo">1293</span><a name="line.1293"></a> -<span class="sourceLineNo">1294</span> return LockState.LOCK_ACQUIRED;<a name="line.1294"></a> -<span class="sourceLineNo">1295</span> }<a name="line.1295"></a> -<span class="sourceLineNo">1296</span><a name="line.1296"></a> -<span class="sourceLineNo">1297</span> /**<a name="line.1297"></a> -<span class="sourceLineNo">1298</span> * Executes the specified procedure<a name="line.1298"></a> -<span class="sourceLineNo">1299</span> * - calls the doExecute() of the procedure<a name="line.1299"></a> -<span class="sourceLineNo">1300</span> * - if the procedure execution didn't fail (e.g. invalid user input)<a name="line.1300"></a> -<span class="sourceLineNo">1301</span> * - ...and returned subprocedures<a name="line.1301"></a> -<span class="sourceLineNo">1302</span> * - the subprocedures are initialized.<a name="line.1302"></a> -<span class="sourceLineNo">1303</span> * - the subprocedures are added to the store<a name="line.1303"></a> -<span class="sourceLineNo">1304</span> * - the subprocedures are added to the runnable queue<a name="line.1304"></a> -<span class="sourceLineNo">1305</span> * - the procedure is now in a WAITING state, waiting for the subprocedures to complete<a name="line.1305"></a> -<span class="sourceLineNo">1306</span> * - ...if there are no subprocedure<a name="line.1306"></a> -<span class="sourceLineNo">1307</span> * - the procedure completed successfully<a name="line.1307"></a> -<span class="sourceLineNo">1308</span> * - if there is a parent (WAITING)<a name="line.1308"></a> -<span class="sourceLineNo">1309</span> * - the parent state will be set to RUNNABLE<a name="line.1309"></a> -<span class="sourceLineNo">1310</span> * - in case of failure<a name="line.1310"></a> -<span class="sourceLineNo">1311</span> * - the store is updated with the new state<a name="line.1311"></a> -<span class="sourceLineNo">1312</span> * - the executor (caller of this method) will start the rollback of the procedure<a name="line.1312"></a> -<span class="sourceLineNo">1313</span> */<a name="line.1313"></a> -<span class="sourceLineNo">1314</span> private void execProcedure(final RootProcedureState procStack, final Procedure procedure) {<a name="line.1314"></a> -<span class="sourceLineNo">1315</span> Preconditions.checkArgument(procedure.getState() == ProcedureState.RUNNABLE);<a name="line.1315"></a> -<span class="sourceLineNo">1316</span><a name="line.1316"></a> -<span class="sourceLineNo">1317</span> // Execute the procedure<a name="line.1317"></a> -<span class="sourceLineNo">1318</span> boolean suspended = false;<a name="line.1318"></a> -<span class="sourceLineNo">1319</span> boolean reExecute = false;<a name="line.1319"></a> -<span class="sourceLineNo">1320</span> Procedure[] subprocs = null;<a name="line.1320"></a> -<span class="sourceLineNo">1321</span> do {<a name="line.1321"></a> -<span class="sourceLineNo">1322</span> reExecute = false;<a name="line.1322"></a> -<span class="sourceLineNo">1323</span> try {<a name="line.1323"></a> -<span class="sourceLineNo">1324</span> subprocs = procedure.doExecute(getEnvironment());<a name="line.1324"></a> -<span class="sourceLineNo">1325</span> if (subprocs != null && subprocs.length == 0) {<a name="line.1325"></a> -<span class="sourceLineNo">1326</span> subprocs = null;<a name="line.1326"></a> -<span class="sourceLineNo">1327</span> }<a name="line.1327"></a> -<span class="sourceLineNo">1328</span> } catch (ProcedureSuspendedException e) {<a name="line.1328"></a> -<span class="sourceLineNo">1329</span> suspended = true;<a name="line.1329"></a> -<span class="sourceLineNo">1330</span> } catch (ProcedureYieldException e) {<a name="line.1330"></a> -<span class="sourceLineNo">1331</span> if (LOG.isTraceEnabled()) {<a name="line.1331"></a> -<span class="sourceLineNo">1332</span> LOG.trace("Yield " + procedure + ": " + e.getMessage());<a name="line.1332"></a> -<span class="sourceLineNo">1333</span> }<a name="line.1333"></a> -<span class="sourceLineNo">1334</span> scheduler.yield(procedure);<a name="line.1334"></a> -<span class="sourceLineNo">1335</span> return;<a name="line.1335"></a> -<span class="sourceLineNo">1336</span> } catch (InterruptedException e) {<a name="line.1336"></a> -<span class="sourceLineNo">1337</span> handleInterruptedException(procedure, e);<a name="line.1337"></a> -<span class="sourceLineNo">1338</span> scheduler.yield(procedure);<a name="line.1338"></a> -<span class="sourceLineNo">1339</span> return;<a name="line.1339"></a> -<span class="sourceLineNo">1340</span> } catch (Throwable e) {<a name="line.1340"></a> -<span class="sourceLineNo">1341</span> // Catch NullPointerExceptions or similar errors...<a name="line.1341"></a> -<span class="sourceLineNo">1342</span> String msg = "CODE-BUG: Uncaught runtime exception: " + procedure;<a name="line.1342"></a> -<span class="sourceLineNo">1343</span> LOG.error(msg, e);<a name="line.1343"></a> -<span class="sourceLineNo">1344</span> procedure.setFailure(new RemoteProcedureException(msg, e));<a name="line.1344"></a> -<span class="sourceLineNo">1345</span> }<a name="line.1345"></a> -<span class="sourceLineNo">1346</span><a name="line.1346"></a> -<span class="sourceLineNo">1347</span> if (!procedure.isFailed()) {<a name="line.1347"></a> -<span class="sourceLineNo">1348</span> if (subprocs != null) {<a name="line.1348"></a> -<span class="sourceLineNo">1349</span> if (subprocs.length == 1 && subprocs[0] == procedure) {<a name="line.1349"></a> -<span class="sourceLineNo">1350</span> // quick-shortcut for a state machine like procedure<a name="line.1350"></a> -<span class="sourceLineNo">1351</span> subprocs = null;<a name="line.1351"></a> -<span class="sourceLineNo">1352</span> reExecute = true;<a name="line.1352"></a> -<span class="sourceLineNo">1353</span> } else {<a name="line.1353"></a> -<span class="sourceLineNo">1354</span> // yield the c
<TRUNCATED>