Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Track operation type in QuerySyncStatus #5833

Open
wants to merge 1 commit into
base: google
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 58 additions & 22 deletions base/src/com/google/idea/blaze/base/qsync/QuerySyncManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,6 @@
import com.google.idea.blaze.base.scope.scopes.ToolWindowScope;
import com.google.idea.blaze.base.settings.BlazeUserSettings;
import com.google.idea.blaze.base.settings.BlazeUserSettings.FocusBehavior;
import com.google.idea.blaze.base.sync.SyncMode;
import com.google.idea.blaze.base.sync.SyncResult;
import com.google.idea.blaze.base.sync.status.BlazeSyncStatus;
import com.google.idea.blaze.base.targetmaps.SourceToTargetMap;
import com.google.idea.blaze.base.toolwindow.Task;
import com.google.idea.blaze.base.util.SaveUtil;
Expand Down Expand Up @@ -87,6 +84,13 @@
* </ul>
*/
public class QuerySyncManager implements Disposable {

/** Type of an operation performed by the sync manager */
public enum Operation {
SYNC,
BUILD
}

private final Logger logger = Logger.getInstance(getClass());

private final Project project;
Expand All @@ -97,6 +101,8 @@ public class QuerySyncManager implements Disposable {
private final ProjectLoader loader;
private volatile QuerySyncProject loadedProject;

private final QuerySyncStatus syncStatus;

private static final BoolExperiment showWindowOnAutomaticSyncErrors =
new BoolExperiment("querysync.autosync.show.console.on.error", true);

Expand All @@ -120,16 +126,16 @@ public static QuerySyncManager getInstance(Project project) {
}

public QuerySyncManager(Project project) {
this.project = project;
this.loader = createProjectLoader(executor, project);
VirtualFileManager.getInstance()
.addAsyncFileListener(new QuerySyncAsyncFileListener(project, this), this);
this(project, null);
}

@NonInjectable
public QuerySyncManager(Project project, ProjectLoader loader) {
public QuerySyncManager(Project project, @Nullable ProjectLoader loader) {
this.project = project;
this.loader = loader;
this.loader = loader != null ? loader : createProjectLoader(executor, project);
this.syncStatus = new QuerySyncStatus(project);
VirtualFileManager.getInstance()
.addAsyncFileListener(new QuerySyncAsyncFileListener(project, this), this);
}

/**
Expand All @@ -147,7 +153,7 @@ protected ProjectLoader createProjectLoader(ListeningExecutorService executor, P
@CanIgnoreReturnValue
public ListenableFuture<Boolean> reloadProject(
QuerySyncActionStatsScope querySyncActionStats, TaskOrigin taskOrigin) {
return run(
return runSync(
"Loading project",
"Re-loading project",
querySyncActionStats,
Expand Down Expand Up @@ -204,7 +210,7 @@ public SourceToTargetMap getSourceToTargetMap() {

@CanIgnoreReturnValue
public ListenableFuture<Boolean> onStartup(QuerySyncActionStatsScope querySyncActionStats) {
return run(
return runSync(
"Loading project",
"Initializing project structure",
querySyncActionStats,
Expand All @@ -215,7 +221,7 @@ public ListenableFuture<Boolean> onStartup(QuerySyncActionStatsScope querySyncAc
@CanIgnoreReturnValue
public ListenableFuture<Boolean> fullSync(
QuerySyncActionStatsScope querySyncActionStats, TaskOrigin taskOrigin) {
return run(
return runSync(
"Updating project structure",
"Re-importing project",
querySyncActionStats,
Expand All @@ -236,7 +242,7 @@ public ListenableFuture<Boolean> fullSync(
public ListenableFuture<Boolean> deltaSync(
QuerySyncActionStatsScope querySyncActionStats, TaskOrigin taskOrigin) {
assertProjectLoaded();
return run(
return runSync(
"Updating project structure",
"Refreshing project",
querySyncActionStats,
Expand All @@ -251,30 +257,52 @@ public ListenableFuture<Boolean> deltaSync(
taskOrigin);
}

private ListenableFuture<Boolean> run(
private ListenableFuture<Boolean> runSync(
String title,
String subTitle,
QuerySyncActionStatsScope querySyncActionStatsScope,
ThrowingScopedOperation operation,
TaskOrigin taskOrigin) {
return run(title, subTitle, querySyncActionStatsScope, operation, taskOrigin, Operation.SYNC);
}

private ListenableFuture<Boolean> runBuild(
String title,
String subTitle,
QuerySyncActionStatsScope querySyncActionStatsScope,
ThrowingScopedOperation operation,
TaskOrigin taskOrigin) {
return run(title, subTitle, querySyncActionStatsScope, operation, taskOrigin, Operation.BUILD);
}

private ListenableFuture<Boolean> run(
String title,
String subTitle,
QuerySyncActionStatsScope querySyncActionStatsScope,
ThrowingScopedOperation operation,
TaskOrigin taskOrigin,
Operation operationType) {
SettableFuture<Boolean> result = SettableFuture.create();
BlazeSyncStatus syncStatus = BlazeSyncStatus.getInstance(project);
syncStatus.syncStarted();
syncStatus.operationStarted(operationType);
Futures.addCallback(
result,
new FutureCallback<Boolean>() {
@Override
public void onSuccess(Boolean success) {
syncStatus.syncEnded(SyncMode.FULL, success ? SyncResult.SUCCESS : SyncResult.FAILURE);
if (success) {
syncStatus.operationEnded();
} else {
syncStatus.operationFailed();
}
}

@Override
public void onFailure(Throwable throwable) {
if (result.isCancelled()) {
syncStatus.syncEnded(SyncMode.FULL, SyncResult.CANCELLED);
syncStatus.operationCancelled();
} else {
syncStatus.operationFailed();
logger.error("Sync failed", throwable);
syncStatus.syncEnded(SyncMode.FULL, SyncResult.FAILURE);
}
}
},
Expand Down Expand Up @@ -369,7 +397,7 @@ public TargetsToBuild getTargetsToBuild(Path workspaceRelativePath) {
public ListenableFuture<Boolean> enableAnalysis(
Set<Label> targets, QuerySyncActionStatsScope querySyncActionStats, TaskOrigin taskOrigin) {
assertProjectLoaded();
return run(
return runBuild(
"Building dependencies",
"Building...",
querySyncActionStats,
Expand All @@ -381,7 +409,7 @@ public ListenableFuture<Boolean> enableAnalysis(
public ListenableFuture<Boolean> enableAnalysisForReverseDeps(
Set<Label> targets, QuerySyncActionStatsScope querySyncActionStats, TaskOrigin taskOrigin) {
assertProjectLoaded();
return run(
return runBuild(
"Building dependencies for affected targets",
"Building...",
querySyncActionStats,
Expand All @@ -397,11 +425,19 @@ public boolean canEnableAnalysisFor(Path workspaceRelativePath) {
return loadedProject.canEnableAnalysisFor(workspaceRelativePath);
}

public boolean syncInProgress() {
return syncStatus.syncInProgress();
}

public boolean buildInProgress() {
return syncStatus.buildInProgress();
}

@CanIgnoreReturnValue
public ListenableFuture<Boolean> generateRenderJar(
PsiFile psiFile, QuerySyncActionStatsScope querySyncActionStats, TaskOrigin taskOrigin) {
assertProjectLoaded();
return run(
return runBuild(
"Building Render jar for Compose preview",
"Building...",
querySyncActionStats,
Expand Down
64 changes: 64 additions & 0 deletions base/src/com/google/idea/blaze/base/qsync/QuerySyncStatus.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* Copyright 2023 The Bazel Authors. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.idea.blaze.base.qsync;

import com.google.idea.blaze.base.qsync.QuerySyncManager.Operation;
import com.google.idea.blaze.base.sync.SyncMode;
import com.google.idea.blaze.base.sync.SyncResult;
import com.google.idea.blaze.base.sync.status.BlazeSyncStatus;
import com.intellij.openapi.project.Project;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicReference;

/** Class that keeps track of query sync operations. */
public class QuerySyncStatus {

private final Project project;
private final AtomicReference<Operation> operationInProgress = new AtomicReference<>(null);

public QuerySyncStatus(Project project) {
this.project = project;
}

public boolean syncInProgress() {
return Objects.equals(operationInProgress.get(), Operation.SYNC);
}

public boolean buildInProgress() {
return Objects.equals(operationInProgress.get(), Operation.BUILD);
}

public void operationStarted(Operation operationType) {
operationInProgress.set(operationType);
}

public void operationCancelled() {
operationEnded(SyncResult.CANCELLED);
}

public void operationFailed() {
operationEnded(SyncResult.FAILURE);
}

public void operationEnded() {
operationEnded(SyncResult.SUCCESS);
}

private void operationEnded(SyncResult result) {
operationInProgress.set(null);
BlazeSyncStatus.getInstance(project).syncEnded(SyncMode.FULL, result);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package com.google.idea.blaze.base.sync.status;

import com.google.idea.blaze.base.qsync.QuerySyncManager;
import com.google.idea.blaze.base.sync.SyncMode;
import com.google.idea.blaze.base.sync.SyncResult;
import com.intellij.openapi.project.Project;
Expand All @@ -31,7 +32,6 @@ enum SyncStatus {

SyncStatus getStatus();


static BlazeSyncStatus getInstance(Project project) {
return project.getService(BlazeSyncStatus.class);
}
Expand All @@ -40,6 +40,11 @@ static BlazeSyncStatus getInstance(Project project) {

void syncEnded(SyncMode syncMode, SyncResult syncResult);

/**
* @deprecated For query sync, use {@link QuerySyncManager#syncInProgress()} or {@link
* QuerySyncManager#buildInProgress()}
*/
@Deprecated
boolean syncInProgress();

void setDirty();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
*/
package com.google.idea.blaze.base.sync.status;

import com.google.idea.blaze.base.qsync.QuerySyncManager;
import com.google.idea.blaze.base.settings.Blaze;
import com.google.idea.blaze.base.settings.BlazeImportSettings.ProjectType;
import com.google.idea.blaze.base.sync.SyncMode;
import com.google.idea.blaze.base.sync.SyncResult;
import com.intellij.openapi.project.Project;
Expand All @@ -25,6 +28,8 @@
*/
public class BlazeSyncStatusImpl implements BlazeSyncStatus {

private final Project project;

public static BlazeSyncStatusImpl getImpl(Project project) {
return (BlazeSyncStatusImpl) BlazeSyncStatus.getInstance(project);
}
Expand All @@ -34,6 +39,7 @@ public static BlazeSyncStatusImpl getImpl(Project project) {

public BlazeSyncStatusImpl(Project project) {
this.stateManager = BlazeSyncStatusStateManager.getInstance(project);
this.project = project;
}

@Override
Expand All @@ -46,6 +52,10 @@ public SyncStatus getStatus() {

@Override
public boolean syncInProgress() {
if (Blaze.getProjectType(project).equals(ProjectType.QUERY_SYNC)) {
QuerySyncManager querySyncManager = QuerySyncManager.getInstance(project);
return querySyncManager.syncInProgress() || querySyncManager.buildInProgress();
}
return syncInProgress.get();
}

Expand Down