Return-Path: X-Original-To: apmail-geode-commits-archive@minotaur.apache.org Delivered-To: apmail-geode-commits-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 12C701874F for ; Wed, 17 Feb 2016 23:10:48 +0000 (UTC) Received: (qmail 62016 invoked by uid 500); 17 Feb 2016 23:10:41 -0000 Delivered-To: apmail-geode-commits-archive@geode.apache.org Received: (qmail 61972 invoked by uid 500); 17 Feb 2016 23:10:41 -0000 Mailing-List: contact commits-help@geode.incubator.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@geode.incubator.apache.org Delivered-To: mailing list commits@geode.incubator.apache.org Received: (qmail 61902 invoked by uid 99); 17 Feb 2016 23:10:41 -0000 Received: from pnap-us-west-generic-nat.apache.org (HELO spamd1-us-west.apache.org) (209.188.14.142) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 17 Feb 2016 23:10:41 +0000 Received: from localhost (localhost [127.0.0.1]) by spamd1-us-west.apache.org (ASF Mail Server at spamd1-us-west.apache.org) with ESMTP id 52BEFC0931 for ; Wed, 17 Feb 2016 23:10:41 +0000 (UTC) X-Virus-Scanned: Debian amavisd-new at spamd1-us-west.apache.org X-Spam-Flag: NO X-Spam-Score: -3.226 X-Spam-Level: X-Spam-Status: No, score=-3.226 tagged_above=-999 required=6.31 tests=[KAM_ASCII_DIVIDERS=0.8, KAM_LAZY_DOMAIN_SECURITY=1, RCVD_IN_DNSWL_HI=-5, RCVD_IN_MSPIKE_H3=-0.01, RCVD_IN_MSPIKE_WL=-0.01, RP_MATCHES_RCVD=-0.006] autolearn=disabled Received: from mx1-lw-eu.apache.org ([10.40.0.8]) by localhost (spamd1-us-west.apache.org [10.40.0.7]) (amavisd-new, port 10024) with ESMTP id aa5UPpAHOJbT for ; Wed, 17 Feb 2016 23:10:40 +0000 (UTC) Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by mx1-lw-eu.apache.org (ASF Mail Server at mx1-lw-eu.apache.org) with SMTP id 0BEC55F21F for ; Wed, 17 Feb 2016 23:10:37 +0000 (UTC) Received: (qmail 60666 invoked by uid 99); 17 Feb 2016 23:10:37 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 17 Feb 2016 23:10:37 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id 11A4DE1120; Wed, 17 Feb 2016 23:10:37 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: dschneider@apache.org To: commits@geode.incubator.apache.org Date: Wed, 17 Feb 2016 23:11:02 -0000 Message-Id: <713762f5640b4f399712a912c3c26cd7@git.apache.org> In-Reply-To: <484c95b0b65e445185b7394e18c15e95@git.apache.org> References: <484c95b0b65e445185b7394e18c15e95@git.apache.org> X-Mailer: ASF-Git Admin Mailer Subject: [27/33] incubator-geode git commit: Fix for GEODE-106 Invalidate operation fails with IndexMaintenanceException with underlying java.lang.ArrayIndexOutOfBoundsException. Fix for GEODE-106 Invalidate operation fails with IndexMaintenanceException with underlying java.lang.ArrayIndexOutOfBoundsException. Project: http://git-wip-us.apache.org/repos/asf/incubator-geode/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-geode/commit/ef944168 Tree: http://git-wip-us.apache.org/repos/asf/incubator-geode/tree/ef944168 Diff: http://git-wip-us.apache.org/repos/asf/incubator-geode/diff/ef944168 Branch: refs/heads/feature/GEODE-831 Commit: ef944168056cd7394085c1ef8d7465d75bcdc031 Parents: 06317e7 Author: Anil Authored: Thu Feb 11 17:10:51 2016 -0800 Committer: Anil Committed: Tue Feb 16 18:51:34 2016 -0800 ---------------------------------------------------------------------- .../query/internal/index/IndexElemArray.java | 94 +++++++++++++------- 1 file changed, 61 insertions(+), 33 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/ef944168/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/index/IndexElemArray.java ---------------------------------------------------------------------- diff --git a/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/index/IndexElemArray.java b/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/index/IndexElemArray.java index b94f975..de694a4 100644 --- a/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/index/IndexElemArray.java +++ b/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/index/IndexElemArray.java @@ -33,6 +33,9 @@ public class IndexElemArray implements Iterable, Collection { private Object[] elementData; private volatile byte size; + /* lock for making size and data changes atomically. */ + private Object lock = new Object(); + public IndexElemArray(int initialCapacity) { if (initialCapacity < 0) { throw new IllegalArgumentException("Illegal Capacity: " + initialCapacity); @@ -111,14 +114,16 @@ public class IndexElemArray implements Iterable, Collection { * or -1 if there is no such index. */ public int indexOf(Object o) { - if (o == null) { - for (int i = 0; i < size; i++) - if (elementData[i] == null) - return i; - } else { - for (int i = 0; i < size; i++) - if (o.equals(elementData[i])) - return i; + synchronized (lock) { + if (o == null) { + for (int i = 0; i < size; i++) + if (elementData[i] == null) + return i; + } else { + for (int i = 0; i < size; i++) + if (o.equals(elementData[i])) + return i; + } } return -1; } @@ -133,8 +138,10 @@ public class IndexElemArray implements Iterable, Collection { * */ public Object get(int index) { - RangeCheck(index); - return elementData[index]; + synchronized (lock) { + RangeCheck(index); + return elementData[index]; + } } /** @@ -150,11 +157,13 @@ public class IndexElemArray implements Iterable, Collection { * */ public Object set(int index, Object element) { - RangeCheck(index); + synchronized (lock) { + RangeCheck(index); - Object oldValue = (Object) elementData[index]; - elementData[index] = element; - return oldValue; + Object oldValue = (Object) elementData[index]; + elementData[index] = element; + return oldValue; + } } /** @@ -167,10 +176,12 @@ public class IndexElemArray implements Iterable, Collection { * @return true (as specified by {@link Collection#add}) * @throws ArrayIndexOutOfBoundsException */ - public synchronized boolean add(Object e) { - ensureCapacity(size + 1); - elementData[size] = e; - ++size; + public boolean add(Object e) { + synchronized (lock) { + ensureCapacity(size + 1); + elementData[size] = e; + ++size; + } return true; } @@ -187,7 +198,7 @@ public class IndexElemArray implements Iterable, Collection { * element to be removed from this list, if present * @return true if this list contained the specified element */ - public synchronized boolean remove(Object o) { + public boolean remove(Object o) { if (o == null) { for (int index = 0; index < size; index++) if (elementData[index] == null) { @@ -215,8 +226,11 @@ public class IndexElemArray implements Iterable, Collection { int numMoved = len - index - 1; if (numMoved > 0) System.arraycopy(elementData, index + 1, newArray, index, numMoved); - elementData = newArray; - --size; + + synchronized (lock) { + elementData = newArray; + --size; + } } /** @@ -225,10 +239,12 @@ public class IndexElemArray implements Iterable, Collection { */ public void clear() { // Let gc do its work - for (int i = 0; i < size; i++) { - elementData[i] = null; + synchronized (lock) { + for (int i = 0; i < size; i++) { + elementData[i] = null; + } + size = 0; } - size = 0; } /** @@ -244,12 +260,14 @@ public class IndexElemArray implements Iterable, Collection { } @Override - public synchronized boolean addAll(Collection c) { + public boolean addAll(Collection c) { Object[] a = c.toArray(); int numNew = a.length; - ensureCapacity(size + numNew); - System.arraycopy(a, 0, elementData, size, numNew); - size += numNew; + synchronized (lock) { + ensureCapacity(size + numNew); + System.arraycopy(a, 0, elementData, size, numNew); + size += numNew; + } return numNew != 0; } @@ -266,6 +284,15 @@ public class IndexElemArray implements Iterable, Collection { private class IndexArrayListIterator implements Iterator { private byte current; private Object currentEntry; + private Object[] elements; + private int len; + + IndexArrayListIterator() { + synchronized (lock) { + elements = elementData; + len = size; + } + } /** * Checks if the array has next element, stores reference to the current @@ -275,7 +302,7 @@ public class IndexElemArray implements Iterable, Collection { */ @Override public boolean hasNext() { - return current < size; + return current < len; } /** @@ -285,11 +312,12 @@ public class IndexElemArray implements Iterable, Collection { @Override public Object next() { try { - currentEntry = elementData[current++]; + currentEntry = elements[current++]; } catch (IndexOutOfBoundsException e) { - // Following exception must never be thrown. - //throw new NoSuchElementException(); - return null; + // We should not be coming here as element-data and + // size are updated atomically. + throw new NoSuchElementException(); + //return null; } return currentEntry; }