From 388d49012236dacf23ce7e991f8018b94dbceb80 Mon Sep 17 00:00:00 2001 From: Brian Long Date: Tue, 1 Dec 2020 16:09:35 -0500 Subject: [PATCH] refactored; disabled repo caching for now --- pom.xml | 164 ++++++++++-------- .../me/brianlong/git/LRUExpiringHashMap.java | 10 ++ .../brianlong/git/LocalRepositoryCache.java | 122 ++++++++----- .../me/brianlong/git/CommandUnitTest.java | 10 +- src/test/resources/log4j2.xml | 2 +- 5 files changed, 179 insertions(+), 129 deletions(-) diff --git a/pom.xml b/pom.xml index 4fc919e..7f45d9d 100644 --- a/pom.xml +++ b/pom.xml @@ -1,77 +1,89 @@ - - 4.0.0 - me.brianlong - github-api - jar - 1.0-SNAPSHOT - - GitHub API & Utilities - - - 1.8 - 1.8 - UTF-8 - - - - - org.eclipse.jgit - org.eclipse.jgit - 5.9.0.202009080501-r - - - org.eclipse.jgit - org.eclipse.jgit.ssh.jsch - 5.9.0.202009080501-r - - - org.apache.httpcomponents - httpclient - 4.5.12 - - - com.fasterxml.jackson.jaxrs - jackson-jaxrs-json-provider - 2.11.0 - - - com.fasterxml.jackson.datatype - jackson-datatype-jsr310 - 2.11.0 - - - junit - junit - 4.13 - test - - - org.apache.logging.log4j - log4j-slf4j-impl - 2.13.2 - test - - - - - - - org.apache.maven.plugins - maven-surefire-plugin - - - unit-tests - test - test - - - **/*IT.class - - - - - - - + + 4.0.0 + me.brianlong + github-api + jar + 1.0-SNAPSHOT + + GitHub API & Utilities + + + 1.8 + 1.8 + UTF-8 + + + + + org.eclipse.jgit + org.eclipse.jgit + 5.9.0.202009080501-r + + + org.eclipse.jgit + org.eclipse.jgit.ssh.jsch + 5.9.0.202009080501-r + + + org.apache.httpcomponents + httpclient + 4.5.12 + + + com.fasterxml.jackson.jaxrs + jackson-jaxrs-json-provider + 2.11.0 + + + com.fasterxml.jackson.datatype + jackson-datatype-jsr310 + 2.11.0 + + + junit + junit + 4.13 + test + + + org.apache.logging.log4j + log4j-slf4j-impl + 2.13.2 + test + + + + + + + org.apache.maven.plugins + maven-surefire-plugin + + + **/*IT.class + + + + + org.apache.maven.plugins + maven-failsafe-plugin + + + integration-tests + integration-test + + integration-test + + + + **/*IT.class + + + + + + + diff --git a/src/main/java/me/brianlong/git/LRUExpiringHashMap.java b/src/main/java/me/brianlong/git/LRUExpiringHashMap.java index 6064626..08f4211 100644 --- a/src/main/java/me/brianlong/git/LRUExpiringHashMap.java +++ b/src/main/java/me/brianlong/git/LRUExpiringHashMap.java @@ -9,8 +9,13 @@ import java.util.List; import java.util.Map; import java.util.Set; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + // TODO need to implement the actual expiration schedule public class LRUExpiringHashMap implements ListeningMap { + + private final Logger logger = LoggerFactory.getLogger(LRUExpiringHashMap.class); private final Map map; private final List> listeners = new LinkedList>(); @@ -28,6 +33,8 @@ public class LRUExpiringHashMap implements ListeningM @Override public void addListener(MapListener listener) { + if (this.logger.isDebugEnabled()) + this.logger.debug("adding listener"); this.listeners.add(listener); } @@ -140,6 +147,9 @@ public class LRUExpiringHashMap implements ListeningM } public void expire(K key) { + if (this.logger.isDebugEnabled()) + this.logger.debug("expiring key from map: " + key); + if (!this.map.containsKey(key)) return; diff --git a/src/main/java/me/brianlong/git/LocalRepositoryCache.java b/src/main/java/me/brianlong/git/LocalRepositoryCache.java index 1ccce39..63bdab4 100644 --- a/src/main/java/me/brianlong/git/LocalRepositoryCache.java +++ b/src/main/java/me/brianlong/git/LocalRepositoryCache.java @@ -2,8 +2,6 @@ package me.brianlong.git; import java.io.File; import java.io.IOException; -import java.util.HashMap; -import java.util.Map; import java.util.Map.Entry; import java.util.UUID; @@ -27,7 +25,7 @@ public class LocalRepositoryCache { private final Logger logger = LoggerFactory.getLogger(LocalRepositoryCache.class); private final LRUExpiringHashMap cachedGits = new LRUExpiringHashMap(30); - private final Map gitIdsToUrls = new HashMap(); +// private final Map gitIdsToUrls = new HashMap(); private final File cacheDirectory = new File(System.getProperty("java.io.tmpdir"), "git"); private LocalRepositoryCache() { @@ -45,8 +43,8 @@ public class LocalRepositoryCache { } private void destroy() { - if (this.logger.isInfoEnabled()) - this.logger.info("destroy()"); + if (this.logger.isDebugEnabled()) + this.logger.debug("clearing local repo cache"); this.clear(); } @@ -63,8 +61,13 @@ public class LocalRepositoryCache { } public synchronized Git acquire(String url, GitCredentials creds, String branch) throws GitAPIException, InvalidRemoteException, RefNotFoundException { - Git git = this.cachedGits.remove(url); - if (git == null) { + if (this.logger.isTraceEnabled()) + this.logger.trace("acquire('" + url + "', " + creds + ", '" + branch + "')"); + +// Git git = this.cachedGits.remove(url); +// if (git == null) { + if (this.logger.isDebugEnabled()) + this.logger.debug("creating temporary Git directory"); File gitRepoDirectory = new File(this.cacheDirectory, UUID.randomUUID().toString() + ".git"); CloneCommand clone = new CloneCommand() @@ -72,29 +75,76 @@ public class LocalRepositoryCache { .setDirectory(gitRepoDirectory); if (branch != null) clone.setBranch(branch); - git = creds != null ? new CredentialedGit(clone, creds) : new CachedGit(clone); - this.gitIdsToUrls.put(git.getRepository().getIdentifier(), url); - } else { - if (branch != null) - git.checkout().setName(branch).call(); - git.pull().call(); - } - - return git; + + if (this.logger.isDebugEnabled()) + this.logger.debug("cloning Git repository: " + url); + return creds != null ? new CredentialedGit(clone, creds) : new CachedGit(clone); +// git = creds != null ? new CredentialedGit(clone, creds) : new CachedGit(clone); +// this.gitIdsToUrls.put(git.getRepository().getIdentifier(), url); +// } else { +// if (branch != null) { +// if (this.logger.isDebugEnabled()) +// this.logger.debug("switching Git branches: " + branch); +// git.checkout().setName(branch).call(); +// } +// +// if (this.logger.isDebugEnabled()) +// this.logger.debug("updating Git branch: " + branch); +// git.pull().call(); +// } } public synchronized void release(Git git) { - String sshUrl = this.gitIdsToUrls.get(git.getRepository().getIdentifier()); - this.cachedGits.put(sshUrl, git); + if (this.logger.isTraceEnabled()) + this.logger.trace("release('" + git.getRepository().getIdentifier() + "')"); + +// String url = this.gitIdsToUrls.get(git.getRepository().getIdentifier()); +// this.cachedGits.put(url, git); + this.expunge(git); } public synchronized void clear() { - this.cachedGits.clear(); + if (this.logger.isTraceEnabled()) + this.logger.trace("clear()"); + +// this.cachedGits.clear(); + } + + private void expunge(Git git) { +// gitIdsToUrls.remove(git.getRepository().getIdentifier()); + + File gitDir = git.getRepository().getDirectory(); + File workingTreeDir = git.getRepository().getWorkTree(); + git.getRepository().close(); + + try { + if (this.logger.isDebugEnabled()) + this.logger.debug("deleting: " + gitDir); + FileUtils.delete(gitDir, FileUtils.RECURSIVE); + } catch (IOException ie) { + this.logger.warn("Failed to delete a git directory: " + gitDir); + if (this.logger.isDebugEnabled()) + this.logger.debug(ie.getMessage(), ie); + gitDir.deleteOnExit(); + } + + try { + if (this.logger.isDebugEnabled()) + this.logger.debug("deleting: " + workingTreeDir); + FileUtils.delete(workingTreeDir, FileUtils.RECURSIVE); + } catch (IOException ie) { + this.logger.warn("Failed to delete a git directory: " + workingTreeDir); + if (this.logger.isDebugEnabled()) + this.logger.debug(ie.getMessage(), ie); + workingTreeDir.deleteOnExit(); + } } private class RepositoryCacheMapListener implements ExpiringMapListener { + + private final Logger logger = LoggerFactory.getLogger(LocalRepositoryCache.class); @Override public void accessed(Entry entry) { @@ -107,7 +157,9 @@ public class LocalRepositoryCache { @Override public void expired(Entry entry) { - this.expunge(entry.getValue()); + if (this.logger.isTraceEnabled()) + this.logger.trace("expired('" + entry.getKey() + "', '" + entry.getValue().getRepository().getIdentifier() + "')"); + expunge(entry.getValue()); } @Override @@ -117,33 +169,9 @@ public class LocalRepositoryCache { @Override public void cleared(Entry entry) { - this.expunge(entry.getValue()); - } - - private void expunge(Git git) { - gitIdsToUrls.remove(git.getRepository().getIdentifier()); - - File gitDir = git.getRepository().getDirectory(); - File workingTreeDir = git.getRepository().getWorkTree(); - git.getRepository().close(); - - try { - FileUtils.delete(gitDir, FileUtils.RECURSIVE); - } catch (IOException ie) { - logger.warn("Failed to delete a git directory: " + gitDir); - if (logger.isDebugEnabled()) - logger.debug(ie.getMessage(), ie); - gitDir.deleteOnExit(); - } - - try { - FileUtils.delete(workingTreeDir, FileUtils.RECURSIVE); - } catch (IOException ie) { - logger.warn("Failed to delete a git directory: " + workingTreeDir); - if (logger.isDebugEnabled()) - logger.debug(ie.getMessage(), ie); - workingTreeDir.deleteOnExit(); - } + if (this.logger.isTraceEnabled()) + this.logger.trace("cleared('" + entry.getKey() + "', '" + entry.getValue().getRepository().getIdentifier() + "')"); + expunge(entry.getValue()); } } diff --git a/src/test/java/me/brianlong/git/CommandUnitTest.java b/src/test/java/me/brianlong/git/CommandUnitTest.java index 1d09b46..fc48d7b 100644 --- a/src/test/java/me/brianlong/git/CommandUnitTest.java +++ b/src/test/java/me/brianlong/git/CommandUnitTest.java @@ -3,12 +3,14 @@ package me.brianlong.git; import java.io.File; import java.io.IOException; import java.util.List; +import java.util.UUID; import org.eclipse.jgit.api.CloneCommand; import org.eclipse.jgit.api.Git; import org.eclipse.jgit.api.ListBranchCommand.ListMode; import org.eclipse.jgit.api.errors.GitAPIException; import org.eclipse.jgit.lib.Ref; +import org.eclipse.jgit.util.FileUtils; import org.junit.AfterClass; import org.junit.Assert; import org.junit.BeforeClass; @@ -23,8 +25,7 @@ public class CommandUnitTest { @BeforeClass public static void init() throws GitAPIException, IOException { - tmpdir = new File(System.getProperty("java.io.tmpdir"), "git.tmp"); - tmpdir.mkdirs(); + tmpdir = new File(System.getProperty("java.io.tmpdir"), "git-" + UUID.randomUUID().toString() + ".tmp"); git = new CloneCommand() .setURI("git@github.com:bmlong137/env-docker-adbp.git") @@ -34,11 +35,10 @@ public class CommandUnitTest { } @AfterClass - public static void cleanup() { - git.getRepository().close(); + public static void cleanup() throws IOException { git.close(); - tmpdir.deleteOnExit(); + FileUtils.delete(tmpdir, FileUtils.RECURSIVE); } @Test diff --git a/src/test/resources/log4j2.xml b/src/test/resources/log4j2.xml index 9ee57d9..7a59c70 100644 --- a/src/test/resources/log4j2.xml +++ b/src/test/resources/log4j2.xml @@ -12,7 +12,7 @@ - +