From c88cee6b63291eeccb3f0b5a10bd1a672dd929d0 Mon Sep 17 00:00:00 2001 From: Vijay Srinivas Date: Wed, 3 Apr 2024 17:55:13 +0530 Subject: [PATCH] test and doc updates --- agent/config/runtime_test.go | 9 +-- agent/config/testdata/full-config.json | 3 +- api/watch/plan.go | 1 + api/watch/plan_test.go | 64 +++++++++++++++++++ api/watch/watch_test.go | 5 +- command/flags/http.go | 9 ++- .../docs/dynamic-app-config/watches.mdx | 2 + 7 files changed, 82 insertions(+), 11 deletions(-) diff --git a/agent/config/runtime_test.go b/agent/config/runtime_test.go index bc2222739c..dd529a6d78 100644 --- a/agent/config/runtime_test.go +++ b/agent/config/runtime_test.go @@ -7047,10 +7047,11 @@ func TestLoad_FullConfig(t *testing.T) { UnixSocketMode: "E8sAwOv4", Watches: []map[string]interface{}{ { - "type": "key", - "datacenter": "GyE6jpeW", - "key": "j9lF1Tve", - "handler": "90N7S4LN", + "type": "key", + "datacenter": "GyE6jpeW", + "key": "j9lF1Tve", + "handler": "90N7S4LN", + "alldatacenters": true, }, { "type": "keyprefix", diff --git a/agent/config/testdata/full-config.json b/agent/config/testdata/full-config.json index 30ede7dd18..d9d8ef2f29 100644 --- a/agent/config/testdata/full-config.json +++ b/agent/config/testdata/full-config.json @@ -929,7 +929,8 @@ "type": "key", "datacenter": "GyE6jpeW", "key": "j9lF1Tve", - "handler": "90N7S4LN" + "handler": "90N7S4LN", + "alldatacenters": true }, { "type": "keyprefix", diff --git a/api/watch/plan.go b/api/watch/plan.go index b70524f839..9bb204edcc 100644 --- a/api/watch/plan.go +++ b/api/watch/plan.go @@ -168,6 +168,7 @@ OUTER: } for _, dc := range dcs { + p.Datacenter = dc conf.Address = p.address conf.Datacenter = dc conf.Token = p.Token diff --git a/api/watch/plan_test.go b/api/watch/plan_test.go index 5a77260473..86d6e5f1de 100644 --- a/api/watch/plan_test.go +++ b/api/watch/plan_test.go @@ -4,12 +4,14 @@ package watch import ( + "reflect" "testing" "time" ) func init() { watchFuncFactory["noop"] = noopWatch + watchFuncFactory["noopdcs"] = noopWatchAllDatacenters } func noopWatch(params map[string]interface{}) (WatcherFunc, error) { @@ -23,6 +25,20 @@ func noopWatch(params map[string]interface{}) (WatcherFunc, error) { return fn, nil } +func noopWatchAllDatacenters(params map[string]interface{}) (WatcherFunc, error) { + fn := func(p *Plan) (BlockingParamVal, interface{}, error) { + var idx WaitIndexVal + if p.Datacenter == "" { + idx = WaitIndexVal(0) + } + if i, ok := p.mapLastParamVal[p.Datacenter].(WaitIndexVal); ok { + idx = i + } + return idx + 1, uint64(idx + 1), nil + } + return fn, nil +} + func mustParse(t *testing.T, q string) *Plan { params := makeParams(t, q) plan, err := Parse(params) @@ -79,6 +95,54 @@ func TestRun_Stop(t *testing.T) { } } +func TestRun_Stop_AllDatacenters(t *testing.T) { + t.Parallel() + plan := mustParse(t, `{"type":"noopdcs","alldatacenters":true}`) + + var expect uint64 = 1 + var expectVal = []uint64{1} + doneCh := make(chan struct{}) + plan.Handler = func(idx uint64, val interface{}) { + if idx != expect { + t.Fatalf("Bad: %d %d", expect, idx) + } + if !(reflect.TypeOf(val).Kind() == reflect.Slice && val.([]interface{})[0] == expectVal[0]) { + t.Fatalf("Bad: %d %d", expect, val) + } + if expect == 1 { + close(doneCh) + } + expect++ + } + + errCh := make(chan error, 1) + go func() { + errCh <- plan.Run("127.0.0.1:8500") + }() + + select { + case <-doneCh: + plan.Stop() + + case <-time.After(1 * time.Second): + t.Fatalf("handler never ran") + } + + select { + case err := <-errCh: + if err != nil { + t.Fatalf("err: %v", err) + } + + case <-time.After(1 * time.Second): + t.Fatalf("watcher didn't exit") + } + + if expect == 1 { + t.Fatalf("Bad: %d", expect) + } +} + func TestRun_Stop_Hybrid(t *testing.T) { t.Parallel() plan := mustParse(t, `{"type":"noop"}`) diff --git a/api/watch/watch_test.go b/api/watch/watch_test.go index f8af19a35e..62d07c817f 100644 --- a/api/watch/watch_test.go +++ b/api/watch/watch_test.go @@ -29,7 +29,7 @@ func TestParseBasic(t *testing.T) { func TestParse_exempt(t *testing.T) { t.Parallel() - params := makeParams(t, `{"type":"key", "key":"foo", "handler": "foobar"}`) + params := makeParams(t, `{"type":"key", "key":"foo", "handler": "foobar","alldatacenters":true}`) p, err := ParseExempt(params, []string{"handler"}) if err != nil { t.Fatalf("err: %v", err) @@ -37,6 +37,9 @@ func TestParse_exempt(t *testing.T) { if p.Type != "key" { t.Fatalf("Bad: %#v", p) } + if !p.AllDatacenters { + t.Fatalf("Bad: %#v", p) + } ex := p.Exempt["handler"] if ex != "foobar" { t.Fatalf("bad: %v", ex) diff --git a/command/flags/http.go b/command/flags/http.go index 58646c73a4..94cbbeb957 100644 --- a/command/flags/http.go +++ b/command/flags/http.go @@ -77,11 +77,10 @@ func (f *HTTPFlags) ServerFlags() *flag.FlagSet { "allows for lower latency and higher throughput, but can result in "+ "stale data. This option has no effect on non-read operations. The "+ "default value is false.") - fs.Var(&f.allDatacenters, "allDatacenters", - "Specify if query to be made for all datacenters. The default value is false and "+ - "will default to the datacenter of the queried agent, if datacenter is "+ - "is not input. If datacenter is provided and allDatacenter is also set true, "+ - "allDatacenter takes precedence.") + fs.Var(&f.allDatacenters, "alldatacenters", + "Specify if query to be made on all datacenters. The default value is false."+ + "If datacenter is provided and alldatacenters is also set true, allDatacenters"+ + "takes precedence.") return fs } diff --git a/website/content/docs/dynamic-app-config/watches.mdx b/website/content/docs/dynamic-app-config/watches.mdx index 8eac588862..611c8144c5 100644 --- a/website/content/docs/dynamic-app-config/watches.mdx +++ b/website/content/docs/dynamic-app-config/watches.mdx @@ -158,6 +158,8 @@ are a few global parameters that all watches support: - `token` - Can be provided to override the agent's default ACL token. - `args` - The handler subprocess and arguments to invoke when the data view updates. - `handler` - The handler shell command to invoke when the data view updates. +- `alldatacenters` - Can be set to true, to make query on all datacenters. Default value is false. + Value provided for `datacenter` is ignored if `allDatacenters` is set to true. ## Watch Types