From 725aa82f414b5fcd3cc3f4df230b3ba1bc45489b Mon Sep 17 00:00:00 2001 From: Simon Brandhof Date: Thu, 21 Sep 2017 19:52:00 +0200 Subject: [PATCH] SONAR-9734 Server temp storage grows on Windows due to cache under temp/ce not cleaned up --- .../computation/issue/TrackerRawInputFactory.java | 4 +++- .../source/SourceHashRepositoryImpl.java | 5 +---- .../LoadCrossProjectDuplicationsRepositoryStep.java | 12 ++++++++---- .../step/LoadDuplicationsFromReportStep.java | 5 +---- .../PersistCrossProjectDuplicationIndexStep.java | 5 +---- .../computation/step/PersistFileSourcesStep.java | 13 +++++-------- .../sonar/api/utils/internal/DefaultTempFolder.java | 2 +- 7 files changed, 20 insertions(+), 26 deletions(-) diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/issue/TrackerRawInputFactory.java b/server/sonar-server/src/main/java/org/sonar/server/computation/issue/TrackerRawInputFactory.java index d692dfd0cd3..a54e7f3007c 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/computation/issue/TrackerRawInputFactory.java +++ b/server/sonar-server/src/main/java/org/sonar/server/computation/issue/TrackerRawInputFactory.java @@ -77,7 +77,9 @@ public class TrackerRawInputFactory { protected LineHashSequence loadLineHashSequence() { List lines; if (component.getType() == Component.Type.FILE) { - lines = newArrayList(sourceLinesRepository.readLines(component)); + try (CloseableIterator linesIt = sourceLinesRepository.readLines(component)) { + lines = newArrayList(linesIt); + } } else { lines = Collections.emptyList(); } diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/source/SourceHashRepositoryImpl.java b/server/sonar-server/src/main/java/org/sonar/server/computation/source/SourceHashRepositoryImpl.java index 0f815c35ecd..9915c291bc9 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/computation/source/SourceHashRepositoryImpl.java +++ b/server/sonar-server/src/main/java/org/sonar/server/computation/source/SourceHashRepositoryImpl.java @@ -59,14 +59,11 @@ public class SourceHashRepositoryImpl implements SourceHashRepository { private String computeRawSourceHash(Component file) { SourceHashComputer sourceHashComputer = new SourceHashComputer(); - CloseableIterator linesIterator = sourceLinesRepository.readLines(file); - try { + try (CloseableIterator linesIterator = sourceLinesRepository.readLines(file)) { while (linesIterator.hasNext()) { sourceHashComputer.addLine(linesIterator.next(), linesIterator.hasNext()); } return sourceHashComputer.getHash(); - } finally { - linesIterator.close(); } } diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/step/LoadCrossProjectDuplicationsRepositoryStep.java b/server/sonar-server/src/main/java/org/sonar/server/computation/step/LoadCrossProjectDuplicationsRepositoryStep.java index addd61f5fab..dd9031a418f 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/computation/step/LoadCrossProjectDuplicationsRepositoryStep.java +++ b/server/sonar-server/src/main/java/org/sonar/server/computation/step/LoadCrossProjectDuplicationsRepositoryStep.java @@ -25,6 +25,7 @@ import java.util.List; import javax.annotation.Nonnull; import org.sonar.api.utils.log.Logger; import org.sonar.api.utils.log.Loggers; +import org.sonar.core.util.CloseableIterator; import org.sonar.db.DbClient; import org.sonar.db.DbSession; import org.sonar.db.duplication.DuplicationUnitDto; @@ -95,10 +96,13 @@ public class LoadCrossProjectDuplicationsRepositoryStep implements ComputationSt @Override public void visitFile(Component file) { - List cpdTextBlocks = newArrayList(reportReader.readCpdTextBlocks(file.getReportAttributes().getRef())); - LOGGER.trace("Found {} cpd blocks on file {}", cpdTextBlocks.size(), file.getKey()); - if (cpdTextBlocks.isEmpty()) { - return; + List cpdTextBlocks; + try (CloseableIterator blocksIt = reportReader.readCpdTextBlocks(file.getReportAttributes().getRef())) { + cpdTextBlocks = newArrayList(blocksIt); + LOGGER.trace("Found {} cpd blocks on file {}", cpdTextBlocks.size(), file.getKey()); + if (cpdTextBlocks.isEmpty()) { + return; + } } Collection hashes = from(cpdTextBlocks).transform(CpdTextBlockToHash.INSTANCE).toList(); diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/step/LoadDuplicationsFromReportStep.java b/server/sonar-server/src/main/java/org/sonar/server/computation/step/LoadDuplicationsFromReportStep.java index ba24979f136..863a768496c 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/computation/step/LoadDuplicationsFromReportStep.java +++ b/server/sonar-server/src/main/java/org/sonar/server/computation/step/LoadDuplicationsFromReportStep.java @@ -66,15 +66,12 @@ public class LoadDuplicationsFromReportStep implements ComputationStep { new TypeAwareVisitorAdapter(CrawlerDepthLimit.FILE, POST_ORDER) { @Override public void visitFile(Component file) { - CloseableIterator duplications = batchReportReader.readComponentDuplications(file.getReportAttributes().getRef()); - try { + try (CloseableIterator duplications = batchReportReader.readComponentDuplications(file.getReportAttributes().getRef())) { int idGenerator = 1; while (duplications.hasNext()) { loadDuplications(file, duplications.next(), idGenerator); idGenerator++; } - } finally { - duplications.close(); } } }).visit(treeRootHolder.getRoot()); diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/step/PersistCrossProjectDuplicationIndexStep.java b/server/sonar-server/src/main/java/org/sonar/server/computation/step/PersistCrossProjectDuplicationIndexStep.java index 8a2410363fe..ee6811bf0ad 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/computation/step/PersistCrossProjectDuplicationIndexStep.java +++ b/server/sonar-server/src/main/java/org/sonar/server/computation/step/PersistCrossProjectDuplicationIndexStep.java @@ -88,8 +88,7 @@ public class PersistCrossProjectDuplicationIndexStep implements ComputationStep private void visitComponent(Component component) { int indexInFile = 0; - CloseableIterator blocks = reportReader.readCpdTextBlocks(component.getReportAttributes().getRef()); - try { + try (CloseableIterator blocks = reportReader.readCpdTextBlocks(component.getReportAttributes().getRef())) { while (blocks.hasNext()) { ScannerReport.CpdTextBlock block = blocks.next(); dbClient.duplicationDao().insert( @@ -104,8 +103,6 @@ public class PersistCrossProjectDuplicationIndexStep implements ComputationStep ); indexInFile++; } - } finally { - blocks.close(); } } } diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/step/PersistFileSourcesStep.java b/server/sonar-server/src/main/java/org/sonar/server/computation/step/PersistFileSourcesStep.java index 074546d588f..c247eb19798 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/computation/step/PersistFileSourcesStep.java +++ b/server/sonar-server/src/main/java/org/sonar/server/computation/step/PersistFileSourcesStep.java @@ -124,17 +124,13 @@ public class PersistFileSourcesStep implements ComputationStep { public void visitFile(Component file) { int fileRef = file.getReportAttributes().getRef(); ScannerReport.Component component = reportReader.readComponent(fileRef); - CloseableIterator linesIterator = sourceLinesRepository.readLines(file); - LineReaders lineReaders = new LineReaders(reportReader, scmInfoRepository, duplicationRepository, file); - try { + try (CloseableIterator linesIterator = sourceLinesRepository.readLines(file); + LineReaders lineReaders = new LineReaders(reportReader, scmInfoRepository, duplicationRepository, file)) { ComputeFileSourceData computeFileSourceData = new ComputeFileSourceData(linesIterator, lineReaders.readers(), component.getLines()); ComputeFileSourceData.Data fileSourceData = computeFileSourceData.compute(); persistSource(fileSourceData, file.getUuid(), lineReaders.getLatestChange()); } catch (Exception e) { throw new IllegalStateException(String.format("Cannot persist sources of %s", file.getKey()), e); - } finally { - linesIterator.close(); - lineReaders.close(); } } @@ -190,7 +186,7 @@ public class PersistFileSourcesStep implements ComputationStep { } } - private static class LineReaders { + private static class LineReaders implements AutoCloseable { private final List readers = new ArrayList<>(); private final List> closeables = new ArrayList<>(); @CheckForNull @@ -226,7 +222,8 @@ public class PersistFileSourcesStep implements ComputationStep { return readers; } - void close() { + @Override + public void close() { for (CloseableIterator reportIterator : closeables) { reportIterator.close(); } diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/utils/internal/DefaultTempFolder.java b/sonar-plugin-api/src/main/java/org/sonar/api/utils/internal/DefaultTempFolder.java index f336064f21a..48518a36f04 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/utils/internal/DefaultTempFolder.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/utils/internal/DefaultTempFolder.java @@ -95,7 +95,7 @@ public class DefaultTempFolder implements TempFolder { try { Files.walkFileTree(tempDir.toPath(), DeleteRecursivelyFileVisitor.INSTANCE); } catch (IOException e) { - LOG.trace("Failed to delete temp folder", e); + LOG.error("Failed to delete temp folder", e); } } -- 2.39.5