|
|
|
@ -2,7 +2,6 @@ package graphvent
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"github.com/google/uuid"
|
|
|
|
|
"time"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type ReqState byte
|
|
|
|
@ -22,399 +21,389 @@ var ReqStateStrings = map[ReqState]string {
|
|
|
|
|
AbortingLock: "AbortingLock",
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (state ReqState) String() string {
|
|
|
|
|
str, mapped := ReqStateStrings[state]
|
|
|
|
|
if mapped == false {
|
|
|
|
|
return "UNKNOWN_REQSTATE"
|
|
|
|
|
} else {
|
|
|
|
|
return str
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
type LockableExt struct{
|
|
|
|
|
State ReqState `gv:"state"`
|
|
|
|
|
ReqID *uuid.UUID `gv:"req_id"`
|
|
|
|
|
Owner *NodeID `gv:"owner" node:"Base"`
|
|
|
|
|
PendingOwner *NodeID `gv:"pending_owner" node:"Base"`
|
|
|
|
|
PendingID uuid.UUID `gv:"pending_id"`
|
|
|
|
|
Owner *NodeID `gv:"owner"`
|
|
|
|
|
PendingOwner *NodeID `gv:"pending_owner"`
|
|
|
|
|
Requirements map[NodeID]ReqState `gv:"requirements" node:"Lockable:"`
|
|
|
|
|
WaitInfos WaitMap `gv:"wait_infos" node:":Base"`
|
|
|
|
|
|
|
|
|
|
Locked map[NodeID]any
|
|
|
|
|
Unlocked map[NodeID]any
|
|
|
|
|
|
|
|
|
|
Waiting WaitMap `gv:"waiting_locks" node:":Lockable"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func NewLockableExt(requirements []NodeID) *LockableExt {
|
|
|
|
|
var reqs map[NodeID]ReqState = nil
|
|
|
|
|
if requirements != nil {
|
|
|
|
|
var unlocked map[NodeID]any = map[NodeID]any{}
|
|
|
|
|
|
|
|
|
|
if len(requirements) != 0 {
|
|
|
|
|
reqs = map[NodeID]ReqState{}
|
|
|
|
|
for _, id := range(requirements) {
|
|
|
|
|
reqs[id] = Unlocked
|
|
|
|
|
for _, req := range(requirements) {
|
|
|
|
|
reqs[req] = Unlocked
|
|
|
|
|
unlocked[req] = nil
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return &LockableExt{
|
|
|
|
|
State: Unlocked,
|
|
|
|
|
Owner: nil,
|
|
|
|
|
PendingOwner: nil,
|
|
|
|
|
Requirements: reqs,
|
|
|
|
|
WaitInfos: WaitMap{},
|
|
|
|
|
Waiting: WaitMap{},
|
|
|
|
|
|
|
|
|
|
Locked: map[NodeID]any{},
|
|
|
|
|
Unlocked: unlocked,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func UnlockLockable(ctx *Context, node *Node) (uuid.UUID, error) {
|
|
|
|
|
signal := NewLockSignal("unlock")
|
|
|
|
|
messages := []SendMsg{{node.ID, signal}}
|
|
|
|
|
signal := NewUnlockSignal()
|
|
|
|
|
messages := []Message{{node.ID, signal}}
|
|
|
|
|
return signal.ID(), ctx.Send(node, messages)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func LockLockable(ctx *Context, node *Node) (uuid.UUID, error) {
|
|
|
|
|
signal := NewLockSignal("lock")
|
|
|
|
|
messages := []SendMsg{{node.ID, signal}}
|
|
|
|
|
signal := NewLockSignal()
|
|
|
|
|
messages := []Message{{node.ID, signal}}
|
|
|
|
|
return signal.ID(), ctx.Send(node, messages)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (ext *LockableExt) Load(ctx *Context, node *Node) error {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
ext.Locked = map[NodeID]any{}
|
|
|
|
|
ext.Unlocked = map[NodeID]any{}
|
|
|
|
|
|
|
|
|
|
func (ext *LockableExt) Unload(ctx *Context, node *Node) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (ext *LockableExt) HandleErrorSignal(ctx *Context, node *Node, source NodeID, signal *ErrorSignal) ([]SendMsg, Changes) {
|
|
|
|
|
var messages []SendMsg = nil
|
|
|
|
|
var changes Changes = nil
|
|
|
|
|
|
|
|
|
|
info, info_found := node.ProcessResponse(ext.WaitInfos, signal)
|
|
|
|
|
if info_found {
|
|
|
|
|
state, found := ext.Requirements[info.Destination]
|
|
|
|
|
if found == true {
|
|
|
|
|
changes.Add("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("state")
|
|
|
|
|
ext.State = Unlocked
|
|
|
|
|
}
|
|
|
|
|
case Locking:
|
|
|
|
|
changes.Add("state")
|
|
|
|
|
ext.Requirements[info.Destination] = Unlocked
|
|
|
|
|
unlocked := 0
|
|
|
|
|
for _, state := range(ext.Requirements) {
|
|
|
|
|
if state == Unlocked {
|
|
|
|
|
unlocked += 1
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if unlocked == len(ext.Requirements) {
|
|
|
|
|
ctx.Log.Logf("lockable", "%s unlocked from error %s from %s", node.ID, signal.Error, source)
|
|
|
|
|
ext.State = Unlocked
|
|
|
|
|
} else {
|
|
|
|
|
ext.State = AbortingLock
|
|
|
|
|
for id, state := range(ext.Requirements) {
|
|
|
|
|
if state == Locked {
|
|
|
|
|
ext.Requirements[id] = Unlocking
|
|
|
|
|
lock_signal := NewLockSignal("unlock")
|
|
|
|
|
ext.WaitInfos[lock_signal.Id] = node.QueueTimeout("unlock", id, lock_signal, 100*time.Millisecond)
|
|
|
|
|
messages = append(messages, SendMsg{id, lock_signal})
|
|
|
|
|
ctx.Log.Logf("lockable", "sent abort unlock to %s from %s", id, node.ID)
|
|
|
|
|
if state == Unlocked {
|
|
|
|
|
ext.Unlocked[id] = nil
|
|
|
|
|
} else if state == Locked {
|
|
|
|
|
ext.Locked[id] = nil
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case Unlocking:
|
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
ctx.Log.Logf("lockable", "Got mapped error %s, but %s isn't a requirement", signal, info.Destination)
|
|
|
|
|
}
|
|
|
|
|
func (ext *LockableExt) Unload(ctx *Context, node *Node) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return messages, changes
|
|
|
|
|
}
|
|
|
|
|
// Handle link signal by adding/removing the requested NodeID
|
|
|
|
|
// returns an error if the node is not unlocked
|
|
|
|
|
func (ext *LockableExt) HandleLinkSignal(ctx *Context, node *Node, source NodeID, signal *LinkSignal) ([]Message, Changes) {
|
|
|
|
|
var messages []Message = nil
|
|
|
|
|
var changes Changes = nil
|
|
|
|
|
|
|
|
|
|
func (ext *LockableExt) HandleLinkSignal(ctx *Context, node *Node, source NodeID, signal *LinkSignal) ([]SendMsg, Changes) {
|
|
|
|
|
var messages []SendMsg = nil
|
|
|
|
|
var changes = Changes{}
|
|
|
|
|
if ext.State == Unlocked {
|
|
|
|
|
switch ext.State {
|
|
|
|
|
case Unlocked:
|
|
|
|
|
switch signal.Action {
|
|
|
|
|
case "add":
|
|
|
|
|
_, exists := ext.Requirements[signal.NodeID]
|
|
|
|
|
if exists == true {
|
|
|
|
|
messages = append(messages, SendMsg{source, NewErrorSignal(signal.ID(), "already_requirement")})
|
|
|
|
|
messages = append(messages, Message{source, NewErrorSignal(signal.ID(), "already_requirement")})
|
|
|
|
|
} else {
|
|
|
|
|
if ext.Requirements == nil {
|
|
|
|
|
ext.Requirements = map[NodeID]ReqState{}
|
|
|
|
|
}
|
|
|
|
|
ext.Requirements[signal.NodeID] = Unlocked
|
|
|
|
|
changes.Add("requirements")
|
|
|
|
|
messages = append(messages, SendMsg{source, NewSuccessSignal(signal.ID())})
|
|
|
|
|
changes = append(changes, "requirements")
|
|
|
|
|
messages = append(messages, Message{source, NewSuccessSignal(signal.ID())})
|
|
|
|
|
}
|
|
|
|
|
case "remove":
|
|
|
|
|
_, exists := ext.Requirements[signal.NodeID]
|
|
|
|
|
if exists == false {
|
|
|
|
|
messages = append(messages, SendMsg{source, NewErrorSignal(signal.ID(), "can't link: not_requirement")})
|
|
|
|
|
messages = append(messages, Message{source, NewErrorSignal(signal.ID(), "not_requirement")})
|
|
|
|
|
} else {
|
|
|
|
|
delete(ext.Requirements, signal.NodeID)
|
|
|
|
|
changes.Add("requirements")
|
|
|
|
|
messages = append(messages, SendMsg{source, NewSuccessSignal(signal.ID())})
|
|
|
|
|
changes = append(changes, "requirements")
|
|
|
|
|
messages = append(messages, Message{source, NewSuccessSignal(signal.ID())})
|
|
|
|
|
}
|
|
|
|
|
default:
|
|
|
|
|
messages = append(messages, SendMsg{source, NewErrorSignal(signal.ID(), "unknown_action")})
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
messages = append(messages, SendMsg{source, NewErrorSignal(signal.ID(), "not_unlocked")})
|
|
|
|
|
messages = append(messages, Message{source, NewErrorSignal(signal.ID(), "unknown_action")})
|
|
|
|
|
}
|
|
|
|
|
return messages, changes
|
|
|
|
|
default:
|
|
|
|
|
messages = append(messages, Message{source, NewErrorSignal(signal.ID(), "not_unlocked: %s", ext.State)})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (ext *LockableExt) HandleSuccessSignal(ctx *Context, node *Node, source NodeID, signal *SuccessSignal) ([]SendMsg, Changes) {
|
|
|
|
|
var messages []SendMsg = nil
|
|
|
|
|
var changes = Changes{}
|
|
|
|
|
if source == node.ID {
|
|
|
|
|
return messages, changes
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
info, info_found := node.ProcessResponse(ext.WaitInfos, signal)
|
|
|
|
|
if info_found == true {
|
|
|
|
|
state, found := ext.Requirements[info.Destination]
|
|
|
|
|
if found == false {
|
|
|
|
|
ctx.Log.Logf("lockable", "Got success signal for requirement that is no longer in the map(%s), ignoring...", info.Destination)
|
|
|
|
|
} else {
|
|
|
|
|
ctx.Log.Logf("lockable", "got mapped response %+v for %+v in state %s", signal, info, ReqStateStrings[state])
|
|
|
|
|
switch state {
|
|
|
|
|
case Locking:
|
|
|
|
|
// Handle an UnlockSignal by either transitioning to Unlocked state,
|
|
|
|
|
// sending unlock signals to requirements, or returning an error signal
|
|
|
|
|
func (ext *LockableExt) HandleUnlockSignal(ctx *Context, node *Node, source NodeID, signal *UnlockSignal) ([]Message, Changes) {
|
|
|
|
|
var messages []Message = nil
|
|
|
|
|
var changes Changes = nil
|
|
|
|
|
|
|
|
|
|
switch ext.State {
|
|
|
|
|
case Locking:
|
|
|
|
|
ext.Requirements[info.Destination] = Locked
|
|
|
|
|
locked := 0
|
|
|
|
|
for _, s := range(ext.Requirements) {
|
|
|
|
|
if s == Locked {
|
|
|
|
|
locked += 1
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
changes.Add("state", "owner", "requirements")
|
|
|
|
|
messages = append(messages, SendMsg{*ext.Owner, NewSuccessSignal(ext.PendingID)})
|
|
|
|
|
case Locked:
|
|
|
|
|
if source != *ext.Owner {
|
|
|
|
|
messages = append(messages, Message{source, NewErrorSignal(signal.Id, "not_owner")})
|
|
|
|
|
} else {
|
|
|
|
|
changes.Add("requirements")
|
|
|
|
|
ctx.Log.Logf("lockable", "PARTIAL LOCK: %s - %d/%d", node.ID, locked, len(ext.Requirements))
|
|
|
|
|
}
|
|
|
|
|
case AbortingLock:
|
|
|
|
|
ext.Requirements[info.Destination] = Unlocking
|
|
|
|
|
|
|
|
|
|
lock_signal := NewLockSignal("unlock")
|
|
|
|
|
ext.WaitInfos[lock_signal.Id] = node.QueueTimeout("unlock", info.Destination, lock_signal, 100*time.Millisecond)
|
|
|
|
|
messages = append(messages, SendMsg{info.Destination, lock_signal})
|
|
|
|
|
if len(ext.Requirements) == 0 {
|
|
|
|
|
changes = append(changes, "state", "owner", "pending_owner")
|
|
|
|
|
|
|
|
|
|
ctx.Log.Logf("lockable", "sending abort_lock to %s for %s", info.Destination, node.ID)
|
|
|
|
|
}
|
|
|
|
|
case AbortingLock:
|
|
|
|
|
ctx.Log.Logf("lockable", "Got success signal in AbortingLock %s", node.ID)
|
|
|
|
|
fallthrough
|
|
|
|
|
case Unlocking:
|
|
|
|
|
ext.Requirements[source] = Unlocked
|
|
|
|
|
ext.Owner = nil
|
|
|
|
|
|
|
|
|
|
unlocked := 0
|
|
|
|
|
for _, s := range(ext.Requirements) {
|
|
|
|
|
if s == Unlocked {
|
|
|
|
|
unlocked += 1
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
ext.PendingOwner = nil
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
changes.Add("state", "owner", "req_id")
|
|
|
|
|
messages = append(messages, SendMsg{previous_owner, NewSuccessSignal(ext.PendingID)})
|
|
|
|
|
} else if old_state == AbortingLock {
|
|
|
|
|
changes.Add("state", "pending_owner")
|
|
|
|
|
messages = append(messages, SendMsg{*ext.PendingOwner, NewErrorSignal(*ext.ReqID, "not_unlocked")})
|
|
|
|
|
ext.PendingOwner = ext.Owner
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
messages = append(messages, Message{source, NewSuccessSignal(signal.Id)})
|
|
|
|
|
} else {
|
|
|
|
|
changes.Add("state")
|
|
|
|
|
ctx.Log.Logf("lockable", "PARTIAL UNLOCK: %s - %d/%d", node.ID, unlocked, len(ext.Requirements))
|
|
|
|
|
changes = append(changes, "state", "waiting", "requirements", "pending_owner")
|
|
|
|
|
|
|
|
|
|
ext.PendingOwner = nil
|
|
|
|
|
|
|
|
|
|
ext.ReqID = &signal.Id
|
|
|
|
|
|
|
|
|
|
ext.State = Unlocking
|
|
|
|
|
for id := range(ext.Requirements) {
|
|
|
|
|
unlock_signal := NewUnlockSignal()
|
|
|
|
|
|
|
|
|
|
ext.Waiting[unlock_signal.Id] = id
|
|
|
|
|
ext.Requirements[id] = Unlocking
|
|
|
|
|
|
|
|
|
|
messages = append(messages, Message{id, unlock_signal})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
default:
|
|
|
|
|
messages = append(messages, Message{source, NewErrorSignal(signal.Id, "not_locked")})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return messages, changes
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Handle a LockSignal and update the extensions owner/requirement states
|
|
|
|
|
func (ext *LockableExt) HandleLockSignal(ctx *Context, node *Node, source NodeID, signal *LockSignal) ([]SendMsg, Changes) {
|
|
|
|
|
var messages []SendMsg = nil
|
|
|
|
|
var changes = Changes{}
|
|
|
|
|
// Handle a LockSignal by either transitioning to a locked state,
|
|
|
|
|
// sending lock signals to requirements, or returning an error signal
|
|
|
|
|
func (ext *LockableExt) HandleLockSignal(ctx *Context, node *Node, source NodeID, signal *LockSignal) ([]Message, Changes) {
|
|
|
|
|
var messages []Message = nil
|
|
|
|
|
var changes Changes = nil
|
|
|
|
|
|
|
|
|
|
switch signal.State {
|
|
|
|
|
case "lock":
|
|
|
|
|
switch ext.State {
|
|
|
|
|
case Unlocked:
|
|
|
|
|
if len(ext.Requirements) == 0 {
|
|
|
|
|
changes = append(changes, "state", "owner", "pending_owner")
|
|
|
|
|
|
|
|
|
|
ext.Owner = &source
|
|
|
|
|
|
|
|
|
|
ext.PendingOwner = &source
|
|
|
|
|
|
|
|
|
|
ext.State = Locked
|
|
|
|
|
new_owner := source
|
|
|
|
|
ext.PendingOwner = &new_owner
|
|
|
|
|
ext.Owner = &new_owner
|
|
|
|
|
changes.Add("state", "pending_owner", "owner")
|
|
|
|
|
messages = append(messages, SendMsg{new_owner, NewSuccessSignal(signal.ID())})
|
|
|
|
|
messages = append(messages, Message{source, NewSuccessSignal(signal.Id)})
|
|
|
|
|
} else {
|
|
|
|
|
changes = append(changes, "state", "requirements", "waiting", "pending_owner")
|
|
|
|
|
|
|
|
|
|
ext.PendingOwner = &source
|
|
|
|
|
|
|
|
|
|
ext.ReqID = &signal.Id
|
|
|
|
|
|
|
|
|
|
ext.State = Locking
|
|
|
|
|
id := signal.ID()
|
|
|
|
|
ext.ReqID = &id
|
|
|
|
|
new_owner := source
|
|
|
|
|
ext.PendingOwner = &new_owner
|
|
|
|
|
ext.PendingID = signal.ID()
|
|
|
|
|
changes.Add("state", "req_id", "pending_owner", "pending_id")
|
|
|
|
|
for id, state := range(ext.Requirements) {
|
|
|
|
|
if state != Unlocked {
|
|
|
|
|
ctx.Log.Logf("lockable", "REQ_NOT_UNLOCKED_WHEN_LOCKING")
|
|
|
|
|
}
|
|
|
|
|
for id := range(ext.Requirements) {
|
|
|
|
|
lock_signal := NewLockSignal()
|
|
|
|
|
|
|
|
|
|
lock_signal := NewLockSignal("lock")
|
|
|
|
|
ext.WaitInfos[lock_signal.Id] = node.QueueTimeout("lock", id, lock_signal, 500*time.Millisecond)
|
|
|
|
|
ext.Waiting[lock_signal.Id] = id
|
|
|
|
|
ext.Requirements[id] = Locking
|
|
|
|
|
|
|
|
|
|
messages = append(messages, SendMsg{id, lock_signal})
|
|
|
|
|
messages = append(messages, Message{id, lock_signal})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
default:
|
|
|
|
|
messages = append(messages, SendMsg{source, NewErrorSignal(signal.ID(), "not_unlocked")})
|
|
|
|
|
ctx.Log.Logf("lockable", "Tried to lock %s while %s", node.ID, ext.State)
|
|
|
|
|
messages = append(messages, Message{source, NewErrorSignal(signal.Id, "not_unlocked: %s", ext.State)})
|
|
|
|
|
}
|
|
|
|
|
case "unlock":
|
|
|
|
|
if ext.State == Locked {
|
|
|
|
|
if len(ext.Requirements) == 0 {
|
|
|
|
|
|
|
|
|
|
return messages, changes
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Handle an error signal by aborting the lock, or retrying the unlock
|
|
|
|
|
func (ext *LockableExt) HandleErrorSignal(ctx *Context, node *Node, source NodeID, signal *ErrorSignal) ([]Message, Changes) {
|
|
|
|
|
var messages []Message = nil
|
|
|
|
|
var changes Changes = nil
|
|
|
|
|
|
|
|
|
|
id, waiting := ext.Waiting[signal.ReqID]
|
|
|
|
|
if waiting == true {
|
|
|
|
|
delete(ext.Waiting, signal.ReqID)
|
|
|
|
|
changes = append(changes, "waiting")
|
|
|
|
|
|
|
|
|
|
switch ext.State {
|
|
|
|
|
case Locking:
|
|
|
|
|
changes = append(changes, "state", "requirements")
|
|
|
|
|
|
|
|
|
|
ext.Requirements[id] = Unlocked
|
|
|
|
|
|
|
|
|
|
unlocked := 0
|
|
|
|
|
for req_id, req_state := range(ext.Requirements) {
|
|
|
|
|
// Unlock locked requirements, and count unlocked requirements
|
|
|
|
|
switch req_state {
|
|
|
|
|
case Locked:
|
|
|
|
|
unlock_signal := NewUnlockSignal()
|
|
|
|
|
|
|
|
|
|
ext.Waiting[unlock_signal.Id] = req_id
|
|
|
|
|
ext.Requirements[req_id] = Unlocking
|
|
|
|
|
|
|
|
|
|
messages = append(messages, Message{req_id, unlock_signal})
|
|
|
|
|
case Unlocked:
|
|
|
|
|
unlocked += 1
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if unlocked == len(ext.Requirements) {
|
|
|
|
|
changes = append(changes, "owner", "state")
|
|
|
|
|
ext.State = Unlocked
|
|
|
|
|
new_owner := source
|
|
|
|
|
ext.PendingOwner = nil
|
|
|
|
|
ext.Owner = nil
|
|
|
|
|
changes.Add("state", "pending_owner", "owner")
|
|
|
|
|
messages = append(messages, SendMsg{new_owner, NewSuccessSignal(signal.ID())})
|
|
|
|
|
} else if source == *ext.Owner {
|
|
|
|
|
ext.State = Unlocking
|
|
|
|
|
id := signal.ID()
|
|
|
|
|
ext.ReqID = &id
|
|
|
|
|
ext.PendingOwner = nil
|
|
|
|
|
ext.PendingID = signal.ID()
|
|
|
|
|
changes.Add("state", "pending_owner", "pending_id", "req_id")
|
|
|
|
|
for id, state := range(ext.Requirements) {
|
|
|
|
|
if state != Locked {
|
|
|
|
|
ctx.Log.Logf("lockable", "REQ_NOT_LOCKED_WHEN_UNLOCKING")
|
|
|
|
|
} else {
|
|
|
|
|
changes = append(changes, "state")
|
|
|
|
|
ext.State = AbortingLock
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
lock_signal := NewLockSignal("unlock")
|
|
|
|
|
ext.WaitInfos[lock_signal.Id] = node.QueueTimeout("unlock", id, lock_signal, 100*time.Millisecond)
|
|
|
|
|
ext.Requirements[id] = Unlocking
|
|
|
|
|
case Unlocking:
|
|
|
|
|
unlock_signal := NewUnlockSignal()
|
|
|
|
|
ext.Waiting[unlock_signal.Id] = id
|
|
|
|
|
messages = append(messages, Message{id, unlock_signal})
|
|
|
|
|
|
|
|
|
|
messages = append(messages, SendMsg{id, lock_signal})
|
|
|
|
|
case AbortingLock:
|
|
|
|
|
req_state := ext.Requirements[id]
|
|
|
|
|
// Mark failed lock as Unlocked, or retry unlock
|
|
|
|
|
switch req_state {
|
|
|
|
|
case Locking:
|
|
|
|
|
ext.Requirements[id] = Unlocked
|
|
|
|
|
|
|
|
|
|
// Check if all requirements unlocked now
|
|
|
|
|
unlocked := 0
|
|
|
|
|
for _, req_state := range(ext.Requirements) {
|
|
|
|
|
if req_state == Unlocked {
|
|
|
|
|
unlocked += 1
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
messages = append(messages, SendMsg{source, NewErrorSignal(signal.ID(), "not_locked")})
|
|
|
|
|
|
|
|
|
|
if unlocked == len(ext.Requirements) {
|
|
|
|
|
changes = append(changes, "owner", "state")
|
|
|
|
|
ext.State = Unlocked
|
|
|
|
|
ext.Owner = nil
|
|
|
|
|
}
|
|
|
|
|
case Unlocking:
|
|
|
|
|
// Handle error for unlocking requirement while unlocking by retrying unlock
|
|
|
|
|
unlock_signal := NewUnlockSignal()
|
|
|
|
|
ext.Waiting[unlock_signal.Id] = id
|
|
|
|
|
messages = append(messages, Message{id, unlock_signal})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
default:
|
|
|
|
|
ctx.Log.Logf("lockable", "LOCK_ERR: unkown state %s", signal.State)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return messages, changes
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (ext *LockableExt) HandleTimeoutSignal(ctx *Context, node *Node, source NodeID, signal *TimeoutSignal) ([]SendMsg, Changes) {
|
|
|
|
|
var messages []SendMsg = nil
|
|
|
|
|
var changes = Changes{}
|
|
|
|
|
// Handle a success signal by checking if all requirements have been locked/unlocked
|
|
|
|
|
func (ext *LockableExt) HandleSuccessSignal(ctx *Context, node *Node, source NodeID, signal *SuccessSignal) ([]Message, Changes) {
|
|
|
|
|
var messages []Message = nil
|
|
|
|
|
var changes Changes = nil
|
|
|
|
|
|
|
|
|
|
id, waiting := ext.Waiting[signal.ReqID]
|
|
|
|
|
if waiting == true {
|
|
|
|
|
delete(ext.Waiting, signal.ReqID)
|
|
|
|
|
changes = append(changes, "waiting")
|
|
|
|
|
|
|
|
|
|
wait_info, found := node.ProcessResponse(ext.WaitInfos, signal)
|
|
|
|
|
if found == true {
|
|
|
|
|
changes.Add("wait_infos")
|
|
|
|
|
state, found := ext.Requirements[wait_info.Destination]
|
|
|
|
|
if found == true {
|
|
|
|
|
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("state")
|
|
|
|
|
ext.State = Unlocked
|
|
|
|
|
case Locking:
|
|
|
|
|
ext.Requirements[id] = Locked
|
|
|
|
|
ext.Locked[id] = nil
|
|
|
|
|
delete(ext.Unlocked, id)
|
|
|
|
|
|
|
|
|
|
if len(ext.Locked) == len(ext.Requirements) {
|
|
|
|
|
ctx.Log.Logf("lockable", "%s FULL_LOCK: %d", node.ID, len(ext.Locked))
|
|
|
|
|
changes = append(changes, "state", "owner", "req_id")
|
|
|
|
|
ext.State = Locked
|
|
|
|
|
|
|
|
|
|
ext.Owner = ext.PendingOwner
|
|
|
|
|
|
|
|
|
|
messages = append(messages, Message{*ext.Owner, NewSuccessSignal(*ext.ReqID)})
|
|
|
|
|
ext.ReqID = nil
|
|
|
|
|
} else {
|
|
|
|
|
ctx.Log.Logf("lockable", "%s PARTIAL_LOCK: %d/%d", node.ID, len(ext.Locked), len(ext.Requirements))
|
|
|
|
|
}
|
|
|
|
|
case AbortingLock:
|
|
|
|
|
req_state := ext.Requirements[id]
|
|
|
|
|
switch req_state {
|
|
|
|
|
case Locking:
|
|
|
|
|
ext.State = AbortingLock
|
|
|
|
|
ext.Requirements[wait_info.Destination] = Unlocked
|
|
|
|
|
for id, state := range(ext.Requirements) {
|
|
|
|
|
if state == Locked {
|
|
|
|
|
ext.Requirements[id] = Unlocking
|
|
|
|
|
lock_signal := NewLockSignal("unlock")
|
|
|
|
|
ext.WaitInfos[lock_signal.Id] = node.QueueTimeout("unlock", id, lock_signal, 100*time.Millisecond)
|
|
|
|
|
messages = append(messages, SendMsg{id, lock_signal})
|
|
|
|
|
ctx.Log.Logf("lockable", "sent abort unlock to %s from %s", id, node.ID)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
unlock_signal := NewUnlockSignal()
|
|
|
|
|
ext.Waiting[unlock_signal.Id] = id
|
|
|
|
|
messages = append(messages, Message{id, unlock_signal})
|
|
|
|
|
case Unlocking:
|
|
|
|
|
ext.Requirements[wait_info.Destination] = Locked
|
|
|
|
|
all_returned := true
|
|
|
|
|
for _, state := range(ext.Requirements) {
|
|
|
|
|
if state == Unlocking {
|
|
|
|
|
all_returned = false
|
|
|
|
|
break
|
|
|
|
|
ext.Requirements[id] = Unlocked
|
|
|
|
|
ext.Unlocked[id] = nil
|
|
|
|
|
delete(ext.Locked, id)
|
|
|
|
|
|
|
|
|
|
unlocked := 0
|
|
|
|
|
for _, req_state := range(ext.Requirements) {
|
|
|
|
|
switch req_state {
|
|
|
|
|
case Unlocked:
|
|
|
|
|
unlocked += 1
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if all_returned == true {
|
|
|
|
|
ext.State = Locked
|
|
|
|
|
|
|
|
|
|
if unlocked == len(ext.Requirements) {
|
|
|
|
|
changes = append(changes, "state", "pending_owner", "req_id")
|
|
|
|
|
|
|
|
|
|
messages = append(messages, Message{*ext.PendingOwner, NewErrorSignal(*ext.ReqID, "not_unlocked: %s", ext.State)})
|
|
|
|
|
ext.State = Unlocked
|
|
|
|
|
ext.ReqID = nil
|
|
|
|
|
ext.PendingOwner = nil
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
ctx.Log.Logf("lockable", "%s timed out", wait_info.Destination)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
case Unlocking:
|
|
|
|
|
ext.Requirements[id] = Unlocked
|
|
|
|
|
ext.Unlocked[id] = Unlocked
|
|
|
|
|
delete(ext.Locked, id)
|
|
|
|
|
|
|
|
|
|
if len(ext.Unlocked) == len(ext.Requirements) {
|
|
|
|
|
changes = append(changes, "state", "owner", "req_id")
|
|
|
|
|
|
|
|
|
|
messages = append(messages, Message{*ext.Owner, NewSuccessSignal(*ext.ReqID)})
|
|
|
|
|
ext.State = Unlocked
|
|
|
|
|
ext.ReqID = nil
|
|
|
|
|
ext.Owner = nil
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return messages, changes
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// LockableExts process status signals by forwarding them to it's owner
|
|
|
|
|
// LockSignal and LinkSignal Direct signals are processed to update the requirement/dependency/lock state
|
|
|
|
|
func (ext *LockableExt) Process(ctx *Context, node *Node, source NodeID, signal Signal) ([]SendMsg, Changes) {
|
|
|
|
|
var messages []SendMsg = nil
|
|
|
|
|
var changes = Changes{}
|
|
|
|
|
func (ext *LockableExt) Process(ctx *Context, node *Node, source NodeID, signal Signal) ([]Message, Changes) {
|
|
|
|
|
var messages []Message = nil
|
|
|
|
|
var changes Changes = nil
|
|
|
|
|
|
|
|
|
|
switch sig := signal.(type) {
|
|
|
|
|
case *StatusSignal:
|
|
|
|
|
// Forward StatusSignals up to the owner(unless that would be a cycle)
|
|
|
|
|
if ext.Owner != nil {
|
|
|
|
|
if *ext.Owner != node.ID {
|
|
|
|
|
messages = append(messages, SendMsg{*ext.Owner, signal})
|
|
|
|
|
messages = append(messages, Message{*ext.Owner, signal})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
case *LinkSignal:
|
|
|
|
|
messages, changes = ext.HandleLinkSignal(ctx, node, source, sig)
|
|
|
|
|
case *LockSignal:
|
|
|
|
|
messages, changes = ext.HandleLockSignal(ctx, node, source, sig)
|
|
|
|
|
case *UnlockSignal:
|
|
|
|
|
messages, changes = ext.HandleUnlockSignal(ctx, node, source, sig)
|
|
|
|
|
case *ErrorSignal:
|
|
|
|
|
messages, changes = ext.HandleErrorSignal(ctx, node, source, sig)
|
|
|
|
|
case *SuccessSignal:
|
|
|
|
|
messages, changes = ext.HandleSuccessSignal(ctx, node, source, sig)
|
|
|
|
|
case *TimeoutSignal:
|
|
|
|
|
messages, changes = ext.HandleTimeoutSignal(ctx, node, source, sig)
|
|
|
|
|
default:
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return messages, changes
|
|
|
|
|