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

Search endpoint forwarding #3680

Merged
merged 3 commits into from
Dec 21, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 0.8 (Unreleased)

BUG FIXES:
* core: Fix search endpoint forwarding for multi-region clusters [GH-3680]

## 0.7.1 (December 19, 2017)

__BACKWARDS INCOMPATIBILITIES:__
Expand Down
7 changes: 7 additions & 0 deletions nomad/search_endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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", "prefix_search"}, time.Now())

aclObj, err := s.srv.ResolveToken(args.AuthToken)
if err != nil {
return err
Expand Down
44 changes: 44 additions & 0 deletions nomad/search_endpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}