Inheritance: IDisposable
Ejemplo n.º 1
0
        protected static void PopulateContextWithUploadableRanges(FileInfo vhdFile, UploadContext context, bool resume)
        {
            using (var vds = new VirtualDiskStream(vhdFile.FullName))
            {
                IEnumerable <IndexRange> ranges = vds.Extents.Select(e => e.Range).ToArray();

                var bs = new BufferedStream(vds);
                if (resume)
                {
                    var alreadyUploadedRanges = context.DestinationBlob.GetPageRangesAsync()
                                                .ConfigureAwait(false).GetAwaiter().GetResult()
                                                .Select(pr => new IndexRange(pr.StartOffset, pr.EndOffset));
                    ranges = IndexRange.SubstractRanges(ranges, alreadyUploadedRanges);
                    context.AlreadyUploadedDataSize = alreadyUploadedRanges.Sum(ir => ir.Length);
                }
                var uploadableRanges = IndexRangeHelper.ChunkRangesBySize(ranges, PageSizeInBytes).ToArray();
                if (vds.DiskType == DiskType.Fixed)
                {
                    var nonEmptyUploadableRanges = GetNonEmptyRanges(bs, uploadableRanges).ToArray();
                    context.UploadableDataSize = nonEmptyUploadableRanges.Sum(r => r.Length);
                    context.UploadableRanges   = nonEmptyUploadableRanges;
                }
                else
                {
                    context.UploadableDataSize = uploadableRanges.Sum(r => r.Length);
                    context.UploadableRanges   = uploadableRanges;
                }
            }
        }
Ejemplo n.º 2
0
        public UploadContext Create()
        {
            AssertIfValidhVhd(localVhd);
            AssertIfValidVhdSize(localVhd);

            this.blobObjectFactory.CreateContainer(blobDestination);

            UploadContext context   = null;
            bool          completed = false;

            try
            {
                context = new UploadContext
                {
                    DestinationBlob     = destinationBlob,
                    SingleInstanceMutex = AcquireSingleInstanceMutex(destinationBlob.Uri)
                };

                if (overWrite)
                {
                    destinationBlob.DeleteIfExistsAsync(DeleteSnapshotsOption.IncludeSnapshots, null, requestOptions, operationContext: null)
                    .ConfigureAwait(false).GetAwaiter().GetResult();
                }

                if (destinationBlob.Exists(requestOptions))
                {
                    Program.SyncOutput.MessageResumingUpload();

                    if (destinationBlob.GetBlobMd5Hash(requestOptions) != null)
                    {
                        throw new InvalidOperationException(
                                  "An image already exists in blob storage with this name. If you want to upload again, use the Overwrite option.");
                    }
                    var metaData = destinationBlob.GetUploadMetaData();

                    AssertMetaDataExists(metaData);
                    AssertMetaDataMatch(metaData, OperationMetaData);

                    PopulateContextWithUploadableRanges(localVhd, context, true);
                    PopulateContextWithDataToUpload(localVhd, context);
                }
                else
                {
                    CreateRemoteBlobAndPopulateContext(context);
                }
                context.Md5HashOfLocalVhd = MD5HashOfLocalVhd;
                completed = true;
            }
            finally
            {
                if (!completed && context != null)
                {
                    context.Dispose();
                }
            }
            return(context);
        }
Ejemplo n.º 3
0
 public BlobSynchronizer(UploadContext context, int maxParallelism)
 {
     this.context             = context;
     this.md5Hash             = context.Md5HashOfLocalVhd;
     this.dataWithRanges      = context.UploadableDataWithRanges;
     this.dataToUpload        = context.UploadableDataSize;
     this.alreadyUploadedData = context.AlreadyUploadedDataSize;
     this.blob           = context.DestinationBlob;
     this.maxParallelism = maxParallelism;
 }
Ejemplo n.º 4
0
 public BlobSynchronizer(UploadContext context, int maxParallelism)
 {
     this.context = context;
     this.md5Hash = context.Md5HashOfLocalVhd;
     this.dataWithRanges = context.UploadableDataWithRanges;
     this.dataToUpload = context.UploadableDataSize;
     this.alreadyUploadedData = context.AlreadyUploadedDataSize;
     this.blob = context.DestinationBlob;
     this.maxParallelism = maxParallelism;
 }
Ejemplo n.º 5
0
 protected override void CreateRemoteBlobAndPopulateContext(UploadContext context)
 {
     CreateRemoteBlob();
     PopulateContextWithUploadableRanges(localVhd, context, false);
     PopulateContextWithDataToUpload(localVhd, context);
 }
Ejemplo n.º 6
0
 protected override void CreateRemoteBlobAndPopulateContext(UploadContext context)
 {
     CreateRemoteBlob();
     PopulateContextWithUploadableRanges(localVhd, context, true);
     PopulateContextWithDataToUpload(localVhd, context);
 }
Ejemplo n.º 7
0
        protected static IEnumerable <DataWithRange> GetDataWithRangesToUpload(FileInfo vhdFile, UploadContext context)
        {
            var uploadableRanges = context.UploadableRanges;
            var manager          = BufferManager.CreateBufferManager(Int32.MaxValue, MaxBufferSize);

            using (var vds = new VirtualDiskStream(vhdFile.FullName))
            {
                foreach (var range in uploadableRanges)
                {
                    var localRange = range;
                    yield return(new DataWithRange(manager)
                    {
                        Data = ReadBytes(vds, localRange, manager),
                        Range = localRange
                    });
                }
            }
            yield break;
        }
Ejemplo n.º 8
0
 protected static void PopulateContextWithDataToUpload(FileInfo vhdFile, UploadContext context)
 {
     context.UploadableDataWithRanges = GetDataWithRangesToUpload(vhdFile, context);
 }
Ejemplo n.º 9
0
 protected abstract void CreateRemoteBlobAndPopulateContext(UploadContext context);
Ejemplo n.º 10
0
 protected static IEnumerable<DataWithRange> GetDataWithRangesToUpload(FileInfo vhdFile, UploadContext context)
 {
     var uploadableRanges = context.UploadableRanges;
     var manager = BufferManager.CreateBufferManager(Int32.MaxValue, MaxBufferSize);
     using (var vds = new VirtualDiskStream(vhdFile.FullName))
     {
         foreach (var range in uploadableRanges)
         {
             var localRange = range;
             yield return new DataWithRange(manager)
                              {
                                  Data = ReadBytes(vds, localRange, manager),
                                  Range = localRange
                              };
         }
     }
     yield break;
 }
Ejemplo n.º 11
0
 protected static void PopulateContextWithDataToUpload(FileInfo vhdFile, UploadContext context)
 {
     context.UploadableDataWithRanges = GetDataWithRangesToUpload(vhdFile, context);
 }
Ejemplo n.º 12
0
        protected static void PopulateContextWithUploadableRanges(FileInfo vhdFile, UploadContext context, bool resume)
        {
            using (var vds = new VirtualDiskStream(vhdFile.FullName))
            {
                IEnumerable<IndexRange> ranges = vds.Extents.Select(e => e.Range).ToArray();

                var bs = new BufferedStream(vds);
                if (resume)
                {
                    var alreadyUploadedRanges = context.DestinationBlob.GetPageRanges().Select(pr => new IndexRange(pr.StartOffset, pr.EndOffset));
                    ranges = IndexRange.SubstractRanges(ranges, alreadyUploadedRanges);
                    context.AlreadyUploadedDataSize = alreadyUploadedRanges.Sum(ir => ir.Length);
                }
                var uploadableRanges = IndexRangeHelper.ChunkRangesBySize(ranges, PageSizeInBytes).ToArray();
                if(vds.DiskType == DiskType.Fixed)
                {
                    var nonEmptyUploadableRanges = GetNonEmptyRanges(bs, uploadableRanges).ToArray();
                    context.UploadableDataSize = nonEmptyUploadableRanges.Sum(r => r.Length);
                    context.UploadableRanges = nonEmptyUploadableRanges;
                }
                else
                {
                    context.UploadableDataSize = uploadableRanges.Sum(r => r.Length);
                    context.UploadableRanges = uploadableRanges;
                }
            }
        }
Ejemplo n.º 13
0
 protected abstract void CreateRemoteBlobAndPopulateContext(UploadContext context);
Ejemplo n.º 14
0
        public UploadContext Create()
        {
            AssertIfValidhVhd(localVhd);
            AssertIfValidVhdSize(localVhd);
            
            this.blobObjectFactory.CreateContainer(blobDestination);

            UploadContext context = null;
            bool completed = false;
            try
            {
                context = new UploadContext
                {
                    DestinationBlob = destinationBlob,
                    SingleInstanceMutex = AcquireSingleInstanceMutex(destinationBlob.Uri)
                };

                if (overWrite)
                {
                    destinationBlob.DeleteIfExists(DeleteSnapshotsOption.IncludeSnapshots, null, requestOptions);
                }

                if (destinationBlob.Exists(requestOptions))
                {
                    Program.SyncOutput.MessageResumingUpload();
                
                    if(destinationBlob.GetBlobMd5Hash(requestOptions) != null)
                    {
                        throw new InvalidOperationException(
                            "An image already exists in blob storage with this name. If you want to upload again, use the Overwrite option.");
                    }
                    var metaData = destinationBlob.GetUploadMetaData();
                
                    AssertMetaDataExists(metaData);
                    AssertMetaDataMatch(metaData, OperationMetaData);

                    PopulateContextWithUploadableRanges(localVhd, context, true);
                    PopulateContextWithDataToUpload(localVhd, context);
                }
                else
                {
                    CreateRemoteBlobAndPopulateContext(context);
                }
                context.Md5HashOfLocalVhd = MD5HashOfLocalVhd;
                completed = true;
            }
            finally
            {
                if(!completed && context != null)
                {
                    context.Dispose();
                }
            }
            return context;
        }