Use round-robin strategy while assigning workers
This commit is contained in:
parent
d6393d46cb
commit
32aa7270e6
|
|
@ -30,6 +30,8 @@ type Controller struct {
|
||||||
|
|
||||||
stopCh chan struct{}
|
stopCh chan struct{}
|
||||||
|
|
||||||
|
curWorkerID uint32 //initialized with 0
|
||||||
|
clusterWorkers map[spec.NamespacedName]uint32
|
||||||
clustersMu sync.RWMutex
|
clustersMu sync.RWMutex
|
||||||
clusters map[spec.NamespacedName]*cluster.Cluster
|
clusters map[spec.NamespacedName]*cluster.Cluster
|
||||||
clusterLogs map[spec.NamespacedName]ringlog.RingLogger
|
clusterLogs map[spec.NamespacedName]ringlog.RingLogger
|
||||||
|
|
@ -54,6 +56,7 @@ func NewController(controllerConfig *spec.ControllerConfig) *Controller {
|
||||||
config: *controllerConfig,
|
config: *controllerConfig,
|
||||||
opConfig: &config.Config{},
|
opConfig: &config.Config{},
|
||||||
logger: logger.WithField("pkg", "controller"),
|
logger: logger.WithField("pkg", "controller"),
|
||||||
|
clusterWorkers: make(map[spec.NamespacedName]uint32),
|
||||||
clusters: make(map[spec.NamespacedName]*cluster.Cluster),
|
clusters: make(map[spec.NamespacedName]*cluster.Cluster),
|
||||||
clusterLogs: make(map[spec.NamespacedName]ringlog.RingLogger),
|
clusterLogs: make(map[spec.NamespacedName]ringlog.RingLogger),
|
||||||
clusterHistory: make(map[spec.NamespacedName]ringlog.RingLogger),
|
clusterHistory: make(map[spec.NamespacedName]ringlog.RingLogger),
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,6 @@ package controller
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"hash/crc32"
|
|
||||||
|
|
||||||
apiextv1beta1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
|
apiextv1beta1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
|
||||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||||
|
|
@ -30,7 +29,20 @@ func (c *Controller) makeClusterConfig() cluster.Config {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Controller) clusterWorkerID(clusterName spec.NamespacedName) uint32 {
|
func (c *Controller) clusterWorkerID(clusterName spec.NamespacedName) uint32 {
|
||||||
return crc32.ChecksumIEEE([]byte(clusterName.String())) % c.opConfig.Workers
|
workerId, ok := c.clusterWorkers[clusterName]
|
||||||
|
if ok {
|
||||||
|
return workerId
|
||||||
|
}
|
||||||
|
|
||||||
|
c.clusterWorkers[clusterName] = c.curWorkerID
|
||||||
|
|
||||||
|
if c.curWorkerID == c.opConfig.Workers-1 {
|
||||||
|
c.curWorkerID = 0
|
||||||
|
} else {
|
||||||
|
c.curWorkerID++
|
||||||
|
}
|
||||||
|
|
||||||
|
return c.clusterWorkers[clusterName]
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Controller) createCRD() error {
|
func (c *Controller) createCRD() error {
|
||||||
|
|
|
||||||
|
|
@ -95,11 +95,11 @@ func TestClusterWorkerID(t *testing.T) {
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
in: spec.NamespacedName{Namespace: "foo", Name: "bar"},
|
in: spec.NamespacedName{Namespace: "foo", Name: "bar"},
|
||||||
expected: 2,
|
expected: 0,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
in: spec.NamespacedName{Namespace: "default", Name: "testcluster"},
|
in: spec.NamespacedName{Namespace: "default", Name: "testcluster"},
|
||||||
expected: 3,
|
expected: 1,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
for _, test := range testTable {
|
for _, test := range testTable {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue