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

164
pom.xml
View File

@ -1,77 +1,89 @@
<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"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
<modelVersion>4.0.0</modelVersion> xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<groupId>me.brianlong</groupId> <modelVersion>4.0.0</modelVersion>
<artifactId>github-api</artifactId> <groupId>me.brianlong</groupId>
<packaging>jar</packaging> <artifactId>github-api</artifactId>
<version>1.0-SNAPSHOT</version> <packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>GitHub API &amp; Utilities</name>
<name>GitHub API &amp; Utilities</name>
<properties>
<maven.compiler.source>1.8</maven.compiler.source> <properties>
<maven.compiler.target>1.8</maven.compiler.target> <maven.compiler.source>1.8</maven.compiler.source>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.target>1.8</maven.compiler.target>
</properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency> <dependencies>
<groupId>org.eclipse.jgit</groupId> <dependency>
<artifactId>org.eclipse.jgit</artifactId> <groupId>org.eclipse.jgit</groupId>
<version>5.9.0.202009080501-r</version> <artifactId>org.eclipse.jgit</artifactId>
</dependency> <version>5.9.0.202009080501-r</version>
<dependency> </dependency>
<groupId>org.eclipse.jgit</groupId> <dependency>
<artifactId>org.eclipse.jgit.ssh.jsch</artifactId> <groupId>org.eclipse.jgit</groupId>
<version>5.9.0.202009080501-r</version> <artifactId>org.eclipse.jgit.ssh.jsch</artifactId>
</dependency> <version>5.9.0.202009080501-r</version>
<dependency> </dependency>
<groupId>org.apache.httpcomponents</groupId> <dependency>
<artifactId>httpclient</artifactId> <groupId>org.apache.httpcomponents</groupId>
<version>4.5.12</version> <artifactId>httpclient</artifactId>
</dependency> <version>4.5.12</version>
<dependency> </dependency>
<groupId>com.fasterxml.jackson.jaxrs</groupId> <dependency>
<artifactId>jackson-jaxrs-json-provider</artifactId> <groupId>com.fasterxml.jackson.jaxrs</groupId>
<version>2.11.0</version> <artifactId>jackson-jaxrs-json-provider</artifactId>
</dependency> <version>2.11.0</version>
<dependency> </dependency>
<groupId>com.fasterxml.jackson.datatype</groupId> <dependency>
<artifactId>jackson-datatype-jsr310</artifactId> <groupId>com.fasterxml.jackson.datatype</groupId>
<version>2.11.0</version> <artifactId>jackson-datatype-jsr310</artifactId>
</dependency> <version>2.11.0</version>
<dependency> </dependency>
<groupId>junit</groupId> <dependency>
<artifactId>junit</artifactId> <groupId>junit</groupId>
<version>4.13</version> <artifactId>junit</artifactId>
<scope>test</scope> <version>4.13</version>
</dependency> <scope>test</scope>
<dependency> </dependency>
<groupId>org.apache.logging.log4j</groupId> <dependency>
<artifactId>log4j-slf4j-impl</artifactId> <groupId>org.apache.logging.log4j</groupId>
<version>2.13.2</version> <artifactId>log4j-slf4j-impl</artifactId>
<scope>test</scope> <version>2.13.2</version>
</dependency> <scope>test</scope>
</dependencies> </dependency>
</dependencies>
<build>
<plugins> <build>
<plugin> <plugins>
<groupId>org.apache.maven.plugins</groupId> <plugin>
<artifactId>maven-surefire-plugin</artifactId> <groupId>org.apache.maven.plugins</groupId>
<executions> <artifactId>maven-surefire-plugin</artifactId>
<execution> <configuration>
<id>unit-tests</id> <excludes>
<phase>test</phase> <exclude>**/*IT.class</exclude>
<goals><goal>test</goal></goals> </excludes>
<configuration> </configuration>
<excludes> </plugin>
<exclude>**/*IT.class</exclude> <plugin>
</excludes> <groupId>org.apache.maven.plugins</groupId>
</configuration> <artifactId>maven-failsafe-plugin</artifactId>
</execution> <executions>
</executions> <execution>
</plugin> <id>integration-tests</id>
</plugins> <phase>integration-test</phase>
</build> <goals>
<goal>integration-test</goal>
</goals>
<configuration>
<includes>
<include>**/*IT.class</include>
</includes>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project> </project>

View File

@ -9,8 +9,13 @@ 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>>();
@ -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,29 +75,76 @@ 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); if (this.logger.isDebugEnabled())
} else { this.logger.debug("cloning Git repository: " + url);
if (branch != null) return creds != null ? new CredentialedGit(clone, creds) : new CachedGit(clone);
git.checkout().setName(branch).call(); // git = creds != null ? new CredentialedGit(clone, creds) : new CachedGit(clone);
git.pull().call(); // this.gitIdsToUrls.put(git.getRepository().getIdentifier(), url);
} // } else {
// if (branch != null) {
return git; // 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>