Search in sources :

Example 6 with RemoteOutputArtifact

use of com.google.idea.blaze.base.command.buildresult.RemoteOutputArtifact in project intellij by bazelbuild.

the class RemoteOutputsCache method updateCache.

private void updateCache(BlazeContext context, Set<RemoteOutputArtifact> toCache, RemoteOutputArtifacts previousOutputs) {
    Map<String, RemoteOutputArtifact> newState = toCache.stream().collect(toImmutableMap(RemoteOutputsCache::getCacheKey, Functions.identity()));
    Map<String, File> cachedFiles = readCachedFiles();
    try {
        Map<String, RemoteOutputArtifact> updatedOutputs = FileCacheDiffer.findUpdatedOutputs(newState, cachedFiles, previousOutputs);
        List<File> removed = cachedFiles.entrySet().stream().filter(e -> !newState.containsKey(e.getKey())).map(Map.Entry::getValue).collect(toImmutableList());
        // Ensure the cache dir exists
        if (!cacheDir.exists()) {
            if (!cacheDir.mkdirs()) {
                IssueOutput.error("Could not create remote outputs cache directory").submit(context);
                context.setHasError();
                return;
            }
        }
        ListenableFuture<?> downloadArtifactsFuture = RemoteArtifactPrefetcher.getInstance().downloadArtifacts(/* projectName= */
        project.getName(), /* outputArtifacts= */
        BlazeArtifact.getRemoteArtifacts(updatedOutputs.values()));
        FutureUtil.waitForFuture(context, downloadArtifactsFuture).timed("PrefetchRemoteOutput", EventType.Prefetching).withProgressMessage("Prefetching output artifacts...").run();
        List<ListenableFuture<?>> futures = new ArrayList<>(copyLocally(updatedOutputs));
        futures.addAll(deleteCacheFiles(removed));
        Futures.allAsList(futures).get();
        this.cachedFiles = newState.keySet().stream().collect(toImmutableMap(Functions.identity(), k -> new File(cacheDir, k)));
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
        context.setCancelled();
    } catch (ExecutionException e) {
        IssueOutput.warn("Remote outputs synchronization didn't complete: " + e.getMessage()).submit(context);
    }
}
Also used : ArrayList(java.util.ArrayList) RemoteOutputArtifact(com.google.idea.blaze.base.command.buildresult.RemoteOutputArtifact) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) ExecutionException(java.util.concurrent.ExecutionException) File(java.io.File) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) ImmutableMap.toImmutableMap(com.google.common.collect.ImmutableMap.toImmutableMap) TargetMap(com.google.idea.blaze.base.ideinfo.TargetMap)

Example 7 with RemoteOutputArtifact

use of com.google.idea.blaze.base.command.buildresult.RemoteOutputArtifact 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 8 with RemoteOutputArtifact

use of com.google.idea.blaze.base.command.buildresult.RemoteOutputArtifact in project intellij by bazelbuild.

the class FileCacheDiffer method shouldUpdateRemote.

private static boolean shouldUpdateRemote(RemoteOutputArtifact newOutput, RemoteOutputArtifacts previousOutputs) {
    RemoteOutputArtifact previous = previousOutputs.findRemoteOutput(newOutput.getRelativePath());
    if (previous == null) {
        return true;
    }
    ArtifactState previousState = previous.toArtifactState();
    ArtifactState newState = newOutput.toArtifactState();
    return previousState == null || (newState != null && previousState.isMoreRecent(newState));
}
Also used : RemoteOutputArtifact(com.google.idea.blaze.base.command.buildresult.RemoteOutputArtifact)

Example 9 with RemoteOutputArtifact

use of com.google.idea.blaze.base.command.buildresult.RemoteOutputArtifact in project intellij by bazelbuild.

the class RemoteOutputArtifacts method appendNewOutputs.

/**
 * Merges this set of outputs with another set, returning a new {@link RemoteOutputArtifacts}
 * instance.
 */
public RemoteOutputArtifacts appendNewOutputs(Set<OutputArtifact> outputs) {
    HashMap<String, RemoteOutputArtifact> map = new HashMap<>(remoteOutputArtifacts);
    // more recently built artifacts replace existing ones with the same path
    outputs.forEach(a -> {
        String key = a.getKey();
        if (!(a instanceof RemoteOutputArtifact)) {
            map.remove(key);
        } else {
            RemoteOutputArtifact newOutput = (RemoteOutputArtifact) a;
            RemoteOutputArtifact other = map.get(key);
            if (other == null || other.getSyncTimeMillis() < newOutput.getSyncTimeMillis()) {
                map.put(key, newOutput);
            }
        }
    });
    return new RemoteOutputArtifacts(ImmutableMap.copyOf(map));
}
Also used : RemoteOutputArtifact(com.google.idea.blaze.base.command.buildresult.RemoteOutputArtifact) HashMap(java.util.HashMap)

Example 10 with RemoteOutputArtifact

use of com.google.idea.blaze.base.command.buildresult.RemoteOutputArtifact in project intellij by bazelbuild.

the class RemoteOutputsCacheTest method testDotInDirectory.

@Test
public void testDotInDirectory() {
    RemoteOutputArtifact artifact = mock(RemoteOutputArtifact.class);
    when(artifact.getKey()).thenReturn("k8-opt/foo.bar/foo.bar");
    assertThat(RemoteOutputsCache.getCacheKey(artifact)).isEqualTo("foo_df1c67cb.bar");
}
Also used : RemoteOutputArtifact(com.google.idea.blaze.base.command.buildresult.RemoteOutputArtifact) Test(org.junit.Test)

Aggregations

RemoteOutputArtifact (com.google.idea.blaze.base.command.buildresult.RemoteOutputArtifact)14 Test (org.junit.Test)8 File (java.io.File)4 ImmutableMap (com.google.common.collect.ImmutableMap)2 ImmutableMap.toImmutableMap (com.google.common.collect.ImmutableMap.toImmutableMap)2 ListenableFuture (com.google.common.util.concurrent.ListenableFuture)2 BlazeArtifact (com.google.idea.blaze.base.command.buildresult.BlazeArtifact)2 Map (java.util.Map)2 ExecutionException (java.util.concurrent.ExecutionException)2 Joiner (com.google.common.base.Joiner)1 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)1 Lists (com.google.common.collect.Lists)1 Maps (com.google.common.collect.Maps)1 Futures (com.google.common.util.concurrent.Futures)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