github.com/pachyderm/pachyderm@v1.13.4/src/server/pkg/transactionenv/txncontext/context.go (about)

     1  package txncontext
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/pachyderm/pachyderm/src/client"
     7  	"github.com/pachyderm/pachyderm/src/client/pfs"
     8  	col "github.com/pachyderm/pachyderm/src/server/pkg/collection"
     9  )
    10  
    11  // PfsPropagater is the interface that PFS implements to propagate commits at
    12  // the end of a transaction.  It is defined here to avoid a circular dependency.
    13  type PfsPropagater interface {
    14  	PropagateCommit(branch *pfs.Branch, isNewCommit bool) error
    15  	Run() error
    16  }
    17  
    18  // PipelineCommitFinisher is an interface to facilitate finishing pipeline commits
    19  // at the end of a transaction
    20  type PipelineCommitFinisher interface {
    21  	FinishPipelineCommits(branch *pfs.Branch) error
    22  	Run() error
    23  }
    24  
    25  // TransactionContext is a helper type to encapsulate the state for a given
    26  // set of operations being performed in the Pachyderm API.  When a new
    27  // transaction is started, a context will be created for it containing these
    28  // objects, which will be threaded through to every API call:
    29  //   ctx: the client context which initiated the operations being performed
    30  //   pachClient: the APIClient associated with the client context ctx
    31  //   stm: the object that controls transactionality with etcd.  This is to ensure
    32  //     that all reads and writes are consistent until changes are committed.
    33  //   txnEnv: a struct containing references to each API server, it can be used
    34  //     to make calls to other API servers (e.g. checking auth permissions)
    35  //   pfsDefer: an interface for ensuring certain PFS cleanup tasks are performed
    36  //     properly (and deduped) at the end of the transaction.
    37  type TransactionContext struct {
    38  	ClientContext  context.Context
    39  	Client         *client.APIClient
    40  	Stm            col.STM
    41  	PfsPropagater  PfsPropagater
    42  	CommitFinisher PipelineCommitFinisher
    43  }
    44  
    45  // PropagateCommit saves a branch to be propagated at the end of the transaction
    46  // (if all operations complete successfully).  This is used to batch together
    47  // propagations and dedupe downstream commits in PFS.
    48  func (t *TransactionContext) PropagateCommit(branch *pfs.Branch, isNewCommit bool) error {
    49  	return t.PfsPropagater.PropagateCommit(branch, isNewCommit)
    50  }
    51  
    52  // Finish runs the commit finisher and pfs propagator
    53  func (t *TransactionContext) Finish() error {
    54  	if t.CommitFinisher != nil {
    55  		if err := t.CommitFinisher.Run(); err != nil {
    56  			return err
    57  		}
    58  	}
    59  	return t.PfsPropagater.Run()
    60  }
    61  
    62  // FinishPipelineCommits saves a pipeline output branch to have its commits
    63  // finished at the end of the transaction
    64  func (t *TransactionContext) FinishPipelineCommits(branch *pfs.Branch) error {
    65  	if t.CommitFinisher != nil {
    66  		return t.CommitFinisher.FinishPipelineCommits(branch)
    67  	}
    68  	return nil
    69  }