Parallel calls to Repository.ObjectDatabase.CreateBlob with identical contents fails.

未关闭
#1,122 2 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
4/5
预计耗时
3-5 天
新手友好度
35/100
Issue 类型
缺陷
描述清晰度
基本清楚
活跃度
停滞
技术栈
csharp, git
领域
databases

调研方向

从 Repository.ObjectDatabase.CreateBlob 开始,针对 bare repository 运行 C# 重现。跟踪相同内容的并行路径,以及报告的 FILE_EXISTS 和 null-blob 结果。并发调用在创建预期 blob 的同时完成,且不出现任一失败,即表示完成。

由索引模型根据 Issue 内容生成。

描述

I would like to insert blobs from memory into an object database in parallel (based on the technique described at http://stackoverflow.com/a/16244234). This works fine most of the time, but is subject to race conditions if the contents are the same. There are two classes of errors I observe:
1. Exception due to FILE_EXISTS conflict when trying to create the directory with the first two characters of the OID under objects.
2. NULL blob returned from CreateBlob.

Here's some example code to reproduce the problem:

using System;
using System.Text;
using System.IO;
using System.Threading.Tasks;
using LibGit2Sharp;

namespace GitODBStress
{
    class Program
    {
        static void Main()
        {

            string repoPath = Path.Combine(Path.GetTempPath(), "Git_Parallel_Odb");
            int iterationCount = 100;

            if (!Directory.Exists(repoPath))
            {
                Directory.CreateDirectory(repoPath);
            }

            var path = Repository.Init(repoPath, isBare:true);
            Console.WriteLine("Made repo at {0}", path);

            var repo = new Repository(path);

            var tasks = new Task[2];
            for (int i = 0; i < iterationCount; ++i)
            {
                var iter = i;
                byte[] buf = Encoding.ASCII.GetBytes("foo" + iter.ToString());
                for (int taskNo = 0; taskNo < 2; ++taskNo)
                {
                    tasks[taskNo] = Task.Factory.StartNew(() =>
                        {
                            MemoryStream ms = new MemoryStream(buf);
                            for (; ; )
                            {
                                try
                                {
                                    var blob = repo.ObjectDatabase.CreateBlob(ms);
                                    if (blob == null)
                                    {
                                        Console.WriteLine("ERROR: NULL blob on iteration {0}", iter);
                                        continue;
                                    }

                                    break;
                                }
                                catch (LibGit2SharpException e)
                                {
                                    Console.WriteLine("ERROR: Caught LibGit2SharpException on iteration {0}: {1}", iter, e.Message);
                                    if (e.Message.Contains("Cannot create a file when that file already exists."))
                                    {
                                        continue;
                                    }
                                }
                            }
                        });
                }

                Task.WaitAll(tasks);
            }

            repo.Dispose();

            // clean up temp repo files
            var files = Directory.GetFiles(repoPath, "*.*", SearchOption.AllDirectories);
            foreach (var f in files)
            {
                var attrib = File.GetAttributes(f);
                if (attrib.HasFlag(FileAttributes.ReadOnly))
                {
                    File.SetAttributes(f, attrib & ~FileAttributes.ReadOnly);
                }
            }

            Directory.Delete(repoPath, true);
        }
    }
}

Thanks!

主要语言
C#
星标
3.5k
派生
925
PR 合并指标
30 天内没有已合并 PR

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

libgit2/libgit2sharp 的其他 Issue

查看 libgit2/libgit2sharp 的全部 Issue

相似的 Issue

更多 C# Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。