Search in sources :

Example 1 with PrefetchStats

use of com.google.idea.blaze.base.prefetch.PrefetchStats in project intellij by bazelbuild.

the class PackageManifestReader method readPackageManifestFiles.

/**
 * @return A map from java source absolute file path to declared package string.
 */
public Map<TargetKey, Map<ArtifactLocation, String>> readPackageManifestFiles(Project project, BlazeContext context, ArtifactLocationDecoder decoder, Map<TargetKey, ArtifactLocation> javaPackageManifests, ListeningExecutorService executorService) {
    Map<OutputArtifact, TargetKey> fileToLabelMap = Maps.newHashMap();
    for (Map.Entry<TargetKey, ArtifactLocation> entry : javaPackageManifests.entrySet()) {
        TargetKey key = entry.getKey();
        BlazeArtifact artifact = decoder.resolveOutput(entry.getValue());
        if (artifact instanceof OutputArtifact) {
            fileToLabelMap.put((OutputArtifact) artifact, key);
        }
    }
    ArtifactsDiff diff;
    try {
        diff = ArtifactsDiff.diffArtifacts(artifactState, fileToLabelMap.keySet());
        artifactState = diff.getNewState();
    } catch (InterruptedException e) {
        throw new ProcessCanceledException(e);
    } catch (ExecutionException e) {
        context.setHasError();
        IssueOutput.error("Updating package manifest files failed: " + e).submit(context);
        throw new AssertionError("Unhandled exception", e);
    }
    // Find all not cached {@link RemoteOutputArtifact} and download them before parsing manifest
    // file
    ImmutableList<RemoteOutputArtifact> toDownload = BlazeArtifact.getRemoteArtifacts(diff.getUpdatedOutputs()).stream().filter(a -> findArtifactInCache(project, a) == null).collect(toImmutableList());
    ListenableFuture<?> fetchRemoteArtifactFuture = RemoteArtifactPrefetcher.getInstance().downloadArtifacts(project.getName(), toDownload);
    ListenableFuture<PrefetchStats> fetchLocalFilesFuture = PrefetchService.getInstance().prefetchFiles(BlazeArtifact.getLocalFiles(diff.getUpdatedOutputs()), true, false);
    if (!FutureUtil.waitForFuture(context, Futures.allAsList(fetchRemoteArtifactFuture, fetchLocalFilesFuture)).timed("FetchPackageManifests", EventType.Prefetching).withProgressMessage("Reading package manifests...").run().success()) {
        return null;
    }
    try {
        long bytesConsumed = toDownload.stream().mapToLong(RemoteOutputArtifact::getLength).sum() + fetchLocalFilesFuture.get().bytesPrefetched();
        if (bytesConsumed > 0) {
            context.output(new NetworkTrafficUsedOutput(bytesConsumed, "packagemanifest"));
        }
    } catch (InterruptedException | ExecutionException e) {
        // Should never happen - the future has already completed.
        logger.error(e);
    // carry on - failing to log the stats should not affect anything else.
    }
    List<ListenableFuture<Void>> futures = Lists.newArrayList();
    for (OutputArtifact file : diff.getUpdatedOutputs()) {
        futures.add(executorService.submit(() -> {
            Map<ArtifactLocation, String> manifest = parseManifestFile(project, file);
            manifestMap.put(fileToLabelMap.get(file), manifest);
            return null;
        }));
    }
    for (ArtifactState file : diff.getRemovedOutputs()) {
        TargetKey key = this.fileToLabelMap.get(file);
        if (key != null) {
            manifestMap.remove(key);
        }
    }
    this.fileToLabelMap = fileToLabelMap.entrySet().stream().filter(e -> diff.getNewState().containsKey(e.getKey().getKey())).collect(toImmutableMap(e -> e.getKey().toArtifactState(), Map.Entry::getValue));
    try {
        Futures.allAsList(futures).get();
    } catch (ExecutionException | InterruptedException e) {
        logger.error(e);
        throw new IllegalStateException("Could not read sources");
    }
    return manifestMap;
}
Also used : BlazeContext(com.google.idea.blaze.base.scope.BlazeContext) BufferedInputStream(java.io.BufferedInputStream) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) PrefetchService(com.google.idea.blaze.base.prefetch.PrefetchService) HashMap(java.util.HashMap) NetworkTrafficUsedOutput(com.google.idea.blaze.base.scope.scopes.NetworkTrafficTrackingScope.NetworkTrafficUsedOutput) ArtifactsDiff(com.google.idea.blaze.base.filecache.ArtifactsDiff) ProcessCanceledException(com.intellij.openapi.progress.ProcessCanceledException) Lists(com.google.common.collect.Lists) ImmutableList(com.google.common.collect.ImmutableList) ArtifactState(com.google.idea.blaze.base.filecache.ArtifactState) Map(java.util.Map) IssueOutput(com.google.idea.blaze.base.scope.output.IssueOutput) Project(com.intellij.openapi.project.Project) Logger(com.intellij.openapi.diagnostic.Logger) Nullable(javax.annotation.Nullable) ArtifactLocation(com.google.idea.blaze.base.ideinfo.ArtifactLocation) ImmutableMap(com.google.common.collect.ImmutableMap) StringUtil(com.intellij.openapi.util.text.StringUtil) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) Common(com.google.devtools.intellij.aspect.Common) PackageManifest(com.google.devtools.intellij.ideinfo.IntellijIdeInfo.PackageManifest) FutureUtil(com.google.idea.blaze.base.async.FutureUtil) InputStreamProvider(com.google.idea.blaze.base.io.InputStreamProvider) IOException(java.io.IOException) RemoteArtifactPrefetcher(com.google.idea.blaze.base.prefetch.RemoteArtifactPrefetcher) FileInputStream(java.io.FileInputStream) Maps(com.google.common.collect.Maps) RemoteOutputsCache(com.google.idea.blaze.base.filecache.RemoteOutputsCache) File(java.io.File) BlazeArtifact(com.google.idea.blaze.base.command.buildresult.BlazeArtifact) OutputArtifact(com.google.idea.blaze.base.command.buildresult.OutputArtifact) ExecutionException(java.util.concurrent.ExecutionException) ImmutableMap.toImmutableMap(com.google.common.collect.ImmutableMap.toImmutableMap) Futures(com.google.common.util.concurrent.Futures) JavaSourcePackage(com.google.devtools.intellij.ideinfo.IntellijIdeInfo.JavaSourcePackage) List(java.util.List) PrefetchStats(com.google.idea.blaze.base.prefetch.PrefetchStats) ServiceManager(com.intellij.openapi.components.ServiceManager) RemoteOutputArtifact(com.google.idea.blaze.base.command.buildresult.RemoteOutputArtifact) TargetKey(com.google.idea.blaze.base.ideinfo.TargetKey) ArtifactLocationDecoder(com.google.idea.blaze.base.sync.workspace.ArtifactLocationDecoder) Joiner(com.google.common.base.Joiner) ListeningExecutorService(com.google.common.util.concurrent.ListeningExecutorService) EventType(com.google.idea.blaze.base.scope.scopes.TimingScope.EventType) InputStream(java.io.InputStream) ArtifactsDiff(com.google.idea.blaze.base.filecache.ArtifactsDiff) ArtifactState(com.google.idea.blaze.base.filecache.ArtifactState) PrefetchStats(com.google.idea.blaze.base.prefetch.PrefetchStats) NetworkTrafficUsedOutput(com.google.idea.blaze.base.scope.scopes.NetworkTrafficTrackingScope.NetworkTrafficUsedOutput) OutputArtifact(com.google.idea.blaze.base.command.buildresult.OutputArtifact) RemoteOutputArtifact(com.google.idea.blaze.base.command.buildresult.RemoteOutputArtifact) ExecutionException(java.util.concurrent.ExecutionException) ProcessCanceledException(com.intellij.openapi.progress.ProcessCanceledException) BlazeArtifact(com.google.idea.blaze.base.command.buildresult.BlazeArtifact) RemoteOutputArtifact(com.google.idea.blaze.base.command.buildresult.RemoteOutputArtifact) ArtifactLocation(com.google.idea.blaze.base.ideinfo.ArtifactLocation) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) TargetKey(com.google.idea.blaze.base.ideinfo.TargetKey) HashMap(java.util.HashMap) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) ImmutableMap.toImmutableMap(com.google.common.collect.ImmutableMap.toImmutableMap)

Example 2 with PrefetchStats

use of com.google.idea.blaze.base.prefetch.PrefetchStats in project intellij by bazelbuild.

the class ProjectUpdateSyncTask method run.

private void run(BlazeContext context) throws SyncCanceledException, SyncFailedException {
    TargetMap targetMap = targetData.targetMap;
    RemoteOutputArtifacts oldRemoteState = RemoteOutputArtifacts.fromProjectData(oldProjectData);
    RemoteOutputArtifacts newRemoteState = targetData.remoteOutputs;
    ArtifactLocationDecoder artifactLocationDecoder = new ArtifactLocationDecoderImpl(blazeInfo, projectState.getWorkspacePathResolver(), newRemoteState);
    Scope.push(context, childContext -> {
        childContext.push(new TimingScope("UpdateRemoteOutputsCache", EventType.Prefetching));
        RemoteOutputsCache.getInstance(project).updateCache(context, targetMap, projectState.getLanguageSettings(), newRemoteState, oldRemoteState, /* clearCache= */
        syncMode == SyncMode.FULL);
    });
    SyncState.Builder syncStateBuilder = new SyncState.Builder();
    Scope.push(context, childContext -> {
        childContext.push(new TimingScope("UpdateSyncState", EventType.Other));
        for (BlazeSyncPlugin syncPlugin : BlazeSyncPlugin.EP_NAME.getExtensions()) {
            syncPlugin.updateSyncState(project, childContext, workspaceRoot, projectState.getProjectViewSet(), projectState.getLanguageSettings(), projectState.getBlazeVersionData(), projectState.getWorkingSet(), artifactLocationDecoder, targetMap, syncStateBuilder, oldProjectData != null ? oldProjectData.getSyncState() : null, syncMode);
        }
    });
    if (context.isCancelled()) {
        throw new SyncCanceledException();
    }
    if (context.hasErrors()) {
        throw new SyncFailedException();
    }
    BlazeProjectData newProjectData = new BlazeProjectData(targetData, blazeInfo, projectState.getBlazeVersionData(), projectState.getWorkspacePathResolver(), artifactLocationDecoder, projectState.getLanguageSettings(), syncStateBuilder.build());
    FileCaches.onSync(project, context, projectState.getProjectViewSet(), newProjectData, oldProjectData, syncMode);
    ListenableFuture<PrefetchStats> prefetch = PrefetchService.getInstance().prefetchProjectFiles(project, projectState.getProjectViewSet(), newProjectData);
    FutureResult<PrefetchStats> result = FutureUtil.waitForFuture(context, prefetch).withProgressMessage("Prefetching files...").timed("PrefetchFiles", EventType.Prefetching).onError("Prefetch failed").run();
    if (result.success()) {
        long prefetched = result.result().bytesPrefetched();
        if (prefetched > 0) {
            context.output(new NetworkTrafficUsedOutput(prefetched, "prefetch"));
        }
    }
    ListenableFuture<DirectoryStructure> directoryStructureFuture = DirectoryStructure.getRootDirectoryStructure(project, workspaceRoot, projectState.getProjectViewSet());
    refreshVirtualFileSystem(context, project, newProjectData);
    DirectoryStructure directoryStructure = FutureUtil.waitForFuture(context, directoryStructureFuture).withProgressMessage("Computing directory structure...").timed("DirectoryStructure", EventType.Other).onError("Directory structure computation failed").run().result();
    if (directoryStructure == null) {
        throw new SyncFailedException();
    }
    boolean success = updateProject(context, projectState.getProjectViewSet(), projectState.getBlazeVersionData(), directoryStructure, oldProjectData, newProjectData);
    if (!success) {
        throw new SyncFailedException();
    }
}
Also used : RemoteOutputArtifacts(com.google.idea.blaze.base.model.RemoteOutputArtifacts) SyncFailedException(com.google.idea.blaze.base.sync.SyncScope.SyncFailedException) TimingScope(com.google.idea.blaze.base.scope.scopes.TimingScope) PrefetchStats(com.google.idea.blaze.base.prefetch.PrefetchStats) SyncState(com.google.idea.blaze.base.model.SyncState) SyncCanceledException(com.google.idea.blaze.base.sync.SyncScope.SyncCanceledException) BlazeProjectData(com.google.idea.blaze.base.model.BlazeProjectData) NetworkTrafficUsedOutput(com.google.idea.blaze.base.scope.scopes.NetworkTrafficTrackingScope.NetworkTrafficUsedOutput) ArtifactLocationDecoder(com.google.idea.blaze.base.sync.workspace.ArtifactLocationDecoder) ArtifactLocationDecoderImpl(com.google.idea.blaze.base.sync.workspace.ArtifactLocationDecoderImpl) TargetMap(com.google.idea.blaze.base.ideinfo.TargetMap) DirectoryStructure(com.google.idea.blaze.base.sync.projectstructure.DirectoryStructure)

Aggregations

PrefetchStats (com.google.idea.blaze.base.prefetch.PrefetchStats)2 NetworkTrafficUsedOutput (com.google.idea.blaze.base.scope.scopes.NetworkTrafficTrackingScope.NetworkTrafficUsedOutput)2 ArtifactLocationDecoder (com.google.idea.blaze.base.sync.workspace.ArtifactLocationDecoder)2 Joiner (com.google.common.base.Joiner)1 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 ImmutableMap.toImmutableMap (com.google.common.collect.ImmutableMap.toImmutableMap)1 Lists (com.google.common.collect.Lists)1 Maps (com.google.common.collect.Maps)1 Futures (com.google.common.util.concurrent.Futures)1 ListenableFuture (com.google.common.util.concurrent.ListenableFuture)1 ListeningExecutorService (com.google.common.util.concurrent.ListeningExecutorService)1 Common (com.google.devtools.intellij.aspect.Common)1 JavaSourcePackage (com.google.devtools.intellij.ideinfo.IntellijIdeInfo.JavaSourcePackage)1 PackageManifest (com.google.devtools.intellij.ideinfo.IntellijIdeInfo.PackageManifest)1 FutureUtil (com.google.idea.blaze.base.async.FutureUtil)1 BlazeArtifact (com.google.idea.blaze.base.command.buildresult.BlazeArtifact)1 OutputArtifact (com.google.idea.blaze.base.command.buildresult.OutputArtifact)1 RemoteOutputArtifact (com.google.idea.blaze.base.command.buildresult.RemoteOutputArtifact)1