From a5786c0ca8aea15ea541467c459582b61c0e5dab Mon Sep 17 00:00:00 2001 From: Chin Huang Date: Tue, 9 Apr 2024 12:17:53 -0700 Subject: [PATCH] Git shallow clone for better performance By default, the server clones the entire commit history from a remote repository. Downloading a huge commit history might be slow, so add a configuration property to truncate the commit history to a few commits. Fixes #1544 --- .../environment-repository/git-backend.adoc | 14 ++++++++++++++ .../environment/JGitEnvironmentProperties.java | 16 ++++++++++++++++ .../environment/JGitEnvironmentRepository.java | 15 +++++++++++++-- 3 files changed, 43 insertions(+), 2 deletions(-) diff --git a/docs/modules/ROOT/pages/server/environment-repository/git-backend.adoc b/docs/modules/ROOT/pages/server/environment-repository/git-backend.adoc index 35608a0451..a95c7399a5 100644 --- a/docs/modules/ROOT/pages/server/environment-repository/git-backend.adoc +++ b/docs/modules/ROOT/pages/server/environment-repository/git-backend.adoc @@ -189,6 +189,20 @@ All other repositories are not cloned until configuration from the repository is NOTE: Setting a repository to be cloned when the Config Server starts up can help to identify a misconfigured configuration source (such as an invalid repository URI) quickly, while the Config Server is starting up. With `cloneOnStart` not enabled for a configuration source, the Config Server may start successfully with a misconfigured or invalid configuration source and not detect an error until an application requests configuration from that configuration source. +By default, the server clones the entire commit history from a remote repository. +Downloading a huge commit history might be slow, so the server can be configured to truncate the commit history to a few commits, as shown in the following top-level example: + +[source,yaml] +---- +spring: + cloud: + config: + server: + git: + uri: https://github.com/spring-cloud-samples/config-repo + depth: 1 +---- + [[authentication]] == Authentication diff --git a/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/environment/JGitEnvironmentProperties.java b/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/environment/JGitEnvironmentProperties.java index 7861839f21..a2e9a248b9 100644 --- a/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/environment/JGitEnvironmentProperties.java +++ b/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/environment/JGitEnvironmentProperties.java @@ -28,6 +28,7 @@ /** * @author Dylan Roberts * @author Gareth Clay + * @author Chin Huang */ public class JGitEnvironmentProperties extends AbstractScmAccessorProperties implements HttpEnvironmentRepositoryProperties { @@ -61,6 +62,13 @@ public class JGitEnvironmentProperties extends AbstractScmAccessorProperties */ private boolean cloneSubmodules = false; + /** + * If greater than zero, then truncate the history to the specified number of commits. + * Generally leads to faster queries because the entire history is not downloaded from + * the remote. + */ + private int depth = 0; + /** * Flag to indicate that the repository should force pull. If true discard any local * changes and take from remote repository. @@ -156,6 +164,14 @@ public void setCloneSubmodules(boolean cloneSubmodules) { this.cloneSubmodules = cloneSubmodules; } + public int getDepth() { + return this.depth; + } + + public void setDepth(int depth) { + this.depth = depth; + } + public boolean isForcePull() { return this.forcePull; } diff --git a/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/environment/JGitEnvironmentRepository.java b/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/environment/JGitEnvironmentRepository.java index 66c34b9f50..1b4a150053 100644 --- a/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/environment/JGitEnvironmentRepository.java +++ b/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/environment/JGitEnvironmentRepository.java @@ -78,6 +78,7 @@ * @author Ryan Lynch * @author Gareth Clay * @author ChaoDong Xi + * @author Chin Huang */ public class JGitEnvironmentRepository extends AbstractScmEnvironmentRepository implements EnvironmentRepository, SearchPathLocator, InitializingBean { @@ -167,7 +168,7 @@ public JGitEnvironmentRepository(ConfigurableEnvironment environment, JGitEnviro this.deleteUntrackedBranches = properties.isDeleteUntrackedBranches(); this.refreshRate = properties.getRefreshRate(); this.skipSslValidation = properties.isSkipSslValidation(); - this.gitFactory = new JGitFactory(properties.isCloneSubmodules()); + this.gitFactory = new JGitFactory(properties.isCloneSubmodules(), properties.getDepth()); this.tryMasterBranch = properties.isTryMasterBranch(); this.observationRegistry = observationRegistry; } @@ -778,12 +779,19 @@ public static class JGitFactory { private final boolean cloneSubmodules; + private final int depth; + public JGitFactory() { - this(false); + this(false, 0); } public JGitFactory(boolean cloneSubmodules) { + this(cloneSubmodules, 0); + } + + public JGitFactory(boolean cloneSubmodules, int depth) { this.cloneSubmodules = cloneSubmodules; + this.depth = depth; } public Git getGitByOpen(File file) throws IOException { @@ -793,6 +801,9 @@ public Git getGitByOpen(File file) throws IOException { public CloneCommand getCloneCommandByCloneRepository() { CloneCommand command = Git.cloneRepository().setCloneSubmodules(cloneSubmodules); + if (depth > 0) { + command.setDepth(depth); + } return command; }