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)); }
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 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); }