2023-06-18 18:33:17 -06:00
|
|
|
package graphvent
|
2023-04-08 13:58:47 -06:00
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
2023-06-23 10:10:25 -06:00
|
|
|
"reflect"
|
2023-04-08 13:58:47 -06:00
|
|
|
"github.com/google/uuid"
|
2023-06-04 13:18:10 -06:00
|
|
|
"os"
|
|
|
|
"github.com/rs/zerolog"
|
|
|
|
"fmt"
|
2023-06-23 10:10:25 -06:00
|
|
|
badger "github.com/dgraph-io/badger/v3"
|
2023-06-23 20:56:09 -06:00
|
|
|
"encoding/json"
|
2023-04-08 13:58:47 -06:00
|
|
|
)
|
|
|
|
|
2023-06-23 10:10:25 -06:00
|
|
|
type GraphContext struct {
|
|
|
|
DB * badger.DB
|
|
|
|
Log Logger
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewGraphContext(db * badger.DB, log Logger) * GraphContext {
|
|
|
|
return &GraphContext{DB: db, Log: log}
|
|
|
|
}
|
|
|
|
|
|
|
|
// A Logger is passed around to record events happening to components enabled by SetComponents
|
2023-06-04 13:18:10 -06:00
|
|
|
type Logger interface {
|
2023-06-23 10:10:25 -06:00
|
|
|
SetComponents(components []string) error
|
|
|
|
// Log a formatted string
|
2023-06-04 13:18:10 -06:00
|
|
|
Logf(component string, format string, items ... interface{})
|
2023-06-23 10:10:25 -06:00
|
|
|
// Log a map of attributes and a format string
|
2023-06-08 16:41:51 -06:00
|
|
|
Logm(component string, fields map[string]interface{}, format string, items ... interface{})
|
2023-06-04 13:18:10 -06:00
|
|
|
}
|
|
|
|
|
2023-06-23 10:10:25 -06:00
|
|
|
func NewConsoleLogger(components []string) *ConsoleLogger {
|
|
|
|
logger := &ConsoleLogger{
|
|
|
|
loggers: map[string]zerolog.Logger{},
|
|
|
|
components: []string{},
|
|
|
|
}
|
|
|
|
|
|
|
|
logger.SetComponents(components)
|
|
|
|
|
|
|
|
return logger
|
2023-06-04 13:18:10 -06:00
|
|
|
}
|
|
|
|
|
2023-06-23 10:10:25 -06:00
|
|
|
// A ConsoleLogger logs to stdout
|
|
|
|
type ConsoleLogger struct {
|
|
|
|
loggers map[string]zerolog.Logger
|
|
|
|
components_lock sync.Mutex
|
|
|
|
components []string
|
|
|
|
}
|
2023-06-04 13:18:10 -06:00
|
|
|
|
2023-06-23 10:10:25 -06:00
|
|
|
func (logger * ConsoleLogger) SetComponents(components []string) error {
|
|
|
|
logger.components_lock.Lock()
|
|
|
|
defer logger.components_lock.Unlock()
|
2023-06-08 16:41:51 -06:00
|
|
|
|
2023-06-04 13:18:10 -06:00
|
|
|
component_enabled := func (component string) bool {
|
|
|
|
for _, c := range(components) {
|
|
|
|
if c == component {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2023-06-23 10:10:25 -06:00
|
|
|
for c, _ := range(logger.loggers) {
|
2023-06-20 16:35:16 -06:00
|
|
|
if component_enabled(c) == false {
|
2023-06-23 10:10:25 -06:00
|
|
|
delete(logger.loggers, c)
|
2023-06-20 16:35:16 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range(components) {
|
2023-06-23 10:10:25 -06:00
|
|
|
_, exists := logger.loggers[c]
|
2023-06-20 16:35:16 -06:00
|
|
|
if component_enabled(c) == true && exists == false {
|
2023-06-23 10:10:25 -06:00
|
|
|
logger.loggers[c] = zerolog.New(os.Stdout).With().Timestamp().Str("component", c).Logger()
|
2023-06-04 13:18:10 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-06-23 10:10:25 -06:00
|
|
|
func (logger * ConsoleLogger) Logm(component string, fields map[string]interface{}, format string, items ... interface{}) {
|
|
|
|
l, exists := logger.loggers[component]
|
2023-06-08 16:41:51 -06:00
|
|
|
if exists == true {
|
|
|
|
log := l.Log()
|
|
|
|
for key, value := range(fields) {
|
|
|
|
log = log.Str(key, fmt.Sprintf("%+v", value))
|
|
|
|
}
|
|
|
|
log.Msg(fmt.Sprintf(format, items...))
|
2023-06-04 13:18:10 -06:00
|
|
|
}
|
2023-06-08 16:41:51 -06:00
|
|
|
}
|
2023-06-04 13:18:10 -06:00
|
|
|
|
2023-06-23 10:10:25 -06:00
|
|
|
func (logger * ConsoleLogger) Logf(component string, format string, items ... interface{}) {
|
|
|
|
l, exists := logger.loggers[component]
|
2023-06-04 13:18:10 -06:00
|
|
|
if exists == true {
|
|
|
|
l.Log().Msg(fmt.Sprintf(format, items...))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-23 10:10:25 -06:00
|
|
|
type NodeID string
|
|
|
|
// Generate a random id
|
|
|
|
func RandID() NodeID {
|
2023-04-08 13:58:47 -06:00
|
|
|
uuid_str := uuid.New().String()
|
2023-06-23 10:10:25 -06:00
|
|
|
return NodeID(uuid_str)
|
2023-04-08 13:58:47 -06:00
|
|
|
}
|
|
|
|
|
2023-06-23 10:10:25 -06:00
|
|
|
type SignalDirection int
|
|
|
|
const (
|
|
|
|
Up SignalDirection = iota
|
|
|
|
Down
|
|
|
|
Direct
|
|
|
|
)
|
|
|
|
|
|
|
|
// GraphSignals are passed around the event tree/resource DAG and cast by Type()
|
2023-06-01 22:42:47 -06:00
|
|
|
type GraphSignal interface {
|
2023-06-23 10:10:25 -06:00
|
|
|
// How to propogate the signal
|
|
|
|
Direction() SignalDirection
|
|
|
|
Source() NodeID
|
2023-06-01 22:42:47 -06:00
|
|
|
Type() string
|
2023-06-04 17:23:49 -06:00
|
|
|
String() string
|
2023-06-01 22:42:47 -06:00
|
|
|
}
|
|
|
|
|
2023-06-23 10:10:25 -06:00
|
|
|
// BaseSignal is the most basic type of signal, it has no additional data
|
2023-06-01 22:42:47 -06:00
|
|
|
type BaseSignal struct {
|
2023-06-23 10:10:25 -06:00
|
|
|
direction SignalDirection
|
|
|
|
source NodeID
|
2023-06-18 18:11:59 -06:00
|
|
|
_type string
|
2023-06-04 17:23:49 -06:00
|
|
|
}
|
|
|
|
|
2023-06-23 10:10:25 -06:00
|
|
|
func (signal BaseSignal) Direction() SignalDirection {
|
|
|
|
return signal.direction
|
2023-06-01 22:42:47 -06:00
|
|
|
}
|
|
|
|
|
2023-06-23 10:10:25 -06:00
|
|
|
func (signal BaseSignal) Source() NodeID {
|
2023-06-01 22:42:47 -06:00
|
|
|
return signal.source
|
|
|
|
}
|
|
|
|
|
|
|
|
func (signal BaseSignal) Type() string {
|
2023-06-18 18:11:59 -06:00
|
|
|
return signal._type
|
2023-06-01 22:42:47 -06:00
|
|
|
}
|
|
|
|
|
2023-06-18 18:11:59 -06:00
|
|
|
func (signal BaseSignal) String() string {
|
2023-06-23 10:10:25 -06:00
|
|
|
return fmt.Sprintf("{direction: %d, source: %s, type: %s}", signal.direction, signal.source, signal._type)
|
2023-06-03 02:45:16 -06:00
|
|
|
}
|
|
|
|
|
2023-06-23 10:10:25 -06:00
|
|
|
func NewBaseSignal(source GraphNode, _type string, direction SignalDirection) BaseSignal {
|
|
|
|
var source_id NodeID = ""
|
2023-06-18 18:11:59 -06:00
|
|
|
if source != nil {
|
|
|
|
source_id = source.ID()
|
|
|
|
}
|
|
|
|
|
|
|
|
signal := BaseSignal{
|
2023-06-23 10:10:25 -06:00
|
|
|
direction: direction,
|
2023-06-18 18:11:59 -06:00
|
|
|
source: source_id,
|
|
|
|
_type: _type,
|
|
|
|
}
|
|
|
|
return signal
|
2023-06-03 02:45:16 -06:00
|
|
|
}
|
|
|
|
|
2023-06-19 15:03:17 -06:00
|
|
|
func NewDownSignal(source GraphNode, _type string) BaseSignal {
|
2023-06-23 10:10:25 -06:00
|
|
|
return NewBaseSignal(source, _type, Down)
|
2023-06-19 15:03:17 -06:00
|
|
|
}
|
2023-06-18 18:11:59 -06:00
|
|
|
|
2023-06-19 15:03:17 -06:00
|
|
|
func NewSignal(source GraphNode, _type string) BaseSignal {
|
2023-06-23 10:10:25 -06:00
|
|
|
return NewBaseSignal(source, _type, Up)
|
2023-06-22 15:50:42 -06:00
|
|
|
}
|
|
|
|
|
2023-06-23 10:10:25 -06:00
|
|
|
func NewDirectSignal(source GraphNode, _type string) BaseSignal {
|
|
|
|
return NewBaseSignal(source, _type, Direct)
|
2023-06-22 15:50:42 -06:00
|
|
|
}
|
|
|
|
|
2023-06-23 10:10:25 -06:00
|
|
|
func NewAbortSignal(source GraphNode) BaseSignal {
|
|
|
|
return NewBaseSignal(source, "abort", Down)
|
2023-06-22 15:50:42 -06:00
|
|
|
}
|
|
|
|
|
2023-06-23 10:10:25 -06:00
|
|
|
func NewCancelSignal(source GraphNode) BaseSignal {
|
|
|
|
return NewBaseSignal(source, "cancel", Down)
|
2023-06-22 15:50:42 -06:00
|
|
|
}
|
|
|
|
|
2023-06-23 10:10:25 -06:00
|
|
|
type NodeState interface {
|
2023-06-23 20:56:09 -06:00
|
|
|
|
2023-06-22 15:50:42 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// GraphNode is the interface common to both DAG nodes and Event tree nodes
|
2023-06-23 10:10:25 -06:00
|
|
|
// They have a NodeState interface which is saved to the database every update
|
2023-06-22 15:50:42 -06:00
|
|
|
type GraphNode interface {
|
2023-06-23 10:10:25 -06:00
|
|
|
ID() NodeID
|
|
|
|
|
2023-06-22 15:50:42 -06:00
|
|
|
State() NodeState
|
2023-06-23 10:10:25 -06:00
|
|
|
StateLock() *sync.RWMutex
|
|
|
|
|
|
|
|
SetState(new_state NodeState)
|
|
|
|
|
|
|
|
// Signal propagation function for listener channels
|
|
|
|
UpdateListeners(ctx * GraphContext, update GraphSignal)
|
|
|
|
// Signal propagation function for connected nodes(defined in state)
|
|
|
|
PropagateUpdate(ctx * GraphContext, update GraphSignal)
|
|
|
|
|
|
|
|
// Register and unregister a channel to propogate updates to
|
2023-06-01 22:42:47 -06:00
|
|
|
RegisterChannel(listener chan GraphSignal)
|
|
|
|
UnregisterChannel(listener chan GraphSignal)
|
2023-06-23 10:10:25 -06:00
|
|
|
// Get a handle to the nodes internal signal channel
|
2023-06-18 19:18:11 -06:00
|
|
|
SignalChannel() chan GraphSignal
|
2023-04-08 13:58:47 -06:00
|
|
|
}
|
|
|
|
|
2023-06-23 10:10:25 -06:00
|
|
|
// Create a new base node with the given ID
|
|
|
|
func NewNode(ctx * GraphContext, id NodeID, state NodeState) BaseNode {
|
2023-06-03 18:56:14 -06:00
|
|
|
node := BaseNode{
|
|
|
|
id: id,
|
2023-06-04 17:23:49 -06:00
|
|
|
signal: make(chan GraphSignal, 512),
|
2023-06-03 18:56:14 -06:00
|
|
|
listeners: map[chan GraphSignal]chan GraphSignal{},
|
2023-06-23 10:10:25 -06:00
|
|
|
state: state,
|
|
|
|
}
|
|
|
|
|
|
|
|
err := WriteDBState(ctx, id, state)
|
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Sprintf("DB_NEW_WRITE_ERROR: %s", err))
|
2023-06-03 18:56:14 -06:00
|
|
|
}
|
2023-06-23 10:10:25 -06:00
|
|
|
|
|
|
|
ctx.Log.Logf("graph", "NEW_NODE: %s - %+v", id, state)
|
2023-06-03 18:56:14 -06:00
|
|
|
return node
|
|
|
|
}
|
|
|
|
|
2023-06-23 10:10:25 -06:00
|
|
|
// BaseNode is the minimum set of fields needed to implement a GraphNode,
|
|
|
|
// and provides a template for more complicated Nodes
|
2023-04-08 13:58:47 -06:00
|
|
|
type BaseNode struct {
|
2023-06-23 10:10:25 -06:00
|
|
|
id NodeID
|
|
|
|
|
2023-06-22 15:50:42 -06:00
|
|
|
state NodeState
|
|
|
|
state_lock sync.RWMutex
|
2023-06-23 10:10:25 -06:00
|
|
|
|
2023-06-01 22:42:47 -06:00
|
|
|
signal chan GraphSignal
|
2023-06-23 10:10:25 -06:00
|
|
|
|
2023-04-08 13:58:47 -06:00
|
|
|
listeners_lock sync.Mutex
|
2023-06-01 22:42:47 -06:00
|
|
|
listeners map[chan GraphSignal]chan GraphSignal
|
2023-04-08 13:58:47 -06:00
|
|
|
}
|
|
|
|
|
2023-06-23 10:10:25 -06:00
|
|
|
func (node * BaseNode) ID() NodeID {
|
|
|
|
return node.id
|
2023-06-18 19:16:11 -06:00
|
|
|
}
|
|
|
|
|
2023-06-22 15:50:42 -06:00
|
|
|
func (node * BaseNode) State() NodeState {
|
|
|
|
return node.state
|
2023-04-08 13:58:47 -06:00
|
|
|
}
|
|
|
|
|
2023-06-23 10:10:25 -06:00
|
|
|
func (node * BaseNode) StateLock() * sync.RWMutex {
|
|
|
|
return &node.state_lock
|
2023-04-08 13:58:47 -06:00
|
|
|
}
|
|
|
|
|
2023-06-23 10:10:25 -06:00
|
|
|
func WriteDBState(ctx * GraphContext, id NodeID, state NodeState) error {
|
|
|
|
ctx.Log.Logf("db", "DB_WRITE: %s - %+v", id, state)
|
|
|
|
|
|
|
|
var serialized_state []byte = nil
|
|
|
|
if state != nil {
|
2023-06-23 20:56:09 -06:00
|
|
|
ser, err := json.Marshal(state)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("DB_MARSHAL_ERROR: %e", err)
|
|
|
|
}
|
|
|
|
serialized_state = ser
|
2023-06-23 10:10:25 -06:00
|
|
|
} else {
|
|
|
|
serialized_state = []byte{}
|
2023-04-08 13:58:47 -06:00
|
|
|
}
|
2023-06-23 10:10:25 -06:00
|
|
|
|
|
|
|
err := ctx.DB.Update(func(txn *badger.Txn) error {
|
|
|
|
err := txn.Set([]byte(id), serialized_state)
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
|
|
|
|
return err
|
2023-06-01 22:42:47 -06:00
|
|
|
}
|
2023-04-08 13:58:47 -06:00
|
|
|
|
2023-06-23 10:10:25 -06:00
|
|
|
func (node * BaseNode) SetState(new_state NodeState) {
|
|
|
|
node.state = new_state
|
|
|
|
}
|
|
|
|
|
2023-06-23 20:56:09 -06:00
|
|
|
// How to prevent the states from being modified if they're pointer receivers?
|
2023-06-23 10:10:25 -06:00
|
|
|
func UseStates(ctx * GraphContext, nodes []GraphNode, states_fn func(states []NodeState)(interface{}, error)) (interface{}, error) {
|
|
|
|
for _, node := range(nodes) {
|
|
|
|
node.StateLock().RLock()
|
2023-04-08 13:58:47 -06:00
|
|
|
}
|
2023-06-23 10:10:25 -06:00
|
|
|
|
|
|
|
states := make([]NodeState, len(nodes))
|
|
|
|
for i, node := range(nodes) {
|
|
|
|
states[i] = node.State()
|
|
|
|
}
|
|
|
|
|
|
|
|
val, err := states_fn(states)
|
|
|
|
|
|
|
|
for _, node := range(nodes) {
|
|
|
|
node.StateLock().RUnlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
return val, err
|
2023-06-01 22:42:47 -06:00
|
|
|
}
|
2023-04-08 13:58:47 -06:00
|
|
|
|
2023-06-23 10:10:25 -06:00
|
|
|
func UpdateStates(ctx * GraphContext, nodes []GraphNode, states_fn func(states []NodeState)([]NodeState, interface{}, error)) (interface{}, error) {
|
|
|
|
for _, node := range(nodes) {
|
|
|
|
node.StateLock().Lock()
|
|
|
|
}
|
|
|
|
|
|
|
|
states := make([]NodeState, len(nodes))
|
|
|
|
for i, node := range(nodes) {
|
|
|
|
states[i] = node.State()
|
|
|
|
}
|
|
|
|
|
|
|
|
new_states, val, err := states_fn(states)
|
|
|
|
|
|
|
|
if new_states != nil {
|
|
|
|
if len(new_states) != len(nodes) {
|
|
|
|
panic(fmt.Sprintf("NODE_NEW_STATE_LEN_MISMATCH: %d/%d", len(new_states), len(nodes)))
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, new_state := range(new_states) {
|
|
|
|
if new_state != nil {
|
|
|
|
old_state_type := reflect.TypeOf(states[i])
|
|
|
|
new_state_type := reflect.TypeOf(new_state)
|
|
|
|
|
|
|
|
if old_state_type != new_state_type {
|
|
|
|
panic(fmt.Sprintf("NODE_STATE_MISMATCH: old - %+v, new - %+v", old_state_type, new_state_type))
|
|
|
|
}
|
|
|
|
|
|
|
|
err := WriteDBState(ctx, nodes[i].ID(), new_state)
|
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Sprintf("DB_WRITE_ERROR: %s", err))
|
|
|
|
}
|
|
|
|
|
|
|
|
nodes[i].SetState(new_state)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, node := range(nodes) {
|
|
|
|
node.StateLock().Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
return val, err
|
2023-06-22 15:50:42 -06:00
|
|
|
}
|
2023-06-01 22:42:47 -06:00
|
|
|
|
2023-06-23 10:10:25 -06:00
|
|
|
func (node * BaseNode) UpdateListeners(ctx * GraphContext, update GraphSignal) {
|
|
|
|
node.listeners_lock.Lock()
|
|
|
|
defer node.listeners_lock.Unlock()
|
|
|
|
closed := []chan GraphSignal{}
|
2023-06-02 17:31:29 -06:00
|
|
|
|
2023-06-23 10:10:25 -06:00
|
|
|
for _, listener := range node.listeners {
|
|
|
|
ctx.Log.Logf("listeners", "UPDATE_LISTENER %s: %p", node.ID(), listener)
|
2023-06-02 17:31:29 -06:00
|
|
|
select {
|
2023-06-23 10:10:25 -06:00
|
|
|
case listener <- update:
|
2023-06-02 17:31:29 -06:00
|
|
|
default:
|
2023-06-23 10:10:25 -06:00
|
|
|
ctx.Log.Logf("listeners", "CLOSED_LISTENER %s: %p", node.ID(), listener)
|
2023-06-03 21:27:20 -06:00
|
|
|
go func(node GraphNode, listener chan GraphSignal) {
|
|
|
|
listener <- NewSignal(node, "listener_closed")
|
|
|
|
close(listener)
|
|
|
|
}(node, listener)
|
2023-06-02 17:31:29 -06:00
|
|
|
closed = append(closed, listener)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, listener := range(closed) {
|
|
|
|
delete(node.listeners, listener)
|
2023-06-01 22:42:47 -06:00
|
|
|
}
|
2023-06-02 17:31:29 -06:00
|
|
|
}
|
|
|
|
|
2023-06-23 10:10:25 -06:00
|
|
|
func (node * BaseNode) PropagateUpdate(ctx * GraphContext, update GraphSignal) {
|
|
|
|
}
|
2023-06-22 15:50:42 -06:00
|
|
|
|
2023-06-23 10:10:25 -06:00
|
|
|
func (node * BaseNode) RegisterChannel(listener chan GraphSignal) {
|
|
|
|
node.listeners_lock.Lock()
|
|
|
|
_, exists := node.listeners[listener]
|
|
|
|
if exists == false {
|
|
|
|
node.listeners[listener] = listener
|
2023-06-22 15:50:42 -06:00
|
|
|
}
|
2023-06-23 10:10:25 -06:00
|
|
|
node.listeners_lock.Unlock()
|
|
|
|
}
|
2023-06-22 15:50:42 -06:00
|
|
|
|
2023-06-23 10:10:25 -06:00
|
|
|
func (node * BaseNode) UnregisterChannel(listener chan GraphSignal) {
|
|
|
|
node.listeners_lock.Lock()
|
|
|
|
_, exists := node.listeners[listener]
|
|
|
|
if exists == false {
|
|
|
|
panic("Attempting to unregister non-registered listener")
|
|
|
|
} else {
|
2023-06-22 15:50:42 -06:00
|
|
|
delete(node.listeners, listener)
|
|
|
|
}
|
2023-06-23 10:10:25 -06:00
|
|
|
node.listeners_lock.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (node * BaseNode) SignalChannel() chan GraphSignal {
|
|
|
|
return node.signal
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a new GraphSinal channel with a buffer of size buffer and register it to a node
|
|
|
|
func GetUpdateChannel(node * BaseNode, buffer int) chan GraphSignal {
|
|
|
|
new_listener := make(chan GraphSignal, buffer)
|
|
|
|
node.RegisterChannel(new_listener)
|
|
|
|
return new_listener
|
|
|
|
}
|
|
|
|
|
|
|
|
// Propogate a signal starting at a node
|
|
|
|
func SendUpdate(ctx * GraphContext, node GraphNode, signal GraphSignal) {
|
|
|
|
if node == nil {
|
|
|
|
panic("Cannot start an update from no node")
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.Log.Logf("update", "UPDATE %s <- %s: %+v", node.ID(), signal.Source(), signal)
|
2023-06-22 15:50:42 -06:00
|
|
|
|
2023-06-23 10:10:25 -06:00
|
|
|
node.UpdateListeners(ctx, signal)
|
|
|
|
node.PropagateUpdate(ctx, signal)
|
2023-04-08 13:58:47 -06:00
|
|
|
}
|
2023-06-02 17:31:29 -06:00
|
|
|
|