From 93d3630c197a9c8ccdae5f4b68c3fedfaa5e46ca Mon Sep 17 00:00:00 2001 From: Djalal Harouni Date: Mon, 11 Apr 2016 09:12:46 +0100 Subject: [PATCH] registry: remove extra loops when fetching Jobs list from etcd Improve Units() call when we fetch and process Jobs and Units from etcd. Remove extra unnecessary loops. We also change the code logic a little bit, since it was always storing the last matched name with the new Unit, but since Job keys do not expect to have two units with the same name, this should be ok. --- registry/job.go | 15 +++------------ 1 file changed, 3 insertions(+), 12 deletions(-) diff --git a/registry/job.go b/registry/job.go index 8eeda2280..ca16282eb 100644 --- a/registry/job.go +++ b/registry/job.go @@ -90,6 +90,7 @@ func (r *EtcdRegistry) Schedule() ([]job.ScheduledUnit, error) { func (r *EtcdRegistry) Units() ([]job.Unit, error) { key := r.prefixed(jobPrefix) opts := &etcd.GetOptions{ + // We need Job Units to be sorted Sort: true, Recursive: true, } @@ -117,7 +118,7 @@ func (r *EtcdRegistry) Units() ([]job.Unit, error) { return unit } - uMap := make(map[string]*job.Unit) + units := make([]job.Unit, 0) for _, dir := range res.Node.Nodes { u, err := r.dirToUnit(dir, unitHashLookupFunc) if err != nil { @@ -127,18 +128,8 @@ func (r *EtcdRegistry) Units() ([]job.Unit, error) { if u == nil { continue } - uMap[u.Name] = u - } - - var sortable sort.StringSlice - for name, _ := range uMap { - sortable = append(sortable, name) - } - sortable.Sort() - units := make([]job.Unit, 0, len(sortable)) - for _, name := range sortable { - units = append(units, *uMap[name]) + units = append(units, *u) } return units, nil