< Summary

Class:Azure.Storage.GeoRedundantReadPolicy
Assembly:Azure.Storage.Blobs
File(s):C:\Git\azure-sdk-for-net\sdk\storage\Azure.Storage.Common\src\Shared\GeoRedundantReadPolicy.cs
Covered lines:28
Uncovered lines:1
Coverable lines:29
Total lines:79
Line coverage:96.5% (28 of 29)
Covered branches:19
Total branches:20
Branch coverage:95% (19 of 20)

Metrics

MethodCyclomatic complexity Line coverage Branch coverage
.ctor(...)-80%50%
OnSendingRequest(...)-100%100%

File(s)

C:\Git\azure-sdk-for-net\sdk\storage\Azure.Storage.Common\src\Shared\GeoRedundantReadPolicy.cs

#LineLine coverage
 1// Copyright (c) Microsoft Corporation. All rights reserved.
 2// Licensed under the MIT License.
 3
 4using System;
 5using Azure.Core;
 6using Azure.Core.Pipeline;
 7
 8namespace Azure.Storage
 9{
 10    /// <summary>
 11    /// This policy is used if the SecondaryUri property is passed in on the clientOptions. It allows for storage
 12    /// accounts configured with RA-GRS to retry GET or HEAD requests against the secondary storage Uri.
 13    /// </summary>
 14    internal class GeoRedundantReadPolicy : HttpPipelineSynchronousPolicy
 15    {
 16        private readonly string _secondaryStorageHost;
 17
 2818        public GeoRedundantReadPolicy(Uri secondaryStorageUri)
 19        {
 2820            if (secondaryStorageUri == null)
 21            {
 022                throw Errors.ArgumentNull(nameof(secondaryStorageUri));
 23            }
 2824            _secondaryStorageHost = secondaryStorageUri.Host;
 2825        }
 26
 27        public override void OnSendingRequest(HttpMessage message)
 28        {
 15229            if (message.Request.Method != RequestMethod.Get && message.Request.Method != RequestMethod.Head)
 30            {
 6831                return;
 32            }
 33
 34            // Look up what the alternate host is set to in the message properties. For the initial request, this will
 35            // not be set.
 8436            string alternateHost =
 8437                message.TryGetProperty(
 8438                    Constants.GeoRedundantRead.AlternateHostKey,
 8439                    out var alternateHostObj)
 8440                ? alternateHostObj as string
 8441                : null;
 8442            if (alternateHost == null)
 43            {
 44                // queue up the secondary host for subsequent retries
 2845                message.SetProperty(Constants.GeoRedundantRead.AlternateHostKey, _secondaryStorageHost);
 2846                return;
 47            }
 48
 49            // Check the flag that indicates whether the resource has not been propagated to the secondary host yet.
 50            // If this flag is set, we don't want to retry against the secondary host again for any subsequent retries.
 51            // Also, the flag being set implies that the current request must already be set to the primary host, so we
 52            // are safe to return without checking if the current host is secondary or primary.
 5653            var resourceNotReplicated =
 5654                message.TryGetProperty(Constants.GeoRedundantRead.ResourceNotReplicated, out var value)
 5655                && (bool)value;
 5656            if (resourceNotReplicated)
 57            {
 458                return;
 59            }
 60
 61            // If alternateHost was not null that means the message is being retried. Hence what is stored in the Host
 62            // property of UriBuilder is actually the host from the last try.
 5263            var lastTriedHost = message.Request.Uri.Host;
 64
 65            // If necessary, set the flag to indicate that the resource has not yet been propagated to the secondary hos
 5266            if (message.HasResponse
 5267                && message.Response.Status == Constants.HttpStatusCode.NotFound
 5268                && lastTriedHost == _secondaryStorageHost)
 69            {
 470                message.SetProperty(Constants.GeoRedundantRead.ResourceNotReplicated, true);
 71            }
 72
 73            // Toggle the host set in the request to use the alternate host for the upcoming attempt, and update the
 74            // the property for the AlternateHostKey to be the host used in the last try.
 5275            message.Request.Uri.Host = alternateHost;
 5276            message.SetProperty(Constants.GeoRedundantRead.AlternateHostKey, lastTriedHost);
 5277        }
 78    }
 79}

Methods/Properties

.ctor(...)
OnSendingRequest(...)