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 --> Instead of using fixed ports in functional tests, use randomized ports. The nested map is ... not pretty but effective. Happy to throw more design at it, if needed. ## Why? <!-- Tell your future self why have you made these changes --> When starting two or more clusters (e.g. xdc tests), different hosts have to be used for the ports not to collide; which causes issues on at least MacOS. ## How did you test it? <!-- How have you verified this change? Tested locally? Added a unit test? Checked in staging env? --> Existing tests. ## 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) -->
358 lines
10 KiB
Go
358 lines
10 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 rpc
|
|
|
|
import (
|
|
"crypto/tls"
|
|
"fmt"
|
|
"math/rand"
|
|
"net"
|
|
"net/http"
|
|
"net/url"
|
|
"sync"
|
|
"time"
|
|
|
|
"go.temporal.io/api/serviceerror"
|
|
"go.temporal.io/server/common"
|
|
"go.temporal.io/server/common/cache"
|
|
"go.temporal.io/server/common/config"
|
|
"go.temporal.io/server/common/convert"
|
|
"go.temporal.io/server/common/log"
|
|
"go.temporal.io/server/common/log/tag"
|
|
"go.temporal.io/server/common/membership"
|
|
"go.temporal.io/server/common/primitives"
|
|
"go.temporal.io/server/common/rpc/encryption"
|
|
"go.temporal.io/server/environment"
|
|
"google.golang.org/grpc"
|
|
"google.golang.org/grpc/credentials"
|
|
)
|
|
|
|
var _ common.RPCFactory = (*RPCFactory)(nil)
|
|
|
|
// RPCFactory is an implementation of common.RPCFactory interface
|
|
type RPCFactory struct {
|
|
config *config.RPC
|
|
serviceName primitives.ServiceName
|
|
logger log.Logger
|
|
|
|
frontendURL string
|
|
frontendHTTPURL string
|
|
frontendHTTPPort int
|
|
frontendTLSConfig *tls.Config
|
|
|
|
grpcListener func() net.Listener
|
|
tlsFactory encryption.TLSConfigProvider
|
|
dialOptions []grpc.DialOption
|
|
monitor membership.Monitor
|
|
// A OnceValues wrapper for createLocalFrontendHTTPClient.
|
|
localFrontendClient func() (*common.FrontendHTTPClient, error)
|
|
interNodeGrpcConnections cache.Cache
|
|
}
|
|
|
|
// NewFactory builds a new RPCFactory
|
|
// conforming to the underlying configuration
|
|
func NewFactory(
|
|
cfg *config.RPC,
|
|
sName primitives.ServiceName,
|
|
logger log.Logger,
|
|
tlsProvider encryption.TLSConfigProvider,
|
|
frontendURL string,
|
|
frontendHTTPURL string,
|
|
frontendHTTPPort int,
|
|
frontendTLSConfig *tls.Config,
|
|
dialOptions []grpc.DialOption,
|
|
monitor membership.Monitor,
|
|
) *RPCFactory {
|
|
f := &RPCFactory{
|
|
config: cfg,
|
|
serviceName: sName,
|
|
logger: logger,
|
|
frontendURL: frontendURL,
|
|
frontendHTTPURL: frontendHTTPURL,
|
|
frontendHTTPPort: frontendHTTPPort,
|
|
frontendTLSConfig: frontendTLSConfig,
|
|
tlsFactory: tlsProvider,
|
|
dialOptions: dialOptions,
|
|
monitor: monitor,
|
|
}
|
|
f.grpcListener = sync.OnceValue(f.createGRPCListener)
|
|
f.localFrontendClient = sync.OnceValues(f.createLocalFrontendHTTPClient)
|
|
f.interNodeGrpcConnections = cache.NewSimple(nil)
|
|
return f
|
|
}
|
|
|
|
func (d *RPCFactory) GetFrontendGRPCServerOptions() ([]grpc.ServerOption, error) {
|
|
var opts []grpc.ServerOption
|
|
|
|
if d.tlsFactory != nil {
|
|
serverConfig, err := d.tlsFactory.GetFrontendServerConfig()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if serverConfig == nil {
|
|
return opts, nil
|
|
}
|
|
opts = append(opts, grpc.Creds(credentials.NewTLS(serverConfig)))
|
|
}
|
|
|
|
return opts, nil
|
|
}
|
|
|
|
func (d *RPCFactory) GetFrontendClientTlsConfig() (*tls.Config, error) {
|
|
if d.tlsFactory != nil {
|
|
return d.tlsFactory.GetFrontendClientConfig()
|
|
}
|
|
|
|
return nil, nil
|
|
}
|
|
|
|
func (d *RPCFactory) GetRemoteClusterClientConfig(hostname string) (*tls.Config, error) {
|
|
if d.tlsFactory != nil {
|
|
return d.tlsFactory.GetRemoteClusterClientConfig(hostname)
|
|
}
|
|
|
|
return nil, nil
|
|
}
|
|
|
|
func (d *RPCFactory) GetInternodeGRPCServerOptions() ([]grpc.ServerOption, error) {
|
|
var opts []grpc.ServerOption
|
|
|
|
if d.tlsFactory != nil {
|
|
serverConfig, err := d.tlsFactory.GetInternodeServerConfig()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if serverConfig == nil {
|
|
return opts, nil
|
|
}
|
|
opts = append(opts, grpc.Creds(credentials.NewTLS(serverConfig)))
|
|
}
|
|
|
|
return opts, nil
|
|
}
|
|
|
|
func (d *RPCFactory) GetInternodeClientTlsConfig() (*tls.Config, error) {
|
|
if d.tlsFactory != nil {
|
|
return d.tlsFactory.GetInternodeClientConfig()
|
|
}
|
|
|
|
return nil, nil
|
|
}
|
|
|
|
// GetGRPCListener returns cached dispatcher for gRPC inbound or creates one
|
|
func (d *RPCFactory) GetGRPCListener() net.Listener {
|
|
return d.grpcListener()
|
|
}
|
|
|
|
func (d *RPCFactory) createGRPCListener() net.Listener {
|
|
hostAddress := net.JoinHostPort(getListenIP(d.config, d.logger).String(), convert.IntToString(d.config.GRPCPort))
|
|
|
|
grpcListener, err := net.Listen("tcp", hostAddress)
|
|
if err != nil || grpcListener == nil || grpcListener.Addr() == nil {
|
|
d.logger.Fatal("Failed to start gRPC listener", tag.Error(err), tag.Service(d.serviceName), tag.Address(hostAddress))
|
|
}
|
|
|
|
d.logger.Info("Created gRPC listener", tag.Service(d.serviceName), tag.Address(hostAddress))
|
|
return grpcListener
|
|
}
|
|
|
|
func getListenIP(cfg *config.RPC, logger log.Logger) net.IP {
|
|
if cfg.BindOnLocalHost && len(cfg.BindOnIP) > 0 {
|
|
logger.Fatal("ListenIP failed, bindOnLocalHost and bindOnIP are mutually exclusive")
|
|
return nil
|
|
}
|
|
|
|
if cfg.BindOnLocalHost {
|
|
return net.ParseIP(environment.GetLocalhostIP())
|
|
}
|
|
|
|
if len(cfg.BindOnIP) > 0 {
|
|
ip := net.ParseIP(cfg.BindOnIP)
|
|
if ip != nil {
|
|
return ip
|
|
}
|
|
logger.Fatal("ListenIP failed, unable to parse bindOnIP value", tag.Address(cfg.BindOnIP))
|
|
return nil
|
|
}
|
|
ip, err := config.ListenIP()
|
|
if err != nil {
|
|
logger.Fatal("ListenIP failed", tag.Error(err))
|
|
return nil
|
|
}
|
|
return ip
|
|
}
|
|
|
|
// CreateRemoteFrontendGRPCConnection creates connection for gRPC calls
|
|
func (d *RPCFactory) CreateRemoteFrontendGRPCConnection(rpcAddress string) *grpc.ClientConn {
|
|
var tlsClientConfig *tls.Config
|
|
var err error
|
|
if d.tlsFactory != nil {
|
|
hostname, _, err2 := net.SplitHostPort(rpcAddress)
|
|
if err2 != nil {
|
|
d.logger.Fatal("Invalid rpcAddress for remote cluster", tag.Error(err2))
|
|
}
|
|
tlsClientConfig, err = d.tlsFactory.GetRemoteClusterClientConfig(hostname)
|
|
|
|
if err != nil {
|
|
d.logger.Fatal("Failed to create tls config for gRPC connection", tag.Error(err))
|
|
return nil
|
|
}
|
|
}
|
|
|
|
return d.dial(rpcAddress, tlsClientConfig)
|
|
}
|
|
|
|
// CreateLocalFrontendGRPCConnection creates connection for internal frontend calls
|
|
func (d *RPCFactory) CreateLocalFrontendGRPCConnection() *grpc.ClientConn {
|
|
return d.dial(d.frontendURL, d.frontendTLSConfig)
|
|
}
|
|
|
|
// CreateInternodeGRPCConnection creates connection for gRPC calls
|
|
func (d *RPCFactory) CreateInternodeGRPCConnection(hostName string) *grpc.ClientConn {
|
|
if c, ok := d.interNodeGrpcConnections.Get(hostName).(*grpc.ClientConn); ok {
|
|
return c
|
|
}
|
|
var tlsClientConfig *tls.Config
|
|
var err error
|
|
if d.tlsFactory != nil {
|
|
tlsClientConfig, err = d.tlsFactory.GetInternodeClientConfig()
|
|
if err != nil {
|
|
d.logger.Fatal("Failed to create tls config for gRPC connection", tag.Error(err))
|
|
return nil
|
|
}
|
|
}
|
|
c := d.dial(hostName, tlsClientConfig)
|
|
d.interNodeGrpcConnections.Put(hostName, c)
|
|
return c
|
|
}
|
|
|
|
func (d *RPCFactory) dial(hostName string, tlsClientConfig *tls.Config) *grpc.ClientConn {
|
|
connection, err := Dial(hostName, tlsClientConfig, d.logger, d.dialOptions...)
|
|
if err != nil {
|
|
d.logger.Fatal("Failed to create gRPC connection", tag.Error(err))
|
|
return nil
|
|
}
|
|
|
|
return connection
|
|
}
|
|
|
|
func (d *RPCFactory) GetTLSConfigProvider() encryption.TLSConfigProvider {
|
|
return d.tlsFactory
|
|
}
|
|
|
|
// CreateLocalFrontendHTTPClient gets or creates a cached frontend client.
|
|
func (d *RPCFactory) CreateLocalFrontendHTTPClient() (*common.FrontendHTTPClient, error) {
|
|
return d.localFrontendClient()
|
|
}
|
|
|
|
// createLocalFrontendHTTPClient creates an HTTP client for communicating with the frontend.
|
|
// It uses either the provided frontendURL or membership to resolve the frontend address.
|
|
func (d *RPCFactory) createLocalFrontendHTTPClient() (*common.FrontendHTTPClient, error) {
|
|
// dialer and transport field values copied from http.DefaultTransport.
|
|
dialer := &net.Dialer{
|
|
Timeout: 30 * time.Second,
|
|
KeepAlive: 30 * time.Second,
|
|
}
|
|
transport := &http.Transport{
|
|
Proxy: http.ProxyFromEnvironment,
|
|
DialContext: dialer.DialContext,
|
|
ForceAttemptHTTP2: true,
|
|
MaxIdleConns: 100,
|
|
IdleConnTimeout: 90 * time.Second,
|
|
TLSHandshakeTimeout: 10 * time.Second,
|
|
ExpectContinueTimeout: 1 * time.Second,
|
|
}
|
|
client := http.Client{}
|
|
|
|
// Default to http unless TLS is configured.
|
|
scheme := "http"
|
|
if d.frontendTLSConfig != nil {
|
|
transport.TLSClientConfig = d.frontendTLSConfig
|
|
scheme = "https"
|
|
}
|
|
|
|
var address string
|
|
if r := serviceResolverFromGRPCURL(d.frontendHTTPURL); r != nil {
|
|
client.Transport = &roundTripper{
|
|
resolver: r,
|
|
underlying: transport,
|
|
httpPort: d.frontendHTTPPort,
|
|
}
|
|
address = "internal" // This will be replaced by the roundTripper
|
|
} else {
|
|
// Use the URL as-is and leave the transport unmodified.
|
|
client.Transport = transport
|
|
address = d.frontendHTTPURL
|
|
}
|
|
|
|
return &common.FrontendHTTPClient{
|
|
Client: client,
|
|
Address: address,
|
|
Scheme: scheme,
|
|
}, nil
|
|
}
|
|
|
|
type roundTripper struct {
|
|
resolver membership.ServiceResolver
|
|
underlying http.RoundTripper
|
|
httpPort int
|
|
}
|
|
|
|
func (rt *roundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
|
|
// Pick a frontend host at random.
|
|
members := rt.resolver.AvailableMembers()
|
|
if len(members) == 0 {
|
|
return nil, serviceerror.NewUnavailable("no frontend host to route request to")
|
|
}
|
|
idx := rand.Intn(len(members))
|
|
member := members[idx]
|
|
|
|
// Replace port with the HTTP port.
|
|
host, _, err := net.SplitHostPort(member.Identity())
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to extract port from frontend member: %w", err)
|
|
}
|
|
address := fmt.Sprintf("%s:%d", host, rt.httpPort)
|
|
|
|
// Replace request's host.
|
|
req.URL.Host = address
|
|
req.Host = address
|
|
return rt.underlying.RoundTrip(req)
|
|
}
|
|
|
|
// serviceResolverFromGRPCURL returns a ServiceResolver if ustr corresponds to a
|
|
// membership url, otherwise nil.
|
|
func serviceResolverFromGRPCURL(ustr string) membership.ServiceResolver {
|
|
u, err := url.Parse(ustr)
|
|
if err != nil {
|
|
return nil
|
|
}
|
|
res, err := membership.GetServiceResolverFromURL(u)
|
|
if err != nil {
|
|
return nil
|
|
}
|
|
return res
|
|
}
|