private void HandleFailures(HttpMessage httpMessage, IPersistentBlob blob)
        {
            int retryInterval;

            if (!httpMessage.HasResponse)
            {
                // HttpRequestException
                // Extend lease time so that it is not picked again for retry.
                blob.Lease(HttpPipelineHelper.MinimumRetryInterval);
            }
            else
            {
                switch (httpMessage.Response.Status)
                {
                case ResponseStatusCodes.PartialSuccess:
                    // Parse retry-after header
                    // Send Failed Messages To Storage
                    // Delete existing file
                    TrackResponse trackResponse = HttpPipelineHelper.GetTrackResponse(httpMessage);
                    var           content       = HttpPipelineHelper.GetPartialContentForRetry(trackResponse, httpMessage.Request.Content);
                    if (content != null)
                    {
                        retryInterval = HttpPipelineHelper.GetRetryInterval(httpMessage.Response);
                        blob.Delete();
                        _storage.SaveTelemetry(content, retryInterval);
                    }
                    break;

                case ResponseStatusCodes.RequestTimeout:
                case ResponseStatusCodes.ResponseCodeTooManyRequests:
                case ResponseStatusCodes.ResponseCodeTooManyRequestsAndRefreshCache:
                    // Extend lease time using retry interval period
                    // so that it is not picked up again before that.
                    retryInterval = HttpPipelineHelper.GetRetryInterval(httpMessage.Response);
                    blob.Lease(retryInterval);
                    break;

                case ResponseStatusCodes.InternalServerError:
                case ResponseStatusCodes.BadGateway:
                case ResponseStatusCodes.ServiceUnavailable:
                case ResponseStatusCodes.GatewayTimeout:
                    // Extend lease time so that it is not picked up again
                    blob.Lease(HttpPipelineHelper.MinimumRetryInterval);
                    break;

                default:
                    // Log Non-Retriable Status and don't retry or store;
                    // File will be cleared by maintenance job
                    break;
                }
            }
        }
Exemplo n.º 2
0
        public void FileBlobTests_Lease()
        {
            var             testFile = new FileInfo(Path.Combine(Path.GetTempPath(), Path.GetRandomFileName()));
            IPersistentBlob blob     = new FileBlob(testFile.FullName);

            var             data = Encoding.UTF8.GetBytes("Hello, World!");
            var             leasePeriodMilliseconds = 1000;
            IPersistentBlob blob1      = blob.Write(data);
            IPersistentBlob leasedBlob = blob1.Lease(leasePeriodMilliseconds);

            Assert.Contains(".lock", ((FileBlob)leasedBlob).FullPath);

            blob1.Delete();
            Assert.False(testFile.Exists);
        }
Exemplo n.º 3
0
        public void FileBlobTests_E2E_Test()
        {
            var             testFile = new FileInfo(Path.Combine(Path.GetTempPath(), Path.GetRandomFileName()));
            IPersistentBlob blob     = new FileBlob(testFile.FullName);

            var             data        = Encoding.UTF8.GetBytes("Hello, World!");
            IPersistentBlob blob1       = blob.Write(data);
            var             blobContent = blob.Read();

            Assert.Equal(testFile.FullName, ((FileBlob)blob1).FullPath);
            Assert.Equal(data, blobContent);

            blob1.Delete();
            Assert.False(testFile.Exists);
        }
        public void FileStorage_E2E_Test()
        {
            var testDirectory = new DirectoryInfo(Path.Combine(Path.GetTempPath(), Path.GetRandomFileName()));

            using var storage = new FileStorage(testDirectory.FullName);

            var data = Encoding.UTF8.GetBytes("Hello, World!");

            // Create blob.
            IPersistentBlob blob1 = storage.CreateBlob(data);

            // Get blob.
            IPersistentBlob blob2 = storage.GetBlob();

            Assert.Single(storage.GetBlobs());

            // Verify file name from both create blob and get blob are same.
            Assert.Equal(((FileBlob)blob1).FullPath, ((FileBlob)blob2).FullPath);

            // Validate if content in the blob is same as buffer data passed to create blob.
            Assert.Equal(data, blob1.Read());

            testDirectory.Delete(true);
        }
Exemplo n.º 5
0
        private void HandleFailures(HttpMessage httpMessage, IPersistentBlob blob)
        {
            int  retryInterval;
            int  statusCode  = 0;
            bool shouldRetry = true;

            if (!httpMessage.HasResponse)
            {
                // HttpRequestException
                // Extend lease time so that it is not picked again for retry.
                blob.Lease(HttpPipelineHelper.MinimumRetryInterval);
            }
            else
            {
                statusCode = httpMessage.Response.Status;
                switch (statusCode)
                {
                case ResponseStatusCodes.PartialSuccess:
                    // Parse retry-after header
                    // Send Failed Messages To Storage
                    // Delete existing file
                    TrackResponse trackResponse = HttpPipelineHelper.GetTrackResponse(httpMessage);
                    var           content       = HttpPipelineHelper.GetPartialContentForRetry(trackResponse, httpMessage.Request.Content);
                    if (content != null)
                    {
                        retryInterval = HttpPipelineHelper.GetRetryInterval(httpMessage.Response);
                        blob.Delete();
                        _storage.SaveTelemetry(content, retryInterval);
                    }
                    break;

                case ResponseStatusCodes.RequestTimeout:
                case ResponseStatusCodes.ResponseCodeTooManyRequests:
                case ResponseStatusCodes.ResponseCodeTooManyRequestsAndRefreshCache:
                    // Extend lease time using retry interval period
                    // so that it is not picked up again before that.
                    retryInterval = HttpPipelineHelper.GetRetryInterval(httpMessage.Response);
                    blob.Lease(retryInterval);
                    break;

                case ResponseStatusCodes.InternalServerError:
                case ResponseStatusCodes.BadGateway:
                case ResponseStatusCodes.ServiceUnavailable:
                case ResponseStatusCodes.GatewayTimeout:
                    // Extend lease time so that it is not picked up again
                    blob.Lease(HttpPipelineHelper.MinimumRetryInterval);
                    break;

                default:
                    // Log Non-Retriable Status and don't retry or store;
                    // File will be cleared by maintenance job
                    shouldRetry = false;
                    break;
                }
            }

            if (shouldRetry)
            {
                AzureMonitorExporterEventSource.Log.WriteWarning("FailedToTransmitFromStorage", $"Error code is {statusCode}: Telemetry is stored offline for retry");
            }
            else
            {
                AzureMonitorExporterEventSource.Log.WriteWarning("FailedToTransmitFromStorage", $"Error code is {statusCode}: Telemetry is dropped");
            }
        }