mirror of
https://github.com/floci-io/floci.git
synced 2026-11-27 07:39:24 -05:00
fix(codeartifact): lease NuGet sidecars through requests (#7438)
This commit is contained in:
1 parent
ae041d690d
commit
d9d2a1edd6
12 files changed
+700
-144
No files matched your search
+15
@@ -79,6 +79,21 @@ public class BagetterSidecarClient implements RepositorySidecarManager {
|
||||
manager.release(repositoryContainerId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void registerRepository(String repositoryContainerId) {
|
||||
manager.registerRepository(repositoryContainerId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void beginUse(String repositoryContainerId) {
|
||||
manager.beginUse(repositoryContainerId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void endUse(String repositoryContainerId) {
|
||||
manager.endUse(repositoryContainerId);
|
||||
}
|
||||
|
||||
/**
|
||||
* NuGet packages have no namespace (AWS: "Python, NuGet, Ruby, and Cargo package versions do
|
||||
* not contain a corresponding component") and exactly one real asset per version, the
|
||||
|
||||
+16
-2
@@ -46,6 +46,7 @@ public class BagetterSidecarManager implements ContainerTeardown {
|
||||
private final ContainerLifecycleManager lifecycleManager;
|
||||
private final EmulatorConfig config;
|
||||
private final PerKeyContainerPool pool;
|
||||
private final SidecarRepositoryLease lease;
|
||||
|
||||
@Inject
|
||||
public BagetterSidecarManager(ContainerBuilder containerBuilder, ContainerLifecycleManager lifecycleManager,
|
||||
@@ -54,6 +55,7 @@ public class BagetterSidecarManager implements ContainerTeardown {
|
||||
this.lifecycleManager = lifecycleManager;
|
||||
this.config = config;
|
||||
this.pool = new PerKeyContainerPool(lifecycleManager, HEALTH_PATH);
|
||||
this.lease = new SidecarRepositoryLease("BaGetter", pool);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -70,7 +72,19 @@ public class BagetterSidecarManager implements ContainerTeardown {
|
||||
|
||||
/** Stops and removes the container for one nuget repository, if one was ever started. */
|
||||
public void release(String nugetRepositoryId) {
|
||||
pool.stopContainer(nugetRepositoryId);
|
||||
lease.release(nugetRepositoryId);
|
||||
}
|
||||
|
||||
public void registerRepository(String nugetRepositoryId) {
|
||||
lease.registerRepository(nugetRepositoryId);
|
||||
}
|
||||
|
||||
public void beginUse(String nugetRepositoryId) {
|
||||
lease.beginUse(nugetRepositoryId);
|
||||
}
|
||||
|
||||
public void endUse(String nugetRepositoryId) {
|
||||
lease.endUse(nugetRepositoryId);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -129,6 +143,6 @@ public class BagetterSidecarManager implements ContainerTeardown {
|
||||
|
||||
@Override
|
||||
public void stopManagedContainers() {
|
||||
pool.stopAll();
|
||||
lease.stopManagedContainers();
|
||||
}
|
||||
}
|
||||
+51
-14
@@ -93,6 +93,14 @@ public class CodeArtifactNuGetController {
|
||||
public Response serviceIndex(@Context HttpHeaders headers, @PathParam("domain") String domain,
|
||||
@PathParam("repository") String repository) {
|
||||
RepositoryHandle handle = requireRepository(headers, domain, repository);
|
||||
try {
|
||||
return serviceIndexWithLease(handle);
|
||||
} finally {
|
||||
endUse(handle.repoId());
|
||||
}
|
||||
}
|
||||
|
||||
private Response serviceIndexWithLease(RepositoryHandle handle) {
|
||||
// domain and repository are free to embed here unescaped: requireRepository above only
|
||||
// returns once CodeArtifactService has confirmed a repository by this exact name already
|
||||
// exists, which it could only do if that name already passed CreateRepository's own
|
||||
@@ -115,9 +123,13 @@ public class CodeArtifactNuGetController {
|
||||
@Produces(MediaType.APPLICATION_JSON)
|
||||
public Response packageVersions(@Context HttpHeaders headers, @PathParam("domain") String domain,
|
||||
@PathParam("repository") String repository, @PathParam("id") String id) {
|
||||
String baseUrl = requireRepository(headers, domain, repository).baseUrl();
|
||||
return proxyGet(baseUrl, "/v3/package/" + SidecarUriUtils.encodeSegment(id) + "/index.json",
|
||||
MediaType.APPLICATION_JSON);
|
||||
RepositoryHandle handle = requireRepository(headers, domain, repository);
|
||||
try {
|
||||
return proxyGet(handle.baseUrl(), "/v3/package/" + SidecarUriUtils.encodeSegment(id) + "/index.json",
|
||||
MediaType.APPLICATION_JSON);
|
||||
} finally {
|
||||
endUse(handle.repoId());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -136,6 +148,15 @@ public class CodeArtifactNuGetController {
|
||||
@PathParam("repository") String repository, @PathParam("id") String id,
|
||||
@PathParam("version") String version, @PathParam("filename") String filename) {
|
||||
RepositoryHandle handle = requireRepository(headers, domain, repository);
|
||||
try {
|
||||
return packageContentWithLease(handle, domain, repository, id, version, filename);
|
||||
} finally {
|
||||
endUse(handle.repoId());
|
||||
}
|
||||
}
|
||||
|
||||
private Response packageContentWithLease(RepositoryHandle handle, String domain, String repository, String id,
|
||||
String version, String filename) {
|
||||
try {
|
||||
service.requireContainerBackedVersionDownloadable(handle.owner(), handle.region(), domain, repository,
|
||||
"nuget", null, id, version, handle.repoId());
|
||||
@@ -197,6 +218,15 @@ public class CodeArtifactNuGetController {
|
||||
}
|
||||
RepositoryHandle handle = requireRepository(headers, domain, repository);
|
||||
|
||||
try {
|
||||
return pushWithLease(headers, domain, repository, content, handle);
|
||||
} finally {
|
||||
endUse(handle.repoId());
|
||||
}
|
||||
}
|
||||
|
||||
private Response pushWithLease(HttpHeaders headers, String domain, String repository, byte[] content,
|
||||
RepositoryHandle handle) {
|
||||
String contentType = headers.getHeaderString(HttpHeaders.CONTENT_TYPE);
|
||||
String boundary = MultipartFormParser.extractBoundary(contentType).orElse(null);
|
||||
if (boundary == null) {
|
||||
@@ -282,16 +312,8 @@ public class CodeArtifactNuGetController {
|
||||
|| response.getStatus() == 202;
|
||||
}
|
||||
if (pushSucceeded) {
|
||||
service.recordContainerBackedAssetBytes(handle.region(), domain, handle.owner(), repository,
|
||||
"nuget", null, packageName, nupkgBytes.length, fenceSnapshot);
|
||||
// Found by Qodo review: a real dotnet nuget push that republishes a coordinate an
|
||||
// earlier updatePackageVersionsStatus/disposePackageVersions call left Archived or
|
||||
// Disposed would otherwise still have its fresh content blocked by that stale
|
||||
// override, the same gap closed for Maven/npm/pypi's own native publish paths. Cleared
|
||||
// only if still unchanged since the guard above ran (Greptile review): a status change
|
||||
// that landed while forwardPush was in flight must not be erased by this.
|
||||
service.forgetContainerBackedStatusOverrideIfUnchanged(handle.region(), domain, handle.owner(),
|
||||
repository, "nuget", null, packageName, version, beforeRevision);
|
||||
service.completeNugetPushIfCurrentRepository(handle.region(), domain, handle.owner(), repository,
|
||||
handle.repoId(), packageName, version, nupkgBytes.length, fenceSnapshot, beforeRevision);
|
||||
}
|
||||
return response;
|
||||
}
|
||||
@@ -370,10 +392,25 @@ public class CodeArtifactNuGetController {
|
||||
.orElseThrow(() -> new NotAuthorizedException("Basic realm=\"floci-codeartifact\""));
|
||||
String repoId;
|
||||
try {
|
||||
repoId = service.ensureFormatContainerId("nuget", scope.region(), domain, scope.owner(), repository);
|
||||
repoId = service.resolveContainerIdAndBeginUse("nuget", scope.region(), domain, scope.owner(),
|
||||
repository, bagetter);
|
||||
} catch (AwsException e) {
|
||||
throw new NotFoundException();
|
||||
}
|
||||
try {
|
||||
return readyRepository(scope, domain, repository, repoId);
|
||||
} catch (RuntimeException e) {
|
||||
endUse(repoId);
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
private void endUse(String repoId) {
|
||||
bagetter.endUse(repoId);
|
||||
}
|
||||
|
||||
private RepositoryHandle readyRepository(AuthorizationTokenScope scope, String domain, String repository,
|
||||
String repoId) {
|
||||
String baseUrl = bagetter.ensureReady(repoId, null);
|
||||
// The real GetRepositoryEndpoint value, not just this controller's own path: a NuGet
|
||||
// client's own service-index parser only keeps a resource whose @id parses as an
|
||||
|
||||
+2
-2
@@ -149,8 +149,8 @@ public class CodeArtifactNuGetExternalConnectionImporter {
|
||||
// this bookkeeping run instead of leaving the claim held (or released) without ever
|
||||
// recording what this attempt actually wrote.
|
||||
if (imported) {
|
||||
service.ensureRetainedPackageOriginDefault(region, domain, owner, repository, "nuget", null,
|
||||
packageId, packageFenceSnapshot);
|
||||
service.ensureRetainedPackageOriginDefaultIfCurrentRepository(region, domain, owner, repository,
|
||||
"nuget", null, packageId, packageFenceSnapshot, repoId);
|
||||
}
|
||||
// Matches the claim taken above, exactly once, regardless of what this attempt actually
|
||||
// wrote: see finishRetainedVersionOriginAttempt's own javadoc for why this must run even
|
||||
|
||||
+71
-17
@@ -707,6 +707,10 @@ public class CodeArtifactService implements Resettable {
|
||||
return id;
|
||||
}
|
||||
|
||||
private void endSidecarUse(RepositorySidecarManager manager, String repoId) {
|
||||
manager.endUse(repoId);
|
||||
}
|
||||
|
||||
public synchronized CodeArtifactRepository updateRepository(String region, String domain, String domainOwner,
|
||||
String repository, String description,
|
||||
List<String> upstreams) {
|
||||
@@ -1101,7 +1105,7 @@ public class CodeArtifactService implements Resettable {
|
||||
revision = currentContainerBackedVersionStatus(owner, region, domain, repository, format, namespace,
|
||||
packageName, version, repoId).revision();
|
||||
} finally {
|
||||
manager.endUse(repoId);
|
||||
endSidecarUse(manager, repoId);
|
||||
}
|
||||
} else {
|
||||
String key = packageVersionKey(region, domain, repository, format, namespace, packageName, version);
|
||||
@@ -1212,7 +1216,7 @@ public class CodeArtifactService implements Resettable {
|
||||
preferredDisplayVersion = manager.preferredDisplayVersion(repoId, domain, repository, namespace,
|
||||
packageName).orElse(null);
|
||||
} finally {
|
||||
manager.endUse(repoId);
|
||||
endSidecarUse(manager, repoId);
|
||||
}
|
||||
Set<String> coveredVersions = raw.stream().map(RepositorySidecarManager.PackageVersionTimestamp::version)
|
||||
.collect(Collectors.toSet());
|
||||
@@ -2549,7 +2553,7 @@ public class CodeArtifactService implements Resettable {
|
||||
version));
|
||||
return pv;
|
||||
} finally {
|
||||
manager.endUse(repoId);
|
||||
endSidecarUse(manager, repoId);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2658,7 +2662,7 @@ public class CodeArtifactService implements Resettable {
|
||||
throw new AwsException("InternalServerException", "Listing " + format + " packages is not yet "
|
||||
+ "supported by this emulator.", 500);
|
||||
} finally {
|
||||
manager.endUse(repoId);
|
||||
endSidecarUse(manager, repoId);
|
||||
}
|
||||
// Raised by Greptile's review: disposing a container-backed package's last version
|
||||
// deletes its real sidecar content, so the sidecar's own listing above no longer names
|
||||
@@ -2949,8 +2953,9 @@ public class CodeArtifactService implements Resettable {
|
||||
* #recordContainerBackedAssetBytes} already does: {@code DeleteRepository} removes a
|
||||
* repository's {@code packageOriginConfigs} entries directly rather than through this fence,
|
||||
* so the package-level fence check below cannot by itself catch an import finishing after
|
||||
* the repository was deleted. Without this, a repository recreated under the same name would
|
||||
* see a stale default written back for a package it never had any version of.
|
||||
* the repository was deleted and remains absent. A sidecar importer also checks the original
|
||||
* container id through {@link #ensureRetainedPackageOriginDefaultIfCurrentRepository}, since
|
||||
* an existence check alone cannot distinguish a same-name replacement.
|
||||
*/
|
||||
public synchronized void ensureRetainedPackageOriginDefault(String region, String domain, String domainOwner,
|
||||
String repository, String format, String namespace, String packageName, long packageFenceSnapshot) {
|
||||
@@ -2983,6 +2988,21 @@ public class CodeArtifactService implements Resettable {
|
||||
new PackageOriginConfig("BLOCK", "ALLOW", format, namespace, packageName));
|
||||
}
|
||||
|
||||
/** Checks the repository generation and writes the default under the same deletion monitor. */
|
||||
public synchronized void ensureRetainedPackageOriginDefaultIfCurrentRepository(String region, String domain,
|
||||
String domainOwner, String repository, String format, String namespace, String packageName,
|
||||
long packageFenceSnapshot, String repositoryContainerId) {
|
||||
String owner = effectiveOwner(domainOwner);
|
||||
CodeArtifactRepository current = repositories.getForAccount(owner, repositoryKey(region, domain, repository))
|
||||
.orElse(null);
|
||||
if (current == null || repositoryContainerId == null || !repositoryContainerId.equals(
|
||||
current.getSidecarContainerIds().get(format))) {
|
||||
return;
|
||||
}
|
||||
ensureRetainedPackageOriginDefault(region, domain, owner, repository, format, namespace, packageName,
|
||||
packageFenceSnapshot);
|
||||
}
|
||||
|
||||
/**
|
||||
* The {@code public:maven-*} external connection name a Maven package version was genuinely
|
||||
* retained from, or {@link Optional#empty()} if it has never been retained at all (either
|
||||
@@ -3257,11 +3277,18 @@ public class CodeArtifactService implements Resettable {
|
||||
private boolean retentionWriteStillAllowed(String region, String domain, String domainOwner, String repository,
|
||||
String format, String namespace, String packageName, String version, String repoId,
|
||||
long versionFenceSnapshot, long episodeId) {
|
||||
String owner = effectiveOwner(domainOwner);
|
||||
if ("nuget".equals(format)) {
|
||||
CodeArtifactRepository current = repositories.getForAccount(owner, repositoryKey(region, domain,
|
||||
repository)).orElse(null);
|
||||
if (current == null || !Objects.equals(repoId, current.getSidecarContainerIds().get(format))) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
if (currentContainerBackedVersionFence(region, domain, domainOwner, repository, format, namespace,
|
||||
packageName, version) != versionFenceSnapshot) {
|
||||
return false;
|
||||
}
|
||||
String owner = effectiveOwner(domainOwner);
|
||||
String status = currentContainerBackedVersionStatus(owner, region, domain, repository, format, namespace,
|
||||
packageName, version, repoId).status();
|
||||
if ("Disposed".equals(status) || "Archived".equals(status)) {
|
||||
@@ -3395,7 +3422,7 @@ public class CodeArtifactService implements Resettable {
|
||||
try {
|
||||
return manager.packageExists(repoId, domain, repository, namespace, packageName);
|
||||
} finally {
|
||||
manager.endUse(repoId);
|
||||
endSidecarUse(manager, repoId);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3687,7 +3714,7 @@ public class CodeArtifactService implements Resettable {
|
||||
asset.setHashes(computeHashes(content));
|
||||
return new PackageVersionAssetResult(asset, revision);
|
||||
} finally {
|
||||
manager.endUse(repoId);
|
||||
endSidecarUse(manager, repoId);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4502,17 +4529,25 @@ public class CodeArtifactService implements Resettable {
|
||||
validatePackageCoordinates(format, namespace, packageName, "reading the readme of");
|
||||
String owner = effectiveOwner(domainOwner);
|
||||
requireRepository(owner, repositoryKey(region, domain, repository), repository);
|
||||
if ("nuget".equals(format)) {
|
||||
RepositorySidecarManager manager = sidecarRegistry.forFormat(format)
|
||||
.orElseThrow(() -> notFound("Package version '" + version + "' of package '" + packageName
|
||||
+ "' was not found.", version, "package-version"));
|
||||
String repoId = resolveContainerIdAndBeginUse(format, region, domain, owner, repository, manager);
|
||||
try {
|
||||
String revision = currentContainerBackedVersionStatus(owner, region, domain, repository, format,
|
||||
namespace, packageName, version, repoId).revision();
|
||||
String readme = manager.fetchPackageVersionReadme(repoId, domain, repository, namespace, packageName,
|
||||
version).orElseThrow(() -> notFound("Package version '" + version + "' of package '"
|
||||
+ packageName + "' was not found.", version, "package-version"));
|
||||
return new PackageVersionReadmeResult(format, namespace, packageName, version, revision, readme);
|
||||
} finally {
|
||||
endSidecarUse(manager, repoId);
|
||||
}
|
||||
}
|
||||
String repoId = ensureFormatContainerId(format, region, domain, owner, repository);
|
||||
String revision = currentContainerBackedVersionStatus(owner, region, domain, repository, format, namespace,
|
||||
packageName, version, repoId).revision();
|
||||
if ("nuget".equals(format)) {
|
||||
String readme = sidecarRegistry.forFormat(format)
|
||||
.flatMap(manager -> manager.fetchPackageVersionReadme(repoId, domain, repository, namespace,
|
||||
packageName, version))
|
||||
.orElseThrow(() -> notFound("Package version '" + version + "' of package '" + packageName
|
||||
+ "' was not found.", version, "package-version"));
|
||||
return new PackageVersionReadmeResult(format, namespace, packageName, version, revision, readme);
|
||||
}
|
||||
// npm and pypi: neither can be answered live (see this method's own javadoc), so a
|
||||
// version that was never captured here means it was never published.
|
||||
String key = packageVersionKey(region, domain, repository, format, namespace,
|
||||
@@ -4931,6 +4966,25 @@ public class CodeArtifactService implements Resettable {
|
||||
containerBackedAssetBytes.put(ownerScopedKey, existing + bytes);
|
||||
}
|
||||
|
||||
/** Completes a NuGet push only while its leased sidecar still belongs to the same repository creation. */
|
||||
public synchronized void completeNugetPushIfCurrentRepository(String region, String domain, String domainOwner,
|
||||
String repository, String repositoryContainerId, String packageName, String version, long bytes,
|
||||
long packageFenceSnapshot, String expectedRevisionOrNull) {
|
||||
String owner = effectiveOwner(domainOwner);
|
||||
CodeArtifactRepository current = repositories.getForAccount(owner, repositoryKey(region, domain, repository))
|
||||
.orElse(null);
|
||||
if (current == null || !Objects.equals(repositoryContainerId,
|
||||
current.getSidecarContainerIds().get("nuget"))) {
|
||||
LOG.warnv("Skipping NuGet publish bookkeeping for {0}/{1}: the repository was replaced while its "
|
||||
+ "sidecar request was in flight.", domain, repository);
|
||||
return;
|
||||
}
|
||||
recordContainerBackedAssetBytes(region, domain, owner, repository, "nuget", null, packageName, bytes,
|
||||
packageFenceSnapshot);
|
||||
forgetContainerBackedStatusOverrideIfUnchanged(region, domain, owner, repository, "nuget", null,
|
||||
packageName, version, expectedRevisionOrNull);
|
||||
}
|
||||
|
||||
/**
|
||||
* Cleans up this service's own byte accounting, deletion fence, and captured readmes for one
|
||||
* container-backed package after something other than {@link #deletePackage} removed it
|
||||
|
||||
+129
@@ -0,0 +1,129 @@
|
||||
package io.github.hectorvent.floci.services.codeartifact;
|
||||
|
||||
import io.github.hectorvent.floci.core.common.docker.PerKeyContainerPool;
|
||||
import org.jboss.logging.Logger;
|
||||
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
/** Keeps a repository's sidecar alive until every admitted use has completed. */
|
||||
final class SidecarRepositoryLease {
|
||||
|
||||
private static final Logger LOG = Logger.getLogger(SidecarRepositoryLease.class);
|
||||
|
||||
private final String sidecarName;
|
||||
private final PerKeyContainerPool pool;
|
||||
private final ConcurrentHashMap<String, UseState> uses = new ConcurrentHashMap<>();
|
||||
|
||||
private static final class UseState {
|
||||
private int active;
|
||||
private boolean releasing;
|
||||
private boolean stopping;
|
||||
private boolean stopped;
|
||||
}
|
||||
|
||||
SidecarRepositoryLease(String sidecarName, PerKeyContainerPool pool) {
|
||||
this.sidecarName = sidecarName;
|
||||
this.pool = pool;
|
||||
}
|
||||
|
||||
void registerRepository(String repositoryId) {
|
||||
uses.putIfAbsent(repositoryId, new UseState());
|
||||
}
|
||||
|
||||
void beginUse(String repositoryId) {
|
||||
UseState use = uses.get(repositoryId);
|
||||
if (use == null) {
|
||||
throw new IllegalStateException("No " + sidecarName + " repository lease for " + repositoryId);
|
||||
}
|
||||
synchronized (use) {
|
||||
if (use.releasing) {
|
||||
throw new IllegalStateException(sidecarName + " sidecar is being released for repository "
|
||||
+ repositoryId);
|
||||
}
|
||||
use.active++;
|
||||
}
|
||||
}
|
||||
|
||||
void endUse(String repositoryId) {
|
||||
UseState use = uses.get(repositoryId);
|
||||
if (use == null) {
|
||||
throw new IllegalStateException("No " + sidecarName + " sidecar use for repository " + repositoryId);
|
||||
}
|
||||
synchronized (use) {
|
||||
if (use.active == 0) {
|
||||
throw new IllegalStateException("No active " + sidecarName + " sidecar use for repository "
|
||||
+ repositoryId);
|
||||
}
|
||||
use.active--;
|
||||
}
|
||||
try {
|
||||
stopIfUnused(repositoryId, use);
|
||||
} catch (RuntimeException e) {
|
||||
LOG.warnv(e, "Could not stop {0} sidecar for repository {1} after its last use; "
|
||||
+ "teardown can retry cleanup.", sidecarName, repositoryId);
|
||||
}
|
||||
}
|
||||
|
||||
void release(String repositoryId) {
|
||||
UseState use = uses.computeIfAbsent(repositoryId, key -> new UseState());
|
||||
synchronized (use) {
|
||||
use.releasing = true;
|
||||
}
|
||||
stopIfUnused(repositoryId, use);
|
||||
}
|
||||
|
||||
private void stopIfUnused(String repositoryId, UseState use) {
|
||||
synchronized (use) {
|
||||
if (!use.releasing || use.active != 0 || use.stopping || use.stopped) {
|
||||
return;
|
||||
}
|
||||
use.stopping = true;
|
||||
}
|
||||
boolean stopped = false;
|
||||
try {
|
||||
if (pool.isTracked(repositoryId)) {
|
||||
pool.stopContainerStrict(repositoryId);
|
||||
}
|
||||
stopped = true;
|
||||
} finally {
|
||||
synchronized (use) {
|
||||
use.stopped = stopped;
|
||||
use.stopping = false;
|
||||
if (stopped) {
|
||||
uses.remove(repositoryId, use);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void stopManagedContainers() {
|
||||
RuntimeException[] failure = new RuntimeException[1];
|
||||
uses.forEach((id, use) -> {
|
||||
synchronized (use) {
|
||||
use.releasing = true;
|
||||
}
|
||||
try {
|
||||
stopIfUnused(id, use);
|
||||
} catch (RuntimeException e) {
|
||||
if (failure[0] == null) {
|
||||
failure[0] = e;
|
||||
} else {
|
||||
failure[0].addSuppressed(e);
|
||||
}
|
||||
}
|
||||
});
|
||||
try {
|
||||
pool.stopAllStrictExcept(Set.copyOf(uses.keySet()));
|
||||
} catch (RuntimeException e) {
|
||||
if (failure[0] == null) {
|
||||
failure[0] = e;
|
||||
} else {
|
||||
failure[0].addSuppressed(e);
|
||||
}
|
||||
}
|
||||
if (failure[0] != null) {
|
||||
throw failure[0];
|
||||
}
|
||||
}
|
||||
}
|
||||
+7
-90
@@ -15,8 +15,6 @@ import jakarta.inject.Inject;
|
||||
import org.jboss.logging.Logger;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
/**
|
||||
* Starts and stops one Verdaccio container per CodeArtifact npm repository, unlike Reposilite's one
|
||||
@@ -60,14 +58,7 @@ public class VerdaccioSidecarManager implements ContainerTeardown {
|
||||
private final ContainerLifecycleManager lifecycleManager;
|
||||
private final EmulatorConfig config;
|
||||
private final PerKeyContainerPool pool;
|
||||
private final ConcurrentHashMap<String, UseState> uses = new ConcurrentHashMap<>();
|
||||
|
||||
private static final class UseState {
|
||||
private int active;
|
||||
private boolean releasing;
|
||||
private boolean stopping;
|
||||
private boolean stopped;
|
||||
}
|
||||
private final SidecarRepositoryLease lease;
|
||||
|
||||
@Inject
|
||||
public VerdaccioSidecarManager(ContainerBuilder containerBuilder, ContainerLifecycleManager lifecycleManager,
|
||||
@@ -76,6 +67,7 @@ public class VerdaccioSidecarManager implements ContainerTeardown {
|
||||
this.lifecycleManager = lifecycleManager;
|
||||
this.config = config;
|
||||
this.pool = new PerKeyContainerPool(lifecycleManager, HEALTH_PATH);
|
||||
this.lease = new SidecarRepositoryLease("Verdaccio", pool);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -89,71 +81,22 @@ public class VerdaccioSidecarManager implements ContainerTeardown {
|
||||
|
||||
/** Stops and removes the container for one npm repository, if one was ever started. */
|
||||
public void release(String npmRepositoryId) {
|
||||
UseState use = uses.computeIfAbsent(npmRepositoryId, key -> new UseState());
|
||||
synchronized (use) {
|
||||
use.releasing = true;
|
||||
}
|
||||
stopIfUnused(npmRepositoryId, use);
|
||||
lease.release(npmRepositoryId);
|
||||
}
|
||||
|
||||
/** Only a repository resolved under CodeArtifactService's deletion lock may admit an id. */
|
||||
public void registerRepository(String npmRepositoryId) {
|
||||
uses.putIfAbsent(npmRepositoryId, new UseState());
|
||||
lease.registerRepository(npmRepositoryId);
|
||||
}
|
||||
|
||||
/** Registers a call that may use this repository's sidecar before it touches the container. */
|
||||
public void beginUse(String npmRepositoryId) {
|
||||
UseState use = uses.get(npmRepositoryId);
|
||||
if (use == null) {
|
||||
throw new IllegalStateException("No Verdaccio repository lease for " + npmRepositoryId);
|
||||
}
|
||||
synchronized (use) {
|
||||
if (use.releasing) {
|
||||
throw new IllegalStateException("Verdaccio sidecar is being released for repository "
|
||||
+ npmRepositoryId);
|
||||
}
|
||||
use.active++;
|
||||
}
|
||||
lease.beginUse(npmRepositoryId);
|
||||
}
|
||||
|
||||
/** Completes a prior {@link #beginUse}, stopping a released container after its last use. */
|
||||
public void endUse(String npmRepositoryId) {
|
||||
UseState use = uses.get(npmRepositoryId);
|
||||
if (use == null) {
|
||||
throw new IllegalStateException("No Verdaccio sidecar use for repository " + npmRepositoryId);
|
||||
}
|
||||
synchronized (use) {
|
||||
if (use.active == 0) {
|
||||
throw new IllegalStateException("No active Verdaccio sidecar use for repository "
|
||||
+ npmRepositoryId);
|
||||
}
|
||||
use.active--;
|
||||
}
|
||||
stopIfUnused(npmRepositoryId, use);
|
||||
}
|
||||
|
||||
private void stopIfUnused(String npmRepositoryId, UseState use) {
|
||||
synchronized (use) {
|
||||
if (!use.releasing || use.active != 0 || use.stopping || use.stopped) {
|
||||
return;
|
||||
}
|
||||
use.stopping = true;
|
||||
}
|
||||
boolean stopped = false;
|
||||
try {
|
||||
if (pool.isTracked(npmRepositoryId)) {
|
||||
pool.stopContainerStrict(npmRepositoryId);
|
||||
}
|
||||
stopped = true;
|
||||
} finally {
|
||||
synchronized (use) {
|
||||
use.stopped = stopped;
|
||||
use.stopping = false;
|
||||
if (stopped) {
|
||||
uses.remove(npmRepositoryId, use);
|
||||
}
|
||||
}
|
||||
}
|
||||
lease.endUse(npmRepositoryId);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -214,32 +157,6 @@ public class VerdaccioSidecarManager implements ContainerTeardown {
|
||||
|
||||
@Override
|
||||
public void stopManagedContainers() {
|
||||
RuntimeException[] failure = new RuntimeException[1];
|
||||
uses.forEach((id, use) -> {
|
||||
synchronized (use) {
|
||||
use.releasing = true;
|
||||
}
|
||||
try {
|
||||
stopIfUnused(id, use);
|
||||
} catch (RuntimeException e) {
|
||||
if (failure[0] == null) {
|
||||
failure[0] = e;
|
||||
} else {
|
||||
failure[0].addSuppressed(e);
|
||||
}
|
||||
}
|
||||
});
|
||||
try {
|
||||
pool.stopAllStrictExcept(Set.copyOf(uses.keySet()));
|
||||
} catch (RuntimeException e) {
|
||||
if (failure[0] == null) {
|
||||
failure[0] = e;
|
||||
} else {
|
||||
failure[0].addSuppressed(e);
|
||||
}
|
||||
}
|
||||
if (failure[0] != null) {
|
||||
throw failure[0];
|
||||
}
|
||||
lease.stopManagedContainers();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user