Skip to content

Commit

Permalink
address review feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
woodsaj authored and tomwilkie committed Feb 5, 2019
1 parent 329ed9a commit ceb9205
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 31 deletions.
11 changes: 2 additions & 9 deletions pkg/loki/loki.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ func (t *Loki) setupAuthMiddleware() {

func (t *Loki) init(m moduleName) error {
// initialize all of our dependencies first
for _, dep := range getDeps(m) {
for _, dep := range orderedDeps(m) {
if err := t.initModule(dep); err != nil {
return err
}
Expand All @@ -122,10 +122,6 @@ func (t *Loki) init(m moduleName) error {
}

func (t *Loki) initModule(m moduleName) error {
if _, ok := t.inited[m]; ok {
return nil
}

level.Info(util.Logger).Log("msg", "initialising", "module", m)
if modules[m].init != nil {
if err := modules[m].init(t); err != nil {
Expand All @@ -151,17 +147,14 @@ func (t *Loki) Stop() error {

func (t *Loki) stop(m moduleName) {
t.stopModule(m)
deps := getDeps(m)
deps := orderedDeps(m)
// iterate over our deps in reverse order and call stopModule
for i := len(deps) - 1; i >= 0; i-- {
t.stopModule(deps[i])
}
}

func (t *Loki) stopModule(m moduleName) {
if _, ok := t.inited[m]; !ok {
return
}
level.Info(util.Logger).Log("msg", "stopping", "module", m)
if modules[m].stop != nil {
if err := modules[m].stop(t); err != nil {
Expand Down
39 changes: 18 additions & 21 deletions pkg/loki/modules.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,41 +187,38 @@ func listDeps(m moduleName) []moduleName {
return deps
}

// getDeps gets a list of all dependencies ordered so that items are always after any of their dependencies.
func getDeps(m moduleName) []moduleName {
// orderedDeps gets a list of all dependencies ordered so that items are always after any of their dependencies.
func orderedDeps(m moduleName) []moduleName {
deps := listDeps(m)

// get a unique list of moduleNames, with a flag for whether they have been added to our result
uniq := make(map[moduleName]bool)
for _, d := range deps {
uniq[d] = false
uniq := map[moduleName]bool{}
for _, dep := range deps {
uniq[dep] = false
}

result := make([]moduleName, len(uniq))
numAdded := 0
result := make([]moduleName, 0)

// keep looping through all modules until they have all been added to the result.
for numAdded < len(uniq) {
for d, s := range uniq {
if s {

for len(result) < len(uniq) {
OUTER:
for name, added := range uniq {
if added {
continue
}
// check if all dependencies have been added to the result
allDepsAdded := true
for _, dep := range modules[d].deps {
for _, dep := range modules[name].deps {
// stop processing this module if one of its dependencies has
// not been added to the result yet.
if !uniq[dep] {
allDepsAdded = false
break
continue OUTER
}
}

// if all of the module's dependencies have been added the result slice,
// if all of the module's dependencies have been added to the result slice,
// then we can safely add this module to the result slice as well.
if allDepsAdded {
uniq[d] = true
result[numAdded] = d
numAdded++
}
uniq[name] = true
result = append(result, name)
}
}
return result
Expand Down
2 changes: 1 addition & 1 deletion pkg/loki/modules_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (

func TestGetDeps(t *testing.T) {
for _, m := range []moduleName{All, Distributor, Ingester, Querier} {
deps := getDeps(m)
deps := orderedDeps(m)
seen := make(map[moduleName]struct{})
// make sure that getDeps always orders dependencies correctly.
for _, d := range deps {
Expand Down

0 comments on commit ceb9205

Please sign in to comment.