/
txn_kvstore_service.go
62 lines (53 loc) · 1.91 KB
/
txn_kvstore_service.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
62
package shopping
import (
"distributed-system/kv"
"distributed-system/tinykv"
"distributed-system/twopc"
"encoding/gob"
"sync"
)
// ShoppingTxnKVStore supports the shopping transations. It wraps
// the KV-store with the shopping txn logics. The logics will be registered
// into the 2pc service, so the txns could be managed by the Coordinator
// and the Parcipants.
type ShoppingTxnKVStore struct {
kv.KVStoreService
transRwLock sync.RWMutex
}
// NewShoppingTxnKVStore inits a new ShoppingTxnKVStore.
func NewShoppingTxnKVStore() *ShoppingTxnKVStore {
tks := &ShoppingTxnKVStore{KVStoreService: tinykv.NewKVStore()}
return tks
}
// ShoppingTxnKVStoreService is the service wrapping the ShoppingTxnKVStore
// and one partipant of 2pc.
type ShoppingTxnKVStoreService struct {
*ShoppingTxnKVStore
ppt *twopc.Participant
network string
}
// NewShoppingTxnKVStoreService starts the transaction-enabled kvstore.
func NewShoppingTxnKVStoreService(network, addr, coordAddr string) *ShoppingTxnKVStoreService {
ppt := twopc.NewParticipant(network, addr, coordAddr)
if ppt == nil {
return nil
}
sks := NewShoppingTxnKVStore()
gob.Register(AddItemTxnInitRet{})
ppt.RegisterCaller(twopc.CallFunc(sks.CartExist), "CartExist")
ppt.RegisterCaller(twopc.CallFunc(sks.CartAddItem), "CartAddItem")
gob.Register(SubmitOrderTxnInitRet{})
ppt.RegisterCaller(twopc.CallFunc(sks.ItemsStockMinus), "ItemsStockMinus")
ppt.RegisterCaller(twopc.CallFunc(sks.OrderRecord), "OrderRecord")
gob.Register(PayOrderTxnInitRet{})
ppt.RegisterCaller(twopc.CallFunc(sks.PayMinus), "PayMinus")
ppt.RegisterCaller(twopc.CallFunc(sks.PayAdd), "PayAdd")
ppt.RegisterCaller(twopc.CallFunc(sks.PayRecord), "PayRecord")
service := &ShoppingTxnKVStoreService{ShoppingTxnKVStore: sks, network: network, ppt: ppt}
ppt.RegisterRPCService(service)
return service
}
// Serve starts the KV-store service.
func (service *ShoppingTxnKVStoreService) Serve() {
// ?
}