Skip to content

Commit c7ee7e2

Browse files
committed
Prevent to setup weight for all nodes every five seconds
Only replica sets with the vshard-storage role have weight attribute. We can use this to optimize setting of replica set weight. There is two cases, when weight should not be set: 1. Current weight is null. It's mean, what this replica set isn't storage and weight wouldn't be applied. 2. Current weight of replica set is the same as weight specified in an annotation.
1 parent 8c63109 commit c7ee7e2

File tree

2 files changed

+45
-2
lines changed

2 files changed

+45
-2
lines changed

controllers/cluster_controller.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ package controllers
3131
import (
3232
"context"
3333
"fmt"
34+
"strconv"
3435
"strings"
3536
"time"
3637

@@ -314,6 +315,15 @@ func (r *ClusterReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct
314315
stsAnnotations := sts.GetAnnotations()
315316
weight := stsAnnotations["tarantool.io/replicaset-weight"]
316317

318+
current_weight, err := topologyClient.GetWeight(sts.GetLabels()["tarantool.io/replicaset-uuid"])
319+
if err != nil {
320+
return ctrl.Result{RequeueAfter: time.Duration(5 * time.Second)}, err
321+
}
322+
323+
if current_weight == -1 || strconv.Itoa(current_weight) == weight {
324+
continue
325+
}
326+
317327
if weight == "0" {
318328
reqLogger.Info("weight is set to 0, checking replicaset buckets for scheduled deletion")
319329
data, err := topologyClient.GetServerStat()

controllers/topology/builtin.go

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,9 @@ type ReplicasetsQueryResponse struct {
9999

100100
// ReplicasetData .
101101
type ReplicasetData struct {
102-
UUID string `json:"uuid"`
103-
Roles []string `json:"roles"`
102+
UUID string `json:"uuid"`
103+
Roles []string `json:"roles"`
104+
Weight *int `json:"weight"`
104105
}
105106

106107
// Statistics .
@@ -143,6 +144,10 @@ var setRsWeightMutation = `mutation editReplicaset($uuid: String!, $weight: Floa
143144
editReplicasetResponse: edit_replicaset(uuid: $uuid, weight: $weight)
144145
}`
145146

147+
var getRsWeightQuery = `query ($uuid: String!) {
148+
replicasets(uuid: $uuid) { weight }
149+
}`
150+
146151
var setRsRolesMutation = `mutation editReplicaset($uuid: String!, $roles: [String!]) {
147152
editReplicasetResponse: edit_replicaset(uuid: $uuid, roles: $roles)
148153
}`
@@ -349,6 +354,34 @@ func (s *BuiltInTopologyService) SetWeight(replicasetUUID string, replicaWeight
349354
return errors.New("something really bad happened")
350355
}
351356

357+
// GetWeight gets weight of a replicaset
358+
func (s *BuiltInTopologyService) GetWeight(replicasetUUID string) (int, error) {
359+
client := graphql.NewClient(s.serviceHost, graphql.WithHTTPClient(&http.Client{Timeout: time.Duration(time.Second * 5)}))
360+
req := graphql.NewRequest(getRsWeightQuery)
361+
362+
reqLogger := log.WithValues("namespace", "topology.builtin")
363+
364+
reqLogger.Info("getting cluster weight", "uuid", replicasetUUID)
365+
366+
req.Var("uuid", replicasetUUID)
367+
368+
resp := &ReplicasetsQueryResponse{}
369+
if err := client.Run(context.TODO(), req, resp); err != nil {
370+
return -1, err
371+
}
372+
373+
if len(resp.Replicasets) == 0 {
374+
return -1, fmt.Errorf("replicaset with uuid: '%s' not found", replicasetUUID)
375+
}
376+
377+
// Instance without role vshard-storage returns null as weight
378+
if resp.Replicasets[0].Weight == nil {
379+
return -1, nil
380+
}
381+
382+
return *resp.Replicasets[0].Weight, nil
383+
}
384+
352385
// SetReplicasetRoles set roles list of replicaset in the Tarantool service
353386
func (s *BuiltInTopologyService) SetReplicasetRoles(replicasetUUID string, roles []string) error {
354387
reqLogger := log.WithValues("namespace", "topology.builtin")

0 commit comments

Comments
 (0)