HCOLL-349 HCOLL-350 stage compilation via annotation processing

This commit is contained in:
leventov
2015-07-17 04:25:33 +03:00
parent eaf027b335
commit fb5f3cf2e1
73 changed files with 5199 additions and 12496 deletions
@@ -0,0 +1,80 @@
/*
* Copyright 2015 Higher Frequency Trading
*
* http://www.higherfrequencytrading.com
*
* 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.hash.impl.stage.data.bytes;
import net.openhft.chronicle.bytes.RandomDataInput;
import net.openhft.chronicle.hash.AbstractData;
import net.openhft.chronicle.hash.impl.stage.entry.HashEntryStages;
import net.openhft.chronicle.hash.impl.stage.hash.CheckOnEachPublicOperation;
import net.openhft.chronicle.hash.impl.stage.hash.KeyBytesInterop;
import net.openhft.sg.Stage;
import net.openhft.sg.StageRef;
import net.openhft.sg.Staged;
@Staged
public class EntryKeyBytesData<K> extends AbstractData<K> {
@StageRef KeyBytesInterop<K, ?, ?> ki;
@StageRef HashEntryStages<K> entry;
@StageRef CheckOnEachPublicOperation checkOnEachPublicOperation;
@Stage("CachedEntryKey") private K cachedEntryKey;
@Stage("CachedEntryKey") private boolean cachedEntryKeyRead = false;
private void initCachedEntryKey() {
cachedEntryKey = innerGetUsing(cachedEntryKey);
cachedEntryKeyRead = true;
}
@Override
public RandomDataInput bytes() {
checkOnEachPublicOperation.checkOnEachPublicOperation();
return entry.entryBS;
}
@Override
public long offset() {
checkOnEachPublicOperation.checkOnEachPublicOperation();
return entry.keyOffset;
}
@Override
public long size() {
checkOnEachPublicOperation.checkOnEachPublicOperation();
return entry.keySize;
}
@Override
public K get() {
checkOnEachPublicOperation.checkOnEachPublicOperation();
return cachedEntryKey;
}
@Override
public K getUsing(K usingKey) {
checkOnEachPublicOperation.checkOnEachPublicOperation();
return innerGetUsing(usingKey);
}
private K innerGetUsing(K usingKey) {
entry.entryBytes.position(entry.keyOffset);
return ki.keyReader.read(entry.entryBytes, size(), usingKey);
}
}
@@ -0,0 +1,70 @@
/*
* Copyright 2015 Higher Frequency Trading
*
* http://www.higherfrequencytrading.com
*
* 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.hash.impl.stage.data.bytes;
import net.openhft.chronicle.bytes.RandomDataInput;
import net.openhft.chronicle.hash.AbstractData;
import net.openhft.chronicle.hash.impl.stage.hash.KeyBytesInterop;
import net.openhft.chronicle.hash.impl.stage.input.HashInputBytes;
import net.openhft.lang.io.Bytes;
import net.openhft.sg.Stage;
import net.openhft.sg.StageRef;
import net.openhft.sg.Staged;
@Staged
public class InputKeyBytesData<K> extends AbstractData<K> {
@StageRef HashInputBytes in;
@StageRef KeyBytesInterop<K, ?, ?> ki;
@Stage("CachedBytesInputKey") private K cachedBytesInputKey;
@Stage("CachedBytesInputKey") private boolean cachedBytesInputKeyRead = false;
private void initCachedBytesInputKey() {
cachedBytesInputKey = getUsing(cachedBytesInputKey);
cachedBytesInputKeyRead = true;
}
@Override
public RandomDataInput bytes() {
return in.inputStore;
}
@Override
public long offset() {
return in.inputKeyOffset;
}
@Override
public long size() {
return in.inputKeySize;
}
@Override
public K get() {
return cachedBytesInputKey;
}
@Override
public K getUsing(K usingKey) {
Bytes inputBytes = in.inputBytes;
inputBytes.position(in.inputKeyOffset);
return ki.keyReader.read(inputBytes, size(), usingKey);
}
}
@@ -0,0 +1,65 @@
/*
* Copyright 2015 Higher Frequency Trading
*
* http://www.higherfrequencytrading.com
*
* 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.hash.impl.stage.data.instance;
import net.openhft.chronicle.hash.impl.CopyingInstanceData;
import net.openhft.chronicle.hash.impl.stage.hash.KeyBytesInterop;
import net.openhft.chronicle.hash.impl.value.instance.KeyInitableData;
import net.openhft.chronicle.hash.serialization.internal.MetaBytesInterop;
import net.openhft.lang.io.DirectBytes;
import net.openhft.sg.Stage;
import net.openhft.sg.StageRef;
import net.openhft.sg.Staged;
@Staged
public abstract class InputKeyInstanceData<K, KI, MKI extends MetaBytesInterop<K, ? super KI>>
extends CopyingInstanceData<K> implements KeyInitableData<K> {
@StageRef KeyBytesInterop<K, KI, MKI> ki;
private K key = null;
@Override
public void initKey(K key) {
this.key = key;
}
@Override
public K instance() {
return key;
}
@Stage("Buffer") private DirectBytes buffer;
@Stage("Buffer") private boolean marshalled = false;
private void initBuffer() {
MKI mki = ki.keyMetaInterop(key);
long size = mki.size(ki.keyInterop, key);
buffer = getBuffer(this.buffer, size);
mki.write(ki.keyInterop, buffer, key);
buffer.flip();
marshalled = true;
}
@Override
public K getUsing(K usingKey) {
buffer.position(0);
return ki.keyReader.read(buffer, buffer.limit(), usingKey);
}
}
@@ -0,0 +1,47 @@
/*
* Copyright 2015 Higher Frequency Trading
*
* http://www.higherfrequencytrading.com
*
* 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.hash.impl.stage.entry;
import net.openhft.chronicle.hash.impl.VanillaChronicleHashHolder;
import net.openhft.sg.StageRef;
import net.openhft.sg.Staged;
@Staged
public class AllocatedChunks {
@StageRef public VanillaChronicleHashHolder<?, ?, ?> hh;
@StageRef public SegmentStages s;
@StageRef public HashEntryStages<?> entry;
public int allocatedChunks = 0;
public void initAllocatedChunks(int allocatedChunks) {
this.allocatedChunks = allocatedChunks;
}
public void incrementSegmentEntriesIfNeeded() {
// don't increment
}
public void initEntryAndKeyCopying(long entrySize, long bytesToCopy) {
initAllocatedChunks(hh.h().inChunks(entrySize));
entry.copyExistingEntry(s.alloc(allocatedChunks), bytesToCopy);
incrementSegmentEntriesIfNeeded();
}
}
@@ -0,0 +1,134 @@
/*
* Copyright 2015 Higher Frequency Trading
*
* http://www.higherfrequencytrading.com
*
* 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.hash.impl.stage.entry;
import net.openhft.chronicle.bytes.BytesStore;
import net.openhft.chronicle.bytes.NativeBytesStore;
import net.openhft.chronicle.hash.Data;
import net.openhft.chronicle.hash.HashEntry;
import net.openhft.chronicle.hash.impl.VanillaChronicleHashHolder;
import net.openhft.chronicle.hash.impl.stage.data.bytes.EntryKeyBytesData;
import net.openhft.chronicle.hash.impl.stage.hash.CheckOnEachPublicOperation;
import net.openhft.lang.io.Bytes;
import net.openhft.sg.Stage;
import net.openhft.sg.StageRef;
import net.openhft.sg.Staged;
import org.jetbrains.annotations.NotNull;
@Staged
public abstract class HashEntryStages<K> implements HashEntry<K> {
@StageRef VanillaChronicleHashHolder<?, ?, ?> hh;
@StageRef public SegmentStages s;
@StageRef public CheckOnEachPublicOperation checkOnEachPublicOperation;
@StageRef public HashLookupPos hlp;
public final Bytes entryBytes = hh.h().ms.bytes();
public final BytesStore entryBS =
new NativeBytesStore<>(entryBytes.address(), entryBytes.capacity(), null, false);
public long pos = -1;
public void initPos(long pos) {
this.pos = pos;
}
public abstract void closePos();
@Stage("EntryOffset") public long keySizeOffset = -1;
public void initEntryOffset() {
keySizeOffset = s.entrySpaceOffset + pos * hh.h().chunkSize;
entryBytes.limit(entryBytes.capacity());
}
public long keySize = -1;
public void initKeySize(long keySize) {
this.keySize = keySize;
}
public long keyOffset = -1;
public void initKeyOffset(long keyOffset) {
this.keyOffset = keyOffset;
}
public void readExistingEntry(long pos) {
initPos(pos);
entryBytes.position(keySizeOffset);
initKeySize(hh.h().keySizeMarshaller.readSize(entryBytes));
initKeyOffset(entryBytes.position());
}
public void writeNewEntry(long pos, Data<?> key) {
initPos(pos);
initKeySize(key.size());
entryBytes.position(keySizeOffset);
hh.h().keySizeMarshaller.writeSize(entryBytes, keySize);
initKeyOffset(entryBytes.position());
key.writeTo(entryBS, keyOffset);
}
public void copyExistingEntry(long newPos, long bytesToCopy) {
long oldKeySizeOffset = keySizeOffset;
long oldKeyOffset = keyOffset;
initPos(newPos);
initKeyOffset(keySizeOffset + (oldKeyOffset - oldKeySizeOffset));
entryBS.write(keySizeOffset, entryBS, oldKeySizeOffset, bytesToCopy);
}
public long keyEnd() {
return keyOffset + keySize;
}
protected long entryEnd() {
return keyEnd();
}
long entrySize() {
return entryEnd() - keySizeOffset;
}
@StageRef EntryKeyBytesData<K> entryKey;
@NotNull
@Override
public Data<K> key() {
checkOnEachPublicOperation.checkOnEachPublicOperation();
return entryKey;
}
@Stage("TheEntrySizeInChunks") public int entrySizeInChunks = 0;
void initTheEntrySizeInChunks() {
entrySizeInChunks = hh.h().inChunks(entrySize());
}
public void initTheEntrySizeInChunks(int actuallyUsedChunks) {
entrySizeInChunks = actuallyUsedChunks;
}
public void innerRemoveEntryExceptHashLookupUpdate() {
s.free(pos, entrySizeInChunks);
s.entries(s.entries() - 1L);
s.incrementModCount();
}
}
@@ -0,0 +1,238 @@
/*
* Copyright 2015 Higher Frequency Trading
*
* http://www.higherfrequencytrading.com
*
* 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.hash.impl.stage.entry;
import net.openhft.chronicle.hash.impl.VanillaChronicleHashHolder;
import net.openhft.chronicle.hash.impl.hashlookup.EntryConsumer;
import net.openhft.lang.Maths;
import net.openhft.sg.Stage;
import net.openhft.sg.StageRef;
import net.openhft.sg.Staged;
import static net.openhft.lang.MemoryUnit.BITS;
import static net.openhft.lang.MemoryUnit.BYTES;
import static net.openhft.lang.io.NativeBytes.UNSAFE;
@Staged
public class HashLookup {
// to fit 64 bits per slot.
public static final int MAX_SEGMENT_CHUNKS = 1 << 30;
public static final int MAX_SEGMENT_ENTRIES = 1 << 29;
public static int valueBits(long actualChunksPerSegment) {
return 64 - Long.numberOfLeadingZeros(actualChunksPerSegment - 1L);
}
public static int keyBits(long entriesPerSegment, int valueBits) {
// key hash cardinality is between 1.0 and 2.0 of key cardinality
int minKeyBits = 64 - Long.numberOfLeadingZeros(entriesPerSegment - 1L);
// This minimizes probability of hash collision => on search, additional cache line touch
// and key comparison, that is quite high cost.
// between 8.0 and 16.0
minKeyBits += 3;
int actualEntryBits = (int) BYTES.align((long) (minKeyBits + valueBits), BITS);
// devote the rest bits for key
return actualEntryBits - valueBits;
}
public static int entrySize(int keyBits, int valueBits) {
return (int) BYTES.alignAndConvert((long) (keyBits + valueBits), BITS);
}
public static long capacityFor(long entriesPerSegment) {
if (entriesPerSegment < 0L)
throw new IllegalArgumentException("entriesPerSegment should be positive");
long capacity = Maths.nextPower2(entriesPerSegment, 64L);
if (((double) entriesPerSegment) / (double) capacity > 2./3.) {
// hash lookup shouldn't be too dense
capacity <<= 1L;
}
return capacity;
}
public static long mask(int bits) {
return (1L << bits) - 1L;
}
public static final long UNSET_KEY = 0L;
public static final long UNSET_ENTRY = 0L;
@Stage("SegmentHashLookup") long address = -1;
@Stage("SegmentHashLookup") long capacityMask;
@Stage("SegmentHashLookup") int hashLookupEntrySize;
@Stage("SegmentHashLookup") long capacityMask2;
@Stage("SegmentHashLookup") int keyBits;
@Stage("SegmentHashLookup") long keyMask;
@Stage("SegmentHashLookup") long valueMask;
@Stage("SegmentHashLookup") long entryMask;
public void initSegmentHashLookup(
long address, long capacity, int entrySize, int keyBits, int valueBits) {
innerInitSegmentHashLookup(address, capacity, entrySize, keyBits, valueBits);
}
@Stage("SegmentHashLookup")
private void innerInitSegmentHashLookup(
long address, long capacity, int entrySize, int keyBits, int valueBits) {
this.address = address;
this.capacityMask = capacity - 1L;
this.hashLookupEntrySize = entrySize;
this.capacityMask2 = capacityMask * entrySize;
this.keyBits = keyBits;
this.keyMask = mask(keyBits);
this.valueMask = mask(valueBits);
this.entryMask = mask(keyBits + valueBits);
}
@StageRef VanillaChronicleHashHolder<?, ?, ?> hh;
@StageRef SegmentStages s;
public void initSegmentHashLookup() {
long hashLookupOffset = hh.h().segmentOffset(s.segmentIndex);
innerInitSegmentHashLookup(hh.h().ms.address() + hashLookupOffset,
hh.h().segmentHashLookupCapacity, hh.h().segmentHashLookupEntrySize,
hh.h().segmentHashLookupKeyBits, hh.h().segmentHashLookupValueBits);
}
long indexToPos(long index) {
return index * hashLookupEntrySize;
}
public long maskUnsetKey(long key) {
return (key &= keyMask) != UNSET_KEY ? key : keyMask;
}
public void checkValueForPut(long value) {
assert (value & ~valueMask) == 0L : "Value out of range, was " + value;
}
public boolean empty(long entry) {
return (entry & entryMask) == UNSET_ENTRY;
}
public long key(long entry) {
return entry & keyMask;
}
public long value(long entry) {
return (entry >>> keyBits) & valueMask;
}
long entry(long key, long value) {
return key | (value << keyBits);
}
public long hlPos(long key) {
return indexToPos(key & capacityMask);
}
public long step(long pos) {
return (pos += hashLookupEntrySize) <= capacityMask2 ? pos : 0L;
}
public long stepBack(long pos) {
return (pos -= hashLookupEntrySize) >= 0 ? pos : capacityMask2;
}
public long readEntry(long pos) {
return UNSAFE.getLong(address + pos);
}
public void writeEntry(long pos, long prevEntry, long key, long value) {
long entry = (prevEntry & ~entryMask) | entry(key, value);
UNSAFE.putLong(address + pos, entry);
}
public void writeEntryVolatile(long pos, long prevEntry, long key, long value) {
long entry = (prevEntry & ~entryMask) | entry(key, value);
UNSAFE.putLongVolatile(null, address + pos, entry);
}
public void putValueVolatile(long pos, long value) {
checkValueForPut(value);
long currentEntry = readEntry(pos);
writeEntryVolatile(pos, currentEntry, key(currentEntry), value);
}
void writeEntry(long pos, long prevEntry, long anotherEntry) {
long entry = (prevEntry & ~entryMask) | (anotherEntry & entryMask);
UNSAFE.putLong(address + pos, entry);
}
void clearEntry(long pos, long prevEntry) {
long entry = (prevEntry & ~entryMask);
UNSAFE.putLong(address + pos, entry);
}
public void clearHashLookup() {
UNSAFE.setMemory(address, capacityMask2 + hashLookupEntrySize, (byte) 0);
}
/**
* Returns "insert" position in terms of consequent putValue()
*/
public long remove(long posToRemove) {
long entryToRemove = readEntry(posToRemove);
long posToShift = posToRemove;
while (true) {
posToShift = step(posToShift);
long entryToShift = readEntry(posToShift);
if (empty(entryToShift))
break;
long insertPos = hlPos(key(entryToShift));
// the following condition essentially means circular permutations
// of three (r = posToRemove, s = posToShift, i = insertPos)
// positions are accepted:
// [...i..r...s.] or
// [...r..s...i.] or
// [...s..i...r.]
boolean cond1 = insertPos <= posToRemove;
boolean cond2 = posToRemove <= posToShift;
if ((cond1 && cond2) ||
// chain wrapped around capacity
(posToShift < insertPos && (cond1 || cond2))) {
writeEntry(posToRemove, entryToRemove, entryToShift);
posToRemove = posToShift;
entryToRemove = entryToShift;
}
}
clearEntry(posToRemove, entryToRemove);
return posToRemove;
}
String hashLookupToString() {
final StringBuilder sb = new StringBuilder("{");
forEach((key, value) -> sb.append(key).append('=').append(value).append(','));
sb.append('}');
return sb.toString();
}
void forEach(EntryConsumer action) {
for (long pos = 0L; pos <= capacityMask2; pos += hashLookupEntrySize) {
long entry = readEntry(pos);
if (!empty(entry))
action.accept(key(entry), value(entry));
}
}
}
@@ -0,0 +1,23 @@
package net.openhft.chronicle.hash.impl.stage.entry;
import net.openhft.sg.Stage;
import net.openhft.sg.Staged;
@Staged
public abstract class HashLookupPos {
public long hashLookupPos = -1;
public abstract boolean hashLookupPosInit();
public void initHashLookupPos(long hashLookupPos) {
this.hashLookupPos = hashLookupPos;
}
@Stage("HashLookupPos")
public void setHashLookupPos(long hashLookupPos) {
this.hashLookupPos = hashLookupPos;
}
public abstract void closeHashLookupPos();
}
@@ -0,0 +1,107 @@
/*
* Copyright 2015 Higher Frequency Trading
*
* http://www.higherfrequencytrading.com
*
* 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.hash.impl.stage.entry;
import net.openhft.sg.StageRef;
import net.openhft.sg.Staged;
import net.openhft.chronicle.hash.locks.InterProcessLock;
import org.jetbrains.annotations.NotNull;
import java.util.concurrent.TimeUnit;
import static net.openhft.chronicle.hash.impl.LocalLockState.READ_LOCKED;
import static net.openhft.chronicle.hash.impl.LocalLockState.UNLOCKED;
@Staged
public class ReadLock implements InterProcessLock {
@StageRef SegmentStages s;
@StageRef HashEntryStages entry;
@StageRef HashLookupPos hlp;
@Override
public boolean isHeldByCurrentThread() {
return s.localLockState.read;
}
@Override
public void lock() {
if (s.localLockState == UNLOCKED) {
s.segmentHeader.readLock(s.segmentHeaderAddress);
s.setLocalLockState(READ_LOCKED);
}
}
@Override
public void lockInterruptibly() throws InterruptedException {
if (s.localLockState == UNLOCKED) {
s.segmentHeader.readLockInterruptibly(s.segmentHeaderAddress);
s.setLocalLockState(READ_LOCKED);
}
}
@Override
public boolean tryLock() {
if (s.localLockState == UNLOCKED) {
if (s.segmentHeader.tryReadLock(s.segmentHeaderAddress)) {
s.setLocalLockState(READ_LOCKED);
return true;
} else {
return false;
}
} else {
return true;
}
}
@Override
public boolean tryLock(long time, @NotNull TimeUnit unit) throws InterruptedException {
if (s.localLockState == UNLOCKED) {
if (s.segmentHeader.tryReadLock(s.segmentHeaderAddress, time, unit)) {
s.setLocalLockState(READ_LOCKED);
return true;
} else {
return false;
}
} else {
return true;
}
}
@Override
public void unlock() {
switch (s.localLockState) {
case UNLOCKED:
return;
case READ_LOCKED:
s.segmentHeader.readUnlock(s.segmentHeaderAddress);
break;
case UPDATE_LOCKED:
s.segmentHeader.updateUnlock(s.segmentHeaderAddress);
break;
case WRITE_LOCKED:
s.segmentHeader.writeUnlock(s.segmentHeaderAddress);
}
s.setLocalLockState(UNLOCKED);
// TODO what should close here?
hlp.closeHashLookupPos();
entry.closePos();
}
}
@@ -0,0 +1,368 @@
/*
* Copyright 2015 Higher Frequency Trading
*
* http://www.higherfrequencytrading.com
*
* 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.hash.impl.stage.entry;
import net.openhft.chronicle.hash.impl.BigSegmentHeader;
import net.openhft.chronicle.hash.impl.LocalLockState;
import net.openhft.chronicle.hash.impl.SegmentHeader;
import net.openhft.chronicle.hash.impl.VanillaChronicleHash;
import net.openhft.chronicle.hash.impl.stage.hash.Chaining;
import net.openhft.chronicle.hash.impl.stage.hash.CheckOnEachPublicOperation;
import net.openhft.chronicle.hash.impl.VanillaChronicleHashHolder;
import net.openhft.chronicle.hash.locks.InterProcessLock;
import net.openhft.chronicle.hash.locks.InterProcessReadWriteUpdateLock;
import net.openhft.lang.collection.DirectBitSet;
import net.openhft.lang.collection.SingleThreadedDirectBitSet;
import net.openhft.lang.io.MultiStoreBytes;
import net.openhft.sg.Stage;
import net.openhft.sg.Staged;
import net.openhft.sg.StageRef;
import org.jetbrains.annotations.NotNull;
import static net.openhft.chronicle.hash.impl.LocalLockState.UNLOCKED;
@Staged
public abstract class SegmentStages implements InterProcessReadWriteUpdateLock {
@StageRef Chaining chaining;
@StageRef public VanillaChronicleHashHolder<?, ?, ?> hh;
@StageRef public CheckOnEachPublicOperation checkOnEachPublicOperation;
@Stage("TheSegmentIndex") public int segmentIndex = -1;
public void initTheSegmentIndex(int segmentIndex) {
this.segmentIndex = segmentIndex;
}
@Stage("SegHeader") long segmentHeaderAddress;
@Stage("SegHeader") SegmentHeader segmentHeader = null;
private void initSegHeader() {
segmentHeaderAddress = hh.h().ms.address() + hh.h().segmentHeaderOffset(segmentIndex);
segmentHeader = BigSegmentHeader.INSTANCE;
}
public long entries() {
return segmentHeader.size(segmentHeaderAddress);
}
public void entries(long size) {
segmentHeader.size(segmentHeaderAddress, size);
}
long nextPosToSearchFrom() {
return segmentHeader.nextPosToSearchFrom(segmentHeaderAddress);
}
public void nextPosToSearchFrom(long nextPosToSearchFrom) {
segmentHeader.nextPosToSearchFrom(segmentHeaderAddress, nextPosToSearchFrom);
}
public long deleted() {
return segmentHeader.deleted(segmentHeaderAddress);
}
public void deleted(long deleted) {
segmentHeader.deleted(segmentHeaderAddress, deleted);
}
public long size() {
return entries() - deleted();
}
@Stage("Locks") public SegmentStages rootContextOnThisSegment = null;
/**
* See the ChMap Ops spec, considerations of nested same-thread concurrent contexts.
* Once context enters the segment, and observes concurrent same-thread context,
* it sets concurrentSameThreadContexts = true for itself and that concurrent context.
* This flag is not dropped on exit of one of these contexts, because between calls of
* this context, nested one could be initialized, does some changes that break our thread-local
* assumptions _and exit_, that is why on exit concurrent context should remain "dirty".
*/
@Stage("Locks") public boolean concurrentSameThreadContexts;
@Stage("Locks") public int latestSameThreadSegmentModCount;
@Stage("Locks") public int contextModCount;
@Stage("Locks")
public void incrementModCount() {
contextModCount = rootContextOnThisSegment.latestSameThreadSegmentModCount =
rootContextOnThisSegment.latestSameThreadSegmentModCount + 1;
}
// chain
@Stage("Locks") SegmentStages nextNode;
@Stage("Locks") LocalLockState localLockState;
@Stage("Locks") int totalReadLockCount;
@Stage("Locks") int totalUpdateLockCount;
@Stage("Locks") int totalWriteLockCount;
public abstract boolean locksInit();
void initLocks() {
localLockState = UNLOCKED;
int indexOfThisContext = chaining.indexInContextChain;
for (int i = indexOfThisContext - 1; i >= 0; i--) {
if (tryFindInitLocksOfThisSegment(this, i))
return;
}
for (int i = indexOfThisContext + 1, size = chaining.contextChain.size(); i < size; i++) {
if (tryFindInitLocksOfThisSegment(this, i))
return;
}
rootContextOnThisSegment = this;
concurrentSameThreadContexts = false;
latestSameThreadSegmentModCount = 0;
contextModCount = 0;
nextNode = null;
totalReadLockCount = 0;
totalUpdateLockCount = 0;
totalWriteLockCount = 0;
}
boolean tryFindInitLocksOfThisSegment(Object thisContext, int index) {
SegmentStages c = chaining.contextAtIndexInChain(index);
if (c.segmentHeader != null && c.segmentHeaderAddress == segmentHeaderAddress &&
c.rootContextOnThisSegment != null) {
//TODO implement nested contexts. Hardest part - bodies of Read/Update/WriteLock classes
throw new IllegalStateException("Nested context not implemented yet");
} else {
return false;
}
}
void closeLocks() {
// TODO should throw ISE on attempt to close root context when children not closed
if (rootContextOnThisSegment == this) {
closeRootLocks();
} else {
closeNestedLocks();
}
localLockState = null;
rootContextOnThisSegment = null;
}
@Stage("Locks")
private void closeNestedLocks() {
unlinkFromSegmentContextsChain();
switch (localLockState) {
case UNLOCKED:
break;
case READ_LOCKED:
int newTotalReadLockCount = this.rootContextOnThisSegment.totalReadLockCount -= 1;
if (newTotalReadLockCount == 0) {
if (this.rootContextOnThisSegment.totalUpdateLockCount == 0 &&
this.rootContextOnThisSegment.totalWriteLockCount == 0) {
segmentHeader.readUnlock(segmentHeaderAddress);
}
} else if (newTotalReadLockCount < 0) {
throw new IllegalStateException("read underflow");
}
break;
case UPDATE_LOCKED:
int newTotalUpdateLockCount =
this.rootContextOnThisSegment.totalUpdateLockCount -= 1;
if (newTotalUpdateLockCount == 0) {
if (this.rootContextOnThisSegment.totalWriteLockCount == 0) {
if (this.rootContextOnThisSegment.totalReadLockCount == 0) {
segmentHeader.updateUnlock(segmentHeaderAddress);
} else {
segmentHeader.downgradeUpdateToReadLock(segmentHeaderAddress);
}
}
} else if (newTotalUpdateLockCount < 0) {
throw new IllegalStateException("update underflow");
}
break;
case WRITE_LOCKED:
int newTotalWriteLockCount = this.rootContextOnThisSegment.totalWriteLockCount -= 1;
if (newTotalWriteLockCount == 0) {
if (this.rootContextOnThisSegment.totalUpdateLockCount > 0) {
segmentHeader.downgradeWriteToUpdateLock(segmentHeaderAddress);
} else {
if (this.rootContextOnThisSegment.totalReadLockCount > 0) {
segmentHeader.downgradeWriteToReadLock(segmentHeaderAddress);
} else {
segmentHeader.writeUnlock(segmentHeaderAddress);
}
}
}
break;
}
}
@Stage("Locks")
private void unlinkFromSegmentContextsChain() {
SegmentStages prevContext = this.rootContextOnThisSegment;
while (true) {
assert prevContext.nextNode != null;
if (prevContext.nextNode == this)
break;
prevContext = prevContext.nextNode;
}
assert nextNode == null;
prevContext.nextNode = null;
}
@Stage("Locks")
private void closeRootLocks() {
assert nextNode == null;
switch (localLockState) {
case UNLOCKED:
return;
case READ_LOCKED:
segmentHeader.readUnlock(segmentHeaderAddress);
return;
case UPDATE_LOCKED:
segmentHeader.updateUnlock(segmentHeaderAddress);
return;
case WRITE_LOCKED:
segmentHeader.writeUnlock(segmentHeaderAddress);
}
}
@Stage("Locks")
public void setLocalLockState(LocalLockState newState) {
localLockState = newState;
}
// TODO their isHeldByCurrentThread should be _context_ local
// i. e. if outer same-thread context hold the lock, isHeld() should be false, but
// lock() op could be shallow
@StageRef public ReadLock innerReadLock;
@StageRef public UpdateLock innerUpdateLock;
@StageRef public WriteLock innerWriteLock;
@NotNull
@Override
public InterProcessLock readLock() {
checkOnEachPublicOperation.checkOnEachPublicOperation();
return innerReadLock;
}
@NotNull
@Override
public InterProcessLock updateLock() {
checkOnEachPublicOperation.checkOnEachPublicOperation();
return innerUpdateLock;
}
@NotNull
@Override
public InterProcessLock writeLock() {
checkOnEachPublicOperation.checkOnEachPublicOperation();
return innerWriteLock;
}
@StageRef public HashLookup hashLookup;
@Stage("Segment") MultiStoreBytes freeListBytes = new MultiStoreBytes();
@Stage("Segment") public SingleThreadedDirectBitSet freeList = new SingleThreadedDirectBitSet();
@Stage("Segment") long entrySpaceOffset = 0;
boolean segmentInit() {
return entrySpaceOffset > 0;
}
void initSegment() {
VanillaChronicleHash<?, ?, ?, ?, ?> h = hh.h();
long hashLookupOffset = h.segmentOffset(segmentIndex);
long freeListOffset = hashLookupOffset + h.segmentHashLookupOuterSize;
freeListBytes.storePositionAndSize(h.ms, freeListOffset, h.segmentFreeListInnerSize);
freeList.reuse(freeListBytes);
entrySpaceOffset = freeListOffset + h.segmentFreeListOuterSize +
h.segmentEntrySpaceInnerOffset;
}
void closeSegment() {
entrySpaceOffset = 0;
}
//TODO refactor/optimize
public long alloc(int chunks) {
VanillaChronicleHash<?, ?, ?, ?, ?> h = hh.h();
if (chunks > h.maxChunksPerEntry)
throw new IllegalArgumentException("Entry is too large: requires " + chunks +
" entry size chucks, " + h.maxChunksPerEntry + " is maximum.");
long ret = freeList.setNextNContinuousClearBits(nextPosToSearchFrom(), chunks);
if (ret == DirectBitSet.NOT_FOUND || ret + chunks > h.actualChunksPerSegment) {
if (ret != DirectBitSet.NOT_FOUND &&
ret + chunks > h.actualChunksPerSegment && ret < h.actualChunksPerSegment)
freeList.clear(ret, h.actualChunksPerSegment);
ret = freeList.setNextNContinuousClearBits(0L, chunks);
if (ret == DirectBitSet.NOT_FOUND || ret + chunks > h.actualChunksPerSegment) {
if (ret != DirectBitSet.NOT_FOUND &&
ret + chunks > h.actualChunksPerSegment &&
ret < h.actualChunksPerSegment)
freeList.clear(ret, h.actualChunksPerSegment);
if (chunks == 1) {
throw new IllegalStateException(
"Segment is full, no free entries found");
} else {
throw new IllegalStateException(
"Segment is full or has no ranges of " + chunks
+ " continuous free chunks"
);
}
}
updateNextPosToSearchFrom(ret, chunks);
} else {
// if bit at nextPosToSearchFrom is clear, it was skipped because
// more than 1 chunk was requested. Don't move nextPosToSearchFrom
// in this case. chunks == 1 clause is just a fast path.
if (chunks == 1 || freeList.isSet(nextPosToSearchFrom())) {
updateNextPosToSearchFrom(ret, chunks);
}
}
return ret;
}
public void free(long fromPos, int chunks) {
freeList.clear(fromPos, fromPos + chunks);
if (fromPos < nextPosToSearchFrom())
nextPosToSearchFrom(fromPos);
}
public void updateNextPosToSearchFrom(long allocated, int chunks) {
long nextPosToSearchFrom = allocated + chunks;
if (nextPosToSearchFrom >= hh.h().actualChunksPerSegment)
nextPosToSearchFrom = 0L;
nextPosToSearchFrom(nextPosToSearchFrom);
}
public void clearSegment() {
innerWriteLock.lock();
hashLookup.clearHashLookup();
freeList.clear();
nextPosToSearchFrom(0L);
entries(0L);
}
public void clear() {
clearSegment();
}
}
@@ -0,0 +1,131 @@
/*
* Copyright 2015 Higher Frequency Trading
*
* http://www.higherfrequencytrading.com
*
* 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.hash.impl.stage.entry;
import net.openhft.sg.StageRef;
import net.openhft.sg.Staged;
import net.openhft.chronicle.hash.locks.InterProcessLock;
import org.jetbrains.annotations.NotNull;
import java.util.concurrent.TimeUnit;
import static net.openhft.chronicle.hash.impl.LocalLockState.READ_LOCKED;
import static net.openhft.chronicle.hash.impl.LocalLockState.UPDATE_LOCKED;
@Staged
public class UpdateLock implements InterProcessLock {
@StageRef SegmentStages s;
@Override
public boolean isHeldByCurrentThread() {
return s.localLockState.update;
}
@Override
public void lock() {
switch (s.localLockState) {
case UNLOCKED:
s.segmentHeader.updateLock(s.segmentHeaderAddress);
s.setLocalLockState(UPDATE_LOCKED);
return;
case READ_LOCKED:
throw forbiddenUpgrade();
case UPDATE_LOCKED:
case WRITE_LOCKED:
// do nothing
}
}
/**
* Non-static because after compilation it becomes inner class which forbids static methods
*/
@NotNull
private IllegalMonitorStateException forbiddenUpgrade() {
return new IllegalMonitorStateException("Cannot upgrade from read to update lock");
}
@Override
public void lockInterruptibly() throws InterruptedException {
switch (s.localLockState) {
case UNLOCKED:
s.segmentHeader.updateLockInterruptibly(s.segmentHeaderAddress);
s.setLocalLockState(UPDATE_LOCKED);
return;
case READ_LOCKED:
throw forbiddenUpgrade();
case UPDATE_LOCKED:
case WRITE_LOCKED:
// do nothing
}
}
@Override
public boolean tryLock() {
switch (s.localLockState) {
case UNLOCKED:
if (s.segmentHeader.tryUpdateLock(s.segmentHeaderAddress)) {
s.setLocalLockState(UPDATE_LOCKED);
return true;
} else {
return false;
}
case READ_LOCKED:
throw forbiddenUpgrade();
case UPDATE_LOCKED:
case WRITE_LOCKED:
return true;
}
throw new AssertionError();
}
@Override
public boolean tryLock(long time, @NotNull TimeUnit unit) throws InterruptedException {
switch (s.localLockState) {
case UNLOCKED:
if (s.segmentHeader.tryUpdateLock(s.segmentHeaderAddress, time, unit)) {
s.setLocalLockState(UPDATE_LOCKED);
return true;
} else {
return false;
}
case READ_LOCKED:
throw forbiddenUpgrade();
case UPDATE_LOCKED:
case WRITE_LOCKED:
return true;
}
throw new AssertionError();
}
@Override
public void unlock() {
switch (s.localLockState) {
case UNLOCKED:
case READ_LOCKED:
return;
case UPDATE_LOCKED:
s.segmentHeader.downgradeUpdateToReadLock(s.segmentHeaderAddress);
break;
case WRITE_LOCKED:
s.segmentHeader.downgradeWriteToReadLock(s.segmentHeaderAddress);
}
s.setLocalLockState(READ_LOCKED);
}
}
@@ -0,0 +1,146 @@
/*
* Copyright 2015 Higher Frequency Trading
*
* http://www.higherfrequencytrading.com
*
* 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.hash.impl.stage.entry;
import net.openhft.sg.StageRef;
import net.openhft.sg.Staged;
import net.openhft.chronicle.hash.locks.InterProcessLock;
import org.jetbrains.annotations.NotNull;
import java.util.concurrent.TimeUnit;
import static net.openhft.chronicle.hash.impl.LocalLockState.UPDATE_LOCKED;
import static net.openhft.chronicle.hash.impl.LocalLockState.WRITE_LOCKED;
@Staged
public class WriteLock implements InterProcessLock {
@StageRef SegmentStages s;
@Override
public boolean isHeldByCurrentThread() {
return s.localLockState.write;
}
@Override
public void lock() {
switch (s.localLockState) {
case UNLOCKED:
s.segmentHeader.writeLock(s.segmentHeaderAddress);
s.setLocalLockState(WRITE_LOCKED);
return;
case READ_LOCKED:
throw forbiddenUpgrade();
case UPDATE_LOCKED:
s.segmentHeader.upgradeUpdateToWriteLock(s.segmentHeaderAddress);
s.setLocalLockState(WRITE_LOCKED);
case WRITE_LOCKED:
// do nothing
}
}
/**
* Non-static because after compilation it becomes inner class which forbids static methods
*/
@NotNull
private IllegalMonitorStateException forbiddenUpgrade() {
return new IllegalMonitorStateException("Cannot upgrade from read to write lock");
}
@Override
public void lockInterruptibly() throws InterruptedException {
switch (s.localLockState) {
case UNLOCKED:
s.segmentHeader.writeLockInterruptibly(s.segmentHeaderAddress);
s.setLocalLockState(WRITE_LOCKED);
return;
case READ_LOCKED:
throw forbiddenUpgrade();
case UPDATE_LOCKED:
s.segmentHeader.upgradeUpdateToWriteLockInterruptibly(s.segmentHeaderAddress);
s.setLocalLockState(WRITE_LOCKED);
case WRITE_LOCKED:
// do nothing
}
}
@Override
public boolean tryLock() {
switch (s.localLockState) {
case UNLOCKED:
if (s.segmentHeader.tryWriteLock(s.segmentHeaderAddress)) {
s.setLocalLockState(WRITE_LOCKED);
return true;
} else {
return false;
}
case READ_LOCKED:
throw forbiddenUpgrade();
case UPDATE_LOCKED:
if (s.segmentHeader.tryUpgradeUpdateToWriteLock(s.segmentHeaderAddress)) {
s.setLocalLockState(WRITE_LOCKED);
return true;
} else {
return false;
}
case WRITE_LOCKED:
return true;
}
throw new AssertionError();
}
@Override
public boolean tryLock(long time, @NotNull TimeUnit unit) throws InterruptedException {
switch (s.localLockState) {
case UNLOCKED:
if (s.segmentHeader.tryWriteLock(s.segmentHeaderAddress, time, unit)) {
s.setLocalLockState(WRITE_LOCKED);
return true;
} else {
return false;
}
case READ_LOCKED:
throw forbiddenUpgrade();
case UPDATE_LOCKED:
if (s.segmentHeader.tryUpgradeUpdateToWriteLock(
s.segmentHeaderAddress, time, unit)) {
s.setLocalLockState(WRITE_LOCKED);
return true;
} else {
return false;
}
case WRITE_LOCKED:
return true;
}
throw new AssertionError();
}
@Override
public void unlock() {
switch (s.localLockState) {
case UNLOCKED:
case READ_LOCKED:
case UPDATE_LOCKED:
return;
case WRITE_LOCKED:
s.segmentHeader.downgradeWriteToUpdateLock(s.segmentHeaderAddress);
}
s.setLocalLockState(UPDATE_LOCKED);
}
}
@@ -0,0 +1,87 @@
/*
* Copyright 2015 Higher Frequency Trading
*
* http://www.higherfrequencytrading.com
*
* 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.hash.impl.stage.hash;
import net.openhft.sg.Staged;
import java.util.ArrayList;
import java.util.List;
@Staged
public class Chaining {
public final List<Chaining> contextChain;
public final int indexInContextChain;
public Chaining() {
contextChain = new ArrayList<>();
contextChain.add(this);
indexInContextChain = 0;
}
public Chaining(Chaining root) {
contextChain = root.contextChain;
indexInContextChain = contextChain.size();
contextChain.add(this);
}
public <T> T contextAtIndexInChain(int index) {
//noinspection unchecked
return (T) contextChain.get(index);
}
boolean used;
public boolean usedInit() {
return used;
}
public void initUsed(boolean used) {
this.used = used;
}
void closeUsed() {
used = false;
}
public <T> T getContext() {
for (Chaining context : contextChain) {
if (!context.usedInit()) {
//noinspection unchecked
return (T) context;
}
}
int maxNestedContexts = 1 << 16;
if (contextChain.size() > maxNestedContexts) {
throw new IllegalStateException("More than " + maxNestedContexts +
" nested ChronicleHash contexts are not supported. Very probable that " +
"you simply forgot to close context somewhere (recommended to use " +
"try-with-resources statement). " +
"Otherwise this is a bug, please report with this " +
"stack trace on https://github.com/OpenHFT/Chronicle-Map/issues");
}
//noinspection unchecked
return (T) createChaining();
}
public Chaining createChaining() {
return new Chaining(this);
}
}
@@ -0,0 +1,32 @@
/*
* Copyright 2015 Higher Frequency Trading
*
* http://www.higherfrequencytrading.com
*
* 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.hash.impl.stage.hash;
import net.openhft.sg.StageRef;
import net.openhft.sg.Staged;
@Staged
public abstract class CheckOnEachPublicOperation {
@StageRef OwnerThreadHolder holder;
public void checkOnEachPublicOperation() {
holder.checkAccessingFromOwnerThread();
}
}
@@ -0,0 +1,41 @@
/*
* Copyright 2015 Higher Frequency Trading
*
* http://www.higherfrequencytrading.com
*
* 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.hash.impl.stage.hash;
import net.openhft.chronicle.hash.impl.VanillaChronicleHashHolder;
import net.openhft.chronicle.hash.serialization.BytesReader;
import net.openhft.chronicle.hash.serialization.internal.MetaBytesInterop;
import net.openhft.sg.StageRef;
import net.openhft.sg.Staged;
@Staged
public class KeyBytesInterop<K, KI, MKI extends MetaBytesInterop<K, ? super KI>> {
@StageRef
VanillaChronicleHashHolder<K, KI, MKI> hh;
@StageRef ThreadLocalCopiesHolder ch;
public final BytesReader<K> keyReader =
hh.h().keyReaderProvider.get(ch.copies, hh.h().originalKeyReader);
public final KI keyInterop = hh.h().keyInteropProvider.get(ch.copies, hh.h().originalKeyInterop);
public MKI keyMetaInterop(K key) {
return hh.h().metaKeyInteropProvider.get(
ch.copies, hh.h().originalMetaKeyInterop, keyInterop, key);
}
}
@@ -0,0 +1,10 @@
package net.openhft.chronicle.hash.impl.stage.hash;
import net.openhft.sg.Staged;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Staged
public class LogHolder {
public static final Logger LOG = LoggerFactory.getLogger(LogHolder.class);
}
@@ -0,0 +1,36 @@
/*
* Copyright 2015 Higher Frequency Trading
*
* http://www.higherfrequencytrading.com
*
* 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.hash.impl.stage.hash;
import net.openhft.sg.Staged;
import java.util.ConcurrentModificationException;
@Staged
public class OwnerThreadHolder {
final Thread owner = Thread.currentThread();
public void checkAccessingFromOwnerThread() {
if (owner != Thread.currentThread()) {
throw new ConcurrentModificationException(
"Context shouldn't be accessed from multiple threads");
}
}
}
@@ -0,0 +1,27 @@
/*
* Copyright 2015 Higher Frequency Trading
*
* http://www.higherfrequencytrading.com
*
* 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.hash.impl.stage.hash;
import net.openhft.lang.threadlocal.ThreadLocalCopies;
import net.openhft.sg.Staged;
@Staged
public class ThreadLocalCopiesHolder {
public final ThreadLocalCopies copies = ThreadLocalCopies.get();
}
@@ -0,0 +1,48 @@
/*
* Copyright 2015 Higher Frequency Trading
*
* http://www.higherfrequencytrading.com
*
* 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.hash.impl.stage.input;
import net.openhft.chronicle.hash.impl.JavaLangBytesReusableBytesStore;
import net.openhft.chronicle.hash.impl.VanillaChronicleHashHolder;
import net.openhft.lang.io.Bytes;
import net.openhft.sg.Stage;
import net.openhft.sg.StageRef;
import net.openhft.sg.Staged;
@Staged
public class HashInputBytes {
@StageRef VanillaChronicleHashHolder<?, ?, ?> hh;
public Bytes inputBytes = null;
public final JavaLangBytesReusableBytesStore inputStore = new JavaLangBytesReusableBytesStore();
public void initInputBytes(Bytes inputBytes) {
this.inputBytes = inputBytes;
inputStore.setBytes(inputBytes);
}
@Stage("InputKeyOffsets") public long inputKeySize = -1;
@Stage("InputKeyOffsets") public long inputKeyOffset;
private void initInputKeyOffsets() {
inputKeySize = hh.h().keySizeMarshaller.readSize(inputBytes);
inputKeyOffset = inputBytes.position();
}
}
@@ -0,0 +1,112 @@
/*
* Copyright 2015 Higher Frequency Trading
*
* http://www.higherfrequencytrading.com
*
* 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.hash.impl.stage.iter;
import net.openhft.chronicle.hash.HashEntry;
import net.openhft.chronicle.hash.impl.stage.entry.HashEntryStages;
import net.openhft.chronicle.hash.impl.stage.entry.HashLookup;
import net.openhft.chronicle.hash.impl.stage.entry.HashLookupPos;
import net.openhft.chronicle.hash.impl.stage.entry.SegmentStages;
import net.openhft.chronicle.hash.impl.stage.hash.CheckOnEachPublicOperation;
import net.openhft.sg.StageRef;
import net.openhft.sg.Staged;
import java.util.function.Predicate;
@Staged
public abstract class HashSegmentIteration<K, E extends HashEntry<K>> implements HashEntry<K> {
@StageRef public SegmentStages s;
@StageRef HashEntryStages<K> e;
@StageRef protected HashLookup hashLookup;
@StageRef public CheckOnEachPublicOperation checkOnEachPublicOperation;
@StageRef protected HashLookupPos hlp;
public boolean entryIsPresent() {
return true;
}
public boolean entryRemovedOnThisIteration = false;
abstract boolean entryRemovedOnThisIterationInit();
protected void initEntryRemovedOnThisIteration(boolean entryRemovedOnThisIteration) {
this.entryRemovedOnThisIteration = entryRemovedOnThisIteration;
}
public boolean forEachRemoving(Predicate<? super E> action) {
s.innerUpdateLock.lock();
try {
long size = s.size();
if (size == 0)
return true;
boolean interrupted = false;
long startPos = 0L;
while (!hashLookup.empty(hashLookup.readEntry(startPos))) {
startPos = hashLookup.step(startPos);
}
hlp.initHashLookupPos(startPos);
do {
hlp.setHashLookupPos(hashLookup.step(hlp.hashLookupPos));
long entry = hashLookup.readEntry(hlp.hashLookupPos);
if (!hashLookup.empty(entry)) {
e.readExistingEntry(hashLookup.value(entry));
if (entryIsPresent()) {
initEntryRemovedOnThisIteration(false);
if (!action.test((E) this)) {
interrupted = true;
break;
} else {
if (--size == 0)
break;
}
}
}
} while (hlp.hashLookupPos != startPos);
return !interrupted;
} finally {
s.innerReadLock.unlock();
initEntryRemovedOnThisIteration(false);
}
}
public void checkEntryNotRemovedOnThisIteration() {
if (entryRemovedOnThisIterationInit())
throw new IllegalStateException("Entry was already removed on this iteration");
}
@Override
public void doRemove() {
checkOnEachPublicOperation.checkOnEachPublicOperation();
initEntryRemovedOnThisIteration(true);
s.innerWriteLock.lock();
try {
// this condition mean -- some other entry taken place of the removed one
if (hashLookup.remove(hlp.hashLookupPos) != hlp.hashLookupPos) {
// if so, should make step back, to compensate step forward on the next iteration,
// to consume the shifted entry
hlp.setHashLookupPos(hashLookup.stepBack(hlp.hashLookupPos));
}
e.innerRemoveEntryExceptHashLookupUpdate();
} finally {
s.innerWriteLock.unlock();
}
}
}
@@ -0,0 +1,107 @@
/*
* Copyright 2015 Higher Frequency Trading
*
* http://www.higherfrequencytrading.com
*
* 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.hash.impl.stage.query;
import net.openhft.chronicle.hash.impl.VanillaChronicleHashHolder;
import net.openhft.chronicle.hash.impl.stage.entry.HashEntryStages;
import net.openhft.chronicle.hash.impl.stage.entry.HashLookup;
import net.openhft.chronicle.hash.impl.stage.entry.HashLookupPos;
import net.openhft.chronicle.hash.impl.stage.entry.SegmentStages;
import net.openhft.sg.Stage;
import net.openhft.sg.StageRef;
import net.openhft.sg.Staged;
import static net.openhft.chronicle.hash.impl.stage.entry.HashLookup.UNSET_KEY;
@Staged
public abstract class HashLookupSearch {
@StageRef SegmentStages s;
@StageRef HashEntryStages<?> e;
@StageRef HashLookup hl;
@StageRef HashQuery op;
@StageRef VanillaChronicleHashHolder<?, ?, ?> hh;
@StageRef HashLookupPos hlp;
@Stage("SearchKey") long searchKey = UNSET_KEY;
@Stage("SearchKey") long searchStartPos;
void initSearchKey() {
searchKey = hl.maskUnsetKey(hh.h().hashSplitting.segmentHash(op.hashOfKey));
searchStartPos = hl.hlPos(searchKey);
}
public long nextPos() {
long pos = hlp.hashLookupPos;
while (true) {
long entry = hl.readEntry(pos);
if (hl.empty(entry)) {
hlp.setHashLookupPos(pos);
return -1L;
}
pos = hl.step(pos);
if (pos == searchStartPos)
break;
if (hl.key(entry) == searchKey) {
hlp.setHashLookupPos(pos);
return hl.value(entry);
}
}
throw new IllegalStateException("MultiMap is full, that most likely means you " +
"misconfigured entrySize/chunkSize, and entries tend to take less chunks than " +
"expected");
}
public void found() {
hlp.setHashLookupPos(hl.stepBack(hlp.hashLookupPos));
}
public void remove() {
hlp.setHashLookupPos(hl.remove(hlp.hashLookupPos));
}
void put(long value) {
hl.checkValueForPut(value);
hl.writeEntry(hlp.hashLookupPos, hl.readEntry(hlp.hashLookupPos), searchKey, value);
}
public void putVolatile(long value) {
hl.checkValueForPut(value);
long currentEntry = hl.readEntry(hlp.hashLookupPos);
assert hl.key(currentEntry) == searchKey;
hl.writeEntryVolatile(hlp.hashLookupPos, currentEntry, searchKey, value);
}
public void putNewVolatile(long value) {
hl.checkValueForPut(value);
long currentEntry = hl.readEntry(hlp.hashLookupPos);
hl.writeEntryVolatile(hlp.hashLookupPos, currentEntry, searchKey, value);
}
public boolean checkSlotContainsExpectedKeyAndValue(long value) {
long entry = hl.readEntry(hlp.hashLookupPos);
return hl.key(entry) == searchKey && hl.value(entry) == value;
}
public boolean checkSlotIsEmpty() {
return hl.empty(hl.readEntry(hlp.hashLookupPos));
}
}
@@ -0,0 +1,155 @@
/*
* Copyright 2015 Higher Frequency Trading
*
* http://www.higherfrequencytrading.com
*
* 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.hash.impl.stage.query;
import net.openhft.chronicle.algo.hashing.LongHashFunction;
import net.openhft.chronicle.bytes.BytesUtil;
import net.openhft.chronicle.hash.Data;
import net.openhft.chronicle.hash.HashEntry;
import net.openhft.chronicle.hash.impl.VanillaChronicleHashHolder;
import net.openhft.chronicle.hash.impl.stage.data.bytes.InputKeyBytesData;
import net.openhft.chronicle.hash.impl.stage.data.instance.InputKeyInstanceData;
import net.openhft.chronicle.hash.impl.stage.entry.HashEntryStages;
import net.openhft.chronicle.hash.impl.stage.entry.HashLookupPos;
import net.openhft.chronicle.hash.impl.stage.entry.SegmentStages;
import net.openhft.chronicle.hash.impl.stage.hash.CheckOnEachPublicOperation;
import net.openhft.sg.Stage;
import net.openhft.sg.StageRef;
import net.openhft.sg.Staged;
import static net.openhft.chronicle.hash.impl.stage.query.HashQuery.SearchState.DELETED;
import static net.openhft.chronicle.hash.impl.stage.query.HashQuery.SearchState.PRESENT;
@Staged
public abstract class HashQuery<K> implements HashEntry<K> {
@StageRef public VanillaChronicleHashHolder<K, ?, ?> hh;
@StageRef public SegmentStages s;
@StageRef public HashEntryStages<K> entry;
@StageRef public HashLookupSearch hashLookupSearch;
@StageRef public CheckOnEachPublicOperation checkOnEachPublicOperation;
@StageRef public HashLookupPos hlp;
@StageRef public InputKeyInstanceData<K, ?, ?> inputKeyInstanceValue;
@StageRef public InputKeyBytesData<K> inputKeyBytesValue;
public void dropSearchIfNestedContextsAndPresentHashLookupSlotCheckFailed() {
if (s.locksInit()) {
if (s.concurrentSameThreadContexts &&
s.rootContextOnThisSegment.latestSameThreadSegmentModCount !=
s.contextModCount) {
if (keySearchInit()) {
if (searchState == PRESENT) {
if (!hashLookupSearch.checkSlotContainsExpectedKeyAndValue(entry.pos))
hlp.closeHashLookupPos();
}
}
}
}
}
public Data<K> inputKey = null;
public void initInputKey(Data<K> inputKey) {
this.inputKey = inputKey;
}
public Data<K> queriedKey() {
checkOnEachPublicOperation.checkOnEachPublicOperation();
return inputKey;
}
public long hashOfKey = 0;
void initHashOfKey() {
hashOfKey = inputKey.hash(LongHashFunction.city_1_1());
}
public enum SearchState {
PRESENT,
DELETED,
ABSENT
}
@Stage("KeySearch") protected SearchState searchState = null;
abstract boolean keySearchInit();
@Stage("KeySearch")
public void setSearchState(SearchState newSearchState) {
this.searchState = newSearchState;
}
void initKeySearch() {
for (long pos; (pos = hashLookupSearch.nextPos()) >= 0L;) {
entry.readExistingEntry(pos);
if (!keyEquals())
continue;
hashLookupSearch.found();
keyFound();
return;
}
searchState = SearchState.ABSENT;
}
boolean keyEquals() {
return inputKey.size() == entry.keySize &&
BytesUtil.bytesEqual(entry.entryBS, entry.keyOffset,
inputKey.bytes(), inputKey.offset(), entry.keySize);
}
@Stage("KeySearch")
void keyFound() {
searchState = PRESENT;
}
abstract void closeKeySearch();
public boolean searchStatePresent() {
return searchState == PRESENT;
}
public boolean searchStateDeleted() {
return searchState == DELETED && !s.concurrentSameThreadContexts &&
s.innerUpdateLock.isHeldByCurrentThread();
}
public boolean searchStateAbsent() {
return !searchStatePresent() && !searchStateDeleted();
}
public boolean entryPresent() {
return searchStatePresent();
}
@Override
public void doRemove() {
checkOnEachPublicOperation.checkOnEachPublicOperation();
s.innerUpdateLock.lock();
if (searchStatePresent()) {
// TODO optimize: if shift-deletion is trivial, updateLock.lock()
s.innerWriteLock.lock();
hashLookupSearch.remove();
entry.innerRemoveEntryExceptHashLookupUpdate();
setSearchState(DELETED);
} else {
throw new IllegalStateException("Entry is absent when doRemove() is called");
}
}
}
@@ -0,0 +1,18 @@
package net.openhft.chronicle.hash.impl.stage.query;
import net.openhft.chronicle.hash.impl.stage.entry.HashLookupPos;
import net.openhft.chronicle.hash.impl.stage.entry.SegmentStages;
import net.openhft.sg.StageRef;
import net.openhft.sg.Staged;
@Staged
public abstract class QueryHashLookupPos extends HashLookupPos {
@StageRef SegmentStages s;
@StageRef HashLookupSearch hashLookupSearch;
public void initHashLookupPos() {
s.innerReadLock.lock();
this.hashLookupPos = hashLookupSearch.searchStartPos;
}
}
@@ -0,0 +1,33 @@
/*
* Copyright 2015 Higher Frequency Trading
*
* http://www.higherfrequencytrading.com
*
* 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.hash.impl.stage.query;
import net.openhft.chronicle.hash.impl.stage.entry.SegmentStages;
import net.openhft.sg.StageRef;
import net.openhft.sg.Staged;
@Staged
public abstract class QuerySegmentStages extends SegmentStages {
@StageRef HashQuery q;
void initTheSegmentIndex() {
segmentIndex = hh.h().hashSplitting.segmentIndex(q.hashOfKey);
}
}
@@ -0,0 +1,45 @@
/*
* Copyright 2015 Higher Frequency Trading
*
* http://www.higherfrequencytrading.com
*
* 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.hash.impl.stage.query;
import net.openhft.chronicle.hash.impl.stage.entry.AllocatedChunks;
import net.openhft.sg.StageRef;
import net.openhft.sg.Staged;
import static net.openhft.chronicle.hash.impl.stage.query.HashQuery.SearchState.PRESENT;
@Staged
public class SearchAllocatedChunks extends AllocatedChunks {
@StageRef HashQuery q;
@Override
public void incrementSegmentEntriesIfNeeded() {
if (q.searchState != PRESENT) {
// update the size before the store fence
s.entries(s.entries() + 1L);
}
}
public void initEntryAndKey(long entrySize) {
initAllocatedChunks(hh.h().inChunks(entrySize));
entry.writeNewEntry(s.alloc(allocatedChunks), q.inputKey);
incrementSegmentEntriesIfNeeded();
}
}