Implement and test the swap job, and add integration test

This commit is contained in:
Winston Li
2016-08-24 18:04:01 +01:00
committed by Michael Mazour
parent dd5694104d
commit 9936fbe3c9
20 changed files with 258 additions and 45 deletions
@@ -25,7 +25,7 @@ public class GitBridgeApp implements Runnable {
"usage: writelatex-git-bridge config_file";
private String configFilePath;
private Config config;
Config config;
private GitBridgeServer server;
/**
@@ -98,6 +98,14 @@ public class Config implements JSONSource {
postbackURL += "/";
}
oauth2 = new Gson().fromJson(configObject.get("oauth2"), Oauth2.class);
swapStore = new Gson().fromJson(
configObject.get("swapStore"),
SwapStoreConfig.class
);
swapJob = new Gson().fromJson(
configObject.get("swapJob"),
SwapJobConfig.class
);
}
public String getSanitisedString() {
@@ -10,6 +10,9 @@ import java.util.Collection;
*/
public interface RepoStore {
/* Still need to get rid of these two methods.
Main dependency: GitRepoStore needs a Repository which needs a directory.
Instead, use a visitor or something. */
String getRepoStorePath();
File getRootDirectory();
@@ -20,7 +23,7 @@ public interface RepoStore {
long totalSize();
/*
/**
* Tars and bzip2s the .git directory of the given project. Throws an
* IOException if the project doesn't exist. The returned stream is a copy
* of the original .git directory, which must be deleted using remove().
@@ -5,9 +5,6 @@ package uk.ac.ic.wlgitbridge.bridge.swap.job;
*/
public class SwapJobConfig {
public static final SwapJobConfig DEFAULT =
new SwapJobConfig(1, 1, 2, 3600000);
private final int minProjects;
private final int lowGiB;
private final int highGiB;
@@ -19,6 +19,10 @@ public class InMemorySwapStore implements SwapStore {
store = new HashMap<>();
}
public InMemorySwapStore(SwapStoreConfig __) {
this();
}
@Override
public void upload(
String projectName,
@@ -17,6 +17,7 @@ public interface SwapStore {
{
put("noop", NoopSwapStore::new);
put("memory", InMemorySwapStore::new);
put("s3", S3SwapStore::new);
}
@@ -19,10 +19,10 @@ import javax.servlet.http.HttpServletRequest;
/* */
public class WLReceivePackFactory implements ReceivePackFactory<HttpServletRequest> {
private final Bridge bridgeAPI;
private final Bridge bridge;
public WLReceivePackFactory(Bridge bridgeAPI) {
this.bridgeAPI = bridgeAPI;
public WLReceivePackFactory(Bridge bridge) {
this.bridge = bridge;
}
@Override
@@ -33,7 +33,7 @@ public class WLReceivePackFactory implements ReceivePackFactory<HttpServletReque
if (hostname == null) {
hostname = httpServletRequest.getLocalName();
}
receivePack.setPreReceiveHook(new WriteLatexPutHook(bridgeAPI, hostname, oauth2));
receivePack.setPreReceiveHook(new WriteLatexPutHook(bridge, hostname, oauth2));
return receivePack;
}
@@ -26,12 +26,12 @@ import java.util.Iterator;
*/
public class WriteLatexPutHook implements PreReceiveHook {
private final Bridge bridgeAPI;
private final Bridge bridge;
private final String hostname;
private final Credential oauth2;
public WriteLatexPutHook(Bridge bridgeAPI, String hostname, Credential oauth2) {
this.bridgeAPI = bridgeAPI;
public WriteLatexPutHook(Bridge bridge, String hostname, Credential oauth2) {
this.bridge = bridge;
this.hostname = hostname;
this.oauth2 = oauth2;
}
@@ -75,7 +75,7 @@ public class WriteLatexPutHook implements PreReceiveHook {
private void handleReceiveCommand(Credential oauth2, Repository repository, ReceiveCommand receiveCommand) throws IOException, GitUserException {
checkBranch(receiveCommand);
checkForcedPush(receiveCommand);
bridgeAPI.putDirectoryContentsToProjectWithName(
bridge.putDirectoryContentsToProjectWithName(
oauth2,
repository.getWorkTree().getName(),
getPushedDirectoryContents(repository,
@@ -37,7 +37,7 @@ import java.util.EnumSet;
*/
public class GitBridgeServer {
private final Bridge bridgeAPI;
private final Bridge bridge;
private final Server jettyServer;
@@ -58,7 +58,7 @@ public class GitBridgeServer {
).resolve(".wlgb").resolve("wlgb.db").toFile()
);
SwapStore swapStore = SwapStore.fromConfig(config.getSwapStore());
bridgeAPI = Bridge.make(
bridge = Bridge.make(
repoStore,
dbStore,
swapStore,
@@ -83,6 +83,7 @@ public class GitBridgeServer {
public void start() {
try {
jettyServer.start();
bridge.startSwapJob();
Log.info(Util.getServiceName() + "-Git Bridge server started");
Log.info("Listening on port: " + port);
Log.info("Bridged to: " + apiBaseURL);
@@ -118,7 +119,7 @@ public class GitBridgeServer {
HandlerCollection handlers = new HandlerList();
handlers.addHandler(initResourceHandler());
handlers.addHandler(new PostbackHandler(bridgeAPI));
handlers.addHandler(new PostbackHandler(bridge));
handlers.addHandler(new DefaultHandler());
api.setHandler(handlers);
@@ -143,7 +144,7 @@ public class GitBridgeServer {
new ServletHolder(
new WLGitServlet(
servletContextHandler,
bridgeAPI
bridge
)
),
"/*"
@@ -152,7 +153,7 @@ public class GitBridgeServer {
}
private Handler initResourceHandler() {
ResourceHandler resourceHandler = new FileHandler(bridgeAPI);
ResourceHandler resourceHandler = new FileHandler(bridge);
resourceHandler.setResourceBase(
new File(rootGitDirectoryPath, ".wlgb/atts").getAbsolutePath()
);
@@ -17,7 +17,7 @@ public class PostbackContents implements JSONSource {
private static final String CODE_SUCCESS = "upToDate";
private final Bridge bridgeAPI;
private final Bridge bridge;
private final String projectName;
private final String postbackKey;
@@ -26,8 +26,8 @@ public class PostbackContents implements JSONSource {
private int versionID;
private SnapshotPostException exception;
public PostbackContents(Bridge bridgeAPI, String projectName, String postbackKey, String contents) {
this.bridgeAPI = bridgeAPI;
public PostbackContents(Bridge bridge, String projectName, String postbackKey, String contents) {
this.bridge = bridge;
this.projectName = projectName;
this.postbackKey = postbackKey;
snapshotPostExceptionBuilder = new SnapshotPostExceptionBuilder();
@@ -43,9 +43,9 @@ public class PostbackContents implements JSONSource {
public void processPostback() throws UnexpectedPostbackException {
if (exception == null) {
bridgeAPI.postbackReceivedSuccessfully(projectName, postbackKey, versionID);
bridge.postbackReceivedSuccessfully(projectName, postbackKey, versionID);
} else {
bridgeAPI.postbackReceivedWithException(projectName, postbackKey, exception);
bridge.postbackReceivedWithException(projectName, postbackKey, exception);
}
}
@@ -19,10 +19,10 @@ import java.io.IOException;
*/
public class PostbackHandler extends AbstractHandler {
private final Bridge bridgeAPI;
private final Bridge bridge;
public PostbackHandler(Bridge bridgeAPI) {
this.bridgeAPI = bridgeAPI;
public PostbackHandler(Bridge bridge) {
this.bridge = bridge;
}
@Override
@@ -39,7 +39,7 @@ public class PostbackHandler extends AbstractHandler {
String projectName = parts[1];
String postbackKey = parts[2];
Log.info(baseRequest.getMethod() + " <- " + baseRequest.getHttpURI());
PostbackContents postbackContents = new PostbackContents(bridgeAPI, projectName, postbackKey, contents);
PostbackContents postbackContents = new PostbackContents(bridge, projectName, postbackKey, contents);
JsonObject body = new JsonObject();
try {