Skip to content

Commit a28e6dd

Browse files
committed
fix: fix 2330 & Schema Cache expandCapacity concurrent issue
1 parent 869fc81 commit a28e6dd

File tree

3 files changed

+82
-5
lines changed

3 files changed

+82
-5
lines changed

hugegraph-core/src/main/java/org/apache/hugegraph/backend/store/ram/IntObjectMap.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ public final class IntObjectMap<V> implements RamMap {
2929
private static final float DEFAULT_INITIAL_FACTOR = 0.25f;
3030

3131
private final int maxSize;
32-
private int currentSize;
33-
private Object[] array;
32+
private volatile int currentSize;
33+
private volatile Object[] array;
3434

3535
public IntObjectMap(int size) {
3636
this.maxSize = size;
@@ -79,10 +79,11 @@ private synchronized void expandCapacity() {
7979
if (this.currentSize == this.maxSize) {
8080
return;
8181
}
82-
this.currentSize = Math.min(this.currentSize * 2, this.maxSize);
83-
Object[] newArray = new Object[this.currentSize];
82+
int newSize = Math.min(this.currentSize * 2, this.maxSize);
83+
Object[] newArray = new Object[newSize];
8484
System.arraycopy(this.array, 0, newArray, 0, this.array.length);
8585
this.clear();
8686
this.array = newArray;
87+
this.currentSize = newSize;
8788
}
8889
}

hugegraph-test/src/main/java/org/apache/hugegraph/unit/UnitTestSuite.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.apache.hugegraph.unit.rocksdb.RocksDBCountersTest;
2828
import org.apache.hugegraph.unit.rocksdb.RocksDBSessionTest;
2929
import org.apache.hugegraph.unit.rocksdb.RocksDBSessionsTest;
30+
import org.apache.hugegraph.unit.store.RamIntObjectMapTest;
3031
import org.junit.runner.RunWith;
3132
import org.junit.runners.Suite;
3233

@@ -148,7 +149,10 @@
148149
Int2IntsMapTest.class,
149150
IdSetTest.class,
150151
IntMapTest.class,
151-
IntSetTest.class
152+
IntSetTest.class,
153+
154+
/* store */
155+
RamIntObjectMapTest.class
152156
})
153157
public class UnitTestSuite {
154158
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with this
4+
* work for additional information regarding copyright ownership. The ASF
5+
* licenses this file to You under the Apache License, Version 2.0 (the
6+
* "License"); you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14+
* License for the specific language governing permissions and limitations
15+
* under the License.
16+
*/
17+
18+
package org.apache.hugegraph.unit.store;
19+
20+
import java.util.concurrent.CountDownLatch;
21+
22+
import org.apache.hugegraph.backend.store.ram.IntObjectMap;
23+
import org.junit.Assert;
24+
import org.junit.Test;
25+
26+
public class RamIntObjectMapTest {
27+
28+
@Test
29+
public void testConcurrency() {
30+
int size = 32;
31+
IntObjectMap<Integer> map = new IntObjectMap<>(size);
32+
33+
final int numThreads = 10;
34+
final CountDownLatch startSignal = new CountDownLatch(1);
35+
final CountDownLatch doneSignal = new CountDownLatch(numThreads);
36+
37+
for (int i = 0; i < numThreads; i++) {
38+
new Thread(() -> {
39+
try {
40+
startSignal.await();
41+
} catch (InterruptedException e) {
42+
e.printStackTrace();
43+
Assert.fail(e.getMessage());
44+
}
45+
46+
for (int j = 0; j < size; j++) {
47+
map.set(j, j);
48+
}
49+
50+
doneSignal.countDown();
51+
}).start();
52+
}
53+
54+
startSignal.countDown();
55+
56+
try {
57+
doneSignal.await();
58+
} catch (InterruptedException e) {
59+
e.printStackTrace();
60+
Assert.fail(e.getMessage());
61+
}
62+
63+
for (int i = 0; i < numThreads; i++) {
64+
new Thread(() -> {
65+
for (int j = 0; j < size; j++) {
66+
Integer value = map.get(j);
67+
Assert.assertNotNull(value);
68+
}
69+
}).start();
70+
}
71+
}
72+
}

0 commit comments

Comments
 (0)