From 2b1d7f3cc6f3dadd34d04d83d7c2feb90f62d2a8 Mon Sep 17 00:00:00 2001 From: Chelsea Holland Komlo Date: Wed, 20 Dec 2017 15:17:28 -0500 Subject: [PATCH] search endpoint forwarding --- nomad/search_endpoint.go | 7 ++++++ nomad/search_endpoint_test.go | 44 +++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+) diff --git a/nomad/search_endpoint.go b/nomad/search_endpoint.go index 61931a5e8b8..512ef3e044f 100644 --- a/nomad/search_endpoint.go +++ b/nomad/search_endpoint.go @@ -2,7 +2,9 @@ package nomad import ( "strings" + "time" + metrics "github.com/armon/go-metrics" memdb "github.com/hashicorp/go-memdb" "github.com/hashicorp/nomad/acl" "github.com/hashicorp/nomad/nomad/state" @@ -114,6 +116,11 @@ func roundUUIDDownIfOdd(prefix string, context structs.Context) string { // PrefixSearch is used to list matches for a given prefix, and returns // matching jobs, evaluations, allocations, and/or nodes. func (s *Search) PrefixSearch(args *structs.SearchRequest, reply *structs.SearchResponse) error { + if done, err := s.srv.forward("Search.PrefixSearch", args, args, reply); done { + return err + } + defer metrics.MeasureSince([]string{"nomad", "search", "prefixsearch"}, time.Now()) + aclObj, err := s.srv.ResolveToken(args.AuthToken) if err != nil { return err diff --git a/nomad/search_endpoint_test.go b/nomad/search_endpoint_test.go index 7cd6fe6b27a..2631b695876 100644 --- a/nomad/search_endpoint_test.go +++ b/nomad/search_endpoint_test.go @@ -712,3 +712,47 @@ func TestSearch_PrefixSearch_RoundDownToEven(t *testing.T) { assert.Equal(1, len(resp.Matches[structs.Jobs])) assert.Equal(job.ID, resp.Matches[structs.Jobs][0]) } + +func TestSearch_PrefixSearch_MultiRegion(t *testing.T) { + assert := assert.New(t) + + jobName := "exampleexample" + + t.Parallel() + s1 := testServer(t, func(c *Config) { + c.NumSchedulers = 0 + c.Region = "foo" + }) + defer s1.Shutdown() + + s2 := testServer(t, func(c *Config) { + c.NumSchedulers = 0 + c.Region = "bar" + }) + defer s2.Shutdown() + + testJoin(t, s1, s2) + testutil.WaitForLeader(t, s1.RPC) + + job := registerAndVerifyJob(s1, t, jobName, 0) + + req := &structs.SearchRequest{ + Prefix: "", + Context: structs.Jobs, + QueryOptions: structs.QueryOptions{ + Region: "foo", + Namespace: job.Namespace, + }, + } + + codec := rpcClient(t, s2) + + var resp structs.SearchResponse + if err := msgpackrpc.CallWithCodec(codec, "Search.PrefixSearch", req, &resp); err != nil { + t.Fatalf("err: %v", err) + } + + assert.Equal(1, len(resp.Matches[structs.Jobs])) + assert.Equal(job.ID, resp.Matches[structs.Jobs][0]) + assert.Equal(uint64(jobIndex), resp.Index) +}