Пример #1
0
 public void OperationState_Can_Concurrently_Get_Timeout_And_Response()
 {
     var counter = 0;
     var timedOutReceived = 0;
     TestHelper.Invoke(() =>
     {
         var clientCallbackCounter = 0;
         Action<Exception, Response> clientCallback = (ex, r) =>
         {
             Interlocked.Increment(ref clientCallbackCounter);
         };
         var state = new OperationState(clientCallback);
         var actions = new Action[]
         {
             () => state.SetTimedOut(new OperationTimedOutException(new IPEndPoint(0, 1), 200), () => Interlocked.Increment(ref timedOutReceived)),
             () => { state.InvokeCallback(null); }
         };
         if ((counter++)%2 == 0)
         {
             //invert order
             actions = actions.Reverse().ToArray();
         }
         TestHelper.ParallelInvoke(actions);
         //Allow callbacks to be called using the default scheduler
         Thread.Sleep(200);
         Assert.AreEqual(1, clientCallbackCounter);
     }, 10);
     Trace.WriteLine(timedOutReceived);
 }
 public OperationStatusInformation(OperationState state,
     int progress = 0,
     string failureMessage = null)
 {
     State = state;
     Progress = progress;
     FailureMessage = failureMessage;
 }
Пример #3
0
        public virtual bool IsDependencyResolved( 
			ProcessState ps, 
			OperationState os, 
		    OperationState changedOpState, 
		    bool satisfyDependency )
        {
            throw new NotImplementedException();
        }
Пример #4
0
 public void OperationState_Cancel_Should_Never_Callback_Client()
 {
     var clientCallbackCounter = 0;
     Action<Exception, Response> clientCallback = (ex, r) =>
     {
         Interlocked.Increment(ref clientCallbackCounter);
     };
     var state = new OperationState(clientCallback);
     state.Cancel();
     state.InvokeCallback(null);
     //Allow callbacks to be called using the default scheduler
     Thread.Sleep(20);
     Assert.AreEqual(0, clientCallbackCounter);
 }
Пример #5
0
 public virtual void Complete(bool completedSynchronously, Exception exception)
 {
     // The callback will be invoked only if completedSynchronously is false.
     // It is the responsibility of the caller or callback to throw the exception.
     this.exception = exception;
     if (completedSynchronously)
     {
         this.State = OperationState.CompletedSynchronously;
     }
     else
     {
         this.State = OperationState.CompletedAsynchronously;
         this.callback(this);
     }
 }
        private void AsyncWorkerCallback(object state)
        {
            _state = OperationState.RunningAsync;

            if (_state == OperationState.Inactive || 
                _routine == null || 
                _routine.OperatingState != RadicalCoroutineOperatingState.Active || 
                _routine.OperationStack.CurrentOperation != this)
            {
                this.Clear();
                return;
            }
            
        WorkerLoopback:
            var op = _routine.OperationStack.PeekSubOperation();
            if (op == null)
            {
                this.Clear();
                return;
            }
            
            object yieldObject;
            if(op.Tick(out yieldObject))
            {
                if(yieldObject == RadicalTask.JumpToAsync && 
                   _state == OperationState.RunningAsync && 
                   _routine != null && 
                   _routine.OperatingState == RadicalCoroutineOperatingState.Active &&
                   _routine.OperationStack.CurrentOperation == this)
                {
                    goto WorkerLoopback;
                }
                else
                {
                    _yieldObject = yieldObject;
                    _state = OperationState.WaitingOnYield;
                }
            }
            else
            {
                if(_routine.OperationStack.PeekSubOperation() == op) _routine.OperationStack.PopSubOperation();
                _yieldObject = null;
                _state = OperationState.Inactive;
            }
        }
Пример #7
0
        public void OperationStateAppendsBuffers()
        {
            var readBuffer = new byte[256];
            var writeBuffer = new byte[256];
            for (byte i = 1; i < 255; i++)
            {
                writeBuffer[i] = i;
            }
            var header = new FrameHeader
            {
                //256 bytes
                Len = new byte[] { 0, 0, 1, 0}
            };
            var operationState = new OperationState();
            operationState.Header = header;
            operationState.AppendBody(writeBuffer, 0, 256);

            operationState.BodyStream.Position = 0;
            operationState.BodyStream.Read(readBuffer, 0, 256);
            Assert.AreEqual(writeBuffer, readBuffer);


            operationState = new OperationState();
            operationState.Header = header;
            operationState.AppendBody(writeBuffer, 0, 100);
            operationState.AppendBody(writeBuffer, 100, 100);
            operationState.AppendBody(writeBuffer, 200, 50);
            operationState.AppendBody(writeBuffer, 250, 6);

            operationState.BodyStream.Position = 0;
            operationState.BodyStream.Read(readBuffer, 0, 256);
            Assert.AreEqual(writeBuffer, readBuffer);

            operationState.BodyStream.Position = 0;
            operationState.BodyStream.Read(readBuffer, 0, 128);
            operationState.BodyStream.Read(readBuffer, 128, 128);
            Assert.AreEqual(writeBuffer, readBuffer);
        }
 public FormOperationController(Version parserVersion, string location, string status, DataGridView dgv) : base(parserVersion, location, status)
 {
     ButtonText = "Parse";
     State      = OperationState.Ready;
     _dgv       = dgv;
 }
Пример #9
0
 public OperationManager(int threadsPerWorker)
 {
     workers = new Dictionary<RootSet, OperationWorker>();
     this.threadsPerWorker = threadsPerWorker;
     state = OperationState.Initialized;
 }
Пример #10
0
 /// <summary>
 /// Start the OperationWorker, or resume from a paused state.
 /// </summary>
 public void Start()
 {
     if (State != OperationState.Paused)
     {
         foreach (Thread t in workerThreads)
         {
             t.Start();
         }
     }
     state = OperationState.Running;
 }
Пример #11
0
 public virtual void Cancel()
 {
     State = OperationState.Canceled;
     NotifyCompletion();
 }
Пример #12
0
 private void NormalRunnable(OperationContext context, string statusText)
 {
     context.NotifyOnCurrentState(statusText, Runnability.Runnable(), ErrorInfo.None());
     _operationState = _operationStatesFactory.RunnableState();
 }
Пример #13
0
 private void rbExchange_CheckedChanged(object sender, EventArgs e)
 {
     if (rbExchange.Checked)
     {
         operationState = OperationState.ExchangeMode;
     }
 }
Пример #14
0
        public virtual IMemberPermission AddMemberPermission <TSource, TargetType>(SecurityOperation operation, OperationState state, string memberName, Expression <Func <TSource, TargetType, bool> > criteria) where TSource : BaseSecurityDbContext
        {
            if (operation.HasFlag(SecurityOperation.Create))
            {
                throw new ArgumentException("The create value of the 'operations' parameter is incorrect in this context. Only the Read and Write operations can be granted by a member permission.");
            }

            if (operation.HasFlag(SecurityOperation.Delete))
            {
                throw new ArgumentException("The delete value of the 'operations' parameter is incorrect in this context. Only the Read and Write operations can be granted by a member permission.");
            }

            Type         targetType   = typeof(TargetType);
            PropertyInfo targetMember = targetType.GetProperty(memberName);

            if (targetMember == null)
            {
                throw new ArgumentException(string.Format("{0} type doesn't contain {1} property.", targetType.Name, memberName));
            }

            var memberPermission = new MemberPermission <TSource, TargetType>(operation, state, memberName, criteria);

            memberPermission.Type           = typeof(TargetType);
            memberPermission.Operations     = operation;
            memberPermission.OperationState = state;
            permissions.Add(memberPermission);
            return(memberPermission);
        }
Пример #15
0
        private void RunWriteQueueAction()
        {
            //Dequeue all items until threshold is passed
            long totalLength = 0;
            RecyclableMemoryStream stream = null;
            var timestamp = GetTimestamp();

            while (totalLength < Connection.CoalescingThreshold)
            {
                OperationState state = null;
                while (_writeQueue.TryDequeue(out var tempState))
                {
                    if (tempState.CanBeWritten())
                    {
                        state = tempState;
                        break;
                    }

                    DecrementInFlight();
                }

                if (state == null)
                {
                    //No more items in the write queue
                    break;
                }

                if (!_freeOperations.TryPop(out short streamId))
                {
                    //Queue it up for later.
                    _writeQueue.Enqueue(state);
                    //When receiving the next complete message, we can process it.
                    Connection.Logger.Info("Enqueued, no streamIds available. If this message is recurrent consider configuring more connections per host or lower the pressure");
                    break;
                }
                Connection.Logger.Verbose("Sending #{0} for {1} to {2}", streamId, state.Request.GetType().Name, EndPoint.EndpointFriendlyName);
                if (_isCanceled)
                {
                    DecrementInFlight();
                    state.InvokeCallback(RequestError.CreateClientError(new SocketException((int)SocketError.NotConnected), true), timestamp);
                    break;
                }
                _pendingOperations.AddOrUpdate(streamId, state, (k, oldValue) => state);
                var startLength = stream?.Length ?? 0;
                try
                {
                    //lazy initialize the stream
                    stream = stream ?? (RecyclableMemoryStream)Configuration.BufferPool.GetStream(Connection.StreamWriteTag);
                    var frameLength = state.WriteFrame(streamId, stream, Serializer, timestamp);
                    _connectionObserver.OnBytesSent(frameLength);
                    totalLength += frameLength;
                }
                catch (Exception ex)
                {
                    //There was an error while serializing or begin sending
                    Connection.Logger.Error(ex);
                    //The request was not written, clear it from pending operations
                    RemoveFromPending(streamId);
                    //Callback with the Exception
                    state.InvokeCallback(RequestError.CreateClientError(ex, true), timestamp);

                    //Reset the stream to before we started writing this frame
                    stream?.SetLength(startLength);
                    break;
                }
            }
            if (totalLength == 0L)
            {
                // Nothing to write, set the queue as not running
                Interlocked.CompareExchange(ref _writeState, Connection.WriteStateInit, Connection.WriteStateRunning);
                // Until now, we were preventing other threads to running the queue.
                // Check if we can now write:
                // a read could have finished (freeing streamIds) or new request could have been added to the queue
                if (!_freeOperations.IsEmpty && !_writeQueue.IsEmpty)
                {
                    //The write queue is not empty
                    //An item was added to the queue but we were running: try to launch a new queue
                    RunWriteQueue();
                }
                if (stream != null)
                {
                    //The stream instance could be created if there was an exception while generating the frame
                    stream.Dispose();
                }
                return;
            }
            //Write and close the stream when flushed
            // ReSharper disable once PossibleNullReferenceException : if totalLength > 0 the stream is initialized
            _tcpSocket.Write(stream, () => stream.Dispose());
        }
Пример #16
0
        public virtual IObjectPermission AddObjectPermission <TSource, TargetType>(SecurityOperation operation, OperationState state, Expression <Func <TSource, TargetType, bool> > criteria) where TSource : BaseSecurityDbContext
        {
            var objectPermission = new ObjectPermission <TSource, TargetType>(operation, state, criteria);

            permissions.Add(objectPermission);
            return(objectPermission);
        }
Пример #17
0
        public override int Read(byte[] buffer, int offset, int count)
        {
            int read = _stream.Read(buffer, offset, count);

            if (_opeState != OperationState.Read)
            {
                _opeState = OperationState.Read;
                Console.WriteLine();
                Console.Write("READ: ");
            }

            for (int i = 0; i < read; ++i)
            {
                Console.Write("{0:X2}", buffer[i]);
            }

            return read;
        }
Пример #18
0
        public override void Write(byte[] buffer, int offset, int count)
        {
            if (_opeState != OperationState.Write)
            {
                _opeState = OperationState.Write;
                Console.WriteLine();
                Console.Write("WRITE: ");
            }

            for (int i = 0; i < count; ++i)
            {
                Console.Write("{0:X2}", buffer[offset + i]);
            }

            _stream.Write(buffer, offset, count);
        }
 public void PurgeTombstones(OperationState result)
 {
     throw new NotImplementedException("Purge tombstones is not supported for Command Line Smuggler");
 }
Пример #20
0
        void OperationStateObserver.Failure(Exception exception, OperationContext context)
        {
            context.NotifyOnCurrentState("Failure", Runnability.Runnable(), ErrorInfo.From(exception));

            _operationState = _operationStatesFactory.RunnableState();
        }
Пример #21
0
 /// <summary>
 /// This method is called to notify that the content stream for a batch operation has been requested.
 /// </summary>
 void IODataStreamListener.StreamRequested()
 {
     this.operationState = OperationState.StreamRequested;
 }
 public VBGet(VBucketNodeLocator locator, string key)
     : base(key)
 {
     this.locator = locator;
     this.state   = OperationState.Unspecified;
 }
Пример #23
0
 public void Dispose()
 {
     state = OperationState.Terminated;
 }
Пример #24
0
 private void Clear()
 {
     _state       = OperationState.Inactive;
     _routine     = null;
     _yieldObject = null;
 }
        internal static bool GuessResponseState(BinaryResponse response, out OperationState state)
        {
            switch (response.StatusCode)
            {
                case 0: state = OperationState.Success;
                    return true;
                case 7: state = OperationState.InvalidVBucket;
                    break;
                default: state = OperationState.Failure;
                    break;
            }

            return false;
        }
 bool IRadicalYieldInstruction.Tick(out object yieldObject)
 {
     switch(_state)
     {
         case OperationState.Inactive:
             if (_routine != null && SPThreadPool.QueueUserWorkItem(this.AsyncWorkerCallback))
             {
                 _state = OperationState.Initializing;
                 yieldObject = null;
                 return true;
             }
             else
             {
                 yieldObject = null;
                 return false;
             }
         case OperationState.Initializing:
         case OperationState.RunningAsync:
             yieldObject = null;
             return true;
         case OperationState.WaitingOnYield:
             yieldObject = _yieldObject;
             this.Clear();
             return true;
         default:
             yieldObject = null;
             return false;
     }
 }
Пример #27
0
        protected void SetError(Exception ex)
        {
            if (ex == null)
                throw new ArgumentNullException("ex");

            Error = ex;
            State = OperationState.Faulted;
            NotifyCompletion();
        }
Пример #28
0
 public override Task ExportDeletions(JsonTextWriter jsonWriter, OperationState result, LastEtagsInfo maxEtagsToFetch)
 {
     throw new NotSupportedException("Exporting deletions is not supported for Command Line Smuggler");
 }
Пример #29
0
        public DynamoDBFlatConfig(DynamoDBOperationConfig operationConfig, DynamoDBContextConfig contextConfig)
        {
            if (operationConfig == null)
                operationConfig = _emptyOperationConfig;
            if (contextConfig == null)
                contextConfig = _emptyContextConfig;

            bool consistentRead = operationConfig.ConsistentRead ?? contextConfig.ConsistentRead ?? false;
            bool skipVersionCheck = operationConfig.SkipVersionCheck ?? contextConfig.SkipVersionCheck ?? false;
            bool ignoreNullValues = operationConfig.IgnoreNullValues ?? contextConfig.IgnoreNullValues ?? false;
            string overrideTableName =
                !string.IsNullOrEmpty(operationConfig.OverrideTableName) ? operationConfig.OverrideTableName : string.Empty;
            string tableNamePrefix =
                !string.IsNullOrEmpty(operationConfig.TableNamePrefix) ? operationConfig.TableNamePrefix :
                !string.IsNullOrEmpty(contextConfig.TableNamePrefix) ? contextConfig.TableNamePrefix : string.Empty;
            bool backwardQuery = operationConfig.BackwardQuery ?? false;
            string indexName =
                !string.IsNullOrEmpty(operationConfig.IndexName) ? operationConfig.IndexName : DefaultIndexName;
            List<ScanCondition> queryFilter = operationConfig.QueryFilter ?? new List<ScanCondition>();
            ConditionalOperatorValues conditionalOperator = operationConfig.ConditionalOperator;
            DynamoDBEntryConversion conversion = operationConfig.Conversion ?? contextConfig.Conversion ?? DynamoDBEntryConversion.CurrentConversion;

            ConsistentRead = consistentRead;
            SkipVersionCheck = skipVersionCheck;
            IgnoreNullValues = ignoreNullValues;
            OverrideTableName = overrideTableName;
            TableNamePrefix = tableNamePrefix;
            BackwardQuery = backwardQuery;
            IndexName = indexName;
            QueryFilter = queryFilter;
            ConditionalOperator = conditionalOperator;
            Conversion = conversion;

            State = new OperationState();
        }
Пример #30
0
 /// <summary>
 /// This method is called to notify that the content stream of a batch operation has been disposed.
 /// </summary>
 void IODataBatchOperationListener.BatchOperationContentStreamDisposed()
 {
     this.operationState = OperationState.StreamDisposed;
 }
 public void ToCancelAndClearState()
 {
     ToCancelState();
     State = OperationState.ClearOnCancel;
 }
Пример #32
0
        public override async Task ExportDeletions(SmugglerJsonTextWriter jsonWriter, OperationState result, LastEtagsInfo maxEtagsToFetch)
        {
            jsonWriter.WritePropertyName("DocsDeletions");
            jsonWriter.WriteStartArray();
            result.LastDocDeleteEtag = await Operations.ExportDocumentsDeletion(jsonWriter, result.LastDocDeleteEtag, maxEtagsToFetch.LastDocDeleteEtag.IncrementBy(1)).ConfigureAwait(false);

            jsonWriter.WriteEndArray();

            jsonWriter.WritePropertyName("AttachmentsDeletions");
            jsonWriter.WriteStartArray();
            result.LastAttachmentsDeleteEtag = await Operations.ExportAttachmentsDeletion(jsonWriter, result.LastAttachmentsDeleteEtag, maxEtagsToFetch.LastAttachmentsDeleteEtag.IncrementBy(1)).ConfigureAwait(false);

            jsonWriter.WriteEndArray();
        }
 public OperationResult(OperationState state, string message = "")
 {
     State   = state;
     Message = message;
 }
 private void Clear()
 {
     _state = OperationState.Inactive;
     _routine = null;
     _yieldObject = null;
 }
Пример #35
0
 /// <summary>
 /// This method is called to notify that the content stream for a batch operation has been requested.
 /// </summary>
 /// <returns>
 /// A task representing any action that is running as part of the status change of the reader;
 /// null if no such action exists.
 /// </returns>
 Task IODataBatchOperationListener.BatchOperationContentStreamRequestedAsync()
 {
     this.operationState = OperationState.StreamRequested;
     return(TaskUtils.CompletedTask);
 }
Пример #36
0
        private bool resolveDep( OperationState dependentState, OperationState potentialDependency, bool satisfyDependency )
        {
            if( potentialDependency.Operation == operation && potentialDependency.State == operationState )
                {
                    //we are a match.  Next we want to ensure that any given dependency is only associated to one other operation state
                    //of a given operation, that is to say an operation can be a dependent to more than one KIND of operation, but only
                    //fulfill a dependency ONCE for a specific operation.

                    //first assume we have a valid slot.
                    bool hasValidSlot = true;

                    //loop through all the prexisting dependents of this potential dependency to make sure there is a valid slot.
                    foreach( OperationState stateSlot in potentialDependency.DependentStates )
                    {
                        //if this dependent's operation equals our target op, and the state slot is NOT the target opstate, this dependency
                        //has already been consumed by a different dependent of the same operation.
                    if( stateSlot.Operation == dependentState.Operation && stateSlot.Id != dependentState.Id )
                        {
                            log.Debug("DEPEDENDANT SLOT CONSUMED ", stateSlot.Operation.Name );
                            hasValidSlot = false;
                        return false;
                        }
                    }

                    if( hasValidSlot )
                    {
                        log.Debug("DEPENDENCY ", operation.Name, " RESOLVED FOR ", dependentState.Id, " BY ", potentialDependency.Id );
                        //we have found a valid operationstate to satisfy this dependency.
                        if( satisfyDependency )
                        {
                            potentialDependency.DependentStates.Add( dependentState );
                            potentialDependency.SaveRelations("DependentStates");
                        }

                    return true;
                    }
                }
            return false;
        }
Пример #37
0
        /// <summary>
        /// Continues reading from the batch message payload.
        /// </summary>
        /// <returns>true if more items were read; otherwise false.</returns>
        private bool ReadImplementation()
        {
            Debug.Assert(this.operationState != OperationState.StreamRequested, "Should have verified that no operation stream is still active.");

            switch (this.State)
            {
            case ODataBatchReaderState.Initial:
                // The stream should be positioned at the beginning of the batch content,
                // that is before the first boundary (or the preamble if there is one).
                this.batchReaderState = this.SkipToNextPartAndReadHeaders();
                break;

            case ODataBatchReaderState.Operation:
                // When reaching this state we already read the MIME headers of the operation.
                // Clients MUST call CreateOperationRequestMessage
                // or CreateOperationResponseMessage to read at least the headers of the operation.
                // This is important since we need to read the ContentId header (if present) and
                // add it to the URL resolver.
                if (this.operationState == OperationState.None)
                {
                    // No message was created; fail
                    throw new ODataException(Strings.ODataBatchReader_NoMessageWasCreatedForOperation);
                }

                // Reset the operation state; the operation state only
                // tracks the state of a batch operation while in state Operation.
                this.operationState = OperationState.None;

                // Also add a pending ContentId header to the URL resolver now. We ensured above
                // that a message has been created for this operation and thus the headers (incl.
                // a potential content ID header) have been read.
                if (this.contentIdToAddOnNextRead != null)
                {
                    this.urlResolver.AddContentId(this.contentIdToAddOnNextRead);
                    this.contentIdToAddOnNextRead = null;
                }

                // When we are done with an operation, we have to skip ahead to the next part
                // when Read is called again. Note that if the operation stream was never requested
                // and the content of the operation has not been read, we'll skip it here.
                Debug.Assert(this.operationState == OperationState.None, "Operation state must be 'None' at the end of the operation.");
                this.batchReaderState = this.SkipToNextPartAndReadHeaders();
                break;

            case ODataBatchReaderState.ChangesetStart:
                // When at the start of a changeset, skip ahead to the first operation in the
                // changeset (or the end boundary of the changeset).
                Debug.Assert(this.batchStream.ChangeSetBoundary != null, "Changeset boundary must have been set by now.");
                this.batchReaderState = this.SkipToNextPartAndReadHeaders();
                break;

            case ODataBatchReaderState.ChangesetEnd:
                // When at the end of a changeset, reset the changeset boundary and the
                // changeset size and then skip to the next part.
                this.ResetChangeSetSize();
                this.batchStream.ResetChangeSetBoundary();
                this.batchReaderState = this.SkipToNextPartAndReadHeaders();
                break;

            case ODataBatchReaderState.Exception:        // fall through
            case ODataBatchReaderState.Completed:
                Debug.Assert(false, "Should have checked in VerifyCanRead that we are not in one of these states.");
                throw new ODataException(Strings.General_InternalError(InternalErrorCodes.ODataBatchReader_ReadImplementation));

            default:
                Debug.Assert(false, "Unsupported reader state " + this.State + " detected.");
                throw new ODataException(Strings.General_InternalError(InternalErrorCodes.ODataBatchReader_ReadImplementation));
            }

            return(this.batchReaderState != ODataBatchReaderState.Completed && this.batchReaderState != ODataBatchReaderState.Exception);
        }
Пример #38
0
        public override bool IsDependencyResolved(
			ProcessState ps, 
			OperationState dependentState, 
			OperationState changedOpState, 
			bool satisfyDependency)
        {
            bool found = false;

            //loop through all other operationstates for the task and look to see if any are a match for operation & state
            if( changedOpState != null )
            {
                found = resolveDep( dependentState, changedOpState, satisfyDependency );
            }

            //otherwise loop through all the op states and see if the dep is satisfied.
            if( ! found )
            {

                //loop through all other operationstates for the process state and look to see if any are a match for operation & state
            foreach( OperationState potentialDependency in ps.Operations )
                {
                    found = resolveDep(dependentState, potentialDependency, satisfyDependency);
                    if( found )
                        break;
                }
            }
            //log.Debug( "returning ", found != IsNonDependency );
            return found != IsNonDependency;
        }
Пример #39
0
 /// <summary>
 /// Sends a new request using the active connection
 /// </summary>
 private void Send(IRequest request, Action <Exception, Response> callback)
 {
     _operation = _connection.Send(request, callback);
 }
Пример #40
0
        public void FeedbackInfo(OperationState state, DatabaseObject dbObject)
        {
            string message = $"{state.ToString()}{(state == OperationState.Begin ? " to" : "")} generate script for { StringHelper.GetFriendlyTypeName(dbObject.GetType().Name).ToLower() } \"{dbObject.Name}\".";

            this.Feedback(FeedbackInfoType.Info, message);
        }
Пример #41
0
        public Task <IOperationResult> AddOperation(
            DocumentDatabase database,
            string description,
            OperationType operationType,
            Func <Action <IOperationProgress>, Task <IOperationResult> > taskFactory,
            long id,
            IOperationDetailedDescription detailedDescription = null,
            OperationCancelToken token = null)
        {
            var operationState = new OperationState
            {
                Status = OperationStatus.InProgress
            };

            var notification = new OperationStatusChange
            {
                OperationId = id,
                State       = operationState
            };

            var operationDescription = new OperationDescription
            {
                Description         = description,
                TaskType            = operationType,
                StartTime           = SystemTime.UtcNow,
                DetailedDescription = detailedDescription
            };

            var operation = new Operation
            {
                Database    = database,
                Id          = id,
                Description = operationDescription,
                Token       = token,
                State       = operationState
            };

            void ProgressNotification(IOperationProgress progress)
            {
                notification.State.Progress = progress;
                RaiseNotifications(notification, operation);
            }

            operation.Task = Task.Run(() => taskFactory(ProgressNotification));

            operation.Task.ContinueWith(taskResult =>
            {
                operationDescription.EndTime = SystemTime.UtcNow;
                operationState.Progress      = null;

                if (taskResult.IsCanceled)
                {
                    operationState.Result = null;
                    operationState.Status = OperationStatus.Canceled;
                }
                else if (taskResult.IsFaulted)
                {
                    var innerException = taskResult.Exception.ExtractSingleInnerException();

                    var isConflict = innerException is DocumentConflictException || innerException is ConcurrencyException;
                    var status     = isConflict ? HttpStatusCode.Conflict : HttpStatusCode.InternalServerError;

                    var shouldPersist = false;

                    switch (operationType)
                    {
                    case OperationType.DatabaseExport:
                    case OperationType.DatabaseImport:
                        shouldPersist = true;
                        break;
                    }

                    operationState.Result = new OperationExceptionResult(innerException, status, shouldPersist);
                    operationState.Status = OperationStatus.Faulted;
                }
                else
                {
                    operationState.Result = taskResult.Result;
                    operationState.Status = OperationStatus.Completed;
                }

                if (_active.TryGetValue(id, out Operation completed))
                {
                    completed.SetCompleted();
                    // add to completed items before removing from active ones to ensure an operation status is accessible all the time
                    _completed.TryAdd(id, completed);
                    _active.TryRemove(id, out completed);
                }

                RaiseNotifications(notification, operation);
            });

            _active.TryAdd(id, operation);

            if (token == null)
            {
                return(operation.Task);
            }

            return(operation.Task.ContinueWith(t =>
            {
                token.Dispose();
                return t;
            }).Unwrap());
        }
 public void ResetOperation()
 {
     state = OperationState.None;
 }
Пример #43
0
 /// <summary>
 /// This method is called to notify that the content stream for a batch operation has been requested.
 /// </summary>
 /// <returns>
 /// A task representing any action that is running as part of the status change of the reader;
 /// null if no such action exists.
 /// </returns>
 Task IODataStreamListener.StreamRequestedAsync()
 {
     this.operationState = OperationState.StreamRequested;
     return(TaskUtils.CompletedTask);
 }
Пример #44
0
 /// <summary>
 /// Undo the given operation based on the state coming out of the perform
 /// </summary>
 /// <param name="context"><see cref="OperationContext"/> in which the operation was performed in</param>
 /// <param name="state">State as a result from when it was performed</param>
 public virtual void Undo(OperationContext context, OperationState state)
 {
 }
Пример #45
0
 private void rbReforce_CheckedChanged(object sender, EventArgs e)
 {
     if (rbReforce.Checked)
     {
         operationState = OperationState.ReforceMode;
     }
 }
 public OperationResult(OperationState state, string message = String.Empty)
 {
     State   = state;
     Message = message;
 }
Пример #47
0
 public void Pause()
 {
     state = OperationState.Paused;
 }
Пример #48
0
 public ExcelImportItemArgs(int row, int col, object cellValue, DataColumn dataColumn, ICell hssfCell, OperationState OperationState, Dictionary <string, object> dataRowValue)
 {
     _rowIndex      = row;
     _colIndex      = col;
     _value         = cellValue;
     CellStatus     = CellStatus.Continue;
     DataColumn     = dataColumn;
     ICell          = hssfCell;
     OperationState = OperationState;
     DataRowValue   = dataRowValue;
 }
Пример #49
0
		internal void RaiseOperationCompleteEvent(EventArgs baseEventArgs, OperationState state)
		{
			OperationStateEventArgs operationStateEventArg = new OperationStateEventArgs();
			operationStateEventArg.OperationState = state;
			this.OperationComplete.SafeInvoke<OperationStateEventArgs>(this, operationStateEventArg);
		}
Пример #50
0
 /// <summary>
 /// This method is called to notify that the content stream of a batch operation has been disposed.
 /// </summary>
 void IODataStreamListener.StreamDisposed()
 {
     this.operationState = OperationState.StreamDisposed;
 }
 public VBGet(VBucketNodeLocator locator, string key)
     : base(key)
 {
     this.locator = locator;
     this.state = OperationState.Unspecified;
 }
Пример #52
0
 public static bool IsFailedOrCancelled(this OperationState operationState) =>
 operationState is OperationState.Failed || operationState is OperationState.Cancelled;
Пример #53
0
 protected void SetCompleted()
 {
     State = OperationState.Succeeded;
     NotifyCompletion();
 }
Пример #54
0
        protected async Task can_backup_and_restore_internal()
        {
            var defaultS3Settings = GetS3Settings();

            using (var store = GetDocumentStore())
            {
                using (var session = store.OpenAsyncSession())
                {
                    await session.StoreAsync(new User { Name = "oren" }, "users/1");

                    session.CountersFor("users/1").Increment("likes", 100);
                    await session.SaveChangesAsync();
                }

                var config = new PeriodicBackupConfiguration
                {
                    BackupType = BackupType.Backup,
                    S3Settings = defaultS3Settings,
                    IncrementalBackupFrequency = "0 0 1 1 *"
                };

                var backupTaskId = (await store.Maintenance.SendAsync(new UpdatePeriodicBackupOperation(config))).TaskId;
                await store.Maintenance.SendAsync(new StartBackupOperation(true, backupTaskId));

                var operation = new GetPeriodicBackupStatusOperation(backupTaskId);
                PeriodicBackupStatus status = null;
                var value = WaitForValue(() =>
                {
                    status = store.Maintenance.Send(operation).Status;
                    return(status?.LastEtag);
                }, expectedVal: 4, timeout: 30_000);
                Assert.True(4 == value, $"gotStatus? {status != null}, Status Error: {status?.Error?.Exception}," +
                            $" S3 Error: {status?.UploadToS3?.Exception}, LocalBackup Exception: {status?.LocalBackup?.Exception}");
                Assert.True(status.LastOperationId != null, $"status.LastOperationId != null, Got status: {status != null}, exception: {status?.Error?.Exception}");

                OperationState backupOperation = null;
                var            operationStatus = WaitForValue(() =>
                {
                    backupOperation = store.Maintenance.Send(new GetOperationStateOperation(status.LastOperationId.Value));
                    return(backupOperation.Status);
                }, OperationStatus.Completed);
                Assert.Equal(OperationStatus.Completed, operationStatus);

                var backupResult = backupOperation.Result as BackupResult;
                Assert.NotNull(backupResult);
                Assert.True(backupResult.Counters.Processed, "backupResult.Counters.Processed");
                Assert.Equal(1, backupResult.Counters.ReadCount);

                using (var session = store.OpenAsyncSession())
                {
                    await session.StoreAsync(new User { Name = "ayende" }, "users/2");

                    session.CountersFor("users/2").Increment("downloads", 200);

                    await session.SaveChangesAsync();
                }

                var lastEtag = store.Maintenance.Send(new GetStatisticsOperation()).LastDocEtag;
                await store.Maintenance.SendAsync(new StartBackupOperation(false, backupTaskId));

                value = WaitForValue(() =>
                {
                    status = store.Maintenance.Send(operation).Status;
                    return(status?.LastEtag);
                }, expectedVal: lastEtag, timeout: 30_000);
                Assert.True(lastEtag == value, $"gotStatus? {status != null}, Status Error: {status?.Error?.Exception}," +
                            $" S3 Error: {status?.UploadToS3?.Exception}, LocalBackup Exception: {status?.LocalBackup?.Exception}");

                // restore the database with a different name
                var databaseName = $"restored_database-{Guid.NewGuid()}";

                var subfolderS3Settings = GetS3Settings(status.FolderName);

                using (RestoreDatabaseFromCloud(
                           store,
                           new RestoreFromS3Configuration {
                    DatabaseName = databaseName, Settings = subfolderS3Settings, DisableOngoingTasks = true
                },
                           TimeSpan.FromSeconds(60)))
                {
                    using (var session = store.OpenAsyncSession(databaseName))
                    {
                        var users = await session.LoadAsync <User>(new[] { "users/1", "users/2" });

                        Assert.True(users.Any(x => x.Value.Name == "oren"));
                        Assert.True(users.Any(x => x.Value.Name == "ayende"));

                        var val = await session.CountersFor("users/1").GetAsync("likes");

                        Assert.Equal(100, val);
                        val = await session.CountersFor("users/2").GetAsync("downloads");

                        Assert.Equal(200, val);
                    }

                    var originalDatabase = await Server.ServerStore.DatabasesLandlord.TryGetOrCreateResourceStore(store.Database);

                    var restoredDatabase = await Server.ServerStore.DatabasesLandlord.TryGetOrCreateResourceStore(databaseName);

                    using (restoredDatabase.DocumentsStorage.ContextPool.AllocateOperationContext(out DocumentsOperationContext ctx))
                        using (ctx.OpenReadTransaction())
                        {
                            var databaseChangeVector = DocumentsStorage.GetDatabaseChangeVector(ctx);
                            Assert.Contains($"A:7-{originalDatabase.DbBase64Id}", databaseChangeVector);
                            Assert.Contains($"A:8-{restoredDatabase.DbBase64Id}", databaseChangeVector);
                        }
                }
            }
        }
Пример #55
0
 public override bool IsDependencyResolved(ProcessState ps, OperationState os, OperationState changedOpState, bool satisfyDependency)
 {
     return ( os.Task.CurrentState == taskState ) != IsNonDependency;
 }
Пример #56
0
 private void rdoPickingDraging_CheckedChanged(object sender, EventArgs e)
 {
     this.operationState = OperationState.PickingDraging;
 }
Пример #57
0
        public override bool IsDependencyResolved(
			ProcessState ps, 
			OperationState os, 
			OperationState changedOpState,
			bool satisfyDependency)
        {
            bool resolved = false;
            bool anyAreFalse = false;
            foreach( OperationDependency opdep in Dependencies )
            {
                bool result = opdep.IsDependencyResolved(ps, os, changedOpState, satisfyDependency);
                if( result && joinOperator == JoinOperator.Or )
                {
                    resolved = true;
                    break;
                }
                else if( ! result && joinOperator == JoinOperator.And )
                {
                    anyAreFalse = true;
                    break;
                }
            }

            if( ( joinOperator == JoinOperator.And && ! anyAreFalse ) ||
                ( joinOperator == JoinOperator.Or && resolved ) )
            {
                return ! IsNonDependency;
            }

            return IsNonDependency;
        }
Пример #58
0
 private void rdoRotating_CheckedChanged(object sender, EventArgs e)
 {
     this.operationState = OperationState.Rotating;
 }
Пример #59
0
 /// <summary>
 /// Raise operation completion event
 /// </summary>
 internal void RaiseOperationCompleteEvent(EventArgs baseEventArgs, OperationState state)
 {
     OperationStateEventArgs operationStateEventArgs = new OperationStateEventArgs();
     operationStateEventArgs.OperationState = state;
     OperationComplete.SafeInvoke(this, operationStateEventArgs);
 } // RaiseOperationCompleteEvent
Пример #60
0
        private void can_backup_and_restore_internal(bool oneTimeBackup)
        {
            using (var holder = new Azure.AzureClientHolder(AzureFactAttribute.AzureSettings))
            {
                using (var store = GetDocumentStore())
                {
                    using (var session = store.OpenSession())
                    {
                        session.Store(new User {
                            Name = "oren"
                        }, "users/1");
                        session.CountersFor("users/1").Increment("likes", 100);
                        session.SaveChanges();
                    }

                    PeriodicBackupStatus status = null;
                    long backupTaskId           = 0;
                    GetPeriodicBackupStatusOperation operation = null;
                    BackupResult backupResult = null;
                    if (oneTimeBackup == false)
                    {
                        var config = new PeriodicBackupConfiguration {
                            BackupType = BackupType.Backup, AzureSettings = holder.Settings, IncrementalBackupFrequency = "0 0 1 1 *"
                        };
                        backupTaskId = (store.Maintenance.Send(new UpdatePeriodicBackupOperation(config))).TaskId;
                        operation    = new GetPeriodicBackupStatusOperation(backupTaskId);
                        store.Maintenance.Send(new StartBackupOperation(true, backupTaskId));

                        var value = WaitForValue(() =>
                        {
                            status = store.Maintenance.Send(operation).Status;
                            return(status?.LastEtag);
                        }, 4);
                        Assert.True(4 == value, $"4 == value, Got status: {status != null}, exception: {status?.Error?.Exception}");
                        Assert.True(status.LastOperationId != null, $"status.LastOperationId != null, Got status: {status != null}, exception: {status?.Error?.Exception}");

                        OperationState backupOperation = null;
                        var            operationStatus = WaitForValue(() =>
                        {
                            backupOperation = store.Maintenance.Send(new GetOperationStateOperation(status.LastOperationId.Value));
                            return(backupOperation.Status);
                        }, OperationStatus.Completed);
                        Assert.Equal(OperationStatus.Completed, operationStatus);

                        backupResult = backupOperation.Result as BackupResult;
                        Assert.NotNull(backupResult);
                        Assert.True(backupResult.Counters.Processed, "backupResult.Counters.Processed");
                        Assert.Equal(1, backupResult.Counters.ReadCount);
                    }

                    using (var session = store.OpenSession())
                    {
                        session.Store(new User {
                            Name = "ayende"
                        }, "users/2");
                        session.CountersFor("users/2").Increment("downloads", 200);

                        session.SaveChanges();
                    }

                    if (oneTimeBackup == false)
                    {
                        var lastEtag = store.Maintenance.Send(new GetStatisticsOperation()).LastDocEtag;
                        store.Maintenance.Send(new StartBackupOperation(false, backupTaskId));
                        var value2 = WaitForValue(() => store.Maintenance.Send(operation).Status.LastEtag, lastEtag);
                        Assert.Equal(lastEtag, value2);
                    }

                    if (oneTimeBackup)
                    {
                        var backupConfiguration = new BackupConfiguration
                        {
                            BackupType    = BackupType.Backup,
                            AzureSettings = holder.Settings,
                        };

                        backupResult = (BackupResult)store.Maintenance.Send(new BackupOperation(backupConfiguration)).WaitForCompletion(TimeSpan.FromSeconds(15));
                        Assert.True(backupResult != null && backupResult.Counters.Processed, "backupResult != null && backupResult.Counters.Processed");
                        Assert.Equal(2, backupResult.Counters.ReadCount);
                    }

                    // restore the database with a different name
                    var databaseName = $"restored_database-{Guid.NewGuid()}";

                    holder.Settings.RemoteFolderName = oneTimeBackup ? $"{holder.Settings.RemoteFolderName}/{backupResult.LocalBackup.BackupDirectory}" : $"{holder.Settings.RemoteFolderName}/{status.FolderName}";
                    var restoreFromGoogleCloudConfiguration = new RestoreFromAzureConfiguration()
                    {
                        DatabaseName        = databaseName,
                        Settings            = holder.Settings,
                        DisableOngoingTasks = true
                    };
                    var googleCloudOperation = new RestoreBackupOperation(restoreFromGoogleCloudConfiguration);
                    var restoreOperation     = store.Maintenance.Server.Send(googleCloudOperation);

                    restoreOperation.WaitForCompletion(TimeSpan.FromSeconds(30));
                    using (var store2 = GetDocumentStore(new Options()
                    {
                        CreateDatabase = false, ModifyDatabaseName = s => databaseName
                    }))
                    {
                        using (var session = store2.OpenSession(databaseName))
                        {
                            var users = session.Load <User>(new[] { "users/1", "users/2" });
                            Assert.True(users.Any(x => x.Value.Name == "oren"));
                            Assert.True(users.Any(x => x.Value.Name == "ayende"));

                            var val = session.CountersFor("users/1").Get("likes");
                            Assert.Equal(100, val);
                            val = session.CountersFor("users/2").Get("downloads");
                            Assert.Equal(200, val);
                        }

                        var originalDatabase = Server.ServerStore.DatabasesLandlord.TryGetOrCreateResourceStore(store.Database).Result;
                        var restoredDatabase = Server.ServerStore.DatabasesLandlord.TryGetOrCreateResourceStore(databaseName).Result;
                        using (restoredDatabase.DocumentsStorage.ContextPool.AllocateOperationContext(out DocumentsOperationContext ctx))
                            using (ctx.OpenReadTransaction())
                            {
                                var databaseChangeVector = DocumentsStorage.GetDatabaseChangeVector(ctx);
                                Assert.Contains($"A:7-{originalDatabase.DbBase64Id}", databaseChangeVector);
                                Assert.Contains($"A:8-{restoredDatabase.DbBase64Id}", databaseChangeVector);
                            }
                    }
                }
            }
        }