/// <summary>
        /// Uploads the buffer.
        /// </summary>
        /// <param name="buffer">The buffer.</param>
        /// <param name="bytesToCopy">The bytes to copy.</param>
        /// <param name="targetStreamOffset">The target stream offset.</param>
        /// <returns></returns>
        private long UploadBuffer(byte[] buffer, int bytesToCopy, long targetStreamOffset)
        {
            //append it to the remote stream
            int  attemptCount    = 0;
            bool uploadCompleted = false;

            while (!uploadCompleted && attemptCount < MaxBufferUploadAttemptCount)
            {
                _token.ThrowIfCancellationRequested();
                attemptCount++;
                try
                {
                    if (targetStreamOffset == 0)
                    {
                        _frontEnd.CreateStream(_segmentMetadata.Path, true, buffer, bytesToCopy);
                    }
                    else
                    {
                        _frontEnd.AppendToStream(_segmentMetadata.Path, buffer, targetStreamOffset, bytesToCopy);
                    }

                    uploadCompleted     = true;
                    targetStreamOffset += bytesToCopy;
                    ReportProgress(targetStreamOffset, false);
                }
                catch
                {
                    //if we tried more than the number of times we were allowed to, give up and throw the exception
                    if (attemptCount >= MaxBufferUploadAttemptCount)
                    {
                        ReportProgress(targetStreamOffset, true);
                        throw;
                    }
                    else
                    {
                        WaitForRetry(attemptCount, this.UseBackOffRetryStrategy, _token);
                    }
                }
            }

            return(targetStreamOffset);
        }
Exemple #2
0
        /// <summary>
        /// Uploads the buffer.
        /// </summary>
        /// <param name="buffer">The buffer.</param>
        /// <param name="bytesToCopy">The bytes to copy.</param>
        /// <param name="targetStreamOffset">The target stream offset.</param>
        /// <returns></returns>
        private long UploadBuffer(byte[] buffer, int bytesToCopy, long targetStreamOffset)
        {
            //append it to the remote stream
            int  attemptCount    = 0;
            bool uploadCompleted = false;

            while (!uploadCompleted && attemptCount < MaxBufferUploadAttemptCount)
            {
                _token.ThrowIfCancellationRequested();
                attemptCount++;
                try
                {
                    if (targetStreamOffset == 0)
                    {
                        _frontEnd.CreateStream(_segmentMetadata.Path, true, buffer, bytesToCopy);
                    }
                    else
                    {
                        _frontEnd.AppendToStream(_segmentMetadata.Path, buffer, targetStreamOffset, bytesToCopy);
                    }

                    uploadCompleted     = true;
                    targetStreamOffset += bytesToCopy;
                    ReportProgress(targetStreamOffset, false);
                }
                catch (AggregateException e)
                {
                    if (e.InnerExceptions.Count == 1 && e.InnerException is AdlsErrorException)
                    {
                        if (((AdlsErrorException)e.InnerException).Body.RemoteException is AdlsBadOffsetException)
                        {
                            // this means we tried to re-upload at the same location and the upload actually succeeded, which means we should move on.
                            uploadCompleted     = true;
                            targetStreamOffset += bytesToCopy;
                            ReportProgress(targetStreamOffset, false);
                        }
                        else
                        {
                            //if we tried more than the number of times we were allowed to, give up and throw the exception
                            if (attemptCount >= MaxBufferUploadAttemptCount)
                            {
                                ReportProgress(targetStreamOffset, true);
                                throw e;
                            }
                            else
                            {
                                WaitForRetry(attemptCount, this.UseBackOffRetryStrategy, _token);
                            }
                        }
                    }
                    else
                    {
                        //if we tried more than the number of times we were allowed to, give up and throw the exception
                        if (attemptCount >= MaxBufferUploadAttemptCount)
                        {
                            ReportProgress(targetStreamOffset, true);
                            throw e;
                        }
                        else
                        {
                            WaitForRetry(attemptCount, this.UseBackOffRetryStrategy, _token);
                        }
                    }
                }
                catch (AdlsErrorException e)
                {
                    if (e.Body.RemoteException is AdlsBadOffsetException)
                    {
                        // this means we tried to re-upload at the same location and the upload actually succeeded, which means we should move on.
                        uploadCompleted     = true;
                        targetStreamOffset += bytesToCopy;
                        ReportProgress(targetStreamOffset, false);
                    }
                    else
                    {
                        //if we tried more than the number of times we were allowed to, give up and throw the exception
                        if (attemptCount >= MaxBufferUploadAttemptCount)
                        {
                            ReportProgress(targetStreamOffset, true);
                            throw e;
                        }
                        else
                        {
                            WaitForRetry(attemptCount, this.UseBackOffRetryStrategy, _token);
                        }
                    }
                }
                catch (Exception ex)
                {
                    //if we tried more than the number of times we were allowed to, give up and throw the exception
                    if (attemptCount >= MaxBufferUploadAttemptCount)
                    {
                        ReportProgress(targetStreamOffset, true);
                        throw ex;
                    }
                    else
                    {
                        WaitForRetry(attemptCount, this.UseBackOffRetryStrategy, _token);
                    }
                }
            }

            return(targetStreamOffset);
        }