2023-06-18 18:33:17 -06:00
|
|
|
package graphvent
|
2023-05-29 19:17:52 -06:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"sync"
|
2023-06-07 22:46:42 -06:00
|
|
|
"errors"
|
2023-05-29 19:17:52 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
// Resources propagate update up to multiple parents, and not downwards
|
|
|
|
// (subscriber to team won't get update to alliance, but subscriber to alliance will get update to team)
|
2023-06-18 19:14:07 -06:00
|
|
|
func (resource * BaseResource) PropagateUpdate(signal GraphSignal) {
|
2023-06-03 18:56:14 -06:00
|
|
|
|
2023-06-18 18:11:59 -06:00
|
|
|
if signal.Downwards() == false {
|
|
|
|
// Child->Parent, resource updates parent resources
|
2023-06-20 15:48:17 -06:00
|
|
|
resource.connection_lock.Lock()
|
|
|
|
defer resource.connection_lock.Unlock()
|
2023-06-18 18:11:59 -06:00
|
|
|
for _, parent := range resource.Parents() {
|
|
|
|
SendUpdate(parent, signal)
|
2023-06-03 01:38:35 -06:00
|
|
|
}
|
2023-06-04 17:23:49 -06:00
|
|
|
} else {
|
2023-06-18 18:11:59 -06:00
|
|
|
// Parent->Child, resource updates lock holder
|
|
|
|
resource.lock_holder_lock.Lock()
|
|
|
|
defer resource.lock_holder_lock.Unlock()
|
|
|
|
if resource.lock_holder != nil {
|
|
|
|
SendUpdate(resource.lock_holder, signal)
|
|
|
|
}
|
2023-06-04 13:18:10 -06:00
|
|
|
|
2023-06-20 15:48:17 -06:00
|
|
|
resource.connection_lock.Lock()
|
|
|
|
defer resource.connection_lock.Unlock()
|
2023-06-18 19:08:33 -06:00
|
|
|
for _, child := range(resource.children) {
|
2023-06-18 18:11:59 -06:00
|
|
|
SendUpdate(child, signal)
|
|
|
|
}
|
|
|
|
}
|
2023-05-29 19:17:52 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Resource is the interface that DAG nodes are made from
|
2023-06-01 13:11:32 -06:00
|
|
|
// A resource needs to be able to represent logical entities and connections to physical entities.
|
|
|
|
// A resource lock could be aborted at any time if this connection is broken, if that happens the event locking it must be aborted
|
|
|
|
// The device connection should be maintained as much as possible(requiring some reconnection behaviour in the background)
|
2023-05-29 19:17:52 -06:00
|
|
|
type Resource interface {
|
|
|
|
GraphNode
|
2023-06-04 17:23:49 -06:00
|
|
|
Owner() GraphNode
|
2023-05-29 19:17:52 -06:00
|
|
|
Children() []Resource
|
|
|
|
Parents() []Resource
|
2023-06-02 17:31:29 -06:00
|
|
|
|
2023-06-20 15:48:17 -06:00
|
|
|
AddParent(parent Resource)
|
|
|
|
AddChild(child Resource)
|
|
|
|
LockConnections()
|
|
|
|
UnlockConnections()
|
2023-06-02 17:31:29 -06:00
|
|
|
|
2023-06-04 17:23:49 -06:00
|
|
|
SetOwner(owner GraphNode)
|
2023-06-02 17:31:29 -06:00
|
|
|
LockState()
|
|
|
|
UnlockState()
|
|
|
|
|
2023-06-20 16:22:22 -06:00
|
|
|
String() string
|
|
|
|
|
2023-06-04 17:23:49 -06:00
|
|
|
lock(node GraphNode) error
|
|
|
|
unlock(node GraphNode) error
|
2023-05-29 19:17:52 -06:00
|
|
|
}
|
|
|
|
|
2023-06-20 16:22:22 -06:00
|
|
|
func (resource * BaseResource) String() string {
|
|
|
|
return resource.Name()
|
|
|
|
}
|
|
|
|
|
2023-06-20 15:48:17 -06:00
|
|
|
// Recurse up cur's parents to ensure r is not present
|
|
|
|
func checkIfParent(r Resource, cur Resource) bool {
|
|
|
|
if r == nil || cur == nil {
|
|
|
|
panic("Cannot recurse DAG with nil")
|
2023-06-02 17:31:29 -06:00
|
|
|
}
|
2023-05-29 19:17:52 -06:00
|
|
|
|
2023-06-20 15:48:17 -06:00
|
|
|
if r.ID() == cur.ID() {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
cur.LockConnections()
|
|
|
|
defer cur.UnlockConnections()
|
|
|
|
for _, p := range(cur.Parents()) {
|
|
|
|
if checkIfParent(r, p) == true {
|
|
|
|
return true
|
2023-06-02 17:31:29 -06:00
|
|
|
}
|
|
|
|
}
|
2023-06-01 13:11:32 -06:00
|
|
|
|
2023-06-20 15:48:17 -06:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Recurse doen cur's children to ensure r is not present
|
|
|
|
func checkIfChild(r Resource, cur Resource) bool {
|
|
|
|
if r == nil || cur == nil {
|
|
|
|
panic("Cannot recurse DAG with nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
if r.ID() == cur.ID() {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
cur.LockConnections()
|
|
|
|
defer cur.UnlockConnections()
|
|
|
|
for _, c := range(cur.Children()) {
|
|
|
|
if checkIfChild(r, c) == true {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
2023-06-02 17:31:29 -06:00
|
|
|
|
2023-06-20 15:48:17 -06:00
|
|
|
return false
|
2023-05-29 19:17:52 -06:00
|
|
|
}
|
|
|
|
|
2023-06-02 17:31:29 -06:00
|
|
|
func UnlockResource(resource Resource, event Event) error {
|
|
|
|
var err error = nil
|
|
|
|
resource.LockState()
|
2023-06-18 18:11:59 -06:00
|
|
|
defer resource.UnlockState()
|
2023-06-02 17:31:29 -06:00
|
|
|
if resource.Owner() == nil {
|
|
|
|
return errors.New("Resource already unlocked")
|
2023-06-01 13:48:38 -06:00
|
|
|
}
|
|
|
|
|
2023-06-02 17:31:29 -06:00
|
|
|
if resource.Owner().ID() != event.ID() {
|
|
|
|
return errors.New("Resource not locked by parent, unlock failed")
|
|
|
|
}
|
|
|
|
|
|
|
|
var lock_err error = nil
|
|
|
|
for _, child := range resource.Children() {
|
|
|
|
err := UnlockResource(child, event)
|
2023-06-01 13:48:38 -06:00
|
|
|
if err != nil {
|
2023-06-02 17:31:29 -06:00
|
|
|
lock_err = err
|
|
|
|
break
|
2023-06-01 13:48:38 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-02 17:31:29 -06:00
|
|
|
if lock_err != nil {
|
2023-06-18 18:11:59 -06:00
|
|
|
return fmt.Errorf("Resource failed to unlock: %s", lock_err)
|
2023-06-02 17:31:29 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
resource.SetOwner(nil)
|
|
|
|
|
|
|
|
err = resource.unlock(event)
|
|
|
|
if err != nil {
|
|
|
|
return errors.New("Failed to unlock resource")
|
|
|
|
}
|
|
|
|
|
2023-06-01 13:48:38 -06:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-06-04 17:23:49 -06:00
|
|
|
func LockResource(resource Resource, node GraphNode) error {
|
2023-06-02 17:31:29 -06:00
|
|
|
resource.LockState()
|
2023-06-18 18:11:59 -06:00
|
|
|
defer resource.UnlockState()
|
2023-06-02 17:31:29 -06:00
|
|
|
if resource.Owner() != nil {
|
2023-06-18 18:11:59 -06:00
|
|
|
return fmt.Errorf("Resource already locked: %s", resource.Name())
|
2023-06-01 13:11:32 -06:00
|
|
|
}
|
|
|
|
|
2023-06-04 17:23:49 -06:00
|
|
|
err := resource.lock(node)
|
2023-06-03 01:38:35 -06:00
|
|
|
if err != nil {
|
2023-06-18 18:11:59 -06:00
|
|
|
return fmt.Errorf("Failed to lock resource: %s", err)
|
2023-06-03 01:38:35 -06:00
|
|
|
}
|
|
|
|
|
2023-06-02 17:31:29 -06:00
|
|
|
var lock_err error = nil
|
2023-06-03 01:38:35 -06:00
|
|
|
locked_resources := []Resource{}
|
2023-06-02 17:31:29 -06:00
|
|
|
for _, child := range resource.Children() {
|
2023-06-04 17:23:49 -06:00
|
|
|
err := LockResource(child, node)
|
2023-06-02 17:31:29 -06:00
|
|
|
if err != nil{
|
|
|
|
lock_err = err
|
|
|
|
break
|
2023-06-01 13:11:32 -06:00
|
|
|
}
|
2023-06-03 01:38:35 -06:00
|
|
|
locked_resources = append(locked_resources, child)
|
2023-06-01 13:11:32 -06:00
|
|
|
}
|
|
|
|
|
2023-06-02 17:31:29 -06:00
|
|
|
if lock_err != nil {
|
2023-06-18 18:11:59 -06:00
|
|
|
return fmt.Errorf("Resource failed to lock: %s", lock_err)
|
2023-06-02 17:31:29 -06:00
|
|
|
}
|
|
|
|
|
2023-06-07 00:36:40 -06:00
|
|
|
log.Logf("resource", "Locked %s", resource.Name())
|
2023-06-04 17:23:49 -06:00
|
|
|
resource.SetOwner(node)
|
2023-06-02 17:31:29 -06:00
|
|
|
|
2023-06-01 13:11:32 -06:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-06-02 17:31:29 -06:00
|
|
|
// BaseResource is the most basic resource that can exist in the DAG
|
|
|
|
// It holds a single state variable, which contains a pointer to the event that is locking it
|
|
|
|
type BaseResource struct {
|
|
|
|
BaseNode
|
|
|
|
parents []Resource
|
2023-06-18 19:08:33 -06:00
|
|
|
children []Resource
|
2023-06-20 15:48:17 -06:00
|
|
|
connection_lock sync.Mutex
|
2023-06-04 17:23:49 -06:00
|
|
|
lock_holder GraphNode
|
|
|
|
lock_holder_lock sync.Mutex
|
2023-06-02 17:31:29 -06:00
|
|
|
state_lock sync.Mutex
|
2023-05-29 19:17:52 -06:00
|
|
|
}
|
|
|
|
|
2023-06-04 17:23:49 -06:00
|
|
|
func (resource * BaseResource) SetOwner(owner GraphNode) {
|
|
|
|
resource.lock_holder_lock.Lock()
|
2023-06-02 17:31:29 -06:00
|
|
|
resource.lock_holder = owner
|
2023-06-04 17:23:49 -06:00
|
|
|
resource.lock_holder_lock.Unlock()
|
2023-06-02 17:31:29 -06:00
|
|
|
}
|
2023-05-29 19:17:52 -06:00
|
|
|
|
2023-06-02 17:31:29 -06:00
|
|
|
func (resource * BaseResource) LockState() {
|
2023-05-29 19:17:52 -06:00
|
|
|
resource.state_lock.Lock()
|
2023-06-02 17:31:29 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (resource * BaseResource) UnlockState() {
|
2023-05-29 19:17:52 -06:00
|
|
|
resource.state_lock.Unlock()
|
2023-06-02 17:31:29 -06:00
|
|
|
}
|
2023-05-29 19:17:52 -06:00
|
|
|
|
2023-06-04 17:23:49 -06:00
|
|
|
func (resource * BaseResource) Owner() GraphNode {
|
2023-06-02 17:31:29 -06:00
|
|
|
return resource.lock_holder
|
|
|
|
}
|
|
|
|
|
|
|
|
//BaseResources don't check anything special when locking/unlocking
|
2023-06-04 17:23:49 -06:00
|
|
|
func (resource * BaseResource) lock(node GraphNode) error {
|
2023-06-02 17:31:29 -06:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-06-04 17:23:49 -06:00
|
|
|
func (resource * BaseResource) unlock(node GraphNode) error {
|
2023-06-02 17:31:29 -06:00
|
|
|
return nil
|
2023-05-29 19:17:52 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (resource * BaseResource) Children() []Resource {
|
2023-06-18 19:08:33 -06:00
|
|
|
return resource.children
|
2023-05-29 19:17:52 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (resource * BaseResource) Parents() []Resource {
|
|
|
|
return resource.parents
|
|
|
|
}
|
|
|
|
|
2023-06-20 15:48:17 -06:00
|
|
|
func (resource * BaseResource) LockConnections() {
|
|
|
|
resource.connection_lock.Lock()
|
2023-06-02 17:31:29 -06:00
|
|
|
}
|
2023-05-29 19:17:52 -06:00
|
|
|
|
2023-06-20 15:48:17 -06:00
|
|
|
func (resource * BaseResource) UnlockConnections() {
|
|
|
|
resource.connection_lock.Unlock()
|
2023-06-02 17:31:29 -06:00
|
|
|
}
|
2023-05-29 19:17:52 -06:00
|
|
|
|
2023-06-20 15:48:17 -06:00
|
|
|
func (resource * BaseResource) AddParent(parent Resource) {
|
2023-05-29 19:17:52 -06:00
|
|
|
resource.parents = append(resource.parents, parent)
|
|
|
|
}
|
|
|
|
|
2023-06-20 15:48:17 -06:00
|
|
|
func (resource * BaseResource) AddChild(child Resource) {
|
|
|
|
resource.children = append(resource.children, child)
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewBaseResource(name string, description string) BaseResource {
|
2023-06-01 22:42:47 -06:00
|
|
|
resource := BaseResource{
|
2023-06-03 18:56:14 -06:00
|
|
|
BaseNode: NewBaseNode(name, description, randid()),
|
2023-05-29 19:17:52 -06:00
|
|
|
parents: []Resource{},
|
2023-06-20 15:48:17 -06:00
|
|
|
children: []Resource{},
|
2023-05-29 19:17:52 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
return resource
|
|
|
|
}
|
2023-06-01 22:42:47 -06:00
|
|
|
|
2023-06-20 15:48:17 -06:00
|
|
|
func LinkResource(resource Resource, child Resource) error {
|
|
|
|
if child == nil || resource == nil {
|
|
|
|
return fmt.Errorf("Will not connect nil to resource DAG")
|
|
|
|
} else if child.ID() == resource.ID() {
|
|
|
|
return fmt.Errorf("Will not connect resource to itself")
|
|
|
|
}
|
|
|
|
|
|
|
|
if checkIfChild(resource, child) {
|
|
|
|
return fmt.Errorf("%s is a child of %s, cannot add as parent", resource.Name(), child.Name())
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, p := range(resource.Parents()) {
|
|
|
|
if checkIfParent(child, p) {
|
|
|
|
return fmt.Errorf("Will not add %s as a parent of itself", child.Name())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
child.AddParent(resource)
|
|
|
|
resource.AddChild(child)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func LinkResources(resource Resource, children []Resource) error {
|
|
|
|
for _, c := range(children) {
|
|
|
|
err := LinkResource(resource, c)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewResource(name string, description string, children []Resource) (* BaseResource, error) {
|
|
|
|
resource := NewBaseResource(name, description)
|
|
|
|
resource_ptr := &resource
|
|
|
|
|
|
|
|
err := LinkResources(resource_ptr, children)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return resource_ptr, nil
|
2023-06-01 22:42:47 -06:00
|
|
|
}
|