2023-06-23 20:56:09 -06:00
|
|
|
package graphvent
|
|
|
|
|
|
|
|
import (
|
2023-08-15 18:23:06 -06:00
|
|
|
"github.com/google/uuid"
|
2023-11-03 22:51:54 -06:00
|
|
|
"time"
|
2023-06-23 20:56:09 -06:00
|
|
|
)
|
|
|
|
|
2023-11-13 13:23:58 -07:00
|
|
|
var AllowParentUnlockPolicy = NewOwnerOfPolicy(Tree{
|
|
|
|
SerializedType(LockSignalType): {
|
|
|
|
Hash(LockStateBase, "unlock"): nil,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2023-11-11 14:52:08 -07:00
|
|
|
var AllowAnyLockPolicy = NewAllNodesPolicy(Tree{
|
|
|
|
SerializedType(LockSignalType): {
|
|
|
|
Hash(LockStateBase, "lock"): nil,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
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
|
|
|
)
|
|
|
|
|
2023-11-03 22:51:54 -06:00
|
|
|
var ReqStateStrings = map[ReqState]string {
|
|
|
|
Unlocked: "Unlocked",
|
|
|
|
Unlocking: "Unlocking",
|
|
|
|
Locked: "Locked",
|
|
|
|
Locking: "Locking",
|
|
|
|
AbortingLock: "AbortingLock",
|
2023-10-01 20:45:44 -06:00
|
|
|
}
|
|
|
|
|
2023-08-11 16:00:36 -06:00
|
|
|
type LockableExt struct{
|
2023-09-12 20:30:18 -06:00
|
|
|
State ReqState `gv:"state"`
|
|
|
|
ReqID *uuid.UUID `gv:"req_id"`
|
2023-09-13 16:27:55 -06:00
|
|
|
Owner *NodeID `gv:"owner"`
|
|
|
|
PendingOwner *NodeID `gv:"pending_owner"`
|
2023-10-01 20:45:44 -06:00
|
|
|
PendingID uuid.UUID `gv:"pending_id"`
|
2023-11-03 22:51:54 -06:00
|
|
|
Requirements map[NodeID]ReqState `gv:"requirements"`
|
|
|
|
WaitInfos WaitMap `gv:"wait_infos"`
|
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-11-03 22:51:54 -06:00
|
|
|
var reqs map[NodeID]ReqState = nil
|
2023-08-11 16:00:36 -06:00
|
|
|
if requirements != nil {
|
2023-11-03 22:51:54 -06:00
|
|
|
reqs = map[NodeID]ReqState{}
|
2023-08-11 16:00:36 -06:00
|
|
|
for _, id := range(requirements) {
|
2023-11-03 22:51:54 -06:00
|
|
|
reqs[id] = Unlocked
|
2023-08-11 16:00:36 -06:00
|
|
|
}
|
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-11-03 22:51:54 -06:00
|
|
|
WaitInfos: WaitMap{},
|
2023-07-26 11:56:10 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-01 20:45:44 -06:00
|
|
|
func UnlockLockable(ctx *Context, node *Node) (uuid.UUID, error) {
|
2023-10-07 23:00:07 -06:00
|
|
|
messages := Messages{}
|
2023-08-15 18:23:06 -06:00
|
|
|
signal := NewLockSignal("unlock")
|
2023-10-14 15:05:23 -06:00
|
|
|
messages = messages.Add(ctx, node.ID, node, nil, signal)
|
2023-10-07 23:00:07 -06:00
|
|
|
return signal.ID(), ctx.Send(messages)
|
2023-07-27 18:08:43 -06:00
|
|
|
}
|
|
|
|
|
2023-10-01 20:45:44 -06:00
|
|
|
func LockLockable(ctx *Context, node *Node) (uuid.UUID, error) {
|
2023-10-07 23:00:07 -06:00
|
|
|
messages := Messages{}
|
2023-08-15 18:23:06 -06:00
|
|
|
signal := NewLockSignal("lock")
|
2023-10-14 15:05:23 -06:00
|
|
|
messages = messages.Add(ctx, node.ID, node, nil, signal)
|
2023-10-07 23:00:07 -06:00
|
|
|
return signal.ID(), ctx.Send(messages)
|
2023-07-27 18:37:06 -06:00
|
|
|
}
|
|
|
|
|
2023-10-07 23:00:07 -06:00
|
|
|
func (ext *LockableExt) HandleErrorSignal(ctx *Context, node *Node, source NodeID, signal *ErrorSignal) (Messages, Changes) {
|
|
|
|
var messages Messages = nil
|
2023-11-11 14:52:08 -07:00
|
|
|
var changes = Changes{}
|
2023-10-01 20:45:44 -06:00
|
|
|
|
2023-11-03 22:51:54 -06:00
|
|
|
info, info_found := node.ProcessResponse(ext.WaitInfos, signal)
|
|
|
|
if info_found {
|
2023-11-07 20:51:34 -07:00
|
|
|
state, found := ext.Requirements[info.Destination]
|
2023-11-03 22:51:54 -06:00
|
|
|
if found == true {
|
2023-11-11 13:53:41 -07:00
|
|
|
changes.Add(LockableExtType, "wait_infos")
|
|
|
|
ctx.Log.Logf("lockable", "got mapped response %+v for %+v in state %s while in %s", signal, info, ReqStateStrings[state], ReqStateStrings[ext.State])
|
|
|
|
switch ext.State {
|
|
|
|
case AbortingLock:
|
|
|
|
ext.Requirements[info.Destination] = Unlocked
|
|
|
|
all_unlocked := true
|
|
|
|
for _, state := range(ext.Requirements) {
|
|
|
|
if state != Unlocked {
|
|
|
|
all_unlocked = false
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if all_unlocked == true {
|
|
|
|
changes.Add(LockableExtType, "state")
|
|
|
|
ext.State = Unlocked
|
|
|
|
}
|
2023-11-03 22:51:54 -06:00
|
|
|
case Locking:
|
2023-11-11 13:53:41 -07:00
|
|
|
changes.Add(LockableExtType, "state")
|
2023-11-03 22:51:54 -06:00
|
|
|
ext.State = AbortingLock
|
2023-11-07 20:51:34 -07:00
|
|
|
ext.Requirements[info.Destination] = Unlocked
|
2023-11-03 22:51:54 -06:00
|
|
|
for id, state := range(ext.Requirements) {
|
|
|
|
if state == Locked {
|
|
|
|
ext.Requirements[id] = Unlocking
|
|
|
|
lock_signal := NewLockSignal("unlock")
|
2023-11-07 20:51:34 -07:00
|
|
|
ext.WaitInfos[lock_signal.Id] = node.QueueTimeout("unlock", id, lock_signal, 100*time.Millisecond)
|
2023-11-03 22:51:54 -06:00
|
|
|
messages = messages.Add(ctx, id, node, nil, lock_signal)
|
|
|
|
ctx.Log.Logf("lockable", "sent abort unlock to %s from %s", id, node.ID)
|
|
|
|
}
|
2023-07-27 22:25:00 -06:00
|
|
|
}
|
2023-11-03 22:51:54 -06:00
|
|
|
case Unlocking:
|
2023-11-13 13:23:58 -07:00
|
|
|
ext.Requirements[info.Destination] = Locked
|
|
|
|
all_returned := true
|
|
|
|
for _, state := range(ext.Requirements) {
|
|
|
|
if state == Unlocking {
|
|
|
|
all_returned = false
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if all_returned == true {
|
|
|
|
ext.State = Locked
|
|
|
|
}
|
2023-08-11 16:00:36 -06:00
|
|
|
}
|
2023-11-03 22:51:54 -06:00
|
|
|
} else {
|
2023-11-07 20:51:34 -07:00
|
|
|
ctx.Log.Logf("lockable", "Got mapped error %s, but %s isn't a requirement", signal, info.Destination)
|
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-10-07 23:00:07 -06:00
|
|
|
return messages, changes
|
2023-08-15 18:23:06 -06:00
|
|
|
}
|
2023-07-27 22:25:00 -06:00
|
|
|
|
2023-10-07 23:00:07 -06:00
|
|
|
func (ext *LockableExt) HandleLinkSignal(ctx *Context, node *Node, source NodeID, signal *LinkSignal) (Messages, Changes) {
|
|
|
|
var messages Messages = nil
|
2023-11-11 14:52:08 -07:00
|
|
|
var changes = Changes{}
|
2023-08-15 19:17:13 -06:00
|
|
|
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-10-14 15:05:23 -06:00
|
|
|
messages = messages.Add(ctx, source, node, nil, NewErrorSignal(signal.ID(), "already_requirement"))
|
2023-08-15 19:17:13 -06:00
|
|
|
} else {
|
2023-08-15 19:28:15 -06:00
|
|
|
if ext.Requirements == nil {
|
2023-11-03 22:51:54 -06:00
|
|
|
ext.Requirements = map[NodeID]ReqState{}
|
2023-08-15 19:28:15 -06:00
|
|
|
}
|
2023-11-03 22:51:54 -06:00
|
|
|
ext.Requirements[signal.NodeID] = Unlocked
|
2023-11-05 21:18:14 -07:00
|
|
|
changes.Add(LockableExtType, "requirements")
|
2023-10-14 15:05:23 -06:00
|
|
|
messages = messages.Add(ctx, source, node, nil, NewSuccessSignal(signal.ID()))
|
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-10-14 15:05:23 -06:00
|
|
|
messages = messages.Add(ctx, source, node, nil, NewErrorSignal(signal.ID(), "can't link: not_requirement"))
|
2023-08-15 19:17:13 -06:00
|
|
|
} else {
|
2023-08-31 19:50:32 -06:00
|
|
|
delete(ext.Requirements, signal.NodeID)
|
2023-11-05 21:18:14 -07:00
|
|
|
changes.Add(LockableExtType, "requirements")
|
2023-10-14 15:05:23 -06:00
|
|
|
messages = messages.Add(ctx, source, node, nil, NewSuccessSignal(signal.ID()))
|
2023-08-15 19:17:13 -06:00
|
|
|
}
|
|
|
|
default:
|
2023-10-14 15:05:23 -06:00
|
|
|
messages = messages.Add(ctx, source, node, nil, NewErrorSignal(signal.ID(), "unknown_action"))
|
2023-08-15 19:17:13 -06:00
|
|
|
}
|
|
|
|
} else {
|
2023-10-14 15:05:23 -06:00
|
|
|
messages = messages.Add(ctx, source, node, nil, NewErrorSignal(signal.ID(), "not_unlocked"))
|
2023-08-15 19:17:13 -06:00
|
|
|
}
|
2023-10-07 23:00:07 -06:00
|
|
|
return messages, changes
|
2023-08-15 19:17:13 -06:00
|
|
|
}
|
|
|
|
|
2023-10-07 23:00:07 -06:00
|
|
|
func (ext *LockableExt) HandleSuccessSignal(ctx *Context, node *Node, source NodeID, signal *SuccessSignal) (Messages, Changes) {
|
|
|
|
var messages Messages = nil
|
2023-11-11 14:52:08 -07:00
|
|
|
var changes = Changes{}
|
2023-10-01 20:54:53 -06:00
|
|
|
if source == node.ID {
|
2023-10-07 23:00:07 -06:00
|
|
|
return messages, changes
|
2023-10-01 20:54:53 -06:00
|
|
|
}
|
2023-10-01 20:45:44 -06:00
|
|
|
|
2023-11-03 22:51:54 -06:00
|
|
|
info, info_found := node.ProcessResponse(ext.WaitInfos, signal)
|
|
|
|
if info_found == true {
|
2023-11-07 20:51:34 -07:00
|
|
|
state, found := ext.Requirements[info.Destination]
|
2023-11-03 22:51:54 -06:00
|
|
|
if found == false {
|
2023-11-07 20:51:34 -07:00
|
|
|
ctx.Log.Logf("lockable", "Got success signal for requirement that is no longer in the map(%s), ignoring...", info.Destination)
|
2023-11-03 22:51:54 -06:00
|
|
|
} else {
|
|
|
|
ctx.Log.Logf("lockable", "got mapped response %+v for %+v in state %s", signal, info, ReqStateStrings[state])
|
|
|
|
switch state {
|
|
|
|
case Locking:
|
|
|
|
switch ext.State {
|
|
|
|
case Locking:
|
2023-11-07 20:51:34 -07:00
|
|
|
ext.Requirements[info.Destination] = Locked
|
2023-11-03 22:51:54 -06:00
|
|
|
locked := 0
|
|
|
|
for _, s := range(ext.Requirements) {
|
|
|
|
if s == Locked {
|
|
|
|
locked += 1
|
|
|
|
}
|
2023-07-27 22:25:00 -06:00
|
|
|
}
|
2023-11-03 22:51:54 -06:00
|
|
|
if locked == len(ext.Requirements) {
|
|
|
|
ctx.Log.Logf("lockable", "WHOLE LOCK: %s - %s - %+v", node.ID, ext.PendingID, ext.PendingOwner)
|
|
|
|
ext.State = Locked
|
|
|
|
ext.Owner = ext.PendingOwner
|
2023-11-05 21:18:14 -07:00
|
|
|
changes.Add(LockableExtType, "state", "owner", "requirements")
|
2023-11-03 22:51:54 -06:00
|
|
|
messages = messages.Add(ctx, *ext.Owner, node, nil, NewSuccessSignal(ext.PendingID))
|
|
|
|
} else {
|
2023-11-05 21:18:14 -07:00
|
|
|
changes.Add(LockableExtType, "requirements")
|
2023-11-03 22:51:54 -06:00
|
|
|
ctx.Log.Logf("lockable", "PARTIAL LOCK: %s - %d/%d", node.ID, locked, len(ext.Requirements))
|
|
|
|
}
|
|
|
|
case AbortingLock:
|
2023-11-07 20:51:34 -07:00
|
|
|
ext.Requirements[info.Destination] = Unlocking
|
2023-07-27 22:25:00 -06:00
|
|
|
|
2023-11-03 22:51:54 -06:00
|
|
|
lock_signal := NewLockSignal("unlock")
|
2023-11-07 20:51:34 -07:00
|
|
|
ext.WaitInfos[lock_signal.Id] = node.QueueTimeout("unlock", info.Destination, lock_signal, 100*time.Millisecond)
|
|
|
|
messages = messages.Add(ctx, info.Destination, node, nil, lock_signal)
|
2023-11-03 22:51:54 -06:00
|
|
|
|
2023-11-07 20:51:34 -07:00
|
|
|
ctx.Log.Logf("lockable", "sending abort_lock to %s for %s", info.Destination, node.ID)
|
2023-07-27 22:25:00 -06:00
|
|
|
}
|
2023-11-03 22:51:54 -06:00
|
|
|
case AbortingLock:
|
|
|
|
ctx.Log.Logf("lockable", "Got success signal in AbortingLock %s", node.ID)
|
|
|
|
fallthrough
|
|
|
|
case Unlocking:
|
|
|
|
ext.Requirements[source] = Unlocked
|
|
|
|
|
|
|
|
unlocked := 0
|
|
|
|
for _, s := range(ext.Requirements) {
|
|
|
|
if s == Unlocked {
|
|
|
|
unlocked += 1
|
|
|
|
}
|
2023-08-15 18:23:06 -06:00
|
|
|
}
|
2023-07-27 22:25:00 -06:00
|
|
|
|
2023-11-03 22:51:54 -06:00
|
|
|
if unlocked == len(ext.Requirements) {
|
|
|
|
old_state := ext.State
|
|
|
|
ext.State = Unlocked
|
|
|
|
ctx.Log.Logf("lockable", "WHOLE UNLOCK: %s - %s - %+v", node.ID, ext.PendingID, ext.PendingOwner)
|
|
|
|
if old_state == Unlocking {
|
|
|
|
previous_owner := *ext.Owner
|
|
|
|
ext.Owner = ext.PendingOwner
|
|
|
|
ext.ReqID = nil
|
2023-11-05 21:18:14 -07:00
|
|
|
changes.Add(LockableExtType, "state", "owner", "req_id")
|
2023-11-03 22:51:54 -06:00
|
|
|
messages = messages.Add(ctx, previous_owner, node, nil, NewSuccessSignal(ext.PendingID))
|
|
|
|
} else if old_state == AbortingLock {
|
2023-11-05 21:18:14 -07:00
|
|
|
changes.Add(LockableExtType, "state", "pending_owner")
|
2023-11-03 22:51:54 -06:00
|
|
|
messages = messages.Add(ctx, *ext.PendingOwner, node, nil, NewErrorSignal(*ext.ReqID, "not_unlocked"))
|
|
|
|
ext.PendingOwner = ext.Owner
|
|
|
|
}
|
|
|
|
} else {
|
2023-11-05 21:18:14 -07:00
|
|
|
changes.Add(LockableExtType, "state")
|
2023-11-03 22:51:54 -06:00
|
|
|
ctx.Log.Logf("lockable", "PARTIAL UNLOCK: %s - %d/%d", node.ID, unlocked, len(ext.Requirements))
|
2023-08-15 18:23:06 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-10-01 20:45:44 -06:00
|
|
|
}
|
|
|
|
|
2023-10-07 23:00:07 -06:00
|
|
|
return messages, changes
|
2023-10-01 20:45:44 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Handle a LockSignal and update the extensions owner/requirement states
|
2023-10-07 23:00:07 -06:00
|
|
|
func (ext *LockableExt) HandleLockSignal(ctx *Context, node *Node, source NodeID, signal *LockSignal) (Messages, Changes) {
|
|
|
|
var messages Messages = nil
|
2023-11-11 14:52:08 -07:00
|
|
|
var changes = Changes{}
|
2023-10-07 23:00:07 -06:00
|
|
|
|
2023-10-01 20:45:44 -06:00
|
|
|
switch signal.State {
|
2023-07-27 19:53:43 -06:00
|
|
|
case "lock":
|
2023-11-03 22:51:54 -06:00
|
|
|
switch ext.State {
|
|
|
|
case 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-11-05 21:18:14 -07:00
|
|
|
changes.Add(LockableExtType, "state", "pending_owner", "owner")
|
2023-10-14 15:05:23 -06:00
|
|
|
messages = messages.Add(ctx, new_owner, node, nil, NewSuccessSignal(signal.ID()))
|
2023-07-27 19:53:43 -06:00
|
|
|
} else {
|
2023-08-15 18:23:06 -06:00
|
|
|
ext.State = Locking
|
2023-10-01 16:45:03 -06:00
|
|
|
id := signal.ID()
|
2023-08-31 19:50:32 -06:00
|
|
|
ext.ReqID = &id
|
2023-08-15 18:23:06 -06:00
|
|
|
new_owner := source
|
|
|
|
ext.PendingOwner = &new_owner
|
2023-10-01 20:45:44 -06:00
|
|
|
ext.PendingID = signal.ID()
|
2023-11-05 21:18:14 -07:00
|
|
|
changes.Add(LockableExtType, "state", "req_id", "pending_owner", "pending_id")
|
2023-11-03 22:51:54 -06:00
|
|
|
for id, state := range(ext.Requirements) {
|
|
|
|
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-11-03 22:51:54 -06:00
|
|
|
|
2023-08-15 18:23:06 -06:00
|
|
|
lock_signal := NewLockSignal("lock")
|
2023-11-11 13:53:41 -07:00
|
|
|
ext.WaitInfos[lock_signal.Id] = node.QueueTimeout("lock", id, lock_signal, 500*time.Millisecond)
|
2023-11-03 22:51:54 -06:00
|
|
|
ext.Requirements[id] = Locking
|
|
|
|
|
2023-10-14 15:05:23 -06:00
|
|
|
messages = messages.Add(ctx, id, node, nil, lock_signal)
|
2023-07-27 19:53:43 -06:00
|
|
|
}
|
2023-08-15 18:23:06 -06:00
|
|
|
}
|
2023-11-03 22:51:54 -06:00
|
|
|
default:
|
2023-10-14 15:05:23 -06:00
|
|
|
messages = messages.Add(ctx, source, node, nil, NewErrorSignal(signal.ID(), "not_unlocked"))
|
2023-11-11 13:53:41 -07:00
|
|
|
ctx.Log.Logf("lockable", "Tried to lock %s while %s", node.ID, ext.State)
|
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-11-05 21:18:14 -07:00
|
|
|
changes.Add(LockableExtType, "state", "pending_owner", "owner")
|
2023-10-14 15:05:23 -06:00
|
|
|
messages = messages.Add(ctx, new_owner, node, nil, NewSuccessSignal(signal.ID()))
|
2023-08-15 18:23:06 -06:00
|
|
|
} else if source == *ext.Owner {
|
|
|
|
ext.State = Unlocking
|
2023-10-01 16:45:03 -06:00
|
|
|
id := signal.ID()
|
2023-08-31 19:50:32 -06:00
|
|
|
ext.ReqID = &id
|
2023-08-15 18:23:06 -06:00
|
|
|
ext.PendingOwner = nil
|
2023-10-01 20:45:44 -06:00
|
|
|
ext.PendingID = signal.ID()
|
2023-11-05 21:18:14 -07:00
|
|
|
changes.Add(LockableExtType, "state", "pending_owner", "pending_id", "req_id")
|
2023-11-03 22:51:54 -06:00
|
|
|
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
|
|
|
}
|
2023-11-03 22:51:54 -06:00
|
|
|
|
2023-08-15 18:23:06 -06:00
|
|
|
lock_signal := NewLockSignal("unlock")
|
2023-11-07 20:51:34 -07:00
|
|
|
ext.WaitInfos[lock_signal.Id] = node.QueueTimeout("unlock", id, lock_signal, 100*time.Millisecond)
|
2023-11-03 22:51:54 -06:00
|
|
|
ext.Requirements[id] = Unlocking
|
|
|
|
|
2023-10-14 15:05:23 -06:00
|
|
|
messages = messages.Add(ctx, id, node, nil, lock_signal)
|
2023-07-27 19:53:43 -06:00
|
|
|
}
|
|
|
|
}
|
2023-08-15 18:23:06 -06:00
|
|
|
} else {
|
2023-10-14 15:05:23 -06:00
|
|
|
messages = messages.Add(ctx, source, node, nil, NewErrorSignal(signal.ID(), "not_locked"))
|
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-10-07 23:00:07 -06:00
|
|
|
return messages, changes
|
2023-07-27 18:37:06 -06:00
|
|
|
}
|
|
|
|
|
2023-11-03 22:51:54 -06:00
|
|
|
func (ext *LockableExt) HandleTimeoutSignal(ctx *Context, node *Node, source NodeID, signal *TimeoutSignal) (Messages, Changes) {
|
|
|
|
var messages Messages = nil
|
2023-11-11 14:52:08 -07:00
|
|
|
var changes = Changes{}
|
2023-11-03 22:51:54 -06:00
|
|
|
|
|
|
|
wait_info, found := node.ProcessResponse(ext.WaitInfos, signal)
|
|
|
|
if found == true {
|
2023-11-11 13:53:41 -07:00
|
|
|
changes.Add(LockableExtType, "wait_infos")
|
2023-11-07 20:51:34 -07:00
|
|
|
state, found := ext.Requirements[wait_info.Destination]
|
2023-11-03 22:51:54 -06:00
|
|
|
if found == true {
|
2023-11-11 14:21:10 -07:00
|
|
|
ctx.Log.Logf("lockable", "%s timed out %s while %s was %s", wait_info.Destination, ReqStateStrings[state], node.ID, ReqStateStrings[state])
|
|
|
|
switch ext.State {
|
|
|
|
case AbortingLock:
|
|
|
|
ext.Requirements[wait_info.Destination] = Unlocked
|
|
|
|
all_unlocked := true
|
|
|
|
for _, state := range(ext.Requirements) {
|
|
|
|
if state != Unlocked {
|
|
|
|
all_unlocked = false
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if all_unlocked == true {
|
|
|
|
changes.Add(LockableExtType, "state")
|
|
|
|
ext.State = Unlocked
|
|
|
|
}
|
2023-11-03 22:54:28 -06:00
|
|
|
case Locking:
|
|
|
|
ext.State = AbortingLock
|
2023-11-07 20:51:34 -07:00
|
|
|
ext.Requirements[wait_info.Destination] = Unlocked
|
2023-11-03 22:54:28 -06:00
|
|
|
for id, state := range(ext.Requirements) {
|
|
|
|
if state == Locked {
|
|
|
|
ext.Requirements[id] = Unlocking
|
|
|
|
lock_signal := NewLockSignal("unlock")
|
2023-11-07 20:51:34 -07:00
|
|
|
ext.WaitInfos[lock_signal.Id] = node.QueueTimeout("unlock", id, lock_signal, 100*time.Millisecond)
|
2023-11-03 22:54:28 -06:00
|
|
|
messages = messages.Add(ctx, id, node, nil, lock_signal)
|
|
|
|
ctx.Log.Logf("lockable", "sent abort unlock to %s from %s", id, node.ID)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case Unlocking:
|
2023-11-13 13:23:58 -07:00
|
|
|
ext.Requirements[wait_info.Destination] = Locked
|
|
|
|
all_returned := true
|
|
|
|
for _, state := range(ext.Requirements) {
|
|
|
|
if state == Unlocking {
|
|
|
|
all_returned = false
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if all_returned == true {
|
|
|
|
ext.State = Locked
|
|
|
|
}
|
2023-11-03 22:54:28 -06:00
|
|
|
}
|
2023-11-03 22:51:54 -06:00
|
|
|
} else {
|
2023-11-07 20:51:34 -07:00
|
|
|
ctx.Log.Logf("lockable", "%s timed out", wait_info.Destination)
|
2023-11-03 22:51:54 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return messages, changes
|
|
|
|
}
|
|
|
|
|
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-10-07 23:00:07 -06:00
|
|
|
func (ext *LockableExt) Process(ctx *Context, node *Node, source NodeID, signal Signal) (Messages, Changes) {
|
|
|
|
var messages Messages = nil
|
2023-11-11 14:52:08 -07:00
|
|
|
var changes = Changes{}
|
2023-10-07 23:00:07 -06:00
|
|
|
|
2023-10-01 16:45:03 -06:00
|
|
|
switch signal.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-10-14 15:05:23 -06:00
|
|
|
messages = messages.Add(ctx, *ext.Owner, node, nil, signal)
|
2023-07-09 14:30:30 -06:00
|
|
|
}
|
2023-07-27 12:20:49 -06:00
|
|
|
}
|
2023-10-07 23:00:07 -06:00
|
|
|
|
2023-07-27 12:20:49 -06:00
|
|
|
case Down:
|
2023-10-07 23:00:07 -06:00
|
|
|
for requirement := range(ext.Requirements) {
|
2023-10-14 15:05:23 -06:00
|
|
|
messages = messages.Add(ctx, requirement, node, nil, signal)
|
2023-07-27 12:20:49 -06:00
|
|
|
}
|
2023-10-07 23:00:07 -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:
|
2023-10-07 23:00:07 -06:00
|
|
|
messages, changes = ext.HandleLinkSignal(ctx, node, source, sig)
|
2023-08-31 19:50:32 -06:00
|
|
|
case *LockSignal:
|
2023-10-07 23:00:07 -06:00
|
|
|
messages, changes = ext.HandleLockSignal(ctx, node, source, sig)
|
2023-08-31 19:50:32 -06:00
|
|
|
case *ErrorSignal:
|
2023-10-07 23:00:07 -06:00
|
|
|
messages, changes = ext.HandleErrorSignal(ctx, node, source, sig)
|
2023-10-01 20:45:44 -06:00
|
|
|
case *SuccessSignal:
|
2023-10-07 23:00:07 -06:00
|
|
|
messages, changes = ext.HandleSuccessSignal(ctx, node, source, sig)
|
2023-11-03 22:51:54 -06:00
|
|
|
case *TimeoutSignal:
|
|
|
|
messages, changes = ext.HandleTimeoutSignal(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-10-07 23:00:07 -06:00
|
|
|
return messages, changes
|
2023-07-25 21:43:15 -06:00
|
|
|
}
|
|
|
|
|
2023-11-11 13:53:41 -07:00
|
|
|
type OwnerOfPolicy struct {
|
|
|
|
PolicyHeader
|
|
|
|
Rules Tree `gv:"rules"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewOwnerOfPolicy(rules Tree) OwnerOfPolicy {
|
|
|
|
return OwnerOfPolicy{
|
|
|
|
PolicyHeader: NewPolicyHeader(),
|
|
|
|
Rules: rules,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (policy OwnerOfPolicy) ContinueAllows(ctx *Context, current PendingACL, signal Signal) RuleResult {
|
|
|
|
return Deny
|
|
|
|
}
|
|
|
|
|
|
|
|
func (policy OwnerOfPolicy) Allows(ctx *Context, principal_id NodeID, action Tree, node *Node)(Messages, RuleResult) {
|
|
|
|
l_ext, err := GetExt[*LockableExt](node, LockableExtType)
|
|
|
|
if err != nil {
|
|
|
|
ctx.Log.Logf("lockable", "OwnerOfPolicy.Allows called on node without LockableExt")
|
|
|
|
return nil, Deny
|
|
|
|
}
|
|
|
|
|
|
|
|
if l_ext.Owner == nil {
|
|
|
|
return nil, Deny
|
|
|
|
}
|
|
|
|
|
|
|
|
if principal_id == *l_ext.Owner {
|
|
|
|
return nil, Allow
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, Deny
|
|
|
|
}
|
|
|
|
|
2023-10-15 15:14:33 -06:00
|
|
|
type RequirementOfPolicy struct {
|
|
|
|
PerNodePolicy
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewRequirementOfPolicy(dep_rules map[NodeID]Tree) RequirementOfPolicy {
|
|
|
|
return RequirementOfPolicy {
|
|
|
|
PerNodePolicy: NewPerNodePolicy(dep_rules),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (policy RequirementOfPolicy) ContinueAllows(ctx *Context, current PendingACL, signal Signal) RuleResult {
|
|
|
|
sig, ok := signal.(*ReadResultSignal)
|
|
|
|
if ok == false {
|
|
|
|
return Deny
|
|
|
|
}
|
|
|
|
|
|
|
|
ext, ok := sig.Extensions[LockableExtType]
|
|
|
|
if ok == false {
|
|
|
|
return Deny
|
|
|
|
}
|
|
|
|
|
|
|
|
reqs_ser, ok := ext["requirements"]
|
|
|
|
if ok == false {
|
|
|
|
return Deny
|
|
|
|
}
|
|
|
|
|
2023-10-29 18:26:14 -06:00
|
|
|
reqs_type, _, err := DeserializeType(ctx, reqs_ser.TypeStack)
|
|
|
|
if err != nil {
|
|
|
|
return Deny
|
|
|
|
}
|
|
|
|
|
|
|
|
reqs_if, _, err := DeserializeValue(ctx, reqs_type, reqs_ser.Data)
|
2023-10-15 15:14:33 -06:00
|
|
|
if err != nil {
|
|
|
|
return Deny
|
|
|
|
}
|
|
|
|
|
|
|
|
requirements, ok := reqs_if.Interface().(map[NodeID]ReqState)
|
|
|
|
if ok == false {
|
|
|
|
return Deny
|
|
|
|
}
|
|
|
|
|
2023-11-05 21:18:14 -07:00
|
|
|
for req := range(requirements) {
|
2023-10-15 15:14:33 -06:00
|
|
|
if req == current.Principal {
|
|
|
|
return policy.NodeRules[sig.NodeID].Allows(current.Action)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Deny
|
|
|
|
}
|