Skip to content
This repository has been archived by the owner on Jan 30, 2020. It is now read-only.

Commit

Permalink
registry: remove extra loops when fetching Jobs list from etcd
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Djalal Harouni committed Apr 11, 2016
1 parent d798e24 commit 93d3630
Showing 1 changed file with 3 additions and 12 deletions.
15 changes: 3 additions & 12 deletions registry/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand Down Expand Up @@ -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 {
Expand All @@ -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
Expand Down

0 comments on commit 93d3630

Please sign in to comment.