public override async Task <CosmosResponseMessage> SendAsync( CosmosRequestMessage request, CancellationToken cancellationToken) { try { using (new ActivityScope(Guid.NewGuid())) { DocumentServiceResponse response = await this.ProcessMessageAsync(request, cancellationToken); return(response.ToCosmosResponseMessage(request)); } } //catch DocumentClientException and exceptions that inherit it. Other exception types happen before a backend request catch (DocumentClientException ex) { return(ex.ToCosmosResponseMessage(request)); } catch (AggregateException ex) { // TODO: because the SDK underneath this path uses ContinueWith or task.Result we need to catch AggregateExceptions here // in order to ensure that underlying DocumentClientExceptions get propagated up correctly. Once all ContinueWith and .Result // is removed this catch can be safely removed. AggregateException innerExceptions = ex.Flatten(); Exception docClientException = innerExceptions.InnerExceptions.FirstOrDefault(innerEx => innerEx is DocumentClientException); if (docClientException != null) { return(((DocumentClientException)docClientException).ToCosmosResponseMessage(request)); } throw; } }
internal async Task <ResponseMessage> ProcessMessageAsync( RequestMessage request, CancellationToken cancellationToken) { if (request == null) { throw new ArgumentNullException(nameof(request)); } DocumentServiceRequest serviceRequest = request.ToDocumentServiceRequest(); ClientSideRequestStatisticsTraceDatum clientSideRequestStatisticsTraceDatum = new ClientSideRequestStatisticsTraceDatum(DateTime.UtcNow); serviceRequest.RequestContext.ClientRequestStatistics = clientSideRequestStatisticsTraceDatum; //TODO: extrace auth into a separate handler string authorization = await((ICosmosAuthorizationTokenProvider)this.client.DocumentClient).GetUserAuthorizationTokenAsync( serviceRequest.ResourceAddress, PathsHelper.GetResourcePath(request.ResourceType), request.Method.ToString(), serviceRequest.Headers, AuthorizationTokenType.PrimaryMasterKey, request.Trace); serviceRequest.Headers[HttpConstants.HttpHeaders.Authorization] = authorization; IStoreModel storeProxy = this.client.DocumentClient.GetStoreProxy(serviceRequest); using (ITrace processMessageAsyncTrace = request.Trace.StartChild( name: $"{storeProxy.GetType().FullName} Transport Request", component: TraceComponent.Transport, level: Tracing.TraceLevel.Info)) { request.Trace = processMessageAsyncTrace; processMessageAsyncTrace.AddDatum("Client Side Request Stats", clientSideRequestStatisticsTraceDatum); DocumentServiceResponse response = null; try { response = request.OperationType == OperationType.Upsert ? await this.ProcessUpsertAsync(storeProxy, serviceRequest, cancellationToken) : await storeProxy.ProcessMessageAsync(serviceRequest, cancellationToken); } finally { processMessageAsyncTrace.UpdateRegionContacted(clientSideRequestStatisticsTraceDatum); } return(response.ToCosmosResponseMessage( request, serviceRequest.RequestContext.RequestChargeTracker)); } }
internal async Task <ResponseMessage> ProcessMessageAsync( RequestMessage request, CancellationToken cancellationToken) { if (request == null) { throw new ArgumentNullException(nameof(request)); } DocumentServiceRequest serviceRequest = request.ToDocumentServiceRequest(); //TODO: extrace auth into a separate handler string authorization = await((ICosmosAuthorizationTokenProvider)this.client.DocumentClient).GetUserAuthorizationTokenAsync( serviceRequest.ResourceAddress, PathsHelper.GetResourcePath(request.ResourceType), request.Method.ToString(), serviceRequest.Headers, AuthorizationTokenType.PrimaryMasterKey, request.DiagnosticsContext); serviceRequest.Headers[HttpConstants.HttpHeaders.Authorization] = authorization; IStoreModel storeProxy = this.client.DocumentClient.GetStoreProxy(serviceRequest); using (ITrace processMessageAsyncTrace = request.Trace.StartChild( name: $"{storeProxy.GetType().FullName} Transport Request", TraceComponent.Transport, Tracing.TraceLevel.Info)) { using (request.DiagnosticsContext.CreateScope(storeProxy.GetType().FullName)) { DocumentServiceResponse response = request.OperationType == OperationType.Upsert ? await this.ProcessUpsertAsync(storeProxy, serviceRequest, cancellationToken) : await storeProxy.ProcessMessageAsync(serviceRequest, cancellationToken); return(response.ToCosmosResponseMessage( request, serviceRequest.RequestContext.RequestChargeTracker, processMessageAsyncTrace)); } } }
public override async Task <CosmosResponseMessage> SendAsync( CosmosRequestMessage request, CancellationToken cancellationToken) { this.ProcessMessagesAsyncThrew = false; this.SendAsyncCalls++; try { using (new ActivityScope(Guid.NewGuid())) { DocumentServiceResponse response = await base.ProcessMessageAsync(request, cancellationToken); return(response.ToCosmosResponseMessage(request)); } } catch (DocumentClientException) { this.ProcessMessagesAsyncThrew = true; throw; } }
public override async Task <ResponseMessage> SendAsync( RequestMessage request, CancellationToken cancellationToken) { using (new ActivityScope(Guid.NewGuid())) { try { DocumentServiceResponse response = await this.ProcessMessageAsync(request, cancellationToken); Debug.Assert(Trace.CorrelationManager.ActivityId != Guid.Empty, "Trace activity id is missing"); return(response.ToCosmosResponseMessage(request)); } //catch DocumentClientException and exceptions that inherit it. Other exception types happen before a backend request catch (DocumentClientException ex) { Debug.Assert(Trace.CorrelationManager.ActivityId != Guid.Empty, "Trace activity id is missing"); return(ex.ToCosmosResponseMessage(request)); } catch (CosmosException ce) { Debug.Assert(Trace.CorrelationManager.ActivityId != Guid.Empty, "Trace activity id is missing"); return(ce.ToCosmosResponseMessage(request)); } catch (AggregateException ex) { Debug.Assert(Trace.CorrelationManager.ActivityId != Guid.Empty, "Trace activity id is missing"); // TODO: because the SDK underneath this path uses ContinueWith or task.Result we need to catch AggregateExceptions here // in order to ensure that underlying DocumentClientExceptions get propagated up correctly. Once all ContinueWith and .Result // is removed this catch can be safely removed. ResponseMessage errorMessage = AggregateExceptionConverter(ex, request); if (errorMessage != null) { return(errorMessage); } throw; } } }
internal async Task <ResponseMessage> ProcessMessageAsync( RequestMessage request, CancellationToken cancellationToken) { if (request == null) { throw new ArgumentNullException(nameof(request)); } DocumentServiceRequest serviceRequest = request.ToDocumentServiceRequest(); ClientSideRequestStatisticsTraceDatum clientSideRequestStatisticsTraceDatum = new ClientSideRequestStatisticsTraceDatum(DateTime.UtcNow, request.Trace.Summary); serviceRequest.RequestContext.ClientRequestStatistics = clientSideRequestStatisticsTraceDatum; //TODO: extrace auth into a separate handler string authorization = await((ICosmosAuthorizationTokenProvider)this.client.DocumentClient).GetUserAuthorizationTokenAsync( serviceRequest.ResourceAddress, PathsHelper.GetResourcePath(request.ResourceType), request.Method.ToString(), serviceRequest.Headers, AuthorizationTokenType.PrimaryMasterKey, request.Trace); serviceRequest.Headers[HttpConstants.HttpHeaders.Authorization] = authorization; IStoreModel storeProxy = this.client.DocumentClient.GetStoreProxy(serviceRequest); using (ITrace processMessageAsyncTrace = request.Trace.StartChild( name: $"{storeProxy.GetType().FullName} Transport Request", component: TraceComponent.Transport, level: Tracing.TraceLevel.Info)) { request.Trace = processMessageAsyncTrace; processMessageAsyncTrace.AddDatum("Client Side Request Stats", clientSideRequestStatisticsTraceDatum); DocumentServiceResponse response = null; try { response = await storeProxy.ProcessMessageAsync(serviceRequest, cancellationToken); } catch (DocumentClientException dce) { // Enrich diagnostics context in-case of auth failures if (dce.StatusCode == System.Net.HttpStatusCode.Unauthorized || dce.StatusCode == System.Net.HttpStatusCode.Forbidden) { TimeSpan authProvideLifeSpan = this.client.DocumentClient.cosmosAuthorization.GetAge(); processMessageAsyncTrace.AddDatum("AuthProvider LifeSpan InSec", authProvideLifeSpan.TotalSeconds); } throw; } finally { processMessageAsyncTrace.UpdateRegionContacted(clientSideRequestStatisticsTraceDatum); } ResponseMessage responseMessage = response.ToCosmosResponseMessage( request, serviceRequest.RequestContext.RequestChargeTracker); // Enrich diagnostics context in-case of auth failures if (responseMessage?.StatusCode == System.Net.HttpStatusCode.Unauthorized || responseMessage?.StatusCode == System.Net.HttpStatusCode.Forbidden) { TimeSpan authProvideLifeSpan = this.client.DocumentClient.cosmosAuthorization.GetAge(); processMessageAsyncTrace.AddDatum("AuthProvider LifeSpan InSec", authProvideLifeSpan.TotalSeconds); } return(responseMessage); } }