-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtxn.go
61 lines (52 loc) · 1.16 KB
/
txn.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// package sdk defined spanner sdk for tikv transaction
package sdk
import (
"context"
"sync"
"go.uber.org/zap"
)
var pdCluster []string
// init gen logger and pd kv client
func init() {
initLogger()
initPDclient(pdCluster)
}
// Transaction defiend spanner transaction interface, implemented by ro transaction and rw transaction
type Transaction interface {
Get(key []byte) ([]byte, error)
Set(key []byte, value []byte) error
Delete(key []byte) error
Rollback() error
Commit() error
Close() error
}
// Begin start a transaction with option wether it is a RO transaction
func Begin(ro bool, ctx context.Context) (Transaction, error) {
if ro {
return beginROTxn()
}
return beginRWTxn()
}
// base transaction op
type transaction struct {
ctx context.Context
store KeyStore
pm Pacemaker
isValid bool
sync.Mutex
txnid uint64
}
func (txn *transaction) setTxnIDNX() error {
var err error
if txn.txnid == 0 {
txn.Lock()
defer txn.Unlock()
txn.txnid, err = oracleClient.GetTimestamp(txn.ctx)
if err != nil {
lg.Error("error in call oracle get ts", zap.Error(err))
return err
}
}
txn.pm = NewPacemaker(txn.ctx, txn.txnid)
return nil
}