jcabrerizo commented on code in PR #1368:
URL: https://github.com/apache/brooklyn-server/pull/1368#discussion_r999266539


##########
core/src/main/java/org/apache/brooklyn/core/workflow/WorkflowErrorHandling.java:
##########
@@ -0,0 +1,144 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.brooklyn.core.workflow;
+
+import com.fasterxml.jackson.annotation.JsonInclude;
+import org.apache.brooklyn.api.mgmt.Task;
+import org.apache.brooklyn.core.mgmt.BrooklynTaskTags;
+import org.apache.brooklyn.util.core.predicates.DslPredicates;
+import org.apache.brooklyn.util.core.task.DynamicTasks;
+import org.apache.brooklyn.util.core.task.TaskTags;
+import org.apache.brooklyn.util.core.task.Tasks;
+import org.apache.brooklyn.util.exceptions.Exceptions;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.List;
+import java.util.concurrent.Callable;
+
+public class WorkflowErrorHandling implements 
Callable<WorkflowErrorHandling.WorkflowErrorHandlingResult> {
+
+    private static final Logger log = 
LoggerFactory.getLogger(WorkflowErrorHandling.class);
+
+    /*
+     * TODO ui for error handling
+     * step task's workflow tag will have ERROR_HANDLED_BY single-key map tag 
pointing at handler parent task, created in this method.
+     * error handler parent task will have 'errorHandlerForTask' field in the 
workflow tag pointing at step task, but no errorHandlerIndex.
+     * if any of the handler steps match, the parent will have 
ERROR_HANDLED_BY pointing at it, and will have a task with the workflow tag with
+     * 'errorHandlerForTask' field pointing at the parent and also 
'errorHandlerIndex' set to the index in the step's error list, but not 
ERROR_HANDLED_BY.
+     *
+     * the workflow execution context's errorHandlerContext will point at the 
error handler context,
+     * but this is cleared when the step runs, and sub-workflows there are not 
stored or replayable.
+     */
+
+    /** returns a result, or null if nothing applied (and caller should 
rethrow the error) */
+    public static Task<WorkflowErrorHandlingResult> 
createStepErrorHandlerTask(WorkflowStepDefinition step, 
WorkflowStepInstanceExecutionContext context, Task<?> stepTask, Throwable 
error) {
+        log.debug("Encountered error in step 
"+context.getWorkflowStepReference()+" '" + stepTask.getDisplayName() + "' 
(handler present): " + Exceptions.collapseText(error));
+        Task<WorkflowErrorHandlingResult> task = 
Tasks.<WorkflowErrorHandlingResult>builder().dynamic(true).displayName(context.getWorkflowStepReference()+"-error-handler")
+                .tag(BrooklynTaskTags.tagForWorkflowStepErrorHandler(context, 
null, context.getTaskId()))
+                .body(new WorkflowErrorHandling(step.getOnError(), 
context.getWorkflowExectionContext(), 
context.getWorkflowExectionContext().currentStepIndex, stepTask, error))
+                .build();
+        TaskTags.addTagDynamically(stepTask, 
BrooklynTaskTags.tagForErrorHandledBy(task));
+        log.debug("Creating step "+context.getWorkflowStepReference()+" error 
handler "+task.getDisplayName()+" in task " + task.getId());
+        return task;
+    }
+
+    /** returns a result, or null if nothing applied (and caller should 
rethrow the error) */
+    public static Task<WorkflowErrorHandlingResult> 
createWorkflowErrorHandlerTask(WorkflowExecutionContext context, Task<?> 
workflowTask, Throwable error) {
+        log.debug("Encountered error in workflow 
"+context.getWorkflowId()+"/"+context.getTaskId()+" '" + 
workflowTask.getDisplayName() + "' (handler present): " + 
Exceptions.collapseText(error));
+        Task<WorkflowErrorHandlingResult> task = 
Tasks.<WorkflowErrorHandlingResult>builder().dynamic(true).displayName(context.getWorkflowId()+"-error-handler")
+                .tag(BrooklynTaskTags.tagForWorkflowStepErrorHandler(context))
+                .body(new WorkflowErrorHandling(context.onError, context, 
null, workflowTask, error))
+                .build();
+        TaskTags.addTagDynamically(workflowTask, 
BrooklynTaskTags.tagForErrorHandledBy(task));
+        log.debug("Creating workflow 
"+context.getWorkflowId()+"/"+context.getTaskId()+" error handler 
"+task.getDisplayName()+" in task " + task.getId());
+        return task;
+    }
+
+    final List<WorkflowStepDefinition> errorOptions;
+    final WorkflowExecutionContext context;
+    final Integer stepIndexIfStepErrorHandler;
+    /** The step or the workflow task */
+    final Task<?> failedTask;
+    final Throwable error;
+
+    public WorkflowErrorHandling(List<Object> errorOptions, 
WorkflowExecutionContext context, Integer stepIndexIfStepErrorHandler, Task<?> 
failedTask, Throwable error) {
+        this.errorOptions = 
WorkflowStepResolution.resolveOnErrorSteps(context.getManagementContext(), 
errorOptions);
+        this.context = context;
+        this.stepIndexIfStepErrorHandler = stepIndexIfStepErrorHandler;
+        this.failedTask = failedTask;
+        this.error = error;
+    }
+
+    @Override
+    public WorkflowErrorHandlingResult call() throws Exception {
+        WorkflowErrorHandlingResult result = new WorkflowErrorHandlingResult();
+        Task handlerParent = Tasks.current();
+        log.debug("Starting "+handlerParent.getDisplayName()+" with "+ 
errorOptions.size()+" handler"+(errorOptions.size()!=1 ? " options" : "")+" in 
task "+handlerParent.getId());
+
+        for (int i = 0; i< errorOptions.size(); i++) {
+            // go through steps, find first that matches
+
+            WorkflowStepDefinition errorStep = errorOptions.get(i);
+
+            WorkflowStepInstanceExecutionContext handlerContext = new 
WorkflowStepInstanceExecutionContext(stepIndexIfStepErrorHandler!=null ? 
stepIndexIfStepErrorHandler : -3, errorStep, context);

Review Comment:
   why -3 as `stepIndex`? if it has some meaning, apart being <0, having a 
constant would me more declarative
   ```
   NO_ERROR_HANDLER_STEP = -3
   ```



##########
rest/rest-resources/src/main/java/org/apache/brooklyn/rest/resources/EntityResource.java:
##########
@@ -358,4 +362,42 @@ public List<Object>  getSpecList(String applicationId, 
String entityId) {
         List<SpecSummary> specTag = 
BrooklynTags.findSpecHierarchyTag(entity.tags().getTags());
         return (List<Object>) resolving(specTag).preferJson(true).resolve();
     }
+
+    @Override
+    public List<WorkflowExecutionContext> getWorkflows(String applicationId, 
String entityId) {

Review Comment:
   I think this new endpoint as the same as the other should check for 
entitlements
   ```
   // getWorkflows and replayWorkflow
           if (Entitlements.isEntitled(mgmt().getEntitlementManager(), 
Entitlements.SEE_ENTITY, entity)) {
               return EntityTransformer.entitySummary(entity, 
ui.getBaseUriBuilder());
           }
   // replayWorkflow
           if (Entitlements.isEntitled(mgmt().getEntitlementManager(), 
Entitlements.REPLAY_WORKFLOW, entity)) {
               return EntityTransformer.entitySummary(entity, 
ui.getBaseUriBuilder());
           }
   
   ```
   replayWorkflow could be also authorised by `Entitlements.INVOKE_EFFECTOR` 
instead creating a new entitlement
   



##########
core/src/main/java/org/apache/brooklyn/core/workflow/steps/SetVariableWorkflowStep.java:
##########
@@ -210,31 +209,39 @@ Object applyMathOperator(List<String> lhs0, List<String> 
rhs0, BiFunction<Intege
                 return ((Maybe)asInteger(x)).orMaybe(() -> asDouble(x)).get();
             }
 
-            Maybe<Double> lhsD = asDouble(lhs);
-            Maybe<Double> rhsD = asDouble(rhs);
-            if (lhsD.isPresent() && rhsD.isPresent()) return 
asDouble(ifDouble.apply(lhsD.get(), rhsD.get())).get();
+            if (ifDouble!=null) {
+                Maybe<Double> lhsD = asDouble(lhs);
+                Maybe<Double> rhsD = asDouble(rhs);
+                if (lhsD.isPresent() && rhsD.isPresent()) return 
asDouble(ifDouble.apply(lhsD.get(), rhsD.get())).get();
+
+                if (lhsD.isAbsent()) throw new 
IllegalArgumentException("Invalid left argument to operation '"+op+"': "+lhs0+" 
=> "+lhs);
+                if (rhsD.isAbsent()) throw new 
IllegalArgumentException("Invalid right argument to operation '"+op+"': 
"+rhs0+" = "+rhs);
+            }
 
-            if (lhsD.isAbsent())
-                throw new IllegalArgumentException("Invalid value for 
operation: "+lhs0+" = "+lhs);
-            if (rhsD.isAbsent()) throw new IllegalArgumentException("Invalid 
value for operation: "+rhs0+" = "+rhs);
+            if (lhsI.isAbsent()) throw new IllegalArgumentException("Invalid 
left argument to operation '"+op+"': "+lhs0+" => "+lhs);
+            if (rhsI.isAbsent()) throw new IllegalArgumentException("Invalid 
right argument to operation '"+op+"': "+rhs0+" = "+rhs);
 
             throw new IllegalArgumentException("Should not come here");
         }
 
-        Object handleMultiple(List<String> lhs, List<String> rhs) {
-            return applyMathOperator(lhs, rhs, (a,b)->a*b, (a,b)->a*b);
+        Object handleMultiply(List<String> lhs, List<String> rhs) {
+            return applyMathOperator("*", lhs, rhs, (a,b)->a*b, (a,b)->a*b);
         }
 
         Object handleDivide(List<String> lhs, List<String> rhs) {
-            return applyMathOperator(lhs, rhs, (a,b)->1.0*a/b, (a,b)->a/b);
+            return applyMathOperator("/", lhs, rhs, (a,b)->1.0*a/b, 
(a,b)->a/b);

Review Comment:
   It's a bit edge case, but if `b==0` it will throw an exception, better to 
control it



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to