[ASTERIXDB-3461][STO] Guard CachePage pinCount

- user model changes: no
- storage format changes: no
- interface changes: no

Details:
Guard against decrementing pinCount to
a negative number

Ext-ref: MB-62736
Change-Id: I85196ec1d99259b43c94d6dc9916a2ae042a9a10
Reviewed-on: https://asterix-gerrit.ics.uci.edu/c/asterixdb/+/18490
Integration-Tests: Jenkins <jenkins@fulliautomatix.ics.uci.edu>
Reviewed-by: Michael Blow <mblow@apache.org>
Tested-by: Jenkins <jenkins@fulliautomatix.ics.uci.edu>
diff --git a/hyracks-fullstack/hyracks/hyracks-storage-common/src/main/java/org/apache/hyracks/storage/common/buffercache/BufferCache.java b/hyracks-fullstack/hyracks/hyracks-storage-common/src/main/java/org/apache/hyracks/storage/common/buffercache/BufferCache.java
index 15a019c..4c2395f 100644
--- a/hyracks-fullstack/hyracks/hyracks-storage-common/src/main/java/org/apache/hyracks/storage/common/buffercache/BufferCache.java
+++ b/hyracks-fullstack/hyracks/hyracks-storage-common/src/main/java/org/apache/hyracks/storage/common/buffercache/BufferCache.java
@@ -254,7 +254,7 @@
                     if (DEBUG) {
                         assert !cPage.confiscated.get();
                     }
-                    cPage.pinCount.incrementAndGet();
+                    cPage.incrementAndGetPinCount();
                     return cPage;
                 }
                 cPage = cPage.next;
@@ -311,7 +311,7 @@
                 // now that we have the pin, ensure the victim's dpid still is < 0, if it's not, decrement
                 // pin count and try again
                 if (victim.dpid >= 0) {
-                    victim.pinCount.decrementAndGet();
+                    victim.decrementAndGetPinCount();
                     return null;
                 }
                 if (DEBUG) {
@@ -353,7 +353,7 @@
                 // now that we have the pin, ensure the victim's bucket hasn't changed, if it has, decrement
                 // pin count and try again
                 if (victimHash != hash(victim.dpid)) {
-                    victim.pinCount.decrementAndGet();
+                    victim.decrementAndGetPinCount();
                     return null;
                 }
                 if (DEBUG) {
@@ -397,7 +397,7 @@
                 // now that we have the pin, ensure the victim's bucket hasn't changed, if it has, decrement
                 // pin count and try again
                 if (victimHash != hash(victim.dpid)) {
-                    victim.pinCount.decrementAndGet();
+                    victim.decrementAndGetPinCount();
                     return null;
                 }
                 if (DEBUG && confiscatedPages.contains(victim)) {
@@ -436,8 +436,8 @@
     private CachedPage findTargetInBucket(long dpid, CachedPage cPage, CachedPage victim) {
         while (cPage != null) {
             if (cPage.dpid == dpid) {
-                cPage.pinCount.incrementAndGet();
-                victim.pinCount.decrementAndGet();
+                cPage.incrementAndGetPinCount();
+                victim.decrementAndGetPinCount();
                 if (DEBUG) {
                     assert !cPage.confiscated.get();
                 }
@@ -610,7 +610,7 @@
         }
 
         context.onUnpin(page);
-        int pinCount = ((CachedPage) page).pinCount.decrementAndGet();
+        int pinCount = ((CachedPage) page).decrementAndGetPinCount();
         if (DEBUG && pinCount == 0) {
             pinnedPageOwner.remove(page);
         }
@@ -700,7 +700,7 @@
             }
             if (cleaned) {
                 cPage.dirty.set(false);
-                cPage.pinCount.decrementAndGet();
+                cPage.decrementAndGetPinCount();
                 // this increment of a volatile is OK as there is only one writer
                 cleanedCount++;
                 synchronized (cleanNotification) {
@@ -925,11 +925,11 @@
                     write(cPage, DefaultBufferCacheWriteContext.INSTANCE);
                 }
                 cPage.dirty.set(false);
-                pinCount = cPage.pinCount.decrementAndGet();
+                pinCount = cPage.decrementAndGetPinCount();
             } else {
                 pinCount = cPage.pinCount.get();
             }
-            if (pinCount > 0) {
+            if (pinCount != 0) {
                 throw new IllegalStateException("Page " + BufferedFileHandle.getFileId(cPage.dpid) + ":"
                         + BufferedFileHandle.getPageId(cPage.dpid)
                         + " is pinned and file is being closed. Pincount is: " + pinCount + " Page is confiscated: "
@@ -1081,7 +1081,7 @@
             // now that we have the pin, ensure the victim's dpid still is < 0, if it's not, decrement
             // pin count and try again
             if (victim.dpid >= 0) {
-                victim.pinCount.decrementAndGet();
+                victim.decrementAndGetPinCount();
                 return false;
             }
         } else {
@@ -1096,7 +1096,7 @@
                 // now that we have the pin, ensure the victim's bucket hasn't changed, if it has, decrement
                 // pin count and try again
                 if (pageHash != hash(victim.dpid)) {
-                    victim.pinCount.decrementAndGet();
+                    victim.decrementAndGetPinCount();
                     return false;
                 }
                 // readjust the next pointers to remove this page from
@@ -1198,7 +1198,7 @@
             // now that we have the pin, ensure the victim's dpid still is < 0, if it's not, decrement
             // pin count and try again
             if (victim.dpid >= 0) {
-                victim.pinCount.decrementAndGet();
+                victim.decrementAndGetPinCount();
                 return null;
             }
             returnPage = victim;
@@ -1383,7 +1383,7 @@
                 cPage.valid = true;
                 cPage.next = bucket.cachedPage;
                 bucket.cachedPage = cPage;
-                cPage.pinCount.decrementAndGet();
+                cPage.decrementAndGetPinCount();
                 if (DEBUG) {
                     assert cPage.pinCount.get() == 0;
                     assert cPage.latch.getReadLockCount() == 0;
@@ -1399,7 +1399,7 @@
             }
         } else {
             cPage.invalidate();
-            cPage.pinCount.decrementAndGet();
+            cPage.decrementAndGetPinCount();
             if (DEBUG) {
                 assert cPage.pinCount.get() == 0;
                 assert cPage.latch.getReadLockCount() == 0;
diff --git a/hyracks-fullstack/hyracks/hyracks-storage-common/src/main/java/org/apache/hyracks/storage/common/buffercache/CachedPage.java b/hyracks-fullstack/hyracks/hyracks-storage-common/src/main/java/org/apache/hyracks/storage/common/buffercache/CachedPage.java
index b9d4e5a..88dfaef 100644
--- a/hyracks-fullstack/hyracks/hyracks-storage-common/src/main/java/org/apache/hyracks/storage/common/buffercache/CachedPage.java
+++ b/hyracks-fullstack/hyracks/hyracks-storage-common/src/main/java/org/apache/hyracks/storage/common/buffercache/CachedPage.java
@@ -62,7 +62,19 @@
     }
 
     public int incrementAndGetPinCount() {
-        return pinCount.incrementAndGet();
+        int count = pinCount.incrementAndGet();
+        if (count <= 0) {
+            throw new IllegalStateException("incrementAndGet: Invalid pinCount: " + count + " in page: " + this);
+        }
+        return count;
+    }
+
+    public int decrementAndGetPinCount() {
+        int count = pinCount.decrementAndGet();
+        if (count < 0) {
+            throw new IllegalStateException("decrementAndGet: Invalid pinCount: " + count + " in page: " + this);
+        }
+        return count;
     }
 
     public CachedPage(int cpid, ByteBuffer buffer, IPageReplacementStrategy pageReplacementStrategy) {