Skip to content

The Unified and Autonomous Development Platform

Cache pull request changed files to speed up gate keeper checking.

robin shine009f209803b9
diff --git a/commons.util/src/main/java/com/pmease/commons/util/FileUtils.java b/commons.util/src/main/java/com/pmease/commons/util/FileUtils.java
index c200be7cbe..dedc639b53 100644
--- a/commons.util/src/main/java/com/pmease/commons/util/FileUtils.java
+++ b/commons.util/src/main/java/com/pmease/commons/util/FileUtils.java
@@ -6,21 +6,31 @@ import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
+import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.Properties;
+import java.util.concurrent.Callable;
+import java.util.concurrent.locks.Lock;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import org.apache.commons.io.IOUtils;
+import org.apache.commons.lang3.SerializationUtils;
import org.apache.tools.ant.DirectoryScanner;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import com.google.common.base.Preconditions;
+import com.google.common.base.Throwables;
import com.pmease.commons.bootstrap.BootstrapUtils;
public class FileUtils extends org.apache.commons.io.FileUtils {
+ private static final Logger logger = LoggerFactory.getLogger(FileUtils.class);
+
/**
* Load properties from specified file. Surrounding white spaces of property values will
* be trimmed off. And a property is considered not defined if the value is trimmed to
@@ -271,4 +281,46 @@ public class FileUtils extends org.apache.commons.io.FileUtils {
createDir(dir);
}
}
+
+ @SuppressWarnings("unchecked")
+ public static <T extends Serializable> T readFile(File file, Callable<T> callable) {
+ T result = null;
+ String lockName;
+ try {
+ lockName = file.getCanonicalPath();
+ } catch (IOException e) {
+ throw new RuntimeException(e);
+ }
+ Lock lock = LockUtils.getReadWriteLock(lockName).readLock();
+ try {
+ lock.lockInterruptibly();
+ if (file.exists()) {
+ byte[] bytes = FileUtils.readFileToByteArray(file);
+ result = (T) SerializationUtils.deserialize(bytes);
+ }
+ } catch (Exception e) {
+ logger.error("Error reading callable result from file '" + file.getAbsolutePath()
+ + "', fall back to execute callable.", e);
+ } finally {
+ lock.unlock();
+ }
+
+ if (result == null) {
+ FileUtils.createDir(file.getParentFile());
+
+ lock = LockUtils.getReadWriteLock(lockName).writeLock();
+ try {
+ lock.lockInterruptibly();
+ Preconditions.checkNotNull(result = callable.call());
+
+ FileUtils.writeByteArrayToFile(file, SerializationUtils.serialize(result));
+ } catch (Exception e) {
+ Throwables.propagate(e);
+ } finally {
+ lock.unlock();
+ }
+ }
+
+ return result;
+ }
}
diff --git a/gitplex.core/src/main/java/com/pmease/gitplex/core/manager/impl/DefaultPullRequestManager.java b/gitplex.core/src/main/java/com/pmease/gitplex/core/manager/impl/DefaultPullRequestManager.java
index df33844c5d..890637c6b7 100644
--- a/gitplex.core/src/main/java/com/pmease/gitplex/core/manager/impl/DefaultPullRequestManager.java
+++ b/gitplex.core/src/main/java/com/pmease/gitplex/core/manager/impl/DefaultPullRequestManager.java
@@ -132,8 +132,6 @@ public class DefaultPullRequestManager implements PullRequestManager {
request.setUpdateDate(audit.getDate());
dao.persist(request);
- deleteRefsUponClose(request);
-
final Long requestId = request.getId();
dao.afterCommit(new Runnable() {
@@ -227,8 +225,6 @@ public class DefaultPullRequestManager implements PullRequestManager {
dao.persist(request);
- deleteRefsUponClose(request);
-
final Long requestId = request.getId();
dao.afterCommit(new Runnable() {
@@ -309,13 +305,6 @@ public class DefaultPullRequestManager implements PullRequestManager {
return strategies;
}
- private void deleteRefsUponClose(PullRequest request) {
- Git git = request.getTarget().getRepository().git();
- for (PullRequestUpdate update: request.getUpdates())
- git.deleteRef(update.getReferentialRef(), null, null);
- git.deleteRef(request.getIntegrateRef(), null, null);
- }
-
@Transactional
@Override
public void onTargetBranchUpdate(PullRequest request) {
diff --git a/gitplex.core/src/main/java/com/pmease/gitplex/core/manager/impl/DefaultStorageManager.java b/gitplex.core/src/main/java/com/pmease/gitplex/core/manager/impl/DefaultStorageManager.java
index 7cf9e91e15..b2b848bd3e 100644
--- a/gitplex.core/src/main/java/com/pmease/gitplex/core/manager/impl/DefaultStorageManager.java
+++ b/gitplex.core/src/main/java/com/pmease/gitplex/core/manager/impl/DefaultStorageManager.java
@@ -47,9 +47,8 @@ public class DefaultStorageManager implements StorageManager {
}
private File createIfNotExist(File dir) {
- if (!dir.exists()) synchronized(this) {
+ if (!dir.exists())
FileUtils.createDir(dir);
- }
return dir;
}
}
diff --git a/gitplex.core/src/main/java/com/pmease/gitplex/core/model/PullRequestUpdate.java b/gitplex.core/src/main/java/com/pmease/gitplex/core/model/PullRequestUpdate.java
index 376f0d8648..e880d39288 100644
--- a/gitplex.core/src/main/java/com/pmease/gitplex/core/model/PullRequestUpdate.java
+++ b/gitplex.core/src/main/java/com/pmease/gitplex/core/model/PullRequestUpdate.java
@@ -10,7 +10,7 @@ import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
-import java.util.concurrent.locks.Lock;
+import java.util.concurrent.Callable;
import javax.annotation.Nullable;
import javax.persistence.CascadeType;
@@ -20,15 +20,11 @@ import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
-import org.apache.commons.lang3.SerializationUtils;
-
import com.google.common.base.Preconditions;
-import com.google.common.base.Throwables;
import com.pmease.commons.git.Commit;
import com.pmease.commons.git.Git;
import com.pmease.commons.hibernate.AbstractEntity;
import com.pmease.commons.util.FileUtils;
-import com.pmease.commons.util.LockUtils;
import com.pmease.gitplex.core.GitPlex;
import com.pmease.gitplex.core.manager.StorageManager;
@@ -52,13 +48,9 @@ public class PullRequestUpdate extends AbstractEntity {
@OneToMany(mappedBy="update", cascade=CascadeType.REMOVE)
private Collection<Vote> votes = new ArrayList<Vote>();
- private transient String referentialCommitHash;
-
- private transient List<Commit> logCommits;
-
private transient List<Commit> commits;
-
- private transient Collection<String> changedFiles;
+
+ private transient CachedInfo cachedInfo;
public PullRequest getRequest() {
return request;
@@ -101,84 +93,11 @@ public class PullRequestUpdate extends AbstractEntity {
this.votes = votes;
}
- public String getReferentialRef() {
- Preconditions.checkNotNull(getId());
- return Repository.REFS_GITPLEX + "updates/" + getId() + "/referential";
- }
-
public String getHeadRef() {
Preconditions.checkNotNull(getId());
return Repository.REFS_GITPLEX + "updates/" + getId() + "/head";
}
- /**
- * Calculate referential commit for change calculation of this update. Referential commit is merged
- * commit of:
- *
- * <li> merge base of update head and target branch head
- * <li> head of previous update
- *
- * Changed files of this update will be calculated between referential commit and head commit
- * and this effectively represents changes made since previous update with merged changes
- * from target branch excluded if there is any.
- *
- * @return
- * referential commit used for change calculation of current update
- */
- public String getReferentialCommitHash() {
- if (referentialCommitHash == null) {
- Git git = getRequest().getTarget().getRepository().git();
- String mergeBase = git.calcMergeBase(getHeadCommitHash(), getRequest().getTarget().getHeadCommitHash());
-
- if (git.isAncestor(getBaseCommitHash(), mergeBase)) {
- referentialCommitHash = mergeBase;
- } else if (git.isAncestor(mergeBase, getBaseCommitHash())) {
- referentialCommitHash = getBaseCommitHash();
- } else {
- Lock lock = LockUtils.getLock("update.getReferentialCommit." + getId());
- try {
- lock.lockInterruptibly();
- String changeRef = getReferentialRef();
- referentialCommitHash = git.parseRevision(changeRef, false);
-
- if (referentialCommitHash != null) {
- Commit commit = git.showRevision(referentialCommitHash);
- if (!commit.getParentHashes().contains(mergeBase) || !commit.getParentHashes().contains(getBaseCommitHash()))
- referentialCommitHash = null;
- }
-
- if (referentialCommitHash == null) {
- File tempDir = FileUtils.createTempDir();
- try {
- Git tempGit = new Git(tempDir);
-
- /*
- * Branch name here is not significant, we just use an existing branch
- * in cloned repository to hold mergeBase, so that we can merge with
- * previousUpdate
- */
- String branchName = getRequest().getTarget().getName();
- tempGit.clone(git.repoDir().getAbsolutePath(), false, true, true, branchName);
- tempGit.updateRef("HEAD", mergeBase, null, null);
- tempGit.reset(null, null);
- Preconditions.checkState(tempGit.merge(getBaseCommitHash(), null, null, "ours", null) != null);
- git.fetch(tempGit, "+HEAD:" + changeRef);
- referentialCommitHash = git.parseRevision(changeRef, true);
- } finally {
- FileUtils.deleteDir(tempDir);
- }
- }
- } catch (InterruptedException e) {
- throw new RuntimeException(e);
- } finally {
- lock.unlock();
- }
- }
- }
-
- return referentialCommitHash;
- }
-
/**
* List votes against this update and all subsequent updates.
* <p>
@@ -201,13 +120,10 @@ public class PullRequestUpdate extends AbstractEntity {
public void deleteRefs() {
Git git = getRequest().getTarget().getRepository().git();
git.deleteRef(getHeadRef(), null, null);
- git.deleteRef(getReferentialRef(), null, null);
}
public Collection<String> getChangedFiles() {
- if (changedFiles == null)
- changedFiles = getRequest().git().listChangedFiles(getReferentialCommitHash(), getHeadCommitHash(), null);
- return changedFiles;
+ return getCachedInfo().getChangedFiles();
}
public String getBaseCommitHash() {
@@ -238,45 +154,58 @@ public class PullRequestUpdate extends AbstractEntity {
return request.getBaseCommit();
}
- @SuppressWarnings("unchecked")
- private List<Commit> getLogCommits() {
- if (logCommits == null) {
+ private CachedInfo getCachedInfo() {
+ if (cachedInfo == null) {
StorageManager storageManager = GitPlex.getInstance(StorageManager.class);
- String lockName = "update.getCommits." + getId();
- File logFile = new File(storageManager.getCacheDir(this), "log");
- Lock lock = LockUtils.getReadWriteLock(lockName).readLock();
- try {
- lock.lockInterruptibly();
- if (logFile.exists()) {
- byte[] bytes = FileUtils.readFileToByteArray(logFile);
- logCommits = (List<Commit>) SerializationUtils.deserialize(bytes);
- }
- } catch (Exception e) {
- Throwables.propagate(e);
- } finally {
- lock.unlock();
- }
-
- if (logCommits == null) {
- lock = LockUtils.getReadWriteLock(lockName).writeLock();
- try {
- lock.lockInterruptibly();
- if (logFile.exists()) {
- byte[] bytes = FileUtils.readFileToByteArray(logFile);
- logCommits = (List<Commit>) SerializationUtils.deserialize(bytes);
+ File cacheFile = new File(storageManager.getCacheDir(this), "cachedInfo");
+ cachedInfo = FileUtils.readFile(cacheFile, new Callable<CachedInfo>() {
+
+ @Override
+ public CachedInfo call() throws Exception {
+ CachedInfo cachedInfo = new CachedInfo();
+
+ Git git = getRequest().getTarget().getRepository().git();
+ cachedInfo.setLogCommits(git.log(getBaseCommitHash(), getHeadCommitHash(), null, 0, 0));
+
+ String mergeBase = git.calcMergeBase(getHeadCommitHash(), getRequest().getTarget().getHeadCommitHash());
+
+ if (git.isAncestor(getBaseCommitHash(), mergeBase)) {
+ cachedInfo.setChangedFiles(git.listChangedFiles(mergeBase, getHeadCommitHash(), null));
+ } else if (git.isAncestor(mergeBase, getBaseCommitHash())) {
+ cachedInfo.setChangedFiles(git.listChangedFiles(getBaseCommitHash(), getHeadCommitHash(), null));
} else {
- Git git = getRequest().getTarget().getRepository().git();
- logCommits = git.log(getBaseCommitHash(), getHeadCommitHash(), null, 0, 0);
- FileUtils.writeByteArrayToFile(logFile, SerializationUtils.serialize((Serializable) logCommits));
+ File tempDir = FileUtils.createTempDir();
+ try {
+ Git tempGit = new Git(tempDir);
+
+ /*
+ * Calculate changed files of this update since merged commit of:
+ *
+ * 1. merge base of update head and target branch head
+ * 2. head of previous update
+ *
+ * This way changed files of this update will exclude merged changes
+ * from target branch if there is any.
+ */
+ String branchName = getRequest().getTarget().getName();
+ tempGit.clone(git.repoDir().getAbsolutePath(), false, true, true, branchName);
+ tempGit.updateRef("HEAD", mergeBase, null, null);
+ tempGit.reset(null, null);
+ Preconditions.checkNotNull(tempGit.merge(getBaseCommitHash(), null, null, "ours", null));
+
+ cachedInfo.setChangedFiles(tempGit.listChangedFiles(
+ tempGit.parseRevision("HEAD", true), getHeadCommitHash(), null));
+ } finally {
+ FileUtils.deleteDir(tempDir);
+ }
}
- } catch (Exception e) {
- Throwables.propagate(e);
- } finally {
- lock.unlock();
+
+ return cachedInfo;
}
- }
+
+ });
}
- return logCommits;
+ return cachedInfo;
}
/**
@@ -306,7 +235,7 @@ public class PullRequestUpdate extends AbstractEntity {
commits = new ArrayList<>();
Map<String, ScoreAwareCommit> allCommits = new LinkedHashMap<>();
- for (Commit commit: getLogCommits()) {
+ for (Commit commit: getCachedInfo().getLogCommits()) {
ScoreAwareCommit scoreAwareCommit = new ScoreAwareCommit();
scoreAwareCommit.setCommit(commit);
allCommits.put(commit.getHash(), scoreAwareCommit);
@@ -345,7 +274,7 @@ public class PullRequestUpdate extends AbstractEntity {
}
public Commit getHeadCommit() {
- Commit headCommit = getLogCommits().get(0);
+ Commit headCommit = getCachedInfo().getLogCommits().get(0);
Preconditions.checkState(headCommit.getHash().equals(getHeadCommitHash()));
return headCommit;
}
@@ -399,4 +328,27 @@ public class PullRequestUpdate extends AbstractEntity {
}
+ private static class CachedInfo implements Serializable {
+
+ private Collection<String> changedFiles;
+
+ private List<Commit> logCommits;
+
+ public Collection<String> getChangedFiles() {
+ return changedFiles;
+ }
+
+ public void setChangedFiles(Collection<String> changedFiles) {
+ this.changedFiles = changedFiles;
+ }
+
+ public List<Commit> getLogCommits() {
+ return logCommits;
+ }
+
+ public void setLogCommits(List<Commit> logCommits) {
+ this.logCommits = logCommits;
+ }
+
+ }
}
diff --git a/gitplex.core/src/test/java/com/pmease/gitplex/core/model/PullRequestUpdateTest.java b/gitplex.core/src/test/java/com/pmease/gitplex/core/model/PullRequestUpdateTest.java
index 57d24dd8d9..92e301c56c 100644
--- a/gitplex.core/src/test/java/com/pmease/gitplex/core/model/PullRequestUpdateTest.java
+++ b/gitplex.core/src/test/java/com/pmease/gitplex/core/model/PullRequestUpdateTest.java
@@ -6,8 +6,8 @@ import org.junit.Assert;
import org.junit.Test;
import org.mockito.Mockito;
+import com.google.common.collect.Sets;
import com.pmease.commons.git.AbstractGitTest;
-import com.pmease.commons.git.Commit;
import com.pmease.commons.git.Git;
import com.pmease.commons.loader.AppLoader;
import com.pmease.gitplex.core.manager.StorageManager;
@@ -55,7 +55,7 @@ public class PullRequestUpdateTest extends AbstractGitTest {
}
@Test
- public void testResolveChangeCommitWhenThereIsNoMerge() {
+ public void testResolveChangedFilesWhenThereIsNoMerge() {
PullRequest request = new PullRequest();
Branch target = new Branch();
target.setRepository(repository);
@@ -95,12 +95,12 @@ public class PullRequestUpdateTest extends AbstractGitTest {
target.setHeadCommitHash(bareGit.parseRevision("master", true));
- Assert.assertEquals(bareGit.showRevision("dev~1").getHash(), request.getLatestUpdate().getReferentialCommitHash());
- Assert.assertEquals(bareGit.showRevision("master~1").getHash(), request.getSortedUpdates().get(0).getReferentialCommitHash());
+ Assert.assertEquals(Sets.newHashSet("c"), update2.getChangedFiles());
+ Assert.assertEquals(Sets.newHashSet("b"), update1.getChangedFiles());
}
@Test
- public void testResolveChangeCommitWhenThereIsMerge() {
+ public void testResolveChangedFilesWhenThereIsMerge() {
PullRequest request = new PullRequest();
Branch target = new Branch();
target.setRepository(repository);
@@ -140,10 +140,7 @@ public class PullRequestUpdateTest extends AbstractGitTest {
target.setHeadCommitHash(bareGit.parseRevision("master", true));
- Commit referentialCommit = bareGit.showRevision(request.getLatestUpdate().getReferentialCommitHash());
- Assert.assertTrue(referentialCommit.getParentHashes().contains(bareGit.showRevision("master").getHash()));
- Assert.assertTrue(referentialCommit.getParentHashes().contains(bareGit.showRevision("dev~2").getHash()));
- Assert.assertEquals(bareGit.showRevision("master~1").getHash(), request.getSortedUpdates().get(0).getReferentialCommitHash());
+ Assert.assertEquals(Sets.newHashSet("4"), update2.getChangedFiles());
}
@Test
@@ -248,6 +245,8 @@ public class PullRequestUpdateTest extends AbstractGitTest {
request.getUpdates().add(update2);
+ target.setHeadCommitHash(bareGit.parseRevision("master", true));
+
Assert.assertEquals(2, update2.getCommits().size());
Assert.assertEquals("d2", update2.getCommits().get(0).getMessage());
Assert.assertEquals("d3", update2.getCommits().get(1).getMessage());