Skip to content

Commit

Permalink
ui: added node constraint solver to UI
Browse files Browse the repository at this point in the history
  • Loading branch information
d4l3k committed Sep 2, 2016
1 parent d86abd2 commit 4601e62
Show file tree
Hide file tree
Showing 18 changed files with 1,709 additions and 565 deletions.
30 changes: 30 additions & 0 deletions server/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import (
"strings"
"time"

yaml "gopkg.in/yaml.v2"

"github.com/gogo/protobuf/proto"
gwruntime "github.com/grpc-ecosystem/grpc-gateway/runtime"
"github.com/pkg/errors"
Expand Down Expand Up @@ -1306,3 +1308,31 @@ func (s *adminServer) queryDescriptorIDPath(
}
return path, nil
}

// Constraints runs a set of constraints against the current stores and returns
// a list of candidates that satisfy them along with their scores.
func (s *adminServer) Constraints(ctx context.Context, req *serverpb.ConstraintsRequest) (*serverpb.ConstraintsResponse, error) {
input := fmt.Sprintf("[%s]", req.Constraints)
var constraints config.Constraints
if err := yaml.Unmarshal([]byte(input), &constraints); err != nil {
return nil, err
}

rs := storage.MakeDefaultRuleSolver(s.server.storePool)
candidates, err := rs.Solve(constraints, nil)
if err != nil {
return nil, err
}
resp := serverpb.ConstraintsResponse{
Stores: s.server.storePool.GetStoreDescs(),
Candidates: make([]serverpb.Candidate, len(candidates)),
}

for i, c := range candidates {
resp.Candidates[i] = serverpb.Candidate{
Desc: c.Store,
Score: c.Score,
}
}
return &resp, nil
}
39 changes: 39 additions & 0 deletions server/admin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import (

"github.com/cockroachdb/cockroach/base"
"github.com/cockroachdb/cockroach/config"
"github.com/cockroachdb/cockroach/roachpb"
"github.com/cockroachdb/cockroach/security"
"github.com/cockroachdb/cockroach/server/serverpb"
"github.com/cockroachdb/cockroach/sql"
Expand Down Expand Up @@ -894,3 +895,41 @@ func TestClusterFreeze(t *testing.T) {
}
}
}

func TestConstraints(t *testing.T) {
defer leaktest.AfterTest(t)()
s, _, _ := serverutils.StartServer(t, base.TestServerArgs{})
defer s.Stopper().Stop()

testCases := []struct {
constraints string
want []roachpb.StoreID
}{
{
"",
[]roachpb.StoreID{1},
},
{
"+nonexistant",
nil,
},
}

for i, tc := range testCases {
var resp serverpb.ConstraintsResponse
path := "constraints/" + tc.constraints
if err := getAdminJSONProto(s, path, &resp); err != nil {
t.Fatal(err)
}
if len(resp.Candidates) != len(tc.want) {
t.Errorf("%d: GET %q len(%+v) = %d; not %d", i, path, resp.Candidates, len(resp.Candidates), len(tc.want))
continue
}
for j, id := range tc.want {
rid := resp.Candidates[j].Desc.StoreID
if rid != id {
t.Errorf("%d: GET %q [%d] = %+v; not %+v", i, path, j, rid, id)
}
}
}
}
Loading

0 comments on commit 4601e62

Please sign in to comment.