Skip to content

Commit

Permalink
client,kv: new savepoint API
Browse files Browse the repository at this point in the history
This patch introduces the KV savepoint API as discussed in the
savepoints RFC:

```go
	// CreateSavepoint establishes a savepoint.
	// This method is only valid when called on RootTxns.
	CreateSavepoint(context.Context) (SavepointToken, error)

	// RollbackToSavepoint rolls back to the given savepoint. The
	// savepoint must not have been rolled back or released already.
	// All savepoints "under" the savepoint being rolled back
	// are also rolled back and their token must not be used any more.
	// This method is only valid when called on RootTxns.
	RollbackToSavepoint(context.Context, SavepointToken) error

	// ReleaseSavepoint releases the given savepoint. The savepoint
	// must not have been rolled back or released already.
	// All savepoints "under" the savepoint being released
	// are also released and their token must not be used any more.
	// This method is only valid when called on RootTxns.
	ReleaseSavepoint(context.Context, SavepointToken) error
```

Release note: None
  • Loading branch information
knz committed Jan 21, 2020
1 parent f231ee8 commit 391354c
Show file tree
Hide file tree
Showing 8 changed files with 834 additions and 0 deletions.
15 changes: 15 additions & 0 deletions pkg/internal/client/mock_transactional_sender.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,21 @@ func (m *MockTransactionalSender) IsSerializablePushAndRefreshNotPossible() bool
return false
}

// CreateSavepoint is part of the client.TxnSender interface.
func (m *MockTransactionalSender) CreateSavepoint(context.Context) (SavepointToken, error) {
panic("unimplemented")
}

// RollbackToSavepoint is part of the client.TxnSender interface.
func (m *MockTransactionalSender) RollbackToSavepoint(context.Context, SavepointToken) error {
panic("unimplemented")
}

// ReleaseSavepoint is part of the client.TxnSender interface.
func (m *MockTransactionalSender) ReleaseSavepoint(context.Context, SavepointToken) error {
panic("unimplemented")
}

// Epoch is part of the TxnSender interface.
func (m *MockTransactionalSender) Epoch() enginepb.TxnEpoch { panic("unimplemented") }

Expand Down
24 changes: 24 additions & 0 deletions pkg/internal/client/sender.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,24 @@ type TxnSender interface {
// TxnStatus exports the txn's status.
TxnStatus() roachpb.TransactionStatus

// CreateSavepoint establishes a savepoint.
// This method is only valid when called on RootTxns.
CreateSavepoint(context.Context) (SavepointToken, error)

// RollbackToSavepoint rolls back to the given savepoint. The
// savepoint must not have been rolled back or released already.
// All savepoints "under" the savepoint being rolled back
// are also rolled back and their token must not be used any more.
// This method is only valid when called on RootTxns.
RollbackToSavepoint(context.Context, SavepointToken) error

// ReleaseSavepoint releases the given savepoint. The savepoint
// must not have been rolled back or released already.
// All savepoints "under" the savepoint being released
// are also released and their token must not be used any more.
// This method is only valid when called on RootTxns.
ReleaseSavepoint(context.Context, SavepointToken) error

// SetFixedTimestamp makes the transaction run in an unusual way, at
// a "fixed timestamp": Timestamp and ReadTimestamp are set to ts,
// there's no clock uncertainty, and the txn's deadline is set to ts
Expand Down Expand Up @@ -274,6 +292,12 @@ const (
SteppingEnabled SteppingMode = true
)

// SavepointToken represents a savepoint.
type SavepointToken interface {
// SavepointToken is a marker interface.
SavepointToken()
}

// TxnStatusOpt represents options for TxnSender.GetMeta().
type TxnStatusOpt int

Expand Down
30 changes: 30 additions & 0 deletions pkg/internal/client/txn.go
Original file line number Diff line number Diff line change
Expand Up @@ -1217,3 +1217,33 @@ func (txn *Txn) ConfigureStepping(ctx context.Context, mode SteppingMode) (prevM
defer txn.mu.Unlock()
return txn.mu.sender.ConfigureStepping(ctx, mode)
}

// CreateSavepoint establishes a savepoint.
// This method is only valid when called on RootTxns.
func (txn *Txn) CreateSavepoint(ctx context.Context) (SavepointToken, error) {
txn.mu.Lock()
defer txn.mu.Unlock()
return txn.mu.sender.CreateSavepoint(ctx)
}

// RollbackToSavepoint rolls back to the given savepoint. The
// savepoint must not have been rolled back or released already.
// All savepoints "under" the savepoint being rolled back
// are also rolled back and their token must not be used any more.
// This method is only valid when called on RootTxns.
func (txn *Txn) RollbackToSavepoint(ctx context.Context, s SavepointToken) error {
txn.mu.Lock()
defer txn.mu.Unlock()
return txn.mu.sender.RollbackToSavepoint(ctx, s)
}

// ReleaseSavepoint releases the given savepoint. The savepoint
// must not have been rolled back or released already.
// All savepoints "under" the savepoint being released
// are also released and their token must not be used any more.
// This method is only valid when called on RootTxns.
func (txn *Txn) ReleaseSavepoint(ctx context.Context, s SavepointToken) error {
txn.mu.Lock()
defer txn.mu.Unlock()
return txn.mu.sender.ReleaseSavepoint(ctx, s)
}
Loading

0 comments on commit 391354c

Please sign in to comment.