Ejemplo n.º 1
0
    /// <inheritdoc/>
    public void Register(IExecutionTask task)
    {
        if (task is null)
        {
            throw new ArgumentNullException(nameof(task));
        }

        var start = false;

        // first we initialize the task execution state.
        // This can be done without acquiring a lock since we only
        // interact with the task object itself.
        _stateMachine.TryInitializeTask(task);
        task.IsRegistered = true;

        lock (_sync)
        {
            if (_stateMachine.RegisterTask(task))
            {
                WorkQueue work = task.IsSerial ? _serial : _work;
                work.Push(task);
                start = ShouldStartProcessing();
            }
            else
            {
                _suspended.Enqueue(task);
            }
        }

        if (start)
        {
            TryContinue();
        }
    }
 public void TaskError(IExecutionTask task, IError error)
 {
     for (var i = 0; i < _listeners.Length; i++)
     {
         _listeners[i].TaskError(task, error);
     }
 }
 public override void TaskError(
     IExecutionTask task,
     IError error
     )
 {
     _logger.LogError(error.Exception, $"During execution of the task {task} the error {ConvertErrorToString(error)} occurred.");
 }
Ejemplo n.º 4
0
        /// <inheritdoc/>
        public void Register(IReadOnlyList <IExecutionTask> tasks)
        {
            if (tasks is null)
            {
                throw new ArgumentNullException(nameof(tasks));
            }

            if (tasks.Count == 1)
            {
                Register(tasks[0]);
                return;
            }

            var started = false;

            // first we initialize the task execution state.
            // This can be done without acquiring a lock since we only
            // interact with the task object itself.
            for (var i = 0; i < tasks.Count; i++)
            {
                IExecutionTask task = tasks[i];
                _stateMachine.TryInitializeTask(task);
                task.IsRegistered = true;
            }

            lock (_sync)
            {
                var start = false;

                for (var i = 0; i < tasks.Count; i++)
                {
                    IExecutionTask task = tasks[i];

                    if (_stateMachine.RegisterTask(task))
                    {
                        WorkQueue work = task.IsSerial ? _serial : _work;
                        work.Push(task);
                        start = true;
                    }
                    else
                    {
                        _suspended.Enqueue(task);
                    }
                }

                if (start)
                {
                    started = TryStartProcessingUnsafe();
                }
            }

            if (started)
            {
                // we invoke the scale diagnostic event after leaving the lock to not block
                // if a an event listener is badly implemented.
                _diagnosticEvents.StartProcessing(_requestContext);
            }
        }
Ejemplo n.º 5
0
        public override void TaskError(IExecutionTask task, IError error)
        {
            if (error.Exception != null)
            {
                LogError(new EventId(5, "GraphQL Task Error"), error.Exception);
            }

            base.TaskError(task, error);
        }
Ejemplo n.º 6
0
        public void Enqueue(IExecutionTask executionTask)
        {
            if (executionTask is null)
            {
                throw new ArgumentNullException(nameof(executionTask));
            }

            AppendTask(ref _head, executionTask);
            IsEmpty = false;
        }
Ejemplo n.º 7
0
    /// <inheritdoc/>
    public void Register(IReadOnlyList <IExecutionTask> tasks)
    {
        if (tasks is null)
        {
            throw new ArgumentNullException(nameof(tasks));
        }

        if (tasks.Count == 1)
        {
            Register(tasks[0]);
            return;
        }

        var start = false;

        // first we initialize the task execution state.
        // This can be done without acquiring a lock since we only
        // interact with the task object itself.
        for (var i = 0; i < tasks.Count; i++)
        {
            IExecutionTask task = tasks[i];
            _stateMachine.TryInitializeTask(task);
            task.IsRegistered = true;
        }

        lock (_sync)
        {
            for (var i = 0; i < tasks.Count; i++)
            {
                IExecutionTask task = tasks[i];

                if (_stateMachine.RegisterTask(task))
                {
                    WorkQueue work = task.IsSerial ? _serial : _work;
                    work.Push(task);
                    start = true;
                }
                else
                {
                    _suspended.Enqueue(task);
                }
            }

            if (start)
            {
                start = ShouldStartProcessing();
            }
        }

        if (start)
        {
            TryContinue();
        }
    }
Ejemplo n.º 8
0
    private void ReportError(IExecutionTask task, IError error)
    {
        if (task is null)
        {
            throw new ArgumentNullException(nameof(task));
        }

        if (error is null)
        {
            throw new ArgumentNullException(nameof(error));
        }

        AssertInitialized();

        if (error is AggregateError aggregateError)
        {
            foreach (IError?innerError in aggregateError.Errors)
            {
                ReportSingle(innerError);
            }
        }
        else
        {
            ReportSingle(error);
        }

        void ReportSingle(IError singleError)
        {
            AddProcessedError(ErrorHandler.Handle(singleError));
        }

        void AddProcessedError(IError processed)
        {
            if (processed is AggregateError ar)
            {
                foreach (IError?ie in ar.Errors)
                {
                    Result.AddError(ie);
                    DiagnosticEvents.TaskError(task, ie);
                }
            }
            else
            {
                Result.AddError(processed);
                DiagnosticEvents.TaskError(task, processed);
            }
        }
    }
        public IDisposable RunTask(IExecutionTask task)
        {
            if (_listeners.Length == 0)
            {
                return(ExecutionDiagnosticEventListener.EmptyScope);
            }

            var scopes = new IDisposable[_resolverListener.Length];

            for (var i = 0; i < _resolverListener.Length; i++)
            {
                scopes[i] = _resolverListener[i].RunTask(task);
            }

            return(new AggregateActivityScope(scopes));
        }
Ejemplo n.º 10
0
    public override void CompleteTask(IQueryPlanState state, IExecutionTask task)
    {
        Debug.Assert(ReferenceEquals(task.State, this), "The task must be part of this step.");

        if (task is ResolverTask {
            ChildTasks : { Count : > 0 }
        } resolverTask)
        {
            foreach (ResolverTask?childTask in resolverTask.ChildTasks)
            {
                state.Selections.Add(childTask.Selection.Id);
            }

            state.RegisterUnsafe(resolverTask.ChildTasks);
        }
    }
        public IActivityScope RunTask(IExecutionTask task)
        {
            if (_listeners.Length == 0)
            {
                return(_empty);
            }

            var scopes = new IActivityScope[_resolverListener.Length];

            for (var i = 0; i < _resolverListener.Length; i++)
            {
                scopes[i] = _resolverListener[i].RunTask(task);
            }

            return(new AggregateActivityScope(scopes));
        }
        private void ReportError(IExecutionTask task, IError error)
        {
            if (task is null)
            {
                throw new ArgumentNullException(nameof(task));
            }

            if (error is null)
            {
                throw new ArgumentNullException(nameof(error));
            }

            AssertInitialized();
            error = ErrorHandler.Handle(error);
            Result.AddError(error);
            DiagnosticEvents.TaskError(task, error);
        }
    void IQueryPlanState.RegisterUnsafe(IReadOnlyList <IExecutionTask> tasks)
    {
        for (var i = 0; i < tasks.Count; i++)
        {
            IExecutionTask task = tasks[i];
            _stateMachine.TryInitializeTask(task);
            task.IsRegistered = true;

            if (_stateMachine.RegisterTask(task))
            {
                WorkQueue work = task.IsSerial ? _serial : _work;
                work.Push(task);
            }
            else
            {
                _suspended.Enqueue(task);
            }
        }
    }
Ejemplo n.º 14
0
        public void CopyTo(WorkQueue work, WorkQueue serial, QueryPlanStateMachine stateMachine)
        {
            IExecutionTask?head = _head;

            _head = null;

            while (head is not null)
            {
                IExecutionTask current = head;
                head         = head.Next;
                current.Next = null;

                if (stateMachine.IsSuspended(current))
                {
                    AppendTask(ref _head, current);
                }
                else
                {
                    (current.IsSerial ? serial : work).Push(current);
                }
            }

            IsEmpty = _head is null;
        }
 public void TaskError(IExecutionTask task, IError error)
 {
 }
 public IDisposable RunTask(IExecutionTask task) => this;
Ejemplo n.º 17
0
 public void Register(IExecutionTask task)
 {
     _stats.TaskCreated();
     _channel.Writer.TryWrite(task);
 }
Ejemplo n.º 18
0
 void IExecutionTaskContext.Register(IExecutionTask task)
 {
     Scheduler.Register(task);
 }
 void IExecutionTaskContext.ReportError(IExecutionTask task, IError error)
 {
     ReportError(task, error);
 }
Ejemplo n.º 20
0
 /// <summary>
 /// Defines if this execution step owns the given task.
 /// </summary>
 /// <param name="task">
 /// The execution task that is evaluated.
 /// </param>
 /// <returns>
 /// <c>true</c> if the task is owned by this step; otherwise, <c>false</c>.
 /// </returns>
 public virtual bool IsOwningTask(IExecutionTask task) => false;
Ejemplo n.º 21
0
 /// <summary>
 /// Completes a task that was spawned from this execution step.
 /// </summary>
 /// <param name="state">
 /// The current query plan execution state.
 /// </param>
 /// <param name="task">
 /// The execution task that was spawned from the execution task.
 /// </param>
 public virtual void CompleteTask(IQueryPlanState state, IExecutionTask task)
 {
 }
Ejemplo n.º 22
0
 private void ReportError(IExecutionTask task, IError error)
 {
     error = ErrorHandler.Handle(error);
     Result.AddError(error);
     DiagnosticEvents.TaskError(task, error);
 }
Ejemplo n.º 23
0
 void IExecutionTaskContext.Completed(IExecutionTask task)
 {
     AssertInitialized();
     Scheduler.Complete(task);
 }
Ejemplo n.º 24
0
 IDisposable IExecutionTaskContext.Track(IExecutionTask task) =>
 DiagnosticEvents.RunTask(task);
Ejemplo n.º 25
0
 private static void AppendTask(ref IExecutionTask?head, IExecutionTask executionTask)
 {
     executionTask.Previous = null;
     executionTask.Next     = head;
     head = executionTask;
 }
 void IExecutionTaskContext.ReportError(IExecutionTask task, Exception exception)
 {
     ReportError(task, ErrorHandler.CreateUnexpectedError(exception).Build());
 }
Ejemplo n.º 27
0
 /// <summary>
 /// Allows tasks to be added which will be executed when the Block
 /// is executed.
 /// </summary>
 /// <param name="task">Task to be executed</param>
 public void RegisterProvider(IExecutionTask task)
 {
     tasks.Add(task);
 }
 IDisposable IExecutionTaskContext.Track(IExecutionTask task)
 {
     AssertInitialized();
     return(DiagnosticEvents.RunTask(task));
 }
Ejemplo n.º 29
0
 /// <summary>
 /// Allows tasks to be added which will be executed when the Block
 /// is executed.
 /// </summary>
 /// <param name="task">Task to be executed</param>
 public void RegisterProvider(IExecutionTask task)
 {
     tasks.Add(task);
 }
        public async Task ExecuteAsync()
        {
            _stateMachine.Start();

            if (_suspended.HasWork)
            {
                _suspended.CopyTo(_work, _serial, _stateMachine);
            }

            _processing = true;
            IExecutionTask?[] buffer = _buffer;

            RESTART:
            try
            {
                do
                {
                    var work = TryTake(buffer);

                    if (work != 0)
                    {
                        if (!buffer[0] !.IsSerial)
                        {
                            for (var i = 0; i < work; i++)
                            {
                                buffer[i] !.BeginExecute(_requestAborted);
                                buffer[i] = null;
                            }
                        }
                        else
                        {
                            try
                            {
                                _batchDispatcher.DispatchOnSchedule = true;

                                for (var i = 0; i < work; i++)
                                {
                                    IExecutionTask task = buffer[i] !;
                                    task.BeginExecute(_requestAborted);
                                    await task.WaitForCompletionAsync(_requestAborted)
                                    .ConfigureAwait(false);

                                    buffer[i] = null;

                                    if (_requestAborted.IsCancellationRequested)
                                    {
                                        break;
                                    }
                                }
                            }
                            finally
                            {
                                _batchDispatcher.DispatchOnSchedule = false;
                            }
                        }
                    }
                    else
                    {
                        if (_work.HasRunningTasks || _serial.HasRunningTasks)
                        {
                            await Task.Yield();
                        }

                        break;
                    }
                } while (!_requestAborted.IsCancellationRequested);
            }
Ejemplo n.º 31
0
 /// <inheritdoc />
 public virtual IActivityScope RunTask(IExecutionTask task) => EmptyScope;