pgraph: Move the timestamp storage into the resource

This commit is contained in:
James Shubin
2017-05-15 12:27:36 -04:00
parent b1e035f96a
commit fbcb562781
3 changed files with 23 additions and 21 deletions

View File

@@ -35,17 +35,6 @@ import (
"golang.org/x/time/rate"
)
// GetTimestamp returns the timestamp of a vertex
func (v *Vertex) GetTimestamp() int64 {
return v.timestamp
}
// UpdateTimestamp updates the timestamp on a vertex and returns the new value
func (v *Vertex) UpdateTimestamp() int64 {
v.timestamp = time.Now().UnixNano() // update
return v.timestamp
}
// OKTimestamp returns true if this element can run right now?
func (g *Graph) OKTimestamp(v *Vertex) bool {
// these are all the vertices pointing TO v, eg: ??? -> v
@@ -54,7 +43,7 @@ func (g *Graph) OKTimestamp(v *Vertex) bool {
// then we can't run right now...
// if they're equal (eg: on init of 0) then we also can't run
// b/c we should let our pre-req's go first...
x, y := v.GetTimestamp(), n.GetTimestamp()
x, y := v.Res.Timestamp(), n.Res.Timestamp()
if b, ok := g.Value("debug"); ok && util.Bool(b) {
log.Printf("%s[%s]: OKTimestamp: (%v) >= %s[%s](%v): !%v", v.GetKind(), v.GetName(), x, n.GetKind(), n.GetName(), y, x >= y)
}
@@ -109,7 +98,7 @@ func (g *Graph) BackPoke(v *Vertex) {
var wg sync.WaitGroup
// these are all the vertices pointing TO v, eg: ??? -> v
for _, n := range g.IncomingGraphVertices(v) {
x, y, s := v.GetTimestamp(), n.GetTimestamp(), n.Res.GetState()
x, y, s := v.Res.Timestamp(), n.Res.Timestamp(), n.Res.GetState()
// If the parent timestamp needs poking AND it's not running
// Process, then poke it. If the parent is in ResStateProcess it
// means that an event is pending, so we'll be expecting a poke
@@ -188,7 +177,7 @@ func (g *Graph) Process(v *Vertex) error {
}
// timestamp must be okay...
if b, ok := g.Value("debug"); ok && util.Bool(b) {
log.Printf("%s[%s]: OKTimestamp(%v)", obj.GetKind(), obj.GetName(), v.GetTimestamp())
log.Printf("%s[%s]: OKTimestamp(%v)", obj.GetKind(), obj.GetName(), v.Res.Timestamp())
}
// semaphores!
@@ -317,7 +306,7 @@ func (g *Graph) Process(v *Vertex) error {
// update this timestamp *before* we poke or the poked
// nodes might fail due to having a too old timestamp!
v.UpdateTimestamp() // this was touched...
v.Res.UpdateTimestamp() // this was touched...
obj.SetState(resources.ResStatePoking) // can't cancel parent poke
if err := g.Poke(v); err != nil {
return errwrap.Wrapf(err, "the Poke() failed")

View File

@@ -49,7 +49,6 @@ type Graph struct {
// Vertex is the primary vertex struct in this library.
type Vertex struct {
resources.Res // anonymous field
timestamp int64 // last updated timestamp ?
}
// Edge is the primary edge struct in this library.

View File

@@ -190,6 +190,8 @@ type Base interface {
ConvergerUIDs() (converger.UID, converger.UID, converger.UID)
GetState() ResState
SetState(ResState)
Timestamp() int64
UpdateTimestamp() int64
Event() error
SendEvent(event.Kind, error) error
ReadEvent(*event.Event) (*error, bool)
@@ -240,6 +242,7 @@ type BaseRes struct {
Kind string
data Data
timestamp int64 // last updated timestamp
state ResState
prefix string // base prefix for this resource
@@ -503,6 +506,17 @@ func (obj *BaseRes) SetState(state ResState) {
obj.state = state
}
// Timestamp returns the timestamp of a resource.
func (obj *BaseRes) Timestamp() int64 {
return obj.timestamp
}
// UpdateTimestamp updates the timestamp and returns the new value.
func (obj *BaseRes) UpdateTimestamp() int64 {
obj.timestamp = time.Now().UnixNano() // update
return obj.timestamp
}
// IsStateOK returns the cached state value.
func (obj *BaseRes) IsStateOK() bool {
return obj.isStateOK