public interface PNCounter extends DistributedObject
The counter supports adding and subtracting values as well as retrieving the current counter value. Each replica of this counter can perform operations locally without coordination with the other replicas, thus increasing availability. The counter guarantees that whenever two nodes have received the same set of updates, possibly in a different order, their state is identical, and any conflicting updates are merged automatically. If no new updates are made to the shared state, all nodes that can communicate will eventually have the same data.
The updates to this counter are applied locally when invoked on a
CRDT replica. A replica can be any hazelcast instance which is not a
client or a lite member. The number of replicas in the cluster is
determined by the PNCounterConfig.getReplicaCount()
configuration
value.
When invoking updates from non-replica instance, the invocation is remote.
This may lead to indeterminate state - the update may be applied but the
response has not been received. In this case, the caller will be notified
with a TargetDisconnectedException
when invoking from a client or a
MemberLeftException
when invoked from a member.
The read and write methods provide monotonic read and RYW (read-your-write)
guarantees. These guarantees are session guarantees which means that if
no replica with the previously observed state is reachable, the session
guarantees are lost and the method invocation will throw a
ConsistencyLostException
. This does not mean
that an update is lost. All the updates are part of some replica and
will be eventually reflected in the state of all other replicas. This
exception just means that you cannot observe your own writes because
all replicas that contain your updates are currently unreachable.
After you have received a ConsistencyLostException
, you can either
wait for a sufficiently up-to-date replica to become reachable in which
case the session can be continued, or you can reset the session by calling
the reset()
method. If you have called the reset()
method,
a new session is started with the next invocation to a CRDT replica.
If the PN Counter is used through a client, the invocations might throw
TargetNotMemberException
, indicating that
the invocation targets are not members of the cluster anymore. That might
happen when the client is connected to a new cluster, but the member list is
not updated yet, or all the replica members of the PN Counter is dead but
the corresponding member list update is not received yet. Upon receiving
the exception, one has to wait until the member list is updated on the
client side and try again.
NOTE:
The CRDT state is kept entirely on non-lite (data) members. If there
aren't any and the methods here are invoked on a lite member, they will
fail with an NoDataMemberInClusterException
.
Modifier and Type | Method and Description |
---|---|
long |
addAndGet(long delta)
Adds the given value to the current value.
|
long |
decrementAndGet()
Decrements by one the current value.
|
long |
get()
Returns the current value of the counter.
|
long |
getAndAdd(long delta)
Adds the given value to the current value.
|
long |
getAndDecrement()
Decrements by one the current value.
|
long |
getAndIncrement()
Increments by one the current value.
|
long |
getAndSubtract(long delta)
Subtracts the given value from the current value.
|
long |
incrementAndGet()
Increments by one the current value.
|
void |
reset()
Resets the observed state by this PN counter.
|
long |
subtractAndGet(long delta)
Subtracts the given value from the current value.
|
destroy, getDestroyContextForTenant, getName, getPartitionKey, getServiceName
long get()
NoDataMemberInClusterException
- if the cluster does not contain
any data membersConsistencyLostException
- if the session guarantees have
been lost (see class level javadoc)Cluster.getClusterVersion()
long getAndAdd(long delta)
delta
- the value to addNoDataMemberInClusterException
- if the cluster does not contain
any data membersConsistencyLostException
- if the session guarantees have
been lost (see class level javadoc)Cluster.getClusterVersion()
long addAndGet(long delta)
delta
- the value to addNoDataMemberInClusterException
- if the cluster does not contain
any data membersConsistencyLostException
- if the session guarantees have
been lost (see class level javadoc)Cluster.getClusterVersion()
long getAndSubtract(long delta)
delta
- the value to addNoDataMemberInClusterException
- if the cluster does not contain
any data membersConsistencyLostException
- if the session guarantees have
been lost (see class level javadoc)Cluster.getClusterVersion()
long subtractAndGet(long delta)
delta
- the value to subtractNoDataMemberInClusterException
- if the cluster does not contain
any data membersConsistencyLostException
- if the session guarantees have
been lost (see class level javadoc)Cluster.getClusterVersion()
long decrementAndGet()
NoDataMemberInClusterException
- if the cluster does not contain
any data membersConsistencyLostException
- if the session guarantees have
been lost (see class level javadoc)Cluster.getClusterVersion()
long incrementAndGet()
NoDataMemberInClusterException
- if the cluster does not contain
any data membersConsistencyLostException
- if the session guarantees have
been lost (see class level javadoc)Cluster.getClusterVersion()
long getAndDecrement()
NoDataMemberInClusterException
- if the cluster does not contain
any data membersConsistencyLostException
- if the session guarantees have
been lost (see class level javadoc)Cluster.getClusterVersion()
long getAndIncrement()
NoDataMemberInClusterException
- if the cluster does not contain
any data membersConsistencyLostException
- if the session guarantees have
been lost (see class level javadoc)Cluster.getClusterVersion()
void reset()
ConsistencyLostException
to reset the proxy and to be able to start a new session.Copyright © 2023 Hazelcast, Inc.. All rights reserved.