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

Use explicit startup/shutdown order for development servers #5459

Merged
merged 1 commit into from
Feb 28, 2024
Merged
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
63 changes: 30 additions & 33 deletions temporal/server_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,12 @@
package temporal

import (
"cmp"
"context"
"fmt"
"sync"

"go.uber.org/multierr"
"golang.org/x/exp/slices"

"go.temporal.io/server/common/cluster"
"go.temporal.io/server/common/config"
Expand Down Expand Up @@ -59,6 +60,17 @@ type (
}
)

// When starting multiple services in one process (typically a development server), start them
// in this order and stop them in the reverse order. This most important part here is that the
// worker depends on the frontend, which depends on matching and history.
var initOrder = map[primitives.ServiceName]int{
primitives.MatchingService: 1,
primitives.HistoryService: 2,
primitives.InternalFrontendService: 3,
primitives.FrontendService: 3,
primitives.WorkerService: 4,
}

// NewServerFxImpl returns a new instance of server that serves one or many services.
func NewServerFxImpl(
opts *serverOptions,
Expand Down Expand Up @@ -110,19 +122,16 @@ func (s *ServerImpl) Start(ctx context.Context) error {
}

func (s *ServerImpl) Stop(ctx context.Context) error {
var wg sync.WaitGroup
wg.Add(len(s.servicesMetadata))
close(s.stoppedCh)

for _, svcMeta := range s.servicesMetadata {
go func(svc *ServicesMetadata) {
svc.Stop(ctx)
wg.Done()
}(svcMeta)
svcs := slices.Clone(s.servicesMetadata)
slices.SortFunc(svcs, func(a, b *ServicesMetadata) int {
return -cmp.Compare(initOrder[a.serviceName], initOrder[b.serviceName]) // note negative
})
for _, svc := range svcs {
svc.Stop(ctx)
}

wg.Wait()

if s.so.metricHandler != nil {
s.so.metricHandler.Stop(s.logger)
}
Expand All @@ -135,32 +144,20 @@ func (s *ServerImpl) startServices() error {
timeout := max(serviceStartTimeout, 2*s.so.config.Global.Membership.MaxJoinDuration)
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
results := make(chan startServiceResult, len(s.servicesMetadata))
for _, svcMeta := range s.servicesMetadata {
go func(svcMeta *ServicesMetadata) {
err := svcMeta.app.Start(ctx)
results <- startServiceResult{
svc: svcMeta,
err: err,
}
}(svcMeta)
}
return s.readResults(results)
}

func (s *ServerImpl) readResults(results chan startServiceResult) (err error) {
for range s.servicesMetadata {
r := <-results
if r.err != nil {
err = multierr.Combine(err, fmt.Errorf("failed to start service %v: %w", r.svc.serviceName, r.err))
svcs := slices.Clone(s.servicesMetadata)
slices.SortFunc(svcs, func(a, b *ServicesMetadata) int {
return cmp.Compare(initOrder[a.serviceName], initOrder[b.serviceName])
})

var allErrs error
for _, svc := range svcs {
err := svc.app.Start(ctx)
if err != nil {
allErrs = multierr.Append(allErrs, fmt.Errorf("failed to start service %v: %w", svc.serviceName, err))
}
}
return
}

type startServiceResult struct {
svc *ServicesMetadata
err error
return allErrs
}

func initSystemNamespaces(
Expand Down
Loading