mirror of
https://github.com/hanzoai/tasks.git
synced 2026-10-30 14:56:19 +00:00
<!-- Describe what has changed in this PR --> **What changed?** This moves the membership monitor Start call from a fx lifecycle hook to each individual service's start method. Note: this is on top of https://github.com/temporalio/temporal/pull/4506 , which is needed so that services can still get the hostinfo (server address) to use when they, eg, initialize their service tagged loggers. <!-- Tell your future self why have you made these changes --> **Why?** In short, because a service instance shouldn't join ringpop, and hence begin to have requests forwarded to it, until it is ready to receive them. In internal testing, focusing on the impact of history service restarts/upgrades, we've seen requests suffer unnecessary delay because they were forwarded to a new history pod before it began accepting on its announced service address. <!-- How have you verified this change? Tested locally? Added a unit test? Checked in staging env? --> **How did you test it?** This has been tested in a staging environment, and for each service (listener, matching, history, worker), scaling up & down pod counts, including to/from zero. ~~Note that with this change, the history service currently generates errors at startup as it attempts lookups during its fx start sequence (inside controller_impl.go), but it will acquire shards as expected. I'll address the error logs in a quick followup PR.~~ (I addressed this in this PR.) <!-- Assuming the worst case, what can be broken when deploying this change to production? --> **Potential risks** If there's a condition that we haven't seen in testing that causes services to lookup ringpop information during fx start time, they'll see errors in places that haven't before. That could manifest as services failing to start up, or failing to take on work as quickly as they had. <!-- Is this PR a hotfix candidate or require that a notification be sent to the broader community? (Yes/No) --> **Is hotfix candidate?** No.
147 lines
5.1 KiB
Go
147 lines
5.1 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 matching
|
|
|
|
import (
|
|
"math/rand"
|
|
"net"
|
|
"sync/atomic"
|
|
"time"
|
|
|
|
"google.golang.org/grpc"
|
|
"google.golang.org/grpc/health"
|
|
healthpb "google.golang.org/grpc/health/grpc_health_v1"
|
|
|
|
"go.temporal.io/server/api/matchingservice/v1"
|
|
"go.temporal.io/server/common"
|
|
"go.temporal.io/server/common/log"
|
|
"go.temporal.io/server/common/log/tag"
|
|
"go.temporal.io/server/common/membership"
|
|
"go.temporal.io/server/common/metrics"
|
|
"go.temporal.io/server/common/persistence/client"
|
|
"go.temporal.io/server/common/persistence/visibility/manager"
|
|
)
|
|
|
|
// Service represents the matching service
|
|
type Service struct {
|
|
status int32
|
|
handler *Handler
|
|
config *Config
|
|
|
|
server *grpc.Server
|
|
logger log.SnTaggedLogger
|
|
membershipMonitor membership.Monitor
|
|
grpcListener net.Listener
|
|
runtimeMetricsReporter *metrics.RuntimeMetricsReporter
|
|
metricsHandler metrics.Handler
|
|
faultInjectionDataStoreFactory *client.FaultInjectionDataStoreFactory
|
|
healthServer *health.Server
|
|
visibilityManager manager.VisibilityManager
|
|
}
|
|
|
|
func NewService(
|
|
grpcServerOptions []grpc.ServerOption,
|
|
serviceConfig *Config,
|
|
logger log.SnTaggedLogger,
|
|
membershipMonitor membership.Monitor,
|
|
grpcListener net.Listener,
|
|
runtimeMetricsReporter *metrics.RuntimeMetricsReporter,
|
|
handler *Handler,
|
|
metricsHandler metrics.Handler,
|
|
faultInjectionDataStoreFactory *client.FaultInjectionDataStoreFactory,
|
|
healthServer *health.Server,
|
|
visibilityManager manager.VisibilityManager,
|
|
) *Service {
|
|
return &Service{
|
|
status: common.DaemonStatusInitialized,
|
|
config: serviceConfig,
|
|
server: grpc.NewServer(grpcServerOptions...),
|
|
handler: handler,
|
|
logger: logger,
|
|
membershipMonitor: membershipMonitor,
|
|
grpcListener: grpcListener,
|
|
runtimeMetricsReporter: runtimeMetricsReporter,
|
|
metricsHandler: metricsHandler,
|
|
faultInjectionDataStoreFactory: faultInjectionDataStoreFactory,
|
|
healthServer: healthServer,
|
|
visibilityManager: visibilityManager,
|
|
}
|
|
}
|
|
|
|
// Start starts the service
|
|
func (s *Service) Start() {
|
|
if !atomic.CompareAndSwapInt32(&s.status, common.DaemonStatusInitialized, common.DaemonStatusStarted) {
|
|
return
|
|
}
|
|
|
|
s.logger.Info("matching starting")
|
|
|
|
// must start base service first
|
|
s.metricsHandler.Counter(metrics.RestartCount).Record(1)
|
|
rand.Seed(time.Now().UnixNano())
|
|
|
|
s.handler.Start()
|
|
|
|
matchingservice.RegisterMatchingServiceServer(s.server, s.handler)
|
|
healthpb.RegisterHealthServer(s.server, s.healthServer)
|
|
s.healthServer.SetServingStatus(serviceName, healthpb.HealthCheckResponse_SERVING)
|
|
|
|
go s.membershipMonitor.Start()
|
|
|
|
s.logger.Info("Starting to serve on matching listener")
|
|
if err := s.server.Serve(s.grpcListener); err != nil {
|
|
s.logger.Fatal("Failed to serve on matching listener", tag.Error(err))
|
|
}
|
|
}
|
|
|
|
// Stop stops the service
|
|
func (s *Service) Stop() {
|
|
if !atomic.CompareAndSwapInt32(&s.status, common.DaemonStatusStarted, common.DaemonStatusStopped) {
|
|
return
|
|
}
|
|
|
|
// remove self from membership ring and wait for traffic to drain
|
|
s.logger.Info("ShutdownHandler: Evicting self from membership ring")
|
|
if err := s.membershipMonitor.EvictSelf(); err != nil {
|
|
s.logger.Error("ShutdownHandler: Failed to evict self from membership ring", tag.Error(err))
|
|
}
|
|
s.healthServer.SetServingStatus(serviceName, healthpb.HealthCheckResponse_NOT_SERVING)
|
|
s.logger.Info("ShutdownHandler: Waiting for others to discover I am unhealthy")
|
|
time.Sleep(s.config.ShutdownDrainDuration())
|
|
|
|
// TODO: Change this to GracefulStop when integration tests are refactored.
|
|
s.server.Stop()
|
|
|
|
s.handler.Stop()
|
|
|
|
s.visibilityManager.Close()
|
|
|
|
s.logger.Info("matching stopped")
|
|
}
|
|
|
|
func (s *Service) GetFaultInjection() *client.FaultInjectionDataStoreFactory {
|
|
return s.faultInjectionDataStoreFactory
|
|
}
|