You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 2 Next »

Choosing algorithm for transactional consistency

There are some possible solutions to guarantee transactional consistency:

  1. Consistent Cut
    + Simple algorithm - requires only some read-write locks to sync threads, and doesn't affect performance much.
    + Doesn't require to much additional space - it just writing few additional messages to WAL.
    - Requires time to recovery (applying every message from WAL to system) that depends on how many operations need to be restored.
    - Requires additional storage to persist WAL archives.
    - Doesn't restore ATOMIC caches. (but can be fixed with ReadRepair).
  2. Incremental physical snapshots
    - High disk IO usage for preparing snapshots.
    - Current implementation of snapshots requires PME, that affects performance much.
    + Fast recovering, doesn't depend on amount operations to restore (WAL-free).
  3. MVCC
    - Ignite failed to support MVCC, very hard to implement.

From those options Consistent Cut is more preferable. It may require optimizations for recovery, but it looks like we can provide some. For example, some options:

  1. Using WAL compaction for archived files, that excludes physical records from WAL files;
  2. Apply DataEntry from WAL in parallel by using striped executor (cache group id and partition id);
  3. Using index over WAL files for fast access to written Consistent Cuts.

Consistent Cut

Goal is to find Point on timeline on every node such that set of committed transactions before (after) this Point is the same on every node in a cluster.

Consider a simple model. On the pictures below there are two lines (P and Q). They represent two different Ignite nodes. Nodes exchange of transaction messages with each other. For simplicity let's consider there is the only state of tx (COMMITTED) and only single message related to one tx (one node notifies another about tx and latter immediately commits it after receiving the message).

Transaction can be described as ordered sequence of events. Correct order of events is known on every node. There are 4 ordered events to describe transaction in this simple model: 

  1. P.tx(COMMITTED): transaction committed on node P;
  2. P.snd(m): send message msg(tx5: P → Q);
  3. Q.rcv(m): receive message msg(tx5: P → Q);
  4. Q.tx(COMMITTED): transaction committed on node Q.

The cut line crosses both lines (P and Q) in the Points. Transactions (tx1, tx2, tx3, tx4) are on the left side of cut on both nodes. But tx5 is different because cut crosses also a message line. Then tx5 commits on different nodes in different states (before / after cut). To make a cut consistent the global order of transaction events must be kept - for any transaction if AFTER state contains some events from this order it means that BEFORE must contain all prior events.

Consider examples. First picture below describes case where tx5 state is:

  • before cut: node P = { }, node Q = { rcv(m), tx(COMMITTED) }.
  • after cut: node P = { tx(COMMITTED), snd(m) }, node Q = { }.

Global state before cut contains events { Q.rcv(m), Q.tx(COMMITTED) } that aren't leading events in the known sequence - it misses first events { P.tx(COMMITTED), P.snd(m) }. But order of events must match order of consistent states (BEFORE, AFTER), then such a cut is inconsistent.  

Second picture below describes case where tx5 state is:

  • before cut: node P = { tx(COMMITTED), snd(m) }, node Q = { }.
  • after cut: node P = { }, node Q = { rcv(m), tx(COMMITTED) }.

Global state before cut contains events { P.tx(COMMITTED), P.snd(m) } that are leading events in the known sequence. Such a cut is consistent.


In case of consistent cut every node knows which side of cut a local transaction belongs to (BEFORE or AFTER). On the second picture, node Q after event rcv knows whether tx5 was included to cut on node P. Then if it was, node Q includes it, otherwise doesn't include. 

Algorithm

There is a paper [1] that proposes an algorithm for implementing distributed consistent cut. This algorithm works without a coordinator of the procedure, it is suitable for processes with no-FIFO channels between processes.

Algorithm's purpose is identify wrong order of messages by signing the messages with actual cut version. Then every node knows which side of the cut this transaction belongs to. 

Algorithm's steps:

  1. Random process can start global Consistent Cut (furthermore, multiple process may start it simultaneously);
  2. Mark every message between distributed processes with CutVersion;
  3. On receiving a message a process has to handle the CutVersion from message payload at first, before applying message;
  4. If receiving CutVersion differs from local CutVersion, node has to trigger the Consistent Cut (CC) procedure;
  5. CC procedure consists of steps:
    1. update local CutVersion;
    2. Consistent state includes nodes state (LocalState) and state of channels between nodes (ChannelState). Node has to commit those states:
      1. CC for node i: Ni = LocalStatei + Σ ChannelStateij
      2. ChannelState includes all messages sent between node i and node j.

Map the algorithm to Ignite

Create a recovery point (Consistent Cut)

For Ignite implementation it's proposed to use only single node to coordinate algorithm. User starts a command for creating new ConsistentCut on single node and this node becomes responsible for coordinating Consistent Cut iteration:

  1. This node starts new DistributedProcess over whole baseline topology (failed if any node in BLT is down):
    1. Local node increments local ConsistentCutVersion, put it into ConsistentCutStartRequest that is the InitMessage for the process.
    2. Every node on receiving this message checks version, and if it's actual it prepares a ConsistentCut future.
    3. On receiving the new ConsistentCutVersion every node inits local ConsistentCut and starts signing TxMessages (see below) with new version.
    4. A node can receive new ConsistentCutVersion by both TxMessage and by ConsistentCutStartRequest.
    5. By receiving new CutVersion this node triggers procedure immediately (before applying message, in case of receiving TxMessage).
    6. if it failed, or cut is inconsistent, or previous received version >= new version → notify reducer about issue (user command for creating recovery point failed).

Consistent and In-consistent Cuts

Consistent Cut, in terms of Ignite implementation, is such cut that correctly finished on all baseline nodes - ConsistentCutStartRecord and ConsistentCutFinishRecord are written.

"In-consistent" Cut is such a cut when one or more baseline nodes hasn't wrote ConsistentCutFinishRecord. It's possible in case of any errors appeared during processing local Cut.

In case of topology change, Ignite nodes finishes local Cuts running in this moment, making them inconsistent.

ConsistentCutVersion

Every ignite nodes tracks current ConsistentCutVersion:

ConsistentCutVersion
class ConsistentCutVersion {
	long version;
}

`version` is a simple counter. It's guaranteed it is raising monotonically, due to it is incremented by discovery communication.

ConsistentCutVersion initialization

ConsistentCutVersion can be initialized with:

For changed server topology: 

  1. On start a server node checks local MetaStorage for pre-stored latest ConsistentCutVersion, default is 0.
  2. This version is packed with JoiningNodeDescoveryData and sent to coordinator.
  3. Every server node on receiving TcpDiscoveryNodeAddedMessage:
    1. finishes local ConsistentCut.
    2. checks received ConsistentCutVersion, and if it's greater than local it updates local version to the received version.

For client node on start-up just sets its version to 0. It automatically updates with cluster version by receiving next GridNearTxPrepareResponse message.

After finished restoring (PITR) the version is used for restoring is used and set after reading WAL archives.

Description of Algorithm 

  1. Every node uses local ConsistentCutVersion to sign transaction messages (see below).
  2. New global ConsistentCut is started by user command.
    1. It initialize a new version - increment of local ConsistentCutVersion 
    2. It sends ConsistentCutStartRequest with new version to every node in a cluster (piggy backed on DistributedProcess).
  3. Every node can receive new version by two ways: with the ConsistentCutStartRequest or by transaction message signed with new version.
  4. On receiving new version node inits local ConsistentCut:
    1. upgrades version
    2. collects active transactions and decides which side of ConsistentCut they belong to
    3. writes start mark into WAL (see below).
  5. Node that first commits transaction signes Finish message with local ConsistentCutVersion (txCutVer)
    1. Near (transaction originated node) for 2PC.
    2. Backup (or primary if backups=0) for 1PC.
  6. For collected active transaction nodes check order of events relative to ConsistentCut by comparing the txCutVer with local ConsistentCutVersion:
    1. if local version is greater than txCutVer then transaction belongs to BEFORE side
    2. if local version is equal to txCutVer then transaction belongs to AFTER side
    3. Node, that local version never is less than txCutVer. Because node must process ConsistentCutVersion before applying message with this version.
  7. After every transaction was finished - node writes FinishRecord with transaction info into WAL.
  8. Notifies a coordinator about finishing local procedure by sending ConsistentCutFinishResponse.

Order of transaction messages

In the 2PC protocol in most cases (except some optimization and corner cases) sequence of events for transaction can be described as set of messages (PrepareRequest, PrepareResponse, FinishRequest, FinishResponse) and set of TransactionState. For case with 2 nodes (P - near, Q - primary) it looks like that:  

P.tx(PREPARING) → P.snd(GridNearPrepareRequest) → Q.rcv(GridNearPrepareRequest) → Q.tx(PREPARED) → Q.snd(GridNearPrepareResponse) → P.rcv(GridNearPrepareResponse) → P.tx(PREPARED) → P.tx(COMMITTED) → P.snd(GridNearFinishRequest) →  Q.rcv(GridNearFinishRequest) → Q.tx(COMMITTED)


Important steps in this sequence:

  • near node commits before primary node (primary node commits before backup node)
  • there is a FinishRequest between between actual commits.

Consistent Cut procedure can start in any moment and cross this sequence in any chain. In this moment every node should decide which side of ConsistentCut every transaction belongs to. Some important notes:

  1. There are some issues with ACTIVE transactions:
    1. They can be long
    2. They may become SUSPENDED and hang ConsistentCut after that
  2. Then it's better to avoid checking such transactions, and algorithm helps it: for every transaction in ACTIVE state there are at least 2 messages to sync CutVersion between nodes (PREPARE response, FINISH request) - it's enough to provide a guarantee that every ACTIVE transaction will on the AFTER side.
  3. We skip SUSPENDED transactions to avoid a Consistent Cut hangs. The only valid way to reach SUSPENDED state is ACTIVE → SUSPENDED. Then no need additional checks there.
  4. For other transactions (>= PREPARING) algorithm listens their finish futures and checks txCutVer

Consider some examples below (P - near, Q - primary):

  1. First picture: P commits tx BEFORE, Q commits tx AFTER. Globally tx BEFORE cut.
    1. P commits tx BEFORE, sign FinishRequest with latest known CutVersion (before new one).
    2. Q collects this tx in moment of cut (as locally it's PREPARED) and waiting for FinishRequest. On receiving it checks message: local CutVer > rcvd txCutVer, then it belongs to BEFORE.

  2. Second picture: P commits tx AFTER, Q commits tx AFTER. Globally tx AFTER cut.
    1. P: cut before rcv(GridNearPrepareResponse), then tx isn't PREPARED yet. Then P signs FinishRequest with new CutVersion.
    2. Q collects this tx in moment of cut (as locally it's PREPARED) and waiting for FinishRequest. On receiving it checks message: local CutVer == rcvd txCutVer, then belongs AFTER.

Pictures below describes a little bit complicate example: P commits tx AFTER, Q commits tx BEFORE. But actually Consistent Cut algorithm doesn't allow such case, because this cut is inconsistent. Global state of tx includes events:  { Q.snd(GridNearPrepareResponse), Q.rcv(GridNearFinishRequest), Q.tx(COMMITTED) }. But this state misses some middle events { P.rcv(GridNearPrepareResponse), P.tx(COMMITTED), P.snd(GridNearFinishRequest) }.

Then this cut is inconsistent. To avoid such cases, FinishRequest is signed with latestCutVer, and node Q must to validate it and trigger Consistent Cut before applying the message. And after that this case equals to case from previous pictures.

  1. P: cut before P.rcv(GridNearPrepareResponse), then tx isn't PREPARED yet. Then P signs FinishRequest with new CutVersion.
  2. Q: by receiving FinishRequest it triggers Consistent Cut, collect PREPARED tx in this moment, and after that check version from FinishRequest.
  3. Case equals to that on the second picture above. Globally tx AFTER cut.

One-Phase commit handling

Sequence of events for 1PC differs:

P.tx(PREPARING) → P.snd(GridNearPrepareRequest) → P.rcv(GridNearPrepareRequest) → Q.tx(PREPARED) → Q.tx(COMMITTED) → Q.snd(GridNearPrepareResponse) → P.rcv(GridNearPrepareResponse) → P.tx(PREPARED) → P.tx(COMMITTED)

Important points in this sequence

  • backup node commits before primary node
  • there is a GridNearPrepareResponse between between actual commits 

Then for 1PC the backup (or primary, if backups=0) is responsible for signing tx with CutVersion, not near node. CutVersion propogates between nodes in reverse order: near ← primary ← backup.

Signing messages

Ignite transaction protocol includes multiple messages. But only some of them affects meaningful (relating to the algorithm) that change state of transactions (PREPARED, COMMITTED):

  1. GridNearTxPrepareRequest / GridDhtTxPrepareRequest
  2. GridNearTxPrepareResponse / GridDhtTxPrepareResponse
  3. GridNearTxFinishRequest / GridDhtTxFinishRequest

Also some messages require to be signed with tx CutVersion to check it them on primary/backup node:

  1. GridNearTxFinishRequest / GridDhtTxFinishRequest
  2. GridNearTxPrepareResponse / GridDhtTxPrepareResponse (for 1PC algorithm).

Ignite components

On receiving a message with new CutVersion node sets it and commits LocalState and ChannelState - to identify wrong order of the events

  1. LocalState maps to local WAL (all of committed transactions are part of LocalState);
  2. ChannelState maps to `IgniteTxManager#activeTransactions`. 
  3. `IgniteTxManager#activeTransactions` doesn't track committing transactions - track them additionally in ConsistentCutManager .

Unstable topology

There are some cases to handle for unstable topology:

  1. Client or non-baseline server node leaves – no need to handle.
  2. Server node leaves:
    1. all nodes finish local running ConsistentCut, making them in-consistent
  3. Server node joins:
    1. all nodes finish local running ConsistentCut, making them in-consistent
    2. all nodes check ConsistentCutVersion received from new node, and update their local version if needed
    3. new node checks whether rebalance was required for recovering. If it is required, then handle it TBD

TBD: Which ways to use to avoid inconsistency between data and WAL after rebalance. There are options:

  1. First solution: set flag ConsistentCutManager#inconsistent  to true , and persist this flag within local MetaStorage.
    1. On receiving new ConsistentCutVersion check the flag and raise an exception.
    2. Clean flag on recovering from ClusterSnapshot, or after creating a ClusterSnapshot.
    3. + Simple handling in runtime.
    4. - Need to rebalance a single node after PITR with file-based (or other?) rebalance: more time for recovery, file-based rebalance has not-resolved issues yet(?). 

  2. Do not disable WAL during rebalance:
    + WAL now is consistent with data, can use it for PITR after rebalancing
    - Too many inserts during rebalance may affect rebalance speed, IO utilization

  3. Automatically create snapshots for rebalancing cache groups:
    + Guarantee of consistency (snapshot is created on PME, after rebalance).
    + Faster recovery.
    - Complexity of handling - Ignite should provide additional tool for merging WAL and multiple snapshots created in different time
    ? is it possible to create snapshot only on single rebalanced node?
    ? Is it possible to sync current PME (on node join) with starting snapshot?

  4. Write a special Rebalance record to WAL with description of demanded partition:
    1. During restore read this record and repeat the historical rebalance at this point, after rebalance resume recovery with existing WALs.
    2. In case Record contains full rebalance - stops recovering with WAL and fallback to full rebalance.
      ? Is it possible to rebalance only specific cache groups, and continue to WAL recovery for others.
      - For historical rebalance during recovery need separate logic for extracting records from WAL archives from other nodes. 

WAL records

There are 2 records: `ConsistentCutStartRecord` for Start event and `ConsistentCutFinishRecord` for Finish event. 

  • ConsistentCutStartRecord: record is written to WAL in moment when CC starts on a local node. It helps to limit amout of active transactions to check. But there is no strict guarantee for all transactions belonged to the BEFORE side to be physically committed before ConsistentCutStartRecord, and vice versa. This is the reason for having ConsistentCutFinishRecord.
  • ConsistentCutFinishRecord: This record is written to WAL after Consistent Cut stopped analyzing transactions and storing them in a particular bucket (BEFORE or AFTER).

It guarantees that the BEFORE side consist of:
1. transactions physically committed before ConsistentCutStartRecord and weren't included into ConsistentCutFinishRecord#after();
2. transactions physically committed between ConsistentCutStartRecord and ConsistentCutFinishRecord and were included into ConsistentCutFinishRecord#before().

It guarantees that the AFTER side consist of:
1. transactions physically committed before ConsistentCutStartRecord and were included into ConsistentCutFinishRecord#after();
2. transactions physically committed after ConsistentCutStartRecord and weren't included into ConsistentCutFinishRecord#before().


It proposes to set ConsistentCutStartRecord#ts  independently on every node, because this time is only approximation - it depends on time of delivering ConsistentCutVersion, time of finishing active transactions, actual time on every node and client app. Then actually there is no need to sync it on multiple nodes. 


ConsistentCutRecord
/** */
public class ConsistentCutStartRecord extends WALRecord {
    /** ConsistentCutVersion, counter on Ignite coordinator. */
    private final ConsistentCutVersion cutVer;

	/** Approximated timestamp of cut state. */
	private final long ts;
}


/** */
public class ConsistentCutFinishRecord extends WALRecord {
    /**
     * Collections of TXs committed BEFORE the ConsistentCut.
     */
    private final Set<GridCacheVersion> before;

     /**
     * Collections of TXs committed AFTER the ConsistentCut.
     */
    private final Set<GridCacheVersion> after;
 }

Links

  1. ON DISTRIBUTED SNAPSHOTS, Ten H. LAI and Tao H. YANG, 29 May 1987
  • No labels