Fixed timing issue between victimization and cleaner. Fixed early termination of clock. Fixed package names. Added HeapBufferAllocator.

git-svn-id: https://hyracks.googlecode.com/svn/trunk/hyracks@78 123451ca-8445-de46-9d55-352943316053
diff --git a/hyracks-storage-common/src/main/java/edu/uci/ics/hyracks/storage/common/storage/buffercache/BufferCache.java b/hyracks-storage-common/src/main/java/edu/uci/ics/hyracks/storage/common/buffercache/BufferCache.java
similarity index 94%
rename from hyracks-storage-common/src/main/java/edu/uci/ics/hyracks/storage/common/storage/buffercache/BufferCache.java
rename to hyracks-storage-common/src/main/java/edu/uci/ics/hyracks/storage/common/buffercache/BufferCache.java
index 50cd7d8..7a866c4 100644
--- a/hyracks-storage-common/src/main/java/edu/uci/ics/hyracks/storage/common/storage/buffercache/BufferCache.java
+++ b/hyracks-storage-common/src/main/java/edu/uci/ics/hyracks/storage/common/buffercache/BufferCache.java
@@ -12,7 +12,7 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-package edu.uci.ics.hyracks.storage.common.storage.buffercache;
+package edu.uci.ics.hyracks.storage.common.buffercache;
 
 import java.io.IOException;
 import java.nio.ByteBuffer;
@@ -24,8 +24,8 @@
 import java.util.concurrent.locks.ReentrantReadWriteLock;
 
 import edu.uci.ics.hyracks.api.exceptions.HyracksDataException;
-import edu.uci.ics.hyracks.storage.common.storage.file.FileInfo;
-import edu.uci.ics.hyracks.storage.common.storage.file.FileManager;
+import edu.uci.ics.hyracks.storage.common.file.FileInfo;
+import edu.uci.ics.hyracks.storage.common.file.FileManager;
 
 public class BufferCache implements IBufferCacheInternal {
     private static final int MAP_FACTOR = 2;
@@ -39,7 +39,6 @@
     private final IPageReplacementStrategy pageReplacementStrategy;
     private final FileManager fileManager;
     private final CleanerThread cleanerThread;
-    private final Object cleanNotification;
 
     private boolean closed;
 
@@ -59,7 +58,6 @@
         }
         this.pageReplacementStrategy = pageReplacementStrategy;
         this.fileManager = fileManager;
-        cleanNotification = new Object();
         cleanerThread = new CleanerThread();
         cleanerThread.start();
         closed = false;
@@ -106,6 +104,10 @@
 
     private CachedPage findPage(long dpid, boolean newPage) {
         int victimizationTryCount = 0;
+        int localCleanCount = -1;
+        synchronized (cleanerThread.cleanNotification) {
+            localCleanCount = cleanerThread.cleanCount;
+        }
         while (true) {
             CachedPage cPage = null;
             /*
@@ -254,11 +256,13 @@
             synchronized (cleanerThread) {
                 cleanerThread.notifyAll();
             }
-            synchronized (cleanNotification) {
-                try {
-                    cleanNotification.wait(1000);
-                } catch (InterruptedException e) {
-                    // Do nothing
+            synchronized (cleanerThread.cleanNotification) {
+                if (cleanerThread.cleanCount == localCleanCount) {
+                    try {
+                        cleanerThread.cleanNotification.wait(1000);
+                    } catch (InterruptedException e) {
+                        // Do nothing
+                    }
                 }
             }
         }
@@ -393,6 +397,12 @@
     private class CleanerThread extends Thread {
         private boolean shutdownStart = false;
         private boolean shutdownComplete = false;
+        private final Object cleanNotification = new Object();
+        private int cleanCount = 0;
+
+        public CleanerThread() {
+            setPriority(MAX_PRIORITY);
+        }
 
         @Override
         public synchronized void run() {
@@ -413,6 +423,7 @@
                                         cPage.dirty.set(false);
                                         cPage.pinCount.decrementAndGet();
                                         synchronized (cleanNotification) {
+                                            ++cleanCount;
                                             cleanNotification.notifyAll();
                                         }
                                     }
diff --git a/hyracks-storage-common/src/main/java/edu/uci/ics/hyracks/storage/common/storage/buffercache/ClockPageReplacementStrategy.java b/hyracks-storage-common/src/main/java/edu/uci/ics/hyracks/storage/common/buffercache/ClockPageReplacementStrategy.java
similarity index 77%
rename from hyracks-storage-common/src/main/java/edu/uci/ics/hyracks/storage/common/storage/buffercache/ClockPageReplacementStrategy.java
rename to hyracks-storage-common/src/main/java/edu/uci/ics/hyracks/storage/common/buffercache/ClockPageReplacementStrategy.java
index bfdc8bb..f305627 100644
--- a/hyracks-storage-common/src/main/java/edu/uci/ics/hyracks/storage/common/storage/buffercache/ClockPageReplacementStrategy.java
+++ b/hyracks-storage-common/src/main/java/edu/uci/ics/hyracks/storage/common/buffercache/ClockPageReplacementStrategy.java
@@ -12,13 +12,15 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-package edu.uci.ics.hyracks.storage.common.storage.buffercache;
+package edu.uci.ics.hyracks.storage.common.buffercache;
 
 import java.util.concurrent.atomic.AtomicBoolean;
 import java.util.concurrent.locks.Lock;
 import java.util.concurrent.locks.ReentrantLock;
 
 public class ClockPageReplacementStrategy implements IPageReplacementStrategy {
+    private static final int MAX_UNSUCCESSFUL_CYCLE_COUNT = 3;
+
     private final Lock lock;
     private IBufferCacheInternal bufferCache;
     private int clockPtr;
@@ -53,11 +55,17 @@
         lock.lock();
         try {
             int startClockPtr = clockPtr;
+            int cycleCount = 0;
             do {
                 ICachedPageInternal cPage = bufferCache.getPage(clockPtr);
 
                 /*
-                 * We do two things here: 1. If the page has been accessed, then we skip it -- The CAS would return false if the current value is false which makes the page a possible candidate for replacement. 2. We check with the buffer manager if it feels its a good idea to use this page as a victim.
+                 * We do two things here:
+                 * 1. If the page has been accessed, then we skip it -- The CAS would return
+                 * false if the current value is false which makes the page a possible candidate
+                 * for replacement.
+                 * 2. We check with the buffer manager if it feels its a good idea to use this
+                 * page as a victim.
                  */
                 AtomicBoolean accessedFlag = getPerPageObject(cPage);
                 if (!accessedFlag.compareAndSet(true, false)) {
@@ -66,7 +74,10 @@
                     }
                 }
                 clockPtr = (clockPtr + 1) % bufferCache.getNumPages();
-            } while (clockPtr != startClockPtr);
+                if (clockPtr == startClockPtr) {
+                    ++cycleCount;
+                }
+            } while (cycleCount < MAX_UNSUCCESSFUL_CYCLE_COUNT);
         } finally {
             lock.unlock();
         }
diff --git a/hyracks-storage-common/src/main/java/edu/uci/ics/hyracks/storage/common/storage/buffercache/DirectBufferAllocator.java b/hyracks-storage-common/src/main/java/edu/uci/ics/hyracks/storage/common/buffercache/DirectBufferAllocator.java
similarity index 93%
rename from hyracks-storage-common/src/main/java/edu/uci/ics/hyracks/storage/common/storage/buffercache/DirectBufferAllocator.java
rename to hyracks-storage-common/src/main/java/edu/uci/ics/hyracks/storage/common/buffercache/DirectBufferAllocator.java
index a808e59..e016bd9 100644
--- a/hyracks-storage-common/src/main/java/edu/uci/ics/hyracks/storage/common/storage/buffercache/DirectBufferAllocator.java
+++ b/hyracks-storage-common/src/main/java/edu/uci/ics/hyracks/storage/common/buffercache/DirectBufferAllocator.java
@@ -12,7 +12,7 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-package edu.uci.ics.hyracks.storage.common.storage.buffercache;
+package edu.uci.ics.hyracks.storage.common.buffercache;
 
 import java.nio.ByteBuffer;
 
diff --git a/hyracks-storage-common/src/main/java/edu/uci/ics/hyracks/storage/common/storage/buffercache/DirectBufferAllocator.java b/hyracks-storage-common/src/main/java/edu/uci/ics/hyracks/storage/common/buffercache/HeapBufferAllocator.java
similarity index 81%
copy from hyracks-storage-common/src/main/java/edu/uci/ics/hyracks/storage/common/storage/buffercache/DirectBufferAllocator.java
copy to hyracks-storage-common/src/main/java/edu/uci/ics/hyracks/storage/common/buffercache/HeapBufferAllocator.java
index a808e59..294a4e8 100644
--- a/hyracks-storage-common/src/main/java/edu/uci/ics/hyracks/storage/common/storage/buffercache/DirectBufferAllocator.java
+++ b/hyracks-storage-common/src/main/java/edu/uci/ics/hyracks/storage/common/buffercache/HeapBufferAllocator.java
@@ -12,16 +12,16 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-package edu.uci.ics.hyracks.storage.common.storage.buffercache;
+package edu.uci.ics.hyracks.storage.common.buffercache;
 
 import java.nio.ByteBuffer;
 
-public class DirectBufferAllocator implements ICacheMemoryAllocator {
+public class HeapBufferAllocator implements ICacheMemoryAllocator {
     @Override
     public ByteBuffer[] allocate(int pageSize, int numPages) {
         ByteBuffer[] buffers = new ByteBuffer[numPages];
         for (int i = 0; i < numPages; ++i) {
-            buffers[i] = ByteBuffer.allocateDirect(pageSize);
+            buffers[i] = ByteBuffer.allocate(pageSize);
         }
         return buffers;
     }
diff --git a/hyracks-storage-common/src/main/java/edu/uci/ics/hyracks/storage/common/storage/buffercache/IBufferCache.java b/hyracks-storage-common/src/main/java/edu/uci/ics/hyracks/storage/common/buffercache/IBufferCache.java
similarity index 93%
rename from hyracks-storage-common/src/main/java/edu/uci/ics/hyracks/storage/common/storage/buffercache/IBufferCache.java
rename to hyracks-storage-common/src/main/java/edu/uci/ics/hyracks/storage/common/buffercache/IBufferCache.java
index 759e4d0..34bfc2f 100644
--- a/hyracks-storage-common/src/main/java/edu/uci/ics/hyracks/storage/common/storage/buffercache/IBufferCache.java
+++ b/hyracks-storage-common/src/main/java/edu/uci/ics/hyracks/storage/common/buffercache/IBufferCache.java
@@ -12,7 +12,7 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-package edu.uci.ics.hyracks.storage.common.storage.buffercache;
+package edu.uci.ics.hyracks.storage.common.buffercache;
 
 import edu.uci.ics.hyracks.api.exceptions.HyracksDataException;
 
diff --git a/hyracks-storage-common/src/main/java/edu/uci/ics/hyracks/storage/common/storage/buffercache/IBufferCacheInternal.java b/hyracks-storage-common/src/main/java/edu/uci/ics/hyracks/storage/common/buffercache/IBufferCacheInternal.java
similarity index 91%
rename from hyracks-storage-common/src/main/java/edu/uci/ics/hyracks/storage/common/storage/buffercache/IBufferCacheInternal.java
rename to hyracks-storage-common/src/main/java/edu/uci/ics/hyracks/storage/common/buffercache/IBufferCacheInternal.java
index a4a31a7..98e8407 100644
--- a/hyracks-storage-common/src/main/java/edu/uci/ics/hyracks/storage/common/storage/buffercache/IBufferCacheInternal.java
+++ b/hyracks-storage-common/src/main/java/edu/uci/ics/hyracks/storage/common/buffercache/IBufferCacheInternal.java
@@ -12,7 +12,7 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-package edu.uci.ics.hyracks.storage.common.storage.buffercache;
+package edu.uci.ics.hyracks.storage.common.buffercache;
 
 public interface IBufferCacheInternal extends IBufferCache {
     public ICachedPageInternal getPage(int cpid);
diff --git a/hyracks-storage-common/src/main/java/edu/uci/ics/hyracks/storage/common/storage/buffercache/ICacheMemoryAllocator.java b/hyracks-storage-common/src/main/java/edu/uci/ics/hyracks/storage/common/buffercache/ICacheMemoryAllocator.java
similarity index 92%
rename from hyracks-storage-common/src/main/java/edu/uci/ics/hyracks/storage/common/storage/buffercache/ICacheMemoryAllocator.java
rename to hyracks-storage-common/src/main/java/edu/uci/ics/hyracks/storage/common/buffercache/ICacheMemoryAllocator.java
index 4f562ab..4cdb895 100644
--- a/hyracks-storage-common/src/main/java/edu/uci/ics/hyracks/storage/common/storage/buffercache/ICacheMemoryAllocator.java
+++ b/hyracks-storage-common/src/main/java/edu/uci/ics/hyracks/storage/common/buffercache/ICacheMemoryAllocator.java
@@ -12,7 +12,7 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-package edu.uci.ics.hyracks.storage.common.storage.buffercache;
+package edu.uci.ics.hyracks.storage.common.buffercache;
 
 import java.nio.ByteBuffer;
 
diff --git a/hyracks-storage-common/src/main/java/edu/uci/ics/hyracks/storage/common/storage/buffercache/ICachedPage.java b/hyracks-storage-common/src/main/java/edu/uci/ics/hyracks/storage/common/buffercache/ICachedPage.java
similarity index 93%
rename from hyracks-storage-common/src/main/java/edu/uci/ics/hyracks/storage/common/storage/buffercache/ICachedPage.java
rename to hyracks-storage-common/src/main/java/edu/uci/ics/hyracks/storage/common/buffercache/ICachedPage.java
index 71db7a4..98c9f21 100644
--- a/hyracks-storage-common/src/main/java/edu/uci/ics/hyracks/storage/common/storage/buffercache/ICachedPage.java
+++ b/hyracks-storage-common/src/main/java/edu/uci/ics/hyracks/storage/common/buffercache/ICachedPage.java
@@ -12,7 +12,7 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-package edu.uci.ics.hyracks.storage.common.storage.buffercache;
+package edu.uci.ics.hyracks.storage.common.buffercache;
 
 import java.nio.ByteBuffer;
 
diff --git a/hyracks-storage-common/src/main/java/edu/uci/ics/hyracks/storage/common/storage/buffercache/ICachedPageInternal.java b/hyracks-storage-common/src/main/java/edu/uci/ics/hyracks/storage/common/buffercache/ICachedPageInternal.java
similarity index 92%
rename from hyracks-storage-common/src/main/java/edu/uci/ics/hyracks/storage/common/storage/buffercache/ICachedPageInternal.java
rename to hyracks-storage-common/src/main/java/edu/uci/ics/hyracks/storage/common/buffercache/ICachedPageInternal.java
index 933922d..baaa8f8 100644
--- a/hyracks-storage-common/src/main/java/edu/uci/ics/hyracks/storage/common/storage/buffercache/ICachedPageInternal.java
+++ b/hyracks-storage-common/src/main/java/edu/uci/ics/hyracks/storage/common/buffercache/ICachedPageInternal.java
@@ -12,7 +12,7 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-package edu.uci.ics.hyracks.storage.common.storage.buffercache;
+package edu.uci.ics.hyracks.storage.common.buffercache;
 
 public interface ICachedPageInternal extends ICachedPage {
     public int getCachedPageId();
diff --git a/hyracks-storage-common/src/main/java/edu/uci/ics/hyracks/storage/common/storage/buffercache/IPageReplacementStrategy.java b/hyracks-storage-common/src/main/java/edu/uci/ics/hyracks/storage/common/buffercache/IPageReplacementStrategy.java
similarity index 93%
rename from hyracks-storage-common/src/main/java/edu/uci/ics/hyracks/storage/common/storage/buffercache/IPageReplacementStrategy.java
rename to hyracks-storage-common/src/main/java/edu/uci/ics/hyracks/storage/common/buffercache/IPageReplacementStrategy.java
index e1b38a3..e723cf6 100644
--- a/hyracks-storage-common/src/main/java/edu/uci/ics/hyracks/storage/common/storage/buffercache/IPageReplacementStrategy.java
+++ b/hyracks-storage-common/src/main/java/edu/uci/ics/hyracks/storage/common/buffercache/IPageReplacementStrategy.java
@@ -12,7 +12,7 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-package edu.uci.ics.hyracks.storage.common.storage.buffercache;
+package edu.uci.ics.hyracks.storage.common.buffercache;
 
 public interface IPageReplacementStrategy {
     public Object createPerPageStrategyObject(int cpid);
diff --git a/hyracks-storage-common/src/main/java/edu/uci/ics/hyracks/storage/common/storage/file/FileInfo.java b/hyracks-storage-common/src/main/java/edu/uci/ics/hyracks/storage/common/file/FileInfo.java
similarity index 96%
rename from hyracks-storage-common/src/main/java/edu/uci/ics/hyracks/storage/common/storage/file/FileInfo.java
rename to hyracks-storage-common/src/main/java/edu/uci/ics/hyracks/storage/common/file/FileInfo.java
index aa2ae79..9b9270d 100644
--- a/hyracks-storage-common/src/main/java/edu/uci/ics/hyracks/storage/common/storage/file/FileInfo.java
+++ b/hyracks-storage-common/src/main/java/edu/uci/ics/hyracks/storage/common/file/FileInfo.java
@@ -12,7 +12,7 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-package edu.uci.ics.hyracks.storage.common.storage.file;
+package edu.uci.ics.hyracks.storage.common.file;
 
 import java.io.RandomAccessFile;
 import java.nio.channels.FileChannel;
diff --git a/hyracks-storage-common/src/main/java/edu/uci/ics/hyracks/storage/common/storage/file/FileManager.java b/hyracks-storage-common/src/main/java/edu/uci/ics/hyracks/storage/common/file/FileManager.java
similarity index 97%
rename from hyracks-storage-common/src/main/java/edu/uci/ics/hyracks/storage/common/storage/file/FileManager.java
rename to hyracks-storage-common/src/main/java/edu/uci/ics/hyracks/storage/common/file/FileManager.java
index 226260a..abdf9d3 100644
--- a/hyracks-storage-common/src/main/java/edu/uci/ics/hyracks/storage/common/storage/file/FileManager.java
+++ b/hyracks-storage-common/src/main/java/edu/uci/ics/hyracks/storage/common/file/FileManager.java
@@ -12,7 +12,7 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-package edu.uci.ics.hyracks.storage.common.storage.file;
+package edu.uci.ics.hyracks.storage.common.file;
 
 import java.io.IOException;
 import java.util.HashMap;
diff --git a/hyracks-storage-common/src/main/java/edu/uci/ics/hyracks/storage/common/storage/sync/LatchType.java b/hyracks-storage-common/src/main/java/edu/uci/ics/hyracks/storage/common/sync/LatchType.java
similarity index 92%
rename from hyracks-storage-common/src/main/java/edu/uci/ics/hyracks/storage/common/storage/sync/LatchType.java
rename to hyracks-storage-common/src/main/java/edu/uci/ics/hyracks/storage/common/sync/LatchType.java
index b0cfdcf..bcdcac1 100644
--- a/hyracks-storage-common/src/main/java/edu/uci/ics/hyracks/storage/common/storage/sync/LatchType.java
+++ b/hyracks-storage-common/src/main/java/edu/uci/ics/hyracks/storage/common/sync/LatchType.java
@@ -12,7 +12,7 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-package edu.uci.ics.hyracks.storage.common.storage.sync;
+package edu.uci.ics.hyracks.storage.common.sync;
 
 public enum LatchType {
     LATCH_X,