This repository has been archived by the owner on Aug 25, 2019. It is now read-only.
/
mry.go
229 lines (190 loc) · 5.04 KB
/
mry.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
package mry
import (
pb "code.google.com/p/goprotobuf/proto"
"github.com/appaquet/nrv"
"time"
)
//
// Database object bound to a specific domain of a given cluster
//
type Db struct {
*Model
ServiceName string
Cluster nrv.Cluster
Storage Storage
Service *nrv.Service
}
func (db *Db) SetupCluster() {
db.Model = newModel()
db.Service = db.Cluster.GetService(db.ServiceName)
db.Service.Bind(&nrv.Binding{
Path: "^/execute$",
Resolver: &nrv.ResolverParam{Count: 1},
Controller: db,
Method: "NrvExecute",
})
db.Service.Bind(&nrv.Binding{
Path: "^/execute/write/(.*)$",
Resolver: &nrv.ResolverParam{Count: 1},
Controller: db,
Method: "NrvExecuteWrite",
})
db.Service.Bind(&nrv.Binding{
Path: "^/execute/read/(.*)$",
Resolver: &nrv.ResolverParam{Count: 1},
Controller: db,
Method: "NrvExecuteRead",
})
db.Cluster.GetDefaultProtocol().AddMarshaller(&trxMarshaller{})
db.Cluster.GetDefaultProtocol().AddMarshaller(&mutMarshaller{})
db.Storage.Init()
}
// TODO: rollback on panic
func (db *Db) NrvExecute(request *nrv.ReceivedRequest) {
logger := nrv.Logger(request.Logger)
trace := logger.Trace("mry")
iTrx := request.Message.Data["t"]
if trx, ok := iTrx.(*Transaction); ok {
logger.Debug("Executing transaction %d %s", *trx.Id)
// dry execution to discover token and some errors
traceDry := logger.Trace("execute_dry")
context := &transactionContext{
dry: true,
db: db,
trx: trx,
logger: logger,
storageTrx: nil,
}
context.init()
db.executeLocal(context)
if context.token == nil && context.ret.Error == nil {
context.setError("Couldn't find token for transaction")
}
traceDry.End()
// continue if no error
if context.ret.Error == nil {
logger.Debug("Transaction has token %d", *context.token)
traceReal := logger.Trace("execute_real")
context = &transactionContext{
dry: false,
db: db,
trx: trx,
logger: logger,
token: context.token,
storageTrx: nil,
}
context.init()
db.executeLocal(context)
if context.ret.Error == nil {
err := context.storageTrx.Commit()
if err != nil {
context.setError("Couldn't commit transaction: %s", err)
}
} else {
logger.Debug("Error executing transaction: %s", context.ret.Error)
}
traceReal.End()
} else {
if context.storageTrx != nil {
context.storageTrx.Rollback()
}
logger.Debug("Error executing transaction in dry mode: %s", context.ret.Error)
}
trace.End()
request.Reply(nrv.Map{
"t": &Transaction{
Id: trx.Id,
Return: context.ret,
},
})
} else {
logger.Error("Received a null transaction")
}
}
func (db *Db) SyncModel() error {
return db.Storage.SyncModel(db.Model)
}
func (db *Db) NewTransaction(cb func(b Block)) *Transaction {
trx := &Transaction{
Id: pb.Uint64(uint64(time.Now().UnixNano())), // TODO: put real id from nrv
}
cb(trx.newBlock())
return trx
}
func (db *Db) Execute(cb func(b Block)) *TransactionReturn {
return db.ExecuteTrx(db.NewTransaction(cb))
}
func (db *Db) ExecuteLog(cb func(b Block), logger nrv.Logger) *TransactionReturn {
return db.ExecuteTrxLog(db.NewTransaction(cb), logger)
}
func (db *Db) ExecuteTrx(t Transactable) *TransactionReturn {
return db.ExecuteTrxLog(t, &nrv.RequestLogger{})
}
func (db *Db) ExecuteTrxLog(t Transactable, logger nrv.Logger) *TransactionReturn {
trx := t.GetTransaction()
resp := <-db.Service.CallChan("/execute", &nrv.Request{
Message: &nrv.Message{
Logger: logger,
Data: nrv.Map{
"t": trx,
},
},
})
return resp.Message.Data["t"].(*Transaction).Return
}
func (db *Db) executeLocal(context *transactionContext) {
if !context.dry {
trxTime := time.Unix(0, int64(*context.trx.Id))
trc := context.logger.Trace("gettrx")
storageTrx, err := context.db.Storage.GetTransaction(*context.token, trxTime)
if err != nil {
context.setError("Couldn't get storage transaction: %s", err)
return
}
context.storageTrx = storageTrx
trc.End()
}
context.trx.execute(context)
}
//
// Transaction marshaller used by nrv to stream stransactions over
// network
//
type trxMarshaller struct {
}
func (pbm *trxMarshaller) MarshallerName() string {
return "mrytrx"
}
func (pbm *trxMarshaller) CanMarshal(obj interface{}) bool {
if _, ok := obj.(*Transaction); ok {
return true
}
return false
}
func (pbm *trxMarshaller) Marshal(obj interface{}) ([]byte, error) {
return pb.Marshal(obj)
}
func (pbm *trxMarshaller) Unmarshal(bytes []byte) (interface{}, error) {
trx := &Transaction{}
err := pb.Unmarshal(bytes, trx)
return trx, err
}
type mutMarshaller struct {
}
func (pbm *mutMarshaller) MarshallerName() string {
return "mrymut"
}
func (pbm *mutMarshaller) CanMarshal(obj interface{}) bool {
if _, ok := obj.(*JobRowMutation); ok {
return true
}
return false
}
func (pbm *mutMarshaller) Marshal(obj interface{}) ([]byte, error) {
return pb.Marshal(obj)
}
func (pbm *mutMarshaller) Unmarshal(bytes []byte) (interface{}, error) {
trx := &JobRowMutation{}
err := pb.Unmarshal(bytes, trx)
return trx, err
}