Skip to content

Commit

Permalink
Restructure repository service
Browse files Browse the repository at this point in the history
Adjust how branches and tags are calculated, and improve error logging.

Affects: #28
  • Loading branch information
io7m committed Apr 14, 2024
1 parent 7bb8f08 commit a13796e
Show file tree
Hide file tree
Showing 15 changed files with 170 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
import io.opentelemetry.api.trace.Span;
import org.eclipse.jgit.api.ArchiveCommand;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.ListBranchCommand;
import org.eclipse.jgit.api.VerifySignatureCommand;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.archive.TgzFormat;
Expand All @@ -62,6 +63,8 @@
import org.eclipse.jgit.transport.CredentialsProvider;
import org.eclipse.jgit.transport.TagOpt;
import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.io.OutputStream;
Expand Down Expand Up @@ -108,6 +111,9 @@

public final class NPSCMJGRepository implements NPSCMRepositoryType
{
private static final Logger LOG =
LoggerFactory.getLogger(NPSCMJGRepository.class);

private static final OpenOption[] TEMPORARY_FILE_OPTIONS =
{TRUNCATE_EXISTING, CREATE, WRITE};

Expand Down Expand Up @@ -346,6 +352,7 @@ public NPArchive commitArchive(
);
} catch (final Exception e) {
recordSpanException(e);
LOG.error("commitArchive: ", e);
final var ex = this.handleException(e);
task.onFailed(ex);
throw ex;
Expand Down Expand Up @@ -435,6 +442,7 @@ private NPSCMCommitSet executeCalculateSince(
);
} catch (final Exception e) {
recordSpanException(e);
LOG.error("executeCalculateSince: ", e);
final var ex = this.handleException(e);
task.onFailed(ex);
throw ex;
Expand Down Expand Up @@ -473,14 +481,20 @@ private NPCommit transformCommit(
final var commitObjectId =
ObjectId.fromString(source.name());

// final var branches =
// Set.copyOf(
// this.git.nameRev()
// .addPrefix("refs/heads")
// .add(commitObjectId)
// .call()
// .values()
// .stream()
// .map(s -> s.replace("refs/heads/", ""))
// .collect(Collectors.toSet())
// );

final var branches =
Set.copyOf(
this.git.nameRev()
.addPrefix("refs/heads")
.add(commitObjectId)
.call()
.values()
);
transformGetBranchesForCommit(source);

final var timeCreated =
OffsetDateTime.ofInstant(
Expand All @@ -495,15 +509,8 @@ private NPCommit transformCommit(
final var timeReceived =
OffsetDateTime.now(UTC);

final var tagList =
this.git.tagList()
.setContains(commitObjectId)
.call();

final var tags =
tagList.stream()
.map(Ref::getName)
.collect(Collectors.toUnmodifiableSet());
this.transformGetTagsForCommit(source);

final var author =
new NPCommitAuthor(
Expand All @@ -526,6 +533,39 @@ private NPCommit transformCommit(
);
}

private Set<String> transformGetBranchesForCommit(
final RevCommit source)
throws GitAPIException
{
return this.git.branchList()
.setContains(source.name())
.setListMode(ListBranchCommand.ListMode.ALL)
.call()
.stream()
.map(Ref::getName)
.map(s -> s.replace("refs/heads/", ""))
.collect(Collectors.toSet());
}

private Set<String> transformGetTagsForCommit(
final RevCommit source)
throws GitAPIException
{
final var tagList =
this.git.tagList()
.call();

final var tagNames = new HashSet<String>();
for (final var ref : tagList) {
final var peeledRef = ref.getPeeledObjectId();
if (Objects.equals(peeledRef, source.toObjectId())) {
tagNames.add(ref.getName().replace("refs/tags/", ""));
}
}

return Set.copyOf(tagNames);
}

private void executeUpdate()
throws NPSCMRepositoryException
{
Expand Down Expand Up @@ -560,6 +600,7 @@ private void executeUpdateGC()
task.onCompleted();
} catch (final Exception e) {
recordSpanException(e);
LOG.error("executeUpdateGC: ", e);
final var ex = this.handleException(e);
task.onFailed(ex);
throw ex;
Expand Down Expand Up @@ -589,6 +630,7 @@ private void executeUpdatePull()
task.onCompleted();
} catch (final Exception e) {
recordSpanException(e);
LOG.error("executeUpdatePull: ", e);
final var ex = this.handleException(e);
task.onFailed(ex);
throw ex;
Expand Down Expand Up @@ -643,19 +685,20 @@ private void executeClone()
Git.cloneRepository()
.setBare(true)
.setCloneAllBranches(true)
.setTagOption(TagOpt.FETCH_TAGS)
.setCloneSubmodules(true)
.setCredentialsProvider(this.createCredentialsProvider())
.setGitDir(this.repoPath.toFile())
.setMirror(true)
.setURI(this.repositoryDescription.url().normalize().toString())
.setCredentialsProvider(this.createCredentialsProvider())
.setProgressMonitor(task)
.setCloneSubmodules(true)
.setTagOption(TagOpt.FETCH_TAGS)
.setURI(this.repositoryDescription.url().normalize().toString())
.call()
);

task.onCompleted();
} catch (final Exception e) {
recordSpanException(e);
LOG.error("executeClone: ", e);
final var ex = this.handleException(e);
task.onFailed(ex);
throw ex;
Expand Down Expand Up @@ -756,6 +799,7 @@ public NPFingerprint commitVerifySignature(
return verifier.keyUsed().fingerprint();
} catch (final Exception e) {
recordSpanException(e);
LOG.error("commitVerifySignature: ", e);
final var ex = this.handleException(e);
task.onFailed(ex);
throw ex;
Expand Down Expand Up @@ -811,6 +855,7 @@ private NPSCMCommitSet executeCalculateSinceTime(
);
} catch (final Exception e) {
recordSpanException(e);
LOG.error("executeCalculateSinceTime: ", e);
final var ex = this.handleException(e);
task.onFailed(ex);
throw ex;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
requires com.io7m.northpike.keys;
requires com.io7m.northpike.telemetry.api;

requires com.googlecode.javaewah;
requires com.io7m.jmulticlose.core;
requires com.io7m.lanark.core;
requires io.opentelemetry.api;
Expand Down
14 changes: 6 additions & 8 deletions com.io7m.northpike.server.main/src/main/resources/logback.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,6 @@
<target>System.err</target>
</appender>

<logger name="org.eclipse.jetty.server"
level="ERROR"/>
<logger name="org.eclipse.jetty.util.ssl.SslContextFactory"
level="ERROR"/>
<logger name="org.eclipse.jetty.server.AbstractConnector"
level="ERROR"/>
<logger name="org.eclipse.jetty" level="INFO"/>

<logger name="com.github.dockerjava"
level="ERROR"/>
<logger name="org.testcontainers"
Expand All @@ -27,6 +19,12 @@
<logger name="com.io7m.blackthorne"
level="INFO"/>

<logger name="org.jooq"
level="INFO"/>

<logger name="org.eclipse.jgit.internal.storage.file"
level="INFO"/>

<root level="TRACE">
<appender-ref ref="STDERR"/>
</root>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ private void processCommandWorkItemSent(
private void processCommandLatencyCheck()
throws InterruptedException, NPException, IOException
{
LOG.debug("Latency: Performing check.");
LOG.trace("Latency: Performing check.");

final var command =
new NPACommandSLatencyCheck(
Expand All @@ -328,7 +328,7 @@ private void processCommandLatencyCheck()
final var duration =
Duration.between(command.timeCurrent(), timeNow);

LOG.debug("Latency: RTT {}", duration);
LOG.trace("Latency: RTT {}", duration);
this.metrics.setAgentLatency(this.agentId, duration);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,13 @@ public NPEventSeverity severity()
@Override
public String name()
{
return "ArchiveCreated";
return "RepositoryArchiveCreated";
}

@Override
public String message()
{
return "Created";
return "RepositoryArchiveCreated";
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public String name()
@Override
public String message()
{
return "CloseFailed";
return "RepositoryCloseFailed";
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public String name()
@Override
public String message()
{
return "Closed";
return "RepositoryClosed";
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public String name()
@Override
public String message()
{
return "ConfigureFailed";
return "RepositoryConfigureFailed";
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public String name()
@Override
public String message()
{
return "Configured";
return "RepositoryConfigured";
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,7 @@ private void processCommandTimed(
{
final var span =
this.telemetry.tracer()
.spanBuilder("Command")
.spanBuilder("RepositoryCommand")
.setAttribute("Command", command.getClass().getSimpleName())
.startSpan();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public String name()
@Override
public String message()
{
return "Started";
return "RepositoryServiceStarted";
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public String name()
@Override
public String message()
{
return "UpdateFailed";
return "RepositoryUpdateFailed";
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public String name()
@Override
public String message()
{
return "Updated";
return "RepositoryUpdated";
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ private void runScheduleTask()

private void runSchedule()
{
LOG.debug("Scheduling task starting");
LOG.trace("Scheduling task starting");

final var span =
this.telemetry.tracer()
Expand Down
Loading

0 comments on commit a13796e

Please sign in to comment.