// Cancellation is handeld by TableServiceContext public static ICancellableAsyncResult BeginExecuteAsync <T, INTERMEDIATE_TYPE>(TableCommand <T, INTERMEDIATE_TYPE> cmd, IRetryPolicy policy, OperationContext operationContext, AsyncCallback callback, object asyncState) { // Note all code below will reference state, not params directly, this will allow common code with async executor ExecutionState <T> executionState = new ExecutionState <T>(cmd, policy, operationContext, callback, asyncState); TableExecutor.AcquireContext(cmd.Context, executionState); InitRequest <T, INTERMEDIATE_TYPE>(executionState); return(executionState); }
public static T ExecuteSync <T, INTERMEDIATE_TYPE>(TableCommand <T, INTERMEDIATE_TYPE> cmd, IRetryPolicy policy, OperationContext operationContext) { // Note all code below will reference state, not params directly, this will allow common code with async executor ExecutionState <T> executionState = new ExecutionState <T>(cmd, policy, operationContext); TableExecutor.AcquireContext(cmd.Context, executionState); bool shouldRetry = false; TimeSpan delay = TimeSpan.Zero; try { // Enter Retryable Section of execution do { executionState.Init(); // 0. Begin Request TableExecutor.StartRequestAttempt(executionState); TableExecutor.CheckTimeout <T>(executionState, true); try { INTERMEDIATE_TYPE tempResult = default(INTERMEDIATE_TYPE); try { Logger.LogInformational(executionState.OperationContext, SR.TraceStartRequestSync, cmd.Context.BaseUri); tempResult = cmd.ExecuteFunc(); executionState.Result = cmd.ParseResponse(tempResult, executionState.Cmd.CurrentResult, cmd); DataServiceResponse dataServiceResponse = tempResult as DataServiceResponse; QueryOperationResponse queryResponse = tempResult as QueryOperationResponse; if (dataServiceResponse != null) { if (dataServiceResponse.IsBatchResponse) { // Attempt to populate response headers if (executionState.Req != null) { SetExecutionStateCommandResult(executionState, dataServiceResponse); Logger.LogInformational(executionState.OperationContext, SR.TraceResponse, executionState.Cmd.CurrentResult.HttpStatusCode, executionState.Cmd.CurrentResult.ServiceRequestID, executionState.Cmd.CurrentResult.ContentMd5, executionState.Cmd.CurrentResult.Etag); } } else { int index = 0; foreach (OperationResponse operationResponse in dataServiceResponse) { // Attempt to populate response headers if (executionState.Req != null) { SetStorageCmdRequestResults(executionState.Cmd.RequestResults.ElementAt(index), operationResponse); Logger.LogInformational(executionState.OperationContext, SR.TraceResponse, executionState.Cmd.RequestResults.ElementAt(index).HttpStatusCode, executionState.Cmd.RequestResults.ElementAt(index).ServiceRequestID, executionState.Cmd.RequestResults.ElementAt(index).ContentMd5, executionState.Cmd.RequestResults.ElementAt(index).Etag); index++; } } } } else if (queryResponse != null) { // Attempt to populate response headers if (executionState.Req != null) { SetStorageCmdRequestResults(executionState.Cmd.CurrentResult, queryResponse); Logger.LogInformational(executionState.OperationContext, SR.TraceResponse, executionState.Cmd.CurrentResult.HttpStatusCode, executionState.Cmd.CurrentResult.ServiceRequestID, executionState.Cmd.CurrentResult.ContentMd5, executionState.Cmd.CurrentResult.Etag); } } } catch (Exception ex) { Logger.LogWarning(executionState.OperationContext, SR.TraceGenericError, ex.Message); // Store exception and invoke callback here. All operations in this try would be non-retryable by default if (executionState.ExceptionRef == null || !(executionState.ExceptionRef is StorageException)) { executionState.ExceptionRef = ExecutorBase.TranslateDataServiceExceptionBasedOnParseError(ex, executionState.Cmd.CurrentResult, executionState.Cmd.ParseDataServiceError); } executionState.Result = cmd.ParseResponse(tempResult, executionState.Cmd.CurrentResult, cmd); // clear exception executionState.ExceptionRef = null; } TableExecutor.FinishRequestAttempt(executionState); Logger.LogInformational(executionState.OperationContext, SR.TraceSuccess); return(executionState.Result); } catch (Exception e) { TableExecutor.FinishRequestAttempt(executionState); StorageException translatedException = ExecutorBase.TranslateDataServiceExceptionBasedOnParseError(e, executionState.Cmd.CurrentResult, executionState.Cmd.ParseDataServiceError); executionState.ExceptionRef = translatedException; Logger.LogInformational(executionState.OperationContext, SR.TraceRetryCheck, executionState.RetryCount, executionState.Cmd.CurrentResult.HttpStatusCode, translatedException.IsRetryable ? "yes" : "no", translatedException.Message); shouldRetry = false; if (translatedException.IsRetryable && (executionState.RetryPolicy != null)) { shouldRetry = executionState.RetryPolicy.ShouldRetry( executionState.RetryCount++, executionState.Cmd.CurrentResult.HttpStatusCode, executionState.ExceptionRef, out delay, executionState.OperationContext); if ((delay < TimeSpan.Zero) || (delay > Constants.MaximumRetryBackoff)) { delay = Constants.MaximumRetryBackoff; } } } if (!shouldRetry || (executionState.OperationExpiryTime.HasValue && (DateTime.Now + delay).CompareTo(executionState.OperationExpiryTime.Value) > 0)) { Logger.LogError(executionState.OperationContext, shouldRetry ? SR.TraceRetryDecisionTimeout : SR.TraceRetryDecisionPolicy, executionState.ExceptionRef.Message); throw executionState.ExceptionRef; } else { if (executionState.Cmd.RecoveryAction != null) { // I.E. Rewind stream etc. executionState.Cmd.RecoveryAction(executionState.Cmd, executionState.ExceptionRef, executionState.OperationContext); } Logger.LogInformational(executionState.OperationContext, SR.TraceRetryDelay, (int)delay.TotalMilliseconds); if (delay > TimeSpan.Zero) { Thread.Sleep(delay); } Logger.LogInformational(executionState.OperationContext, SR.TraceRetry); } }while (shouldRetry); // should never get here, either return, or throw; throw new NotImplementedException(SR.InternalStorageError); } finally { ReleaseContext(cmd.Context); } }