Skip to content

Commit

Permalink
Make BuildArtifactCache an independent service
Browse files Browse the repository at this point in the history
Make BuildArtifactCache an independent IntelliJ project service, so that
it may be invoked in both LegacySync and QuerySync projects.

Bug: 385469770
Bug: 392903689
Bug: 392903190
Test: Existing tests are deleted as am not sure how to test project
service

Change-Id: Id1605d87e596aaac84c9a7d77f5d0bd884761213

AOSP: dcad2a45d63a39ae85d52aaf7d3c8a4ac3af91af
  • Loading branch information
Googler authored and sellophane committed Mar 11, 2025
1 parent be4eb6a commit 7203fd2
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 23 deletions.
10 changes: 2 additions & 8 deletions base/src/com/google/idea/blaze/base/qsync/CacheCleaner.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,10 @@ public class CacheCleaner implements BuildArtifactCache.CleanRequest {
private final Logger logger = Logger.getInstance(CacheCleaner.class);

private final Project project;
private final QuerySyncManager querySyncManager;
private final AtomicReference<AccessToken> activeCleanRequest = new AtomicReference<>();

CacheCleaner(Project project, QuerySyncManager qsm) {
CacheCleaner(Project project) {
this.project = project;
this.querySyncManager = qsm;
}

@Override
Expand Down Expand Up @@ -83,11 +81,7 @@ private void doClean() {
new Task.Backgroundable(project, "Cleaning build cache") {
@Override
public void run(@NotNull ProgressIndicator progressIndicator) {
BuildArtifactCache cache =
querySyncManager
.getLoadedProject()
.map(QuerySyncProject::getBuildArtifactCache)
.orElse(null);
BuildArtifactCache cache = project.getService(BuildArtifactCache.class);
if (cache == null) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;
import com.google.common.util.concurrent.MoreExecutors;
import com.google.idea.blaze.base.qsync.cache.ArtifactFetchers;
import com.google.idea.blaze.common.Context;
import com.google.idea.blaze.common.PrintOutput;
import com.google.idea.blaze.common.artifact.ArtifactFetcher;
import com.google.idea.blaze.common.artifact.OutputArtifact;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Pair;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
Expand All @@ -39,8 +41,8 @@ public class DynamicallyDispatchingArtifactFetcher implements ArtifactFetcher<Ou

private final ImmutableList<ArtifactFetcher<?>> fetchers;

public DynamicallyDispatchingArtifactFetcher(ImmutableList<ArtifactFetcher<?>> fetchers) {
this.fetchers = fetchers;
public DynamicallyDispatchingArtifactFetcher(Project project) {
this.fetchers = ImmutableList.copyOf(ArtifactFetchers.EP_NAME.getExtensionList());
}

@Override
Expand Down
13 changes: 1 addition & 12 deletions base/src/com/google/idea/blaze/base/qsync/ProjectLoaderImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -249,13 +249,7 @@ private QuerySyncProjectDeps instantiateDeps(BlazeContext context) throws BuildE
Registry projectTransformRegistry = new Registry();
SnapshotHolder graph = new SnapshotHolder();
graph.addListener((c, i) -> projectModificationTracker.incModificationCount());
Path buildCachePath = getBuildCachePath(project);
BuildArtifactCache artifactCache =
BuildArtifactCache.create(
buildCachePath,
createArtifactFetcher(),
executor,
QuerySyncManager.getInstance(project).cacheCleanRequest());
BuildArtifactCache artifactCache = project.getService(BuildArtifactCache.class);

DependencyBuilder dependencyBuilder =
createDependencyBuilder(
Expand Down Expand Up @@ -381,11 +375,6 @@ private Path getSnapshotFilePath(BlazeImportSettings importSettings) {
return BlazeDataStorage.getProjectDataDir(importSettings).toPath().resolve("qsyncdata.gz");
}

private ArtifactFetcher<OutputArtifact> createArtifactFetcher() {
return new DynamicallyDispatchingArtifactFetcher(
ImmutableList.copyOf(ArtifactFetchers.EP_NAME.getExtensions()));
}

/**
* Returns an {@link ImmutableSet} of rule kinds that query sync or plugin know how to resolve
* symbols for without building. The rules query sync always builds even if they are part of the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ public QuerySyncManager(Project project, @Nullable ProjectLoader loader) {
this.loader = loader != null ? loader : createProjectLoader(project);
this.syncStatus = new QuerySyncStatus(project);
this.fileListener = QuerySyncAsyncFileListener.createAndListen(project, this);
this.cacheCleaner = new CacheCleaner(project, this);
this.cacheCleaner = new CacheCleaner(project);
}

/**
Expand Down
1 change: 1 addition & 0 deletions querysync/java/com/google/idea/blaze/qsync/deps/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ java_library(
"//shared:proto",
"//shared:vcs",
"//third_party/java/auto_value",
"//intellij_platform_sdk:plugin_api",
"@com_google_guava_guava//jar",
"@protobuf//:protobuf_java",
"@error_prone_annotations//jar",
Expand Down
1 change: 1 addition & 0 deletions shared/java/com/google/idea/blaze/common/artifact/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ java_library(
deps = [
"@jsr305_annotations//jar",
"//shared",
"//intellij_platform_sdk:plugin_api",
"@com_google_guava_guava//jar",
"@error_prone_annotations//jar",
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package com.google.idea.blaze.common.artifact;

import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.collect.ImmutableList.toImmutableList;
import static com.google.common.collect.ImmutableMap.toImmutableMap;
import static com.google.common.collect.ImmutableSet.toImmutableSet;
Expand All @@ -36,14 +37,18 @@
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;
import com.google.common.util.concurrent.ListeningExecutorService;
import com.google.common.util.concurrent.MoreExecutors;
import com.google.idea.blaze.common.Context;
import com.google.idea.blaze.common.PrintOutput;
import com.google.idea.blaze.common.artifact.ArtifactFetcher.ArtifactDestination;
import com.google.idea.blaze.exception.BuildException;
import com.intellij.openapi.project.Project;
import com.intellij.util.concurrency.AppExecutorUtil;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.BasicFileAttributeView;
import java.nio.file.attribute.FileTime;
import java.time.Duration;
Expand Down Expand Up @@ -112,6 +117,15 @@ public class BuildArtifactCacheDirectory implements BuildArtifactCache {
*/
private final StampedLock lock = new StampedLock();

public BuildArtifactCacheDirectory(Project project) throws BuildException {
this(
Paths.get(checkNotNull(project.getBasePath())).resolve(".buildcache"),
project.getService(ArtifactFetcher.class),
MoreExecutors.listeningDecorator(
AppExecutorUtil.createBoundedApplicationPoolExecutor("BuildArtifactCache", 128)),
project.getService(CleanRequest.class));
}

public BuildArtifactCacheDirectory(
Path cacheDir,
ArtifactFetcher<OutputArtifact> fetcher,
Expand Down

0 comments on commit 7203fd2

Please sign in to comment.