Exemplo n.º 1
0
        public override void OnSendingRequest(HttpPipelineMessage message)
        {
            if (message.Request.Method != RequestMethod.Get && message.Request.Method != RequestMethod.Head)
            {
                return;
            }

            // Look up what the alternate host is set to in the message properties. For the initial request, this will
            // not be set.
            string alternateHost =
                message.TryGetProperty(
                    Constants.GeoRedundantRead.AlternateHostKey,
                    out var alternateHostObj)
                ? alternateHostObj as string
                : null;

            if (alternateHost == null)
            {
                // queue up the secondary host for subsequent retries
                message.SetProperty(Constants.GeoRedundantRead.AlternateHostKey, _secondaryStorageHost);
                return;
            }

            // Check the flag that indicates whether the resource has not been propagated to the secondary host yet.
            // If this flag is set, we don't want to retry against the secondary host again for any subsequent retries.
            // Also, the flag being set implies that the current request must already be set to the primary host, so we
            // are safe to return without checking if the current host is secondary or primary.
            var resourceNotReplicated =
                message.TryGetProperty(Constants.GeoRedundantRead.ResourceNotReplicated, out var value) &&
                (bool)value;

            if (resourceNotReplicated)
            {
                return;
            }

            // If alternateHost was not null that means the message is being retried. Hence what is stored in the Host
            // property of UriBuilder is actually the host from the last try.
            var lastTriedHost = message.Request.Uri.Host;

            // If necessary, set the flag to indicate that the resource has not yet been propagated to the secondary host.
            if (message.HasResponse &&
                message.Response.Status == Constants.HttpStatusCode.NotFound &&
                lastTriedHost == _secondaryStorageHost)
            {
                message.SetProperty(Constants.GeoRedundantRead.ResourceNotReplicated, true);
            }

            // Toggle the host set in the request to use the alternate host for the upcoming attempt, and update the
            // the property for the AlternateHostKey to be the host used in the last try.
            message.Request.Uri.Host = alternateHost;
            message.SetProperty(Constants.GeoRedundantRead.AlternateHostKey, lastTriedHost);
        }
Exemplo n.º 2
0
        public void TryGetPropertyIsCaseSensitive()
        {
            HttpPipelineMessage message = new HttpPipelineMessage(new MockRequest(), new ResponseClassifier(), CancellationToken.None);

            message.SetProperty("someName", "value");

            Assert.False(message.TryGetProperty("SomeName", out object value));
        }
Exemplo n.º 3
0
        public void TryGetPropertyIsCaseSensitive()
        {
            HttpPipelineMessage message = new HttpPipelineMessage(CancellationToken.None);

            message.SetProperty("someName", "value");

            Assert.False(message.TryGetProperty("SomeName", out object value));
        }
Exemplo n.º 4
0
        public void TryGetPropertyReturnsValueIfSet()
        {
            HttpPipelineMessage message = new HttpPipelineMessage(new MockRequest(), new ResponseClassifier());

            message.SetProperty("someName", "value");

            Assert.True(message.TryGetProperty("someName", out object value));
            Assert.AreEqual("value", value);
        }
Exemplo n.º 5
0
        public void TryGetPropertyReturnsValueIfSet()
        {
            HttpPipelineMessage message = new HttpPipelineMessage(CancellationToken.None);

            message.SetProperty("someName", "value");

            Assert.True(message.TryGetProperty("someName", out object value));
            Assert.AreEqual("value", value);
        }
Exemplo n.º 6
0
        public void OnSendingRequest_FirstTry_ShouldUsePrimary_ShouldSetAlternateToSecondary()
        {
            var message = new HttpPipelineMessage(
                CreateMockRequest(MockPrimaryUri),
                new StorageResponseClassifier(MockSecondaryUri));

            var policy = new GeoRedundantReadPolicy(MockSecondaryUri);

            policy.OnSendingRequest(message);

            Assert.AreEqual(MockPrimaryUri.Host, message.Request.Uri.Host);
            Assert.IsTrue(message.TryGetProperty(Constants.GeoRedundantRead.AlternateHostKey, out object val) &&
                          (string)val == MockSecondaryUri.Host);
        }
Exemplo n.º 7
0
        public void OnSendingRequest_PutRequest_ShouldIgnore()
        {
            MockRequest request = CreateMockRequest(MockPrimaryUri);

            request.Method = RequestMethod.Put;
            var message = new HttpPipelineMessage(
                request,
                new StorageResponseClassifier(MockSecondaryUri));

            var policy = new GeoRedundantReadPolicy(MockSecondaryUri);

            policy.OnSendingRequest(message);

            Assert.AreEqual(MockPrimaryUri.Host, message.Request.Uri.Host);
            Assert.IsFalse(message.TryGetProperty(Constants.GeoRedundantRead.AlternateHostKey, out object val) &&
                           (string)val == MockSecondaryUri.Host);
        }
Exemplo n.º 8
0
        public void OnSendingRequest_404onSecondary_ShouldSetNotFoundFlag_ShouldUsePrimary()
        {
            var message = new HttpPipelineMessage(
                CreateMockRequest(MockSecondaryUri),
                new StorageResponseClassifier(MockSecondaryUri))

            {
                Response = new MockResponse(Constants.HttpStatusCode.NotFound)
            };

            message.SetProperty(Constants.GeoRedundantRead.AlternateHostKey, MockSecondaryUri.Host);
            var policy = new GeoRedundantReadPolicy(MockSecondaryUri);

            policy.OnSendingRequest(message);

            Assert.AreEqual(MockSecondaryUri.Host, message.Request.Uri.Host);
            Assert.IsTrue(message.TryGetProperty(Constants.GeoRedundantRead.ResourceNotReplicated, out object val) &&
                          (bool)val);
        }
Exemplo n.º 9
0
        public void TryGetPropertyReturnsFalseIfNotExist()
        {
            HttpPipelineMessage message = new HttpPipelineMessage(new MockRequest(), new ResponseClassifier());

            Assert.False(message.TryGetProperty("someName", out _));
        }
Exemplo n.º 10
0
        public void TryGetPropertyReturnsFalseIfNotExist()
        {
            HttpPipelineMessage message = new HttpPipelineMessage(CancellationToken.None);

            Assert.False(message.TryGetProperty("someName", out _));
        }