Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

state: reflect key Nomad data within internal store #30

Merged
merged 42 commits into from
Apr 29, 2020
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
99d4a86
WIP: work in-progress commit.
jrasell Mar 17, 2020
d83cdf7
Merge branch 'master' into f-blocking-query-track-policies
jrasell Mar 20, 2020
0a7e60d
WIP: autoscaler internal state store.
jrasell Mar 23, 2020
8eb9d7a
Merge branch 'master' into f-blocking-query-track-policies
jrasell Mar 23, 2020
560bfec
WIP: tidy go module dependancies and tidy agent import list.
jrasell Mar 23, 2020
e9e33f3
Merge branch 'master' into f-blocking-query-track-policies
jrasell Apr 7, 2020
c5d9be2
wip: additional changes based on testing.
jrasell Apr 7, 2020
3acfcef
WIP: update based on feedback.
jrasell Apr 8, 2020
70a5df7
Merge branch 'master' into f-blocking-query-track-policies
jrasell Apr 8, 2020
d39df92
Merge branch 'master' into f-blocking-query-track-policies
jrasell Apr 20, 2020
ee7681d
Merge remote-tracking branch 'origin/master' into f-blocking-query-tr…
lgfa29 Apr 20, 2020
dc50a17
Merge branch 'master' into f-blocking-query-track-policies
jrasell Apr 21, 2020
23616fd
WIP: further updates based on discussion and API updates.
jrasell Apr 22, 2020
4f833a0
Merge branch 'master' into f-blocking-query-track-policies
jrasell Apr 22, 2020
7bebc20
add evaluation_interval to policy
lgfa29 Apr 20, 2020
9c4c2b1
update docs
lgfa29 Apr 20, 2020
7aa2049
start implementing new control loop
lgfa29 Apr 23, 2020
71f14c9
WIP: update Nomad target plugin to track job status.
jrasell Apr 23, 2020
bdc136e
fix nomad target plugin deadlock
lgfa29 Apr 23, 2020
7ac24a8
improve code organization for the new control loop
lgfa29 Apr 24, 2020
50e30c7
WIP: Nomad target plugin updates amongst others.
jrasell Apr 24, 2020
f572045
improve code organization for the policy handler
lgfa29 Apr 24, 2020
cf2beb6
update agent to use new policy manager
lgfa29 Apr 24, 2020
e33f4a2
WIP: add docstrings to Nomad target plugin functions.
jrasell Apr 27, 2020
290876f
improve policy validation from Nomad and add tests
lgfa29 Apr 28, 2020
b8ed922
Merge branch 'master' into f-new-control-loop
lgfa29 Apr 28, 2020
538ea74
WIP: only append to multierror when needed in policy validate.
jrasell Apr 28, 2020
7ee1bdd
WIP: remove unused func argument to handler.generateEvaluation.
jrasell Apr 28, 2020
2972573
WIP: remove incorrect/old interface func comments.
jrasell Apr 28, 2020
016827b
remove previous state store implementation
lgfa29 Apr 28, 2020
3352aab
move parsing tests
lgfa29 Apr 28, 2020
839437f
validate policy source type
lgfa29 Apr 28, 2020
552204c
Merge branch 'master' into f-new-control-loop
lgfa29 Apr 28, 2020
9d1a637
fix master merge
lgfa29 Apr 28, 2020
375d4cb
update docstring
lgfa29 Apr 28, 2020
cc3c3b5
move pointer conversion functions to helper package
lgfa29 Apr 29, 2020
f5a99a6
move agent stop logic to a defer statement
lgfa29 Apr 29, 2020
c35a3cb
WIP: watcher should use 5 min blocking query.
jrasell Apr 29, 2020
fdd62f2
make sure handler is removed from internal state when stopped
lgfa29 Apr 29, 2020
b78f92c
fix wrong type error messages
lgfa29 Apr 29, 2020
94be19d
improve context handling when waiting for blocking queries
lgfa29 Apr 29, 2020
c30e919
fix object type in error message
lgfa29 Apr 29, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 9 additions & 19 deletions agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package agent
import (
"context"
"fmt"
"reflect"
"sync"
"time"

Expand All @@ -15,7 +14,8 @@ import (
"github.com/hashicorp/nomad-autoscaler/plugins/manager"
strategypkg "github.com/hashicorp/nomad-autoscaler/plugins/strategy"
targetpkg "github.com/hashicorp/nomad-autoscaler/plugins/target"
"github.com/hashicorp/nomad-autoscaler/policystorage"
"github.com/hashicorp/nomad-autoscaler/state"
"github.com/hashicorp/nomad-autoscaler/state/policy"
"github.com/hashicorp/nomad/api"
)

Expand All @@ -42,7 +42,8 @@ func (a *Agent) Run(ctx context.Context) error {
return err
}

ps := policystorage.Nomad{Client: a.nomadClient}
stateHandler := state.NewHandler(ctx, a.logger, a.nomadClient)
stateHandler.Start()

// launch plugins
if err := a.setupPlugins(); err != nil {
Expand All @@ -63,34 +64,23 @@ Loop:
for {
select {
case <-ticker.C:
logger := a.logger.With("policy_storage", reflect.TypeOf(ps))
logger.Info("reading policies")

// read policies
policies, err := ps.List()
if err != nil {
logger.Error("failed to fetch policies", "error", err)
continue
}
logger.Info(fmt.Sprintf("found %d policies", len(policies)))
policies := stateHandler.PolicyState.List()
a.logger.Info(fmt.Sprintf("found %d policies", len(policies)))

// handle policies
for _, p := range policies {
wg.Add(1)
go func(ID string) {
go func(policy *policy.Policy) {
defer wg.Done()
select {
case <-ctx.Done():
return
default:
policy, err := ps.Get(ID)
if err != nil {
logger.Error("failed to fetch policy", "policy_id", ID, "error", err)
return
}
a.handlePolicy(policy)
}
}(p.ID)
}(p)
}
wg.Wait()
case <-ctx.Done():
Expand Down Expand Up @@ -163,7 +153,7 @@ func (a *Agent) generateNomadClient() error {
return nil
}

func (a *Agent) handlePolicy(p *policystorage.Policy) {
func (a *Agent) handlePolicy(p *policy.Policy) {
logger := a.logger.With(
"policy_id", p.ID,
"source", p.Source,
Expand Down
20 changes: 0 additions & 20 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ github.com/json-iterator/go v1.1.9/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/u
github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w=
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFBFZlji/RkVcI2GknAs/DXo4wKdlNEc=
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pretty v0.2.0 h1:s5hAObm+yFO5uHYt5dYjxi2rXrsnmRpJx4OYvIWUaQs=
github.com/kr/pretty v0.2.0/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
Expand All @@ -89,14 +88,11 @@ github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kylelemons/godebug v0.0.0-20170820004349-d65d576e9348 h1:MtvEpTB6LX3vkb4ax0b5D2DHbNAUsen0Gx5wZoq3lV4=
github.com/kylelemons/godebug v0.0.0-20170820004349-d65d576e9348/go.mod h1:B69LEHPfb2qLo0BaaOLcbitczOKLWTsrBG9LczfCD4k=
github.com/mattn/go-colorable v0.0.9 h1:UVL0vNpWh04HeJXV0KLcaT7r06gOH2l4OW6ddYRUIY4=
github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU=
github.com/mattn/go-colorable v0.1.4 h1:snbPLB8fVfU9iwbbo30TPtbLRzwWu6aJS6Xh4eaaviA=
github.com/mattn/go-colorable v0.1.4/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE=
github.com/mattn/go-isatty v0.0.3 h1:ns/ykhmWi7G9O+8a448SecJU3nSMBXJfqQkl0upE1jI=
github.com/mattn/go-isatty v0.0.3/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4=
github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
github.com/mattn/go-isatty v0.0.10 h1:qxFzApOv4WsAL965uUPIsXzAKCZxN2p9UqdhFS4ZW10=
github.com/mattn/go-isatty v0.0.10/go.mod h1:qgIWMr58cqv1PHHyhnkY9lrL7etaEgOFcMEpPG5Rm84=
github.com/mattn/go-isatty v0.0.11/go.mod h1:PhnuNfih5lzO57/f3n+odYbM4JtupLOxQOAqxQCu2WE=
github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY=
Expand All @@ -108,11 +104,9 @@ github.com/mitchellh/copystructure v1.0.0 h1:Laisrj+bAB6b/yJwB5Bt3ITZhGJdqmxquMK
github.com/mitchellh/copystructure v1.0.0/go.mod h1:SNtv71yrdKgLRyLFxmLdkAbkKEFWgYaq1OVrnRcwhnw=
github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y=
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
github.com/mitchellh/go-testing-interface v0.0.0-20171004221916-a61a99592b77 h1:7GoSOOW2jpsfkntVKaS2rAr1TJqfcxotyaUcuxoZSzg=
github.com/mitchellh/go-testing-interface v0.0.0-20171004221916-a61a99592b77/go.mod h1:kRemZodwjscx+RGhAo8eIhFbs2+BFgRtFPeD/KE+zxI=
github.com/mitchellh/go-testing-interface v1.0.0 h1:fzU/JVNcaqHQEcVFAKeR41fkiLdIPrefOvVG1VZ96U0=
github.com/mitchellh/go-testing-interface v1.0.0/go.mod h1:kRemZodwjscx+RGhAo8eIhFbs2+BFgRtFPeD/KE+zxI=
github.com/mitchellh/go-wordwrap v0.0.0-20150314170334-ad45545899c7 h1:DpOJ2HYzCv8LZP15IdmG+YdwD2luVPHITV96TkirNBM=
github.com/mitchellh/go-wordwrap v0.0.0-20150314170334-ad45545899c7/go.mod h1:ZXFpozHsX6DPmq2I0TCekCxypsnAUbP2oI0UX1GXzOo=
github.com/mitchellh/go-wordwrap v1.0.0 h1:6GlHJ/LTGMrIJbwgdqdl2eEH8o+Exx/0m8ir9Gns0u4=
github.com/mitchellh/go-wordwrap v1.0.0/go.mod h1:ZXFpozHsX6DPmq2I0TCekCxypsnAUbP2oI0UX1GXzOo=
Expand Down Expand Up @@ -149,59 +143,46 @@ github.com/prometheus/procfs v0.0.8/go.mod h1:7Qr8sr6344vo1JqZ6HhLceV9o3AJ1Ff+Gx
github.com/sergi/go-diff v1.0.0 h1:Kpca3qRNrduNnOQeazBd0ysaKrUJiIuISHxogkT9RPQ=
github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo=
github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
github.com/sirupsen/logrus v1.4.2 h1:SPIRibHv4MatM3XXNO2BJeFLZwZ2LvZgfQ5+UNI2im4=
github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=
github.com/spf13/pflag v1.0.2/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/vmihailenco/msgpack v3.3.3+incompatible h1:wapg9xDUZDzGCNFlwc5SqI1rvcciqcxEHac4CYj89xI=
github.com/vmihailenco/msgpack v3.3.3+incompatible/go.mod h1:fy3FlTQTDXWkZ7Bh6AcGMlsjHatGryHQYUTf1ShIgkk=
github.com/zclconf/go-cty v1.2.0 h1:sPHsy7ADcIZQP3vILvTjrh74ZA175TFP5vqiNK1UmlI=
github.com/zclconf/go-cty v1.2.0/go.mod h1:hOPWgoHbaTUnI5k4D2ld+GRpFJSCe6bCM7m1q/N4PQ8=
github.com/zclconf/go-cty v1.3.1 h1:QIOZl+CKKdkv4l2w3lG23nNzXgLoxsWLSEdg1MlX4p0=
github.com/zclconf/go-cty v1.3.1/go.mod h1:YO23e2L18AG+ZYQfSobnY4G65nvwvprPCxBHkufUH1k=
golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20190426145343-a29dc8fdc734 h1:p/H982KKEjUnLJkM3tt/LemDnOc1GiZL5FCVlORJ5zo=
golang.org/x/crypto v0.0.0-20190426145343-a29dc8fdc734/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/net v0.0.0-20180811021610-c39426892332/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20180826012351-8a410e7b638d h1:g9qWBGx4puODJTMVyoPrpoxPFgVGd+z1DZwjfRu4d0I=
golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20181114220301-adae6a3d119a/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3 h1:0GoQqolDA55aaLxZyTzK/Y2ePZzZTUrRacwib7cNsYQ=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190613194153-d28f0bde5980 h1:dfGZHvZk057jK2MCeWus/TowKpJ8y4AmooUzdBSR9GU=
golang.org/x/net v0.0.0-20190613194153-d28f0bde5980/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4 h1:YUO/7uOKsKeq9UokNS62b8FYywz3ker1l1vDZRCRefw=
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20180823144017-11551d06cbcc h1:MeuS1UDyZyFH++6vVy44PuufTeFF0d0nfI6XB87YGSk=
golang.org/x/sys v0.0.0-20180823144017-11551d06cbcc/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20181116152217-5ac8a444bdc5/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190129075346-302c3dd5f1cc h1:WiYx1rIFmx8c0mXAFtv5D/mHyKe1+jmuP7PViuwqwuQ=
golang.org/x/sys v0.0.0-20190129075346-302c3dd5f1cc/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190502175342-a43fa875dd82 h1:vsphBvatvfbhlb4PO1BYSr9dzugGxJ/SQHoNufZJq1w=
golang.org/x/sys v0.0.0-20190502175342-a43fa875dd82/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20191008105621-543471e840be/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200122134326-e047566fdf82 h1:ywK/j/KkyTHcdyYSZNXGjMwgmDSfjglYZ3vStQ/gSCU=
golang.org/x/sys v0.0.0-20200122134326-e047566fdf82/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200124204421-9fbb57f87de9 h1:1/DFK4b7JH8DmkqhUk48onnSfrPzImPoVxuomtbT2nk=
golang.org/x/sys v0.0.0-20200124204421-9fbb57f87de9/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.2 h1:tW2bmiBqwgJj/UpqtC8EpXEZVYOwU0yG4iWbprSVAcs=
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
Expand All @@ -221,7 +202,6 @@ gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.5 h1:ymVxjfMaHvXD8RqPRmzHHsB3VvucivSkIAvJFDI5O3c=
gopkg.in/yaml.v2 v2.2.5/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10=
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
14 changes: 14 additions & 0 deletions helper/blocking/blocking.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package blocking

// IndexHasChanged is used to check whether a returned blocking query has an
// updated index, compared to a tracked value.
func IndexHasChanged(new, old uint64) bool { return new > old }

// FindMaxFound is used to determine which value passed is the greatest. This
// is used to track the most recently found highest index value.
func FindMaxFound(new, old uint64) uint64 {
if new <= old {
return old
}
return new
}
70 changes: 70 additions & 0 deletions helper/blocking/blocking_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package blocking

import (
"testing"

"github.com/stretchr/testify/assert"
)

func Test_indexHasChange(t *testing.T) {
testCases := []struct {
newValue uint64
oldValue uint64
expectedReturn bool
}{
{
newValue: 13,
oldValue: 7,
expectedReturn: true,
},
{
newValue: 13696,
oldValue: 13696,
expectedReturn: false,
},
{
newValue: 7,
oldValue: 13,
expectedReturn: false,
},
}

for _, tc := range testCases {
res := IndexHasChanged(tc.newValue, tc.oldValue)
assert.Equal(t, tc.expectedReturn, res)
}
}

func Test_findMaxFound(t *testing.T) {
testCases := []struct {
newValue uint64
oldValue uint64
expectedReturn uint64
}{
{
newValue: 13,
oldValue: 7,
expectedReturn: 13,
},
{
newValue: 13696,
oldValue: 13696,
expectedReturn: 13696,
},
{
newValue: 7,
oldValue: 13,
expectedReturn: 13,
},
{
newValue: 1,
oldValue: 0,
expectedReturn: 1,
},
}

for _, tc := range testCases {
res := FindMaxFound(tc.newValue, tc.oldValue)
assert.Equal(t, tc.expectedReturn, res)
}
}
35 changes: 0 additions & 35 deletions policystorage/policy.go

This file was deleted.

85 changes: 85 additions & 0 deletions state/handler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package state

import (
"context"
"sync"

hclog "github.com/hashicorp/go-hclog"
"github.com/hashicorp/nomad-autoscaler/state/policy"
"github.com/hashicorp/nomad-autoscaler/state/policy/source"
nomadSource "github.com/hashicorp/nomad-autoscaler/state/policy/source/nomad"
"github.com/hashicorp/nomad-autoscaler/state/status"
"github.com/hashicorp/nomad/api"
)

// Handler manages the internal state storage for the Autoscaler agent and
// should be used to ensure required information from Nomad (our source of
// truth) is stored locally. This reduces pressure on the Nomad API and allows
// for faster lookups.
type Handler struct {

// ctx is the context passed by the controlling agent in order to propagate
// shutdown.
//
// TODO(jrasell) we should figure out how to shutdown blocking queries
// based on this.
ctx context.Context

log hclog.Logger
nomad *api.Client

// PolicyState is the interface for interacting with the internal policy
// state. It is public as the agent needs to be able to list policies.
PolicyState policy.State

// policyUpdateChan is the channel where policy.Watcher process should send
// any updates to scaling policies. The state handler is responsible for
// listening to this, and processing any items on the channel.
policyUpdateChan chan *api.ScalingPolicy

// policySource
policySource source.PolicySource

// statusState is the interface for interacting with the internal job scale
// status state.
statusState status.State

// statusWatcherHandlerLock is the mutex which should be used when
// manipulating the statusWatcherHandlers map.
statusWatcherHandlerLock sync.RWMutex

// statusWatcherHandlers is a mapping on our job scaling status blocking
// query handlers. Each job which is running and configured with at least
// one scaling policy should have an associated watcher. The map is keyed
// by the JobID.
statusWatcherHandlers map[string]*status.Watcher
}

// NewHandler is used to build a new state Handler object for use in managing
// the Autoscaler internal state.
func NewHandler(ctx context.Context, log hclog.Logger, nomad *api.Client) *Handler {
h := Handler{
ctx: ctx,
log: log.Named("state_handler"),
nomad: nomad,
PolicyState: policy.NewStateBackend(),
policyUpdateChan: make(chan *api.ScalingPolicy, 10),
statusWatcherHandlers: make(map[string]*status.Watcher),
statusState: status.NewStateBackend(),
}

h.policySource = nomadSource.NewNomadPolicySource(h.log, h.nomad)

return &h
}

// Start starts the initially required state handling processes.
func (h *Handler) Start() {

// Start the policy update handler before anything else.
go h.policyUpdateHandler()

// The policy source runs as a single process per Autoscaler agent, start
// it now.
go h.policySource.Start(h.policyUpdateChan)
}
Loading