-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2301 from influxdb/races
Distributed query load balancing and failover
- Loading branch information
Showing
9 changed files
with
291 additions
and
20 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
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,77 @@ | ||
package influxdb | ||
|
||
import ( | ||
"math/rand" | ||
"time" | ||
) | ||
|
||
// Balancer represents a load-balancing algorithm for a set of DataNodes | ||
type Balancer interface { | ||
// Next returns the next DataNode according to the balancing method | ||
// or nil if there are no nodes available | ||
Next() *DataNode | ||
} | ||
|
||
type dataNodeBalancer struct { | ||
dataNodes []*DataNode // data nodes to balance between | ||
p int // current node index | ||
} | ||
|
||
// NewDataNodeBalancer create a shuffled, round-robin balancer so that | ||
// multiple instances will return nodes in randomized order and each | ||
// each returned DataNode will be repeated in a cycle | ||
func NewDataNodeBalancer(dataNodes []*DataNode) Balancer { | ||
// make a copy of the dataNode slice so we can randomize it | ||
// without affecting the original instance as well as ensure | ||
// that each Balancer returns nodes in a different order | ||
nodes := make([]*DataNode, len(dataNodes)) | ||
copy(nodes, dataNodes) | ||
|
||
b := &dataNodeBalancer{ | ||
dataNodes: nodes, | ||
} | ||
b.shuffle() | ||
return b | ||
} | ||
|
||
// shuffle randomizes the ordering the balancers available DataNodes | ||
func (b *dataNodeBalancer) shuffle() { | ||
for i := range b.dataNodes { | ||
j := rand.Intn(i + 1) | ||
b.dataNodes[i], b.dataNodes[j] = b.dataNodes[j], b.dataNodes[i] | ||
} | ||
} | ||
|
||
// online returns a slice of the DataNodes that are online | ||
func (b *dataNodeBalancer) online() []*DataNode { | ||
now := time.Now().UTC() | ||
up := []*DataNode{} | ||
for _, n := range b.dataNodes { | ||
if n.OfflineUntil.After(now) { | ||
continue | ||
} | ||
up = append(up, n) | ||
} | ||
return up | ||
} | ||
|
||
// Next returns the next available DataNode | ||
func (b *dataNodeBalancer) Next() *DataNode { | ||
// only use online nodes | ||
up := b.online() | ||
|
||
// no nodes online | ||
if len(up) == 0 { | ||
return nil | ||
} | ||
|
||
// rollover back to the beginning | ||
if b.p >= len(up) { | ||
b.p = 0 | ||
} | ||
|
||
d := up[b.p] | ||
b.p += 1 | ||
|
||
return d | ||
} |
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,109 @@ | ||
package influxdb_test | ||
|
||
import ( | ||
"fmt" | ||
"net/url" | ||
"testing" | ||
|
||
"github.com/influxdb/influxdb" | ||
) | ||
|
||
func newDataNodes() []*influxdb.DataNode { | ||
nodes := []*influxdb.DataNode{} | ||
for i := 1; i <= 2; i++ { | ||
u, _ := url.Parse(fmt.Sprintf("http://localhost:999%d", i)) | ||
nodes = append(nodes, &influxdb.DataNode{ID: uint64(i), URL: u}) | ||
} | ||
return nodes | ||
} | ||
|
||
func TestBalancerEmptyNodes(t *testing.T) { | ||
b := influxdb.NewDataNodeBalancer([]*influxdb.DataNode{}) | ||
got := b.Next() | ||
if got != nil { | ||
t.Errorf("expected nil, got %v", got) | ||
} | ||
} | ||
|
||
func TestBalancerUp(t *testing.T) { | ||
nodes := newDataNodes() | ||
b := influxdb.NewDataNodeBalancer(nodes) | ||
|
||
// First node in randomized round-robin order | ||
first := b.Next() | ||
if first == nil { | ||
t.Errorf("expected datanode, got %v", first) | ||
} | ||
|
||
// Second node in randomized round-robin order | ||
second := b.Next() | ||
if second == nil { | ||
t.Errorf("expected datanode, got %v", second) | ||
} | ||
|
||
// Should never get the same node in order twice | ||
if first.ID == second.ID { | ||
t.Errorf("expected first != second. got %v = %v", first.ID, second.ID) | ||
} | ||
} | ||
|
||
func TestBalancerDown(t *testing.T) { | ||
nodes := newDataNodes() | ||
b := influxdb.NewDataNodeBalancer(nodes) | ||
|
||
nodes[0].Down() | ||
|
||
// First node in randomized round-robin order | ||
first := b.Next() | ||
if first == nil { | ||
t.Errorf("expected datanode, got %v", first) | ||
} | ||
|
||
// Second node should rollover to the first up node | ||
second := b.Next() | ||
if second == nil { | ||
t.Errorf("expected datanode, got %v", second) | ||
} | ||
|
||
// Health node should be returned each time | ||
if first.ID != 2 && first.ID != second.ID { | ||
t.Errorf("expected first != second. got %v = %v", first.ID, second.ID) | ||
} | ||
} | ||
|
||
func TestBalancerBackUp(t *testing.T) { | ||
nodes := newDataNodes() | ||
b := influxdb.NewDataNodeBalancer(nodes) | ||
|
||
nodes[0].Down() | ||
|
||
for i := 0; i < 3; i++ { | ||
got := b.Next() | ||
if got == nil { | ||
t.Errorf("expected datanode, got %v", got) | ||
} | ||
|
||
if exp := uint64(2); got.ID != exp { | ||
t.Errorf("wrong node id: exp %v, got %v", exp, got.ID) | ||
} | ||
} | ||
|
||
nodes[0].Up() | ||
|
||
// First node in randomized round-robin order | ||
first := b.Next() | ||
if first == nil { | ||
t.Errorf("expected datanode, got %v", first) | ||
} | ||
|
||
// Second node should rollover to the first up node | ||
second := b.Next() | ||
if second == nil { | ||
t.Errorf("expected datanode, got %v", second) | ||
} | ||
|
||
// Should get both nodes returned | ||
if first.ID == second.ID { | ||
t.Errorf("expected first != second. got %v = %v", first.ID, second.ID) | ||
} | ||
} |
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
Oops, something went wrong.