public class VersionLockedObject<T> extends Object
Users of the VersionLockedObject cannot access the versioned object directly, rather they use the access() and update() methods to read and manipulate the object.
access() and update() allow the user to provide functions to execute under locks. These functions execute various "unsafe" methods provided by this object which inspect and manipulate the object state.
syncObjectUnsafe() enables the user to bring the object to a given version, and the VersionLockedObject manages any sync or rollback of updates necessary.
Created by mwei on 11/13/16.
Constructor and Description |
---|
VersionLockedObject(Supplier<T> newObjectFn,
StreamViewSMRAdapter smrStream,
Map<String,ICorfuSMRUpcallTarget<T>> upcallTargets,
Map<String,IUndoRecordFunction<T>> undoRecordTargets,
Map<String,IUndoFunction<T>> undoTargets,
Set<String> resetSet)
The VersionLockedObject maintains a versioned object which is backed by an ISMRStream,
and is optionally backed by an additional optimistic update stream.
|
Modifier and Type | Method and Description |
---|---|
<R> R |
access(Function<VersionLockedObject<T>,Boolean> directAccessCheckFunction,
Consumer<VersionLockedObject<T>> updateFunction,
Function<T,R> accessFunction)
Access the internal state of the object, trying first to optimistically access
the object, then obtaining a write lock the optimistic access fails.
|
protected void |
applyUndoRecordUnsafe(SMREntry record)
Given a SMR entry with an undo record, undo the update.
|
void |
applyUpdateToStreamUnsafe(SMREntry entry,
long globalAddress)
Apply an SMREntry to the version object, while
doing bookkeeping for the underlying stream.
|
Object |
applyUpdateUnsafe(SMREntry entry)
Apply an SMR update to the object, possibly optimistically.
|
void |
gc(long trimMark)
Run gc on this object.
|
UUID |
getID()
Deprecated.
|
WriteSetSMRStream |
getOptimisticStreamUnsafe()
Get a handle to the optimistic stream.
|
long |
getVersionUnsafe()
Get the version of this object.
|
boolean |
isOptimisticallyModifiedUnsafe()
Check whether this object is currently under optimistic modifications.
|
long |
logUpdate(SMREntry entry,
boolean saveUpcall)
Log an update to this object, noting a request to save the
upcall result if necessary.
|
boolean |
optimisticallyOwnedByThreadUnsafe()
Check whether or not this object was modified by this thread.
|
void |
optimisticCommitUnsafe()
Drop the optimistic stream, effectively making optimistic updates
to this object permanent.
|
protected void |
optimisticRollbackUnsafe()
Roll back the optimistic stream, resetting the object if it can not
be restored.
|
void |
resetUnsafe()
Reset this object to the uninitialized state.
|
void |
rollbackObjectUnsafe(long rollbackVersion)
Roll the object back to the supplied version if possible.
|
protected void |
rollbackStreamUnsafe(ISMRStream stream,
long rollbackVersion)
Roll back the given stream by applying undo records in reverse order
from the current stream position until rollbackVersion.
|
void |
seek(long globalAddress)
Move the pointer for this object (effectively, forcefuly
change the version of this object without playing
any updates).
|
void |
setOptimisticStreamUnsafe(WriteSetSMRStream optimisticStream)
Set the optimistic stream for this thread, rolling back
any previous threads if they were present.
|
void |
syncObjectUnsafe(long timestamp)
Bring the object to the requested version, rolling back or syncing
the object from the log if necessary to reach the requested version.
|
protected void |
syncStreamUnsafe(ISMRStream stream,
long timestamp)
Sync this stream by playing updates forward in the stream until
the given timestamp.
|
String |
toString()
Generate the summary string for this version locked object.
|
<R> R |
update(Function<VersionLockedObject<T>,R> updateFunction)
Update the object under a write lock.
|
public VersionLockedObject(Supplier<T> newObjectFn, StreamViewSMRAdapter smrStream, Map<String,ICorfuSMRUpcallTarget<T>> upcallTargets, Map<String,IUndoRecordFunction<T>> undoRecordTargets, Map<String,IUndoFunction<T>> undoTargets, Set<String> resetSet)
newObjectFn
- A function passed to instantiate a new instance of this object.smrStream
- Stream View backing this object.upcallTargets
- UpCall map for this object.undoRecordTargets
- Undo record function map for this object.undoTargets
- Undo functions map.resetSet
- Reset set for this object.public void gc(long trimMark)
public <R> R access(Function<VersionLockedObject<T>,Boolean> directAccessCheckFunction, Consumer<VersionLockedObject<T>> updateFunction, Function<T,R> accessFunction)
If the directAccessCheckFunction returns true, then we execute the accessFunction without running updateFunction. If false, we execute the updateFunction to allow the user to modify the state of the object before calling accessFunction.
directAccessCheckFunction is executed under an optimistic read lock. Read-only unsafe operations are permitted.
updateFunction is executed under a write lock. Both read and write unsafe operations are permitted.
accessFunction is accessed either under a read lock or write lock depending on whether an update was necessary or not.
R
- The type of the access function return.directAccessCheckFunction
- A function which returns True if the object can be
accessed without being updated.updateFunction
- A function which is executed when direct access
is not allowed and the object must be updated.accessFunction
- A function which allows the user to directly access
the object while locked in the state enforced by
either the directAccessCheckFunction or updateFunction.public <R> R update(Function<VersionLockedObject<T>,R> updateFunction)
R
- The type of the return of the updateFunction.updateFunction
- A function to execute once the write lock has been acquired.public void rollbackObjectUnsafe(long rollbackVersion)
Unsafe, requires that the caller has acquired a write lock.
rollbackVersion
- The version to rollback to.NoRollbackException
- If the object cannot be rolled back to
the supplied version.public void seek(long globalAddress)
globalAddress
- The global address to set the pointer topublic void syncObjectUnsafe(long timestamp)
timestamp
- The timestamp to update the object to.public long logUpdate(SMREntry entry, boolean saveUpcall)
entry
- The entry to log.saveUpcall
- True, if the upcall result should be
saved, false otherwise.public WriteSetSMRStream getOptimisticStreamUnsafe()
public void optimisticCommitUnsafe()
public boolean optimisticallyOwnedByThreadUnsafe()
public void setOptimisticStreamUnsafe(WriteSetSMRStream optimisticStream)
optimisticStream
- The new optimistic stream to install.public long getVersionUnsafe()
public boolean isOptimisticallyModifiedUnsafe()
public void resetUnsafe()
@Deprecated public UUID getID()
public String toString()
The format of this string is [type]@[version][+] (where + is the optimistic flag)
protected void applyUndoRecordUnsafe(SMREntry record)
record
- The record to undo.public Object applyUpdateUnsafe(SMREntry entry)
entry
- The entry to apply.protected void rollbackStreamUnsafe(ISMRStream stream, long rollbackVersion)
stream
- The stream of SMR updates to apply in
reverse order.rollbackVersion
- The version to stop roll back at.NoRollbackException
- If an entry in the stream did not contain
undo information.protected void syncStreamUnsafe(ISMRStream stream, long timestamp)
When the stream is trimmed, this exception is passed up to the caller, unless the timestamp was Address.MAX, in which the entire object is reset and re-try the sync, which should pick up any checkpoint that was inserted.
stream
- The stream to sync forwardtimestamp
- The timestamp to sync up to.protected void optimisticRollbackUnsafe()
public void applyUpdateToStreamUnsafe(SMREntry entry, long globalAddress)
entry
- Copyright © 2019 CorfuDB. All rights reserved.