Skip to content

Commit

Permalink
Allow non valid zones for instances to be add to a placement (#1479)
Browse files Browse the repository at this point in the history
* Allow non valid zones for instances to be add to a placement

* Address feedback

* - rename test
  - generate mock

* Address feedback
  • Loading branch information
nerd0 authored Mar 20, 2019
1 parent bcc109b commit 69fffb9
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 1 deletion.
14 changes: 14 additions & 0 deletions src/cluster/placement/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ const (
defaultIsSharded = true
// By default partial replace should be allowed for better distribution.
defaultAllowPartialReplace = true
// By default the zone of the hosts within a placement should match the zone
// that the placement was created with.
defaultAllowAllZones = false
)

type deploymentOptions struct {
Expand Down Expand Up @@ -68,6 +71,7 @@ type options struct {
validateFn ValidateFn
nowFn clock.NowFn
allowPartialReplace bool
allowAllZones bool
addAllCandidates bool
dryrun bool
isSharded bool
Expand All @@ -89,6 +93,7 @@ func NewOptions() Options {
isShardCutoffFn: defaultShardValidationFn,
validateFn: Validate,
nowFn: time.Now,
allowAllZones: defaultAllowAllZones,
}
}

Expand All @@ -101,6 +106,15 @@ func (o options) SetAllowPartialReplace(allowPartialReplace bool) Options {
return o
}

func (o options) AllowAllZones() bool {
return o.allowAllZones
}

func (o options) SetAllowAllZones(allowAllZones bool) Options {
o.allowAllZones = allowAllZones
return o
}

func (o options) AddAllCandidates() bool {
return o.addAllCandidates
}
Expand Down
30 changes: 29 additions & 1 deletion src/cluster/placement/placement_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions src/cluster/placement/selector/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ func filterZones(
var validZone string
if opts != nil {
validZone = opts.ValidZone()
if opts.AllowAllZones() {
return candidates
}
}
if validZone == "" && len(p.Instances()) > 0 {
validZone = p.Instances()[0].Zone()
Expand Down
19 changes: 19 additions & 0 deletions src/cluster/placement/selector/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,22 @@ func TestGetValidCandidates(t *testing.T) {
require.NoError(t, err)
require.Equal(t, []placement.Instance{i3, i3, i4}, res)
}

func TestGetValidCandidatesAllowAllZones(t *testing.T) {
i1 := placement.NewInstance().SetID("i1").SetZone("z1")
i2 := placement.NewInstance().SetID("i2").SetZone("z1")
i3 := placement.NewInstance().SetID("i3").SetZone("z2")

p := placement.NewPlacement().
SetInstances([]placement.Instance{i3, i1, i2}).
SetIsSharded(false).
SetReplicaFactor(1)

i4 := placement.NewInstance().SetID("i4").SetZone("z2")
i5 := placement.NewInstance().SetID("i5").SetZone("z3")
candidates := []placement.Instance{i4, i5}
res, err := getValidCandidates(p, candidates, placement.NewOptions().
SetAllowAllZones(true))
require.NoError(t, err)
require.Equal(t, []placement.Instance{i4, i5}, res)
}
8 changes: 8 additions & 0 deletions src/cluster/placement/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,14 @@ type Options interface {
// SetAllowPartialReplace sets AllowPartialReplace.
SetAllowPartialReplace(allowPartialReplace bool) Options

// AllowAllZones will enable the placement to contain hosts that
// are not contained within the same zone of the actual placement. This is
// needed for services that require cross zone communication.
AllowAllZones() bool

// SetAllowAllZones sets AllowAllZones.
SetAllowAllZones(allowAllZones bool) Options

// AddAllCandidates determines whether the placement will attempt to add all
// candidates when adding instances or just a single one.
AddAllCandidates() bool
Expand Down

0 comments on commit 69fffb9

Please sign in to comment.