/
partition.go
263 lines (238 loc) · 8.12 KB
/
partition.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
package worker
import (
"fmt"
"github.com/AsynkronIT/protoactor-go/actor"
"github.com/gogo/protobuf/types"
"github.com/rerorero/prerogel/command"
"github.com/rerorero/prerogel/plugin"
"github.com/rerorero/prerogel/util"
"github.com/sirupsen/logrus"
)
type partitionActor struct {
util.ActorUtil
partitionID uint64
behavior actor.Behavior
plugin plugin.Plugin
vertices map[plugin.VertexID]*actor.PID
vertexProps *actor.Props
ackRecorder *util.AckRecorder
aggregatedCurrentStep map[string]*types.Any
}
// NewPartitionActor returns an actor instance
func NewPartitionActor(plg plugin.Plugin, vertexProps *actor.Props, logger *logrus.Logger) actor.Actor {
ar := &util.AckRecorder{}
ar.Clear()
a := &partitionActor{
plugin: plg,
ActorUtil: util.ActorUtil{
Logger: logger,
},
vertexProps: vertexProps,
vertices: make(map[plugin.VertexID]*actor.PID),
ackRecorder: ar,
}
a.behavior.Become(a.waitInit)
return a
}
// Receive is message handler
func (state *partitionActor) Receive(context actor.Context) {
if state.ActorUtil.IsSystemMessage(context.Message()) {
// ignore
return
}
switch cmd := context.Message().(type) {
case *command.ClusterInfo:
state.broadcastToVertices(context, cmd)
return
case *command.GetVertexValue:
if v, ok := state.vertices[plugin.VertexID(cmd.VertexId)]; ok {
context.Forward(v)
} else {
state.LogWarn(context, fmt.Sprintf("%v no such vertex id in partition %v", cmd.VertexId, state.partitionID))
context.Respond(&command.GetVertexValueAck{VertexId: cmd.VertexId})
}
return
default:
state.behavior.Receive(context)
return
}
}
func (state *partitionActor) waitInit(context actor.Context) {
switch cmd := context.Message().(type) {
case *command.InitPartition: // sent from parent
state.partitionID = cmd.PartitionId
context.Respond(&command.InitPartitionAck{
PartitionId: state.partitionID,
})
state.resetAckRecorder()
state.behavior.Become(state.idle)
state.ActorUtil.LogInfo(context, "initialization partition has completed")
return
default:
state.ActorUtil.Fail(context, fmt.Errorf("[waitInit] unhandled partition command: command=%#v", cmd))
return
}
}
func (state *partitionActor) idle(context actor.Context) {
switch cmd := context.Message().(type) {
case *command.LoadVertex:
vid := plugin.VertexID(cmd.VertexId)
if _, ok := state.vertices[vid]; ok {
err := fmt.Sprintf("vertex has already created: id=%s", cmd.VertexId)
state.ActorUtil.LogError(context, err)
context.Respond(&command.LoadVertexAck{VertexId: string(cmd.VertexId), Error: err})
return
}
pid, err := context.SpawnNamed(state.vertexProps, fmt.Sprintf("v%v", vid))
if err != nil {
err := fmt.Sprintf("failed to spawn actor: id=%s", cmd.VertexId)
state.ActorUtil.LogError(context, err)
context.Respond(&command.LoadVertexAck{VertexId: string(cmd.VertexId), Error: err})
return
}
state.vertices[vid] = pid
context.Forward(pid)
return
case *command.LoadPartitionVertices:
state.resetAckRecorder()
var loadErr string
if err := state.plugin.NewPartitionVertices(state.partitionID, cmd.NumOfPartitions, func(v plugin.Vertex) {
// TODO: concurrency unsafe
vid := v.GetID()
pid, err := context.SpawnNamed(state.vertexProps, fmt.Sprintf("v%v", vid))
if err != nil {
loadErr = fmt.Sprintf("failed to spawn actor: id=%s", vid)
state.ActorUtil.LogError(context, loadErr)
return
}
state.vertices[vid] = pid
context.Request(pid, &loadVertexLocal{vertex: v})
state.ackRecorder.AddToWaitList(string(vid))
}); err != nil {
state.ackRecorder.Clear()
state.ActorUtil.LogError(context, err.Error())
context.Respond(&command.LoadPartitionVerticesAck{PartitionId: state.partitionID, Error: err.Error()})
return
}
if loadErr != "" {
state.ackRecorder.Clear()
context.Respond(&command.LoadPartitionVerticesAck{PartitionId: state.partitionID, Error: loadErr})
return
}
state.ActorUtil.LogDebug(context, fmt.Sprintf("start waiting for loading partition vertices"))
state.behavior.Become(state.waitLoadPartitionVertices)
return
case *command.SuperStepBarrier:
if len(state.vertices) == 0 {
state.ActorUtil.LogInfo(context, fmt.Sprintf("[idle] no vertex is assigned"))
return
}
state.resetAckRecorder()
state.broadcastToVertices(context, cmd)
state.behavior.Become(state.waitSuperStepBarrierAck)
state.aggregatedCurrentStep = make(map[string]*types.Any)
return
case *command.SuperStepMessage:
state.handleMessage(context, cmd)
return
default:
state.ActorUtil.Fail(context, fmt.Errorf("[idle] unhandled partition command: command=%#v", cmd))
return
}
}
func (state *partitionActor) waitLoadPartitionVertices(context actor.Context) {
switch cmd := context.Message().(type) {
case *command.LoadVertexAck:
if !state.ackRecorder.Ack(cmd.VertexId) {
state.ActorUtil.LogWarn(context, fmt.Sprintf("LoadVertexAck(partition) duplicated: id=%v", cmd.VertexId))
}
if state.ackRecorder.HasCompleted() {
context.Send(context.Parent(), &command.LoadPartitionVerticesAck{
PartitionId: state.partitionID,
})
state.resetAckRecorder()
state.behavior.Become(state.idle)
state.ActorUtil.LogDebug(context, "loading partition finished")
}
return
default:
state.ActorUtil.Fail(context, fmt.Errorf("[waitLoadPartitionVertices] unhandled partition command: command=%#v", cmd))
return
}
}
func (state *partitionActor) waitSuperStepBarrierAck(context actor.Context) {
switch cmd := context.Message().(type) {
case *command.SuperStepBarrierAck: // sent from parent
if !state.ackRecorder.Ack(cmd.VertexId) {
state.ActorUtil.LogWarn(context, fmt.Sprintf("SuperStepBarrierAck duplicated: id=%v", cmd.VertexId))
}
if state.ackRecorder.HasCompleted() {
context.Send(context.Parent(), &command.SuperStepBarrierPartitionAck{
PartitionId: state.partitionID,
})
state.resetAckRecorder()
state.behavior.Become(state.superstep)
state.ActorUtil.LogDebug(context, "partition: super step barrier end")
}
return
default:
state.ActorUtil.Fail(context, fmt.Errorf("[waitSpuerStepBarrierAck] unhandled partition command: command=%#v", cmd))
return
}
}
func (state *partitionActor) superstep(context actor.Context) {
switch cmd := context.Message().(type) {
case *command.Compute: // sent from parent
state.resetAckRecorder()
state.broadcastToVertices(context, cmd)
return
case *command.ComputeAck: // sent from vertices
// TODO: aggregate halted status
if cmd.AggregatedValues != nil {
if err := aggregateValueMap(state.plugin.GetAggregators(), state.aggregatedCurrentStep, cmd.AggregatedValues); err != nil {
state.ActorUtil.Fail(context, err)
return
}
}
if !state.ackRecorder.Ack(cmd.VertexId) {
state.ActorUtil.LogWarn(context, fmt.Sprintf("ComputeAck duplicated: id=%v", cmd.VertexId))
}
if state.ackRecorder.HasCompleted() {
context.Send(context.Parent(), &command.ComputePartitionAck{
PartitionId: state.partitionID,
AggregatedValues: state.aggregatedCurrentStep,
})
state.resetAckRecorder()
state.aggregatedCurrentStep = nil
state.behavior.Become(state.idle)
state.ActorUtil.LogInfo(context, "partition: compute has completed")
}
return
case *command.SuperStepMessage:
state.handleMessage(context, cmd)
return
default:
state.ActorUtil.Fail(context, fmt.Errorf("[superstep] unhandled partition command: command=%#v", cmd))
return
}
}
func (state *partitionActor) handleMessage(context actor.Context, cmd *command.SuperStepMessage) {
if _, ok := state.vertices[plugin.VertexID(cmd.SrcVertexId)]; ok {
context.Forward(context.Parent())
} else if pid, ok := state.vertices[plugin.VertexID(cmd.DestVertexId)]; ok {
context.Forward(pid)
} else {
state.ActorUtil.LogError(context, fmt.Sprintf("[superstep] unknown destination message: msg=%#v", cmd))
}
}
func (state *partitionActor) broadcastToVertices(context actor.Context, msg interface{}) {
for _, pid := range state.vertices {
context.Request(pid, msg)
}
}
func (state *partitionActor) resetAckRecorder() {
state.ackRecorder.Clear()
for id := range state.vertices {
state.ackRecorder.AddToWaitList(string(id))
}
}