Class LWWRegister<A>
- java.lang.Object
-
- akka.cluster.ddata.LWWRegister<A>
-
- All Implemented Interfaces:
ReplicatedData
,ReplicatedDataSerialization
,java.io.Serializable
public final class LWWRegister<A> extends java.lang.Object implements ReplicatedData, ReplicatedDataSerialization
Implements a 'Last Writer Wins Register' CRDT, also called a 'LWW-Register'.It is described in the paper A comprehensive study of Convergent and Commutative Replicated Data Types.
Merge takes the register with highest timestamp. Note that this relies on synchronized clocks.
LWWRegister
should only be used when the choice of value is not important for concurrent updates occurring within the clock skew.Merge takes the register updated by the node with lowest address (
UniqueAddress
is ordered) if the timestamps are exactly the same.Instead of using timestamps based on
System.currentTimeMillis()
time it is possible to use a timestamp value based on something else, for example an increasing version number from a database record that is used for optimistic concurrency control.The
defaultClock
is using max value ofSystem.currentTimeMillis()
andcurrentTimestamp + 1
. This means that the timestamp is increased for changes on the same node that occurs within the same millisecond. It also means that it is safe to use theLWWRegister
without synchronized clocks when there is only one active writer, e.g. a Cluster Singleton. Such a single writer should then first read current value withReadMajority
(or more) before changing and writing the value withWriteMajority
(or more).For first-write-wins semantics you can use the
reverseClock()
instead of thedefaultClock()
This class is immutable, i.e. "modifying" methods return a new instance.
- See Also:
- Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interface
LWWRegister.Clock<A>
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static <A> LWWRegister<A>
apply(SelfUniqueAddress node, A initialValue)
static <A> LWWRegister<A>
apply(SelfUniqueAddress node, A initialValue, LWWRegister.Clock<A> clock)
static <A> LWWRegister<A>
create(SelfUniqueAddress node, A initialValue)
Java APIstatic <A> LWWRegister<A>
create(SelfUniqueAddress node, A initialValue, LWWRegister.Clock<A> clock)
Java APIstatic <A> LWWRegister<A>
create(A initialValue, SelfUniqueAddress node, LWWRegister.Clock<A> clock)
Scala API Creates aLWWRegister
with implicits, given deprecatedapply
functions using Cluster constrain overloading.static <A> LWWRegister.Clock<A>
create$default$3(A initialValue)
static <A> LWWRegister.Clock<A>
defaultClock()
The defaultLWWRegister.Clock
is using max value ofSystem.currentTimeMillis()
andcurrentTimestamp + 1
.boolean
equals(java.lang.Object o)
A
getValue()
Java APIint
hashCode()
LWWRegister<A>
merge(LWWRegister<A> that)
INTERNAL APIstatic <A> LWWRegister.Clock<A>
reverseClock()
ThisLWWRegister.Clock
can be used for first-write-wins semantics.long
timestamp()
java.lang.String
toString()
static <A> scala.Option<A>
unapply(LWWRegister<A> c)
Extract thevalue()
.UniqueAddress
updatedBy()
The currentvalue
was set by this node.A
value()
LWWRegister<A>
withValue(SelfUniqueAddress node, A value)
Change the value of the register.LWWRegister<A>
withValue(SelfUniqueAddress node, A value, LWWRegister.Clock<A> clock)
Change the value of the register.LWWRegister<A>
withValueOf(A value, SelfUniqueAddress node, LWWRegister.Clock<A> clock)
Change the value of the register.LWWRegister.Clock<A>
withValueOf$default$3(A value)
-
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface akka.cluster.ddata.ReplicatedData
merge
-
-
-
-
Method Detail
-
defaultClock
public static <A> LWWRegister.Clock<A> defaultClock()
The defaultLWWRegister.Clock
is using max value ofSystem.currentTimeMillis()
andcurrentTimestamp + 1
.
-
reverseClock
public static <A> LWWRegister.Clock<A> reverseClock()
ThisLWWRegister.Clock
can be used for first-write-wins semantics. It is using min value of-System.currentTimeMillis()
andcurrentTimestamp + 1
, i.e. it is counting backwards.
-
apply
public static <A> LWWRegister<A> apply(SelfUniqueAddress node, A initialValue)
-
apply
public static <A> LWWRegister<A> apply(SelfUniqueAddress node, A initialValue, LWWRegister.Clock<A> clock)
-
create
public static <A> LWWRegister<A> create(A initialValue, SelfUniqueAddress node, LWWRegister.Clock<A> clock)
Scala API Creates aLWWRegister
with implicits, given deprecatedapply
functions using Cluster constrain overloading.
-
create
public static <A> LWWRegister<A> create(SelfUniqueAddress node, A initialValue, LWWRegister.Clock<A> clock)
Java API
-
create
public static <A> LWWRegister<A> create(SelfUniqueAddress node, A initialValue)
Java API
-
create$default$3
public static <A> LWWRegister.Clock<A> create$default$3(A initialValue)
-
unapply
public static <A> scala.Option<A> unapply(LWWRegister<A> c)
Extract thevalue()
.
-
value
public A value()
-
timestamp
public long timestamp()
-
getValue
public A getValue()
Java API
-
withValue
public LWWRegister<A> withValue(SelfUniqueAddress node, A value, LWWRegister.Clock<A> clock)
Change the value of the register.You can provide your
clock
implementation instead of using timestamps based onSystem.currentTimeMillis()
time. The timestamp can for example be an increasing version number from a database record that is used for optimistic concurrency control.
-
withValue
public LWWRegister<A> withValue(SelfUniqueAddress node, A value)
Change the value of the register.
-
withValueOf
public LWWRegister<A> withValueOf(A value, SelfUniqueAddress node, LWWRegister.Clock<A> clock)
Change the value of the register.You can provide your
clock
implementation instead of using timestamps based onSystem.currentTimeMillis()
time. The timestamp can for example be an increasing version number from a database record that is used for optimistic concurrency control.
-
updatedBy
public UniqueAddress updatedBy()
The currentvalue
was set by this node.
-
withValueOf$default$3
public LWWRegister.Clock<A> withValueOf$default$3(A value)
-
merge
public LWWRegister<A> merge(LWWRegister<A> that)
INTERNAL API
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
equals
public boolean equals(java.lang.Object o)
- Overrides:
equals
in classjava.lang.Object
-
hashCode
public int hashCode()
- Overrides:
hashCode
in classjava.lang.Object
-
-