forked from cockroachdb/cockroach
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ccl/sqlproxyccl: add basic Balancer component for load balancing
Previously, all the load balancing logic is within the directory cache, which makes it difficult to implement the connection rebalancing feature. This commit extracts all of those logic out of the directory cache into a new Balancer component, which will be responsible for load balancing moving forward. For that to work, the directory cache now will only return pods in two forms: - LookupTenantPods - TryLookupTenantPods The "try" variant will return an error if the cluster hasn't been fetched into the directory's cache yet, and will never attempt to resume the tenant if there are no available pods. LookupTenantPods will block until there is at least one available SQL pod. Release note: None
- Loading branch information
1 parent
07b7e27
commit 7235584
Showing
15 changed files
with
433 additions
and
268 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
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,31 @@ | ||
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") | ||
|
||
go_library( | ||
name = "balancer", | ||
srcs = [ | ||
"balancer.go", | ||
"pod.go", | ||
], | ||
importpath = "github.com/cockroachdb/cockroach/pkg/ccl/sqlproxyccl/balancer", | ||
visibility = ["//visibility:public"], | ||
deps = [ | ||
"//pkg/ccl/sqlproxyccl/tenant", | ||
"//pkg/util/randutil", | ||
"//pkg/util/syncutil", | ||
"@com_github_cockroachdb_errors//:errors", | ||
], | ||
) | ||
|
||
go_test( | ||
name = "balancer_test", | ||
srcs = [ | ||
"balancer_test.go", | ||
"pod_test.go", | ||
], | ||
embed = [":balancer"], | ||
deps = [ | ||
"//pkg/ccl/sqlproxyccl/tenant", | ||
"//pkg/util/leaktest", | ||
"@com_github_stretchr_testify//require", | ||
], | ||
) |
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,62 @@ | ||
// Copyright 2022 The Cockroach Authors. | ||
// | ||
// Licensed as a CockroachDB Enterprise file under the Cockroach Community | ||
// License (the "License"); you may not use this file except in compliance with | ||
// the License. You may obtain a copy of the License at | ||
// | ||
// https://github.com/cockroachdb/cockroach/blob/master/licenses/CCL.txt | ||
|
||
package balancer | ||
|
||
import ( | ||
"math/rand" | ||
|
||
"github.com/cockroachdb/cockroach/pkg/ccl/sqlproxyccl/tenant" | ||
"github.com/cockroachdb/cockroach/pkg/util/randutil" | ||
"github.com/cockroachdb/cockroach/pkg/util/syncutil" | ||
"github.com/cockroachdb/errors" | ||
) | ||
|
||
// ErrNoAvailablePods is an error that indicates that no pods are available | ||
// for selection. | ||
var ErrNoAvailablePods = errors.New("no available pods") | ||
|
||
// Balancer handles load balancing of SQL connections within the proxy. | ||
// All methods on the Balancer instance are thread-safe. | ||
type Balancer struct { | ||
// mu synchronizes access to fields in the struct. | ||
mu struct { | ||
syncutil.Mutex | ||
|
||
// rng corresponds to the random number generator instance which will | ||
// be used for load balancing. | ||
rng *rand.Rand | ||
} | ||
} | ||
|
||
// NewBalancer constructs a new Balancer instance that is responsible for | ||
// load balancing SQL connections within the proxy. | ||
func NewBalancer() *Balancer { | ||
b := &Balancer{} | ||
b.mu.rng, _ = randutil.NewPseudoRand() | ||
return b | ||
} | ||
|
||
// SelectTenantPod selects a tenant pod from the given list based on a weighted | ||
// CPU load algorithm. It is expected that all pods within the list belongs to | ||
// the same tenant. If no pods are available, this returns ErrNoAvailablePods. | ||
func (b *Balancer) SelectTenantPod(pods []*tenant.Pod) (*tenant.Pod, error) { | ||
pod := selectTenantPod(b.randFloat32(), pods) | ||
if pod == nil { | ||
return nil, ErrNoAvailablePods | ||
} | ||
return pod, nil | ||
} | ||
|
||
// randFloat32 generates a random float32 within the bounds [0, 1) and is | ||
// thread-safe. | ||
func (b *Balancer) randFloat32() float32 { | ||
b.mu.Lock() | ||
defer b.mu.Unlock() | ||
return b.mu.rng.Float32() | ||
} |
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,36 @@ | ||
// Copyright 2022 The Cockroach Authors. | ||
// | ||
// Licensed as a CockroachDB Enterprise file under the Cockroach Community | ||
// License (the "License"); you may not use this file except in compliance with | ||
// the License. You may obtain a copy of the License at | ||
// | ||
// https://github.com/cockroachdb/cockroach/blob/master/licenses/CCL.txt | ||
|
||
package balancer_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/cockroachdb/cockroach/pkg/ccl/sqlproxyccl/balancer" | ||
"github.com/cockroachdb/cockroach/pkg/ccl/sqlproxyccl/tenant" | ||
"github.com/cockroachdb/cockroach/pkg/util/leaktest" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestBalancer(t *testing.T) { | ||
defer leaktest.AfterTest(t)() | ||
|
||
b := balancer.NewBalancer() | ||
|
||
t.Run("no pods", func(t *testing.T) { | ||
pod, err := b.SelectTenantPod([]*tenant.Pod{}) | ||
require.EqualError(t, err, balancer.ErrNoAvailablePods.Error()) | ||
require.Nil(t, pod) | ||
}) | ||
|
||
t.Run("few pods", func(t *testing.T) { | ||
pod, err := b.SelectTenantPod([]*tenant.Pod{{Addr: "1"}, {Addr: "2"}}) | ||
require.NoError(t, err) | ||
require.Contains(t, []string{"1", "2"}, pod.Addr) | ||
}) | ||
} |
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,54 @@ | ||
// Copyright 2022 The Cockroach Authors. | ||
// | ||
// Licensed as a CockroachDB Enterprise file under the Cockroach Community | ||
// License (the "License"); you may not use this file except in compliance with | ||
// the License. You may obtain a copy of the License at | ||
// | ||
// https://github.com/cockroachdb/cockroach/blob/master/licenses/CCL.txt | ||
|
||
package balancer | ||
|
||
import ( | ||
"math/rand" | ||
"testing" | ||
|
||
"github.com/cockroachdb/cockroach/pkg/ccl/sqlproxyccl/tenant" | ||
"github.com/cockroachdb/cockroach/pkg/util/leaktest" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestSelectTenantPods(t *testing.T) { | ||
defer leaktest.AfterTest(t)() | ||
|
||
t.Run("no pods", func(t *testing.T) { | ||
require.Nil(t, selectTenantPod(0, nil)) | ||
}) | ||
|
||
t.Run("one pod", func(t *testing.T) { | ||
pod := selectTenantPod(0, []*tenant.Pod{{Addr: "1"}}) | ||
require.Equal(t, "1", pod.Addr) | ||
}) | ||
|
||
t.Run("many pods", func(t *testing.T) { | ||
pods := []*tenant.Pod{ | ||
{Addr: "1", Load: 0.0}, | ||
{Addr: "2", Load: 0.5}, | ||
{Addr: "3", Load: 0.9}, | ||
} | ||
|
||
distribution := map[string]int{} | ||
rng := rand.New(rand.NewSource(0)) | ||
|
||
for i := 0; i < 10000; i++ { | ||
pod := selectTenantPod(rng.Float32(), pods) | ||
distribution[pod.Addr]++ | ||
} | ||
|
||
// Assert that the distribution is a roughly function of 1 - Load. | ||
require.Equal(t, map[string]int{ | ||
"1": 6121, | ||
"2": 3214, | ||
"3": 665, | ||
}, distribution) | ||
}) | ||
} |
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.