繁体   English   中英

多线程上传器

[英]Multithreaded Uploader

我想将巨大的文件(介于2到40 GB之间)上传到Azure Blob存储。

首先,我成功地将每个文件分割成多个块(每个块= 2 MB)。 然后,我一个接一个地上传块,每个块成功上传后,我会更新一个临时文件,以便在应用程序关闭的情况下可以继续上传。

现在,我要使上传操作成为多线程。 在审查了TPL之后,我很困惑从哪里开始!

从TPL开始的任何指导?

void Upload(int segmentId)
{
    try
    {
        string blockId = GetBlockId(segmentId);
        var segment = GetSegment(FilePath, segmentId, SeqmentSize);
        var md5Hash = CalcMd5Hash(segment);

        var blob = new CloudBlockBlob(_link.Uri);
        using (var memoryStream = new MemoryStream(segment))
        {
            blob.PutBlock(blockId, memoryStream, md5Hash);
        }

        SerializeStatus();
    }
    catch (Exception exception)
    {
        ...
    }
}

我在很久以前就构建了类似的东西(尽管我使用了异步方法而不是TPL),我想upload really large blobs with resumable capability 这是我所做的:

  1. 首先,根据块大小,我将文件分成多个块。 每个块均分配有一个ID。 然后,我创建了一个对象,其中包含块ID和该块的状态。 为简单起见,我不停地出现以下情况的一大块- NotStartedSuccessfulFailed
  2. 然后,我创建了这些块的集合,并将该数据序列化为文件。
  3. 根据并行线程的数量(比方说x),我从状态为NotStarted的集合中获取了x个项目。 然后,我并行处理了这些块。 我将块ID传递为用户状态,这样当我回叫时,根据上传状态,我会相应地更新集合并序列化数据。
  4. 所有块都上传完后,我检查是否有任何失败的块。 如果有的话,我重试这些块。
  5. 一旦所有块都成功完成,我就可以从chunks集合中创建一个阻止列表,然后提交该阻止列表。 如果提交阻止列表操作成功,我只是删除了包含块数据的文件。

希望这可以帮助。

更新

看看这个伪代码,看看它是否对您有帮助:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace UploadLargeBlob
{
    class Program
    {
        static void Main(string[] args)
        {
            List<ChunkInformation> chunksToUpload = new List<ChunkInformation>();
            CreateChunkCollection("MyVeryLargeFile", 2*1024*1024);
            int numberOfParallelThreads = 8;
            do
            {
                var chunksToProcess = chunksToUpload.Where(c => c.Status == ChunkStatus.NotStarted || c.Status == ChunkStatus.Failed).Take(numberOfParallelThreads);
                if (chunksToProcess.Count() == 0)
                {
                    break;
                }
                List<Task> tasks = new List<Task>();
                try
                {
                    foreach (var chunk in chunksToProcess)
                    {
                        tasks.Add(Task.Factory.StartNew(() =>
                            {
                                DoUpload(chunk);
                            }, chunk));
                    }
                    Task.WaitAll(tasks.ToArray());
                }
                catch (AggregateException excep)
                {
                    foreach (var task in tasks)
                    {
                        if (task.Exception != null)
                        {
                            ChunkInformation chunk = task.AsyncState as ChunkInformation;
                            chunk.Status = ChunkStatus.Failed;
                            //Now serialize the data.
                        }
                    }
                }
            }
            while (true);
        }

        static void DoUpload(ChunkInformation chunk)
        {
            //Do the actual upload

            //Update chunk status once chunk is uploaded
            chunk.Status = ChunkStatus.Successful;

            //Serialize the data.
        }

        static void CreateChunkCollection(string fileName, int chunkSize)
        {
        }
    }

    public class ChunkInformation
    {
        public string Id
        {
            get;
            set;
        }

        public ChunkStatus Status
        {
            get;
            set;
        }
    }

    public enum ChunkStatus
    {
        NotStarted,
        Successful,
        Failed
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM