Skip to content

Commit

Permalink
server/constraints: remove default constraint
Browse files Browse the repository at this point in the history
  • Loading branch information
huachaohuang committed Nov 30, 2016
1 parent 6e0efad commit 501e53c
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 63 deletions.
6 changes: 2 additions & 4 deletions server/balancer.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,10 +152,8 @@ func (r *replicaChecker) Check(region *regionInfo) *balanceOperator {
}
}
if len(stores) < constraints.MaxReplicas {
// If we reach here and we don't have enough replicas,
// it means that we can't satisfy all constraints for now,
// but at least we try to satisfy the max replicas requirement.
log.Warnf("region replication constraints can not be satisfied: %v", region)
// No matter whether we can satisfy all constraints or not,
// we should at least ensure that the region has enough replicas.
return r.addPeer(region, nil)
}

Expand Down
4 changes: 0 additions & 4 deletions server/constraints.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,6 @@ func newConstraints(maxReplicas int, constraints []*Constraint) *Constraints {
if maxReplicas <= sumReplicas {
// Max replicas should not be smaller than the sum replicas.
maxReplicas = sumReplicas
} else {
// Add a default constraint to cover other replicas.
defaultConstraint := &Constraint{Replicas: maxReplicas - sumReplicas}
constraints = append(constraints, defaultConstraint)
}
return &Constraints{
MaxReplicas: maxReplicas,
Expand Down
63 changes: 8 additions & 55 deletions server/constraints_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,7 @@ func (s *testConstraintsSuite) TestReplicas(c *C) {
// No constraints, add default constraint with 3 replicas.
cs := newConstraints(3, constraints)
c.Assert(cs.MaxReplicas, Equals, 3)
c.Assert(cs.Constraints, HasLen, 1)
c.Assert(cs.Constraints[0].Labels, IsNil)
c.Assert(cs.Constraints[0].Replicas, Equals, 3)
c.Assert(cs.Constraints, IsNil)

constraints = append(constraints, &Constraint{
Labels: map[string]string{"zone": "us-west", "disk": "ssd"},
Expand All @@ -79,42 +77,9 @@ func (s *testConstraintsSuite) TestReplicas(c *C) {
// max replicas 4 > sum replicas 3, add default constraint with 1 replica.
cs = newConstraints(4, constraints)
c.Assert(cs.MaxReplicas, Equals, 4)
c.Assert(cs.Constraints, HasLen, 3)
c.Assert(cs.Constraints, HasLen, 2)
c.Assert(cs.Constraints[0], DeepEquals, constraints[0])
c.Assert(cs.Constraints[1], DeepEquals, constraints[1])
c.Assert(cs.Constraints[2].Labels, IsNil)
c.Assert(cs.Constraints[2].Replicas, Equals, 1)
}

func (s *testConstraintsSuite) TestDefaultConstraint(c *C) {
var stores []*storeInfo

// Constraints with a default constraint.
cs := newConstraints(3, nil)
result := cs.Match(stores)
c.Assert(result.stores, HasLen, 0)
c.Assert(result.constraints[0].stores, HasLen, 0)

// Store 1,2 matches the default constraint.
stores = append(stores, newStoreInfo(&metapb.Store{Id: 1}))
stores = append(stores, newStoreInfo(&metapb.Store{Id: 2}))
result = cs.Match(stores)
c.Assert(result.stores, HasLen, 2)
c.Assert(result.stores[1], DeepEquals, cs.Constraints[0])
c.Assert(result.stores[2], DeepEquals, cs.Constraints[0])
c.Assert(result.constraints[0].stores, DeepEquals, stores)

// Store 1,2,3 matches the default constraint.
// Since the default constraint has been satisfied, store 4 don't need to match it.
stores = append(stores, newStoreInfo(&metapb.Store{Id: 3}))
stores = append(stores, newStoreInfo(&metapb.Store{Id: 4}))
result = cs.Match(stores)
c.Assert(result.stores, HasLen, 3)
c.Assert(result.stores[1], DeepEquals, cs.Constraints[0])
c.Assert(result.stores[2], DeepEquals, cs.Constraints[0])
c.Assert(result.stores[3], DeepEquals, cs.Constraints[0])
c.Assert(result.stores[4], IsNil)
c.Assert(result.constraints[0].stores, DeepEquals, stores[:3])
}

func (s *testConstraintsSuite) TestConstraints(c *C) {
Expand All @@ -128,14 +93,13 @@ func (s *testConstraintsSuite) TestConstraints(c *C) {
Replicas: 1,
},
}
// Constraints with 1 default replica.
cs := newConstraints(4, constraints)
// cs.Constraints[2] is the default constraint.

var stores []*storeInfo
result := cs.Match(stores)
c.Assert(result.stores, HasLen, 0)
c.Assert(result.constraints[0].stores, HasLen, 0)
c.Assert(result.constraints[0].stores, IsNil)
c.Assert(result.constraints[1].stores, IsNil)

// Create 5 stores with no labels.
stores = append(stores, newStoreInfo(&metapb.Store{Id: 0}))
Expand All @@ -144,17 +108,13 @@ func (s *testConstraintsSuite) TestConstraints(c *C) {
stores = append(stores, newStoreInfo(&metapb.Store{Id: 3}))
stores = append(stores, newStoreInfo(&metapb.Store{Id: 4}))

// Store 0 will match the default constraint and the other are redundant.
result = cs.Match(stores)
c.Assert(result.stores, HasLen, 1)
c.Assert(result.stores[0], DeepEquals, cs.Constraints[2])
c.Assert(result.stores, HasLen, 0)
c.Assert(result.constraints[0].stores, IsNil)
c.Assert(result.constraints[1].stores, IsNil)
c.Assert(result.constraints[2].stores, DeepEquals, stores[0:1])

// Store 0 matches constraint 0.
// Store 1 matches constraint 1.
// Store 2 matches constraint 2 (default constraint).
stores[0].Labels = []*metapb.StoreLabel{
{Key: "zone", Value: "us-west"},
{Key: "disk", Value: "ssd"},
Expand All @@ -175,16 +135,13 @@ func (s *testConstraintsSuite) TestConstraints(c *C) {
}

result = cs.Match(stores)
c.Assert(result.stores, HasLen, 3)
c.Assert(result.stores, HasLen, 2)
c.Assert(result.stores[0], DeepEquals, cs.Constraints[0])
c.Assert(result.stores[1], DeepEquals, cs.Constraints[1])
c.Assert(result.stores[2], DeepEquals, cs.Constraints[2])
c.Assert(result.constraints[0].stores, DeepEquals, stores[0:1])
c.Assert(result.constraints[1].stores, DeepEquals, stores[1:2])
c.Assert(result.constraints[2].stores, DeepEquals, stores[2:3])

// Store 0,1 can match constraint 0.
// Store 2 can match constraint 2 (default constraint).
// Store 3,4 can match constraint 1 (but store 4 is redundant).
stores[0].Labels = []*metapb.StoreLabel{
{Key: "zone", Value: "us-west"},
Expand All @@ -206,14 +163,12 @@ func (s *testConstraintsSuite) TestConstraints(c *C) {
}

result = cs.Match(stores)
c.Assert(result.stores, HasLen, 4)
c.Assert(result.stores, HasLen, 3)
c.Assert(result.stores[0], DeepEquals, cs.Constraints[0])
c.Assert(result.stores[1], DeepEquals, cs.Constraints[0])
c.Assert(result.stores[2], DeepEquals, cs.Constraints[2])
c.Assert(result.stores[3], DeepEquals, cs.Constraints[1])
c.Assert(result.constraints[0].stores, DeepEquals, stores[0:2])
c.Assert(result.constraints[1].stores, DeepEquals, stores[3:4])
c.Assert(result.constraints[2].stores, DeepEquals, stores[2:3])

// Store 0,1,2 can match constraint 0 (but store 2 is redundant).
// Store 3,4 can match constraint 1 (but store 4 is redundant).
Expand Down Expand Up @@ -242,12 +197,10 @@ func (s *testConstraintsSuite) TestConstraints(c *C) {
}

result = cs.Match(stores)
c.Assert(result.stores, HasLen, 4)
c.Assert(result.stores, HasLen, 3)
c.Assert(result.stores[0], DeepEquals, cs.Constraints[0])
c.Assert(result.stores[1], DeepEquals, cs.Constraints[0])
c.Assert(result.stores[2], DeepEquals, cs.Constraints[2])
c.Assert(result.stores[3], DeepEquals, cs.Constraints[1])
c.Assert(result.constraints[0].stores, DeepEquals, stores[0:2])
c.Assert(result.constraints[1].stores, DeepEquals, stores[3:4])
c.Assert(result.constraints[2].stores, DeepEquals, stores[2:3])
}

0 comments on commit 501e53c

Please sign in to comment.