【问题标题】:Transactional CloudBlobs事务性 CloudBlob
【发布时间】:2011-07-29 07:18:12
【问题描述】:

我将 SQL Azure 用于 Blob 元数据存储,将 Azure Blob 存储用于实际 Blob。 Blob 创建/删除是通过在环境 TransactionScope 中登记这些操作来实现的。到目前为止一切正常,但我想知道是否有人可以推荐对删除操作的优化(请参阅下面的源代码),这可能会消除下载 blob 内容以回滚的要求。

public class CloudBlobDeletionEnlistment : CloudBlobBaseEnlistment,
  IEnlistmentNotification,
  IDisposable
{
  public CloudBlobDeletionEnlistment(Guid ownerId, string blobId, CloudBlobContainer container, Logger logger, IUserUploadActivity currentUploadActivity)
  {
    ctx = new Context { OwnerId = ownerId, BlobId = blobId, Container = container, Logger = logger, CurrentUploadActivity = currentUploadActivity };
  }

  public ~CloudBlobDeletionEnlistment()
  {
    Dispose(false);
  }

  public class Context
  {
    public Guid OwnerId;
    public string BlobId;
    public string ContentFileName;
    public string MimeType;
    public bool IsCompressed;
    public CloudBlobContainer Container;
    public Logger Logger;
    public IUserUploadActivity CurrentUploadActivity;
  }

  private readonly Context ctx;
  private CloudBlob blob;

  public void Prepare(PreparingEnlistment preparingEnlistment)
  {
    blob = ctx.Container.GetBlobReference(ctx.BlobId);

    // save backup information
    ctx.ContentFileName = Path.GetTempFileName();
    blob.DownloadToFile(ctx.ContentFileName);
    blob.FetchAttributes();
    ctx.MimeType = blob.Metadata[Constants.BlobMetaAttributeContentType];
    ctx.IsCompressed = bool.Parse(blob.Metadata[Constants.BlobMetaAttributeCompressed]);

    // delete it
    blob.DeleteIfExists();

    // done
    preparingEnlistment.Prepared();
  }

  public void Commit(Enlistment enlistment)
  {
    Cleanup();

    // done
    enlistment.Done();
  }

  public void Rollback(Enlistment enlistment)
  {
    if (blob != null)
    {
      try
      {
        blob.UploadFile(ctx.ContentFileName);
        blob.Metadata[Constants.BlobMetaAttributeContentType] = ctx.MimeType;
        blob.Metadata[Constants.BlobMetaAttributeCompressed] = ctx.IsCompressed.ToString();
        blob.SetMetadata();
      }

      finally
      {
        Cleanup();
      }
    }

    else Cleanup();

    // done
    enlistment.Done();
  }

  public void InDoubt(Enlistment enlistment)
  {
    Cleanup();

    enlistment.Done();
  }

  void Cleanup()
  {
    // delete the temporary file holding the blob content
    if (!string.IsNullOrEmpty(ctx.ContentFileName) && File.Exists(ctx.ContentFileName))
    {
      File.Delete(ctx.ContentFileName);
      ctx.ContentFileName = null;
    }
  }

  public void Dispose()
  {
    Dispose(true);
    GC.SuppressFinalize(this);
  }

  protected virtual void Dispose(bool disposing)
  {
    if (disposing)
    {
      // free managed resources
    }

    // free native resources if there are any.
    Cleanup();
  }
  #endregion
}

【问题讨论】:

    标签: c# .net transactions azure


    【解决方案1】:

    对我来说,这似乎不是一种安全的回滚机制 - 上传可能会失败,如果发生这种情况,那么您的数据一致性就会被破坏。

    如果您通过将 blob 的名称放入数据库中的 ToBeDeleted 表中来删除 blob,然后您设置一些常规作业不时删除这些 blob,那会怎样?

    【讨论】:

      【解决方案2】:

      在我看来,您想在单个事务上下文中创建一个 blob 并创建元数据。这是不可能的。您的程序逻辑必须“成为事务”。

      同样适用于删除逻辑。

      【讨论】:

      • 当然这只是 ACID 事务的近似值,但在我看来,这比在实际数据库事务之外处理它要好。
      • 这是一个老问题,但关于程序逻辑,是否有设计模式?因为它看起来像是在重新发明轮子..?
      猜你喜欢
      • 1970-01-01
      • 2020-03-16
      • 2010-12-08
      • 2020-12-15
      • 2013-03-29
      • 1970-01-01
      • 2012-08-19
      • 2013-07-21
      • 1970-01-01
      相关资源
      最近更新 更多