| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | using System; |
| | 5 | | using Azure.Core; |
| | 6 | | using Azure.Core.Pipeline; |
| | 7 | |
|
| | 8 | | namespace 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 | |
|
| 12 | 18 | | public GeoRedundantReadPolicy(Uri secondaryStorageUri) |
| | 19 | | { |
| 12 | 20 | | if (secondaryStorageUri == null) |
| | 21 | | { |
| 0 | 22 | | throw Errors.ArgumentNull(nameof(secondaryStorageUri)); |
| | 23 | | } |
| 12 | 24 | | _secondaryStorageHost = secondaryStorageUri.Host; |
| 12 | 25 | | } |
| | 26 | |
|
| | 27 | | public override void OnSendingRequest(HttpMessage message) |
| | 28 | | { |
| 12 | 29 | | if (message.Request.Method != RequestMethod.Get && message.Request.Method != RequestMethod.Head) |
| | 30 | | { |
| 2 | 31 | | 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. |
| 10 | 36 | | string alternateHost = |
| 10 | 37 | | message.TryGetProperty( |
| 10 | 38 | | Constants.GeoRedundantRead.AlternateHostKey, |
| 10 | 39 | | out var alternateHostObj) |
| 10 | 40 | | ? alternateHostObj as string |
| 10 | 41 | | : null; |
| 10 | 42 | | if (alternateHost == null) |
| | 43 | | { |
| | 44 | | // queue up the secondary host for subsequent retries |
| 2 | 45 | | message.SetProperty(Constants.GeoRedundantRead.AlternateHostKey, _secondaryStorageHost); |
| 2 | 46 | | 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. |
| 8 | 53 | | var resourceNotReplicated = |
| 8 | 54 | | message.TryGetProperty(Constants.GeoRedundantRead.ResourceNotReplicated, out var value) |
| 8 | 55 | | && (bool)value; |
| 8 | 56 | | if (resourceNotReplicated) |
| | 57 | | { |
| 2 | 58 | | 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. |
| 6 | 63 | | 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 |
| 6 | 66 | | if (message.HasResponse |
| 6 | 67 | | && message.Response.Status == Constants.HttpStatusCode.NotFound |
| 6 | 68 | | && lastTriedHost == _secondaryStorageHost) |
| | 69 | | { |
| 2 | 70 | | 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. |
| 6 | 75 | | message.Request.Uri.Host = alternateHost; |
| 6 | 76 | | message.SetProperty(Constants.GeoRedundantRead.AlternateHostKey, lastTriedHost); |
| 6 | 77 | | } |
| | 78 | | } |
| | 79 | | } |