2023-06-23 20:56:09 -06:00
|
|
|
package graphvent
|
|
|
|
|
|
|
|
import (
|
2023-08-15 18:23:06 -06:00
|
|
|
"github.com/google/uuid"
|
2023-06-23 20:56:09 -06:00
|
|
|
)
|
|
|
|
|
2023-08-15 18:23:06 -06:00
|
|
|
type ReqState byte
|
2023-08-11 16:00:36 -06:00
|
|
|
const (
|
|
|
|
Unlocked = ReqState(0)
|
|
|
|
Unlocking = ReqState(1)
|
|
|
|
Locked = ReqState(2)
|
|
|
|
Locking = ReqState(3)
|
2023-08-15 18:23:06 -06:00
|
|
|
AbortingLock = ReqState(4)
|
2023-08-11 16:00:36 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
type LockableExt struct{
|
2023-08-31 19:50:32 -06:00
|
|
|
State ReqState `ext:""`
|
|
|
|
ReqID *uuid.UUID `ext:""`
|
|
|
|
Owner *NodeID `ext:""`
|
|
|
|
PendingOwner *NodeID `ext:""`
|
|
|
|
Requirements map[NodeID]ReqState `ext:""`
|
2023-07-28 11:21:18 -06:00
|
|
|
}
|
|
|
|
|
2023-07-25 21:43:15 -06:00
|
|
|
func (ext *LockableExt) Type() ExtType {
|
|
|
|
return LockableExtType
|
2023-06-28 21:49:23 -06:00
|
|
|
}
|
|
|
|
|
2023-08-11 13:01:32 -06:00
|
|
|
func NewLockableExt(requirements []NodeID) *LockableExt {
|
2023-08-11 16:00:36 -06:00
|
|
|
var reqs map[NodeID]ReqState = nil
|
|
|
|
if requirements != nil {
|
|
|
|
reqs = map[NodeID]ReqState{}
|
|
|
|
for _, id := range(requirements) {
|
|
|
|
reqs[id] = Unlocked
|
|
|
|
}
|
2023-08-11 13:01:32 -06:00
|
|
|
}
|
2023-07-26 11:56:10 -06:00
|
|
|
return &LockableExt{
|
2023-08-15 18:23:06 -06:00
|
|
|
State: Unlocked,
|
2023-07-27 18:37:06 -06:00
|
|
|
Owner: nil,
|
2023-07-27 19:53:43 -06:00
|
|
|
PendingOwner: nil,
|
2023-08-11 13:01:32 -06:00
|
|
|
Requirements: reqs,
|
2023-07-26 11:56:10 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-28 00:04:18 -06:00
|
|
|
// Send the signal to unlock a node from itself
|
2023-08-15 18:23:06 -06:00
|
|
|
func UnlockLockable(ctx *Context, owner *Node, target NodeID) (uuid.UUID, error) {
|
2023-08-08 14:00:17 -06:00
|
|
|
msgs := Messages{}
|
2023-08-15 18:23:06 -06:00
|
|
|
signal := NewLockSignal("unlock")
|
2023-08-31 19:50:32 -06:00
|
|
|
msgs = msgs.Add(ctx, owner.ID, owner.Key, signal, target)
|
|
|
|
return signal.Header().ID, ctx.Send(msgs)
|
2023-07-27 18:08:43 -06:00
|
|
|
}
|
|
|
|
|
2023-07-28 00:04:18 -06:00
|
|
|
// Send the signal to lock a node from itself
|
2023-08-15 18:23:06 -06:00
|
|
|
func LockLockable(ctx *Context, owner *Node, target NodeID) (uuid.UUID, error) {
|
2023-08-08 14:00:17 -06:00
|
|
|
msgs := Messages{}
|
2023-08-15 18:23:06 -06:00
|
|
|
signal := NewLockSignal("lock")
|
2023-08-31 19:50:32 -06:00
|
|
|
msgs = msgs.Add(ctx, owner.ID, owner.Key, signal, target)
|
|
|
|
return signal.Header().ID, ctx.Send(msgs)
|
2023-07-27 18:37:06 -06:00
|
|
|
}
|
|
|
|
|
2023-08-31 19:50:32 -06:00
|
|
|
func (ext *LockableExt) HandleErrorSignal(ctx *Context, node *Node, source NodeID, signal *ErrorSignal) Messages {
|
2023-08-15 18:23:06 -06:00
|
|
|
str := signal.Error
|
2023-08-31 19:50:32 -06:00
|
|
|
ctx.Log.Logf("lockable", "ERROR_SIGNAL: %s->%s %+v", source, node.ID, str)
|
2023-08-07 20:26:02 -06:00
|
|
|
|
2023-08-15 18:23:06 -06:00
|
|
|
msgs := Messages {}
|
|
|
|
switch str {
|
|
|
|
case "not_unlocked":
|
|
|
|
if ext.State == Locking {
|
|
|
|
ext.State = AbortingLock
|
|
|
|
ext.Requirements[source] = Unlocked
|
|
|
|
for id, state := range(ext.Requirements) {
|
|
|
|
if state == Locked {
|
2023-08-11 16:00:36 -06:00
|
|
|
ext.Requirements[id] = Unlocking
|
2023-08-31 19:50:32 -06:00
|
|
|
msgs = msgs.Add(ctx, node.ID, node.Key, NewLockSignal("unlock"), id)
|
2023-07-27 22:25:00 -06:00
|
|
|
}
|
2023-08-11 16:00:36 -06:00
|
|
|
}
|
2023-07-27 22:25:00 -06:00
|
|
|
}
|
2023-08-15 18:23:06 -06:00
|
|
|
case "not_locked":
|
|
|
|
panic("RECEIVED not_locked, meaning a node thought it held a lock it didn't")
|
|
|
|
case "not_requirement":
|
|
|
|
}
|
2023-07-27 22:25:00 -06:00
|
|
|
|
2023-08-15 18:23:06 -06:00
|
|
|
return msgs
|
|
|
|
}
|
2023-07-27 22:25:00 -06:00
|
|
|
|
2023-08-31 19:50:32 -06:00
|
|
|
func (ext *LockableExt) HandleLinkSignal(ctx *Context, node *Node, source NodeID, signal *LinkSignal) Messages {
|
2023-08-15 19:17:13 -06:00
|
|
|
msgs := Messages {}
|
|
|
|
if ext.State == Unlocked {
|
2023-08-31 19:50:32 -06:00
|
|
|
switch signal.Action {
|
2023-08-15 19:17:13 -06:00
|
|
|
case "add":
|
2023-08-31 19:50:32 -06:00
|
|
|
_, exists := ext.Requirements[signal.NodeID]
|
2023-08-15 19:17:13 -06:00
|
|
|
if exists == true {
|
2023-08-31 19:50:32 -06:00
|
|
|
msgs = msgs.Add(ctx, node.ID, node.Key, NewErrorSignal(signal.ID, "already_requirement"), source)
|
2023-08-15 19:17:13 -06:00
|
|
|
} else {
|
2023-08-15 19:28:15 -06:00
|
|
|
if ext.Requirements == nil {
|
|
|
|
ext.Requirements = map[NodeID]ReqState{}
|
|
|
|
}
|
2023-08-31 19:50:32 -06:00
|
|
|
ext.Requirements[signal.NodeID] = Unlocked
|
|
|
|
msgs = msgs.Add(ctx, node.ID, node.Key, NewErrorSignal(signal.ID, "req_added"), source)
|
2023-08-15 19:17:13 -06:00
|
|
|
}
|
|
|
|
case "remove":
|
2023-08-31 19:50:32 -06:00
|
|
|
_, exists := ext.Requirements[signal.NodeID]
|
2023-08-15 19:17:13 -06:00
|
|
|
if exists == false {
|
2023-08-31 19:50:32 -06:00
|
|
|
msgs = msgs.Add(ctx, node.ID, node.Key, NewErrorSignal(signal.ID, "not_requirement"), source)
|
2023-08-15 19:17:13 -06:00
|
|
|
} else {
|
2023-08-31 19:50:32 -06:00
|
|
|
delete(ext.Requirements, signal.NodeID)
|
|
|
|
msgs = msgs.Add(ctx, node.ID, node.Key, NewErrorSignal(signal.ID, "req_removed"), source)
|
2023-08-15 19:17:13 -06:00
|
|
|
}
|
|
|
|
default:
|
2023-08-31 19:50:32 -06:00
|
|
|
msgs = msgs.Add(ctx, node.ID, node.Key, NewErrorSignal(signal.ID, "unknown_action"), source)
|
2023-08-15 19:17:13 -06:00
|
|
|
}
|
|
|
|
} else {
|
2023-08-31 19:50:32 -06:00
|
|
|
msgs = msgs.Add(ctx, node.ID, node.Key, NewErrorSignal(signal.ID, "not_unlocked"), source)
|
2023-08-15 19:17:13 -06:00
|
|
|
}
|
|
|
|
return msgs
|
|
|
|
}
|
|
|
|
|
2023-08-15 18:23:06 -06:00
|
|
|
// Handle a LockSignal and update the extensions owner/requirement states
|
2023-08-31 19:50:32 -06:00
|
|
|
func (ext *LockableExt) HandleLockSignal(ctx *Context, node *Node, source NodeID, signal *LockSignal) Messages {
|
|
|
|
ctx.Log.Logf("lockable", "LOCK_SIGNAL: %s->%s %+v", source, node.ID, signal.State)
|
2023-07-27 22:25:00 -06:00
|
|
|
|
2023-08-15 18:23:06 -06:00
|
|
|
msgs := Messages{}
|
2023-08-31 19:50:32 -06:00
|
|
|
switch signal.State {
|
2023-07-27 22:25:00 -06:00
|
|
|
case "locked":
|
2023-08-15 18:23:06 -06:00
|
|
|
state, found := ext.Requirements[source]
|
|
|
|
if found == false {
|
2023-08-31 19:50:32 -06:00
|
|
|
msgs = msgs.Add(ctx, node.ID, node.Key, NewErrorSignal(signal.ID, "not_requirement"), source)
|
2023-08-15 18:23:06 -06:00
|
|
|
} else if state == Locking {
|
|
|
|
if ext.State == Locking {
|
2023-08-11 16:00:36 -06:00
|
|
|
ext.Requirements[source] = Locked
|
2023-08-15 18:23:06 -06:00
|
|
|
reqs := 0
|
|
|
|
locked := 0
|
|
|
|
for _, s := range(ext.Requirements) {
|
|
|
|
reqs += 1
|
|
|
|
if s == Locked {
|
|
|
|
locked += 1
|
2023-07-27 22:25:00 -06:00
|
|
|
}
|
2023-08-15 18:23:06 -06:00
|
|
|
}
|
2023-07-27 22:25:00 -06:00
|
|
|
|
2023-08-15 18:23:06 -06:00
|
|
|
if locked == reqs {
|
|
|
|
ext.State = Locked
|
|
|
|
ext.Owner = ext.PendingOwner
|
2023-08-31 19:50:32 -06:00
|
|
|
msgs = msgs.Add(ctx, node.ID, node.Key, NewLockSignal("locked"), *ext.Owner)
|
2023-08-15 18:23:06 -06:00
|
|
|
} else {
|
2023-08-31 19:50:32 -06:00
|
|
|
ctx.Log.Logf("lockable", "PARTIAL LOCK: %s - %d/%d", node.ID, locked, reqs)
|
2023-07-27 22:25:00 -06:00
|
|
|
}
|
2023-08-15 18:23:06 -06:00
|
|
|
} else if ext.State == AbortingLock {
|
|
|
|
ext.Requirements[source] = Unlocking
|
2023-08-31 19:50:32 -06:00
|
|
|
msgs = msgs.Add(ctx, node.ID, node.Key, NewLockSignal("unlock"), source)
|
2023-07-27 22:25:00 -06:00
|
|
|
}
|
2023-07-27 19:53:43 -06:00
|
|
|
}
|
2023-08-15 18:23:06 -06:00
|
|
|
case "unlocked":
|
|
|
|
state, found := ext.Requirements[source]
|
|
|
|
if found == false {
|
2023-08-31 19:50:32 -06:00
|
|
|
msgs = msgs.Add(ctx, node.ID, node.Key, NewErrorSignal(signal.ID, "not_requirement"), source)
|
2023-08-15 18:23:06 -06:00
|
|
|
} else if state == Unlocking {
|
|
|
|
ext.Requirements[source] = Unlocked
|
|
|
|
reqs := 0
|
|
|
|
unlocked := 0
|
|
|
|
for _, s := range(ext.Requirements) {
|
|
|
|
reqs += 1
|
|
|
|
if s == Unlocked {
|
|
|
|
unlocked += 1
|
|
|
|
}
|
2023-08-11 16:00:36 -06:00
|
|
|
}
|
2023-07-27 22:25:00 -06:00
|
|
|
|
2023-08-15 18:23:06 -06:00
|
|
|
if unlocked == reqs {
|
|
|
|
old_state := ext.State
|
|
|
|
ext.State = Unlocked
|
|
|
|
if old_state == Unlocking {
|
|
|
|
ext.Owner = ext.PendingOwner
|
2023-08-31 19:50:32 -06:00
|
|
|
ext.ReqID = nil
|
|
|
|
msgs = msgs.Add(ctx, node.ID, node.Key, NewLockSignal("unlocked"), *ext.Owner)
|
2023-08-15 18:23:06 -06:00
|
|
|
} else if old_state == AbortingLock {
|
2023-08-31 19:50:32 -06:00
|
|
|
msgs = msgs.Add(ctx ,node.ID, node.Key, NewErrorSignal(*ext.ReqID, "not_unlocked"), *ext.PendingOwner)
|
2023-08-15 18:23:06 -06:00
|
|
|
ext.PendingOwner = ext.Owner
|
|
|
|
}
|
|
|
|
} else {
|
2023-08-31 19:50:32 -06:00
|
|
|
ctx.Log.Logf("lockable", "PARTIAL UNLOCK: %s - %d/%d", node.ID, unlocked, reqs)
|
2023-08-15 18:23:06 -06:00
|
|
|
}
|
|
|
|
}
|
2023-07-27 19:53:43 -06:00
|
|
|
case "lock":
|
2023-08-15 18:23:06 -06:00
|
|
|
if ext.State == Unlocked {
|
2023-07-27 19:53:43 -06:00
|
|
|
if len(ext.Requirements) == 0 {
|
2023-08-15 18:23:06 -06:00
|
|
|
ext.State = Locked
|
|
|
|
new_owner := source
|
|
|
|
ext.PendingOwner = &new_owner
|
|
|
|
ext.Owner = &new_owner
|
2023-08-31 19:50:32 -06:00
|
|
|
msgs = msgs.Add(ctx, node.ID, node.Key, NewLockSignal("locked"), new_owner)
|
2023-07-27 19:53:43 -06:00
|
|
|
} else {
|
2023-08-15 18:23:06 -06:00
|
|
|
ext.State = Locking
|
2023-08-31 19:50:32 -06:00
|
|
|
id := signal.ID
|
|
|
|
ext.ReqID = &id
|
2023-08-15 18:23:06 -06:00
|
|
|
new_owner := source
|
|
|
|
ext.PendingOwner = &new_owner
|
2023-07-27 19:53:43 -06:00
|
|
|
for id, state := range(ext.Requirements) {
|
2023-08-11 16:00:36 -06:00
|
|
|
if state != Unlocked {
|
2023-08-31 19:50:32 -06:00
|
|
|
ctx.Log.Logf("lockable", "REQ_NOT_UNLOCKED_WHEN_LOCKING")
|
2023-07-27 19:53:43 -06:00
|
|
|
}
|
2023-08-11 16:00:36 -06:00
|
|
|
ext.Requirements[id] = Locking
|
2023-08-15 18:23:06 -06:00
|
|
|
lock_signal := NewLockSignal("lock")
|
2023-08-31 19:50:32 -06:00
|
|
|
msgs = msgs.Add(ctx, node.ID, node.Key, lock_signal, id)
|
2023-07-27 19:53:43 -06:00
|
|
|
}
|
2023-08-15 18:23:06 -06:00
|
|
|
}
|
|
|
|
} else {
|
2023-08-31 19:50:32 -06:00
|
|
|
msgs = msgs.Add(ctx, node.ID, node.Key, NewErrorSignal(signal.ID, "not_unlocked"), source)
|
2023-08-15 18:23:06 -06:00
|
|
|
}
|
|
|
|
case "unlock":
|
|
|
|
if ext.State == Locked {
|
|
|
|
if len(ext.Requirements) == 0 {
|
|
|
|
ext.State = Unlocked
|
|
|
|
new_owner := source
|
|
|
|
ext.PendingOwner = nil
|
|
|
|
ext.Owner = nil
|
2023-08-31 19:50:32 -06:00
|
|
|
msgs = msgs.Add(ctx, node.ID, node.Key, NewLockSignal("unlocked"), new_owner)
|
2023-08-15 18:23:06 -06:00
|
|
|
} else if source == *ext.Owner {
|
|
|
|
ext.State = Unlocking
|
2023-08-31 19:50:32 -06:00
|
|
|
id := signal.ID
|
|
|
|
ext.ReqID = &id
|
2023-08-15 18:23:06 -06:00
|
|
|
ext.PendingOwner = nil
|
|
|
|
for id, state := range(ext.Requirements) {
|
|
|
|
if state != Locked {
|
2023-08-31 19:50:32 -06:00
|
|
|
ctx.Log.Logf("lockable", "REQ_NOT_LOCKED_WHEN_UNLOCKING")
|
2023-08-15 18:23:06 -06:00
|
|
|
}
|
|
|
|
ext.Requirements[id] = Unlocking
|
|
|
|
lock_signal := NewLockSignal("unlock")
|
2023-08-31 19:50:32 -06:00
|
|
|
msgs = msgs.Add(ctx, node.ID, node.Key, lock_signal, id)
|
2023-07-27 19:53:43 -06:00
|
|
|
}
|
|
|
|
}
|
2023-08-15 18:23:06 -06:00
|
|
|
} else {
|
2023-08-31 19:50:32 -06:00
|
|
|
msgs = msgs.Add(ctx, node.ID, node.Key, NewErrorSignal(signal.ID, "not_locked"), source)
|
2023-07-27 19:53:43 -06:00
|
|
|
}
|
2023-07-27 18:37:06 -06:00
|
|
|
default:
|
2023-08-31 19:50:32 -06:00
|
|
|
ctx.Log.Logf("lockable", "LOCK_ERR: unkown state %s", signal.State)
|
2023-07-27 18:37:06 -06:00
|
|
|
}
|
2023-08-15 18:23:06 -06:00
|
|
|
return msgs
|
2023-07-27 18:37:06 -06:00
|
|
|
}
|
|
|
|
|
2023-07-28 00:04:18 -06:00
|
|
|
// LockableExts process Up/Down signals by forwarding them to owner, dependency, and requirement nodes
|
|
|
|
// LockSignal and LinkSignal Direct signals are processed to update the requirement/dependency/lock state
|
2023-08-08 14:00:17 -06:00
|
|
|
func (ext *LockableExt) Process(ctx *Context, node *Node, source NodeID, signal Signal) Messages {
|
|
|
|
messages := Messages{}
|
2023-08-31 19:50:32 -06:00
|
|
|
switch signal.Header().Direction {
|
2023-07-22 20:21:17 -06:00
|
|
|
case Up:
|
2023-08-11 13:01:32 -06:00
|
|
|
if ext.Owner != nil {
|
2023-07-27 15:27:14 -06:00
|
|
|
if *ext.Owner != node.ID {
|
2023-08-31 19:50:32 -06:00
|
|
|
messages = messages.Add(ctx, node.ID, node.Key, signal, *ext.Owner)
|
2023-07-09 14:30:30 -06:00
|
|
|
}
|
2023-07-27 12:20:49 -06:00
|
|
|
}
|
|
|
|
case Down:
|
2023-08-11 13:01:32 -06:00
|
|
|
for requirement, _ := range(ext.Requirements) {
|
2023-08-31 19:50:32 -06:00
|
|
|
messages = messages.Add(ctx, node.ID, node.Key, signal, requirement)
|
2023-07-27 12:20:49 -06:00
|
|
|
}
|
2023-07-22 20:21:17 -06:00
|
|
|
case Direct:
|
2023-08-31 19:50:32 -06:00
|
|
|
switch sig := signal.(type) {
|
|
|
|
case *LinkSignal:
|
|
|
|
messages = ext.HandleLinkSignal(ctx, node, source, sig)
|
|
|
|
case *LockSignal:
|
|
|
|
messages = ext.HandleLockSignal(ctx, node, source, sig)
|
|
|
|
case *ErrorSignal:
|
|
|
|
messages = ext.HandleErrorSignal(ctx, node, source, sig)
|
2023-07-27 16:21:27 -06:00
|
|
|
default:
|
|
|
|
}
|
2023-07-22 20:21:17 -06:00
|
|
|
default:
|
2023-07-24 17:07:27 -06:00
|
|
|
}
|
2023-08-07 20:26:02 -06:00
|
|
|
return messages
|
2023-07-25 21:43:15 -06:00
|
|
|
}
|
|
|
|
|