From 89a1d93efdb4ee18b4d520328e8a33f4966996ed Mon Sep 17 00:00:00 2001 From: Maxim Shafirov Date: Mon, 18 Jul 2011 16:02:13 +0400 Subject: [PATCH] Avoid having many flushes running at the same time. --- .../util/indexing/FileBasedIndex.java | 47 +++++++++++-------- .../vfs/newvfs/persistent/FSRecords.java | 22 ++++----- 2 files changed, 37 insertions(+), 32 deletions(-) diff --git a/platform/lang-impl/src/com/intellij/util/indexing/FileBasedIndex.java b/platform/lang-impl/src/com/intellij/util/indexing/FileBasedIndex.java index 3110ce6cafa3..0d69ad3a05e5 100644 --- a/platform/lang-impl/src/com/intellij/util/indexing/FileBasedIndex.java +++ b/platform/lang-impl/src/com/intellij/util/indexing/FileBasedIndex.java @@ -316,14 +316,14 @@ public class FileBasedIndex implements ApplicationComponent { myFlushingFuture = JobScheduler.getScheduler().scheduleAtFixedRate(new Runnable() { int lastModCount = 0; public void run() { - ApplicationManager.getApplication().executeOnPooledThread(new Runnable() { - public void run() { - if (lastModCount == myLocalModCount && !HeavyProcessLatch.INSTANCE.isRunning()) { + if (lastModCount == myLocalModCount) { + ApplicationManager.getApplication().executeOnPooledThread(new Runnable() { + public void run() { flushAllIndices(); } - lastModCount = myLocalModCount; - } - }); + }); + lastModCount = myLocalModCount; + } } }, 5000, 5000, TimeUnit.MILLISECONDS); @@ -621,22 +621,31 @@ public class FileBasedIndex implements ApplicationComponent { } } + private volatile boolean flushRunning = false; private void flushAllIndices() { - IndexingStamp.flushCache(); - for (ID indexId : new ArrayList>(myIndices.keySet())) { - if (HeavyProcessLatch.INSTANCE.isRunning()) { - return; - } - try { - final UpdatableIndex index = getIndex(indexId); - if (index != null) { - index.flush(); + if (flushRunning || HeavyProcessLatch.INSTANCE.isRunning()) return; + flushRunning = true; + + try { + IndexingStamp.flushCache(); + for (ID indexId : new ArrayList>(myIndices.keySet())) { + if (HeavyProcessLatch.INSTANCE.isRunning()) { + return; + } + try { + final UpdatableIndex index = getIndex(indexId); + if (index != null) { + index.flush(); + } + } + catch (StorageException e) { + LOG.info(e); + requestRebuild(indexId); } } - catch (StorageException e) { - LOG.info(e); - requestRebuild(indexId); - } + } + finally { + flushRunning = false; } } diff --git a/platform/platform-impl/src/com/intellij/openapi/vfs/newvfs/persistent/FSRecords.java b/platform/platform-impl/src/com/intellij/openapi/vfs/newvfs/persistent/FSRecords.java index 2b123c2f88c8..510f34b50677 100644 --- a/platform/platform-impl/src/com/intellij/openapi/vfs/newvfs/persistent/FSRecords.java +++ b/platform/platform-impl/src/com/intellij/openapi/vfs/newvfs/persistent/FSRecords.java @@ -309,14 +309,14 @@ public class FSRecords implements Forceable { int lastModCount = 0; public void run() { - ApplicationManager.getApplication().executeOnPooledThread(new Runnable() { - public void run() { - if (lastModCount == ourLocalModificationCount && !HeavyProcessLatch.INSTANCE.isRunning()) { + if (lastModCount == ourLocalModificationCount) { + ApplicationManager.getApplication().executeOnPooledThread(new Runnable() { + public void run() { flushSome(); } - lastModCount = ourLocalModificationCount; - } - }); + }); + } + lastModCount = ourLocalModificationCount; } }, 5000, 5000, TimeUnit.MILLISECONDS); } @@ -336,6 +336,8 @@ public class FSRecords implements Forceable { } public static void flushSome() { + if (!isDirty() || HeavyProcessLatch.INSTANCE.isRunning()) return; + synchronized (lock) { if (myFlushingFuture == null) { return; // avoid NPE when close has already taken place @@ -508,7 +510,6 @@ public class FSRecords implements Forceable { public static void deleteRecordRecursively(int id) { synchronized (lock) { try { - DbConnection.markDirty(); incModCount(id); doDeleteRecursively(id); } @@ -764,6 +765,7 @@ public class FSRecords implements Forceable { } private static void incModCount(int id) { + DbConnection.markDirty(); ourLocalModificationCount++; final int count = getModCount() + 1; getRecords().putInt(HEADER_GLOBAL_MODCOUNT_OFFSET, count); @@ -810,7 +812,6 @@ public class FSRecords implements Forceable { synchronized (lock) { try { - DbConnection.markDirty(); incModCount(id); putRecordInt(id, PARENT_OFFSET, parent); } @@ -835,7 +836,6 @@ public class FSRecords implements Forceable { public static void setName(int id, String name) { synchronized (lock) { try { - DbConnection.markDirty(); incModCount(id); putRecordInt(id, NAME_OFFSET, getNames().enumerate(name)); } @@ -855,7 +855,6 @@ public class FSRecords implements Forceable { synchronized (lock) { try { if (markAsChange) { - DbConnection.markDirty(); incModCount(id); } putRecordInt(id, FLAGS_OFFSET, flags); @@ -875,7 +874,6 @@ public class FSRecords implements Forceable { public static void setLength(int id, long len) { synchronized (lock) { try { - DbConnection.markDirty(); incModCount(id); getRecords().putLong(getOffset(id, LENGTH_OFFSET), len); } @@ -894,7 +892,6 @@ public class FSRecords implements Forceable { public static void setTimestamp(int id, long value) { synchronized (lock) { try { - DbConnection.markDirty(); incModCount(id); getRecords().putLong(getOffset(id, TIMESTAMP_OFFSET), value); } @@ -1176,7 +1173,6 @@ public class FSRecords implements Forceable { public void writeBytes(ByteSequence bytes, int fileId) throws IOException { final int page; synchronized (lock) { - DbConnection.markDirty(); incModCount(fileId); page = findOrCreatePage(); }