brooklyn-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From aledsage <...@git.apache.org>
Subject [GitHub] brooklyn-server pull request #480: Config self reference fix
Date Tue, 28 Feb 2017 14:08:25 GMT
Github user aledsage commented on a diff in the pull request:

    https://github.com/apache/brooklyn-server/pull/480#discussion_r103454836
  
    --- Diff: core/src/main/java/org/apache/brooklyn/util/core/task/ValueResolver.java ---
    @@ -352,33 +357,73 @@ public T get() {
             
             //if the expected type is a closure or map and that's what we have, we're done
(or if it's null);
             //but not allowed to return a future or DeferredSupplier as the resolved value
    -        if (v==null || (!forceDeep && type.isInstance(v) && !Future.class.isInstance(v)
&& !DeferredSupplier.class.isInstance(v)))
    +        if (v==null || (!forceDeep && type.isInstance(v) && !Future.class.isInstance(v)
&& !DeferredSupplier.class.isInstance(v) && !TaskFactory.class.isInstance(v)))
                 return Maybe.of((T) v);
             
             try {
    -            if (immediately && v instanceof ImmediateSupplier) {
    -                final ImmediateSupplier<?> supplier = (ImmediateSupplier<?>)
v;
    +            boolean allowImmediateExecution = false;
    +            boolean bailOutAfterImmediateExecution = false;
    +            
    +            if (v instanceof ImmediateSupplier) {
    +                allowImmediateExecution = true;
    +                
    +            } else {
    +                if ((v instanceof TaskFactory<?>) && !(v instanceof DeferredSupplier))
{
    +                    v = ((TaskFactory<?>)v).newTask();
    +                    allowImmediateExecution = true;
    +                    bailOutAfterImmediateExecution = true;
    +                    BrooklynTaskTags.setTransient(((TaskAdaptable<?>)v).asTask());
    +                    if (isEvaluatingImmediately()) {
    +                        // not needed if executing immediately
    +                        BrooklynTaskTags.addTagDynamically( ((TaskAdaptable<?>)v).asTask(),
BrooklynTaskTags.IMMEDIATE_TASK_TAG );
    +                    }
    +                }
    +                
    +                //if it's a task or a future, we wait for the task to complete
    +                if (v instanceof TaskAdaptable<?>) {
    +                    v = ((TaskAdaptable<?>) v).asTask();
    +                }
    +            }
    +            
    +            if (allowImmediateExecution && isEvaluatingImmediately()) {
    +                // TODO could allow for everything, when evaluating immediately -- but
if the target isn't safe to run again
    +                // then we have to fail if immediate didn't work; to avoid breaking semantics
we only do that for a few cases;
    +                // might be nice to get to the point where we can break those semantics
however, 
    +                // ie weakening what getImmediate supports and making it be non-blocking,
so that bailOut=true is the default.
    +                // if: v instanceof TaskFactory -- it is safe, it's a new API (but it
is currently the only one supported);
    +                //     more than safe, we have to do it -- or add code here to cancel
tasks -- because it spawns new tasks
    +                //     (other objects passed through here don't get cancelled, because
other things might try again later;
    +                //     ie a task or future passed in here might naturally be long-running
so cancelling is wrong,
    +                //     but with a task factory generated task it would leak if we submitted
and didn't cancel!)
    --- End diff --
    
    Another failing test I wrote in `ValueResolverTest`, which leaks tasks (because the outer
task is cancelled, while the inner tasks are not).
    
    I'm a bit surprised that it fails - I had assumed that the `SequentialTask` would cancel
its children when it was cancelled.
    
    ```
        public void testTaskFactoryGetImmediatelyDoesNotBlockWithNestedTasks() {
            final int NUM_CALLS = 3;
            final AtomicInteger executingCount = new AtomicInteger();
            final List<SequentialTask<?>> outerTasks = Lists.newArrayList();
            
            TaskFactory<Task<?>> taskFactory = new TaskFactory<Task<?>>()
{
                @Override public Task<?> newTask() {
                    SequentialTask<?> result = new SequentialTask<>(ImmutableList.of(new
Callable<String>() {
                        public String call() {
                            executingCount.incrementAndGet();
                            try {
                                Time.sleep(Duration.ONE_MINUTE);
                                return "myval";
                            } finally {
                                executingCount.decrementAndGet();
                            }
                        }}));
                    outerTasks.add(result);
                    return result;
                }
            };
            for (int i = 0; i < NUM_CALLS; i++) {
                Maybe<String> result = Tasks.resolving(taskFactory).as(String.class).context(app).immediately(true).getMaybe();
                Asserts.assertTrue(result.isAbsent(), "result="+result);
            }
            // the call below default times out after 30s while the task above is still running
            Asserts.succeedsEventually(new Runnable() {
                public void run() {
                    Asserts.assertEquals(outerTasks.size(), NUM_CALLS);
                    for (Task<?> task : outerTasks) {
                        Asserts.assertTrue(task.isDone());
                        Asserts.assertTrue(task.isCancelled());
                    }
                }
            });
            Asserts.succeedsEventually(new Runnable() {
                public void run() {
                    Asserts.assertEquals(executingCount.get(), 0);
                }
            });
        }
    ```



---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

Mime
View raw message