-
Notifications
You must be signed in to change notification settings - Fork 1
/
executer.go
666 lines (621 loc) · 17.5 KB
/
executer.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
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
// Copyright 2021 Massimo Comuzzo, Michele Pasqua and Marino Miculan
// SPDX-License-Identifier: Apache-2.0
package goabu
import (
"errors"
"fmt"
"reflect"
"strconv"
"sync"
"github.com/abu-lang/goabu/config"
"github.com/abu-lang/goabu/ecarule"
"github.com/abu-lang/goabu/memory"
"github.com/abu-lang/goabu/parser"
"github.com/abu-lang/goabu/stringset"
"github.com/hyperjumptech/grule-rule-engine/ast"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
type Executer struct {
memory memory.ResourceController
lockMemory sync.RWMutex
types map[string]string
pool []Update
coordinator execCoordinator
updateReceiver chan<- preparedUpdates
lockPool sync.Mutex
ruleLibrary map[string]ecarule.RuleDict
lockRules sync.Mutex
invariants []*ast.Expression
workingMemory *ast.WorkingMemory
dataContext ast.IDataContext
lexerParserPool sync.Pool
agent Agent
lockAgent sync.Mutex
logLevel zap.AtomicLevel
logger *zap.Logger
optimistExec bool
optimistInput bool
lockOptimistic sync.Mutex
}
func NewExecuter(
mem memory.ResourceController,
rules []string,
agt Agent,
lc config.LogConfig,
invariants ...string,
) (*Executer, error) {
res := &Executer{
memory: mem.Copy(),
pool: make([]Update, 0),
coordinator: newCoordinator(),
ruleLibrary: make(map[string]ecarule.RuleDict),
invariants: make([]*ast.Expression, 0, len(invariants)),
agent: agt,
}
if res.memory.HasDuplicates() {
return nil, errors.New("multiple resources have the same name")
}
err := validNames(res.memory.ResourceNames())
if err != nil {
return nil, err
}
res.types = res.memory.Types()
res.dataContext, res.workingMemory, err = newEmptyGruleStructures(map[string]memory.Resources{"this": res.memory.GetResources()})
if err != nil {
return nil, err
}
res.lexerParserPool = sync.Pool{
New: func() interface{} {
return parser.New(res.types, res.workingMemory, &res.lockMemory)
},
}
if lc.Encoding == "" {
lc.Encoding = "console"
}
if lc.Encoding != "console" && lc.Encoding != "json" {
return nil, fmt.Errorf("unsupported log encoding: %s", lc.Encoding)
}
zapCfg, ok := config.LogPreset(lc.Encoding).(zap.Config)
if !ok {
return nil, errors.New("invalid logger preset")
}
res.logLevel = zapCfg.Level
res.logger, err = zapCfg.Build()
if err != nil {
return nil, err
}
res.SetLogLevel(lc.Level)
err = res.addInvariants(invariants...)
if err != nil {
return nil, err
}
err = res.AddRules(rules...)
if err != nil {
return nil, err
}
res.updateReceiver = res.startUpdateReceiver()
err = mem.Start()
if err != nil {
return nil, err
}
go res.receiveInputs()
err = res.StartAgent()
if err != nil {
return nil, err
}
return res, nil
}
func (m *Executer) StartAgent() error {
m.lockAgent.Lock()
defer m.lockAgent.Unlock()
err := m.agent.Start()
if err != nil {
return err
}
go m.receiveExternalActions()
err = m.agent.Join()
if err != nil {
return err
}
return nil
}
func (m *Executer) StopAgent() error {
m.lockAgent.Lock()
defer m.lockAgent.Unlock()
return m.agent.Stop()
}
func (m *Executer) SetAgent(agt Agent) error {
m.lockAgent.Lock()
defer m.lockAgent.Unlock()
if m.agent.IsRunning() {
return errors.New("agent is still running")
}
m.agent = agt
return nil
}
func (m *Executer) TakeState() (memory.Resources, []Update) {
m.coordinator.requestWrite(false)
m.lockMemory.RLock()
memCopy := m.memory.Copy().GetResources()
m.lockMemory.RUnlock()
lock := make(chan bool)
m.updateReceiver <- preparedUpdates{confirm: lock}
lock <- false // no updates are added
poolCopy := make([]Update, 0, len(m.pool))
for _, update := range m.pool {
var updateCopy Update = make([]Assignment, len(update))
copy(updateCopy, update)
poolCopy = append(poolCopy, updateCopy)
}
<-lock
m.coordinator.closeWrite()
return memCopy, poolCopy
}
func (m *Executer) DoIfStable(f func()) bool {
m.coordinator.requestWrite(false)
lock := make(chan bool)
m.updateReceiver <- preparedUpdates{confirm: lock}
lock <- false // no updates are added
stable := len(m.pool) == 0
if stable {
f()
}
<-lock
m.coordinator.closeWrite()
return stable
}
func (m *Executer) HasRule(name string) bool {
m.lockRules.Lock()
defer m.lockRules.Unlock()
return m.hasRuleAux(name)
}
// AddRules adds a list of GoAbU rules to the node's knowledge base.
func (m *Executer) AddRules(rules ...string) error {
if len(rules) == 0 {
return nil
}
m.lockRules.Lock()
defer m.lockRules.Unlock()
parser := m.lexerParserPool.Get().(ecarule.Parser)
defer m.lexerParserPool.Put(parser)
parsedRules, errs := parser.Parse(rules...)
if len(errs) > 0 {
for _, err := range errs {
m.logger.Error("error during parsing: "+err.Error(),
zap.String("act", "parse"),
zap.Strings("obj", rules))
}
m.logger.Sync()
return errs[0]
}
if len(parsedRules) == 1 {
return m.addRuleAux(parsedRules[0])
}
return addList(parsedRules, m.addRuleAux)
}
func (m *Executer) Exec() {
m.coordinator.requestWrite(m.HasOptimisticExec())
defer m.coordinator.closeWrite()
m.lockPool.Lock()
if len(m.pool) == 0 {
m.lockPool.Unlock()
return
}
update, index := m.chooseUpdate()
m.lockPool.Unlock()
m.logger.Info(fmt.Sprintf("Exec: %v", update), zap.String("act", "exec"), zapUpdate("update", update))
workingSet := stringset.Make()
for _, action := range update {
workingSet.Insert(action.Resource)
}
m.coordinator.fixWorkingSetWrite(workingSet)
m.lockPool.Lock()
m.removeUpdate(index)
m.lockPool.Unlock()
m.lockMemory.Lock()
var modified stringset.Set
if len(m.invariants) > 0 {
copy := m.memory.Extract(workingSet.Slice())
modified = m.applyUpdate(update, false)
if !m.invariantsOk() {
m.memory.Enclose(copy)
for _, action := range update {
if modified.Has(action.Resource) {
modified.Remove(action.Resource)
m.workingMemory.ResetVariable(action.variable)
}
}
m.lockMemory.Unlock()
m.coordinator.confirmWrite()
m.logger.Info(fmt.Sprintf("Exec-Fail: %v would violate the invariants", update),
zap.String("act", "exec-fail"),
zapUpdate("update", update))
return
}
} else {
modified = m.applyUpdate(update, false)
}
m.signalModified(modified)
m.discovery(modified)
m.logger.Debug("Terminated Exec", zap.String("act", "exec"))
m.logger.Sync()
}
func (m *Executer) Input(actions string) error {
parsed, err := m.parseActions(actions)
if err != nil {
return err
}
workingSet := stringset.Make()
for _, p := range parsed {
workingSet.Insert(p.Resource)
}
m.coordinator.requestWrite(m.HasOptimisticInput())
defer m.coordinator.closeWrite()
m.coordinator.fixWorkingSetWrite(workingSet)
m.lockMemory.RLock()
update, err := evalActions(parsed, m.dataContext, m.workingMemory)
if err != nil {
m.logger.Panic("Error during input actions evaluation: "+err.Error(),
zap.String("act", "eval"),
zap.String("obj", actions))
}
m.lockMemory.RUnlock()
m.logger.Info("Input: "+actions, zap.String("act", "input"), zapUpdate("update", update))
m.lockMemory.Lock()
m.discovery(m.applyUpdate(update, true))
m.logger.Debug("Processed input", zap.String("act", "input"))
m.logger.Sync()
return nil
}
func (m *Executer) LogLevel() int {
switch m.logLevel.Level() {
case zapcore.DebugLevel:
return config.LogDebug
case zapcore.InfoLevel:
return config.LogInfo
case zapcore.WarnLevel:
return config.LogWarning
case zapcore.ErrorLevel:
return config.LogError
case zapcore.DPanicLevel, zapcore.PanicLevel, zapcore.FatalLevel:
return config.LogFatal
}
// should not be reached
return -2
}
func (m *Executer) SetLogLevel(l int) {
m.agent.SetLogLevel(l)
if l < config.LogDebug {
l = config.LogDebug
} else if l > config.LogFatal {
l = config.LogFatal
}
zapLevel := zapcore.InfoLevel
switch l {
case config.LogDebug:
zapLevel = zapcore.DebugLevel
case config.LogWarning:
zapLevel = zapcore.WarnLevel
case config.LogError:
zapLevel = zapcore.ErrorLevel
case config.LogFatal:
zapLevel = zapcore.PanicLevel
}
m.logLevel.SetLevel(zapLevel)
}
func (m *Executer) SetOptimisticExec(b bool) {
m.lockOptimistic.Lock()
m.optimistExec = b
m.lockOptimistic.Unlock()
}
func (m *Executer) SetOptimisticInput(b bool) {
m.lockOptimistic.Lock()
m.optimistInput = b
m.lockOptimistic.Unlock()
}
func (m *Executer) HasOptimisticExec() bool {
m.lockOptimistic.Lock()
defer m.lockOptimistic.Unlock()
return m.optimistExec
}
func (m *Executer) HasOptimisticInput() bool {
m.lockOptimistic.Lock()
defer m.lockOptimistic.Unlock()
return m.optimistInput
}
func (m *Executer) chooseUpdate() (Update, int) {
// TODO: implement other strategies
return m.pool[0], 0
}
func (m *Executer) removeUpdate(index int) {
m.pool = append(m.pool[:index], m.pool[index+1:len(m.pool)]...)
}
func (m *Executer) applyUpdate(update Update, input bool) stringset.Set {
modified := stringset.Make()
for _, action := range update {
variable := action.variable
variable = m.workingMemory.AddVariable(variable)
currentVal, err := variable.Evaluate(m.dataContext, m.workingMemory)
if err != nil {
m.logger.Panic(fmt.Sprintf("Could not evaluate resource %s: %s", action.Resource, err.Error()),
zap.String("act", "eval_var"),
zapUpdate("action", action))
}
if reflect.DeepEqual(currentVal, action.Value) {
m.logger.Debug(fmt.Sprintf("Skipping action %v: resource value would not change", action),
zap.String("act", "assign"),
zapUpdate("action", action))
continue
}
ltype := currentVal.Type()
rtype := action.Value.Type()
if !rtype.AssignableTo(ltype) {
m.logger.DPanic(fmt.Sprintf("Skipping action %v: cannot assign a %v to a %v", action, rtype, ltype),
zap.String("act", "assign"),
zapUpdate("action", action))
} else {
err := variable.Assign(action.Value, m.dataContext, m.workingMemory)
if err != nil {
m.logger.Panic("Could not perform assignment: "+err.Error(),
zap.String("act", "assign"),
zapUpdate("action", action))
}
modified.Insert(action.Resource)
if input {
m.memory.Modified(action.Resource)
m.logger.Debug(fmt.Sprintf("Modified resource \"%s\"", action.Resource),
zap.String("act", "assign"),
zapUpdate("action", action))
}
}
}
return modified
}
func (m *Executer) invariantsOk() bool {
for _, inv := range m.invariants {
val, err := inv.Evaluate(m.dataContext, m.workingMemory)
if err != nil {
m.logger.Panic("Could not evaluate invariant: "+err.Error(),
zap.String("act", "eval_inv"),
zap.String("obj", inv.GetGrlText()))
}
if !val.Bool() {
return false
}
}
return true
}
func (m *Executer) signalModified(modified stringset.Set) {
for r := range modified {
m.memory.Modified(r)
m.logger.Debug(fmt.Sprintf("Modified resource \"%s\"", r),
zap.String("act", "assign"),
zap.Any(r, m.resourceValue(r).Interface()),
)
}
}
func (m *Executer) resourceValue(resource string) reflect.Value {
// TODO: Simplify resource access
return reflect.ValueOf(m.memory.GetResources()).FieldByName(m.types[resource]).MapIndex(reflect.ValueOf(resource))
}
// discovery given a set of modified resource names adds to the pool the updates coming from the
// triggered local rules and adds the updates from the global rules in the pools of the other nodes.
func (m *Executer) discovery(modified stringset.Set) {
updates, wire := m.triggeredActions(modified)
m.lockMemory.Unlock()
ok := make(chan bool)
m.updateReceiver <- preparedUpdates{updates: updates, confirm: ok}
ok <- true
<-ok
m.coordinator.confirmWrite()
m.logger.Info(fmt.Sprintf("Discovery found %d updates", len(updates)),
zap.String("act", "discovery"),
zapUpdates("updates", updates))
if len(wire.Tasks) > 0 {
payload, err := marshalWireTasks(wire)
if err != nil {
m.logger.Panic("Error during external actions marshalling: "+err.Error(),
zap.String("act", "marshalling"),
zap.String("obj", "external actions"))
}
tentatives := 0
for {
err = m.agent.ForAll(payload)
if err == nil {
break
}
tentatives++
if tentatives%10 == 0 {
m.logger.Error(fmt.Sprintf("Failed %d transactions", tentatives),
zap.String("act", "for_all"),
zap.Int("transactions", tentatives))
}
}
}
}
// triggeredActions, given a set of modified resources, calculates the local updates and the partially evaluated tasks
// that are to be sent to the other nodes.
func (m *Executer) triggeredActions(modified stringset.Set) ([]Update, wireTasks) {
var newpool []Update
var wTask wireTasks
rules := m.activeRules(modified)
localResources := stringset.Make()
for _, rule := range rules {
for _, task := range rule.LocalTasks {
tActions, err := condEvalActions(task.Condition, task.Actions, m.dataContext, m.workingMemory)
if err != nil {
m.logger.Panic("Error during actions evaluation: "+err.Error(),
zap.String("act", "eval"),
zap.String("obj", "actions"))
}
newpool = appendNonempty(newpool, tActions)
}
for _, task := range rule.RemoteTasks {
wTask.Tasks = append(wTask.Tasks, task)
localResources.Add(stringset.Make(task.LocalResources...))
}
}
wTask.Resources = m.memory.Extract(localResources.Slice())
return newpool, wTask
}
func (m *Executer) activeRules(modified stringset.Set) ecarule.RuleDict {
res := ecarule.MakeRuleDict()
m.lockRules.Lock()
for resource := range modified {
res.Add(m.ruleLibrary[resource])
}
m.lockRules.Unlock()
return res
}
func (m *Executer) hasRuleAux(name string) bool {
for _, d := range m.ruleLibrary {
if d.Has(name) {
return true
}
}
return false
}
// addRuleAux adds a [ecarule.Rule] to the node's knowledge base.
func (m *Executer) addRuleAux(rule ecarule.Rule) error {
if m.hasRuleAux(rule.Name) {
return fmt.Errorf("there is already a rule named %s", rule.Name)
}
for _, evt := range rule.Events {
if m.ruleLibrary[evt] == nil {
m.ruleLibrary[evt] = ecarule.MakeRuleDict()
}
m.ruleLibrary[evt].Insert(&rule)
}
m.logger.Debug("Introduced new rule", zap.String("act", "add_rule"), zap.String("obj", rule.Name))
return nil
}
func (m *Executer) addActions(actions string) error {
parsed, err := m.parseActions(actions)
if err != nil {
return err
}
update, err := evalActions(parsed, m.dataContext, m.workingMemory)
if err != nil {
return err
}
m.pool = append(m.pool, update)
return nil
}
func (m *Executer) addPool(pl []string) error {
return addList(pl, m.addActions)
}
// addInvariants constructs the node's invariant by forming a logical conjunction from
// a list of local boolean expressions.
func (m *Executer) addInvariants(invs ...string) error {
if len(invs) == 0 {
return nil
}
parser := m.lexerParserPool.Get().(ecarule.Parser)
defer m.lexerParserPool.Put(parser)
exps, errs := parser.ParseExpressions(invs...)
if len(errs) > 0 {
for _, err := range errs {
m.logger.Error("error during parsing: "+err.Error(),
zap.String("act", "parse"),
zap.Strings("obj", invs))
}
m.logger.Sync()
return errs[0]
}
for i, exp := range exps {
val, err := exp.Evaluate(m.dataContext, m.workingMemory)
if err != nil {
m.logger.Error("Could not evaluate invariant: "+err.Error(),
zap.String("act", "eval_inv"),
zap.String("obj", invs[i]))
return err
}
if val.Kind() != reflect.Bool {
m.logger.Error("Invariant with non-boolean type",
zap.String("act", "add_inv"),
zap.String("obj", invs[i]))
return fmt.Errorf("type of invariant #%d is not boolean", i)
}
m.invariants = append(m.invariants, exp)
}
return nil
}
// parseActions parses a series of local actions.
func (m *Executer) parseActions(actions string) ([]ecarule.Action, error) {
parser := m.lexerParserPool.Get().(ecarule.Parser)
defer m.lexerParserPool.Put(parser)
res, errs := parser.ParseActions(actions)
if len(errs) > 0 {
for _, err := range errs {
m.logger.Error("error during parsing: "+err.Error(),
zap.String("act", "parse"),
zap.String("obj", actions))
}
m.logger.Sync()
return nil, errs[0]
}
return res, nil
}
// newEmptyGruleStructures creates a clean working memory and data context containing
// the [memory.Resources] from resources as struct instances referenced by the map keys.
func newEmptyGruleStructures(resources map[string]memory.Resources) (ast.IDataContext, *ast.WorkingMemory, error) {
dataContext := ast.NewDataContext()
kbName := "dummy"
for name, rs := range resources {
rs := rs
kbName += "_" + name
err := dataContext.Add(name, &(rs))
if err != nil {
return dataContext, nil, err
}
}
version := "0.0.0"
knowledgeBase := &ast.KnowledgeBase{
Name: kbName,
Version: version,
RuleEntries: make(map[string]*ast.RuleEntry),
WorkingMemory: ast.NewWorkingMemory(kbName, version),
}
err := dataContext.Add("DEFUNC",
makeBuiltinFunctions(
knowledgeBase,
knowledgeBase.WorkingMemory,
dataContext,
))
if err != nil {
return dataContext, nil, err
}
knowledgeBase.InitializeContext(dataContext)
return dataContext, knowledgeBase.WorkingMemory, nil
}
// validNames verifies if the arguments can be used as valid identifiers in GoAbU rules.
func validNames(names []string) error {
if len(names) == 0 {
return errors.New("no resource specified")
}
results := parser.ValidateIdentifiers(names...)
for i, n := range names {
if !results[i] {
return fmt.Errorf(`invalid resource name: "%s"`, n)
}
}
return nil
}
func addList[T any](objs []T, add func(T) error) error {
var fstErr error
failed := ""
for i, obj := range objs {
err := add(obj)
if err != nil {
failed += strconv.Itoa(i) + ", "
if fstErr == nil {
fstErr = err
}
}
}
if fstErr != nil {
return fmt.Errorf("could not add elements with indexes %s as %s", failed[:len(failed)-2], fstErr.Error())
}
return nil
}