This repository was archived by the owner on Jun 7, 2020. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathevent.go
More file actions
335 lines (286 loc) · 11.2 KB
/
Copy pathevent.go
File metadata and controls
335 lines (286 loc) · 11.2 KB
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
package surge
import (
"fmt"
"time"
"unsafe"
)
//
// interfaces
//
type EventInterface interface {
GetSource() NodeRunnerInterface
GetCreationTime() time.Time
GetTriggerTime() time.Time
GetTarget() NodeRunnerInterface
GetTio() TioInterface
GetGroup() GroupInterface
GetSize() int // size in bytes
IsMcast() bool
GetTioStage() string
String() string
setOneArg(arg interface{})
}
//
// event processing and cloning callback types
//
type processEventCb func(ev EventInterface) int
type cloneEventCb func(src EventInterface) EventInterface
type EventClonerInterface interface {
clone(ev EventInterface) EventInterface
}
//
// generic event that must trigger at a certain time
//
type TimedAnyEvent struct {
crtime time.Time
source NodeRunnerInterface
thtime time.Time
//
target NodeRunnerInterface
tio TioInterface
targetgroup GroupInterface
sizeb int64 // bytes
mcast bool
tiostage string
}
func newTimedAnyEvent(src NodeRunnerInterface, when time.Duration, args ...interface{}) *TimedAnyEvent {
assert(when > 0)
triggertime := Now.Add(when)
ev := &TimedAnyEvent{
crtime: Now,
source: src,
thtime: triggertime,
target: nil,
tio: nil,
targetgroup: nil,
sizeb: 0,
mcast: false}
ev.setArgs(args)
if ev.sizeb == 0 {
ev.sizeb = int64(unsafe.Sizeof(*ev))
}
return ev
}
func (e *TimedAnyEvent) setArgs(args []interface{}) {
for i := 0; i < len(args); i++ {
e.setOneArg(args[i])
}
}
func (e *TimedAnyEvent) setOneArg(a interface{}) {
switch a.(type) {
case NodeRunnerInterface:
e.target = a.(NodeRunnerInterface)
case TioInterface:
e.tio = a.(TioInterface)
case GroupInterface:
e.targetgroup = a.(GroupInterface)
case int:
e.sizeb = int64(a.(int))
case bool:
e.mcast = a.(bool)
case string:
e.tiostage = a.(string)
default:
assert(false, fmt.Sprintf("unexpected type: %#v", a))
}
}
//
// interfaces
//
func (e *TimedAnyEvent) GetSource() NodeRunnerInterface { return e.source }
func (e *TimedAnyEvent) GetCreationTime() time.Time { return e.crtime }
func (e *TimedAnyEvent) GetTriggerTime() time.Time { return e.thtime }
func (e *TimedAnyEvent) GetTarget() NodeRunnerInterface { return e.target }
func (e *TimedAnyEvent) GetTio() TioInterface { return e.tio.GetTio() }
func (e *TimedAnyEvent) GetGroup() GroupInterface { return e.targetgroup }
func (e *TimedAnyEvent) GetSize() int { return int(e.sizeb) }
func (e *TimedAnyEvent) IsMcast() bool { return e.mcast }
func (e *TimedAnyEvent) GetTioStage() string { return e.tiostage }
func (e *TimedAnyEvent) String() string {
dcreated := e.crtime.Sub(time.Time{})
dtriggered := e.thtime.Sub(time.Time{})
pref := "Event"
if e.mcast {
pref = "Mcast"
}
return fmt.Sprintf("[%s src=%v,%11.10v,%11.10v,tgt=%v]", pref, e.source.String(), dcreated, dtriggered, e.target.String())
}
//=====================================================================
//
// put-chunk unicast & multicast models: control & data
//
//=====================================================================
type zEvent struct {
TimedAnyEvent
}
type zControlEvent struct {
zEvent
cid int64
}
type ReplicaPutRequestEvent struct {
zControlEvent
sizeb int64 // size in bytes
num int // replica num
}
func newReplicaPutRequestEvent(gwy NodeRunnerInterface, srv NodeRunnerInterface, rep *PutReplica, tio TioInterface) *ReplicaPutRequestEvent {
at := configNetwork.durationControlPDU + config.timeClusterTrip
timedev := newTimedAnyEvent(gwy, at, srv, tio, configNetwork.sizeControlPDU)
return &ReplicaPutRequestEvent{zControlEvent{zEvent{*timedev}, rep.chunk.cid}, rep.chunk.sizeb, rep.num}
}
type McastChunkPutRequestEvent struct {
zControlEvent
sizeb int64 // size in bytes
winleft time.Time // reserve bid at or after this time
rzvgroup *RzvGroup // rendezvous group that has been already negotiated via previous McastChunkPut...
}
func newMcastChunkPutRequestEvent(gwy NodeRunnerInterface, group GroupInterface, chunk *Chunk, srv NodeRunnerInterface, tio TioInterface) *McastChunkPutRequestEvent {
at := configNetwork.durationControlPDU + config.timeClusterTrip
timedev := newTimedAnyEvent(gwy, at, group, srv, tio, true, configNetwork.sizeControlPDU)
cntrlev := &zControlEvent{zEvent{*timedev}, chunk.cid}
e := &McastChunkPutRequestEvent{zControlEvent: *cntrlev, sizeb: chunk.sizeb, winleft: TimeNil}
return e
}
func (e *McastChunkPutRequestEvent) String() string {
printid := uqrand(e.cid)
s := fmt.Sprintf("MCPRE %v=>%v,c#%d,ngt-group=%s", e.source.String(), e.target.String(), printid, e.targetgroup.String())
win := &TimWin{e.winleft, TimeNil}
if win.isNil() {
return fmt.Sprintf("[%s]", s)
}
return fmt.Sprintf("[%s,win=%s]", s, win.String())
}
type McastChunkPutAcceptEvent struct {
zControlEvent
rzvgroup GroupInterface
bid *PutBid
sizeb int64 // size in bytes
}
func newMcastChunkPutAcceptEvent(gwy NodeRunnerInterface, ngtgroup GroupInterface, chunk *Chunk, rzvgroup GroupInterface, tio TioInterface) *McastChunkPutAcceptEvent {
at := configNetwork.durationControlPDU + config.timeClusterTrip
timedev := newTimedAnyEvent(gwy, at, ngtgroup, tio, tio.GetTarget(), true, configNetwork.sizeControlPDU)
return &McastChunkPutAcceptEvent{zControlEvent{zEvent{*timedev}, chunk.cid}, rzvgroup, nil, chunk.sizeb}
}
func (e *McastChunkPutAcceptEvent) String() string {
printid := uqrand(e.cid)
return fmt.Sprintf("[MCPAE %v=>%v,c#%d,%s]", e.source.String(), e.target.String(), printid, e.rzvgroup.String())
}
// carries one server bid => gwy
type BidEvent struct {
zControlEvent
bid *PutBid
}
func (e *BidEvent) String() string {
printid := uqrand(e.cid)
return fmt.Sprintf("[MBE %v=>%v,c#%d]", e.source.String(), e.target.String(), printid)
}
func newBidEvent(srv NodeRunnerInterface, gwy NodeRunnerInterface, group GroupInterface, chunkid int64, bid *PutBid, tio TioInterface) *BidEvent {
atnet := configNetwork.durationControlPDU + config.timeClusterTrip
timedev := newTimedAnyEvent(srv, atnet, gwy, group, tio, configNetwork.sizeControlPDU)
return &BidEvent{zControlEvent{zEvent{*timedev}, chunkid}, bid}
}
// carries multiple bids generated by proxy on behalf of selected targets
type ProxyBidsEvent struct {
zControlEvent
bids []*PutBid
sizeb int64 // chunk size in bytes
}
func (e *ProxyBidsEvent) String() string {
printid := uqrand(e.cid)
return fmt.Sprintf("[ProxyBE %v=>%v,c#%d]", e.source.String(), e.target.String(), printid)
}
func newProxyBidsEvent(srv NodeRunnerInterface, gwy NodeRunnerInterface, group GroupInterface, chunkid int64, tio TioInterface, numbids int, chunksizeb int64, args ...*PutBid) *ProxyBidsEvent {
atnet := configNetwork.durationControlPDU + config.timeClusterTrip
timedev := newTimedAnyEvent(srv, atnet, gwy, group, tio, configNetwork.sizeControlPDU)
bids := make([]*PutBid, numbids)
for i := 0; i < len(args); i++ {
bids[i] = args[i]
}
return &ProxyBidsEvent{zControlEvent{zEvent{*timedev}, chunkid}, bids, chunksizeb}
}
// bid-done event, server => proxy
type BidDoneEvent struct {
zControlEvent
bid *PutBid
reservedIOdone time.Time
}
func (e *BidDoneEvent) String() string {
printid := uqrand(e.cid)
reservedIOdone := fmt.Sprintf("%-12.10v", e.reservedIOdone.Sub(time.Time{}))
return fmt.Sprintf("[BDE %v=>%v,c#%d],%s", e.source.String(), e.target.String(), printid, reservedIOdone)
}
func newBidDoneEvent(srv NodeRunnerInterface, proxy NodeRunnerInterface, bid *PutBid, reservedIOdone time.Time) *BidDoneEvent {
atnet := configNetwork.durationControlPDU + config.timeClusterTrip
timedev := newTimedAnyEvent(srv, atnet, proxy, bid.tio, configNetwork.sizeControlPDU)
return &BidDoneEvent{zControlEvent{zEvent{*timedev}, bid.tio.cid}, bid, reservedIOdone}
}
// acks ReplicaPut request, not to confuse with ReplicaPutAck
type ReplicaPutRequestAckEvent struct {
zControlEvent
num int // replica num
}
func newReplicaPutRequestAckEvent(srv NodeRunnerInterface, gwy NodeRunnerInterface, flow FlowInterface, tio TioInterface, diskdelay time.Duration) *ReplicaPutRequestAckEvent {
atnet := configNetwork.durationControlPDU + config.timeClusterTrip + diskdelay
timedev := newTimedAnyEvent(srv, atnet, gwy, tio, configNetwork.sizeControlPDU)
assert(flow.GetCid() == tio.GetCid())
assert(flow.GetRepnum() == tio.GetRepnum())
return &ReplicaPutRequestAckEvent{zControlEvent{zEvent{*timedev}, flow.GetCid()}, flow.GetRepnum()}
}
// acks receiving the entire replica data, not to confuse with PutRequestAck
type ReplicaPutAckEvent struct {
zControlEvent
num int // replica num
}
func (e *ReplicaPutAckEvent) String() string {
printid := uqrand(e.cid)
dtriggered := e.thtime.Sub(time.Time{})
return fmt.Sprintf("[RPAE %v=>%v,c#%d(%d),at=%11.10v]", e.source.String(), e.target.String(), printid, e.num, dtriggered)
}
func newReplicaPutAckEvent(srv NodeRunnerInterface, gwy NodeRunnerInterface, tio TioInterface, atdisk time.Duration) *ReplicaPutAckEvent {
atnet := configNetwork.durationControlPDU + config.timeClusterTrip
at := atnet + atdisk
timedev := newTimedAnyEvent(srv, at, gwy, tio, configNetwork.sizeControlPDU)
return &ReplicaPutAckEvent{zControlEvent{zEvent{*timedev}, tio.GetCid()}, tio.GetRepnum()}
}
type zDataEvent struct {
zEvent
cid int64
num int
offset int64
tobandwidth int64
}
type ReplicaDataEvent struct {
zDataEvent
}
func newReplicaDataEvent(gwy NodeRunnerInterface, srv NodeRunnerInterface, cid int64, repnum int, flow FlowInterface, frsize int) *ReplicaDataEvent {
tio := flow.GetTio()
assert(flow.GetCid() == tio.GetCid())
assert(cid == tio.GetCid())
assert(flow.GetRepnum() == tio.GetRepnum())
at := sizeToDuration(frsize, "B", flow.getbw(), "b") + config.timeClusterTrip
timedev := newTimedAnyEvent(gwy, at, srv, tio, frsize)
return &ReplicaDataEvent{zDataEvent{zEvent{*timedev}, cid, repnum, flow.getoffset(), flow.getbw()}}
}
// alternative c-tor
func newRepDataEvent(gwy NodeRunnerInterface, srv NodeRunnerInterface, tio TioInterface, offset int64, frsize int) *ReplicaDataEvent {
flow := tio.GetFlow()
at := sizeToDuration(frsize, "B", flow.getbw(), "b") + config.timeClusterTrip
timedev := newTimedAnyEvent(gwy, at, srv, tio, frsize)
return &ReplicaDataEvent{zDataEvent{zEvent{*timedev}, tio.GetCid(), tio.GetRepnum(), offset, flow.getbw()}}
}
func (e *ReplicaDataEvent) String() string {
printid := uqrand(e.cid)
dcreated := e.crtime.Sub(time.Time{})
dtriggered := e.thtime.Sub(time.Time{})
if e.targetgroup == nil {
return fmt.Sprintf("[RDE %v=>%v,c#%d,num=%d,offset=%d,(%11.10v,%11.10v)]",
e.source.String(), e.target.String(), printid, e.num, e.offset, dcreated, dtriggered)
}
return fmt.Sprintf("[mcast-RDE %v=>%v,group=%v,c#%d,offset=%d,(%11.10v,%11.10v)]",
e.source.String(), e.target.String(), e.targetgroup.String(), printid, e.offset, dcreated, dtriggered)
}
// note: constructs ReplicaDataEvent with srv == nil and group != nil
func newMcastChunkDataEvent(gwy NodeRunnerInterface, rzvgroup GroupInterface, chunk *Chunk, flow FlowInterface, frsize int, tio TioInterface) *ReplicaDataEvent {
at := sizeToDuration(frsize, "B", flow.getbw(), "b") + config.timeClusterTrip
timedev := newTimedAnyEvent(gwy, at, rzvgroup, tio, tio.GetTarget(), true, frsize)
return &ReplicaDataEvent{zDataEvent{zEvent{*timedev}, chunk.cid, 0, flow.getoffset(), flow.getbw()}}
}