Search in sources :

Example 56 with ListeningExecutorService

use of com.google.common.util.concurrent.ListeningExecutorService in project intellij by bazelbuild.

the class JarFilter method parseJavaFiles.

/**
 * Finds the expected jar archive file name prefixes for the java files.
 */
private static List<String> parseJavaFiles(List<Path> javaFiles) throws IOException {
    ListeningExecutorService executorService = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors()));
    List<ListenableFuture<String>> futures = Lists.newArrayList();
    for (final Path javaFile : javaFiles) {
        futures.add(executorService.submit(() -> {
            String packageString = getDeclaredPackageOfJavaFile(javaFile);
            return packageString != null ? getArchiveFileNamePrefix(javaFile.toString(), packageString) : null;
        }));
    }
    try {
        List<String> archiveFileNamePrefixes = Futures.allAsList(futures).get();
        List<String> result = Lists.newArrayList();
        for (String archiveFileNamePrefix : archiveFileNamePrefixes) {
            if (archiveFileNamePrefix != null) {
                result.add(archiveFileNamePrefix);
            }
        }
        return result;
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
        throw new IOException(e);
    } catch (ExecutionException e) {
        throw new IOException(e);
    }
}
Also used : Path(java.nio.file.Path) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) ListeningExecutorService(com.google.common.util.concurrent.ListeningExecutorService) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException)

Example 57 with ListeningExecutorService

use of com.google.common.util.concurrent.ListeningExecutorService in project intellij by bazelbuild.

the class PackageParser method parsePackageStrings.

@VisibleForTesting
Map<ArtifactLocation, String> parsePackageStrings(List<ArtifactLocation> sources) throws Exception {
    ListeningExecutorService executorService = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors()));
    Map<ArtifactLocation, ListenableFuture<String>> futures = Maps.newHashMap();
    for (final ArtifactLocation source : sources) {
        futures.put(source, executorService.submit(() -> getDeclaredPackageOfJavaFile(source)));
    }
    Map<ArtifactLocation, String> map = Maps.newHashMap();
    for (Entry<ArtifactLocation, ListenableFuture<String>> entry : futures.entrySet()) {
        String value = entry.getValue().get();
        if (value != null) {
            map.put(entry.getKey(), value);
        }
    }
    return map;
}
Also used : ArtifactLocation(com.google.devtools.intellij.ideinfo.IntellijIdeInfo.ArtifactLocation) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) ListeningExecutorService(com.google.common.util.concurrent.ListeningExecutorService) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 58 with ListeningExecutorService

use of com.google.common.util.concurrent.ListeningExecutorService in project intellij by bazelbuild.

the class BlazeIdeInterfaceAspectsImpl method updateState.

@Nullable
static State updateState(Project project, BlazeContext parentContext, @Nullable State prevState, ImmutableMap<File, Long> fileState, BlazeConfigurationHandler configHandler, WorkspaceLanguageSettings workspaceLanguageSettings, ImportRoots importRoots, AspectStrategy aspectStrategy, List<File> newFiles, List<File> removedFiles, boolean mergeWithOldState) {
    Result<State> result = Scope.push(parentContext, (ScopedFunction<Result<State>>) context -> {
        context.push(new TimingScope("UpdateTargetMap", EventType.Other));
        ImmutableMap<File, Long> nextFileState = fileState;
        if (mergeWithOldState && prevState != null) {
            ImmutableMap.Builder<File, Long> fileStateBuilder = ImmutableMap.<File, Long>builder().putAll(fileState);
            for (Map.Entry<File, Long> entry : prevState.fileState.entrySet()) {
                if (!fileState.containsKey(entry.getKey())) {
                    fileStateBuilder.put(entry);
                }
            }
            nextFileState = fileStateBuilder.build();
        }
        State state = new State();
        state.fileState = nextFileState;
        state.workspaceLanguageSettings = workspaceLanguageSettings;
        state.aspectStrategyName = aspectStrategy.getName();
        Map<TargetKey, TargetIdeInfo> targetMap = Maps.newHashMap();
        if (prevState != null) {
            targetMap.putAll(prevState.targetMap.map());
            state.fileToTargetMapKey.putAll(prevState.fileToTargetMapKey);
        }
        if (!mergeWithOldState) {
            for (File removedFile : removedFiles) {
                TargetKey key = state.fileToTargetMapKey.remove(removedFile);
                if (key != null) {
                    targetMap.remove(key);
                }
            }
        }
        AtomicLong totalSizeLoaded = new AtomicLong(0);
        Set<LanguageClass> ignoredLanguages = Sets.newConcurrentHashSet();
        ListeningExecutorService executor = BlazeExecutor.getInstance().getExecutor();
        List<ListenableFuture<TargetFilePair>> futures = Lists.newArrayList();
        for (File file : newFiles) {
            futures.add(executor.submit(() -> {
                totalSizeLoaded.addAndGet(file.length());
                IntellijIdeInfo.TargetIdeInfo message = aspectStrategy.readAspectFile(file);
                TargetIdeInfo target = protoToTarget(workspaceLanguageSettings, importRoots, message, ignoredLanguages);
                return new TargetFilePair(file, target);
            }));
        }
        Set<TargetKey> newTargets = new HashSet<>();
        Set<String> configurations = new LinkedHashSet<>();
        configurations.add(configHandler.defaultConfigurationPathComponent);
        int duplicateTargetLabels = 0;
        try {
            for (TargetFilePair targetFilePair : Futures.allAsList(futures).get()) {
                if (targetFilePair.target != null) {
                    File file = targetFilePair.file;
                    String config = configHandler.getConfigurationPathComponent(file);
                    configurations.add(config);
                    TargetKey key = targetFilePair.target.key;
                    if (targetMap.putIfAbsent(key, targetFilePair.target) == null) {
                        state.fileToTargetMapKey.forcePut(file, key);
                    } else {
                        if (!newTargets.add(key)) {
                            duplicateTargetLabels++;
                        }
                        if (Objects.equals(config, configHandler.defaultConfigurationPathComponent)) {
                            targetMap.put(key, targetFilePair.target);
                            state.fileToTargetMapKey.forcePut(file, key);
                        }
                    }
                }
            }
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
            return Result.error(null);
        } catch (ExecutionException e) {
            return Result.error(e);
        }
        context.output(PrintOutput.log(String.format("Loaded %d aspect files, total size %dkB", newFiles.size(), totalSizeLoaded.get() / 1024)));
        if (duplicateTargetLabels > 0) {
            context.output(new PerformanceWarning(String.format("There were %d duplicate rules, built with the following " + "configurations: %s.\nYour IDE sync is slowed down by ~%d%%.", duplicateTargetLabels, configurations, (100 * duplicateTargetLabels / targetMap.size()))));
        }
        ignoredLanguages.retainAll(LanguageSupport.availableAdditionalLanguages(workspaceLanguageSettings.getWorkspaceType()));
        warnIgnoredLanguages(project, context, ignoredLanguages);
        state.targetMap = new TargetMap(ImmutableMap.copyOf(targetMap));
        return Result.of(state);
    });
    if (result.error != null) {
        logger.error(result.error);
        return null;
    }
    return result.result;
}
Also used : ExternalTask(com.google.idea.blaze.base.async.process.ExternalTask) AdditionalLanguagesHelper(com.google.idea.blaze.base.lang.AdditionalLanguagesHelper) Result(com.google.idea.blaze.base.scope.Result) LanguageSupport(com.google.idea.blaze.base.sync.projectview.LanguageSupport) PrefetchService(com.google.idea.blaze.base.prefetch.PrefetchService) ScopedFunction(com.google.idea.blaze.base.scope.ScopedFunction) Map(java.util.Map) TargetExpression(com.google.idea.blaze.base.model.primitives.TargetExpression) FileUtil(com.intellij.openapi.util.io.FileUtil) Logger(com.intellij.openapi.diagnostic.Logger) IntellijIdeInfo(com.google.devtools.intellij.ideinfo.IntellijIdeInfo) FileDiffer(com.google.idea.blaze.base.filecache.FileDiffer) BlazeConsoleLineProcessorProvider(com.google.idea.blaze.base.console.BlazeConsoleLineProcessorProvider) Blaze(com.google.idea.blaze.base.settings.Blaze) BiMap(com.google.common.collect.BiMap) ImmutableSet(com.google.common.collect.ImmutableSet) ImmutableMap(com.google.common.collect.ImmutableMap) AspectStrategy(com.google.idea.blaze.base.sync.aspects.strategy.AspectStrategy) Predicate(java.util.function.Predicate) Collection(java.util.Collection) BuildSystemProvider(com.google.idea.blaze.base.bazel.BuildSystemProvider) FutureUtil(com.google.idea.blaze.base.async.FutureUtil) Set(java.util.Set) WorkspaceLanguageSettings(com.google.idea.blaze.base.sync.projectview.WorkspaceLanguageSettings) BuildResultHelper(com.google.idea.blaze.base.command.buildresult.BuildResultHelper) PrintOutput(com.google.idea.blaze.base.scope.output.PrintOutput) Sets(com.google.common.collect.Sets) Serializable(java.io.Serializable) BlazeCommandName(com.google.idea.blaze.base.command.BlazeCommandName) Objects(java.util.Objects) List(java.util.List) LineProcessingOutputStream(com.google.idea.blaze.base.async.process.LineProcessingOutputStream) Status(com.google.idea.blaze.base.sync.aspects.BuildResult.Status) ProjectViewSet(com.google.idea.blaze.base.projectview.ProjectViewSet) WorkspaceRoot(com.google.idea.blaze.base.model.primitives.WorkspaceRoot) TargetKey(com.google.idea.blaze.base.ideinfo.TargetKey) ShardedTargetList(com.google.idea.blaze.base.sync.sharding.ShardedTargetList) ArtifactLocationDecoder(com.google.idea.blaze.base.sync.workspace.ArtifactLocationDecoder) TargetMap(com.google.idea.blaze.base.ideinfo.TargetMap) Joiner(com.google.common.base.Joiner) ListeningExecutorService(com.google.common.util.concurrent.ListeningExecutorService) BlazeContext(com.google.idea.blaze.base.scope.BlazeContext) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) AspectStrategyProvider(com.google.idea.blaze.base.sync.aspects.strategy.AspectStrategyProvider) Function(java.util.function.Function) ArrayList(java.util.ArrayList) NavigatableAdapter(com.intellij.pom.NavigatableAdapter) HashSet(java.util.HashSet) Kind(com.google.idea.blaze.base.model.primitives.Kind) Lists(com.google.common.collect.Lists) ImmutableList(com.google.common.collect.ImmutableList) BuildSystem(com.google.idea.blaze.base.settings.Blaze.BuildSystem) Scope(com.google.idea.blaze.base.scope.Scope) OutputGroup(com.google.idea.blaze.base.sync.aspects.strategy.AspectStrategy.OutputGroup) IssueOutput(com.google.idea.blaze.base.scope.output.IssueOutput) TargetIdeInfo(com.google.idea.blaze.base.ideinfo.TargetIdeInfo) Project(com.intellij.openapi.project.Project) LinkedHashSet(java.util.LinkedHashSet) Nullable(javax.annotation.Nullable) LanguageClass(com.google.idea.blaze.base.model.primitives.LanguageClass) PerformanceWarning(com.google.idea.blaze.base.scope.output.PerformanceWarning) ImportRoots(com.google.idea.blaze.base.sync.projectview.ImportRoots) PrefetchFileSource(com.google.idea.blaze.base.prefetch.PrefetchFileSource) SyncState(com.google.idea.blaze.base.model.SyncState) Maps(com.google.common.collect.Maps) File(java.io.File) ExecutionException(java.util.concurrent.ExecutionException) BlazeExecutor(com.google.idea.blaze.base.async.executor.BlazeExecutor) AtomicLong(java.util.concurrent.atomic.AtomicLong) HashBiMap(com.google.common.collect.HashBiMap) Futures(com.google.common.util.concurrent.Futures) Ordering(com.google.common.collect.Ordering) BlazeVersionData(com.google.idea.blaze.base.model.BlazeVersionData) BlazeConfigurationHandler(com.google.idea.blaze.base.command.info.BlazeConfigurationHandler) TimingScope(com.google.idea.blaze.base.scope.scopes.TimingScope) BlazeFlags(com.google.idea.blaze.base.command.BlazeFlags) BlazeInvocationContext(com.google.idea.blaze.base.command.BlazeInvocationContext) BlazeCommand(com.google.idea.blaze.base.command.BlazeCommand) EventType(com.google.idea.blaze.base.scope.scopes.TimingScope.EventType) ImmutableSet(com.google.common.collect.ImmutableSet) Set(java.util.Set) ProjectViewSet(com.google.idea.blaze.base.projectview.ProjectViewSet) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) PerformanceWarning(com.google.idea.blaze.base.scope.output.PerformanceWarning) Result(com.google.idea.blaze.base.scope.Result) TimingScope(com.google.idea.blaze.base.scope.scopes.TimingScope) List(java.util.List) ShardedTargetList(com.google.idea.blaze.base.sync.sharding.ShardedTargetList) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) ExecutionException(java.util.concurrent.ExecutionException) ImmutableMap(com.google.common.collect.ImmutableMap) TargetIdeInfo(com.google.idea.blaze.base.ideinfo.TargetIdeInfo) AtomicLong(java.util.concurrent.atomic.AtomicLong) SyncState(com.google.idea.blaze.base.model.SyncState) IntellijIdeInfo(com.google.devtools.intellij.ideinfo.IntellijIdeInfo) AtomicLong(java.util.concurrent.atomic.AtomicLong) ListeningExecutorService(com.google.common.util.concurrent.ListeningExecutorService) TargetKey(com.google.idea.blaze.base.ideinfo.TargetKey) File(java.io.File) Map(java.util.Map) BiMap(com.google.common.collect.BiMap) ImmutableMap(com.google.common.collect.ImmutableMap) TargetMap(com.google.idea.blaze.base.ideinfo.TargetMap) HashBiMap(com.google.common.collect.HashBiMap) TargetMap(com.google.idea.blaze.base.ideinfo.TargetMap) Nullable(javax.annotation.Nullable)

Example 59 with ListeningExecutorService

use of com.google.common.util.concurrent.ListeningExecutorService in project intellij by bazelbuild.

the class DirectoryStructure method computeRootDirectoryStructure.

private static DirectoryStructure computeRootDirectoryStructure(Project project, WorkspaceRoot workspaceRoot, ProjectViewSet projectViewSet) throws ExecutionException, InterruptedException {
    ListeningExecutorService executorService = FetchExecutor.EXECUTOR;
    FileOperationProvider fileOperationProvider = FileOperationProvider.getInstance();
    ImportRoots importRoots = ImportRoots.builder(workspaceRoot, Blaze.getBuildSystem(project)).add(projectViewSet).build();
    Collection<WorkspacePath> rootDirectories = importRoots.rootDirectories();
    Set<WorkspacePath> excludeDirectories = importRoots.excludeDirectories();
    List<ListenableFuture<PathStructurePair>> futures = Lists.newArrayListWithExpectedSize(rootDirectories.size());
    for (WorkspacePath rootDirectory : rootDirectories) {
        futures.add(walkDirectoryStructure(workspaceRoot, excludeDirectories, fileOperationProvider, executorService, rootDirectory));
    }
    ImmutableMap.Builder<WorkspacePath, DirectoryStructure> result = ImmutableMap.builder();
    for (PathStructurePair pair : Futures.allAsList(futures).get()) {
        if (pair != null) {
            result.put(pair.path, pair.directoryStructure);
        }
    }
    return new DirectoryStructure(result.build());
}
Also used : WorkspacePath(com.google.idea.blaze.base.model.primitives.WorkspacePath) ImportRoots(com.google.idea.blaze.base.sync.projectview.ImportRoots) FileOperationProvider(com.google.idea.blaze.base.io.FileOperationProvider) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) ListeningExecutorService(com.google.common.util.concurrent.ListeningExecutorService) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 60 with ListeningExecutorService

use of com.google.common.util.concurrent.ListeningExecutorService in project intellij by bazelbuild.

the class DirectoryStructure method walkDirectoryStructure.

private static ListenableFuture<PathStructurePair> walkDirectoryStructure(WorkspaceRoot workspaceRoot, Set<WorkspacePath> excludeDirectories, FileOperationProvider fileOperationProvider, ListeningExecutorService executorService, WorkspacePath workspacePath) {
    if (excludeDirectories.contains(workspacePath)) {
        return Futures.immediateFuture(null);
    }
    File file = workspaceRoot.fileForPath(workspacePath);
    if (!fileOperationProvider.isDirectory(file)) {
        return Futures.immediateFuture(null);
    }
    ListenableFuture<File[]> childrenFuture = executorService.submit(() -> fileOperationProvider.listFiles(file));
    return Futures.transformAsync(childrenFuture, children -> {
        if (children == null) {
            return Futures.immediateFuture(null);
        }
        List<ListenableFuture<PathStructurePair>> futures = Lists.newArrayListWithExpectedSize(children.length);
        for (File child : children) {
            WorkspacePath childWorkspacePath;
            try {
                childWorkspacePath = workspaceRoot.workspacePathFor(child);
            } catch (IllegalArgumentException e) {
                // stop at directories with unhandled characters.
                continue;
            }
            futures.add(walkDirectoryStructure(workspaceRoot, excludeDirectories, fileOperationProvider, executorService, childWorkspacePath));
        }
        return Futures.transform(Futures.allAsList(futures), (Function<List<PathStructurePair>, PathStructurePair>) pairs -> {
            Builder<WorkspacePath, DirectoryStructure> result = ImmutableMap.builder();
            for (PathStructurePair pair : pairs) {
                if (pair != null) {
                    result.put(pair.path, pair.directoryStructure);
                }
            }
            return new PathStructurePair(workspacePath, new DirectoryStructure(result.build()));
        }, executorService);
    }, executorService);
}
Also used : WorkspacePath(com.google.idea.blaze.base.model.primitives.WorkspacePath) Blaze(com.google.idea.blaze.base.settings.Blaze) ImportRoots(com.google.idea.blaze.base.sync.projectview.ImportRoots) Function(com.google.common.base.Function) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) ImmutableMap(com.google.common.collect.ImmutableMap) Collection(java.util.Collection) Set(java.util.Set) FileOperationProvider(com.google.idea.blaze.base.io.FileOperationProvider) File(java.io.File) Builder(com.google.common.collect.ImmutableMap.Builder) ExecutionException(java.util.concurrent.ExecutionException) Futures(com.google.common.util.concurrent.Futures) List(java.util.List) Lists(com.google.common.collect.Lists) ProjectViewSet(com.google.idea.blaze.base.projectview.ProjectViewSet) WorkspaceRoot(com.google.idea.blaze.base.model.primitives.WorkspaceRoot) Project(com.intellij.openapi.project.Project) WorkspacePath(com.google.idea.blaze.base.model.primitives.WorkspacePath) FetchExecutor(com.google.idea.blaze.base.prefetch.FetchExecutor) ListeningExecutorService(com.google.common.util.concurrent.ListeningExecutorService) Builder(com.google.common.collect.ImmutableMap.Builder) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) List(java.util.List) File(java.io.File)

Aggregations

ListeningExecutorService (com.google.common.util.concurrent.ListeningExecutorService)201 Test (org.junit.Test)115 ListenableFuture (com.google.common.util.concurrent.ListenableFuture)75 ArrayList (java.util.ArrayList)43 CountDownLatch (java.util.concurrent.CountDownLatch)29 ExecutorService (java.util.concurrent.ExecutorService)28 IOException (java.io.IOException)25 ExecutionException (java.util.concurrent.ExecutionException)25 Interval (org.joda.time.Interval)25 DateTime (org.joda.time.DateTime)23 List (java.util.List)21 Callable (java.util.concurrent.Callable)20 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)20 DruidServer (io.druid.client.DruidServer)18 DataSegment (io.druid.timeline.DataSegment)18 DruidServer (org.apache.druid.client.DruidServer)17 ImmutableMap (com.google.common.collect.ImmutableMap)16 File (java.io.File)16 Map (java.util.Map)16 ApplicationId (org.apache.hadoop.yarn.api.records.ApplicationId)15