refactored reference impl: off-heap ChronicleStampedLock.java

This commit is contained in:
cotton-ben
2020-07-04 18:07:38 -04:00
parent 3a21267f16
commit a97439a3b0
5 changed files with 87 additions and 12 deletions
@@ -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
* <p>
@@ -15,14 +18,16 @@ import static net.openhft.chronicle.values.Values.newNativeReference;
*/
public class ChronicleStampedLock extends StampedLock {
ChronicleMap<String, BondVOInterface> chm;
BondVOInterface offHeapLock = newNativeReference(BondVOInterface.class);
BondVOInterface lastWriterT = newNativeReference(BondVOInterface.class);
{
ChronicleMap<String, ChronicleStampedLockVOInterface> 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<String, ChronicleStampedLockVOInterface> 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();
}
}
@@ -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);
}
@@ -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);
@@ -41,4 +41,20 @@ public class DirtyReadTolerance<K,V> {
);
//.create();
}
static ChronicleMap<String, ChronicleStampedLockVOInterface> 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();
}
}
@@ -19,7 +19,9 @@ public class DirtyReadVictim {
*/
ChronicleMap<String, BondVOInterface> 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() +