2023-06-18 18:33:17 -06:00
|
|
|
package graphvent
|
2023-04-08 13:58:47 -06:00
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
2023-07-01 13:03:28 -06:00
|
|
|
"reflect"
|
2023-04-08 13:58:47 -06:00
|
|
|
"github.com/google/uuid"
|
2023-07-01 13:03:28 -06:00
|
|
|
"github.com/graphql-go/graphql"
|
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-07-01 13:03:28 -06:00
|
|
|
// For persistance, each node needs the following functions(* is a placeholder for the node/state type):
|
|
|
|
// Load*State - StateLoadFunc that returns the NodeState interface to attach to the node
|
|
|
|
// Load* - NodeLoadFunc that returns the GraphNode restored from it's loaded state
|
|
|
|
|
|
|
|
// For convenience, the following functions are a good idea to define for composability:
|
|
|
|
// Restore*State - takes in the nodes serialized data to allow for easier nesting of inherited Load*State functions
|
|
|
|
// Save*State - serialize the node into it's json counterpart to be included as part of a larger json
|
|
|
|
|
2023-06-30 13:25:35 -06:00
|
|
|
type StateLoadFunc func(*GraphContext, []byte, NodeMap)(NodeState, error)
|
2023-06-28 21:49:23 -06:00
|
|
|
type StateLoadMap map[string]StateLoadFunc
|
|
|
|
type NodeLoadFunc func(*GraphContext, NodeID)(GraphNode, error)
|
|
|
|
type NodeLoadMap map[string]NodeLoadFunc
|
2023-07-03 13:14:48 -06:00
|
|
|
type InfoLoadFunc func(*GraphContext, map[string]interface{})(ThreadInfo, error)
|
|
|
|
type InfoLoadMap map[string]InfoLoadFunc
|
2023-06-23 10:10:25 -06:00
|
|
|
type GraphContext struct {
|
|
|
|
DB * badger.DB
|
|
|
|
Log Logger
|
2023-06-28 21:49:23 -06:00
|
|
|
NodeLoadFuncs NodeLoadMap
|
|
|
|
StateLoadFuncs StateLoadMap
|
2023-07-03 13:14:48 -06:00
|
|
|
InfoLoadFuncs InfoLoadMap
|
2023-07-01 13:03:28 -06:00
|
|
|
GQL * GQLContext
|
|
|
|
}
|
|
|
|
|
|
|
|
type GQLContext struct {
|
|
|
|
Schema graphql.Schema
|
|
|
|
ValidNodes ObjTypeMap
|
|
|
|
NodeType reflect.Type
|
|
|
|
ValidLockables ObjTypeMap
|
|
|
|
LockableType reflect.Type
|
|
|
|
ValidThreads ObjTypeMap
|
|
|
|
ThreadType reflect.Type
|
|
|
|
}
|
|
|
|
|
2023-07-01 13:47:12 -06:00
|
|
|
func NewGQLContext(additional_types TypeList, extended_types ObjTypeMap, extended_queries FieldMap, extended_subscriptions FieldMap, extended_mutations FieldMap) (*GQLContext, error) {
|
2023-07-01 13:03:28 -06:00
|
|
|
type_list := TypeList{
|
|
|
|
GQLTypeSignalInput(),
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, gql_type := range(additional_types) {
|
|
|
|
type_list = append(type_list, gql_type)
|
|
|
|
}
|
|
|
|
|
|
|
|
type_map := ObjTypeMap{}
|
|
|
|
type_map[reflect.TypeOf((*BaseLockable)(nil))] = GQLTypeBaseLockable()
|
|
|
|
type_map[reflect.TypeOf((*BaseThread)(nil))] = GQLTypeBaseThread()
|
|
|
|
type_map[reflect.TypeOf((*GQLThread)(nil))] = GQLTypeGQLThread()
|
|
|
|
type_map[reflect.TypeOf((*BaseSignal)(nil))] = GQLTypeSignal()
|
|
|
|
|
|
|
|
for go_t, gql_t := range(extended_types) {
|
|
|
|
type_map[go_t] = gql_t
|
|
|
|
}
|
|
|
|
|
|
|
|
valid_nodes := ObjTypeMap{}
|
|
|
|
valid_lockables := ObjTypeMap{}
|
|
|
|
valid_threads := ObjTypeMap{}
|
|
|
|
|
|
|
|
node_type := reflect.TypeOf((*GraphNode)(nil)).Elem()
|
|
|
|
lockable_type := reflect.TypeOf((*Lockable)(nil)).Elem()
|
|
|
|
thread_type := reflect.TypeOf((*Thread)(nil)).Elem()
|
|
|
|
|
|
|
|
for go_t, gql_t := range(type_map) {
|
|
|
|
if go_t.Implements(node_type) {
|
|
|
|
valid_nodes[go_t] = gql_t
|
|
|
|
}
|
|
|
|
if go_t.Implements(lockable_type) {
|
|
|
|
valid_lockables[go_t] = gql_t
|
|
|
|
}
|
|
|
|
if go_t.Implements(thread_type) {
|
|
|
|
valid_threads[go_t] = gql_t
|
|
|
|
}
|
|
|
|
type_list = append(type_list, gql_t)
|
|
|
|
}
|
|
|
|
|
|
|
|
queries := graphql.Fields{
|
|
|
|
"Self": GQLQuerySelf(),
|
|
|
|
}
|
|
|
|
|
|
|
|
for key, val := range(extended_queries) {
|
|
|
|
queries[key] = val
|
|
|
|
}
|
|
|
|
|
|
|
|
subscriptions := graphql.Fields{
|
|
|
|
"Update": GQLSubscriptionUpdate(),
|
2023-07-03 18:58:15 -06:00
|
|
|
"Self": GQLSubscriptionSelf(),
|
2023-07-01 13:03:28 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
for key, val := range(extended_subscriptions) {
|
|
|
|
subscriptions[key] = val
|
|
|
|
}
|
|
|
|
|
2023-07-01 13:47:12 -06:00
|
|
|
mutations := graphql.Fields{
|
|
|
|
"SendUpdate": GQLMutationSendUpdate(),
|
|
|
|
}
|
|
|
|
|
|
|
|
for key, val := range(extended_mutations) {
|
|
|
|
mutations[key] = val
|
|
|
|
}
|
|
|
|
|
2023-07-01 13:03:28 -06:00
|
|
|
schemaConfig := graphql.SchemaConfig{
|
|
|
|
Types: type_list,
|
|
|
|
Query: graphql.NewObject(graphql.ObjectConfig{
|
|
|
|
Name: "Query",
|
|
|
|
Fields: queries,
|
|
|
|
}),
|
|
|
|
Mutation: graphql.NewObject(graphql.ObjectConfig{
|
|
|
|
Name: "Mutation",
|
|
|
|
Fields: mutations,
|
|
|
|
}),
|
|
|
|
Subscription: graphql.NewObject(graphql.ObjectConfig{
|
|
|
|
Name: "Subscription",
|
|
|
|
Fields: subscriptions,
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
|
|
|
|
schema, err := graphql.NewSchema(schemaConfig)
|
|
|
|
if err != nil{
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx := GQLContext{
|
|
|
|
Schema: schema,
|
|
|
|
ValidNodes: valid_nodes,
|
|
|
|
NodeType: node_type,
|
|
|
|
ValidThreads: valid_threads,
|
|
|
|
ThreadType: thread_type,
|
|
|
|
ValidLockables: valid_lockables,
|
|
|
|
LockableType: lockable_type,
|
|
|
|
}
|
|
|
|
|
|
|
|
return &ctx, nil
|
2023-06-23 10:10:25 -06:00
|
|
|
}
|
|
|
|
|
2023-06-30 13:25:35 -06:00
|
|
|
func LoadNode(ctx * GraphContext, id NodeID) (GraphNode, error) {
|
|
|
|
// Initialize an empty list of loaded nodes, then start loading them from id
|
|
|
|
loaded_nodes := map[NodeID]GraphNode{}
|
|
|
|
return LoadNodeRecurse(ctx, id, loaded_nodes)
|
|
|
|
}
|
|
|
|
|
|
|
|
type DBJSONBase struct {
|
|
|
|
Type string `json:"type"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if a node is already loaded, load it's state bytes from the DB and parse the type if it's not already loaded
|
|
|
|
// Call the node load function related to the type, which will call this parse function recusively as needed
|
|
|
|
func LoadNodeRecurse(ctx * GraphContext, id NodeID, loaded_nodes map[NodeID]GraphNode) (GraphNode, error) {
|
|
|
|
node, exists := loaded_nodes[id]
|
|
|
|
if exists == false {
|
|
|
|
state_bytes, err := ReadDBState(ctx, id)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var base DBJSONBase
|
|
|
|
err = json.Unmarshal(state_bytes, &base)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.Log.Logf("graph", "GRAPH_DB_LOAD: %s(%s)", base.Type, id)
|
|
|
|
|
|
|
|
node_fn, exists := ctx.NodeLoadFuncs[base.Type]
|
|
|
|
if exists == false {
|
2023-07-01 13:03:28 -06:00
|
|
|
return nil, fmt.Errorf("%s is not a known node type: %s", base.Type, state_bytes)
|
2023-06-30 13:25:35 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
node, err = node_fn(ctx, id)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
loaded_nodes[id] = node
|
|
|
|
|
|
|
|
state_fn, exists := ctx.StateLoadFuncs[base.Type]
|
|
|
|
if exists == false {
|
|
|
|
return nil, fmt.Errorf("%s is not a known node state type", base.Type)
|
|
|
|
}
|
|
|
|
|
|
|
|
state, err := state_fn(ctx, state_bytes, loaded_nodes)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
node.SetState(state)
|
|
|
|
}
|
|
|
|
return node, nil
|
|
|
|
}
|
|
|
|
|
2023-07-03 13:14:48 -06:00
|
|
|
func NewGraphContext(db * badger.DB, log Logger, state_loads StateLoadMap, node_loads NodeLoadMap, info_loads InfoLoadMap, types TypeList, type_map ObjTypeMap, queries FieldMap, subscriptions FieldMap, mutations FieldMap) * GraphContext {
|
2023-07-01 13:41:38 -06:00
|
|
|
gql, err := NewGQLContext(types, type_map, queries, subscriptions, mutations)
|
2023-07-01 13:03:28 -06:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2023-06-28 21:49:23 -06:00
|
|
|
ctx := GraphContext{
|
2023-07-01 13:03:28 -06:00
|
|
|
GQL: gql,
|
2023-06-28 21:49:23 -06:00
|
|
|
DB: db,
|
|
|
|
Log: log,
|
|
|
|
NodeLoadFuncs: NodeLoadMap{
|
2023-07-02 12:14:04 -06:00
|
|
|
"simple_lockable": LoadSimpleLockable,
|
|
|
|
"simple_thread": LoadSimpleThread,
|
2023-07-01 13:03:28 -06:00
|
|
|
"gql_thread": LoadGQLThread,
|
2023-06-28 21:49:23 -06:00
|
|
|
},
|
|
|
|
StateLoadFuncs: StateLoadMap{
|
2023-07-02 12:14:04 -06:00
|
|
|
"simple_lockable": LoadSimpleLockableState,
|
|
|
|
"simple_thread": LoadSimpleThreadState,
|
2023-07-01 13:03:28 -06:00
|
|
|
"gql_thread": LoadGQLThreadState,
|
2023-06-28 21:49:23 -06:00
|
|
|
},
|
2023-07-03 13:14:48 -06:00
|
|
|
InfoLoadFuncs: InfoLoadMap{
|
|
|
|
"gql_thread": LoadGQLThreadInfo,
|
|
|
|
},
|
2023-06-28 21:49:23 -06:00
|
|
|
}
|
|
|
|
|
2023-07-02 09:34:36 -06:00
|
|
|
for name, fn := range(state_loads) {
|
|
|
|
ctx.StateLoadFuncs[name] = fn
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, fn := range(node_loads) {
|
|
|
|
ctx.NodeLoadFuncs[name] = fn
|
|
|
|
}
|
|
|
|
|
2023-07-03 13:14:48 -06:00
|
|
|
for name, fn := range(info_loads) {
|
|
|
|
ctx.InfoLoadFuncs[name] = fn
|
|
|
|
}
|
|
|
|
|
2023-06-28 21:49:23 -06:00
|
|
|
return &ctx
|
2023-06-23 10:10:25 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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-26 22:39:24 -06:00
|
|
|
// Log a structure to a file by marshalling and unmarshalling the json
|
|
|
|
Logj(component string, s 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-26 22:39:24 -06:00
|
|
|
func (logger * ConsoleLogger) Logj(component string, s interface{}, format string, items ... interface{}) {
|
|
|
|
m := map[string]interface{}{}
|
|
|
|
ser, err := json.Marshal(s)
|
|
|
|
if err != nil {
|
|
|
|
panic("LOG_MARSHAL_ERR")
|
|
|
|
}
|
|
|
|
err = json.Unmarshal(ser, &m)
|
|
|
|
if err != nil {
|
|
|
|
panic("LOG_UNMARSHAL_ERR")
|
|
|
|
}
|
|
|
|
logger.Logm(component, m, 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-26 22:30:02 -06:00
|
|
|
FDirection SignalDirection `json:"direction"`
|
|
|
|
FSource NodeID `json:"source"`
|
|
|
|
FType string `json:"type"`
|
2023-06-26 22:06:09 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (state BaseSignal) String() string {
|
|
|
|
ser, err := json.Marshal(state)
|
|
|
|
if err != nil {
|
|
|
|
return "STATE_SER_ERR"
|
|
|
|
}
|
|
|
|
return string(ser)
|
2023-06-04 17:23:49 -06:00
|
|
|
}
|
|
|
|
|
2023-06-23 10:10:25 -06:00
|
|
|
func (signal BaseSignal) Direction() SignalDirection {
|
2023-06-26 22:30:02 -06:00
|
|
|
return signal.FDirection
|
2023-06-01 22:42:47 -06:00
|
|
|
}
|
|
|
|
|
2023-06-23 10:10:25 -06:00
|
|
|
func (signal BaseSignal) Source() NodeID {
|
2023-06-26 22:30:02 -06:00
|
|
|
return signal.FSource
|
2023-06-01 22:42:47 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (signal BaseSignal) Type() string {
|
2023-06-26 22:30:02 -06:00
|
|
|
return signal.FType
|
2023-06-01 22:42:47 -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-26 22:30:02 -06:00
|
|
|
FDirection: direction,
|
|
|
|
FSource: source_id,
|
|
|
|
FType: _type,
|
2023-06-18 18:11:59 -06:00
|
|
|
}
|
|
|
|
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 21:21:14 -06:00
|
|
|
func AbortSignal(source GraphNode) BaseSignal {
|
2023-06-23 10:10:25 -06:00
|
|
|
return NewBaseSignal(source, "abort", Down)
|
2023-06-22 15:50:42 -06:00
|
|
|
}
|
|
|
|
|
2023-06-23 21:21:14 -06:00
|
|
|
func CancelSignal(source GraphNode) BaseSignal {
|
2023-06-23 10:10:25 -06:00
|
|
|
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-28 21:49:23 -06:00
|
|
|
// Human-readable name of the node, not guaranteed to be unique
|
2023-06-25 20:20:59 -06:00
|
|
|
Name() string
|
2023-06-28 21:49:23 -06:00
|
|
|
// Type of the node this state is attached to. Used to deserialize the state to a node from the database
|
|
|
|
Type() string
|
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)
|
2023-07-04 18:45:23 -06:00
|
|
|
PropagateUpdate(ctx * GraphContext, update GraphSignal, states NodeStateMap)
|
2023-06-23 10:10:25 -06:00
|
|
|
|
2023-06-24 19:48:59 -06:00
|
|
|
// Get an update channel for the node to be notified of signals
|
|
|
|
UpdateChannel(buffer int) chan GraphSignal
|
|
|
|
|
2023-06-23 10:10:25 -06:00
|
|
|
// 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-28 21:49:23 -06:00
|
|
|
const NODE_SIGNAL_BUFFER = 256
|
|
|
|
|
|
|
|
func RestoreNode(ctx * GraphContext, id NodeID) BaseNode {
|
|
|
|
node := BaseNode{
|
|
|
|
id: id,
|
|
|
|
signal: make(chan GraphSignal, NODE_SIGNAL_BUFFER),
|
|
|
|
listeners: map[chan GraphSignal]chan GraphSignal{},
|
2023-07-05 14:50:21 -06:00
|
|
|
listeners_lock: &sync.Mutex{},
|
2023-06-28 21:49:23 -06:00
|
|
|
state: nil,
|
2023-07-05 14:50:21 -06:00
|
|
|
state_lock: &sync.RWMutex{},
|
2023-06-28 21:49:23 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
ctx.Log.Logf("graph", "RESTORE_NODE: %s", node.id)
|
|
|
|
return node
|
|
|
|
}
|
|
|
|
|
2023-07-03 13:14:48 -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 {
|
|
|
|
ser, err := json.Marshal(state)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("DB_MARSHAL_ERROR: %e", err)
|
|
|
|
}
|
|
|
|
serialized_state = ser
|
|
|
|
} else {
|
|
|
|
serialized_state = []byte{}
|
|
|
|
}
|
|
|
|
|
|
|
|
err := ctx.DB.Update(func(txn *badger.Txn) error {
|
|
|
|
err := txn.Set([]byte(id), serialized_state)
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-06-26 01:16:44 -06:00
|
|
|
// Create a new base node with a new ID
|
|
|
|
func NewNode(ctx * GraphContext, state NodeState) (BaseNode, error) {
|
2023-06-03 18:56:14 -06:00
|
|
|
node := BaseNode{
|
2023-06-26 01:16:44 -06:00
|
|
|
id: RandID(),
|
2023-06-28 21:49:23 -06:00
|
|
|
signal: make(chan GraphSignal, NODE_SIGNAL_BUFFER),
|
2023-06-03 18:56:14 -06:00
|
|
|
listeners: map[chan GraphSignal]chan GraphSignal{},
|
2023-07-05 14:50:21 -06:00
|
|
|
listeners_lock: &sync.Mutex{},
|
2023-06-23 10:10:25 -06:00
|
|
|
state: state,
|
2023-07-05 14:50:21 -06:00
|
|
|
state_lock: &sync.RWMutex{},
|
2023-06-23 10:10:25 -06:00
|
|
|
}
|
|
|
|
|
2023-06-26 01:16:44 -06:00
|
|
|
err := WriteDBState(ctx, node.id, state)
|
2023-06-23 10:10:25 -06:00
|
|
|
if err != nil {
|
2023-06-26 01:16:44 -06:00
|
|
|
return node, fmt.Errorf("DB_NEW_WRITE_ERROR: %e", err)
|
2023-06-03 18:56:14 -06:00
|
|
|
}
|
2023-06-23 10:10:25 -06:00
|
|
|
|
2023-06-26 01:16:44 -06:00
|
|
|
ctx.Log.Logf("graph", "NEW_NODE: %s - %+v", node.id, state)
|
|
|
|
return node, nil
|
2023-06-03 18:56:14 -06:00
|
|
|
}
|
|
|
|
|
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
|
2023-07-05 14:50:21 -06:00
|
|
|
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-07-05 14:50:21 -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 {
|
2023-07-05 14:50:21 -06:00
|
|
|
return node.state_lock
|
2023-04-08 13:58:47 -06:00
|
|
|
}
|
|
|
|
|
2023-06-28 21:49:23 -06:00
|
|
|
func ReadDBState(ctx * GraphContext, id NodeID) ([]byte, error) {
|
|
|
|
var bytes []byte
|
|
|
|
err := ctx.DB.View(func(txn *badger.Txn) error {
|
|
|
|
item, err := txn.Get([]byte(id))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return item.Value(func(val []byte) error {
|
|
|
|
bytes = append([]byte{}, val...)
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
ctx.Log.Logf("db", "DB_READ_ERR: %s - %e", id, err)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.Log.Logf("db", "DB_READ: %s - %s", id, string(bytes))
|
|
|
|
|
|
|
|
return bytes, nil
|
|
|
|
}
|
|
|
|
|
2023-06-28 23:51:44 -06:00
|
|
|
func WriteDBStates(ctx * GraphContext, nodes NodeMap) error{
|
|
|
|
ctx.Log.Logf("db", "DB_WRITES: %d", len(nodes))
|
2023-06-30 13:25:35 -06:00
|
|
|
serialized_states := map[NodeID][]byte{}
|
2023-06-28 23:51:44 -06:00
|
|
|
for _, node := range(nodes) {
|
|
|
|
ser, err := json.Marshal(node.State())
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("DB_MARSHAL_ERROR: %e", err)
|
|
|
|
}
|
2023-06-30 13:25:35 -06:00
|
|
|
serialized_states[node.ID()] = ser
|
2023-06-28 23:51:44 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
err := ctx.DB.Update(func(txn *badger.Txn) error {
|
|
|
|
i := 0
|
|
|
|
for id, _ := range(nodes) {
|
2023-06-30 13:25:35 -06:00
|
|
|
ctx.Log.Logf("db", "DB_WRITE: %s - %s", id, string(serialized_states[id]))
|
|
|
|
err := txn.Set([]byte(id), serialized_states[id])
|
2023-06-28 23:51:44 -06:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("DB_MARSHAL_ERROR: %e", err)
|
|
|
|
}
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-06-23 10:10:25 -06:00
|
|
|
func (node * BaseNode) SetState(new_state NodeState) {
|
|
|
|
node.state = new_state
|
|
|
|
}
|
|
|
|
|
2023-06-25 13:39:00 -06:00
|
|
|
func checkForDuplicate(nodes []GraphNode) error {
|
|
|
|
found := map[NodeID]bool{}
|
|
|
|
for _, node := range(nodes) {
|
|
|
|
if node == nil {
|
|
|
|
return fmt.Errorf("Cannot get state of nil node")
|
|
|
|
}
|
|
|
|
|
|
|
|
_, exists := found[node.ID()]
|
|
|
|
if exists == true {
|
|
|
|
return fmt.Errorf("Attempted to get state of %s twice", node.ID())
|
|
|
|
}
|
|
|
|
found[node.ID()] = true
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-07-04 18:45:23 -06:00
|
|
|
func NodeList[K GraphNode](list []K) []GraphNode {
|
|
|
|
nodes := make([]GraphNode, len(list))
|
|
|
|
for i, node := range(list) {
|
|
|
|
nodes[i] = node
|
|
|
|
}
|
|
|
|
return nodes
|
|
|
|
}
|
|
|
|
|
2023-06-28 21:49:23 -06:00
|
|
|
type NodeStateMap map[NodeID]NodeState
|
2023-06-28 23:51:44 -06:00
|
|
|
type NodeMap map[NodeID]GraphNode
|
|
|
|
type StatesFn func(states NodeStateMap) error
|
|
|
|
type NodesFn func(nodes NodeMap) error
|
2023-06-28 21:49:23 -06:00
|
|
|
func UseStates(ctx * GraphContext, nodes []GraphNode, states_fn StatesFn) error {
|
|
|
|
states := NodeStateMap{}
|
|
|
|
return UseMoreStates(ctx, nodes, states, states_fn)
|
|
|
|
}
|
|
|
|
func UseMoreStates(ctx * GraphContext, nodes []GraphNode, states NodeStateMap, states_fn StatesFn) error {
|
2023-06-25 13:39:00 -06:00
|
|
|
err := checkForDuplicate(nodes)
|
|
|
|
if err != nil {
|
2023-06-28 00:48:49 -06:00
|
|
|
return err
|
2023-06-25 13:39:00 -06:00
|
|
|
}
|
|
|
|
|
2023-06-28 21:49:23 -06:00
|
|
|
locked_nodes := []GraphNode{}
|
2023-06-23 10:10:25 -06:00
|
|
|
for _, node := range(nodes) {
|
2023-06-28 21:49:23 -06:00
|
|
|
_, locked := states[node.ID()]
|
|
|
|
if locked == false {
|
|
|
|
node.StateLock().RLock()
|
|
|
|
states[node.ID()] = node.State()
|
|
|
|
locked_nodes = append(locked_nodes, node)
|
|
|
|
}
|
2023-06-23 10:10:25 -06:00
|
|
|
}
|
|
|
|
|
2023-06-28 00:48:49 -06:00
|
|
|
err = states_fn(states)
|
2023-06-23 10:10:25 -06:00
|
|
|
|
2023-06-28 21:49:23 -06:00
|
|
|
for _, node := range(locked_nodes) {
|
|
|
|
delete(states, node.ID())
|
2023-06-23 10:10:25 -06:00
|
|
|
node.StateLock().RUnlock()
|
|
|
|
}
|
|
|
|
|
2023-06-28 00:48:49 -06:00
|
|
|
return err
|
2023-06-01 22:42:47 -06:00
|
|
|
}
|
2023-04-08 13:58:47 -06:00
|
|
|
|
2023-06-28 23:51:44 -06:00
|
|
|
func UpdateStates(ctx * GraphContext, nodes []GraphNode, nodes_fn NodesFn) error {
|
|
|
|
locked_nodes := NodeMap{}
|
|
|
|
err := UpdateMoreStates(ctx, nodes, locked_nodes, nodes_fn)
|
|
|
|
if err == nil {
|
|
|
|
err = WriteDBStates(ctx, locked_nodes)
|
2023-06-25 13:39:00 -06:00
|
|
|
}
|
|
|
|
|
2023-06-28 23:51:44 -06:00
|
|
|
for _, node := range(locked_nodes) {
|
|
|
|
node.StateLock().Unlock()
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
func UpdateMoreStates(ctx * GraphContext, nodes []GraphNode, locked_nodes NodeMap, nodes_fn NodesFn) error {
|
2023-06-23 10:10:25 -06:00
|
|
|
for _, node := range(nodes) {
|
2023-06-28 23:51:44 -06:00
|
|
|
_, locked := locked_nodes[node.ID()]
|
2023-06-28 21:49:23 -06:00
|
|
|
if locked == false {
|
|
|
|
node.StateLock().Lock()
|
2023-06-28 23:51:44 -06:00
|
|
|
locked_nodes[node.ID()] = node
|
2023-06-23 10:10:25 -06:00
|
|
|
}
|
2023-06-28 21:49:23 -06:00
|
|
|
}
|
2023-06-23 10:10:25 -06:00
|
|
|
|
2023-06-28 23:51:44 -06:00
|
|
|
return nodes_fn(locked_nodes)
|
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-07-04 18:45:23 -06:00
|
|
|
func (node * BaseNode) PropagateUpdate(ctx * GraphContext, update GraphSignal, states NodeStateMap) {
|
2023-06-23 10:10:25 -06:00
|
|
|
}
|
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
|
2023-06-24 19:48:59 -06:00
|
|
|
func (node * BaseNode) UpdateChannel(buffer int) chan GraphSignal {
|
2023-06-23 10:10:25 -06:00
|
|
|
new_listener := make(chan GraphSignal, buffer)
|
|
|
|
node.RegisterChannel(new_listener)
|
|
|
|
return new_listener
|
|
|
|
}
|
|
|
|
|
|
|
|
// Propogate a signal starting at a node
|
2023-07-04 18:45:23 -06:00
|
|
|
func SendUpdate(ctx * GraphContext, node GraphNode, signal GraphSignal, states NodeStateMap) {
|
2023-06-23 10:10:25 -06:00
|
|
|
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)
|
2023-07-04 18:45:23 -06:00
|
|
|
node.PropagateUpdate(ctx, signal, states)
|
2023-04-08 13:58:47 -06:00
|
|
|
}
|
2023-06-02 17:31:29 -06:00
|
|
|
|