Skip to content
Permalink
Browse files
381: Retry failed materialization of hosted repository storage
Reviewed-by: jvernee
  • Loading branch information
rwestberg committed May 8, 2020
1 parent c967e0a commit eb9b6b1e81be1181182d39b59b1e1aee579307ff
Showing with 27 additions and 20 deletions.
  1. +27 −20 storage/src/main/java/org/openjdk/skara/storage/HostedRepositoryStorage.java
@@ -36,11 +36,10 @@
private final String authorName;
private final String authorEmail;
private final String message;
private final Repository localRepository;
private final StorageSerializer<T> serializer;
private final StorageDeserializer<T> deserializer;
private final Repository localRepository;

private Hash hash;
private RepositoryStorage<T> repositoryStorage;
private Set<T> current;

@@ -54,27 +53,36 @@
this.serializer = serializer;
this.deserializer = deserializer;

try {
Repository localRepository;
localRepository = tryMaterialize(repository, localStorage, ref, fileName, authorName, authorEmail, message);
repositoryStorage = new RepositoryStorage<>(localRepository, fileName, authorName, authorEmail, message, serializer, deserializer);
current = current();
}

private static Repository tryMaterialize(HostedRepository repository, Path localStorage, String ref, String fileName, String authorName, String authorEmail, String message) {
int retryCount = 0;
IOException lastException = null;

while (retryCount < 10) {
try {
localRepository = Repository.materialize(localStorage, repository.url(), "+" + ref + ":storage");
try {
return Repository.materialize(localStorage, repository.url(), "+" + ref + ":storage");
} catch (IOException ignored) {
// The remote ref may not yet exist
Repository localRepository = Repository.init(localStorage, repository.repositoryType());
var storage = Files.writeString(localStorage.resolve(fileName), "");
localRepository.add(storage);
var firstCommit = localRepository.commit(message, authorName, authorEmail);

// If the materialization failed for any other reason than the remote ref not existing, this will fail
localRepository.push(firstCommit, repository.url(), ref);
return localRepository;
}
} catch (IOException e) {
// The remote ref may not yet exist
localRepository = Repository.init(localStorage, repository.repositoryType());
var storage = Files.writeString(localStorage.resolve(fileName), "");
localRepository.add(storage);
var firstCommit = localRepository.commit(message, authorName, authorEmail);

// If the materialization failed for any other reason than the remote ref not existing, this will fail
localRepository.push(firstCommit, repository.url(), ref);
lastException = e;
}
this.localRepository = localRepository;
hash = localRepository.head();
repositoryStorage = new RepositoryStorage<>(localRepository, fileName, authorName, authorEmail, message, serializer, deserializer);
current = current();
} catch (IOException e) {
throw new UncheckedIOException(e);
retryCount++;
}
throw new UncheckedIOException("Retry count exceeded", lastException);
}

@Override
@@ -100,7 +108,6 @@ public void put(Collection<T> items) {
try {
var updatedHash = localRepository.head();
localRepository.push(updatedHash, hostedRepository.url(), ref);
hash = updatedHash;
current = updated;
return;
} catch (IOException e) {

0 comments on commit eb9b6b1

Please sign in to comment.