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); } }
public static void InitRequest <T, INTERMEDIATE_TYPE>(ExecutionState <T> executionState) { try { executionState.Init(); // 0. Begin Request TableExecutor.StartRequestAttempt(executionState); if (TableExecutor.CheckTimeout <T>(executionState, false)) { TableExecutor.EndOperation <T, INTERMEDIATE_TYPE>(executionState); return; } lock (executionState.CancellationLockerObject) { if (TableExecutor.CheckCancellation(executionState)) { TableExecutor.EndOperation <T, INTERMEDIATE_TYPE>(executionState); return; } TableCommand <T, INTERMEDIATE_TYPE> tableCommandRef = executionState.Cmd as TableCommand <T, INTERMEDIATE_TYPE>; // Execute Call Logger.LogInformational(executionState.OperationContext, SR.TraceStartRequestAsync, tableCommandRef.Context.BaseUri); tableCommandRef.Begin( (res) => { executionState.UpdateCompletedSynchronously(res.CompletedSynchronously); INTERMEDIATE_TYPE tResult = default(INTERMEDIATE_TYPE); try { tResult = tableCommandRef.End(res); executionState.Result = tableCommandRef.ParseResponse(tResult, executionState.Cmd.CurrentResult, tableCommandRef); if (executionState.Req != null) { DataServiceResponse dataServiceResponse = tResult as DataServiceResponse; QueryOperationResponse queryResponse = tResult 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); lock (executionState.CancellationLockerObject) { if (executionState.CancelRequested) { // Ignore DSC exception if request was canceled. return; } } // 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); } try { executionState.Result = tableCommandRef.ParseResponse(tResult, executionState.Cmd.CurrentResult, tableCommandRef); // clear exception executionState.ExceptionRef = null; } catch (Exception parseEx) { Logger.LogWarning(executionState.OperationContext, SR.TraceGenericError, ex.Message); executionState.ExceptionRef = parseEx; } } finally { EndOperation <T, INTERMEDIATE_TYPE>(executionState); } }, null); if (tableCommandRef.Context != null) { executionState.CancelDelegate = tableCommandRef.Context.InternalCancel; } } } 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.OnComplete(); } }
public static T ExecuteSync <T>(RESTCommand <T> cmd, IRetryPolicy policy, OperationContext operationContext) { // Note all code below will reference state, not params directly, this will allow common code with async executor using (ExecutionState <T> executionState = new ExecutionState <T>(cmd, policy, operationContext)) { bool shouldRetry = false; TimeSpan delay = TimeSpan.Zero; do { try { executionState.Init(); // 0. Begin Request Executor.StartRequestAttempt(executionState); // Steps 1-4 Executor.ProcessStartOfRequest(executionState, SR.TraceStartRequestSync); Executor.CheckTimeout <T>(executionState, true); } catch (Exception ex) { Logger.LogError(executionState.OperationContext, SR.TraceInitRequestError, ex.Message); // Store exception and throw here. All operations in this try would be non-retryable by default. At this point, the request is not even made. // Therefore, we will not get extended error info. Hence ParseError doesn't matter here. StorageException storageEx = ExecutorBase.TranslateExceptionBasedOnParseError(ex, executionState.Cmd.CurrentResult, executionState.Resp, executionState.Cmd.ParseError); storageEx.IsRetryable = false; executionState.ExceptionRef = storageEx; throw executionState.ExceptionRef; } // Enter Retryable Section of execution try { // 5. potentially upload data if (cmd.SendStream != null) { executionState.CurrentOperation = ExecutorOperation.BeginGetRequestStream; Logger.LogInformational(executionState.OperationContext, SR.TracePrepareUpload); executionState.Req.Timeout = (int)executionState.RemainingTimeout.TotalMilliseconds; executionState.ReqStream = executionState.Req.GetRequestStream(); executionState.CurrentOperation = ExecutorOperation.BeginUploadRequest; Logger.LogInformational(executionState.OperationContext, SR.TraceUpload); MultiBufferMemoryStream multiBufferMemoryStream = cmd.SendStream as MultiBufferMemoryStream; try { if (multiBufferMemoryStream != null && !cmd.SendStreamLength.HasValue) { multiBufferMemoryStream.FastCopyTo(executionState.ReqStream, executionState.OperationExpiryTime); } else { // don't calculate md5 here as we should have already set this for auth purposes executionState.RestCMD.SendStream.WriteToSync(executionState.ReqStream, cmd.SendStreamLength, null /* maxLength */, false, true, executionState, null /* streamCopyState */); } executionState.ReqStream.Flush(); executionState.ReqStream.Dispose(); executionState.ReqStream = null; } catch (Exception) { executionState.Req.Abort(); throw; } } // 6. Get response try { executionState.CurrentOperation = ExecutorOperation.BeginGetResponse; Logger.LogInformational(executionState.OperationContext, SR.TraceGetResponse); executionState.Req.Timeout = (int)executionState.RemainingTimeout.TotalMilliseconds; executionState.Resp = (HttpWebResponse)executionState.Req.GetResponse(); executionState.CurrentOperation = ExecutorOperation.EndGetResponse; } catch (WebException ex) { Logger.LogWarning(executionState.OperationContext, SR.TraceGetResponseError, ex.Message); executionState.Resp = (HttpWebResponse)ex.Response; if (ex.Status == WebExceptionStatus.Timeout || executionState.ReqTimedOut) { throw new TimeoutException(); } if (executionState.Resp == null) { throw; } else { executionState.ExceptionRef = ExecutorBase.TranslateExceptionBasedOnParseError(ex, executionState.Cmd.CurrentResult, executionState.Resp, executionState.Cmd.ParseError); } } // Response Logger.LogInformational(executionState.OperationContext, SR.TraceResponse, executionState.Cmd.CurrentResult.HttpStatusCode, executionState.Cmd.CurrentResult.ServiceRequestID, executionState.Cmd.CurrentResult.ContentMd5, executionState.Cmd.CurrentResult.Etag); Executor.FireResponseReceived(executionState); // 7. Do Response parsing (headers etc, no stream available here) if (cmd.PreProcessResponse != null) { executionState.CurrentOperation = ExecutorOperation.PreProcess; executionState.Result = cmd.PreProcessResponse(cmd, executionState.Resp, executionState.ExceptionRef, executionState.OperationContext); // clear exception executionState.ExceptionRef = null; Logger.LogInformational(executionState.OperationContext, SR.TracePreProcessDone); } // 8. (Potentially reads stream from server) executionState.CurrentOperation = ExecutorOperation.GetResponseStream; cmd.ResponseStream = executionState.Resp.GetResponseStream(); if (!cmd.RetrieveResponseStream) { cmd.DestinationStream = Stream.Null; } if (cmd.DestinationStream != null) { if (cmd.StreamCopyState == null) { cmd.StreamCopyState = new StreamDescriptor(); } try { executionState.CurrentOperation = ExecutorOperation.BeginDownloadResponse; Logger.LogInformational(executionState.OperationContext, SR.TraceDownload); cmd.ResponseStream.WriteToSync(cmd.DestinationStream, null /* copyLength */, null /* maxLength */, cmd.CalculateMd5ForResponseStream, false, executionState, cmd.StreamCopyState); } finally { cmd.ResponseStream.Dispose(); cmd.ResponseStream = null; } } // Step 9 - This will not be called if an exception is raised during stream copying Executor.ProcessEndOfRequest(executionState); Executor.FinishRequestAttempt(executionState); return(executionState.Result); } catch (Exception e) { Logger.LogWarning(executionState.OperationContext, SR.TraceGenericError, e.Message); Executor.FinishRequestAttempt(executionState); StorageException translatedException = ExecutorBase.TranslateExceptionBasedOnParseError(e, executionState.Cmd.CurrentResult, executionState.Resp, executionState.Cmd.ParseError); 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)) { executionState.CurrentLocation = Executor.GetNextLocation(executionState.CurrentLocation, cmd.LocationMode); Logger.LogInformational(executionState.OperationContext, SR.TraceNextLocation, executionState.CurrentLocation); IExtendedRetryPolicy extendedRetryPolicy = executionState.RetryPolicy as IExtendedRetryPolicy; if (extendedRetryPolicy != null) { RetryContext retryContext = new RetryContext( executionState.RetryCount++, cmd.CurrentResult, executionState.CurrentLocation, cmd.LocationMode); RetryInfo retryInfo = extendedRetryPolicy.Evaluate(retryContext, executionState.OperationContext); if (retryInfo != null) { Logger.LogInformational(executionState.OperationContext, SR.TraceRetryInfo, retryInfo.TargetLocation, retryInfo.UpdatedLocationMode); shouldRetry = true; executionState.CurrentLocation = retryInfo.TargetLocation; cmd.LocationMode = retryInfo.UpdatedLocationMode; delay = retryInfo.RetryInterval; } } else { shouldRetry = executionState.RetryPolicy.ShouldRetry( executionState.RetryCount++, cmd.CurrentResult.HttpStatusCode, executionState.ExceptionRef, out delay, executionState.OperationContext); } if ((delay < TimeSpan.Zero) || (delay > Constants.MaximumRetryBackoff)) { delay = Constants.MaximumRetryBackoff; } } } finally { if (executionState.Resp != null) { executionState.Resp.Close(); executionState.Resp = null; } } 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); } if (delay > TimeSpan.Zero) { Logger.LogInformational(executionState.OperationContext, SR.TraceRetryDelay, (int)delay.TotalMilliseconds); Thread.Sleep(delay); } Logger.LogInformational(executionState.OperationContext, SR.TraceRetry); } Executor.FireRetrying(executionState); }while (shouldRetry); } // should never get here, either return, or throw; throw new NotImplementedException(SR.InternalStorageError); }