ignite-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sboi...@apache.org
Subject [48/50] [abbrv] ignite git commit: IGNITE-3809: Fix for ArrayIndexOutOfBoundsException in GridUnsafeLru. - Fixes #1017.
Date Thu, 08 Sep 2016 15:41:30 GMT
IGNITE-3809: Fix for ArrayIndexOutOfBoundsException in GridUnsafeLru. - Fixes #1017.

Signed-off-by: shtykh_roman <rshtykh@yahoo.com>


Project: http://git-wip-us.apache.org/repos/asf/ignite/repo
Commit: http://git-wip-us.apache.org/repos/asf/ignite/commit/31b9bb84
Tree: http://git-wip-us.apache.org/repos/asf/ignite/tree/31b9bb84
Diff: http://git-wip-us.apache.org/repos/asf/ignite/diff/31b9bb84

Branch: refs/heads/ignite-961
Commit: 31b9bb84dd29259121759c655bac6247d2b93285
Parents: 69632c8
Author: shtykh_roman <rshtykh@yahoo.com>
Authored: Wed Sep 7 14:35:31 2016 +0900
Committer: shtykh_roman <rshtykh@yahoo.com>
Committed: Wed Sep 7 14:35:31 2016 +0900

----------------------------------------------------------------------
 .../util/offheap/unsafe/GridUnsafeLru.java      | 30 +++++++++++++++++---
 1 file changed, 26 insertions(+), 4 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ignite/blob/31b9bb84/modules/core/src/main/java/org/apache/ignite/internal/util/offheap/unsafe/GridUnsafeLru.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/util/offheap/unsafe/GridUnsafeLru.java
b/modules/core/src/main/java/org/apache/ignite/internal/util/offheap/unsafe/GridUnsafeLru.java
index aaff4f9..ea65217 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/util/offheap/unsafe/GridUnsafeLru.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/util/offheap/unsafe/GridUnsafeLru.java
@@ -28,8 +28,7 @@ import org.apache.ignite.internal.util.typedef.internal.S;
 /**
  * Striped LRU queue.
  */
-@SuppressWarnings("ForLoopReplaceableByForEach")
-class GridUnsafeLru {
+@SuppressWarnings("ForLoopReplaceableByForEach") class GridUnsafeLru {
     /** Number of stripes. */
     private final short cnt;
 
@@ -47,6 +46,9 @@ class GridUnsafeLru {
     /** Current round-robin remove stripe index. */
     private final AtomicInteger rmvIdx;
 
+    /** Max stripe index count. */
+    private final int maxIdxCnt;
+
     /** Released flag. */
     private AtomicBoolean released = new AtomicBoolean(false);
 
@@ -68,6 +70,8 @@ class GridUnsafeLru {
 
         addIdx = new AtomicInteger();
         rmvIdx = new AtomicInteger(cnt / 2);
+
+        maxIdxCnt = cnt - 1;
     }
 
     /**
@@ -156,7 +160,7 @@ class GridUnsafeLru {
      * @throws GridOffHeapOutOfMemoryException If failed.
      */
     long offer(int part, long addr, int hash) throws GridOffHeapOutOfMemoryException {
-        return lrus[addIdx.getAndIncrement() % cnt].offer(part, addr, hash);
+        return lrus[incrementAndGet(addIdx, maxIdxCnt)].offer(part, addr, hash);
     }
 
     /**
@@ -165,7 +169,7 @@ class GridUnsafeLru {
      * @return Queue node address.
      */
     long prePoll() {
-        int idx = rmvIdx.getAndIncrement();
+        int idx = incrementAndGet(rmvIdx, maxIdxCnt);
 
         // Must try to poll from each LRU.
         for (int i = 0; i < lrus.length; i++) {
@@ -180,6 +184,7 @@ class GridUnsafeLru {
 
     /**
      * Removes polling node from the queue.
+     *
      * @param qAddr Queue node address.
      */
     void poll(long qAddr) {
@@ -215,6 +220,23 @@ class GridUnsafeLru {
         }
     }
 
+    /**
+     * Atomically increments the given value by one, re-starting from 0 when the specified
maximum is reached.
+     *
+     * @param value Value to increment.
+     * @param max Maximum after reaching which the value is reset to 0.
+     * @return Incremented value.
+     */
+    private int incrementAndGet(AtomicInteger value, int max) {
+        while (true) {
+            int cur = value.get();
+            int next = cur == max ? 0 : cur + 1;
+
+            if (value.compareAndSet(cur, next))
+                return next;
+        }
+    }
+
     /** {@inheritDoc} */
     @Override public String toString() {
         return S.toString(GridUnsafeLru.class, this);


Mime
View raw message