61
Learning Greenplum Distributed Transaction Code Analysis & Call flow

Learning Greenplum Distributed Transaction

Embed Size (px)

Citation preview

Page 1: Learning Greenplum Distributed Transaction

Learning Greenplum Distributed TransactionCode Analysis & Call flow

Page 2: Learning Greenplum Distributed Transaction

Transaction Framework

Begin Transaction

ClientServer

EndTransaction

UPDATE

Transaction Block

Begin Transaction Block

End Transaction Block

Low level Transaction Layer

Execute SQL

PostgreSQLTransactionStateData-TBlockState : high-level stat-TransState : low-level state

Page 3: Learning Greenplum Distributed Transaction

QE Distributed Transaction

Transaction Framework

Begin Transaction

客户端Master

EndTransaction

UPDATE

Transaction Block

Begin Transaction Block

End Transaction Block

Low level Transaction Layer

Greenplum

Segment 0

Transaction Block

Begin Transaction Block

End Transaction Block

Low level Transaction Layer

Execute SQL

QD DTM

Execute SQL

QE Distributed Transaction

Segment 0

Transaction Block

Begin Transaction Block

End Transaction Block

Low level Transaction Layer

Execute SQL

Dtm Command

Dtm Command

TMGXACT-DtxState

TransactionStateData-TBlockState : high-level stat-TransState : low-level state

DtxContext

TransactionStateData-TBlockState : high-level stat-TransState : low-level state

DtxContext

Page 4: Learning Greenplum Distributed Transaction

Main Call Flow (Normal)Master(GP_ROLE_DISPATCH) Seg-N

exec_simple_query

start_xact_command

ProcessUtility

finish_xact_command

doQEDistributedExplicitBegin

setupQEDtxContext

performDtxProtocolCommand

exec_mpp_dtx_protocol_command

firstchar = ‘M’ only for “BEGIN”

firstchar = ‘T'

doDispatchDtxProtocolCommandDTX_PROTOCOL_COMMAND

Middle Layer function

low level function

Middle Layer function

low level function

Page 5: Learning Greenplum Distributed Transaction

Main Call Flow (Retry)Master(GP_ROLE_DISPATCH) Seg-N

doDtxPhase2Retry

StartTransactionCommand

doDispatchDtxProtocolCommand

CommitTransactionCommand

DTX_CONTEXT_QD_RETRY_PHASE_2

When DTX_STATE_RETRY_COMMIT_PREPARED

DTX_STATE_RETRY_COMMIT_PREPAREDDTX_STATE_RETRY_ABORT_PREPARED

doInsertForgetCommitted

performDtxProtocolCommand

exec_mpp_dtx_protocol_command

firstchar = ‘T'

doDispatchDtxProtocolCommandDTX_PROTOCOL_COMMAND

Page 6: Learning Greenplum Distributed Transaction

Middle Layer function

• Normal system's calls• StartTransactionCommand• CommitTransactionCommand• AbortCurrentTransaction

• User change calls by issue “BEGIN, COMMIT, ROLLBACK, SAVEPOINT, ROLLBACK TO or RELEASE”• BeginTransactionBlock• EndTransactionBlock• UserAbortTransactionBlock• PrepareTransactionBlock

• DefineSavepoint• RollbackToSavepoint• ReleaseSavepoint

Page 7: Learning Greenplum Distributed Transaction

Low level calls

• StartTransaction• CommitTransaction• AbortTransaction• CleanupTransaction• PrepareTransaction

• StartSubTransaction• CommitSubTransaction• AbortSubTransaction• CleanupSubTransaction

Page 8: Learning Greenplum Distributed Transaction

Postgres Normal middle level call flow

/ StartTransactionCommand;

/ StartTransaction;

1) < ProcessUtility; << BEGIN

\ BeginTransactionBlock;

\ CommitTransactionCommand;

/ StartTransactionCommand;

2) / ProcessQuery; << SELECT ...

\ CommitTransactionCommand;

\ CommandCounterIncrement;

/ StartTransactionCommand;

3) / ProcessQuery; << INSERT ...

\ CommitTransactionCommand;

\ CommandCounterIncrement;

/ StartTransactionCommand;

/ ProcessUtility; << COMMIT

4) < EndTransactionBlock;

\ CommitTransactionCommand;

\ CommitTransaction;

1) BEGIN2) SELECT * FROM foo3) INSERT INTO foo VALUES (...)4) COMMIT

Page 9: Learning Greenplum Distributed Transaction

High Level State transfer

TBLOCK_DEFAULT

TBLOCK_START

TBLOCK_ABORT_PENDING

TBLOCK_BEGIN

TBLOCK_INPROGRESS

TBLOCK_ABORT

TBLOCK_END

TBLOCK_ABORT_END

BeginTransactionBlock

StartTransactionCommand

AbortCurrentTransaction

UserAbortTransactionBlock

EndTransactionBlock

CommitTransactionCommandAbortCurrentTransaction

CommitTransactionCommandAbortCurrentTransaction

CommitTransactionCommandAbortCurrentTransaction

EndTransactionBlock

UserAbortTransactionBlock

AbortCurrentTransaction

AbortCurrentTransactionCommitTransactionCommand

PostgreSQL

TBLOCK_PREPARE

PrepareTransactionBlock

CommitTransactionCommand

failed xact failed xact

AbortOutOfAnyTransaction

AbortOutOfAnyTransaction

CommitTransactionCommand

Page 10: Learning Greenplum Distributed Transaction

High Level State transfer

TBLOCK_SUBEND

TBLOCK_SUBBEGIN

TBLOCK_SUBABORT_PENDING

TBLOCK_SUBINPROGRESS

TBLOCK_SUBABORT

TBLOCK_SUBABORT_RESTARTTBLOCK_SUBABORT_END

TBLOCK_SUBRESTART

PostgreSQL

CommitTransactionCommand

PushTransaction

ReleaseSavepoint

EndTransactionBlock

AbortCurrentTransaction

UserAbortTransactionBlock

EndTransactionBlock

EndTransactionBlock

UserAbortTransactionBlock

RollbackToSavepoint

RollbackToSavepoint

RollbackToSavepoint

RollbackToSavepoint

failed xact

failed xactfailed xact

Page 11: Learning Greenplum Distributed Transaction

Detailed transfer

TBLOCK_DEFAULT

TBLOCK_START

TBLOCK_ABORT_PENDING

StartTransactionCommand AbortCurrentTransaction

UserAbortTransactionBlock

CommitTransactionCommand

AbortCurrentTransaction

CommitTransactionCommand

Single Query Transaction

Error Happened

Rollback issued

Page 12: Learning Greenplum Distributed Transaction

Detailed transfer

TBLOCK_DEFAULT

TBLOCK_START

TBLOCK_BEGIN

TBLOCK_INPROGRESS

BeginTransactionBlock

StartTransactionCommandCommitTransactionCommand

Explicit Begin

AbortCurrentTransaction

Error Happened

Page 13: Learning Greenplum Distributed Transaction

Detailed transfer

TBLOCK_DEFAULTTBLOCK_INPROGRESS

TBLOCK_END

EndTransactionBlock

CommitTransactionCommandAbortCurrentTransaction

TBLOCK_PREPARE

PrepareTransactionBlock

CommitTransactionCommand

AbortOutOfAnyTransaction

Commit/End

Error Happened

Page 14: Learning Greenplum Distributed Transaction

Detailed transfer

TBLOCK_INPROGRESS

TBLOCK_ABORT

AbortCurrentTransaction

failed xact

SQL Error Happened in Transaction

Page 15: Learning Greenplum Distributed Transaction

Detailed transfer

TBLOCK_DEFAULT

TBLOCK_INPROGRESS

TBLOCK_ABORTTBLOCK_ABORT_END

CommitTransactionCommandAbortCurrentTransaction

EndTransactionBlock

UserAbortTransactionBlock

failed xactfailed xact

AbortOutOfAnyTransaction

Rollback

TBLOCK_ABORT_PENDING

CommitTransactionCommandAbortCurrentTransaction

UserAbortTransactionBlock

Page 16: Learning Greenplum Distributed Transaction

Low Level State transfer

TRANS_DEFAULT

TRANS_START

TRANS_COMMITTRANS_INPROGRESS

TRANS_ABORT

TRANS_PREPARE

StartTransactionStartTransaction

CommitTransaction

PrepareTransaction PrepareTransaction

CommitTransaction

AbortTransaction CleanupTransaction

Page 17: Learning Greenplum Distributed Transaction

TM Control Block - TMGXACT

char gid[TMGIDSIZE];

DistributedTransactionId gxid;

DtxState state;

int sessionId;

TransactionId localXid;

LocalDistribXactRef localDistribXactRef;

bool explicitBeginRemembered;

DistributedTransactionId xminDistributedSnapshot;

bool bumpedPhase1Count;

bool badPrepareGangs;

bool retryPhase2RecursionStop;

int debugIndex;

bool directTransaction;

uint16 directTransactionContentId;

Page 18: Learning Greenplum Distributed Transaction

TM Actions to RM

• Normal• doPrepareTransaction

• doDispatchDtxProtocolCommand(DTX_PROTOCOL_COMMAND_PREPARE)• doInsertForgetCommitted

• Insert FORGET COMMITTED into the xlog.• doNotifyingCommitPrepared

• doDispatchDtxProtocolCommand(DTX_PROTOCOL_COMMAND_COMMIT_PREPARED)• doNotifyingAbort

• doDispatchDtxProtocolCommand(DTX_PROTOCOL_COMMAND_ABORT_PREPARED)

• Crash Recovery• doNotifyCommittedInDoubt

• doDispatchDtxProtocolCommand(DTX_PROTOCOL_COMMAND_RECOVERY_COMMIT_PREPARED)

• doAbortInDoubt• doDispatchDtxProtocolCommand(DTX_PROTOCOL_COMMAND_RECOVERY_ABORT_PREP

ARED)

Page 19: Learning Greenplum Distributed Transaction

TM Actions to RM

• Others• sendDtxExplicitBegin

• dispatchDtxCommand(“BEGIN ISOLATION LEVEL %s, READ %s”)

• DefineDispatchSavepoint• dispatchDtxCommand(“savepoint”)

• ReleaseSavepoint• dispatchDtxCommand(“release savepoint”)

• DispatchRollbackToSavepoint• dispatchDtxCommand(“rollback to savepoint”)

Page 20: Learning Greenplum Distributed Transaction

TM dispatch command to RM

• doDispatchDtxProtocolCommand• cdbdisp_dispatchDtxProtocolCommand

• cdbdisp_dispatchToGang(GP_DISPATCH_COMMAND_TYPE_DTX_PROTOCOL)

• CdbCheckDispatchResult(&ds, DISPATCH_WAIT_NONE);• ----------------wait for QE------------------• cdbdisp_returnResults• cdbdisp_destroyDispatchThreads

Page 21: Learning Greenplum Distributed Transaction

DTM to RM protocol commands

• DTX_PROTOCOL_COMMAND_NONE

• DTX_PROTOCOL_COMMAND_STAY_AT_OR_BECOME_IMPLIED_WRITER = 1,

• DTX_PROTOCOL_COMMAND_ABORT_NO_PREPARED,

• DTX_PROTOCOL_COMMAND_PREPARE,

• DTX_PROTOCOL_COMMAND_ABORT_SOME_PREPARED,

• DTX_PROTOCOL_COMMAND_COMMIT_PREPARED,

• DTX_PROTOCOL_COMMAND_ABORT_PREPARED,

• DTX_PROTOCOL_COMMAND_RETRY_COMMIT_PREPARED,

• DTX_PROTOCOL_COMMAND_RETRY_ABORT_PREPARED,

• DTX_PROTOCOL_COMMAND_RECOVERY_COMMIT_PREPARED,

• DTX_PROTOCOL_COMMAND_RECOVERY_ABORT_PREPARED,

• DTX_PROTOCOL_COMMAND_SUBTRANSACTION_BEGIN_INTERNAL,

• DTX_PROTOCOL_COMMAND_SUBTRANSACTION_ROLLBACK_INTERNAL,

• DTX_PROTOCOL_COMMAND_SUBTRANSACTION_RELEASE_INTERNAL,

Page 22: Learning Greenplum Distributed Transaction

TM State transfer

DTX_STATE_NONE

DTX_STATE_ACTIVE_DISTRIBUTED

DTX_STATE_PREPARING

DTX_STATE_INSERTING_COMMITTEDDTX_STATE_INSERTED_COMMITTED

DTX_STATE_FORCED_COMMITTED DTX_STATE_INSERTING_FORGET_COMMITTEDDTX_STATE_INSERTED_FORGET_COMMITTED

DTX_STATE_NOTIFYING_COMMIT_PREPARED

doPrepareTransaction

DTX_STATE_PREPARED

doPrepareTransaction

DTX_STATE_NOTIFYING_ABORT_SOME_PREPARED

DTX_STATE_NOTIFYING_ABORT_NO_PREPARED

DTX_STATE_NOTIFYING_ABORT_PREPARED

RecordTransactionCommit/ forcedDistributedCommitted

doNotifyingCommitPrepared doInsertForgetCommitted

RecordTransactionCommit/insertingDistributedCommitted

rollbackDtxTransaction

DTX_STATE_RETRY_ABORT_PREPAREDrollbackDtxTransaction(badPrepareGangs)

rollbackDtxTransaction(badPrepare)

rollbackDtxTransaction(xa rollback??)

DTX_STATE_RETRY_COMMIT_PREPARED

doNotifyingCommitPrepared(commit failed)doDtxPhase2Retry/doInsertForgetCommitted

createDtx/initGxact

startTransaction/createDtx

DTX_STATE_ACTIVE_NOT_DISTRIBUTEDdtmPreCmd

Page 23: Learning Greenplum Distributed Transaction

DTM State

• Uninitialized transaction• DTX_STATE_NONE = 0

• The distributed transaction is active• DTX_STATE_ACTIVE_NOT_DISTRIBUTED //auto-commit on the QEs

• DTX_STATE_ACTIVE_DISTRIBUTED //it is explicit or an implicit writer transaction

Page 24: Learning Greenplum Distributed Transaction

DTM State

• For two-phase commit, the first phase is about to run• DTX_STATE_PREPARING

• For two-phase commit, the first phase has completed• DTX_STATE_PREPARED

• DTX_STATE_INSERTING_COMMITTED

• DTX_STATE_INSERTED_COMMITTED

• DTX_STATE_FORCED_COMMITTED

• DTX_STATE_NOTIFYING_COMMIT_PREPARED

• DTX_STATE_INSERTING_FORGET_COMMITTED

• DTX_STATE_INSERTED_FORGET_COMMITTED

Page 25: Learning Greenplum Distributed Transaction

DTM State

• Transaction rollback has been requested and QD is notifying all QD processes• no QEs have started work on the first phase of two-phase commit

• DTX_STATE_NOTIFYING_ABORT_NO_PREPARED

• at least one QE has done the first phase of two-phase commit• DTX_STATE_NOTIFYING_ABORT_SOME_PREPARED

• the QE processes have done the first phase of two-phase commit• DTX_STATE_NOTIFYING_ABORT_PREPARED,• DTX_STATE_RETRY_COMMIT_PREPARED,• DTX_STATE_RETRY_ABORT_PREPARED,• DTX_STATE_CRASH_COMMITTED

Page 26: Learning Greenplum Distributed Transaction

DTX Context

• DTX_CONTEXT_LOCAL_ONLY • There is no distributed transaction. This is the initial state and, for

utility mode connections or master-only queries, the only state. It is also the state to which the QD and QE return to between transactions.

• DTX_CONTEXT_QD_DISTRIBUTED_CAPABLE,• On QD: the process is currently part of a distributed transaction.

Whether or not the transaction has been started on a QE is not a part of the QD state -- that is tracked by assigning one of the DTX_CONTEXT_QE* values on the QE process, and by updating the state field of the currentGxact.

• DTX_CONTEXT_QD_RETRY_PHASE_2,• On QD: indicates that the second-phase of the two-phase commit is

being retried.

Page 27: Learning Greenplum Distributed Transaction

DTX Context

• DTX_CONTEXT_QE_ENTRY_DB_SINGLETON,• TODO: how is something an Entry db? fix this documentation

• DTX_CONTEXT_QE_AUTO_COMMIT_IMPLICIT,• On a QE that is the root of a query, this context means that the distributed transaction was opened implicitly by a non-writing query in an auto-commit

transaction.

• This is essentially the same as _TWO_PHASE_IMPLICIT_WRITER except we know the query won't dirty anything and so we don't need a two-phase commit.

• DTX_CONTEXT_QE_TWO_PHASE_EXPLICIT_WRITER,• On a QE, this context means that the distributed transaction was opened explicitly by a BEGIN query statement Note that this state can happen even if there

are no writer queries (because this state is entered when the BEGIN statement is processed).

• DTX_CONTEXT_QE_TWO_PHASE_IMPLICIT_WRITER,• On a QE that is the root of a query, this context means that the distributed transaction was opened implicitly by a writing query in an auto-commit transaction,

and this is the writing QE process Same as _AUTO_COMMIT_IMPLICIT except that we need a two-phase of the dirty data.

• DTX_CONTEXT_QE_READER,• On a QE that is not the root of a query, this context is used. This can happen regardless of whether the overall transaction for the query is implicit or explicit,

and auto-commit vs two-phase

• DTX_CONTEXT_QE_PREPARED,• On the QE, the two-phase commit has been prepared (first phase complete).

• DTX_CONTEXT_QE_FINISH_PREPARED• On the QE, the two-phase commit has been committed or aborted (second phase complete).

Page 28: Learning Greenplum Distributed Transaction

TM: ProcessUtility

• case nodeTag(parsetree) = T_TransactionStmt• Case Stmt->kind:

• -----normal-----• TRANS_STMT_BEGIN or TRANS_STMT_START

• BeginTransactionBlock

• sendDtxExplicitBegin

• TRANS_STMT_COMMIT• EndTransactionBlock

• TRANS_STMT_ROLLBACK• UserAbortTransactionBlock

Page 29: Learning Greenplum Distributed Transaction

TM/RM : ProcessUtility

• case nodeTag(parsetree) = T_TransactionStmt• Case Stmt->kind:

• -----savepoint-----• TRANS_STMT_SAVEPOINT

• RequireTransactionChain• sendDtxExplicitBegin• DefineDispatchSavepoint

• TRANS_STMT_RELEASE• RequireTransactionChain• ReleaseSavepoint

• TRANS_STMT_ROLLBACK_TO• RequireTransactionChain• RollbackToSavepoint

Page 30: Learning Greenplum Distributed Transaction

RM: ProcessUtility ??? XA???

• case nodeTag(parsetree) = T_TransactionStmt• Case Stmt->kind:

• -----I think it’s XA feature, not know when called in RM----• TRANS_STMT_PREPARE

• PrepareTransactionBlock

• TRANS_STMT_COMMIT_PREPARED • PreventTransactionChain

• FinishPreparedTransaction

• TRANS_STMT_ROLLBACK_PREPARED • PreventTransactionChain

• FinishPreparedTransaction

Page 31: Learning Greenplum Distributed Transaction

RM: doQEDistributedExplicitBegin

• Handle ExplicitBegin• StartTransactionCommand();• BeginTransactionBlock();• CommitTransactionCommand();

Page 32: Learning Greenplum Distributed Transaction

RM: performDtxProtocolCommand

• Case dtxProtocolCommand• DTX_PROTOCOL_COMMAND_STAY_AT_OR_BECOME_IMPLIED_WRITER

• Case DistributedTransactionContext:• DTX_CONTEXT_LOCAL_ONLY

• setupQEDtxContext• StartTransactionCommand

• DTX_CONTEXT_QE_TWO_PHASE_IMPLICIT_WRITER

• DTX_PROTOCOL_COMMAND_ABORT_NO_PREPARED• AbortOutOfAnyTransaction

• DTX_PROTOCOL_COMMAND_PREPARE• Case DistributedTransactionContext:

• DTX_CONTEXT_QE_TWO_PHASE_EXPLICIT_WRITER or DTX_CONTEXT_QE_TWO_PHASE_IMPLICIT_WRITER• performDtxProtocolPrepare

• DTX_PROTOCOL_COMMAND_ABORT_SOME_PREPARED• Case DistributedTransactionContext:

• DTX_CONTEXT_QE_TWO_PHASE_EXPLICIT_WRITER or DTX_CONTEXT_QE_TWO_PHASE_IMPLICIT_WRITER• AbortOutOfAnyTransaction

• DTX_CONTEXT_QE_PREPARED• performDtxProtocolAbortPrepared

• DTX_PROTOCOL_COMMAND_COMMIT_PREPARED• performDtxProtocolCommitPrepared

• DTX_PROTOCOL_COMMAND_ABORT_PREPARED• performDtxProtocolAbortPrepared

Page 33: Learning Greenplum Distributed Transaction

RM: performDtxProtocolCommand

• Case dtxProtocolCommand• DTX_PROTOCOL_COMMAND_RETRY_COMMIT_PREPARED

• performDtxProtocolCommitPrepared• DTX_PROTOCOL_COMMAND_RETRY_ABORT_PREPARED

• performDtxProtocolAbortPrepared• DTX_PROTOCOL_COMMAND_RECOVERY_COMMIT_PREPARED

• performDtxProtocolCommitPrepared• DTX_PROTOCOL_COMMAND_RECOVERY_ABORT_PREPARED

• performDtxProtocolAbortPrepared

• DTX_PROTOCOL_COMMAND_SUBTRANSACTION_BEGIN_INTERNAL• Case DistributedTransactionContext:

• DTX_CONTEXT_LOCAL_ONLY• setupQEDtxContext• StartTransactionCommand

• BeginInternalSubTransaction• DTX_PROTOCOL_COMMAND_SUBTRANSACTION_ROLLBACK_INTERNAL

• RollbackAndReleaseCurrentSubTransaction• DTX_PROTOCOL_COMMAND_SUBTRANSACTION_RELEASE_INTERNAL

• ReleaseCurrentSubTransaction

Page 34: Learning Greenplum Distributed Transaction

Log Type

• Nomal log:• #define XLOG_XACT_COMMIT 0x00• #define XLOG_XACT_ABORT 0x20

• 2PC Log• RM Log

• #define XLOG_XACT_PREPARE 0x10• #define XLOG_XACT_COMMIT_PREPARED 0x30• #define XLOG_XACT_ABORT_PREPARED 0x40

• TM log• #define XLOG_XACT_DISTRIBUTED_COMMIT 0x50• #define XLOG_XACT_DISTRIBUTED_FORGET 0x60

Page 35: Learning Greenplum Distributed Transaction

TM Log write

• RecordTransactionCommitif (isDtxPrepared)

{

XLogInsert(RM_XACT_ID, XLOG_XACT_DISTRIBUTED_COMMIT)

} else {

XLogInsert(RM_XACT_ID, XLOG_XACT_COMMIT);

}

XLogFlush()

• RecordDistributedForgetCommittedXLogInsert(RM_XACT_ID, XLOG_XACT_DISTRIBUTED_FORGET | XLOG_NO_TRAN)

Page 36: Learning Greenplum Distributed Transaction

TM Log write

• XLOG_XACT_DISTRIBUTED_COMMIT• XLogRecData rdata[4];

0

1

2

3

XLogRecData

Data: xl_xact_commitLen: MinSizeOfXactCommit

Data: persistentCommitBufferLen: persistentCommitSerializeLen

Data: childrenLen: nchildren * sizeof(TransactionId)

Data: TMGXACT_LOGLen: sizeof(TMGXACT_LOG)

persistentCommitObjectCount > 0

nchildren > 0

isDtxPrepared

Page 37: Learning Greenplum Distributed Transaction

TM Log write

• XLOG_XACT_DISTRIBUTED_FORGET• XLogRecData rdata[4];

0

XLogRecData

Data: xl_xact_distributed_forgetLen: sizeof(xl_xact_distributed_forget)

TMGXACT_LOG

Page 38: Learning Greenplum Distributed Transaction

RM Log write

• Prepare LOG• Log state format

1. TwoPhaseFileHeader2. TransactionId[] (subtransactions)3. RelFileNode[] (files to be deleted at commit)4. RelFileNode[] (files to be deleted at abort)5. TwoPhaseRecordOnDisk6. ...7. TwoPhaseRecordOnDisk (end sentinel, rmid == TWOPHASE_RM_END_ID)8. CRC32

• XLog

Page 39: Learning Greenplum Distributed Transaction

RM Log write

• Log state format

Page 40: Learning Greenplum Distributed Transaction

RM Log write

• XLOG_XACT_PREPARE• XLogRecData rdata[n];

0

1

2

XLogRecDataData: TwoPhaseFileHeader

Len: TwoPhaseFileHeader or 512

Data: childrenLen: nchildren * sizeof(TransactionId)

Data: persistentCommitBufferLen: persistentCommitSerializeLen

nchildren > 0

persistentCommitObjectCount > 0

3Data: TwoPhaseRecordOnDisk

(TWOPHASE_RM_NOTIFY_ID)Len: sizeof(TwoPhaseRecordOnDisk)

3Data: relname

Len: sizeof(relname)

4Data: TwoPhaseRecordOnDisk

(TWOPHASE_RM_FLATFILES_ID)Len: sizeof(TwoPhaseRecordOnDisk)

info != 0

5Data: TwoPhaseRecordOnDisk

(TWOPHASE_RM_INVAL_ID)Len: sizeof(TwoPhaseRecordOnDisk)

RelcacheInitFileInval != NULL

6 x nData: TwoPhaseRecordOnDisk(TWOPHASE_RM_LOCK_ID)

Len: sizeof(TwoPhaseRecordOnDisk)

Has locks

6 x nData: TwoPhaseLockRecord

Len: sizeof(TwoPhaseLockRecord)

Has locks

7 x nData: TwoPhaseRecordOnDisk

(TWOPHASE_RM_PGSTAT_ID)Len: sizeof(TwoPhaseRecordOnDisk)

7 x nData: TwoPhasePgStatRecord

Len: sizeof(TwoPhasePgStatRecord)

xact_state != NULL

8Data: TwoPhaseRecordOnDisk(TWOPHASE_RM_END_ID)

Len: sizeof(TwoPhaseRecordOnDisk)

Page 41: Learning Greenplum Distributed Transaction

RM Log write

• XLOG_XACT_DISTRIBUTED_COMMIT• XLogRecData rdata[4]

0

1

2

XLogRecData

Data: xl_xact_commit_preparedLen: MinSizeOfXactCommitPrepared

Data: persistentCommitBufferLen: persistentCommitSerializeLen

Data: childrenLen: nchildren * sizeof(TransactionId)

persistentPrepareObjectCount > 0

nsubxacts > 0

Page 42: Learning Greenplum Distributed Transaction

RM Log write

• XLOG_XACT_DISTRIBUTED_COMMIT• XLogRecData rdata[3];

0

1

2

XLogRecData

Data: xl_xact_abort_preparedLen: MinSizeOfXactAbortPrepared

Data: persistentCommitBufferLen: persistentCommitSerializeLen

Data: childrenLen: nchildren * sizeof(TransactionId)

persistentCommitObjectCount > 0

nchildren > 0

Page 43: Learning Greenplum Distributed Transaction

RM Log write

• EndPrepareXLogInsert(RM_XACT_ID, XLOG_XACT_PREPARE)XLogFlush()

• RecordTransactionAbortXLogInsert(RM_XACT_ID, XLOG_XACT_ABORT)XLogFlush() only when we are deleting files

• RecordTransactionCommitPreparedXLogInsert(RM_XACT_ID, XLOG_XACT_COMMIT_PREPARED | XLOG_NO_TRAN)XLogFlush()

• RecordTransactionAbortPreparedXLogInsert(RM_XACT_ID, XLOG_XACT_ABORT_PREPARED | XLOG_NO_TRAN)XLogFlush()

Page 44: Learning Greenplum Distributed Transaction

Log Write Seq when CommitMaster(GP_ROLE_DISPATCH) Seg-N

XLogFlush

XLogInsert (RM_XACT_ID, XLOG_XACT_PREPARE)

XLogInsert (RM_XACT_ID, XLOG_XACT_DISTRIBUTED_COMMIT)

XLogFlush

XLogInsert (RM_XACT_ID, XLOG_XACT_DISTRIBUTED_FORGET | XLOG_NO_TRAN)

XLogFlushXLogInsert (RM_XACT_ID, XLOG_XACT_COMMIT_PREPARED | XLOG_NO_TRAN)

XLogInsert(RM_XACT_ID, XLOG_XACT_ABORT)

XLogFlushXLogInsert (RM_XACT_ID, XLOG_XACT_ABORT_PREPARED | XLOG_NO_TRAN)

XLogInsert (RM_XACT_ID, XLOG_XACT_DISTRIBUTED_FORGET | XLOG_NO_TRAN)

XLogFlushXLogInsert (RM_XACT_ID, XLOG_XACT_COMMIT_PREPARED | XLOG_NO_TRAN)

Commit

Rollback

Retry Commit

Retry Rollback

Page 45: Learning Greenplum Distributed Transaction

Test Details

• BEGIN• INSERT

• END• MANUNALLY ROLLBACK• INSERT FAILED WITH AUTO ROLLBACK

• Kill RM-0 before master write log, RM-1 Prepared• Kill RM-0 when master send commit to RMs, RM all Prepared• Error happened when master send commit to RMs, no Gangs

errors

• Crash recovery

Page 46: Learning Greenplum Distributed Transaction

PostgresMain

Master(GP_ROLE_DISPATCH) Seg-N

PostgresMainfirstchar = 'Q'

exec_simple_query

start_xact_command

StartTransactionCommand

StartTransactionBlock state: TBLOCK_DEFAULT -> TBLOCK_STARTED

State: TRANS_DEFAULT -> TRANS_START

PortalRun

ProcessUtilityTRANS_STMT_START

BeginTransactionBlockBlock state: TBLOCK_STARTED -> TBLOCK_BEGIN

sendDtxExplicitBegin

DTX_CONTEXT_QD_DISTRIBUTED_CAPABLE

createDtxGxact->state : DTX_STATE_NONE -> DTX_STATE_ACTIVE_NOT_DISTRIBUTED

dispatchDtxCommand

cdbdisp_dispatchRMCommand

cdbdisp_dispatchCommand

cdbdisp_dispatchToGang

CdbCheckDispatchResult

cdbdisp_returnResults

cdbdisp_destroyDispatchThreads

CommitTransactionCommand

finish_xact_command

Block state: TBLOCK_BEGIN -> TBLOCK_INPROGRESS

doQEDistributedExplicitBegin

StartTransactionCommand

BeginTransactionBlock

CommitTransactionCommand

setupQEDtxContext

needTwoPhase = false; explicitBegin = true

Block state: TBLOCK_DEFAULT -> TBLOCK_STARTED

State: TRANS_DEFAULT -> TRANS_START

StartTransaction

firstchar = ‘M'

Block state: TBLOCK_STARTED -> TBLOCK_BEGIN

Block state: TBLOCK_BEGIN -> TBLOCK_INPROGRESS

State: TRANS_START -> TRANS_INPROGRESS

State: TRANS_START -> TRANS_INPROGRESS

pq_flush

ReadyForQuery_QEWriter

DTX_CONTEXT_LOCAL_ONLY

DistributedTransactionContext: DTX_CONTEXT_LOCAL_ONLY -> DTX_CONTEXT_QE_TWO_PHASE_EXPLICIT_

BEGIN;

--------------------wait----------------------

dtmPreCommandGxact->state: DTX_STATE_ACTIVE_NOT_DISTRIBUTED -> DTX_STATE_ACTIVE_DISTRIBUTED

Page 47: Learning Greenplum Distributed Transaction

PostgresMain

Master(GP_ROLE_DISPATCH) Seg-N

PostgresMain

firstchar = 'Q'

exec_simple_query

start_xact_command

StartTransactionCommandTBLOCK_INPROGRESS

PortalRun

PortalRunMulti

ProcessQueryExecutorStart

start_xact_command

exec_mpp_query

firstchar = ‘M'

dtmPreCommand

StartTransactionCommand

INSERT ;

CommitTransactionCommand

finish_xact_command

--------------------wait----------------------

PortalRun

PortalRunMulti

ProcessQueryExecutorRun

ExecutePlan

ExecInsert

heap_insertheap_log_tuple_insert

heap_log_tuple_insertXLogInsert (RM_HEAP_ID)

CommitTransactionCommand

finish_xact_command

ExecutorEndmppExecutorFinishup

CdbCheckDispatchResult

CommandId++

Page 48: Learning Greenplum Distributed Transaction

PostgresMain

Master(GP_ROLE_DISPATCH) Seg-N

PostgresMain

firstchar = 'Q'

exec_simple_query

start_xact_command

StartTransactionCommandTBLOCK_INPROGRESS

PortalRun

ProcessUtilityTRANS_STMT_COMMIT

EndTransactionBlockBlock state: TBLOCK_INPROGRESS -> TBLOCK_END

CommitTransactionCommand

finish_xact_command

BeginCommand

performDtxProtocolCommand

exec_mpp_dtx_protocol_command

State: TRANS_BEGIN -> TRANS_PREPARE

firstchar = ‘T'

CommitTransaction

prepareDtxTransaction

dtxProtocolCommand = DTX_PROTOCOL_COMMAND_PREPARE

DistributedTransactionContext == DTX_CONTEXT_QE_TWO_PHASE_EXPLICIT_WRITER

performDtxProtocolPrepare

StartTransactionCommand

PrepareTransactionBlock

CommitTransactionCommand

DTX_CONTEXT_QE_TWO_PHASE_EXPLICIT_WRITER -> DTX_CONTEXT_QE_PREPARED

EndTransactionBlockBlock state: TBLOCK_INPROGRESS -> TBLOCK_END

Block state: TBLOCK_END -> TBLOCK_PREPARE

PrepareTransaction

XLogFlush

Block state: TBLOCK_PREPARE -> TBLOCK_DEFAULT

pq_flush

ReadyForQuery_QEWriter

END/COMMIT 1;

doPrepareTransactionGxactstate: DTX_STATE_ACTIVE_DISTRIBUTED ->DTX_STATE_PREPARING

doDispatchDtxProtocolCommandDTX_PROTOCOL_COMMAND_PREPARE

cdbdisp_dispatchDtxProtocolCommand

Gxactstate: DTX_STATE_PREPARING -> DTX_STATE_PREPARED

cdbdisp_returnResults

EndPrepare

--------------------wait---------------------- XLogInsert (RM_XACT_ID, XLOG_XACT_PREPARE)

State: TRANS_INPROGRESS -> TRANS_COMMIT

State: TRANS_PREPARE -> TRANS_DEFAULT

StartPreparegxact = MarkAsPreparing()

Page 49: Learning Greenplum Distributed Transaction

Master(GP_ROLE_DISPATCH) Seg-N

PostgresMain

BeginCommandperformDtxProtocolCommand

exec_mpp_dtx_protocol_commandfirstchar = ‘T'

dtxProtocolCommand = DTX_PROTOCOL_COMMAND_COMMIT_PREPARED

performDtxProtocolCommitPreparedStartTransactionCommand

FinishPreparedTransaction

CommitTransactionCommand

DTX_CONTEXT_QE_PREPARED -> DTX_CONTEXT_LOCAL_ONLY

FinishPreparedTransaction

Block state: TBLOCK_DEFAULT -> TBLOCK_STARTED

Block state: TBLOCK_STARTED -> TBLOCK_DEFAULT

State: TRANS_DEFAULT -> TRANS_START

StartTransaction

DTX_CONTEXT_QE_PREPARED -> DTX_CONTEXT_QE_FINISH_PREPARED

State: TRANS_START -> TRANS_INPROGRESS

State:TRANS_INPROGRESS ->TRANS_COMMIT

CommitTransaction

RecordTransactionCommitXLogFlush

END/COMMIT 2;

doDispatchDtxProtocolCommandDTX_PROTOCOL_COMMAND_COMMIT_PREPARED

cdbdisp_dispatchDtxProtocolCommand

cdbdisp_returnResults

doNotifyingCommitPrepared

notifyCommittedDtxTransaction

Dtx state : DTX_STATE_FORCED_COMMITTED -> DTX_STATE_NOTIFYING_COMMIT_PREPARED

doInsertForgetCommittedDtx state : DTX_STATE_NOTIFYING_COMMIT_PREPARED -> DTX_STATE_INSERTING_FORGET_COMMITTED

Dtx state : DTX_STATE_NOTIFYING_COMMIT_PREPARED -> DTX_STATE_INSERTED_FORGET_COMMITTED

doInsertForgetCommitted

RecordDistributedForgetCommitted

RecordTransactionCommit

forcedDistributedCommittedDtx state : DTX_STATE_INSERTED_COMMITTED-> DTX_STATE_FORCED_COMMITTED

--------------------wait----------------------

insertingDistributedCommittedDtx state : DTX_STATE_PREPARED -> DTX_STATE_INSERTING_COMMITTED

XLogInsert (RM_XACT_ID, XLOG_XACT_DISTRIBUTED_COMMIT)

insertedDistributedCommittedDtx state : DTX_STATE_INSERTING_COMMITTED -> DTX_STATE_INSERTED_COMMITTED

RecordTransactionCommitPrepared

XLogFlush

CommitTransactionCommand

finish_xact_command

CommitTransaction

……

……

XLogFlush

……

XLogInsert (RM_XACT_ID, XLOG_XACT_COMMIT_PREPARED

XLogInsert (RM_XACT_ID, XLOG_XACT_DISTRIBUTED_FORGET | XLOG_NO_TRAN)

Block state: TBLOCK_END -> TBLOCK_DEFAULT

State:TRANS_COMMIT ->TRANS_DEFAULT

State:TRANS_COMMIT ->TRANS_DEFAULT

Page 50: Learning Greenplum Distributed Transaction

PostgresMain

Master(GP_ROLE_DISPATCH) Seg-N

PostgresMain

firstchar = 'Q'

exec_simple_query

start_xact_command

StartTransactionCommandTBLOCK_INPROGRESS

PortalRun

ProcessUtilityTRANS_STMT_ROLLBACK

UserAbortTransactionBlockBlock state: TBLOCK_INPROGRESS -> TBLOCK_ABORT_PENDING

CommitTransactionCommand

finish_xact_command

BeginCommand

performDtxProtocolCommand

exec_mpp_dtx_protocol_command

firstchar = ‘T'

AbortTransaction

rollbackDtxTransaction

dtxProtocolCommand = DTX_PROTOCOL_COMMAND_ABORT_NO_PREPARED

DistributedTransactionContext == DTX_CONTEXT_QE_TWO_PHASE_EXPLICIT_WRITER

AbortOutOfAnyTransaction

pq_flush

ReadyForQuery_QEWriter

ROLLBACK;

Gxactstate: DTX_STATE_ACTIVE_DISTRIBUTED ->DTX_STATE_NOTIFYING_ABORT_NO_PREPARED

doDispatchDtxProtocolCommandDTX_PROTOCOL_COMMAND_ABORT_NO_PREPARED

cdbdisp_dispatchDtxProtocolCommand

cdbdisp_returnResults

--------------------wait----------------------

CleanupTransaction

Block state: TBLOCK_ABORT_PENDING -> TBLOCK_DEFAULT

State: TRANS_INPROGRESS -> TRANS_ABORT

doNotifyingAbort

AbortTransaction

CleanupTransaction

Block state: TBLOCK_ABORT_PENDING -> TBLOCK_DEFAULT

State: TRANS_INPROGRESS -> TRANS_ABORT

State: TRANS_ABORT -> TRANS_DEFAULT

State: TRANS_ABORT -> TRANS_DEFAULT

RecordTransactionAbortXLogInsert(RM_XACT_ID, XLOG_XACT_ABORT)

Page 51: Learning Greenplum Distributed Transaction

PostgresMain

Master(GP_ROLE_DISPATCH) Seg-N

PostgresMain

firstchar = 'Q'

AbortCurrentTransaction

AbortTransaction BeginCommand

performDtxProtocolCommand

exec_mpp_dtx_protocol_command

firstchar = ‘T'

rollbackDtxTransactiondtxProtocolCommand = DTX_PROTOCOL_COMMAND_ABORT_NO_PREPARED

DistributedTransactionContext == DTX_CONTEXT_QE_TWO_PHASE_EXPLICIT_WRITER

AbortOutOfAnyTransaction

pq_flush

ReadyForQuery_QEWriter

INSERT FAILED;

Gxactstate: DTX_STATE_ACTIVE_DISTRIBUTED ->DTX_STATE_NOTIFYING_ABORT_NO_PREPARED

doDispatchDtxProtocolCommandDTX_PROTOCOL_COMMAND_ABORT_NO_PREPARED

cdbdisp_dispatchDtxProtocolCommand

cdbdisp_returnResults

--------------------wait----------------------

CleanupTransaction

Block state: TBLOCK_ABORT_END -> TBLOCK_DEFAULT

State: TRANS_INPROGRESS -> TRANS_ABORT

doNotifyingAbort

AbortTransaction

CleanupTransaction

Block state: TBLOCK_ABORT_PENDING -> TBLOCK_DEFAULT

State: TRANS_INPROGRESS -> TRANS_ABORT

State: TRANS_ABORT -> TRANS_DEFAULT

State: TRANS_ABORT -> TRANS_DEFAULT

RecordTransactionAbortXLogInsert(RM_XACT_ID, XLOG_XACT_ABORT)

Need to call rollback or end manually here!!!PostgresMain

firstchar = 'Q'

exec_simple_querystart_xact_command

StartTransactionCommand

Block state: TBLOCK_INPROGRESS -> TBLOCK_ABORT

PortalRunProcessUtility

TRANS_STMT_ROLLBACK

UserAbortTransactionBlockBlock state: TBLOCK_ABORT -> TBLOCK_ABORT_END

CommitTransactionCommandfinish_xact_command

Page 52: Learning Greenplum Distributed Transaction

Master(GP_ROLE_DISPATCH) Seg-N

Kill RM-0 before master write log, RM-1 Prepared

--------------------some RM Failed----------------------

PostgresMain

CommitTransactionCommand

finish_xact_command

CommitTransaction

prepareDtxTransaction

doPrepareTransaction

AbortCurrentTransaction

TRANS_INPROGRESSTBLOCK_ENDDTX_STATE_PREPARING

AbortTransaction

rollbackDtxTransaction

Gxactstate: DTX_STATE_PREPARING -> DTX_STATE_RETRY_ABORT_PREPARED

State: TRANS_INPROGRESS -> TRANS_ABORT

disconnectAndDestroyAllGangs

GP Kill RM-1

doDtxPhase2Retry

StartTransactionCommand

DTX_STATE_RETRY_COMMIT_PREPARED

StartTransaction

TRANS_DEFAULT -> TRANS_START

doDispatchDtxProtocolCommand

GP Renew all RMs

PostgresMain

BeginCommand

performDtxProtocolCommand

exec_mpp_dtx_protocol_command

firstchar = ‘T'

dtxProtocolCommand = DTX_PROTOCOL_COMMAND_ABORT_PREPARED

performDtxProtocolAbortPrepared

StartTransactionCommand

FinishPreparedTransaction

CommitTransactionCommand

DTX_CONTEXT_LOCAL_ONLY -> DTX_CONTEXT_QD_RETRY_PHASE_2

DTX_CONTEXT_QD_DISTRIBUTED_CAPABLE -> DTX_CONTEXT_LOCAL_ONLY

CommitTransactionCommand

DTX_CONTEXT_QD_RETRY_PHASE_2 -> DTX_CONTEXT_LOCAL_ONLY

finishDistributedTransactionContext

Do nothing on RM-0

Do Abort Prepared on RM-1

performDtxProtocolAbortPrepared

StartTransactionCommandFinishPreparedTransaction

CommitTransactionCommand

RecordTransactionAbortPrepared

XLogFlushXLogInsert (RM_XACT_ID, XLOG_XACT_ABORT_PREPARED

GP Kill all RMs then

DTX_CONTEXT_QE_PREPARED -> DTX_CONTEXT_QE_FINISH_PREPARED

Page 53: Learning Greenplum Distributed Transaction

Master(GP_ROLE_DISPATCH) Seg-N

Kill RM-0 when master send commit to RMs

--------------------some RM Failed----------------------

PostgresMain

disconnectAndDestroyAllGangs

GP Kill RM-1

doDtxPhase2Retry

StartTransactionCommandDTX_STATE_RETRY_COMMIT_PREPARED

StartTransactionTRANS_DEFAULT -> TRANS_START

DTX_PROTOCOL_COMMAND_COMMIT_PREPARED

doDispatchDtxProtocolCommand

GP Renew all RMs

DTX_CONTEXT_LOCAL_ONLY -> DTX_CONTEXT_QD_RETRY_PHASE_2

DTX_CONTEXT_QD_DISTRIBUTED_CAPABLE -> DTX_CONTEXT_LOCAL_ONLY

CommitTransactionCommandDTX_CONTEXT_QD_RETRY_PHASE_2 -> DTX_CONTEXT_LOCAL_ONLY

Do nothing on RM-0

Do Commit Prepared on RM-1

performDtxProtocolCommitPrepared

StartTransactionCommandFinishPreparedTransaction

CommitTransactionCommand

RecordTransactionCommitPrepared

XLogFlushXLogInsert (RM_XACT_ID, XLOG_XACT_COMMIT_PREPARED

doInsertForgetCommittedDtx state : DTX_STATE_RETRY_COMMIT_PREPARED -> DTX_STATE_INSERTING_FORGET_COMMITTED

Dtx state : DTX_STATE_INSERTING_FORGET_COMMITTED -> DTX_STATE_INSERTED_FORGET_COMMITTED

doInsertForgetCommitted

RecordDistributedForgetCommitted

XLogInsert (RM_XACT_ID, XLOG_XACT_DISTRIBUTED_FORGET | XLOG_NO_TRAN)

doDispatchDtxProtocolCommand

DTX_CONTEXT_QE_PREPARED -> DTX_CONTEXT_QE_FINISH_PREPARED

RecordTransactionCommitCommitTransaction

XLogFlush

Dtx state : DTX_STATE_NOTIFYING_COMMIT_PREPARED-> DTX_STATE_RETRY_COMMIT_PREPARED

Page 54: Learning Greenplum Distributed Transaction

Master(GP_ROLE_DISPATCH) Seg-N

Kill all after RMs write prepare log

---------------------------- ---------------------------- ------------------- Kill All TM/RMs and restart ------------------- ---------------------------- --

PostgresMain

DTX_PROTOCOL_COMMAND_PREPARE

doDispatchDtxProtocolCommand

PostgresMain

exec_mpp_dtx_protocol_command

firstchar = ‘T'

XLogFlush

XLogInsert (RM_XACT_ID, XLOG_XACT_PREPARE)

Gp_role == GP_ROLE_DISPATCH

initTMinitTM_recover_as_needed

recoverTMrecoverInDoubtTransactions

gatherRMInDoubtTransactions

cdbdisp_dispatchRMCommand(select gid from pg_prepared_xacts)

First char: 'M';Return indoubt transactions

abortRMInDoubtTransactionsdoAbortInDoubt

doDispatchDtxProtocolCommand

performDtxProtocolAbortPrepared

StartTransactionCommandFinishPreparedTransaction

RecordTransactionAbortPreparedXLogInsert (RM_XACT_ID, XLOG_XACT_ABORT_PREPARED | XLOG_NO_TRAN)

DTX_CONTEXT_QE_PREPARED -> DTX_CONTEXT_QE_FINISH_PREPARED

segmentMaxDistributedTransactionId

gatherRMInDoubtTransactions

cdbdisp_dispatchRMCommand(select gp_max_distributed_xid())

First char: 'M';Return gp_max_distributed_xid

First char: ‘T';

DTX_PROTOCOL_COMMAND_RECOVERY_ABORT_PREPARED

XLogFlush

shmNumGxacts = 0

ReplayRedoFromUtilityModeredoDistributedCommitRecord

Page 55: Learning Greenplum Distributed Transaction

Master(GP_ROLE_DISPATCH) Seg-N

Kill all after TMs write commit log

---------------------------- ---------------------------- ------------------- Kill All TM/RMs and restart ------------------- ---------------------------- --

PostgresMain

DTX_PROTOCOL_COMMAND_COMMIT_PREPARED

doDispatchDtxProtocolCommand

PostgresMain

exec_mpp_dtx_protocol_command

firstchar = ‘T'

XLogFlush

XLogInsert (RM_XACT_ID, XLOG_XACT_PREPARE)

Gp_role == GP_ROLE_DISPATCH

initTMinitTM_recover_as_needed

recoverTMrecoverInDoubtTransactions

shmNumGxacts = 1

gatherRMInDoubtTransactionscdbdisp_dispatchRMCommand(select gid from pg_prepared_xacts)

segmentMaxDistributedTransactionId

gatherRMInDoubtTransactions

cdbdisp_dispatchRMCommand(select gp_max_distributed_xid())

First char: 'M';Return gp_max_distributed_xid

ReplayRedoFromUtilityModeredoDistributedCommitRecord

dispatchDtxProtocolCommand

doNotifyCommittedInDoubt

performDtxProtocolCommitPrepared

StartTransactionCommandFinishPreparedTransaction

RecordTransactionAbortPreparedXLogInsert (RM_XACT_ID, XLOG_XACT_COMMIT_PREPARED | XLOG_NO_TRAN)

DTX_CONTEXT_QE_PREPARED -> DTX_CONTEXT_QE_FINISH_PREPARED

First char: ‘T';

XLogFlush

doInsertForgetCommitted

RecordDistributedForgetCommitted

XLogInsert (RM_XACT_ID, XLOG_XACT_DISTRIBUTED_FORGET | XLOG_NO_TRAN)

First char: 'M';Return 0 indoubt transactions

Page 56: Learning Greenplum Distributed Transaction

Master(GP_ROLE_DISPATCH) Seg-N

savepoint s1; PostgresMainfirstchar = ‘M'

exec_simple_query

start_xact_command

PortalRun

PortalRunMulti

CommitTransactionCommand

finish_xact_command

state: TRANS_DEFAULT-> TRANS_START-

StartSubTransaction

Block state: TBLOCK_SUBBEGIN -> TBLOCK_SUBINPROGRESS

state: TRANS_START-> TRANS_INPROGRESS

dispatchDtxCommand (SAVEPOINT s1)

CommitTransactionCommand

finish_xact_command

state: TRANS_DEFAULT-> TRANS_START-

StartSubTransaction

Block state: TBLOCK_SUBBEGIN -> TBLOCK_SUBINPROGRESS

state: TRANS_START-> TRANS_INPROGRESS

ProcessUtilityPortalRunUtility

DefineDispatchSavepoint

DefineSavepoint

PushTransaction

TRANS_STMT_SAVEPOINT

TRANS_DEFAULTTBLOCK_SUBBEGIN

ProcessUtility

DefineSavepoint

sendDtxExplicitBegin();

Page 57: Learning Greenplum Distributed Transaction

Master(GP_ROLE_DISPATCH) Seg-N

rollback to savepoint s2 PostgresMain

firstchar = ‘M'

exec_simple_query

start_xact_command

PortalRun

PortalRunMulti

CommitTransactionCommand

finish_xact_command

state: TRANS_DEFAULT-> TRANS_START-

StartSubTransaction

state: TRANS_START-> TRANS_INPROGRESS

dispatchDtxCommand (rollback to savepoint s2)

CommitTransactionCommand

finish_xact_command

AbortSubTransaction

state: TRANS_INPROGRESS -> TRANS_ABORT

ProcessUtility

PortalRunUtility

RollbackToSavepoint

TRANS_STMT_ROLLBACK_TO

ProcessUtility

RollbackToSavepointTRANS_STMT_ROLLBACK_TO

Sub state: TBLOCK_SUBINPROGRESS -> TBLOCK_SUBABORT_PENDING

State: TBLOCK_SUBINPROGRESS -> TBLOCK_SUBRESTART

CleanupSubTransaction

state: TRANS_ABORT -> TRANS_DEFAULT

DispatchRollbackToSavepoint

Sub state: TBLOCK_SUBINPROGRESS -> TBLOCK_SUBABORT_PENDING

State: TBLOCK_SUBINPROGRESS -> TBLOCK_SUBRESTART

AbortSubTransactionstate: TRANS_INPROGRESS -> TRANS_ABORT

CleanupSubTransactionstate: TRANS_ABORT -> TRANS_DEFAULT

RecordSubTransactionAbort

XLogInsert(RM_XACT_ID, XLOG_XACT_ABORT)

DefineSavepoint

PushTransactionTRANS_DEFAULTTBLOCK_SUBBEGIN

StartSubTransactionState: TBLOCK_SUBBEGIN -> TBLOCK_SUBINPROGRESS

DefineSavepoint

PushTransactionTRANS_DEFAULTTBLOCK_SUBBEGIN

StartSubTransactionState: TBLOCK_SUBBEGIN -> TBLOCK_SUBINPROGRESS

Page 58: Learning Greenplum Distributed Transaction

Master(GP_ROLE_DISPATCH) Seg-N

release savepoint s2 PostgresMain

firstchar = ‘M'

exec_simple_query

CommitTransactionCommand

finish_xact_command

state: TRANS_INPROGRESS -> TRANS_COMMIT

CommitSubTransaction

CommitTransactionCommand

finish_xact_command

CommitSubTransaction

state: TRANS_INPROGRESS -> TRANS_COMMIT

ProcessUtilityTRANS_STMT_RELEASE

ProcessUtility

ReleaseSavepointTRANS_STMT_RELEASE

dispatchDtxCommand

While Sub blockState: TBLOCK_SUBEND

If blockState: TBLOCK_END

CommitTransactionState: TBLOCK_END -> TBLOCK_DEFAULT

If blockState: TBLOCK_PREPARE

PopTransaction

Commit with spState: TRANS_BEGIN -> TRANS_PREPARE

PrepareTransaction

XLogFlush

EndPrepare

XLogInsert (RM_XACT_ID, XLOG_XACT_PREPARE)

State: TRANS_PREPARE -> TRANS_DEFAULT

prepareDtxTransaction

......

Page 59: Learning Greenplum Distributed Transaction

Debug tips

• regres01 55749 55656 0 08:50 ? 00:00:00 postgres: port 5432, regres01 postgres [local] con6 [local] cmd5 idle in transaction

• regres01 56475 55619 0 09:28 ? 00:00:00 postgres: port 40000, regres01 postgres localhost(6655) con6 seg0 idle in transaction

• regres01 56478 55620 0 09:28 ? 00:00:00 postgres: port 40001, regres01 postgres localhost(14862) con6 seg1 idle in transaction

• gp_vmem_idle_resource_timeout is for idle mem time in case the resource will be released

• gpconfig -c gp_vmem_idle_resource_timeout –v 300

Page 60: Learning Greenplum Distributed Transaction

Debug tips• b StartTransactionCommand

• b CommitTransactionCommand

• b AbortCurrentTransaction

• b BeginTransactionBlock

• b EndTransactionBlock

• b UserAbortTransactionBlock

• b DefineSavepoint

• b RollbackToSavepoint

• b ReleaseSavepoint

• b StartTransaction

• b CommitTransaction

• b AbortTransaction

• b CleanupTransaction

• b StartSubTransaction

• b CommitSubTransaction

• b AbortSubTransaction

• b CleanupSubTransaction

• b performDtxProtocolCommand

• b exec_mpp_dtx_protocol_command

• b recoverTM

• b recoverInDoubtTransactions

• b gatherRMInDoubtTransactions

• b abortRMInDoubtTransactions

• b cleanRMResultSets

• b resolveInDoubtDtx

• b doDispatchDtxProtocolCommand

• b doPrepareTransaction

• b doInsertForgetCommitted

• b doNotifyingCommitPrepared

• b doNotifyingAbort

• b doNotifyCommittedInDoubt

• b doAbortInDoubt

• b doQEDistributedExplicitBegin

• b UtilityModeSaveRedo

• b ReplayRedoFromUtilityMode

• b RemoveRedoUtilityModeFile

• b performDtxProtocolCommitPrepared

• b performDtxProtocolAbortPrepared

• b cdbdisp_debugDispatchResult

• b ProcessUtility

• b prepareDtxTransaction

• b PrepareTransaction

• b XLogFlush

• b ReadyForQuery_QEWriter

• b RecordDistributedForgetCommitted

• b RecordTransactionCommit

• b RecordCrashTransactionAbortRecord

• b XLogInsert

• b XLogFlush

• b forcedDistributedCommitted

• b rollbackDtxTransaction

• b dtmPreCommand

• b PrepareTransaction

• b disconnectAndDestroyAllGangs

Page 61: Learning Greenplum Distributed Transaction

61