mirror of
https://github.com/hanzoai/tasks.git
synced 2026-10-04 02:04:03 +00:00
## What changed? <!-- Describe what has changed in this PR --> Added `WithStaticHosts` start option. It is not explicitly exposed via the Server's CLI options. I'm assuming most users start the Server via https://github.com/temporalio/cli. **In a follow-up PR, I will add this option to the CLI dev server.** ## Why? Dynamic service membership isn't always required; when running all services locally, a static host map is sufficient. This will reduce the startup time slightly - but more importantly, not print "Not enough hosts to serve the request" randomly anymore. This change is based on the assumption that fixing the root cause of this issue is very much non-trivial. Therefore, opting out of the complex membership machinery when it's not needed seemed like a pragmatic change. ## How did you test it? <!-- How have you verified this change? Tested locally? Added a unit test? Checked in staging env? --> For the regular path, dynamic membership, there's a test case. ### ✅ (Minor) startup speedup Ran a simple Workflow Update example; with and without the option. `hyperfine --runs 10 'go run cmd/main.go'` | Before | **After** | |--------|--------| | <img width="632" alt="Screenshot 2024-03-07 at 8 38 41 PM" src="https://github.com/temporalio/temporal/assets/159852/6d237e1b-e976-4694-ab8e-41b7b06cb263"> | <img width="628" alt="Screenshot 2024-03-07 at 8 39 19 PM" src="https://github.com/temporalio/temporal/assets/159852/25452405-e756-4df7-8e14-957a1c4688dc"> | ### ✅ Less noisy <details><summary>script</summary> <p> ``` for i in {1..10}; do echo "Run #$i" output=$( { go run cmd/main.go ; } 2>&1 ) if echo "$output" | grep -q "Not enough hosts to serve the request"; then echo "'Not enough hosts to serve the request'" fi done ``` </p> </details> | Before | **After** | |--------|--------| | <img width="325" alt="Screenshot 2024-03-07 at 8 47 49 PM" src="https://github.com/temporalio/temporal/assets/159852/a3579f63-adf2-40af-ad08-432a9a73cc8f"> | <img width="68" alt="Screenshot 2024-03-07 at 8 48 30 PM" src="https://github.com/temporalio/temporal/assets/159852/cf568a56-2878-499f-97ab-a13f5c056311"> | ## Potential risks <!-- Assuming the worst case, what can be broken when deploying this change to production? --> ## Documentation <!-- Have you made sure this change doesn't falsify anything currently stated in `docs/`? If significant new behavior is added, have you described that in `docs/`? --> ## Is hotfix candidate? <!-- Is this PR a hotfix candidate or does it require a notification to be sent to the broader community? (Yes/No) --> --------- Co-authored-by: David Reiss <[email protected]>
140 lines
4.3 KiB
Go
140 lines
4.3 KiB
Go
// The MIT License
|
|
//
|
|
// Copyright (c) 2020 Temporal Technologies Inc. All rights reserved.
|
|
//
|
|
// Copyright (c) 2020 Uber Technologies, Inc.
|
|
//
|
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
// of this software and associated documentation files (the "Software"), to deal
|
|
// in the Software without restriction, including without limitation the rights
|
|
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
// copies of the Software, and to permit persons to whom the Software is
|
|
// furnished to do so, subject to the following conditions:
|
|
//
|
|
// The above copyright notice and this permission notice shall be included in
|
|
// all copies or substantial portions of the Software.
|
|
//
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
// THE SOFTWARE.
|
|
|
|
package temporal
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
|
|
"golang.org/x/exp/slices"
|
|
"google.golang.org/grpc"
|
|
|
|
"go.temporal.io/server/client"
|
|
"go.temporal.io/server/common/authorization"
|
|
"go.temporal.io/server/common/config"
|
|
"go.temporal.io/server/common/dynamicconfig"
|
|
"go.temporal.io/server/common/log"
|
|
"go.temporal.io/server/common/membership/static"
|
|
"go.temporal.io/server/common/metrics"
|
|
persistenceClient "go.temporal.io/server/common/persistence/client"
|
|
"go.temporal.io/server/common/persistence/visibility"
|
|
"go.temporal.io/server/common/primitives"
|
|
"go.temporal.io/server/common/resolver"
|
|
"go.temporal.io/server/common/rpc/encryption"
|
|
"go.temporal.io/server/common/searchattribute"
|
|
)
|
|
|
|
type (
|
|
synchronizationModeParams struct {
|
|
blockingStart bool
|
|
interruptCh <-chan interface{}
|
|
}
|
|
|
|
serverOptions struct {
|
|
serviceNames map[primitives.ServiceName]struct{}
|
|
|
|
config *config.Config
|
|
configDir string
|
|
env string
|
|
zone string
|
|
hostsByService map[primitives.ServiceName]static.Hosts
|
|
|
|
startupSynchronizationMode synchronizationModeParams
|
|
|
|
logger log.Logger
|
|
namespaceLogger log.Logger
|
|
authorizer authorization.Authorizer
|
|
tlsConfigProvider encryption.TLSConfigProvider
|
|
claimMapper authorization.ClaimMapper
|
|
audienceGetter authorization.JWTAudienceMapper
|
|
persistenceServiceResolver resolver.ServiceResolver
|
|
elasticsearchHttpClient *http.Client
|
|
dynamicConfigClient dynamicconfig.Client
|
|
customDataStoreFactory persistenceClient.AbstractDataStoreFactory
|
|
customVisibilityStoreFactory visibility.VisibilityStoreFactory
|
|
clientFactoryProvider client.FactoryProvider
|
|
searchAttributesMapper searchattribute.Mapper
|
|
customFrontendInterceptors []grpc.UnaryServerInterceptor
|
|
metricHandler metrics.Handler
|
|
}
|
|
)
|
|
|
|
func newServerOptions(opts []ServerOption) *serverOptions {
|
|
so := &serverOptions{
|
|
// Set defaults here.
|
|
persistenceServiceResolver: resolver.NewNoopResolver(),
|
|
}
|
|
for _, opt := range opts {
|
|
opt.apply(so)
|
|
}
|
|
|
|
return so
|
|
}
|
|
|
|
func (so *serverOptions) loadAndValidate() error {
|
|
for serviceName := range so.serviceNames {
|
|
if !slices.Contains(Services, string(serviceName)) {
|
|
return fmt.Errorf("invalid service %q in service list %v", serviceName, so.serviceNames)
|
|
}
|
|
}
|
|
|
|
if so.config == nil {
|
|
err := so.loadConfig()
|
|
if err != nil {
|
|
return fmt.Errorf("unable to load config: %w", err)
|
|
}
|
|
}
|
|
|
|
err := so.validateConfig()
|
|
if err != nil {
|
|
return fmt.Errorf("config validation error: %w", err)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (so *serverOptions) loadConfig() error {
|
|
so.config = &config.Config{}
|
|
err := config.Load(so.env, so.configDir, so.zone, so.config)
|
|
if err != nil {
|
|
return fmt.Errorf("config file corrupted: %w", err)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (so *serverOptions) validateConfig() error {
|
|
if err := so.config.Validate(); err != nil {
|
|
return err
|
|
}
|
|
|
|
for name := range so.serviceNames {
|
|
if _, ok := so.config.Services[string(name)]; !ok {
|
|
return fmt.Errorf("%q service is missing in config", name)
|
|
}
|
|
}
|
|
return nil
|
|
}
|