Skip to content

Commit

Permalink
refactor(datastore): Rename function (#10063)
Browse files Browse the repository at this point in the history
  • Loading branch information
bhshkh authored May 7, 2024
1 parent 3ffbbbe commit 949242c
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 8 deletions.
6 changes: 3 additions & 3 deletions datastore/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -800,7 +800,7 @@ func (c *Client) RunAggregationQuery(ctx context.Context, aq *AggregationQuery)
// Parse the read options.
txn := aq.query.trans
if txn != nil {
defer txn.acquireLock()()
defer txn.stateLockDeferUnlock()()
}

req.ReadOptions, err = parseQueryReadOptions(aq.query.eventual, txn)
Expand Down Expand Up @@ -834,7 +834,7 @@ func validateReadOptions(eventual bool, t *Transaction) error {
return nil
}
if eventual {
return errors.New("datastore: cannot use EventualConsistency query in a transaction")
return errEventualConsistencyTransaction
}
if t.state == transactionStateExpired {
return errExpiredTransaction
Expand Down Expand Up @@ -963,7 +963,7 @@ func (t *Iterator) nextBatch() error {

txn := t.trans
if txn != nil {
defer txn.acquireLock()()
defer txn.stateLockDeferUnlock()()
}

var err error
Expand Down
2 changes: 1 addition & 1 deletion datastore/query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -886,7 +886,7 @@ func TestAggregationQueryIsNil(t *testing.T) {
}

func TestValidateReadOptions(t *testing.T) {
eventualInTxnErr := errors.New("datastore: cannot use EventualConsistency query in a transaction")
eventualInTxnErr := errEventualConsistencyTransaction

for _, test := range []struct {
desc string
Expand Down
25 changes: 21 additions & 4 deletions datastore/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ import (
// to a conflict with a concurrent transaction.
var ErrConcurrentTransaction = errors.New("datastore: concurrent transaction")

var errExpiredTransaction = errors.New("datastore: transaction expired")
var (
errExpiredTransaction = errors.New("datastore: transaction expired")
errEventualConsistencyTransaction = errors.New("datastore: cannot use EventualConsistency query in a transaction")
)

type transactionSettings struct {
attempts int
Expand Down Expand Up @@ -228,8 +231,22 @@ func (t *Transaction) beginLaterTransaction() (err error) {
return nil
}

// Acquire state lock if transaction has not started
func (t *Transaction) acquireLock() func() {
// Acquires state lock if transaction has not started. No-op otherwise
// The returned function unlocks the state if it was locked.
//
// Usage:
//
// func (t *Transaction) someFunction() {
// ...
// if t != nil {
// defer t.stateLockDeferUnlock()()
// }
// ....
// }
//
// This ensures that state is locked before any of the following lines are exexcuted
// The lock will be released after 'someFunction' ends
func (t *Transaction) stateLockDeferUnlock() func() {
if t.state == transactionStateNotStarted {
t.stateLock.Lock()
// Check whether state changed while waiting to acquire lock
Expand Down Expand Up @@ -416,7 +433,7 @@ func (t *Transaction) get(spanName string, keys []*Key, dst interface{}) (err er
defer func() { trace.EndSpan(t.ctx, err) }()

if t != nil {
defer t.acquireLock()()
defer t.stateLockDeferUnlock()()
}

opts, err := t.parseReadOptions()
Expand Down

0 comments on commit 949242c

Please sign in to comment.