attempt to reduce amount of fetches: if we did not find a commit in local clone we... master
authorPavel Sher <pavel.sher@jetbrains.com>
Mon, 28 Jan 2019 17:10:33 +0000 (18:10 +0100)
committerPavel Sher <pavel.sher@jetbrains.com>
Mon, 28 Jan 2019 17:10:33 +0000 (18:10 +0100)
if we don't do that, then when fetch finishes we'll perform one more fetch even though it is unnecessary because commit is already fetched

git-server/src/jetbrains/buildServer/buildTriggers/vcs/git/GitCollectChangesPolicy.java

index f6c62fb8db6ce5ab3cdee0e120c91335f8fc470b..5924e3907126c935d9817335ca7fa8edde678ffe 100644 (file)
@@ -33,6 +33,7 @@ import org.jetbrains.annotations.NotNull;
 
 import java.io.IOException;
 import java.util.*;
+import java.util.concurrent.locks.ReentrantLock;
 
 import static com.intellij.openapi.util.text.StringUtil.isEmpty;
 
@@ -156,6 +157,21 @@ public class GitCollectChangesPolicy implements CollectChangesBetweenRepositorie
       if (myCommitLoader.findCommit(db, revision) != null)
         continue;
 
+      ReentrantLock lock = myRepositoryManager.getWriteLock(root.getRepositoryDir());
+      if (!lock.tryLock()) {
+        // some other thread fetches data to our repository,
+        // to avoid unnecessary fetches we need to wait for this operation to complete and then try to find commit again
+        lock.lock();
+        try {
+          if (myCommitLoader.findCommit(db, revision) != null)
+            continue;
+        } finally {
+          lock.unlock();
+        }
+      } else {
+        lock.unlock();
+      }
+
       if (!fetch.isInvoked())
         fetch.fetchTrackedRefs();