-
Notifications
You must be signed in to change notification settings - Fork 3
/
brain_local.go
612 lines (521 loc) · 14.2 KB
/
brain_local.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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
package zenmodel
import (
"fmt"
"sync"
"time"
"github.com/dgraph-io/ristretto"
"github.com/zenmodel/zenmodel/internal/constants"
"github.com/zenmodel/zenmodel/internal/errors"
"github.com/zenmodel/zenmodel/internal/log"
"github.com/zenmodel/zenmodel/internal/utils"
"go.uber.org/zap"
"k8s.io/client-go/util/workqueue"
)
func NewBrainLocal(brainprint Brainprint, withOpts ...Option) *BrainLocal {
brain := &BrainLocal{
id: utils.GenUUID(),
Brainprint: brainprint,
brainLocalOptions: brainLocalOptions{
rateLimiterBaseDelay: 5 * time.Millisecond,
rateLimiterMaxDelay: 1000 * time.Second,
workerNum: 5,
memoryNumCounters: 1e7, // number of keys to track frequency of (10M).
memoryMaxCost: 1 << 30, // maximum cost of cache (1GB).
},
memory: nil,
queue: nil,
state: BrainStateSleeping,
logger: log.GetLogger(),
}
// reset maintainer, logger, ID, etc.
for _, opt := range withOpts {
opt.apply(brain)
}
brain.logger = brain.logger.With(zap.String("brain", brain.id))
brain.logger.Debug("new local brain created", zap.Object("brainprint", &brainprint))
return brain
}
type BrainLocal struct {
// ID 不可编辑
id string
Brainprint
// read only state, unable to set
// Brainprint is in the Running state when there are 1 or more Activate neuron
// or 1 or more StandBy Edge.
state BrainState
// brain memories
memory *ristretto.Cache
brainLocalOptions
// local maintainer queue
queue workqueue.RateLimitingInterface
wg sync.WaitGroup
logger *zap.Logger
}
type brainLocalOptions struct {
rateLimiterBaseDelay time.Duration
rateLimiterMaxDelay time.Duration
workerNum int
memoryNumCounters int64
memoryMaxCost int64
}
func (b *BrainLocal) Entry() error {
// get all entry links
linkIDs := make([]string, 0)
for _, link := range b.links {
if link.IsEntryLink() {
linkIDs = append(linkIDs, link.id)
}
}
return b.trigLinks(linkIDs...)
}
func (b *BrainLocal) EntryWithMemory(keysAndValues ...interface{}) error {
if err := b.SetMemory(keysAndValues...); err != nil {
return err
}
return b.Entry()
}
func (b *BrainLocal) TrigLinks(linkIDs ...string) error {
return b.trigLinks(linkIDs...)
}
func (b *BrainLocal) SetMemory(keysAndValues ...interface{}) error {
if len(keysAndValues)%2 != 0 {
return fmt.Errorf("key and value are not paired")
}
if err := b.ensureMemoryInit(); err != nil {
// TODO wrap error
return err
}
for i := 0; i < len(keysAndValues); i += 2 {
k := keysAndValues[i]
v := keysAndValues[i+1]
b.memory.Set(k, v, 1) // TODO maybe calculate cost
b.logger.Debug("set memory", zap.Any("key", k), zap.Any("value", v))
}
b.memory.Wait()
return nil
}
func (b *BrainLocal) GetMemory(key interface{}) interface{} {
if b.memory == nil {
return nil
}
v, _ := b.memory.Get(key)
return v
}
func (b *BrainLocal) ExistMemory(key interface{}) bool {
if b.memory == nil {
return false
}
_, ok := b.memory.Get(key)
return ok
}
func (b *BrainLocal) DeleteMemory(key interface{}) {
if b.memory == nil {
return
}
b.memory.Del(key)
}
func (b *BrainLocal) ClearMemory() {
if b.memory == nil {
return
}
b.memory.Clear()
}
func (b *BrainLocal) GetState() BrainState {
return b.state
}
func (b *BrainLocal) Wait() {
b.wg.Wait()
}
func (b *BrainLocal) Start() {
b.logger.Info("brain maintainer start", zap.Int("workerNum", b.workerNum))
// re-new queue
if b.queue != nil {
b.ShutDown()
}
b.queue = workqueue.NewRateLimitingQueueWithConfig(
workqueue.NewItemExponentialFailureRateLimiter(b.rateLimiterBaseDelay, b.rateLimiterMaxDelay),
workqueue.RateLimitingQueueConfig{
Name: "brain-queue-local", // TODO add brain ID ?
})
// start worker, will terminate goroutine when queue shutdown
for i := 0; i < b.workerNum; i++ {
b.wg.Add(1)
go b.runWorker()
}
}
func (b *BrainLocal) ShutDown() {
b.logger.Info("brain maintainer shutdown")
b.queue.ShutDown()
}
func (b *BrainLocal) SendMessage(message constants.Message) {
b.logger.Debug("send message", zap.Object("message", message))
b.queue.Add(message)
}
func (b *BrainLocal) runWorker() {
defer b.wg.Done()
for b.processFromQueue() {
}
}
// should block, without goroutine
func (b *BrainLocal) processFromQueue() bool {
msg, shutdown := b.queue.Get()
if shutdown {
return false
}
defer b.queue.Done(msg)
message := msg.(constants.Message)
logger := b.logger.With(zap.Object("message", message))
logger.Debug("process message")
switch message.Kind {
case constants.MessageKindLink:
// 1. link message
if err := b.HandleLink(message.Action, message.ID); err != nil {
logger.Error("handle link message error", zap.Error(err))
return true
}
case constants.MessageKindNeuron:
// 2. neuron message
if err := b.HandleNeuron(message.Action, message.ID); err != nil {
logger.Error("handle neuron message error", zap.Error(err))
if errors.ErrorIsTryProcessLater(err) {
b.queue.AddAfter(msg, 500*time.Millisecond)
}
return true
}
case constants.MessageKindBrain:
if err := b.HandleBrain(message.Action); err != nil {
logger.Error("handle brain message error", zap.Error(err))
return true
}
default:
// logger unsupported message kind
logger.Error("message error", zap.Error(errors.ErrUnsupportedMessageKind(message.Kind)))
return true
}
// 3. 重新计算 brain 状态并刷新
if message.Kind != constants.MessageKindBrain {
b.RefreshState()
}
b.queue.Forget(msg)
return true
}
func (b *BrainLocal) trigLinks(linkIDs ...string) error {
if len(linkIDs) == 0 {
return nil
}
if err := b.ensureMemoryInit(); err != nil {
// TODO wrap error
return err
}
// ensure brain maintainer start
b.ensureStart()
// goroutine wait
var wg sync.WaitGroup
for _, linkID := range linkIDs {
link := b.getLink(linkID)
if link == nil {
continue
}
wg.Add(1)
go b.trigLink(&wg, link)
}
wg.Wait()
return nil
}
func (b *BrainLocal) ensureStart() {
if b.state == BrainStateSleeping {
b.Start()
b.state = BrainStateAwake
}
return
}
func (b *BrainLocal) trigLink(wg *sync.WaitGroup, link *Link) {
defer wg.Done()
if link.state != LinkStateReady {
// change link state as ready
link.state = LinkStateReady
// send link message
b.SendMessage(constants.Message{
Kind: constants.MessageKindLink,
Action: constants.MessageActionLinkReady,
ID: link.id,
})
}
return
}
func (b *BrainLocal) RefreshState() {
inhibitCnt, activateCnt := b.getNeuronCountByState()
initCnt, waitCnt, readyCnt := b.getLinkCountByState()
b.logger.Debug("count link and neuron by state",
zap.Int("neuron_inhibit", inhibitCnt), zap.Int("neuron_activated", activateCnt),
zap.Int("link_init", initCnt), zap.Int("link_wait", waitCnt), zap.Int("link_ready", readyCnt))
// send brain sleep message
if activateCnt+waitCnt+readyCnt == 0 {
b.SendMessage(constants.Message{
Kind: constants.MessageKindBrain,
Action: constants.MessageActionBrainSleep,
})
} else { // > 0, set to running
b.state = BrainStateRunning
}
}
func (b *BrainLocal) getNeuronCountByState() (int, int) {
var inhibitCnt, activateCnt int
for _, neuron := range b.neurons {
switch neuron.state {
case NeuronStateInhibited:
inhibitCnt++
case NeuronStateActivated:
activateCnt++
}
}
return inhibitCnt, activateCnt
}
func (b *BrainLocal) getLinkCountByState() (int, int, int) {
var initCnt, waitCnt, readyCnt int
for _, link := range b.links {
switch link.state {
case LinkStateInit:
initCnt++
case LinkStateWait:
waitCnt++
case LinkStateReady:
readyCnt++
}
}
return initCnt, waitCnt, readyCnt
}
func (b *BrainLocal) HandleLink(action constants.MessageAction, linkID string) error {
link := b.getLink(linkID)
if link == nil {
return errors.ErrLinkNotFound(linkID)
}
switch action {
case constants.MessageActionLinkInit:
// do nothing
case constants.MessageActionLinkWait:
// do nothing
case constants.MessageActionLinkReady:
destNeuron := b.getNeuron(link.to)
if destNeuron == nil {
return errors.ErrNeuronNotFound(link.to)
}
// try dest neuron activate
b.SendMessage(constants.Message{
Kind: constants.MessageKindNeuron,
Action: constants.MessageActionNeuronTryActivate,
ID: destNeuron.id,
})
default:
return errors.ErrUnsupportedMessageAction(action)
}
return nil
}
func (b *BrainLocal) HandleNeuron(action constants.MessageAction, neuronID string) error {
neuron := b.getNeuron(neuronID)
if neuron == nil {
return errors.ErrNeuronNotFound(neuronID)
}
switch action {
case constants.MessageActionNeuronTryInhibit:
// do nothing , neuron 只有自己 Inhibit
case constants.MessageActionNeuronTryActivate:
return b.tryActivateNeuron(neuron)
case constants.MessageActionNeuronCastAnyway:
return b.castAnyway(neuron)
default:
return errors.ErrUnsupportedMessageAction(action)
}
return nil
}
func (b *BrainLocal) HandleBrain(action constants.MessageAction) error {
switch action {
case constants.MessageActionBrainSleep:
b.ShutDown()
b.allLinkNeuronInhibit()
b.state = BrainStateSleeping
return nil
default:
return errors.ErrUnsupportedMessageAction(action)
}
}
func (b *BrainLocal) allLinkNeuronInhibit() {
for _, link := range b.links {
link.state = LinkStateInit
}
for _, neuron := range b.neurons {
neuron.state = NeuronStateInhibited
}
}
func (b *BrainLocal) tryActivateNeuron(neuron *Neuron) error {
if neuron.state == NeuronStateActivated {
b.logger.Debug("neuron already activated", zap.String("neuron", neuron.id))
return nil
}
should := b.ifNeuronShouldActivate(neuron)
if !should {
b.logger.Debug("neuron should not be activated", zap.String("neuron", neuron.id))
return nil
}
// should END, send brain sleep message
if neuron.id == EndNeuronID {
b.logger.Info("arrival at END neuron")
b.SendMessage(constants.Message{
Kind: constants.MessageKindBrain,
Action: constants.MessageActionBrainSleep,
})
return nil
}
return b.activateNeuron(neuron)
}
func (b *BrainLocal) tryCast(neuron *Neuron) error {
logger := b.logger.With(zap.String("neuron", neuron.id))
if neuron.state != NeuronStateInhibited {
logger.Debug("neuron not process done, should not cast")
return nil
}
logger.Debug("neuron try to cast")
// 决策出边/传导组
selected := neuron.selectFn(&brainLocalRuntime{
brain: b,
currentNeuron: nil,
})
// 选中的 cast group 中的 link 状态为 wait 的设置为 ready,SendMessage (为 init 的则不改变)
selectedLinks := make(map[string]struct{})
for linkID := range neuron.castGroups[selected] {
selectedLinks[linkID] = struct{}{}
link := b.getLink(linkID)
if link.state == LinkStateWait {
link.state = LinkStateReady
b.SendMessage(constants.Message{
Kind: constants.MessageKindLink,
Action: constants.MessageActionLinkReady,
ID: link.id,
})
} else { // LinkStateWait or LinkStateReady
logger.Debug("already cast, will not cast again", zap.String("link", link.id))
}
}
// 未选择的 out-link 状态从 wait 变为 init
for _, group := range neuron.castGroups {
for linkID := range group {
_, found := selectedLinks[linkID]
if found {
continue
}
link := b.getLink(linkID)
if link.state == LinkStateWait {
link.state = LinkStateInit
}
}
}
return nil
}
func (b *BrainLocal) castAnyway(neuron *Neuron) error {
logger := b.logger.With(zap.String("neuron", neuron.id))
logger.Debug("neuron will cast anyway")
// 决策出边/传导组
selected := neuron.selectFn(&brainLocalRuntime{
brain: b,
currentNeuron: nil,
})
// 选中的 cast group 中的 link 状态为 wait 的设置为 ready,SendMessage
selectedLinks := make(map[string]struct{})
for linkID := range neuron.castGroups[selected] {
selectedLinks[linkID] = struct{}{}
link := b.getLink(linkID)
if link.state == LinkStateWait || link.state == LinkStateInit { // different with tryCast(), link can also cast when its state is Init
link.state = LinkStateReady
b.SendMessage(constants.Message{
Kind: constants.MessageKindLink,
Action: constants.MessageActionLinkReady,
ID: link.id,
})
} else { // LinkStateReady
return errors.Wrapf(errors.ErrTryProcessLater, "already casting")
}
}
// 未选择的 out-link 状态变为 wait, 因为之前的 cast anyway 可能会将 link 设置为 init 或 ready
for _, group := range neuron.castGroups {
for linkID := range group {
_, found := selectedLinks[linkID]
if found {
continue
}
link := b.getLink(linkID)
link.state = LinkStateWait
}
}
return nil
}
// ifNeuronShouldActivate return link IDs in trigged trigger group
func (b *BrainLocal) ifNeuronShouldActivate(neuron *Neuron) bool {
if b.state == BrainStateSleeping {
return false
}
// 如果任一触发组中的 link 全都是 Ready, 则应该 activate neuron
for _, group := range neuron.triggerGroups {
trigLinks := make([]*Link, 0)
for _, linkID := range group {
link := b.getLink(linkID)
if link.state == LinkStateReady {
trigLinks = append(trigLinks, link)
} else {
break
}
}
if len(trigLinks) == len(group) && len(group) != 0 {
return true
}
}
return false
}
func (b *BrainLocal) activateNeuron(neuron *Neuron) error {
neuron.state = NeuronStateActivated
// in-link set init
for _, group := range neuron.triggerGroups {
for _, linkID := range group {
link := b.getLink(linkID)
link.state = LinkStateInit
}
}
// out-link set wait
for _, group := range neuron.castGroups {
for linkID := range group {
link := b.getLink(linkID)
link.state = LinkStateWait
}
}
neuron.count.process++
err := neuron.processor.Process(&brainLocalRuntime{
brain: b,
currentNeuron: neuron,
})
neuron.state = NeuronStateInhibited
if err != nil {
neuron.count.failed++
return err
}
// SucceedCount++
neuron.count.succeed++
return b.tryCast(neuron)
}
func (b *BrainLocal) ensureMemoryInit() error {
if b.memory != nil {
return nil
}
return b.initMemory()
}
func (b *BrainLocal) initMemory() error {
cache, err := ristretto.NewCache(&ristretto.Config{
NumCounters: b.memoryNumCounters, // number of keys to track frequency of (10M).
MaxCost: b.memoryMaxCost, // maximum cost of cache (1GB).
BufferItems: 64, // number of keys per Get buffer.
})
if err != nil {
// TODO Wrap error
return err
}
b.memory = cache
return nil
}