refactored; disabled repo caching for now

This commit is contained in:
Brian Long 2020-12-01 16:09:35 -05:00
parent 13e190fd5c
commit 388d490122
5 changed files with 179 additions and 129 deletions

24
pom.xml
View File

@ -1,4 +1,5 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" <project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<groupId>me.brianlong</groupId> <groupId>me.brianlong</groupId>
@ -59,16 +60,27 @@
<plugin> <plugin>
<groupId>org.apache.maven.plugins</groupId> <groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId> <artifactId>maven-surefire-plugin</artifactId>
<executions>
<execution>
<id>unit-tests</id>
<phase>test</phase>
<goals><goal>test</goal></goals>
<configuration> <configuration>
<excludes> <excludes>
<exclude>**/*IT.class</exclude> <exclude>**/*IT.class</exclude>
</excludes> </excludes>
</configuration> </configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<executions>
<execution>
<id>integration-tests</id>
<phase>integration-test</phase>
<goals>
<goal>integration-test</goal>
</goals>
<configuration>
<includes>
<include>**/*IT.class</include>
</includes>
</configuration>
</execution> </execution>
</executions> </executions>
</plugin> </plugin>

View File

@ -9,9 +9,14 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
// TODO need to implement the actual expiration schedule // TODO need to implement the actual expiration schedule
public class LRUExpiringHashMap<K extends Serializable, V> implements ListeningMap<K, V> { public class LRUExpiringHashMap<K extends Serializable, V> implements ListeningMap<K, V> {
private final Logger logger = LoggerFactory.getLogger(LRUExpiringHashMap.class);
private final Map<ExpiringHashKey, V> map; private final Map<ExpiringHashKey, V> map;
private final List<MapListener<K, V>> listeners = new LinkedList<MapListener<K, V>>(); private final List<MapListener<K, V>> listeners = new LinkedList<MapListener<K, V>>();
private final long expirationTimeMillis; private final long expirationTimeMillis;
@ -28,6 +33,8 @@ public class LRUExpiringHashMap<K extends Serializable, V> implements ListeningM
@Override @Override
public void addListener(MapListener<K, V> listener) { public void addListener(MapListener<K, V> listener) {
if (this.logger.isDebugEnabled())
this.logger.debug("adding listener");
this.listeners.add(listener); this.listeners.add(listener);
} }
@ -140,6 +147,9 @@ public class LRUExpiringHashMap<K extends Serializable, V> implements ListeningM
} }
public void expire(K key) { public void expire(K key) {
if (this.logger.isDebugEnabled())
this.logger.debug("expiring key from map: " + key);
if (!this.map.containsKey(key)) if (!this.map.containsKey(key))
return; return;

View File

@ -2,8 +2,6 @@ package me.brianlong.git;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry; import java.util.Map.Entry;
import java.util.UUID; import java.util.UUID;
@ -27,7 +25,7 @@ public class LocalRepositoryCache {
private final Logger logger = LoggerFactory.getLogger(LocalRepositoryCache.class); private final Logger logger = LoggerFactory.getLogger(LocalRepositoryCache.class);
private final LRUExpiringHashMap<String, Git> cachedGits = new LRUExpiringHashMap<String, Git>(30); private final LRUExpiringHashMap<String, Git> cachedGits = new LRUExpiringHashMap<String, Git>(30);
private final Map<String, String> gitIdsToUrls = new HashMap<String, String>(); // private final Map<String, String> gitIdsToUrls = new HashMap<String, String>();
private final File cacheDirectory = new File(System.getProperty("java.io.tmpdir"), "git"); private final File cacheDirectory = new File(System.getProperty("java.io.tmpdir"), "git");
private LocalRepositoryCache() { private LocalRepositoryCache() {
@ -45,8 +43,8 @@ public class LocalRepositoryCache {
} }
private void destroy() { private void destroy() {
if (this.logger.isInfoEnabled()) if (this.logger.isDebugEnabled())
this.logger.info("destroy()"); this.logger.debug("clearing local repo cache");
this.clear(); this.clear();
} }
@ -63,8 +61,13 @@ public class LocalRepositoryCache {
} }
public synchronized Git acquire(String url, GitCredentials creds, String branch) throws GitAPIException, InvalidRemoteException, RefNotFoundException { public synchronized Git acquire(String url, GitCredentials creds, String branch) throws GitAPIException, InvalidRemoteException, RefNotFoundException {
Git git = this.cachedGits.remove(url); if (this.logger.isTraceEnabled())
if (git == null) { 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"); File gitRepoDirectory = new File(this.cacheDirectory, UUID.randomUUID().toString() + ".git");
CloneCommand clone = new CloneCommand() CloneCommand clone = new CloneCommand()
@ -72,30 +75,77 @@ public class LocalRepositoryCache {
.setDirectory(gitRepoDirectory); .setDirectory(gitRepoDirectory);
if (branch != null) if (branch != null)
clone.setBranch(branch); 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) { public synchronized void release(Git git) {
String sshUrl = this.gitIdsToUrls.get(git.getRepository().getIdentifier()); if (this.logger.isTraceEnabled())
this.cachedGits.put(sshUrl, git); 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() { 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<String, Git> { private class RepositoryCacheMapListener implements ExpiringMapListener<String, Git> {
private final Logger logger = LoggerFactory.getLogger(LocalRepositoryCache.class);
@Override @Override
public void accessed(Entry<String, Git> entry) { public void accessed(Entry<String, Git> entry) {
} }
@ -107,7 +157,9 @@ public class LocalRepositoryCache {
@Override @Override
public void expired(Entry<String, Git> entry) { public void expired(Entry<String, Git> entry) {
this.expunge(entry.getValue()); if (this.logger.isTraceEnabled())
this.logger.trace("expired('" + entry.getKey() + "', '" + entry.getValue().getRepository().getIdentifier() + "')");
expunge(entry.getValue());
} }
@Override @Override
@ -117,33 +169,9 @@ public class LocalRepositoryCache {
@Override @Override
public void cleared(Entry<String, Git> entry) { public void cleared(Entry<String, Git> entry) {
this.expunge(entry.getValue()); if (this.logger.isTraceEnabled())
} this.logger.trace("cleared('" + entry.getKey() + "', '" + entry.getValue().getRepository().getIdentifier() + "')");
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();
}
} }
} }

View File

@ -3,12 +3,14 @@ package me.brianlong.git;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.util.List; import java.util.List;
import java.util.UUID;
import org.eclipse.jgit.api.CloneCommand; import org.eclipse.jgit.api.CloneCommand;
import org.eclipse.jgit.api.Git; import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.ListBranchCommand.ListMode; import org.eclipse.jgit.api.ListBranchCommand.ListMode;
import org.eclipse.jgit.api.errors.GitAPIException; import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.lib.Ref; import org.eclipse.jgit.lib.Ref;
import org.eclipse.jgit.util.FileUtils;
import org.junit.AfterClass; import org.junit.AfterClass;
import org.junit.Assert; import org.junit.Assert;
import org.junit.BeforeClass; import org.junit.BeforeClass;
@ -23,8 +25,7 @@ public class CommandUnitTest {
@BeforeClass @BeforeClass
public static void init() throws GitAPIException, IOException { public static void init() throws GitAPIException, IOException {
tmpdir = new File(System.getProperty("java.io.tmpdir"), "git.tmp"); tmpdir = new File(System.getProperty("java.io.tmpdir"), "git-" + UUID.randomUUID().toString() + ".tmp");
tmpdir.mkdirs();
git = new CloneCommand() git = new CloneCommand()
.setURI("git@github.com:bmlong137/env-docker-adbp.git") .setURI("git@github.com:bmlong137/env-docker-adbp.git")
@ -34,11 +35,10 @@ public class CommandUnitTest {
} }
@AfterClass @AfterClass
public static void cleanup() { public static void cleanup() throws IOException {
git.getRepository().close();
git.close(); git.close();
tmpdir.deleteOnExit(); FileUtils.delete(tmpdir, FileUtils.RECURSIVE);
} }
@Test @Test

View File

@ -12,7 +12,7 @@
<Logger name="org.apache.http" level="trace" additivity="false"> <Logger name="org.apache.http" level="trace" additivity="false">
<AppenderRef ref="STDOUT" /> <AppenderRef ref="STDOUT" />
</Logger> </Logger>
<Root level="trace"> <Root level="info">
<AppenderRef ref="STDOUT"/> <AppenderRef ref="STDOUT"/>
</Root> </Root>
</Loggers> </Loggers>