From 5f4346f45ddc82b1e053a059fb194881212513d5 Mon Sep 17 00:00:00 2001 From: Snjezana Peco Date: Fri, 5 Apr 2019 19:23:51 +0200 Subject: [PATCH] Batch Maven project imports to reduce memory consumption Signed-off-by: Snjezana Peco --- .../handlers/WorkspaceDiagnosticsHandler.java | 4 ++- .../managers/MavenProjectImporter.java | 36 +++++++++++++++++-- .../maven/batch/batch1/batchchild/pom.xml | 11 ++++++ .../projects/maven/batch/batch1/pom.xml | 17 +++++++++ .../projects/maven/batch/batch10/pom.xml | 11 ++++++ .../projects/maven/batch/batch11/pom.xml | 11 ++++++ .../projects/maven/batch/batch2/pom.xml | 11 ++++++ .../projects/maven/batch/batch3/pom.xml | 12 +++++++ .../projects/maven/batch/batch4/pom.xml | 11 ++++++ .../projects/maven/batch/batch5/pom.xml | 11 ++++++ .../projects/maven/batch/batch6/pom.xml | 11 ++++++ .../projects/maven/batch/batch7/pom.xml | 11 ++++++ .../projects/maven/batch/batch8/pom.xml | 11 ++++++ .../projects/maven/batch/batch9/pom.xml | 11 ++++++ .../projects/maven/batch/pom.xml | 22 ++++++++++++ .../managers/MavenBuildSupportTest.java | 14 ++++++++ 16 files changed, 212 insertions(+), 3 deletions(-) create mode 100644 org.eclipse.jdt.ls.tests/projects/maven/batch/batch1/batchchild/pom.xml create mode 100644 org.eclipse.jdt.ls.tests/projects/maven/batch/batch1/pom.xml create mode 100644 org.eclipse.jdt.ls.tests/projects/maven/batch/batch10/pom.xml create mode 100644 org.eclipse.jdt.ls.tests/projects/maven/batch/batch11/pom.xml create mode 100644 org.eclipse.jdt.ls.tests/projects/maven/batch/batch2/pom.xml create mode 100644 org.eclipse.jdt.ls.tests/projects/maven/batch/batch3/pom.xml create mode 100644 org.eclipse.jdt.ls.tests/projects/maven/batch/batch4/pom.xml create mode 100644 org.eclipse.jdt.ls.tests/projects/maven/batch/batch5/pom.xml create mode 100644 org.eclipse.jdt.ls.tests/projects/maven/batch/batch6/pom.xml create mode 100644 org.eclipse.jdt.ls.tests/projects/maven/batch/batch7/pom.xml create mode 100644 org.eclipse.jdt.ls.tests/projects/maven/batch/batch8/pom.xml create mode 100644 org.eclipse.jdt.ls.tests/projects/maven/batch/batch9/pom.xml create mode 100644 org.eclipse.jdt.ls.tests/projects/maven/batch/pom.xml diff --git a/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/handlers/WorkspaceDiagnosticsHandler.java b/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/handlers/WorkspaceDiagnosticsHandler.java index 6c06412cdb..e59cbe9248 100644 --- a/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/handlers/WorkspaceDiagnosticsHandler.java +++ b/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/handlers/WorkspaceDiagnosticsHandler.java @@ -34,6 +34,7 @@ import org.eclipse.core.runtime.OperationCanceledException; import org.eclipse.jdt.core.ICompilationUnit; import org.eclipse.jdt.core.IJavaModelMarker; +import org.eclipse.jdt.core.IJavaProject; import org.eclipse.jdt.core.JavaCore; import org.eclipse.jdt.core.JavaModelException; import org.eclipse.jdt.ls.core.internal.JDTUtils; @@ -129,7 +130,8 @@ public boolean visit(IResourceDelta delta) throws CoreException { if (JavaCore.isJavaLikeFileName(file.getName())) { ICompilationUnit cu = (ICompilationUnit) JavaCore.create(file); // Clear the diagnostics for the resource not on the classpath - if (cu.getJavaProject() == null || !cu.getJavaProject().isOnClasspath(cu)) { + IJavaProject javaProject = cu.getJavaProject(); + if (javaProject == null || !javaProject.isOnClasspath(cu)) { String uri = JDTUtils.getFileURI(resource); this.connection.publishDiagnostics(new PublishDiagnosticsParams(ResourceUtils.toClientUri(uri), Collections.emptyList())); return false; diff --git a/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/managers/MavenProjectImporter.java b/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/managers/MavenProjectImporter.java index 6f8f46ff4d..1a034eeaa1 100644 --- a/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/managers/MavenProjectImporter.java +++ b/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/managers/MavenProjectImporter.java @@ -13,6 +13,7 @@ import java.io.File; import java.nio.file.FileSystems; import java.nio.file.PathMatcher; +import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.Iterator; @@ -42,6 +43,7 @@ import org.eclipse.m2e.core.internal.MavenPluginActivator; import org.eclipse.m2e.core.internal.preferences.MavenConfigurationImpl; import org.eclipse.m2e.core.internal.preferences.ProblemSeverity; +import org.eclipse.m2e.core.project.IMavenProjectImportResult; import org.eclipse.m2e.core.project.IProjectConfigurationManager; import org.eclipse.m2e.core.project.LocalProjectScanner; import org.eclipse.m2e.core.project.MavenProjectInfo; @@ -50,6 +52,8 @@ @SuppressWarnings("restriction") public class MavenProjectImporter extends AbstractProjectImporter { + private static final int MAX_PROJECTS_TO_IMPORT = 10; + public static final String IMPORTING_MAVEN_PROJECTS = "Importing Maven project(s)"; public static final String POM_FILE = "pom.xml"; @@ -149,8 +153,36 @@ public void importToWorkspace(IProgressMonitor monitor) throws CoreException, Op } } if (!toImport.isEmpty()) { - ProjectImportConfiguration importConfig = new ProjectImportConfiguration(); - configurationManager.importProjects(toImport, importConfig, subMonitor.split(75)); + if (toImport.size() > MAX_PROJECTS_TO_IMPORT) { + JavaLanguageServerPlugin.logInfo("Projects size:" + toImport.size()); + Iterator iter = toImport.iterator(); + List results = new ArrayList<>(MAX_PROJECTS_TO_IMPORT); + SubMonitor monitor2 = SubMonitor.convert(monitor, toImport.size() * 2); + int it = 1; + while (iter.hasNext()) { + int percent = Math.min(100, it++ * 100 * MAX_PROJECTS_TO_IMPORT / (toImport.size() + 1)); + monitor2.setTaskName(percent + "% " + IMPORTING_MAVEN_PROJECTS); + List importPartial = new ArrayList<>(); + int i = 0; + while (i++ < MAX_PROJECTS_TO_IMPORT && iter.hasNext()) { + importPartial.add(iter.next()); + } + ProjectImportConfiguration importConfig = new ProjectImportConfiguration(); + List result = configurationManager.importProjects(importPartial, importConfig, monitor2.split(MAX_PROJECTS_TO_IMPORT)); + results.addAll(result); + monitor2.setWorkRemaining(toImport.size() * 2 - it * MAX_PROJECTS_TO_IMPORT); + } + List imported = new ArrayList<>(results.size()); + for (IMavenProjectImportResult result : results) { + imported.add(result.getProject()); + } + monitor2.setTaskName("Updating Maven project(s)"); + updateProjects(imported, lastWorkspaceStateSaved, monitor2.split(projects.size())); + monitor2.done(); + } else { + ProjectImportConfiguration importConfig = new ProjectImportConfiguration(); + configurationManager.importProjects(toImport, importConfig, subMonitor.split(75)); + } } subMonitor.setWorkRemaining(20); updateProjects(projects, lastWorkspaceStateSaved, subMonitor.split(20)); diff --git a/org.eclipse.jdt.ls.tests/projects/maven/batch/batch1/batchchild/pom.xml b/org.eclipse.jdt.ls.tests/projects/maven/batch/batch1/batchchild/pom.xml new file mode 100644 index 0000000000..1db16d17cc --- /dev/null +++ b/org.eclipse.jdt.ls.tests/projects/maven/batch/batch1/batchchild/pom.xml @@ -0,0 +1,11 @@ + + 4.0.0 + + foo.bar + batch1 + 0.0.1-SNAPSHOT + + batchchild + \ No newline at end of file diff --git a/org.eclipse.jdt.ls.tests/projects/maven/batch/batch1/pom.xml b/org.eclipse.jdt.ls.tests/projects/maven/batch/batch1/pom.xml new file mode 100644 index 0000000000..ab10af8fc3 --- /dev/null +++ b/org.eclipse.jdt.ls.tests/projects/maven/batch/batch1/pom.xml @@ -0,0 +1,17 @@ + + 4.0.0 + + foo.bar + batch + 0.0.1-SNAPSHOT + + batch1 + pom + + + batchchild + + + \ No newline at end of file diff --git a/org.eclipse.jdt.ls.tests/projects/maven/batch/batch10/pom.xml b/org.eclipse.jdt.ls.tests/projects/maven/batch/batch10/pom.xml new file mode 100644 index 0000000000..0fe6351456 --- /dev/null +++ b/org.eclipse.jdt.ls.tests/projects/maven/batch/batch10/pom.xml @@ -0,0 +1,11 @@ + + 4.0.0 + + foo.bar + multimodule + 0.0.1-SNAPSHOT + + batch10 + \ No newline at end of file diff --git a/org.eclipse.jdt.ls.tests/projects/maven/batch/batch11/pom.xml b/org.eclipse.jdt.ls.tests/projects/maven/batch/batch11/pom.xml new file mode 100644 index 0000000000..faefd98ff7 --- /dev/null +++ b/org.eclipse.jdt.ls.tests/projects/maven/batch/batch11/pom.xml @@ -0,0 +1,11 @@ + + 4.0.0 + + foo.bar + multimodule + 0.0.1-SNAPSHOT + + batch11 + \ No newline at end of file diff --git a/org.eclipse.jdt.ls.tests/projects/maven/batch/batch2/pom.xml b/org.eclipse.jdt.ls.tests/projects/maven/batch/batch2/pom.xml new file mode 100644 index 0000000000..5b4be12652 --- /dev/null +++ b/org.eclipse.jdt.ls.tests/projects/maven/batch/batch2/pom.xml @@ -0,0 +1,11 @@ + + 4.0.0 + + foo.bar + multimodule + 0.0.1-SNAPSHOT + + batch2 + \ No newline at end of file diff --git a/org.eclipse.jdt.ls.tests/projects/maven/batch/batch3/pom.xml b/org.eclipse.jdt.ls.tests/projects/maven/batch/batch3/pom.xml new file mode 100644 index 0000000000..f0bfde6078 --- /dev/null +++ b/org.eclipse.jdt.ls.tests/projects/maven/batch/batch3/pom.xml @@ -0,0 +1,12 @@ + + 4.0.0 + + foo.bar + multimodule + 0.0.1-SNAPSHOT + + batch3 + jar + \ No newline at end of file diff --git a/org.eclipse.jdt.ls.tests/projects/maven/batch/batch4/pom.xml b/org.eclipse.jdt.ls.tests/projects/maven/batch/batch4/pom.xml new file mode 100644 index 0000000000..ca83a39497 --- /dev/null +++ b/org.eclipse.jdt.ls.tests/projects/maven/batch/batch4/pom.xml @@ -0,0 +1,11 @@ + + 4.0.0 + + foo.bar + multimodule + 0.0.1-SNAPSHOT + + batch4 + \ No newline at end of file diff --git a/org.eclipse.jdt.ls.tests/projects/maven/batch/batch5/pom.xml b/org.eclipse.jdt.ls.tests/projects/maven/batch/batch5/pom.xml new file mode 100644 index 0000000000..19648669b7 --- /dev/null +++ b/org.eclipse.jdt.ls.tests/projects/maven/batch/batch5/pom.xml @@ -0,0 +1,11 @@ + + 4.0.0 + + foo.bar + multimodule + 0.0.1-SNAPSHOT + + batch5 + \ No newline at end of file diff --git a/org.eclipse.jdt.ls.tests/projects/maven/batch/batch6/pom.xml b/org.eclipse.jdt.ls.tests/projects/maven/batch/batch6/pom.xml new file mode 100644 index 0000000000..07cf6dacd6 --- /dev/null +++ b/org.eclipse.jdt.ls.tests/projects/maven/batch/batch6/pom.xml @@ -0,0 +1,11 @@ + + 4.0.0 + + foo.bar + multimodule + 0.0.1-SNAPSHOT + + batch6 + \ No newline at end of file diff --git a/org.eclipse.jdt.ls.tests/projects/maven/batch/batch7/pom.xml b/org.eclipse.jdt.ls.tests/projects/maven/batch/batch7/pom.xml new file mode 100644 index 0000000000..3874dd9428 --- /dev/null +++ b/org.eclipse.jdt.ls.tests/projects/maven/batch/batch7/pom.xml @@ -0,0 +1,11 @@ + + 4.0.0 + + foo.bar + multimodule + 0.0.1-SNAPSHOT + + batch7 + \ No newline at end of file diff --git a/org.eclipse.jdt.ls.tests/projects/maven/batch/batch8/pom.xml b/org.eclipse.jdt.ls.tests/projects/maven/batch/batch8/pom.xml new file mode 100644 index 0000000000..f002dc5b97 --- /dev/null +++ b/org.eclipse.jdt.ls.tests/projects/maven/batch/batch8/pom.xml @@ -0,0 +1,11 @@ + + 4.0.0 + + foo.bar + multimodule + 0.0.1-SNAPSHOT + + batch8 + \ No newline at end of file diff --git a/org.eclipse.jdt.ls.tests/projects/maven/batch/batch9/pom.xml b/org.eclipse.jdt.ls.tests/projects/maven/batch/batch9/pom.xml new file mode 100644 index 0000000000..4e92260848 --- /dev/null +++ b/org.eclipse.jdt.ls.tests/projects/maven/batch/batch9/pom.xml @@ -0,0 +1,11 @@ + + 4.0.0 + + foo.bar + multimodule + 0.0.1-SNAPSHOT + + batch9 + \ No newline at end of file diff --git a/org.eclipse.jdt.ls.tests/projects/maven/batch/pom.xml b/org.eclipse.jdt.ls.tests/projects/maven/batch/pom.xml new file mode 100644 index 0000000000..e10c921d2b --- /dev/null +++ b/org.eclipse.jdt.ls.tests/projects/maven/batch/pom.xml @@ -0,0 +1,22 @@ + + 4.0.0 + foo.bar + batch + 0.0.1-SNAPSHOT + pom + + batch1 + batch2 + batch3 + batch4 + batch5 + batch6 + batch7 + batch8 + batch9 + batch10 + batch11 + + \ No newline at end of file diff --git a/org.eclipse.jdt.ls.tests/src/org/eclipse/jdt/ls/core/internal/managers/MavenBuildSupportTest.java b/org.eclipse.jdt.ls.tests/src/org/eclipse/jdt/ls/core/internal/managers/MavenBuildSupportTest.java index f8268e4666..d5269bb7b7 100644 --- a/org.eclipse.jdt.ls.tests/src/org/eclipse/jdt/ls/core/internal/managers/MavenBuildSupportTest.java +++ b/org.eclipse.jdt.ls.tests/src/org/eclipse/jdt/ls/core/internal/managers/MavenBuildSupportTest.java @@ -28,7 +28,9 @@ import org.apache.commons.io.FileUtils; import org.eclipse.core.resources.IFile; import org.eclipse.core.resources.IProject; +import org.eclipse.core.resources.IWorkspaceRoot; import org.eclipse.core.resources.IncrementalProjectBuilder; +import org.eclipse.core.resources.ResourcesPlugin; import org.eclipse.core.runtime.NullProgressMonitor; import org.eclipse.core.runtime.jobs.IJobChangeEvent; import org.eclipse.core.runtime.jobs.IJobChangeListener; @@ -41,6 +43,7 @@ import org.eclipse.jdt.internal.core.BinaryType; import org.eclipse.jdt.ls.core.internal.DependencyUtil; import org.eclipse.jdt.ls.core.internal.JobHelpers; +import org.eclipse.jdt.ls.core.internal.ProjectUtils; import org.eclipse.jdt.ls.core.internal.SourceContentProvider; import org.eclipse.jdt.ls.core.internal.WorkspaceHelper; import org.eclipse.jdt.ls.core.internal.managers.ProjectsManager.CHANGE_TYPE; @@ -174,6 +177,17 @@ public void testDownloadSources() throws Exception { assertNotNull(source); } + @Test + public void testBatchImport() throws Exception { + IProject project = importMavenProject("batch"); + waitForBackgroundJobs(); + assertTrue(ProjectUtils.isMavenProject(project)); + IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot(); + assertEquals(root.getProjects().length, 14); + project = root.getProject("batchchild"); + assertTrue(ProjectUtils.isMavenProject(project)); + } + protected void testNonStandardCompilerId(String projectName) throws Exception { IProject project = importMavenProject(projectName); assertIsJavaProject(project);