-
Notifications
You must be signed in to change notification settings - Fork 20
/
localconnection.go
367 lines (330 loc) · 10.2 KB
/
localconnection.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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
package client
import (
"encoding/binary"
"fmt"
cc "github.com/msackman/chancell"
"goshawkdb.io/common"
cmsgs "goshawkdb.io/common/capnp"
"goshawkdb.io/server"
msgs "goshawkdb.io/server/capnp"
"goshawkdb.io/server/configuration"
"goshawkdb.io/server/paxos"
eng "goshawkdb.io/server/txnengine"
"log"
"sync"
)
type LocalConnection struct {
sync.Mutex
cellTail *cc.ChanCellTail
enqueueQueryInner func(localConnectionMsg, *cc.ChanCell, cc.CurCellConsumer) (bool, cc.CurCellConsumer)
queryChan <-chan localConnectionMsg
rmId common.RMId
connectionManager paxos.ConnectionManager
namespace []byte
submitter *SimpleTxnSubmitter
nextTxnNumber uint64
nextVarNumber uint64
txnQuery localConnectionTxnQuery
}
type localConnectionMsg interface {
witness() localConnectionMsg
}
type localConnectionMsgBasic struct{}
func (lcmb localConnectionMsgBasic) witness() localConnectionMsg { return lcmb }
type localConnectionMsgShutdown struct{ localConnectionMsgBasic }
type localConnectionMsgStatus struct {
localConnectionMsgBasic
*server.StatusConsumer
}
type localConnectionMsgOutcomeReceived struct {
localConnectionMsgBasic
sender common.RMId
txn *eng.TxnReader
outcome *msgs.Outcome
}
type localConnectionMsgTopologyChanged struct {
localConnectionMsgBasic
localConnectionMsgSyncQuery
topology *configuration.Topology
}
type localConnectionTxnQuery interface {
errored(error)
}
type localConnectionMsgSyncQuery struct {
resultChan chan struct{}
err error
}
func (lcmsq *localConnectionMsgSyncQuery) init() {
lcmsq.resultChan = make(chan struct{})
}
func (lcmsq *localConnectionMsgSyncQuery) errored(err error) {
lcmsq.err = err
lcmsq.maybeClose()
}
func (lcmsq *localConnectionMsgSyncQuery) maybeClose() {
select {
case <-lcmsq.resultChan:
default:
close(lcmsq.resultChan)
}
}
type localConnectionMsgRunClientTxn struct {
localConnectionMsgBasic
localConnectionMsgSyncQuery
txn *cmsgs.ClientTxn
varPosMap map[common.VarUUId]*common.Positions
translationCallback eng.TranslationCallback
txnReader *eng.TxnReader
outcome *msgs.Outcome
}
func (lcmrct *localConnectionMsgRunClientTxn) consumer(txn *eng.TxnReader, outcome *msgs.Outcome, err error) error {
lcmrct.txnReader = txn
lcmrct.outcome = outcome
lcmrct.err = err
lcmrct.maybeClose()
return nil
}
type localConnectionMsgRunTxn struct {
localConnectionMsgBasic
localConnectionMsgSyncQuery
txn *msgs.Txn
txnId *common.TxnId
activeRMs []common.RMId
backoff *server.BinaryBackoffEngine
txnReader *eng.TxnReader
outcome *msgs.Outcome
}
func (lcmrt *localConnectionMsgRunTxn) consumer(txn *eng.TxnReader, outcome *msgs.Outcome, err error) error {
lcmrt.txnReader = txn
lcmrt.outcome = outcome
lcmrt.err = err
lcmrt.maybeClose()
return nil
}
func (lc *LocalConnection) NextVarUUId() *common.VarUUId {
lc.Lock()
defer lc.Unlock()
vUUId := common.MakeVarUUId(lc.namespace)
binary.BigEndian.PutUint64(vUUId[0:8], lc.nextVarNumber)
lc.nextVarNumber++
return vUUId
}
func (lc *LocalConnection) enqueueQuery(msg localConnectionMsg) bool {
var f cc.CurCellConsumer
f = func(cell *cc.ChanCell) (bool, cc.CurCellConsumer) {
return lc.enqueueQueryInner(msg, cell, f)
}
return lc.cellTail.WithCell(f)
}
func (lc *LocalConnection) enqueueQuerySync(msg localConnectionMsg, resultChan chan struct{}) bool {
if lc.enqueueQuery(msg) {
select {
case <-resultChan:
return true
case <-lc.cellTail.Terminated:
return false
}
} else {
return false
}
}
func (lc *LocalConnection) Shutdown(sync paxos.Blocking) {
if lc.enqueueQuery(localConnectionMsgShutdown{}) && sync == paxos.Sync {
lc.cellTail.Wait()
}
}
func (lc *LocalConnection) Status(sc *server.StatusConsumer) {
lc.enqueueQuery(localConnectionMsgStatus{StatusConsumer: sc})
}
func (lc *LocalConnection) SubmissionOutcomeReceived(sender common.RMId, txn *eng.TxnReader, outcome *msgs.Outcome) {
server.Log("LC Received submission outcome for", txn.Id)
lc.enqueueQuery(localConnectionMsgOutcomeReceived{
sender: sender,
txn: txn,
outcome: outcome,
})
}
func (lc *LocalConnection) TopologyChanged(topology *configuration.Topology, done func(bool)) {
msg := &localConnectionMsgTopologyChanged{topology: topology}
msg.init()
if lc.enqueueQuery(msg) {
go func() {
select {
case <-msg.resultChan:
done(true)
case <-lc.cellTail.Terminated:
select {
case <-msg.resultChan:
done(true)
default:
done(false)
}
}
}()
} else {
done(false)
}
}
func (lc *LocalConnection) RunClientTransaction(txn *cmsgs.ClientTxn, varPosMap map[common.VarUUId]*common.Positions, translationCallback eng.TranslationCallback) (*eng.TxnReader, *msgs.Outcome, error) {
query := &localConnectionMsgRunClientTxn{
txn: txn,
varPosMap: varPosMap,
translationCallback: translationCallback,
}
query.init()
if lc.enqueueQuerySync(query, query.resultChan) {
return query.txnReader, query.outcome, query.err
} else {
return nil, nil, nil
}
}
// txn must be root in its segment
func (lc *LocalConnection) RunTransaction(txn *msgs.Txn, txnId *common.TxnId, backoff *server.BinaryBackoffEngine, activeRMs ...common.RMId) (*eng.TxnReader, *msgs.Outcome, error) {
query := &localConnectionMsgRunTxn{
txn: txn,
txnId: txnId,
backoff: backoff,
activeRMs: activeRMs,
}
query.init()
if lc.enqueueQuerySync(query, query.resultChan) {
return query.txnReader, query.outcome, query.err
} else {
return nil, nil, nil
}
}
type localConnectionMsgServerConnectionsChanged struct {
servers map[common.RMId]paxos.Connection
done func()
}
func (lcmscc localConnectionMsgServerConnectionsChanged) witness() localConnectionMsg { return lcmscc }
func (lc *LocalConnection) ConnectedRMs(servers map[common.RMId]paxos.Connection) {
lc.enqueueQuery(localConnectionMsgServerConnectionsChanged{servers: servers})
}
func (lc *LocalConnection) ConnectionLost(rmId common.RMId, servers map[common.RMId]paxos.Connection) {
lc.enqueueQuery(localConnectionMsgServerConnectionsChanged{servers: servers})
}
func (lc *LocalConnection) ConnectionEstablished(rmId common.RMId, conn paxos.Connection, servers map[common.RMId]paxos.Connection, done func()) {
lc.enqueueQuery(localConnectionMsgServerConnectionsChanged{
servers: servers,
done: done,
})
}
func NewLocalConnection(rmId common.RMId, bootCount uint32, cm paxos.ConnectionManager) *LocalConnection {
namespace := make([]byte, common.KeyLen)
binary.BigEndian.PutUint32(namespace[12:16], bootCount)
binary.BigEndian.PutUint32(namespace[16:20], uint32(rmId))
lc := &LocalConnection{
rmId: rmId,
connectionManager: cm,
namespace: namespace,
submitter: NewSimpleTxnSubmitter(rmId, bootCount, cm),
nextTxnNumber: 0,
nextVarNumber: 0,
}
var head *cc.ChanCellHead
head, lc.cellTail = cc.NewChanCellTail(
func(n int, cell *cc.ChanCell) {
queryChan := make(chan localConnectionMsg, n)
cell.Open = func() { lc.queryChan = queryChan }
cell.Close = func() { close(queryChan) }
lc.enqueueQueryInner = func(msg localConnectionMsg, curCell *cc.ChanCell, cont cc.CurCellConsumer) (bool, cc.CurCellConsumer) {
if curCell == cell {
select {
case queryChan <- msg:
return true, nil
default:
return false, nil
}
} else {
return false, cont
}
}
})
go lc.actorLoop(head)
return lc
}
func (lc *LocalConnection) actorLoop(head *cc.ChanCellHead) {
topology := lc.connectionManager.AddTopologySubscriber(eng.ConnectionSubscriber, lc)
defer lc.connectionManager.RemoveTopologySubscriberAsync(eng.ConnectionSubscriber, lc)
servers := lc.connectionManager.ClientEstablished(0, lc)
if servers == nil {
panic("LocalConnection failed to register with ConnectionManager!")
}
defer lc.connectionManager.ClientLost(0, lc)
lc.submitter.TopologyChanged(topology)
lc.submitter.ServerConnectionsChanged(servers)
var (
err error
queryChan <-chan localConnectionMsg
queryCell *cc.ChanCell
)
chanFun := func(cell *cc.ChanCell) { queryChan, queryCell = lc.queryChan, cell }
head.WithCell(chanFun)
terminate := false
for !terminate {
if msg, ok := <-queryChan; ok {
switch msgT := msg.(type) {
case localConnectionMsgShutdown:
terminate = true
case *localConnectionMsgTopologyChanged:
err = lc.submitter.TopologyChanged(msgT.topology)
msgT.maybeClose()
case *localConnectionMsgRunTxn:
lc.runTransaction(msgT)
case *localConnectionMsgRunClientTxn:
err = lc.runClientTransaction(msgT)
case localConnectionMsgOutcomeReceived:
err = lc.submitter.SubmissionOutcomeReceived(msgT.sender, msgT.txn, msgT.outcome)
case localConnectionMsgServerConnectionsChanged:
err = lc.submitter.ServerConnectionsChanged(msgT.servers)
if msgT.done != nil {
msgT.done()
}
case localConnectionMsgStatus:
lc.status(msgT.StatusConsumer)
default:
err = fmt.Errorf("Fatal to LocalConnection: Received unexpected message: %#v", msgT)
}
terminate = terminate || err != nil
} else {
head.Next(queryCell, chanFun)
}
}
if err != nil {
log.Println("LocalConnection error:", err)
}
lc.submitter.Shutdown()
lc.cellTail.Terminate()
}
func (lc *LocalConnection) runClientTransaction(txnQuery *localConnectionMsgRunClientTxn) error {
txn := txnQuery.txn
txnId := lc.getNextTxnId()
txn.SetId(txnId[:])
server.Log("LC starting client txn", txnId)
if varPosMap := txnQuery.varPosMap; varPosMap != nil {
lc.submitter.EnsurePositions(varPosMap)
}
return lc.submitter.SubmitClientTransaction(txnQuery.translationCallback, txn, txnId, txnQuery.consumer, nil, true, nil)
}
func (lc *LocalConnection) runTransaction(txnQuery *localConnectionMsgRunTxn) {
txnId := txnQuery.txnId
txn := txnQuery.txn
if txnId == nil {
txnId = lc.getNextTxnId()
txn.SetId(txnId[:])
server.Log("LC starting txn", txnId)
}
lc.submitter.SubmitTransaction(txn, txnId, txnQuery.activeRMs, txnQuery.consumer, txnQuery.backoff)
}
func (lc *LocalConnection) getNextTxnId() *common.TxnId {
txnId := common.MakeTxnId(lc.namespace)
binary.BigEndian.PutUint64(txnId[0:8], lc.nextTxnNumber)
lc.nextTxnNumber++
return txnId
}
func (lc *LocalConnection) status(sc *server.StatusConsumer) {
sc.Emit("LocalConnection")
lc.submitter.Status(sc.Fork())
sc.Join()
}