Skip to content

Commit f8de577

Browse files
waited for all pods ready during generated hashring initialization (#3)
* return when encountering error * waited for all pods ready during generated hashring initialization
1 parent 16a968a commit f8de577

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

.golangci.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ linters:
2424
- perfsprint
2525
- maligned
2626
- gosec
27+
- gocognit
2728

2829
linters-settings:
2930
errcheck:

main.go

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -571,10 +571,24 @@ func (c *controller) sync(ctx context.Context) {
571571
continue
572572
}
573573

574-
// If there's an increase in replicas we poll for the new replicas to be ready
575-
if _, ok := c.replicas[sts.Name]; ok && c.replicas[sts.Name] < *sts.Spec.Replicas {
574+
stsReplica, exist := c.replicas[sts.Name]
575+
// If hashring is not initialized, need to wait for all pods ready within statefulset before generating hashring
576+
if !exist && c.options.allowOnlyReadyReplicas {
577+
for i := int32(0); i < *sts.Spec.Replicas; i++ {
578+
start := time.Now()
579+
podName := fmt.Sprintf("%s-%d", sts.Name, i)
580+
581+
if err := c.waitForPod(ctx, podName); err != nil {
582+
level.Warn(c.logger).Log("msg", "failed waiting for pod ready during hashring intialization", "pod", podName, "duration", time.Since(start), "err", err)
583+
return
584+
}
585+
586+
level.Debug(c.logger).Log("msg", "waited until new pod was ready during hashring intialization", "pod", podName, "duration", time.Since(start))
587+
}
588+
} else if exist && stsReplica < *sts.Spec.Replicas {
589+
// If there's an increase in replicas we poll for the new replicas to be ready
576590
// Iterate over new replicas to wait until they are running
577-
for i := c.replicas[sts.Name]; i < *sts.Spec.Replicas; i++ {
591+
for i := stsReplica; i < *sts.Spec.Replicas; i++ {
578592
start := time.Now()
579593
podName := fmt.Sprintf("%s-%d", sts.Name, i)
580594

0 commit comments

Comments
 (0)