You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ignite.apache.org by sh...@apache.org on 2016/09/07 05:36:16 UTC

ignite git commit: IGNITE-3809: Fix for ArrayIndexOutOfBoundsException in GridUnsafeLru. - Fixes #1017.

Repository: ignite
Updated Branches:
  refs/heads/master 69632c8fa -> 31b9bb84d


IGNITE-3809: Fix for ArrayIndexOutOfBoundsException in GridUnsafeLru. - Fixes #1017.

Signed-off-by: shtykh_roman <rs...@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/master
Commit: 31b9bb84dd29259121759c655bac6247d2b93285
Parents: 69632c8
Author: shtykh_roman <rs...@yahoo.com>
Authored: Wed Sep 7 14:35:31 2016 +0900
Committer: shtykh_roman <rs...@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);