public Task OnReportFinishTaskSuccess(string taskId, long instanceId) { PendingCommandContext startContext = this.pendingStartCommands.Remove(taskId); if (startContext != null) { startContext.Cancel(); } PendingCommandContext finishContext = this.pendingFinishCommands.Get(taskId); if (finishContext != null) { if (finishContext.ShouldInjectFault) { return(Task.Factory.StartNew(() => { throw new InvalidOperationException("OnReportFinishTaskSuccess: Injected Fault"); })); } if (finishContext.InstanceId == instanceId) { finishContext.Complete(); this.pendingFinishCommands.Remove(taskId); } else { // Not the instance we were expecting, request again with // the current instance this.ProcessFinishCommand(finishContext); } } return(Utility.CreateCompletedTask <object>(null)); }
public Task OnReportStartTaskSuccess(string taskId, long instanceId) { PendingCommandContext commandContext = this.pendingStartCommands.Get(taskId); if (commandContext != null) { if (commandContext.ShouldInjectFault) { return(Task.Factory.StartNew(() => { throw new InvalidOperationException("OnReportStartTaskSuccess: Injected Fault"); })); } if (commandContext.InstanceId == instanceId) { commandContext.Complete(); // Leave start context in map for correlation with finish context } else { // Not the instance we were expecting, request again with // the current instance this.ProcessStartCommand(commandContext); } } return(Utility.CreateCompletedTask <object>(null)); }
private void ProcessCommand(PendingCommandContext commandContext) { if (commandContext.StartCommand != null) { if (!this.pendingStartCommands.TryAdd(commandContext)) { string message = string.Format( CultureInfo.InvariantCulture, "Stale start command {0}:{1}", commandContext.TaskId, commandContext.InstanceId); throw Trace.CreateException( TraceType, NativeTypes.FABRIC_ERROR_CODE.E_ABORT, message); } this.ProcessStartCommand(commandContext); } else if (commandContext.FinishCommand != null) { PendingCommandContext startContext = this.pendingStartCommands.Remove(commandContext.MapKey); if (startContext != null) { commandContext.FinishCommand.TryUpdateInstanceId(startContext.InstanceId); startContext.Cancel(); } else { commandContext.FinishCommand.TryUpdateInstanceId(DateTime.UtcNow.Ticks); } if (!this.pendingFinishCommands.TryAdd(commandContext)) { string message = string.Format( CultureInfo.InvariantCulture, "Stale finish command {0}:{1}", commandContext.TaskId, commandContext.InstanceId); throw Trace.CreateException( TraceType, NativeTypes.FABRIC_ERROR_CODE.E_ABORT, message); } this.ProcessFinishCommand(commandContext); } else { throw Trace.CreateException( TraceType, NativeTypes.FABRIC_ERROR_CODE.E_INVALIDARG, "Invalid Infrastructure Service command"); } }
public Task OnReportTaskFailure(string taskId, long instanceId) { PendingCommandContext startContext = this.pendingStartCommands.Get(taskId); PendingCommandContext finishContext = this.pendingFinishCommands.Get(taskId); if (startContext != null) { if (startContext.ShouldInjectFault) { return(Task.Factory.StartNew(() => { throw new InvalidOperationException("OnReportTaskFailure: Injected Fault"); })); } if (startContext.InstanceId == instanceId) { startContext.Fail(Trace.CreateException( TraceType, NativeTypes.FABRIC_ERROR_CODE.E_FAIL, "Task failure reported")); this.pendingStartCommands.Remove(startContext.MapKey); } else if (finishContext != null) { startContext.Cancel(); this.pendingStartCommands.Remove(startContext.MapKey); } else if (finishContext == null) { this.ProcessStartCommand(startContext); } } if (finishContext != null) { if (finishContext.ShouldInjectFault) { return(Task.Factory.StartNew(() => { throw new InvalidOperationException("OnReportTaskFailure: Injected Fault"); })); } if (finishContext.InstanceId == instanceId) { finishContext.Fail(Trace.CreateException( TraceType, NativeTypes.FABRIC_ERROR_CODE.E_FAIL, "Task failure reported")); this.pendingFinishCommands.Remove(finishContext.MapKey); } else { this.ProcessFinishCommand(finishContext); } } return(Utility.CreateCompletedTask <object>(null)); }
public Task ScheduleProcessCommand( InfrastructureServiceCommand command, TimeSpan timeout, CancellationToken cancelToken) { var commandContext = new PendingCommandContext(command, timeout, cancelToken); return(Task.Factory.StartNew( () => this.ProcessCommand(commandContext), cancelToken, TaskCreationOptions.PreferFairness, TaskScheduler.Default)); }
public PendingCommandContext Get(string key) { PendingCommandContext context = null; lock (this.contexts) { if (this.contexts.ContainsKey(key)) { context = this.contexts[key]; } } return(context); }
private void OnInfrastructureTaskRequestComplete(Task prevTask, PendingCommandContext commandContext) { var ex = prevTask.Exception; if (ex != null) { Exception translated = Utility.TryTranslateManagedExceptionToCOM(ex.InnerException); commandContext.Fail(translated == null ? ex.InnerException : translated); } else { // Do nothing: wait for the ack from CM } }
private void ProcessStartCommand(PendingCommandContext commandContext) { var task = this.infrastructureServiceAgent.StartInfrastructureTaskAsync( commandContext.StartCommand.TaskDescription, commandContext.RemainingTimeout, commandContext.CancelToken); task.ContinueWith((prev) => { this.VerifyQueryResults( commandContext, commandContext.StartCommand.TaskId, commandContext.StartCommand.InstanceId, commandContext.StartCommand.TaskDescription.NodeTasks); this.OnInfrastructureTaskRequestComplete(prev, commandContext); }); }
public bool TryAdd(PendingCommandContext context) { bool isAdded = false; PendingCommandContext oldContext = null; lock (this.contexts) { if (this.contexts.ContainsKey(context.MapKey)) { if (this.contexts[context.MapKey].InstanceId <= context.InstanceId) { oldContext = this.contexts[context.MapKey]; if (context.TryStartContext(this.ContextFaultHandler)) { this.contexts[context.MapKey] = context; isAdded = true; } } } else { if (context.TryStartContext(this.ContextFaultHandler)) { this.contexts.Add(context.MapKey, context); isAdded = true; } } } if (oldContext != null) { oldContext.Cancel(); } return(isAdded); }
private void VerifyQueryResults( PendingCommandContext commandContext, string taskId, long instanceId, ReadOnlyCollection <NodeTaskDescription> nodeTasks) { var task = this.infrastructureServiceAgent.QueryInfrastructureTaskAsync( commandContext.RemainingTimeout, commandContext.CancelToken); var queryResult = task.Result; bool isTaskFound = false; StringBuilder resultBuilder = new StringBuilder(); foreach (var it in queryResult.Items) { if (taskId == it.Description.TaskId && instanceId <= it.Description.InstanceId) { isTaskFound = true; if (nodeTasks != null && !NodeTaskDescription.AreEqual(nodeTasks, it.Description.NodeTasks)) { isTaskFound = false; } } StringBuilder nodeBuilder = new StringBuilder(); foreach (var node in it.Description.NodeTasks) { nodeBuilder.AppendFormat(CultureInfo.InvariantCulture, " {0}:{1} ", node.NodeName, node.TaskType); } resultBuilder.AppendFormat(CultureInfo.InvariantCulture, "Task = '{0}:{1}({2})[{3}]'{4}", it.Description.TaskId, it.Description.InstanceId, it.State, nodeBuilder.ToString(), Environment.NewLine); } Trace.WriteInfo( TraceType, "QueryResults = {0}", resultBuilder.ToString()); if (!isTaskFound) { throw Trace.CreateException( TraceType, NativeTypes.FABRIC_ERROR_CODE.E_FAIL, "VerifyQueryResults({0}, {1}, {2}) failed", taskId, instanceId, nodeTasks); } }