[jps cache] Add an opportunity to download caches for the branch

GitOrigin-RevId: 73ab844781ad9ed79d328fbbd296d4244d0d61f1
This commit is contained in:
Mikhail Mazurkevich
2020-06-25 18:55:34 +03:00
committed by intellij-monorepo-bot
parent 5016a60182
commit d6ac511732
7 changed files with 41 additions and 19 deletions
@@ -1,8 +1,14 @@
package com.intellij.jps.cache;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.project.Project;
import com.intellij.util.concurrency.AppExecutorUtil;
import git4idea.repo.GitRepository;
import git4idea.repo.GitRepositoryManager;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.List;
import java.util.concurrent.ExecutorService;
import static com.intellij.execution.process.ProcessIOExecutorService.INSTANCE;
@@ -19,4 +25,13 @@ public class JpsCachesPluginUtil {
LOG.info("Executor service will be configured with " + threadsCount + " threads");
return threadsCount;
}
public static @Nullable String getCurrentGitBranch(@NotNull Project project) {
List<GitRepository> repositories = GitRepositoryManager.getInstance(project).getRepositories();
if (repositories.size() != 1) {
LOG.warn("Unable to detect current branch for the project with the multiple git repositories");
return null;
}
return repositories.get(0).getCurrentBranchName();
}
}
@@ -27,6 +27,7 @@ public class JpsCachesProjectStateListener implements GitRepositoryChangeListene
previousCommitId = currentRevision;
LOG.info("Remote repository commit changed to " + currentRevision);
JpsOutputLoaderManager outputLoaderManager = JpsOutputLoaderManager.getInstance(repository.getProject());
JpsServerAuthExtension.checkAuthenticatedInBackgroundThread(outputLoaderManager, () -> outputLoaderManager.notifyAboutNearestCache());
JpsServerAuthExtension.checkAuthenticatedInBackgroundThread(outputLoaderManager, () ->
outputLoaderManager.notifyAboutNearestCache(branch.getName()));
}
}
@@ -1,5 +1,6 @@
package com.intellij.jps.cache.action;
import com.intellij.jps.cache.JpsCachesPluginUtil;
import com.intellij.jps.cache.client.JpsServerAuthExtension;
import com.intellij.jps.cache.loader.JpsOutputLoaderManager;
import com.intellij.openapi.actionSystem.AnActionEvent;
@@ -12,7 +13,9 @@ public class JpsForceUpdateCachesAction extends DumbAwareAction {
public void actionPerformed(@NotNull AnActionEvent actionEvent) {
Project project = actionEvent.getProject();
if (project == null) return;
String branch = JpsCachesPluginUtil.getCurrentGitBranch(project);
if (branch == null) return;
JpsOutputLoaderManager outputLoaderManager = JpsOutputLoaderManager.getInstance(project);
JpsServerAuthExtension.checkAuthenticatedInBackgroundThread(outputLoaderManager, () -> outputLoaderManager.load(true));
JpsServerAuthExtension.checkAuthenticatedInBackgroundThread(outputLoaderManager, () -> outputLoaderManager.load(true, branch));
}
}
@@ -1,5 +1,6 @@
package com.intellij.jps.cache.action;
import com.intellij.jps.cache.JpsCachesPluginUtil;
import com.intellij.jps.cache.client.JpsServerAuthExtension;
import com.intellij.jps.cache.loader.JpsOutputLoaderManager;
import com.intellij.openapi.actionSystem.AnActionEvent;
@@ -12,7 +13,9 @@ public class JpsUpdateCachesAction extends DumbAwareAction {
public void actionPerformed(@NotNull AnActionEvent actionEvent) {
Project project = actionEvent.getProject();
if (project == null) return;
String branch = JpsCachesPluginUtil.getCurrentGitBranch(project);
if (branch == null) return;
JpsOutputLoaderManager outputLoaderManager = JpsOutputLoaderManager.getInstance(project);
JpsServerAuthExtension.checkAuthenticatedInBackgroundThread(outputLoaderManager, () -> outputLoaderManager.load(false));
JpsServerAuthExtension.checkAuthenticatedInBackgroundThread(outputLoaderManager, () -> outputLoaderManager.load(false, branch));
}
}
@@ -13,7 +13,7 @@ import java.util.Set;
public interface JpsServerClient {
@NotNull
Set<String> getAllCacheKeys(@NotNull Project project);
Set<String> getAllCacheKeys(@NotNull Project project, @NotNull String branchName);
@Nullable
File downloadMetadataById(@NotNull String metadataId, @NotNull File targetDir);
File downloadCacheById(@NotNull SegmentedProgressIndicatorManager downloadIndicatorManager, @NotNull String cacheId,
@@ -43,7 +43,6 @@ public class TemporaryCacheServerClient implements JpsServerClient {
private static final Logger LOG = Logger.getInstance("com.intellij.jps.cache.client.TemporaryCacheServerClient");
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
static final TemporaryCacheServerClient INSTANCE = new TemporaryCacheServerClient();
private static final String REPOSITORY_NAME = "";
private final String stringThree;
private TemporaryCacheServerClient() {
@@ -53,8 +52,8 @@ public class TemporaryCacheServerClient implements JpsServerClient {
@NotNull
@Override
public Set<String> getAllCacheKeys(@NotNull Project project) {
Map<String, List<String>> response = doGetRequest(project, getRequestHeaders());
public Set<String> getAllCacheKeys(@NotNull Project project, @NotNull String branchName) {
Map<String, List<String>> response = doGetRequest(project, branchName, getRequestHeaders());
if (response == null) return Collections.emptySet();
return response.values().stream().flatMap(Collection::stream).collect(Collectors.toSet());
}
@@ -62,7 +61,7 @@ public class TemporaryCacheServerClient implements JpsServerClient {
@Nullable
@Override
public File downloadMetadataById(@NotNull String metadataId, @NotNull File targetDir) {
String downloadUrl = stringThree + REPOSITORY_NAME + "/metadata/" + metadataId;
String downloadUrl = stringThree + "/metadata/" + metadataId;
DownloadableFileService service = DownloadableFileService.getInstance();
String fileName = "metadata.json";
DownloadableFileDescription description = service.createFileDescription(downloadUrl, fileName);
@@ -93,7 +92,7 @@ public class TemporaryCacheServerClient implements JpsServerClient {
@Override
public File downloadCacheById(@NotNull SegmentedProgressIndicatorManager downloadIndicatorManager, @NotNull String cacheId,
@NotNull File targetDir) {
String downloadUrl = stringThree + REPOSITORY_NAME + "/caches/" + cacheId;
String downloadUrl = stringThree + "/caches/" + cacheId;
String fileName = "portable-build-cache.zip";
DownloadableFileService service = DownloadableFileService.getInstance();
DownloadableFileDescription description = service.createFileDescription(downloadUrl, fileName);
@@ -125,7 +124,7 @@ public class TemporaryCacheServerClient implements JpsServerClient {
targetDir.mkdirs();
Map<String, AffectedModule> urlToModuleNameMap = affectedModules.stream().collect(Collectors.toMap(
module -> stringThree + REPOSITORY_NAME + "/" + module.getType() + "/" + module.getName() + "/" + module.getHash(),
module -> stringThree + "/" + module.getType() + "/" + module.getName() + "/" + module.getHash(),
module -> module));
DownloadableFileService service = DownloadableFileService.getInstance();
@@ -155,9 +154,10 @@ public class TemporaryCacheServerClient implements JpsServerClient {
}
}
private Map<String, List<String>> doGetRequest(@NotNull Project project, @NotNull Map<String, String> headers) {
private Map<String, List<String>> doGetRequest(@NotNull Project project, @NotNull String branchName, @NotNull Map<String, String> headers) {
try {
return HttpRequests.request(stringThree + REPOSITORY_NAME + "/commit_history.json")
String urlPrefix = !branchName.equals("master") ? "/" + branchName : "";
return HttpRequests.request(stringThree + urlPrefix + "/commit_history.json")
.tuner(tuner -> headers.forEach((k, v) -> tuner.addRequestProperty(k, v)))
.connect(it -> {
URLConnection connection = it.getConnection();
@@ -83,11 +83,11 @@ public class JpsOutputLoaderManager implements Disposable {
if (!buildManager.isGeneratePortableCachesEnabled()) buildManager.setGeneratePortableCachesEnabled(true);
}
public void load(boolean isForceUpdate) {
public void load(boolean isForceUpdate, @NotNull String branchName) {
Task.Backgroundable task = new Task.Backgroundable(myProject, PROGRESS_TITLE) {
@Override
public void run(@NotNull ProgressIndicator indicator) {
Pair<String, Integer> commitInfo = getNearestCommit(isForceUpdate);
Pair<String, Integer> commitInfo = getNearestCommit(isForceUpdate, branchName);
if (commitInfo != null) {
// Drop JPS metadata to force plugin for downloading all compilation outputs
if (isForceUpdate) myMetadataLoader.dropCurrentProjectMetadata();
@@ -103,8 +103,8 @@ public class JpsOutputLoaderManager implements Disposable {
ProgressManager.getInstance().runProcessWithProgressAsynchronously(task, processIndicator);
}
public void notifyAboutNearestCache() {
Pair<String, Integer> commitInfo = getNearestCommit(false);
public void notifyAboutNearestCache(@NotNull String branchName) {
Pair<String, Integer> commitInfo = getNearestCommit(false, branchName);
if (commitInfo == null) return;
String notificationContent = commitInfo.second == 1
@@ -118,15 +118,15 @@ public class JpsOutputLoaderManager implements Disposable {
.addAction(NotificationAction.createSimple(JpsCacheBundle.messagePointer(
"action.NotificationAction.JpsOutputLoaderManager.text.update.caches"), () -> {
notification.expire();
load(false);
load(false, branchName);
}));
Notifications.Bus.notify(notification, myProject);
});
}
@Nullable
private Pair<String, Integer> getNearestCommit(boolean isForceUpdate) {
Set<String> allCacheKeys = myServerClient.getAllCacheKeys(myProject);
private Pair<String, Integer> getNearestCommit(boolean isForceUpdate, @NotNull String branchName) {
Set<String> allCacheKeys = myServerClient.getAllCacheKeys(myProject, branchName);
String previousCommitId = PropertiesComponent.getInstance().getValue(LATEST_COMMIT_ID);
List<Iterator<String>> repositoryList = GitRepositoryUtil.getCommitsIterator(myProject);