Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: waitgroup in gorouting not done in time when list runtimes has error #6473

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 42 additions & 28 deletions internal/tools/orchestrator/services/runtime/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"github.com/gorilla/schema"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"golang.org/x/sync/errgroup"
"google.golang.org/grpc/metadata"
"gopkg.in/yaml.v3"

Expand Down Expand Up @@ -1491,7 +1492,7 @@
sync.RWMutex
m map[uint64]*apistructs.RuntimeSummaryDTO
}{m: make(map[uint64]*apistructs.RuntimeSummaryDTO)}
wg := sync.WaitGroup{}
group := &errgroup.Group{}

Check warning on line 1495 in internal/tools/orchestrator/services/runtime/runtime.go

View check run for this annotation

Codecov / codecov/patch

internal/tools/orchestrator/services/runtime/runtime.go#L1495

Added line #L1495 was not covered by tests
for i := 0; i < len(runtimes); i++ {
runtime := runtimes[i]
deployment, err := r.db.FindLastDeployment(runtime.ID)
Expand Down Expand Up @@ -1521,35 +1522,48 @@
}
}
if runtime.ScheduleName.Namespace != "" && runtime.ScheduleName.Name != "" {
wg.Add(1)
go func(rt dbclient.Runtime, wg *sync.WaitGroup, servicesMap *struct {
sync.RWMutex
m map[uint64]*apistructs.RuntimeSummaryDTO
}, deployment *dbclient.Deployment, runtimeHPARules []dbclient.RuntimeHPA, runtimeVPARules []dbclient.RuntimeVPA) {
d := apistructs.RuntimeSummaryDTO{}
sg, err := r.serviceGroupImpl.InspectServiceGroupWithTimeout(rt.ScheduleName.Namespace, rt.ScheduleName.Name)
if err != nil {
l.WithError(err).Warnf("failed to inspect servicegroup: %s/%s",
rt.ScheduleName.Namespace, rt.ScheduleName.Name)
} else if sg.Status == "Ready" || sg.Status == "Healthy" {
d.Status = apistructs.RuntimeStatusHealthy
}
var dice diceyml.Object
if err = json.Unmarshal([]byte(deployment.Dice), &dice); err != nil {
logrus.Error(apierrors.ErrGetRuntime.InvalidState(strutil.Concat("dice.json invalid: ", err.Error())))
return
}
d.Services = make(map[string]*apistructs.RuntimeInspectServiceDTO)
fillRuntimeDataWithServiceGroup(&d.RuntimeInspectDTO, dice.Services, dice.Jobs, sg, nil, string(deployment.Status))
updatePARuleEnabledStatusToDisplay(runtimeHPARules, runtimeVPARules, &d.RuntimeInspectDTO)
servicesMap.Lock()
servicesMap.m[rt.ID] = &d
servicesMap.Unlock()
wg.Done()
}(runtime, &wg, &servicesMap, deployment, runtimeHPARules, runtimeVPARules)
group.Go(func() error {
return func(rt dbclient.Runtime, servicesMap *struct {
sync.RWMutex
m map[uint64]*apistructs.RuntimeSummaryDTO
}, deployment *dbclient.Deployment, runtimeHPARules []dbclient.RuntimeHPA, runtimeVPARules []dbclient.RuntimeVPA) error {
d := apistructs.RuntimeSummaryDTO{}
sg, err := r.serviceGroupImpl.InspectServiceGroupWithTimeout(rt.ScheduleName.Namespace, rt.ScheduleName.Name)
if err != nil {
l.WithError(err).Warnf("failed to inspect servicegroup: %s/%s",
rt.ScheduleName.Namespace, rt.ScheduleName.Name)
} else if sg.Status == apistructs.StatusReady || sg.Status == apistructs.RuntimeStatusHealthy {
d.Status = apistructs.RuntimeStatusHealthy
}

Check warning on line 1537 in internal/tools/orchestrator/services/runtime/runtime.go

View check run for this annotation

Codecov / codecov/patch

internal/tools/orchestrator/services/runtime/runtime.go#L1525-L1537

Added lines #L1525 - L1537 were not covered by tests

var dice *diceyml.Object
diceYaml, err := diceyml.New([]byte(deployment.Dice), false)
if err != nil {
err = apierrors.ErrGetRuntime.InvalidState(strutil.Concat("can't convert to DiceYaml: ", err.Error()))
logrus.Error(err)
return err
}
if diceYaml.Obj() == nil {
err = apierrors.ErrGetRuntime.InvalidState(strutil.Concat("dice object is nil"))
logrus.Error(err)
return err
}
dice = diceYaml.Obj()
d.Services = make(map[string]*apistructs.RuntimeInspectServiceDTO)
fillRuntimeDataWithServiceGroup(&d.RuntimeInspectDTO, dice.Services, dice.Jobs, sg, nil, string(deployment.Status))
updatePARuleEnabledStatusToDisplay(runtimeHPARules, runtimeVPARules, &d.RuntimeInspectDTO)
servicesMap.Lock()
servicesMap.m[rt.ID] = &d
servicesMap.Unlock()
return nil

Check warning on line 1558 in internal/tools/orchestrator/services/runtime/runtime.go

View check run for this annotation

Codecov / codecov/patch

internal/tools/orchestrator/services/runtime/runtime.go#L1539-L1558

Added lines #L1539 - L1558 were not covered by tests
}(runtime, &servicesMap, deployment, runtimeHPARules, runtimeVPARules)
})

}
}
wg.Wait()
if err = group.Wait(); err != nil {
return nil, err
}

Check warning on line 1566 in internal/tools/orchestrator/services/runtime/runtime.go

View check run for this annotation

Codecov / codecov/patch

internal/tools/orchestrator/services/runtime/runtime.go#L1564-L1566

Added lines #L1564 - L1566 were not covered by tests
logrus.Debug("get services finished")
return servicesMap.m, nil

Expand Down
Loading