Skip to content

Commit

Permalink
start and stop modules in correct order
Browse files Browse the repository at this point in the history
When starting up, start all dependencies first then start the target
module.  When stopping, reverse the order to ensure we dont try and
stop a module while there is still something running that needs it.

fixes #105
  • Loading branch information
woodsaj authored and tomwilkie committed Feb 5, 2019
1 parent 899faf1 commit 329ed9a
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 18 deletions.
41 changes: 24 additions & 17 deletions pkg/loki/loki.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,15 +111,20 @@ func (t *Loki) setupAuthMiddleware() {
}

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

for _, dep := range modules[m].deps {
if err := t.init(dep); err != nil {
// initialize all of our dependencies first
for _, dep := range getDeps(m) {
if err := t.initModule(dep); err != nil {
return err
}
}
// lastly, initialize the requested module
return t.initModule(m)
}

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 {
Expand All @@ -145,21 +150,23 @@ func (t *Loki) Stop() error {
}

func (t *Loki) stop(m moduleName) {
if _, ok := t.inited[m]; !ok {
return
}
delete(t.inited, m)

for _, dep := range modules[m].deps {
t.stop(dep)
t.stopModule(m)
deps := getDeps(m)
// iterate over our deps in reverse order and call stopModule
for i := len(deps) - 1; i >= 0; i-- {
t.stopModule(deps[i])
}
}

if modules[m].stop == nil {
func (t *Loki) stopModule(m moduleName) {
if _, ok := t.inited[m]; !ok {
return
}

level.Info(util.Logger).Log("msg", "stopping", "module", m)
if err := modules[m].stop(t); err != nil {
level.Error(util.Logger).Log("msg", "error stopping", "module", m, "err", err)
if modules[m].stop != nil {
if err := modules[m].stop(t); err != nil {
level.Error(util.Logger).Log("msg", "error stopping", "module", m, "err", err)
}
}
delete(t.inited, m)
}
51 changes: 50 additions & 1 deletion pkg/loki/modules.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func (m moduleName) String() string {
case All:
return "all"
default:
panic(fmt.Sprintf("unknow module name: %d", m))
panic(fmt.Sprintf("unknown module name: %d", m))
}
}

Expand Down Expand Up @@ -178,6 +178,55 @@ func (t *Loki) stopStore() error {
return nil
}

// listDeps recursively gets a list of dependencies for a passed moduleName
func listDeps(m moduleName) []moduleName {
deps := modules[m].deps
for _, d := range modules[m].deps {
deps = append(deps, listDeps(d)...)
}
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 {
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
}

result := make([]moduleName, len(uniq))
numAdded := 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 {
continue
}
// check if all dependencies have been added to the result
allDepsAdded := true
for _, dep := range modules[d].deps {
if !uniq[dep] {
allDepsAdded = false
break
}
}

// if all of the module's dependencies have been added 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++
}
}
}
return result
}

type module struct {
deps []moduleName
init func(t *Loki) error
Expand Down
21 changes: 21 additions & 0 deletions pkg/loki/modules_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package loki

import (
"testing"
)

func TestGetDeps(t *testing.T) {
for _, m := range []moduleName{All, Distributor, Ingester, Querier} {
deps := getDeps(m)
seen := make(map[moduleName]struct{})
// make sure that getDeps always orders dependencies correctly.
for _, d := range deps {
seen[d] = struct{}{}
for _, dep := range modules[d].deps {
if _, ok := seen[dep]; !ok {
t.Errorf("module %s has dependency %s which has not been seen.", d, dep)
}
}
}
}
}

0 comments on commit 329ed9a

Please sign in to comment.