/**
     * Try to cancel an asynchronous request identified by its task.
     */
    public bool TryCancelWaitAsync(Task <bool> awaiterTask)
    {
        AsyncWaiter awaiter = null;

        lock (theLock) {
            foreach (AsyncWaiter _awaiter in asyncWaiters)
            {
                if (_awaiter.Task == awaiterTask)
                {
                    awaiter = _awaiter;
                    asyncWaiters.Remove(awaiter);
                    awaiter.done = true;
                    break;
                }
            }
        }
        // Complete cancelled async wait with TaskCanceledException
        if (awaiter != null)
        {
            // Release the resources associated with the async waiter, and
            // complete the TaskCompletionSource with TaskCanceledException.
            awaiter.Dispose(false);
            awaiter.SetCanceled();
        }
        return(awaiter != null);
    }
Exemplo n.º 2
0
        private async Task InvokeAndValidateFaultAsync(
            string actionText,
            Func <Task> action,
            CancellationToken token)
        {
            for (int i = 0; i < 5; i++)
            {
                if (this.failoverTestScenarioParameters.TimeToRun - this.GetElapsedTime() > TimeSpan.Zero && !token.IsCancellationRequested)
                {
                    this.ReportProgress("Executing FailoverScenario fault - '{0}'", actionText);

                    Log.WriteInfo(TraceType, "Testing Failover Scenario by '{0}'. Iteration {1}", actionText, i);
                    Task testActionTask = action().ContinueWith(t =>
                    {
                        this.HandleTaskComplete(t, "FaultAction", actionText);
                    });

                    await testActionTask;

                    Log.WriteInfo(TraceType, "Validating Service health and availability after '{0}'", actionText);
                    await this.FabricClient.TestManager.ValidateServiceAsync(
                        this.failoverTestScenarioParameters.PartitionSelector.ServiceName,
                        this.failoverTestScenarioParameters.MaxServiceStabilizationTimeout,
                        token).ContinueWith(t =>
                    {
                        this.HandleTaskComplete(t, "ValidateServiceAsync", actionText);
                    });

                    Log.WriteInfo(TraceType, "Waiting {0} seconds before next fault", this.failoverTestScenarioParameters.WaitTimeBetweenFaults.TotalSeconds);
                    await AsyncWaiter.WaitAsync(this.failoverTestScenarioParameters.WaitTimeBetweenFaults, token);
                }
            }
        }
Exemplo n.º 3
0
        public void Exit()
        {
            AsyncWaiter waiter = null;

            lock (this.syncRoot)
            {
                if (this.count == 0)
                {
                    throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, Resources.Error_SemaphoreCountAlreadyReachedZero));
                }

                this.count--;

                if (this.waiters.Count > 0)
                {
                    waiter = this.waiters.Dequeue();
                    this.count++;
                }
            }

            if (waiter != null)
            {
                waiter.Signal();
            }
        }
Exemplo n.º 4
0
        public void Exit()
        {
            AsyncWaiter waiter = null;

            lock (this.syncRoot)
            {
                if (this.count == 0)
                {
                    throw new InvalidOperationException(
                              "Semaphore's count has already reached zero before this operaiton. Make sure Exit() is called only after successfully entered the semaphore");
                }

                this.count--;

                if (this.waiters.Count > 0)
                {
                    waiter = this.waiters.Dequeue();
                    this.count++;
                }
            }

            if (waiter != null)
            {
                waiter.Signal();
            }
        }
 public bool WaitAsync(Action <object, TimeoutException> callback, object state, TimeSpan timeout)
 {
     if (!this.isSignaled || (this.isSignaled && (this.resetMode == EventResetMode.AutoReset)))
     {
         lock (this.syncObject)
         {
             if (this.isSignaled && (this.resetMode == EventResetMode.AutoReset))
             {
                 this.isSignaled = false;
             }
             else if (!this.isSignaled)
             {
                 AsyncWaiter item = new AsyncWaiter(this, callback, state);
                 if (this.asyncWaiters == null)
                 {
                     this.asyncWaiters = new List <AsyncWaiter>();
                 }
                 this.asyncWaiters.Add(item);
                 if (timeout != TimeSpan.MaxValue)
                 {
                     if (timerCompleteCallback == null)
                     {
                         timerCompleteCallback = new Action <object>(AsyncWaitHandle.OnTimerComplete);
                     }
                     item.SetTimer(timerCompleteCallback, item, timeout);
                 }
                 return(false);
             }
         }
     }
     return(true);
 }
    /**
     * Auxiliary methods
     */

    /**
     * Common code when cancelling an async waiter.
     */
    private void WaitCancellationHandler(object _awaiterNode, bool canceling)
    {
        LinkedListNode <AsyncWaiter> awaiterNode = (LinkedListNode <AsyncWaiter>)_awaiterNode;
        AsyncWaiter awaiter  = awaiterNode.Value;
        bool        complete = false;

        lock (theLock) {
            if (!awaiter.done)
            {
                asyncWaiters.Remove(awaiterNode);
                complete = awaiter.done = true;
            }
        }
        if (complete)
        {
            awaiter.Dispose(canceling);
            if (canceling)
            {
                awaiter.SetCanceled();
            }
            else
            {
                awaiter.SetResult(false);
            }
        }
    }
    /**
     * Asynchronous Task-based Asynchronous Pattern (TAP) interface.
     */

    /**
     * Wait asynchronously for the latch to open enabling, optionally, a timeout
     * and/or cancellation.
     */
    public Task <bool> WaitAsync(int timeout = Timeout.Infinite,
                                 CancellationToken cToken = default(CancellationToken))
    {
        // The "open" field is volatile, so the visibility is guaranteed
        if (open)
        {
            return(trueTask);
        }
        lock (theLock) {
            // After acquire the lock we must re-check the latch state, because
            // however it may have been opened by another thread.
            if (open)
            {
                return(trueTask);
            }

            // If the wait was specified as immediate, return failure
            if (timeout == 0)
            {
                return(falseTask);
            }

            // If a cancellation was already requested return a task in the Canceled state
            if (cToken.IsCancellationRequested)
            {
                return(Task.FromCanceled <bool>(cToken));
            }

            // Create a request node and insert it in requests queue
            AsyncWaiter awaiter = new AsyncWaiter(cToken);
            LinkedListNode <AsyncWaiter> awaiterNode = asyncWaiters.AddLast(awaiter);

            /**
             * Activate the specified cancelers owning the lock.
             */

            /**
             * Since the timeout handler, that runs on a thread pool's worker thread,
             * acquires the lock before access the "request.timer" and "request.cTokenRegistration"
             * these assignements will be visible to timer handler.
             */
            if (timeout != Timeout.Infinite)
            {
                awaiter.timer = new Timer(timeoutHandler, awaiterNode, timeout, Timeout.Infinite);
            }

            /**
             * If the cancellation token is already in the canceled state, the cancellation
             * delegate will run immediately and synchronously, which *causes no damage* because
             * this processing is terminal and the implicit locks can be acquired recursively.
             */
            if (cToken.CanBeCanceled)
            {
                awaiter.cTokenRegistration = cToken.Register(cancellationHandler, awaiterNode);
            }

            // Return the Task<bool> that represents the async wait
            return(awaiter.Task);
        }
    }
Exemplo n.º 8
0
        public Task<bool> WaitAsync(Int32 duration, CancellationToken token)
        {
           VerboseLog("{0:000}|{1}|async wait requested", Thread.CurrentThread.Id, _id);

            CheckDisposed();
            token.ThrowIfCancellationRequested();

            if (_sem.TryAcquire())
            {
                VerboseLog("{0:000}|{1}|async wait immediate success", Thread.CurrentThread.Id,_id);
                return Task.FromResult(true);
            }
                

            if(duration == 0)
                return Task.FromResult(false);
            
            if (_asyncWaiters == null)
            {
                lock (this)
                {
                    if (_asyncWaiters == null)
                        _asyncWaiters = new ConcurrentLinkedQueue<AsyncWaiter>();
                }
            }

            AsyncWaiter waiter = new AsyncWaiter();
            TaskCompletionSource<bool> task = new TaskCompletionSource<bool>();
            
            waiter.Task = task;

            if (duration != -1)
            {
                waiter.CancelDelay = new CancellationTokenSource();
                waiter.Delay = Task.Delay(duration, waiter.CancelDelay.Token);
                waiter.Delay.ContinueWith(new ActionContinuation(() => TimeoutAsync(waiter)));
            }

            if (token != CancellationToken.None)
            {
                waiter.CancelRegistration = token.Register(() => CancelAsync(waiter));
            }
         
            _asyncWaiters.Add(waiter);

            VerboseLog("{0:000}|{1}|async wait enqued: {2:X}; {3}", Thread.CurrentThread.Id, _id, waiter.GetHashCode(), waiter.Task.Task.Id);

            if (_wasDisposed || token.IsCancellationRequested || waiter.Delay != null && waiter.Delay.IsCompleted)
            {
                // Mitigate the above race where a finishing condition occured
                // before where able to add our waiter to the waiters list.
                if (_asyncWaiters.Remove(waiter))
                {
                    CleanUpWaiter(waiter);
                }
            }

            return task.Task;
        }
Exemplo n.º 9
0
        public void LoadHttpDataFollowedByCancel()
        {
            var request = new HttpDataSource("https://www.google.com?q=a");
            var waiter  = new AsyncWaiter();

            request.SendRequestAsync(HttpDataSource.MethodGet, HttpDataSourceResponseType.AsString);
            request.Cancel();
            request.SendRequestAsync(HttpDataSource.MethodGet, HttpDataSourceResponseType.AsString);
            Assert.AreEqual(WaiterResults.Success, waiter.Wait(10, request), "Couldn't load data");
        }
Exemplo n.º 10
0
        public bool EndEnter(IAsyncResult asyncResult)
        {
            AsyncWaiter asyncWaiter = asyncResult as AsyncWaiter;

            if (asyncWaiter != null)
            {
                return(AsyncWaiter.End(asyncResult));
            }

            return(CompletedAsyncResult <bool> .End(asyncResult));
        }
Exemplo n.º 11
0
        public void TestInit()
        {
            connection = new BayeuxConnection("http://192.168.2.179:8080/cometd/");
            connection.DataReceived     += LogDataReceived;
            connection.DataFailed       += LogDataFailed;
            connection.ConnectionFailed += LogConnectionFailed;
            connection.Timeout           = 2000;

            waiter = new AsyncWaiter();
            waiter.Reset();
        }
Exemplo n.º 12
0
        public void Set()
        {
            List <AsyncWaiter> toCallList = null;
            AsyncWaiter        toCall     = null;

            if (!_isSignaled)
            {
                lock (_syncObject)
                {
                    if (!_isSignaled)
                    {
                        if (_resetMode == EventResetMode.ManualReset)
                        {
                            _isSignaled = true;
                            Monitor.PulseAll(_syncObject);
                            toCallList    = _asyncWaiters;
                            _asyncWaiters = null;
                        }
                        else
                        {
                            if (_syncWaiterCount > 0)
                            {
                                Monitor.Pulse(_syncObject);
                            }
                            else if (_asyncWaiters != null && _asyncWaiters.Count > 0)
                            {
                                toCall = _asyncWaiters[0];
                                _asyncWaiters.RemoveAt(0);
                            }
                            else
                            {
                                _isSignaled = true;
                            }
                        }
                    }
                }
            }

            if (toCallList != null)
            {
                foreach (var waiter in toCallList)
                {
                    waiter.CancelTimer();
                    waiter.Call();
                }
            }

            if (toCall != null)
            {
                toCall.CancelTimer();
                toCall.Call();
            }
        }
Exemplo n.º 13
0
            private static void OnWaiterCompleted(IAsyncResult result)
            {
                PendingOperationTracker.WaitPendingOperationsAsyncResult asyncState = (PendingOperationTracker.WaitPendingOperationsAsyncResult)result.AsyncState;
                if (AsyncWaiter.End(result))
                {
                    asyncState.Complete(result.CompletedSynchronously);
                    return;
                }
                TimeoutException timeoutException = new TimeoutException(SRCore.TimeoutOnOperation(asyncState.timeout));

                asyncState.Complete(result.CompletedSynchronously, timeoutException);
            }
Exemplo n.º 14
0
        public void Set()
        {
            List <AsyncWaiter> toCallList = null;
            AsyncWaiter        toCall     = null;

            if (!this.isSignaled)
            {
                lock (syncObject)
                {
                    if (!this.isSignaled)
                    {
                        if (this.resetMode == EventResetMode.ManualReset)
                        {
                            this.isSignaled = true;
                            Monitor.PulseAll(syncObject);
                            toCallList        = this.asyncWaiters;
                            this.asyncWaiters = null;
                        }
                        else
                        {
                            if (this.syncWaiterCount > 0)
                            {
                                Monitor.Pulse(syncObject);
                            }
                            else if (this.asyncWaiters != null && this.asyncWaiters.Count > 0)
                            {
                                toCall = this.asyncWaiters[0];
                                this.asyncWaiters.RemoveAt(0);
                            }
                            else
                            {
                                this.isSignaled = true;
                            }
                        }
                    }
                }
            }

            if (toCallList != null)
            {
                foreach (AsyncWaiter waiter in toCallList)
                {
                    waiter.CancelTimer();
                    waiter.Call();
                }
            }

            if (toCall != null)
            {
                toCall.CancelTimer();
                toCall.Call();
            }
        }
Exemplo n.º 15
0
        public void LoadHttpData()
        {
            var request = new HttpDataSource("https://www.google.com?q=a");
            var waiter  = new AsyncWaiter();
            var filter  = new FilterDebugListener("Filter HttpDataSource", "Core.HttpDataSource");

            DebugLog.AddListener(filter);

            request.SendRequestAsync(HttpDataSource.MethodGet, HttpDataSourceResponseType.AsString);
            Assert.AreEqual(WaiterResults.Success, waiter.Wait(10, request), "Couldn't load data");

            Assert.AreNotEqual(0, filter.Count, "Invalid number of captured log messages!");
        }
Exemplo n.º 16
0
        public async Task PerformAsync(IRetryContext context, CancellationToken cancellationToken)
        {
            TestabilityTrace.TraceSource.WriteNoise("DelayStrategy", "{0}: Sleeping '{1}' seconds.", context.ActivityId, this.delayTime.TotalSeconds);

            await AsyncWaiter.WaitAsync(this.delayTime, cancellationToken);

            TimeSpan nextSleepTime = this.getNextDelayTime(this.delayTime);

            if (nextSleepTime != this.delayTime)
            {
                TestabilityTrace.TraceSource.WriteNoise("DelayStrategy", "{0}: Increasing current delay time from '{1}' to '{2}' seconds.", context.ActivityId, this.delayTime.TotalSeconds, nextSleepTime.TotalSeconds);
                this.delayTime = nextSleepTime;
            }
        }
Exemplo n.º 17
0
            public static AsyncWaiter Acquire(Action action)
            {
                if (stack.Count == 0)
                {
                    return new AsyncWaiter {
                               action = action
                    }
                }
                ;
                AsyncWaiter waiter = stack.Pop();

                waiter.action = action;
                return(waiter);
            }
Exemplo n.º 18
0
        public IAsyncResult BeginEnter(AsyncCallback callback, object state)
        {
            lock (this.syncRoot)
            {
                if (this.count < this.maxCount)
                {
                    this.count++;
                    return(new CompletedAsyncResult <bool>(true, callback, state));
                }

                AsyncWaiter asyncWaiter = new AsyncWaiter(callback, state);
                this.waiters.Enqueue(asyncWaiter);
                return(asyncWaiter);
            }
        }
        public void Set()
        {
            List <AsyncWaiter> asyncWaiters = null;
            AsyncWaiter        waiter       = null;

            if (!this.isSignaled)
            {
                lock (this.syncObject)
                {
                    if (!this.isSignaled)
                    {
                        if (this.resetMode == EventResetMode.ManualReset)
                        {
                            this.isSignaled = true;
                            Monitor.PulseAll(this.syncObject);
                            asyncWaiters      = this.asyncWaiters;
                            this.asyncWaiters = null;
                        }
                        else if (this.syncWaiterCount > 0)
                        {
                            Monitor.Pulse(this.syncObject);
                        }
                        else if ((this.asyncWaiters != null) && (this.asyncWaiters.Count > 0))
                        {
                            waiter = this.asyncWaiters[0];
                            this.asyncWaiters.RemoveAt(0);
                        }
                        else
                        {
                            this.isSignaled = true;
                        }
                    }
                }
            }
            if (asyncWaiters != null)
            {
                foreach (AsyncWaiter waiter2 in asyncWaiters)
                {
                    waiter2.CancelTimer();
                    waiter2.Call();
                }
            }
            if (waiter != null)
            {
                waiter.CancelTimer();
                waiter.Call();
            }
        }
Exemplo n.º 20
0
        public ReliableLog(
            DispatcherQueueSetting setting,
            IReliableStateManager stateManager)
            : base(nameof(ReliableLog <TMessage>))
        {
            this.setting                   = setting;
            this.stateManager              = stateManager;
            this.maxQueueLength            = setting.MaxQueueLength;
            this.recordDictionaryKeyName   = this.setting.Name + ":RecordDictionary";
            this.metaDictionaryKeyName     = this.setting.Name + ":MetaDictionary";
            this.lastCommittedIndexKeyName = this.setting.Name + ":LastCommit";
            this.checkpointKeyName         = this.setting.Name + ":Checkpoint";

            this.lastIndex       = this.lastCommittedIndex = this.currentCheckpointIndex = new RecordInfo(RecordInfo.InvalidIndex, this.maxQueueLength);
            this.inflightAppends = new List <CommitInfo>();
            this.waiter          = new AsyncWaiter();
        }
Exemplo n.º 21
0
            private static void OnWaiterSignaled(IAsyncResult result)
            {
                ConcurrentTransactionalDictionary <TKey, TValue> .TransactionalAsyncWaiter asyncState = (ConcurrentTransactionalDictionary <TKey, TValue> .TransactionalAsyncWaiter)result.AsyncState;
                Exception timeoutException = null;

                try
                {
                    if (!AsyncWaiter.End(result))
                    {
                        timeoutException = new TimeoutException();
                    }
                }
                catch (OperationCanceledException operationCanceledException)
                {
                    timeoutException = operationCanceledException;
                }
                asyncState.Complete(false, timeoutException);
            }
Exemplo n.º 22
0
        public ValueTask LockAsync()
        {
            AsyncWaiter asyncWaiter;

            lock (m_Sync)
            {
                if (m_State == State.Free)
                {
                    m_State = State.Locked;
                    return(new ValueTask());
                }

                asyncWaiter = new AsyncWaiter();
                m_WaitersQueue.Enqueue(asyncWaiter);
            }

            return(new ValueTask(asyncWaiter.Task));
        }
Exemplo n.º 23
0
            public WaitPendingOperationsAsyncResult(PendingOperationTracker owner, TimeSpan timeout, AsyncCallback callback, object state) : base(callback, state)
            {
                this.owner   = owner;
                this.timeout = timeout;
                AsyncWaiter asyncWaiter = null;

                lock (this.owner.syncLock)
                {
                    this.owner.rejectNewRequests = true;
                    if (this.owner.pendingOperationCount > (long)0)
                    {
                        asyncWaiter = new AsyncWaiter(timeout, PendingOperationTracker.WaitPendingOperationsAsyncResult.StaticOnWaiterCompleted, this);
                        this.owner.waiters.Add(asyncWaiter);
                    }
                }
                if (asyncWaiter == null)
                {
                    base.Complete(true);
                }
            }
        private static void OnTimerComplete(object state)
        {
            AsyncWaiter     item   = (AsyncWaiter)state;
            AsyncWaitHandle parent = item.Parent;
            bool            flag   = false;

            lock (parent.syncObject)
            {
                if ((parent.asyncWaiters != null) && parent.asyncWaiters.Remove(item))
                {
                    item.TimedOut = true;
                    flag          = true;
                }
            }
            item.CancelTimer();
            if (flag)
            {
                item.Call();
            }
        }
Exemplo n.º 25
0
        public void LoadHttpDataFollowedByMultipleCancel()
        {
            var request = new HttpDataSource("https://www.google.pl/search?q=a");
            var waiter  = new AsyncWaiter();

            counter = 0;
            request.DataReceived      += delegate { counter++; };
            request.DataReceiveFailed += delegate { counter++; };

            for (int i = 0; i < 10; i++)
            {
                request.SendRequestAsync(HttpDataSource.MethodGet, HttpDataSourceResponseType.AsString);

                waiter.Sleep(130);
                request.Cancel();
            }
            request.SendRequestAsync(HttpDataSource.MethodGet, HttpDataSourceResponseType.AsString);

            Assert.AreEqual(WaiterResults.Success, waiter.Wait(10, request), "Couldn't load data");
            Assert.AreEqual(1, counter, "Should receive data only once!");
        }
Exemplo n.º 26
0
        static void OnTimerComplete(object state)
        {
            AsyncWaiter     waiter     = (AsyncWaiter)state;
            AsyncWaitHandle thisPtr    = waiter.Parent;
            bool            callWaiter = false;

            lock (thisPtr.syncObject)
            {
                // If still in the waiting list (that means it hasn't been signaled)
                if (thisPtr.asyncWaiters != null && thisPtr.asyncWaiters.Remove(waiter))
                {
                    waiter.TimedOut = true;
                    callWaiter      = true;
                }
            }

            waiter.CancelTimer();

            if (callWaiter)
            {
                waiter.Call();
            }
        }
Exemplo n.º 27
0
        public bool WaitAsync(Action <object, TimeoutException> callback, object state, TimeSpan timeout)
        {
            if (!_isSignaled || (_isSignaled && _resetMode == EventResetMode.AutoReset))
            {
                lock (_syncObject)
                {
                    if (_isSignaled && _resetMode == EventResetMode.AutoReset)
                    {
                        _isSignaled = false;
                    }
                    else if (!_isSignaled)
                    {
                        AsyncWaiter waiter = new AsyncWaiter(this, callback, state);

                        if (_asyncWaiters == null)
                        {
                            _asyncWaiters = new List <AsyncWaiter>();
                        }

                        _asyncWaiters.Add(waiter);

                        if (timeout != TimeSpan.MaxValue)
                        {
                            if (s_timerCompleteCallback == null)
                            {
                                s_timerCompleteCallback = new Action <object>(OnTimerComplete);
                            }
                            waiter.SetTimer(s_timerCompleteCallback, waiter, timeout);
                        }
                        return(false);
                    }
                }
            }

            return(true);
        }
    /**
     *	Synchronous interface implemented using the asynchronous TAP interface.
     */

    /**
     * Try to cancel an asynchronous request identified by its task.
     */
    private bool CancelWaitByTask(Task <bool> awaiterTask)
    {
        AsyncWaiter awaiter = null;

        lock (theLock) {
            foreach (AsyncWaiter _awaiter in asyncWaiters)
            {
                if (_awaiter.Task == awaiterTask)
                {
                    awaiter = _awaiter;
                    asyncWaiters.Remove(_awaiter);
                    awaiter.done = true;
                    break;
                }
            }
        }
        if (awaiter != null)
        {
            // Dispose resources and complete async wait with TaskCanceledException
            awaiter.Dispose(false);
            awaiter.SetCanceled();
        }
        return(awaiter != null);
    }
Exemplo n.º 29
0
        public bool WaitAsync(Action<object, TimeoutException> callback, object state, TimeSpan timeout)
        {
            if (!this.isSignaled || (this.isSignaled && this.resetMode == EventResetMode.AutoReset))
            {
                lock (syncObject)
                {
                    if (this.isSignaled && this.resetMode == EventResetMode.AutoReset)
                    {
                        this.isSignaled = false;
                    }
                    else if (!this.isSignaled)
                    {
                        AsyncWaiter waiter = new AsyncWaiter(this, callback, state);

                        if (this.asyncWaiters == null)
                        {
                            this.asyncWaiters = new List<AsyncWaiter>();
                        }

                        this.asyncWaiters.Add(waiter);

                        if (timeout != TimeSpan.MaxValue)
                        {
                            if (timerCompleteCallback == null)
                            {
                                timerCompleteCallback = new Action<object>(OnTimerComplete);
                            }
                            waiter.SetTimer(timerCompleteCallback, waiter, timeout);
                        }
                        return false;
                    }
                }
            }

            return true;
        }
Exemplo n.º 30
0
 public TransactionalAsyncWaiter(AsyncCallback callback, object state) : base(callback, state)
 {
     this.waiter = new AsyncWaiter(TimeSpan.MaxValue, ConcurrentTransactionalDictionary <TKey, TValue> .TransactionalAsyncWaiter.onWaiterSignaled, this, false);
 }
Exemplo n.º 31
0
        public static void Wait <T>(this T operation, Action <T> action) where T : AsyncOperation
        {
            if (operation == null)
            {
                throw new ArgumentNullException("operation");
            }
            if (action == null)
            {
                throw new ArgumentNullException("action");
            }

            operations.Add(new KeyValuePair <AsyncOperation, Action <AsyncOperation> >(operation, AsyncWaiter <T> .Acquire(action).Emit));
        }
Exemplo n.º 32
0
            public override async Task <ActionStateBase> RunAsync(CancellationToken cancellationToken, ServiceInternalFaultInfo serviceInternalFaultInfo)
            {
                InvokeQuorumLossState state = Convert(this.State);

                Guid partitionId = state.Info.PartitionId;
                List <Tuple <string, string> > unreliableTransportInfo = state.Info.UnreliableTransportInfo;
                List <long> targetReplicas = state.Info.ReplicaIds;

                var unreliableTransportTaskList = new List <Task>();
                List <Tuple <string, string> > unreliableTransportInfoList = new List <Tuple <string, string> >();

                foreach (Tuple <string, string> ut in unreliableTransportInfo)
                {
                    string nodeName     = ut.Item1;
                    string behaviorName = ut.Item2;

                    System.Fabric.Common.UnreliableTransportBehavior behavior = new System.Fabric.Common.UnreliableTransportBehavior("*", "StatefulServiceReopen");
                    behavior.AddFilterForPartitionId(partitionId);

                    TestabilityTrace.TraceSource.WriteInfo(StepBase.TraceType, "{0} - applying '{1}'", this.State.OperationId, behaviorName);

                    unreliableTransportTaskList.Add(FabricClientRetryHelper.ExecuteFabricActionWithRetryAsync(
                                                        () => this.FabricClient.TestManager.AddUnreliableTransportBehaviorAsync(
                                                            nodeName,
                                                            behaviorName,
                                                            behavior,
                                                            this.RequestTimeout,
                                                            cancellationToken),
                                                        this.OperationTimeout,
                                                        cancellationToken));
                }

                await Task.WhenAll(unreliableTransportTaskList).ConfigureAwait(false);

                // Bug#2271465 - Unreliable transport through API should return only once the behavior has been successfully applied
                await Task.Delay(TimeSpan.FromSeconds(5.0), cancellationToken).ConfigureAwait(false);

                List <Task> tasks = new List <Task>();

                foreach (long replicaId in targetReplicas)
                {
                    ReplicaSelector replicaSelector = ReplicaSelector.ReplicaIdOf(PartitionSelector.PartitionIdOf(this.partitionSelector.ServiceName, partitionId), replicaId);

                    TestabilityTrace.TraceSource.WriteInfo(StepBase.TraceType, "{0} - faulting replica with id={1}", this.State.OperationId, replicaId);
                    Task task = FaultAnalysisServiceUtility.RestartReplicaAsync(this.FabricClient, replicaSelector, CompletionMode.DoNotVerify, this.RequestTimeout, this.OperationTimeout, cancellationToken);
                    tasks.Add(task);
                }

                await Task.WhenAll(tasks).ConfigureAwait(false);

                ActionTest.PerformInternalServiceFaultIfRequested(this.State.OperationId, serviceInternalFaultInfo, this.State, cancellationToken, true);

                TestabilityTrace.TraceSource.WriteInfo(StepBase.TraceType, "{0} - keeping partition in quorum loss for '{1}'", this.State.OperationId, state.Info.QuorumLossDuration);
                await Task.Delay(state.Info.QuorumLossDuration, cancellationToken).ConfigureAwait(false);

                TimeoutHelper timeoutHelper = new TimeoutHelper(this.OperationTimeout);

                bool conditionSatisfied = false;

                int quorumLossCheckRetries = FASConstants.QuorumLossCheckRetryCount;

                do
                {
                    TestabilityTrace.TraceSource.WriteInfo(StepBase.TraceType, "{0} - checking PartitionStatus", this.State.OperationId);
                    ServicePartitionList partitionsResult = await FabricClientRetryHelper.ExecuteFabricActionWithRetryAsync(
                        () => this.FabricClient.QueryManager.GetPartitionListAsync(
                            this.partitionSelector.ServiceName,
                            null,
                            this.RequestTimeout,
                            cancellationToken),
                        this.OperationTimeout,
                        cancellationToken).ConfigureAwait(false);

                    foreach (StatefulServicePartition partition in partitionsResult)
                    {
                        if (partition.PartitionInformation.Id == partitionId)
                        {
                            if (partition.PartitionStatus == ServicePartitionStatus.InQuorumLoss)
                            {
                                conditionSatisfied = true;
                                break;
                            }
                        }
                    }

                    await AsyncWaiter.WaitAsync(TimeSpan.FromSeconds(5), cancellationToken).ConfigureAwait(false);
                }while (!conditionSatisfied && quorumLossCheckRetries-- > 0);

                if (!conditionSatisfied)
                {
                    string error = string.Format(CultureInfo.InvariantCulture, "{0} - Service could not induce quorum loss for service '{1}', partition '{2}'. Please retry", this.State.OperationId, this.partitionSelector.ServiceName, partitionId);
                    TestabilityTrace.TraceSource.WriteWarning(StepBase.TraceType, error);

                    throw new FabricTransientException("The operation could not be performed, please retry", FabricErrorCode.NotReady);
                }

                await QuorumLossStepsFactory.RemoveUTAsync(this.FabricClient, this.State, this.RequestTimeout, this.OperationTimeout, cancellationToken);

                state.StateProgress.Push(StepStateNames.CompletedSuccessfully);

                return(state);
            }
Exemplo n.º 33
0
        private void TimeoutAsync(AsyncWaiter waiter)
        {
            if (waiter.CancelDelay.IsCancellationRequested)
                return;
            VerboseLog("{0:000}|{1}|async wait timed out", Thread.CurrentThread.Id,_id);

            if (_asyncWaiters.Remove(waiter))
            {
                CleanUpWaiter(waiter);
            }

            waiter.Task.TrySetResult(false);
        }
Exemplo n.º 34
0
        /// <summary>
        /// Will set ObjectedDisposedException is we were disposed.
        /// </summary>
        private void CleanUpWaiter(AsyncWaiter waiter)
        {
            waiter.CancelRegistration.Dispose();

            if (waiter.Delay != null)
            {
                waiter.CancelDelay.Cancel();
                waiter.Delay.Dispose();
            }

            if (_wasDisposed)
            {
                VerboseLog("{0:000}|{1}|disposing async waiter: {2}", Thread.CurrentThread.Id, _id, waiter.Task.Task.Id);
                waiter.Task.TrySetException(new ObjectDisposedException(GetType().Name));
            }
        }
Exemplo n.º 35
0
        private void CancelAsync(AsyncWaiter waiter)
        {
            VerboseLog("{0:000}|{1}|async wait cancelled", Thread.CurrentThread.Id, _id);

            if (_asyncWaiters.Remove(waiter))
            {
                CleanUpWaiter(waiter);
            }
            waiter.Task.TrySetCanceled();
        }