Skip to content

Commit

Permalink
storage: remove rule solver
Browse files Browse the repository at this point in the history
Remove the concept of rules and replace it with clean direct functions.
  • Loading branch information
BramGruneir committed Dec 7, 2016
1 parent 9db4532 commit 7ca73d8
Show file tree
Hide file tree
Showing 4 changed files with 644 additions and 551 deletions.
91 changes: 29 additions & 62 deletions pkg/storage/allocator.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,38 +212,38 @@ func (a *Allocator) AllocateTarget(
rangeID roachpb.RangeID,
relaxConstraints bool,
) (*roachpb.StoreDescriptor, error) {
sl, aliveStoreCount, throttledStoreCount := a.storePool.getStoreList(rangeID)
if a.options.UseRuleSolver {
sl, _, throttledStoreCount := a.storePool.getStoreList(rangeID)
// When there are throttled stores that do match, we shouldn't send
// the replica to purgatory.
if throttledStoreCount > 0 {
return nil, errors.Errorf("%d matching stores are currently throttled", throttledStoreCount)
}

candidates := allocateRuleSolver.Solve(
candidates := allocateCandidates(
sl,
constraints,
existing,
a.storePool.getNodeLocalities(existing),
a.storePool.deterministic,
)
candidates = candidates.onlyValid()
if len(candidates) == 0 {
return nil, &allocatorError{
required: constraints.Constraints,
}
if log.V(3) {
log.Infof(context.TODO(), "allocate candidates: %s", candidates)
}
if len(candidates) != 0 {
chosenCandidate := candidates.selectGood(a.randGen).store
return &chosenCandidate, nil
}

// When there are throttled stores that do match, we shouldn't send
// the replica to purgatory.
if throttledStoreCount > 0 {
return nil, errors.Errorf("%d matching stores are currently throttled", throttledStoreCount)
}
return nil, &allocatorError{
required: constraints.Constraints,
}
chosenCandidate := candidates.selectGood(a.randGen).store
return &chosenCandidate, nil
}

existingNodes := make(nodeIDSet, len(existing))
for _, repl := range existing {
existingNodes[repl.NodeID] = struct{}{}
}

sl, aliveStoreCount, throttledStoreCount := a.storePool.getStoreList(rangeID)

// Because more redundancy is better than less, if relaxConstraints, the
// matching here is lenient, and tries to find a target by relaxing an
// attribute constraint, from last attribute to first.
Expand Down Expand Up @@ -301,13 +301,15 @@ func (a Allocator) RemoveTarget(
var badStoreID roachpb.StoreID

if a.options.UseRuleSolver {
candidates := removeRuleSolver.Solve(
candidates := removeCandidates(
sl,
constraints,
existing,
a.storePool.getNodeLocalities(existing),
a.storePool.deterministic,
)
if log.V(3) {
log.Infof(context.TODO(), "remove candidates: %s", candidates)
}
if len(candidates) != 0 {
badStoreID = candidates.selectBad(a.randGen).store.StoreID
}
Expand Down Expand Up @@ -358,13 +360,9 @@ func (a Allocator) RebalanceTarget(
if !a.options.AllowRebalance {
return nil, nil
}
sl, _, _ := a.storePool.getStoreList(rangeID)

if a.options.UseRuleSolver {
sl, _, _ := a.storePool.getStoreList(rangeID)
if log.V(3) {
log.Infof(context.TODO(), "rebalance-target (lease-holder=%d):\n%s", leaseStoreID, sl)
}

var shouldRebalance bool
for _, repl := range existing {
if leaseStoreID == repl.StoreID {
Expand All @@ -380,48 +378,18 @@ func (a Allocator) RebalanceTarget(
return nil, nil
}

// Load the exiting storesIDs into a map so to eliminate having to loop
// through the existing descriptors more than once.
existingStoreIDs := make(map[roachpb.StoreID]struct{})
for _, repl := range existing {
existingStoreIDs[repl.StoreID] = struct{}{}
}

// Split the store list into existing and candidate stores lists so that
// we can call solve independently on both store lists.
var existingDescs []roachpb.StoreDescriptor
var candidateDescs []roachpb.StoreDescriptor
for _, desc := range sl.stores {
if _, ok := existingStoreIDs[desc.StoreID]; ok {
existingDescs = append(existingDescs, desc)
} else {
candidateDescs = append(candidateDescs, desc)
}
}

existingStoreList := makeStoreList(existingDescs)
candidateStoreList := makeStoreList(candidateDescs)

localities := a.storePool.getNodeLocalities(existing)
existingCandidates := rebalanceExisting.Solve(
existingStoreList,
constraints,
existing,
localities,
a.storePool.deterministic,
)
candidates := rebalance.Solve(
candidateStoreList,
existingCandidates, candidates := rebalanceCandidates(
sl,
constraints,
existing,
localities,
a.storePool.getNodeLocalities(existing),
a.storePool.deterministic,
)

/*
fmt.Printf("existing: %s\n", existingCandidates)
fmt.Printf("candidates: %s\n", candidates)
*/
if log.V(3) {
log.Infof(context.TODO(), "existing replicas: %s", existingCandidates)
log.Infof(context.TODO(), "candidates: %s", candidates)
}

// Find all candidates that are better than the worst existing.
targets := candidates.betterThan(existingCandidates[len(existingCandidates)-1])
Expand All @@ -432,7 +400,6 @@ func (a Allocator) RebalanceTarget(
return nil, nil
}

sl, _, _ := a.storePool.getStoreList(rangeID)
sl = sl.filter(constraints)
if log.V(3) {
log.Infof(context.TODO(), "rebalance-target (lease-holder=%d):\n%s", leaseStoreID, sl)
Expand Down
Loading

0 comments on commit 7ca73d8

Please sign in to comment.