-
Notifications
You must be signed in to change notification settings - Fork 1
/
vertex.go
332 lines (290 loc) · 9.27 KB
/
vertex.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
package worker
import (
"fmt"
"reflect"
"github.com/AsynkronIT/protoactor-go/actor"
"github.com/gogo/protobuf/types"
"github.com/google/uuid"
"github.com/pkg/errors"
"github.com/rerorero/prerogel/aggregator"
"github.com/rerorero/prerogel/command"
"github.com/rerorero/prerogel/plugin"
"github.com/rerorero/prerogel/util"
"github.com/sirupsen/logrus"
)
type vertexActor struct {
util.ActorUtil
behavior actor.Behavior
plugin plugin.Plugin
vertex plugin.Vertex
partitionID uint64
halted bool
prevStepMessages []plugin.Message
messageQueue []plugin.Message
ackRecorder *util.AckRecorder
computeRespondTo *actor.PID
aggregatedCurrentStep map[string]*types.Any
statsMessageSent uint64
}
type loadVertexLocal struct {
vertex plugin.Vertex
}
type computeContextImpl struct {
superStep uint64
ctx actor.Context
vertexActor *vertexActor
aggregatedPrevStep map[string]*types.Any
}
var _ = (plugin.ComputeContext)(&computeContextImpl{})
func (c *computeContextImpl) SuperStep() uint64 {
return c.superStep
}
func (c *computeContextImpl) ReceivedMessages() []plugin.Message {
return c.vertexActor.prevStepMessages
}
func (c *computeContextImpl) SendMessageTo(dest plugin.VertexID, m plugin.Message) error {
pb, err := c.vertexActor.plugin.MarshalMessage(m)
if err != nil {
c.vertexActor.ActorUtil.LogError(c.ctx, fmt.Sprintf("failed to marshal message: id=%v, message=%#v", c.vertexActor.vertex.GetID(), m))
return err
}
messageID := uuid.New().String()
c.ctx.Request(c.ctx.Parent(), &command.SuperStepMessage{
Uuid: messageID,
SuperStep: c.superStep,
SrcVertexId: string(c.vertexActor.vertex.GetID()),
DestVertexId: string(dest),
Message: pb,
})
c.vertexActor.ActorUtil.LogDebug(c.ctx, fmt.Sprintf("message sent: uuid=%s, %v -> %v",
messageID, c.vertexActor.vertex.GetID(), dest))
if !c.vertexActor.ackRecorder.AddToWaitList(messageID) {
c.vertexActor.ActorUtil.LogWarn(c.ctx, fmt.Sprintf("duplicate superstep message: from=%v to=%v", c.vertexActor.vertex.GetID(), dest))
}
return nil
}
func (c *computeContextImpl) VoteToHalt() {
c.vertexActor.ActorUtil.LogDebug(c.ctx, "halted by user")
c.vertexActor.halted = true
}
func (c *computeContextImpl) GetAggregated(aggregatorName string) (plugin.AggregatableValue, bool, error) {
aggregator, err := findAggregator(c.vertexActor.plugin.GetAggregators(), aggregatorName)
if err != nil {
return nil, false, err
}
value, ok := c.aggregatedPrevStep[aggregatorName]
if ok {
v, err := aggregator.UnmarshalValue(value)
if err != nil {
return nil, false, errors.Wrapf(err, "failed to unmarshal aggregated value: %+v", value)
}
return v, true, nil
}
return nil, false, nil
}
func (c *computeContextImpl) PutAggregatable(aggregatorName string, v plugin.AggregatableValue) error {
aggregator, err := findAggregator(c.vertexActor.plugin.GetAggregators(), aggregatorName)
if err != nil {
return err
}
current, ok := c.vertexActor.aggregatedCurrentStep[aggregatorName]
if ok {
// aggregate TODO: verbose marshalling
v2, err := aggregator.UnmarshalValue(current)
if err != nil {
return errors.Wrapf(err, "failed to unmarshal aggregated value: %+v", current.Value)
}
val, err := aggregator.Aggregate(v, v2)
if err != nil {
return errors.Wrap(err, "failed to Aggregate()")
}
pb, err := aggregator.MarshalValue(val)
if err != nil {
return errors.Wrapf(err, "failed to marshal aggregatable value: %#v", v)
}
c.vertexActor.aggregatedCurrentStep[aggregatorName] = pb
return nil
}
pb, err := aggregator.MarshalValue(v)
if err != nil {
return errors.Wrapf(err, "failed to marshal aggregatable value: %#v", v)
}
c.vertexActor.aggregatedCurrentStep[aggregatorName] = pb
return nil
}
// NewVertexActor returns an actor instance
func NewVertexActor(plugin plugin.Plugin, logger *logrus.Logger) actor.Actor {
ar := &util.AckRecorder{}
ar.Clear()
a := &vertexActor{
plugin: plugin,
ActorUtil: util.ActorUtil{
Logger: logger,
},
ackRecorder: ar,
}
a.behavior.Become(a.waitInit)
return a
}
// Receive is message handler
func (state *vertexActor) Receive(context actor.Context) {
if state.ActorUtil.IsSystemMessage(context.Message()) {
return
}
switch cmd := context.Message().(type) {
case *command.GetVertexValue:
ack := &command.GetVertexValueAck{
VertexId: cmd.VertexId,
}
if state.vertex != nil {
ack.Value = state.vertex.GetValueAsString()
}
context.Respond(ack)
return
default:
state.behavior.Receive(context)
return
}
}
func (state *vertexActor) waitInit(context actor.Context) {
var vert plugin.Vertex
switch cmd := context.Message().(type) {
case *command.LoadVertex:
var err error
vert, err = state.plugin.NewVertex(plugin.VertexID(cmd.VertexId))
if err != nil {
e := fmt.Sprintf("failed to NewVertex: id=%s err=%s", cmd.VertexId, err.Error())
state.ActorUtil.LogError(context, e)
context.Respond(&command.LoadVertexAck{VertexId: string(state.vertex.GetID()), Error: e})
return
}
case *loadVertexLocal:
vert = cmd.vertex
default:
state.ActorUtil.Fail(context, fmt.Errorf("[waitInit] unhandled vertex command: command=%#v(%v)", cmd, reflect.TypeOf(cmd)))
}
if state.vertex != nil {
err := fmt.Sprintf("vertex has already initialized: id=%s", vert.GetID())
state.ActorUtil.LogError(context, err)
context.Respond(&command.LoadVertexAck{VertexId: string(state.vertex.GetID()), Error: err})
return
}
state.vertex = vert
context.Respond(&command.LoadVertexAck{
VertexId: string(state.vertex.GetID()),
})
state.behavior.Become(state.superstep)
state.ActorUtil.LogInfo(context, fmt.Sprintf("vertex %v loaded", state.vertex.GetID()))
}
func (state *vertexActor) superstep(context actor.Context) {
switch cmd := context.Message().(type) {
case *command.SuperStepBarrier:
// move messages from queue to buffer
state.prevStepMessages = state.messageQueue
state.messageQueue = nil
context.Respond(&command.SuperStepBarrierAck{
VertexId: string(state.vertex.GetID()),
})
state.ActorUtil.LogDebug(context, fmt.Sprintf("received barrier message"))
return
case *command.Compute:
state.computeRespondTo = context.Sender()
state.onComputed(context, cmd)
return
case *command.SuperStepMessage:
if state.vertex.GetID() != plugin.VertexID(cmd.DestVertexId) {
state.ActorUtil.Fail(context, fmt.Errorf("inconsistent vertex id: %#v", *cmd))
return
}
// TODO: verify cmd.SuperStep
pb, err := state.plugin.UnmarshalMessage(cmd.Message)
if err != nil {
state.ActorUtil.Fail(context, fmt.Errorf("failed to unmarshal message: %#v", *cmd))
return
}
state.messageQueue = append(state.messageQueue, pb)
state.halted = false
context.Respond(&command.SuperStepMessageAck{
Uuid: cmd.Uuid,
})
return
case *command.SuperStepMessageAck:
if state.ackRecorder.HasCompleted() {
state.ActorUtil.LogWarn(context, fmt.Sprintf("unhaneled message id=%v, compute() has already completed", cmd.Uuid))
return
}
if !state.ackRecorder.Ack(cmd.Uuid) {
state.ActorUtil.LogWarn(context, fmt.Sprintf("duplicated or unhaneled message: uuid=%v", cmd.Uuid))
}
if state.ackRecorder.HasCompleted() {
state.respondComputeAck(context)
}
return
default:
state.ActorUtil.Fail(context, fmt.Errorf("[superstep] unhandled vertex command: command=%#v", cmd))
return
}
}
func (state *vertexActor) onComputed(ctx actor.Context, cmd *command.Compute) {
state.aggregatedCurrentStep = make(map[string]*types.Any)
state.statsMessageSent = 0
// force to compute() in super step 0
// otherwise halt if there are no messages
if cmd.SuperStep == 0 {
state.halted = false
} else if len(state.prevStepMessages) == 0 {
state.ActorUtil.LogDebug(ctx, "halted due to no message")
state.halted = true
}
if state.halted {
state.respondComputeAck(ctx)
return
}
state.ackRecorder.Clear()
computeContext := &computeContextImpl{
superStep: cmd.SuperStep,
ctx: ctx,
vertexActor: state,
aggregatedPrevStep: cmd.AggregatedValues,
}
if err := state.vertex.Compute(computeContext); err != nil {
state.ActorUtil.Fail(ctx, errors.Wrap(err, "failed to compute"))
return
}
state.statsMessageSent = uint64(state.ackRecorder.Size())
if state.ackRecorder.HasCompleted() {
state.respondComputeAck(ctx)
}
return
}
func (state *vertexActor) respondComputeAck(ctx actor.Context) {
if len(state.messageQueue) > 0 {
// activate if it receives messages to be handled in the next step
state.halted = false
}
stats, err := findAggregator(state.plugin.GetAggregators(), VertexStatsName)
if err == nil {
var active uint64
if !state.halted {
active = 1
}
pb, err := stats.MarshalValue(&aggregator.VertexStats{
ActiveVertices: active,
TotalVertices: 1,
MessagesSent: state.statsMessageSent,
})
if err != nil {
state.ActorUtil.LogError(ctx, fmt.Sprintf("failed to marshal stats: %v", err))
return
}
state.aggregatedCurrentStep[VertexStatsName] = pb
}
// TODO: store vertex state
ctx.Send(state.computeRespondTo, &command.ComputeAck{
VertexId: string(state.vertex.GetID()),
Halted: state.halted,
AggregatedValues: state.aggregatedCurrentStep,
})
state.aggregatedCurrentStep = nil
state.ActorUtil.LogDebug(ctx, "compute() completed")
}