-
Notifications
You must be signed in to change notification settings - Fork 4.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
264 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/* | ||
* | ||
* Copyright 2019 gRPC authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package wrr | ||
|
||
import "google.golang.org/grpc/internal/grpcrand" | ||
|
||
// weightedItem is a wrapped weighted item that is used to implement weighted random algorithm. | ||
type weightedItem struct { | ||
Item interface{} | ||
Weight int64 | ||
} | ||
|
||
// randomWRR is a struct that contains weighted items implement weighted random algorithm. | ||
type randomWRR struct { | ||
items []*weightedItem | ||
sumOfWeights int64 | ||
} | ||
|
||
// NewRandom creates a new WRR with random. | ||
func NewRandom() WRR { | ||
return &randomWRR{} | ||
} | ||
|
||
func (rw *randomWRR) Next() (item interface{}) { | ||
if rw.sumOfWeights == 0 { | ||
return nil | ||
} | ||
// Random number in [0, sum). | ||
randomWeight := grpcrand.Int63n(rw.sumOfWeights) | ||
for _, item := range rw.items { | ||
randomWeight = randomWeight - item.Weight | ||
if randomWeight < 0 { | ||
return item.Item | ||
} | ||
} | ||
|
||
return rw.items[len(rw.items)-1].Item | ||
} | ||
|
||
func (rw *randomWRR) Add(item interface{}, weight int64) { | ||
rItem := &weightedItem{Item: item, Weight: weight} | ||
rw.items = append(rw.items, rItem) | ||
rw.sumOfWeights += weight | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/* | ||
* | ||
* Copyright 2019 gRPC authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package wrr | ||
|
||
// WRR defines an interface that implements weighted round robin. | ||
type WRR interface { | ||
// Add adds an item with weight to the WRR set. | ||
Add(item interface{}, weight int64) | ||
// Next returns the next picked item. | ||
// | ||
// Next needs to be thread safe. | ||
Next() interface{} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
/* | ||
* | ||
* Copyright 2019 gRPC authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package wrr | ||
|
||
import ( | ||
"errors" | ||
"math" | ||
"testing" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
) | ||
|
||
const iterCount = 10000 | ||
|
||
func equalApproximate(a, b float64) error { | ||
opt := cmp.Comparer(func(x, y float64) bool { | ||
delta := math.Abs(x - y) | ||
mean := math.Abs(x+y) / 2.0 | ||
return delta/mean < 0.05 | ||
}) | ||
if !cmp.Equal(a, b, opt) { | ||
return errors.New(cmp.Diff(a, b)) | ||
} | ||
return nil | ||
} | ||
|
||
func testWRRNext(t *testing.T, newWRR func() WRR) { | ||
tests := []struct { | ||
name string | ||
weights []int64 | ||
}{ | ||
{ | ||
name: "1-1-1", | ||
weights: []int64{1, 1, 1}, | ||
}, | ||
{ | ||
name: "1-2-3", | ||
weights: []int64{1, 2, 3}, | ||
}, | ||
{ | ||
name: "5-3-2", | ||
weights: []int64{5, 3, 2}, | ||
}, | ||
{ | ||
name: "17-23-37", | ||
weights: []int64{17, 23, 37}, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
var sumOfWeights int64 | ||
|
||
w := newWRR() | ||
for i, weight := range tt.weights { | ||
w.Add(i, weight) | ||
sumOfWeights += weight | ||
} | ||
|
||
results := make(map[int]int) | ||
for i := 0; i < iterCount; i++ { | ||
results[w.Next().(int)]++ | ||
} | ||
|
||
wantRatio := make([]float64, len(tt.weights)) | ||
for i, weight := range tt.weights { | ||
wantRatio[i] = float64(weight) / float64(sumOfWeights) | ||
} | ||
gotRatio := make([]float64, len(tt.weights)) | ||
for i, count := range results { | ||
gotRatio[i] = float64(count) / iterCount | ||
} | ||
|
||
for i := range wantRatio { | ||
if err := equalApproximate(gotRatio[i], wantRatio[i]); err != nil { | ||
t.Errorf("%v not equal %v", i, err) | ||
} | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestRandomWRRNext(t *testing.T) { | ||
testWRRNext(t, NewRandom) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters