From fbcb56278172c2fccddc108cdc8b8ffcab4a49a8 Mon Sep 17 00:00:00 2001 From: James Shubin Date: Mon, 15 May 2017 12:27:36 -0400 Subject: [PATCH] pgraph: Move the timestamp storage into the resource --- pgraph/actions.go | 19 ++++--------------- pgraph/pgraph.go | 3 +-- resources/resources.go | 22 ++++++++++++++++++---- 3 files changed, 23 insertions(+), 21 deletions(-) diff --git a/pgraph/actions.go b/pgraph/actions.go index 9dfaeee6..aa5634ae 100644 --- a/pgraph/actions.go +++ b/pgraph/actions.go @@ -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") diff --git a/pgraph/pgraph.go b/pgraph/pgraph.go index 2d2b945b..e803bce0 100644 --- a/pgraph/pgraph.go +++ b/pgraph/pgraph.go @@ -48,8 +48,7 @@ type Graph struct { // Vertex is the primary vertex struct in this library. type Vertex struct { - resources.Res // anonymous field - timestamp int64 // last updated timestamp ? + resources.Res // anonymous field } // Edge is the primary edge struct in this library. diff --git a/resources/resources.go b/resources/resources.go index feaa4325..b4cb2638 100644 --- a/resources/resources.go +++ b/resources/resources.go @@ -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) @@ -238,10 +240,11 @@ type BaseRes struct { MetaParams MetaParams `yaml:"meta"` // struct of all the metaparams Recv map[string]*Send // mapping of key to receive on from value - Kind string - data Data - state ResState - prefix string // base prefix for this resource + Kind string + data Data + timestamp int64 // last updated timestamp + state ResState + prefix string // base prefix for this resource eventsLock *sync.Mutex // locks around sending and closing of events channel eventsDone bool @@ -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