From a97439a3b0f5264e7c0c3ca56b284c2b3c1bf909 Mon Sep 17 00:00:00 2001 From: cotton-ben Date: Sat, 4 Jul 2020 18:07:38 -0400 Subject: [PATCH] refactored reference impl: off-heap ChronicleStampedLock.java --- .../revelations/ChronicleStampedLock.java | 37 +++++++++++++++---- .../ChronicleStampedLockVOInterface.java | 31 ++++++++++++++++ .../acid/revelations/DirtyReadOffender.java | 4 +- .../acid/revelations/DirtyReadTolerance.java | 16 ++++++++ .../acid/revelations/DirtyReadVictim.java | 11 ++++-- 5 files changed, 87 insertions(+), 12 deletions(-) create mode 100644 src/test/java/net/openhft/chronicle/map/fromdocs/acid/revelations/ChronicleStampedLockVOInterface.java diff --git a/src/test/java/net/openhft/chronicle/map/fromdocs/acid/revelations/ChronicleStampedLock.java b/src/test/java/net/openhft/chronicle/map/fromdocs/acid/revelations/ChronicleStampedLock.java index 9f2059899..5c8ee5c08 100644 --- a/src/test/java/net/openhft/chronicle/map/fromdocs/acid/revelations/ChronicleStampedLock.java +++ b/src/test/java/net/openhft/chronicle/map/fromdocs/acid/revelations/ChronicleStampedLock.java @@ -1,13 +1,16 @@ package net.openhft.chronicle.map.fromdocs.acid.revelations; +import net.openhft.affinity.AffinitySupport; import net.openhft.chronicle.map.ChronicleMap; -import net.openhft.chronicle.map.fromdocs.BondVOInterface; +import net.openhft.chronicle.map.ChronicleMapBuilder; +import java.io.File; import java.io.IOException; import java.util.concurrent.locks.StampedLock; import static net.openhft.chronicle.values.Values.newNativeReference; + /** * ben.cotton@rutgers.edu *

@@ -15,14 +18,16 @@ import static net.openhft.chronicle.values.Values.newNativeReference; */ public class ChronicleStampedLock extends StampedLock { - ChronicleMap chm; - BondVOInterface offHeapLock = newNativeReference(BondVOInterface.class); - BondVOInterface lastWriterT = newNativeReference(BondVOInterface.class); - { + ChronicleMap chm; + ChronicleStampedLockVOInterface offHeapLock = + newNativeReference(ChronicleStampedLockVOInterface.class); + ChronicleStampedLockVOInterface lastWriterT = + newNativeReference(ChronicleStampedLockVOInterface.class); + + + ChronicleStampedLock(String chronicelStampedLockLocality) { try { - chm = DirtyReadTolerance.offHeap( - "C:\\Users\\buddy\\dev\\shm\\OPERAND_CHRONICLE_MAP" - ); + chm = DirtyReadTolerance.offHeapLock(chronicelStampedLockLocality); chm.acquireUsing("Stamp ", offHeapLock); //mock'd chm.acquireUsing("LastWriterTime ", lastWriterT); System.out.println( @@ -124,4 +129,20 @@ public class ChronicleStampedLock extends StampedLock { ); } + static ChronicleMap offHeapLock(String operand) + throws IOException { + // ensure thread ids are globally unique. + AffinitySupport.setThreadId(); + return ChronicleMapBuilder.of(String.class, ChronicleStampedLockVOInterface.class) + .entries(16) + .averageKeySize("123456789".length()) + .createPersistedTo( + new File( + operand + // "C:\\Users\\buddy\\dev\\shm\\OPERAND_CHRONICLE_MAP" + ) + ); + //.create(); + } + } diff --git a/src/test/java/net/openhft/chronicle/map/fromdocs/acid/revelations/ChronicleStampedLockVOInterface.java b/src/test/java/net/openhft/chronicle/map/fromdocs/acid/revelations/ChronicleStampedLockVOInterface.java new file mode 100644 index 000000000..88c86641e --- /dev/null +++ b/src/test/java/net/openhft/chronicle/map/fromdocs/acid/revelations/ChronicleStampedLockVOInterface.java @@ -0,0 +1,31 @@ +/* + * Copyright 2012-2018 Chronicle Map Contributors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package net.openhft.chronicle.map.fromdocs.acid.revelations; + +import net.openhft.chronicle.values.Array; +import net.openhft.chronicle.values.Group; +import net.openhft.chronicle.values.MaxUtf8Length; + +interface ChronicleStampedLockVOInterface { + + @Group(0) + long getEntryLockState(); + + void setEntryLockState(long entryLockState); + + +} diff --git a/src/test/java/net/openhft/chronicle/map/fromdocs/acid/revelations/DirtyReadOffender.java b/src/test/java/net/openhft/chronicle/map/fromdocs/acid/revelations/DirtyReadOffender.java index c756c2c81..60bd12749 100644 --- a/src/test/java/net/openhft/chronicle/map/fromdocs/acid/revelations/DirtyReadOffender.java +++ b/src/test/java/net/openhft/chronicle/map/fromdocs/acid/revelations/DirtyReadOffender.java @@ -24,7 +24,9 @@ public class DirtyReadOffender { " @t=" + System.currentTimeMillis() + " DirtyReadOffender established chm " ); - StampedLock offHeapLock = new ChronicleStampedLock(); + StampedLock offHeapLock = new ChronicleStampedLock( + "C:\\Users\\buddy\\dev\\shm\\OPERAND_ChronicleStampedLock" + ); BondVOInterface bond = newNativeReference(BondVOInterface.class); //BondVOInterface cslMock = newNativeReference(BondVOInterface.class); chm.acquireUsing("369604101", bond); diff --git a/src/test/java/net/openhft/chronicle/map/fromdocs/acid/revelations/DirtyReadTolerance.java b/src/test/java/net/openhft/chronicle/map/fromdocs/acid/revelations/DirtyReadTolerance.java index 16dbc97c7..51d658343 100644 --- a/src/test/java/net/openhft/chronicle/map/fromdocs/acid/revelations/DirtyReadTolerance.java +++ b/src/test/java/net/openhft/chronicle/map/fromdocs/acid/revelations/DirtyReadTolerance.java @@ -41,4 +41,20 @@ public class DirtyReadTolerance { ); //.create(); } + + static ChronicleMap offHeapLock(String operand) + throws IOException { + // ensure thread ids are globally unique. + AffinitySupport.setThreadId(); + return ChronicleMapBuilder.of(String.class, ChronicleStampedLockVOInterface.class) + .entries(16) + .averageKeySize("123456789".length()) + .createPersistedTo( + new File( + operand + // "C:\\Users\\buddy\\dev\\shm\\OPERAND_CHRONICLE_MAP" + ) + ); + //.create(); + } } \ No newline at end of file diff --git a/src/test/java/net/openhft/chronicle/map/fromdocs/acid/revelations/DirtyReadVictim.java b/src/test/java/net/openhft/chronicle/map/fromdocs/acid/revelations/DirtyReadVictim.java index 53d80ed70..9adc3731a 100644 --- a/src/test/java/net/openhft/chronicle/map/fromdocs/acid/revelations/DirtyReadVictim.java +++ b/src/test/java/net/openhft/chronicle/map/fromdocs/acid/revelations/DirtyReadVictim.java @@ -19,7 +19,9 @@ public class DirtyReadVictim { */ ChronicleMap chm = DirtyReadTolerance.offHeap( - "C:\\Users\\buddy\\dev\\shm\\OPERAND_CHRONICLE_MAP" + "C:\\Users\\buddy\\dev\\shm\\" + + "=" + + ".bOPERAND_CHRONICLE_MAP" ); Double coupon = 0.00; BondVOInterface bond = newNativeReference(BondVOInterface.class); @@ -29,7 +31,11 @@ public class DirtyReadVictim { " ,,@t=" + System.currentTimeMillis() + " DirtyReadVictim ENTERING offHeapLock.tryOptimisticRead()" ); - StampedLock offHeapLock = new ChronicleStampedLock(); + StampedLock offHeapLock = + new ChronicleStampedLock( + "C:\\Users\\buddy\\dev\\shm\\" + + "OPERAND_ChronicleStampedLock" + ); while ((stamp = offHeapLock.tryOptimisticRead()) < 0) { ; } @@ -72,7 +78,6 @@ public class DirtyReadVictim { coupon + " " + "stamp = [" + stamp + "]" ); - } else { System.out.println( " ,,@t=" + System.currentTimeMillis() +