org.apache.ignite.internal.processors.cache

## Class PartitionTxUpdateCounterImpl

- java.lang.Object
- - org.apache.ignite.internal.processors.cache.PartitionTxUpdateCounterImpl

All Implemented Interfaces:
  Iterable<long[]>, PartitionUpdateCounter

Direct Known Subclasses:
  PartitionMvccTxUpdateCounterImpl, PartitionTxUpdateCounterDebugWrapper

    public class PartitionTxUpdateCounterImpl
    extends Object
    implements PartitionUpdateCounter

Update counter implementation used for transactional cache groups in persistent mode.

Implements new partition update counter flow to avoid situations when:

1. update counter could be incremented and persisted while corresponding update is not recorded to WAL.
2. update counter could be prematurely incremented causing missed rebalancing.

All these situations are sources of partitions desync.

Below a short description of new flow:

1. Update counter is reserved for each update in partition on tx prepare phase (which always happens on primary partition owner). Reservation causes HWM increment.
2. Reserved counter values are propagated on backup nodes and stored in backup transactions.
3. On commit reserved counters are assigned to cache entries.
4. LWM is incremented ONLY after corresponding WAL data record for each entry was written.
5. In case of rollback (manual or during tx recovery on node failure) reserved updates are also applied and logged to WAL using `RollbackRecord` for further recovery purposes.

### Field Summary

Fields
`static int` `MAX_MISSED_UPDATES` Max allowed missed updates.
`protected AtomicLong` `reserveCntr` HWM.

### Constructor Summary

Constructors
Constructor and Description
`PartitionTxUpdateCounterImpl()`

### Method Summary

All Methods Instance Methods Concrete Methods
`boolean` `empty()`
`boolean` `equals(Object o)`
`GridLongList` `finalizeUpdateCounters()` Flushes pending update counters closing all possible gaps.
`long` `get()` Get LWM.
`@Nullable byte[]` `getBytes()`
`protected long` `highestAppliedCounter()`
`void` `init(long initUpdCntr, @Nullable byte[] cntrUpdData)` Restores update counter state.
`long` `initial()`
`Iterator<long[]>` `iterator()`
`long` `next()` Increment LWM by 1.
`long` `next(long delta)` Increment LWM by delta.
`long` `reserve(long delta)` Increment HWM by delta.
`long` `reserved()` Returns HWM.
`void` `reset()` Reset counter internal state to zero.
`boolean` `sequential()`
`String` `toString()`
`void` `update(long val)` Sets update counter to absolute value.
`boolean` `update(long start, long delta)` Applies counter update out of range.
`void` `updateInitial(long start, long delta)`

### Methods inherited from class java.lang.Object

`clone, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait`

### Methods inherited from interface java.lang.Iterable

`forEach, spliterator`

### Field Detail

#### MAX_MISSED_UPDATES

    public static final int MAX_MISSED_UPDATES

Max allowed missed updates. Overflow will trigger critical failure handler to prevent OOM.

See Also:
  Constant Field Values

#### reserveCntr

    protected final AtomicLong reserveCntr

HWM.

### Constructor Detail

#### PartitionTxUpdateCounterImpl

    public PartitionTxUpdateCounterImpl()

### Method Detail

#### init

    public void init(long initUpdCntr,
                     @Nullable
                     @Nullable byte[] cntrUpdData)

Restores update counter state.

Specified by:
  `init` in interface `PartitionUpdateCounter`
Parameters:
  `initUpdCntr` - LWM.
  `cntrUpdData` - Counter updates raw data.

#### initial

    public long initial()

Specified by:
  `initial` in interface `PartitionUpdateCounter`

#### get

    public long get()

Get LWM.

Specified by:
  `get` in interface `PartitionUpdateCounter`
Returns:
  Current LWM.

#### highestAppliedCounter

    protected long highestAppliedCounter()

#### next

    public long next()

Description copied from interface: `PartitionUpdateCounter`
Increment LWM by 1.

Specified by:
  `next` in interface `PartitionUpdateCounter`
Returns:
  Next update counter. For tx mode called by `DataStreamerImpl` IsolatedUpdater.

#### update

    public void update(long val)
                throws IgniteCheckedException

Sets update counter to absolute value. All missed updates will be discarded.

Specified by:
  `update` in interface `PartitionUpdateCounter`
Parameters:
  `val` - Absolute value.
Throws:
  `IgniteCheckedException` - if counter cannot be set to passed value due to incompatibility with current state.

#### update

    public boolean update(long start,
                          long delta)

Applies counter update out of range. Update ranges must not intersect.

Specified by:
  `update` in interface `PartitionUpdateCounter`
Parameters:
  `start` - Start (<= lwm).
  `delta` - Delta.
Returns:
  `True` if update was actually applied.

#### updateInitial

    public void updateInitial(long start,
                              long delta)

Specified by:
  `updateInitial` in interface `PartitionUpdateCounter`
Parameters:
  `start` - Counter.
  `delta` - Delta.

#### finalizeUpdateCounters

    public GridLongList finalizeUpdateCounters()

Flushes pending update counters closing all possible gaps.

Specified by:
  `finalizeUpdateCounters` in interface `PartitionUpdateCounter`
Returns:
  Even-length array of pairs [start, end] for each gap.

#### reserve

    public long reserve(long delta)

Increment HWM by delta.

Specified by:
  `reserve` in interface `PartitionUpdateCounter`
Parameters:
  `delta` - Delta.
Returns:
  New HWM.

#### next

    public long next(long delta)

Increment LWM by delta.

Specified by:
  `next` in interface `PartitionUpdateCounter`
Parameters:
  `delta` - Delta.
Returns:
  New LWM.

#### sequential

    public boolean sequential()

Specified by:
  `sequential` in interface `PartitionUpdateCounter`
Returns:
  `True` if counter has no missed updates.

#### getBytes

    @Nullable
    public @Nullable byte[] getBytes()

Specified by:
  `getBytes` in interface `PartitionUpdateCounter`

#### reset

    public void reset()

Reset counter internal state to zero.

Specified by:
  `reset` in interface `PartitionUpdateCounter`

#### equals

    public boolean equals(Object o)

Overrides:
  `equals` in class `Object`

#### reserved

    public long reserved()

Returns HWM.

Specified by:
  `reserved` in interface `PartitionUpdateCounter`
Returns:
  Current HWM.

#### empty

    public boolean empty()

Specified by:
  `empty` in interface `PartitionUpdateCounter`
Returns:
  `True` if counter has not seen any update.

#### iterator

    public Iterator<long[]> iterator()

Specified by:
  `iterator` in interface `Iterable<long[]>`
Specified by:
  `iterator` in interface `PartitionUpdateCounter`
Returns:
  Iterator for pairs [start, range] for each out-of-order update in the update counter sequence.

#### toString

    public String toString()

Overrides:
  `toString` in class `Object`

${current.year} Copyright © Apache Software Foundation

Follow @ApacheIgnite

Ignite Database and Caching Platform | : ver. 2.8.0
Release Date | : February 27 2020