Safer unlocking on eventListener calls

This commit is contained in:
leventov
2015-12-16 17:33:20 +03:00
parent 93ed26e983
commit 0d716996ea
2 changed files with 49 additions and 39 deletions
@@ -957,6 +957,8 @@ final class ReplicatedChronicleMap<K, KI, MKI extends MetaBytesInterop<K, ? supe
if (eventListener != null) {
V valueInstance = toValue.toInstance(copies, v, valueSize);
writeUnlock();
// TODO unlocking is dangerous, because this method is a part of
// acquireUsingLocked()
try {
eventListener.onPut(keyInstance, valueInstance, null, false, true,
true, localIdentifier, replacedIdentifier, timestamp,
@@ -1159,6 +1161,8 @@ final class ReplicatedChronicleMap<K, KI, MKI extends MetaBytesInterop<K, ? supe
updateResult = isDeleted ? UpdateResult.INSERT : UpdateResult.UPDATE;
}
replaceValueDeletedCallback(segmentState, hashLookup, pos, isDeleted);
// put callbacks
boolean hasValueChanged = updateResult != UpdateResult.UNCHANGED;
onPutMaybeRemote(segmentState.pos, false);
@@ -1184,21 +1188,6 @@ final class ReplicatedChronicleMap<K, KI, MKI extends MetaBytesInterop<K, ? supe
}
}
// for DRY (reusing replaceValueAndNotifyPut() method),
// size is updated AFTER callbacks are called.
// however this shouldn't be an issue because exclusive segment lock
// is still held
if (isDeleted) {
incrementSize();
// if they are NOT equal, it means the entry was relocated in putValue(),
// hence position is already set
if (pos == segmentState.pos) {
hashLookup.putPosition(segmentState.pos);
} else {
assert hashLookup.getPositions().isSet(segmentState.pos);
}
}
return updateResult;
}
// key is not found
@@ -1315,20 +1304,7 @@ final class ReplicatedChronicleMap<K, KI, MKI extends MetaBytesInterop<K, ? supe
entry, pos, hashLookup, readValue,
resultUnused, isDeleted, remote,
replacedIdentifier, replacedTimestamp);
// for DRY (reusing replaceValueAndNotifyPut() method),
// size is updated AFTER callbacks are called.
// however this shouldn't be an issue because exclusive segment lock
// is still held
if (isDeleted) {
incrementSize();
// if they are NOT equal, it means the entry was relocated in putValue(),
// hence position is already set
if (pos == segmentState.pos) {
hashLookup.putPosition(segmentState.pos);
} else {
assert hashLookup.getPositions().isSet(segmentState.pos);
}
}
if (resultUnused)
return null;
return isDeleted ? readValue.readNull() : prevValue;
@@ -1379,6 +1355,21 @@ final class ReplicatedChronicleMap<K, KI, MKI extends MetaBytesInterop<K, ? supe
return resultUnused ? null : readValue.readNull();
}
@Override
void replaceValueDeletedCallback(
SegmentState segmentState, MultiMap hashLookup, long pos, boolean isDeleted) {
if (isDeleted) {
incrementSize();
// if they are NOT equal, it means the entry was relocated in putValue(),
// hence position is already set
if (pos == segmentState.pos) {
hashLookup.putPosition(segmentState.pos);
} else {
assert hashLookup.getPositions().isSet(segmentState.pos);
}
}
}
/**
* Used only with replication, its sometimes possible to receive an old ( or stale update )
* from a remote map. This method is used to determine if we should ignore such updates.
@@ -1643,11 +1634,6 @@ final class ReplicatedChronicleMap<K, KI, MKI extends MetaBytesInterop<K, ? supe
getExpectedValueInterops, expectedValue, getNewValueInterops, newValue,
readValue, toValue, pos, entry, hashLookup,
replacedIdentifier, replacedTimestamp);
if (result != Boolean.FALSE) {
entry.position(timestampPos);
entry.writeLong(timestamp);
entry.writeByte(identifier);
}
return result;
}
// key is not found
@@ -1658,6 +1644,13 @@ final class ReplicatedChronicleMap<K, KI, MKI extends MetaBytesInterop<K, ? supe
}
}
@Override
void updateReplicationBytesOnKeyPresentOnReplace(
Bytes entry, long timestampPos, long timestamp, byte identifier) {
entry.position(timestampPos);
entry.writeLong(timestamp);
entry.writeByte(identifier);
}
public void dirtyEntries(final long timeStamp,
final EntryModifiableCallback callback,