HCOLL-231 Use only write locking while we solve a concurrency issue.

This commit is contained in:
Peter Lawrey
2014-12-10 19:06:16 +00:00
parent ae949e406a
commit 628e2c6100
2 changed files with 38 additions and 22 deletions
@@ -34,7 +34,6 @@ import net.openhft.chronicle.hash.serialization.SizeMarshaller;
import net.openhft.chronicle.hash.serialization.internal.*;
import net.openhft.lang.collection.DirectBitSet;
import net.openhft.lang.collection.SingleThreadedDirectBitSet;
import net.openhft.lang.io.*;
import net.openhft.lang.io.serialization.JDKObjectSerializer;
import net.openhft.lang.io.serialization.ObjectSerializer;
import net.openhft.lang.model.DataValueClasses;
@@ -50,6 +49,7 @@ import java.nio.ByteOrder;
import java.nio.channels.FileChannel;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
@@ -1477,7 +1477,7 @@ class VanillaChronicleMap<K, KI, MKI extends MetaBytesInterop<K, ? super KI>,
*/
static final long LOCK_OFFSET = 0L; // 64-bit
static final long SIZE_OFFSET = LOCK_OFFSET + 8L; // 32-bit
ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
final NativeBytes segmentHeader;
final NativeBytes bytes;
final long entriesOffset;
@@ -1566,7 +1566,16 @@ class VanillaChronicleMap<K, KI, MKI extends MetaBytesInterop<K, ? super KI>,
public final ReadLocked<K, KI, MKI, V, VI, MVI> readLock(
@Nullable SegmentState segmentState) {
while (true) {
final boolean success = segmentHeader.tryRWReadLock(LOCK_OFFSET, lockTimeOutNS);
// final boolean success = segmentHeader.tryRWReadLock(LOCK_OFFSET, lockTimeOutNS);
final boolean success = segmentHeader.tryRWWriteLock(LOCK_OFFSET, lockTimeOutNS);
/*
boolean success = false;
try {
success = lock.readLock().tryLock(lockTimeOutNS, TimeUnit.NANOSECONDS);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
*/
if (success) {
if (segmentState != null) {
return segmentState.readLocked(this);
@@ -1592,6 +1601,14 @@ class VanillaChronicleMap<K, KI, MKI extends MetaBytesInterop<K, ? super KI>,
startWriteLock = System.nanoTime();
while (true) {
final boolean success = segmentHeader.tryRWWriteLock(LOCK_OFFSET, lockTimeOutNS);
/*
boolean success = false;
try {
success = lock.writeLock().tryLock(lockTimeOutNS, TimeUnit.NANOSECONDS);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
*/
if (success) {
if (segmentState != null) {
if (isNativeValueClass) {
@@ -1617,7 +1634,9 @@ class VanillaChronicleMap<K, KI, MKI extends MetaBytesInterop<K, ? super KI>,
@Override
public final void readUnlock() {
try {
segmentHeader.unlockRWReadLock(LOCK_OFFSET);
// segmentHeader.unlockRWReadLock(LOCK_OFFSET);
segmentHeader.unlockRWWriteLock(LOCK_OFFSET);
// lock.readLock().unlock();
} catch (IllegalMonitorStateException e) {
errorListener.errorOnUnlock(e);
}
@@ -1627,6 +1646,7 @@ class VanillaChronicleMap<K, KI, MKI extends MetaBytesInterop<K, ? super KI>,
public final void writeUnlock() {
try {
segmentHeader.unlockRWWriteLock(LOCK_OFFSET);
// lock.writeLock().unlock();
} catch (IllegalMonitorStateException e) {
errorListener.errorOnUnlock(e);
}
@@ -26,6 +26,8 @@ import org.junit.Test;
import java.beans.XMLEncoder;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.util.*;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
@@ -40,12 +42,13 @@ import static org.junit.Assert.*;
public class LargeEntriesTest {
static final int ENTRIES = 6400;
static final int ENTRY_SIZE = 1024;
static final int ENTRY_SIZE = 102400;
private static final boolean TESTING = Boolean.getBoolean("testing");
@Test
@Ignore
public void testLargeStrings() throws ExecutionException, InterruptedException {
public void testLargeStrings() throws ExecutionException, InterruptedException, IOException {
File file = File.createTempFile("largeEntries", ".deleteme");
final ChronicleMap<String, String> map = ChronicleMapBuilder
.of(String.class, String.class)
.valueMarshaller(SnappyStringMarshaller.INSTANCE)
@@ -53,7 +56,7 @@ public class LargeEntriesTest {
.entries(ENTRIES * 2)
.entrySize(ENTRY_SIZE / 4)
.putReturnsNull(true)
.create();
.createPersistedTo(file);
{
int threads = 4; //Runtime.getRuntime().availableProcessors();
ExecutorService es = Executors.newFixedThreadPool(threads);
@@ -124,20 +127,14 @@ public class LargeEntriesTest {
SnappyStringMarshaller.INSTANCE.read(bytes);
bytes.release();
}
Object lock = TESTING ? Thread.currentThread() : map;
List<String> keys = new ArrayList<>();
for (int i = start; i < finish; i++) {
String key = "key-" + i;
String object;
synchronized (lock) {
map.put(key, value);
keys.add(key);
for (String s : keys)
assertTrue(s, map.containsKey(s));
object = map.get(key);
assertTrue(key, map.containsKey(key));
}
map.put(key, value);
object = map.get(key);
assertTrue(key, map.containsKey(key));
assertNotNull(key, object);
assertEquals(key, entrySize, object.length());
@@ -147,12 +144,11 @@ public class LargeEntriesTest {
for (int i = start; i < finish; i++) {
// System.out.println(i);
String key = "key-" + i;
synchronized (lock) {
String object = map.get(key);
assertNotNull(key, object);
assertEquals(key, entrySize, object.length());
}
String object = map.get(key);
assertNotNull(key, object);
assertEquals(key, entrySize, object.length());
}
}
}