go.temporal.io/server/service/matching/service.go

125 LOC · 61 covered · 64 uncovered · 7 ranges · 64 concepts · 2 introducers · 12 tests

File neighbourhood

The centred file is linked to every concept that introduces one of its ranges, every test that runs code from the file, and the gray connector concepts standing between those tests and the file's own introducer concepts. Undirected links join concepts to every file where they introduce source and concepts to the tests they introduce; arrows show specialization between the displayed concepts and bridge only concepts omitted from this view. Concept colors match the source ranges below; connector concepts have no source color and are shown in gray.

Focused file, its introducer and connector concepts, their introduced files, and tests that run code from the file

In the embedded map, ordinary wheel input scrolls the page; use the visible controls to zoom and drag to pan. Open the full-screen map for canvas navigation: wheel pans, Ctrl/Command plus wheel zooms, and arrow keys pan when this region is focused. On touch screens, open the full-screen map to pan or pinch. If JavaScript or WebGL is unavailable, use the related-file, concept, and source links on this page.

Graph controls are ready.

Interactive rendering requires JavaScript and WebGL. Use the related-file, concept, and source links on this page while the interactive map is unavailable.

1 package matching
2
3 import (
4 "net"
5 "time"
6
7 "go.temporal.io/server/api/matchingservice/v1"
8 "go.temporal.io/server/common/log"
9 "go.temporal.io/server/common/log/tag"
10 "go.temporal.io/server/common/membership"
11 "go.temporal.io/server/common/metrics"
12 "go.temporal.io/server/common/persistence/visibility/manager"
13 "go.temporal.io/server/common/util"
14 "google.golang.org/grpc"
15 "google.golang.org/grpc/health"
16 healthpb "google.golang.org/grpc/health/grpc_health_v1"
17 "google.golang.org/grpc/reflection"
18 )
19
20 // Service represents the matching service
21 type Service struct {
22 handler *Handler
23 config *Config
24
25 server *grpc.Server
26 logger log.SnTaggedLogger
27 membershipMonitor membership.Monitor
28 grpcListener net.Listener
29 runtimeMetricsReporter *metrics.RuntimeMetricsReporter
30 metricsHandler metrics.Handler
31 healthServer *health.Server
32 visibilityManager manager.VisibilityManager
33 }
34
35 func NewService(
36 server *grpc.Server,
37 serviceConfig *Config,
38 logger log.SnTaggedLogger,
39 membershipMonitor membership.Monitor,
40 grpcListener net.Listener,
41 runtimeMetricsReporter *metrics.RuntimeMetricsReporter,
42 handler *Handler,
43 metricsHandler metrics.Handler,
44 healthServer *health.Server,
45 visibilityManager manager.VisibilityManager,
46 > ) *Service { fx.go ×44
47 > return &Service{
48 > config: serviceConfig,
49 > server: server,
50 > handler: handler,
51 > logger: logger,
52 > membershipMonitor: membershipMonitor,
53 > grpcListener: grpcListener,
54 > runtimeMetricsReporter: runtimeMetricsReporter,
55 > metricsHandler: metricsHandler,
56 > healthServer: healthServer,
57 > visibilityManager: visibilityManager,
58 > }
59 > }
60
61 // Start starts the service
62 > func (s *Service) Start() { fx.go ×44
63 > s.logger.Info("matching starting")
64 >
65 > // must start base service first
66 > metrics.RestartCount.With(s.metricsHandler).Record(1)
67 >
68 > s.handler.Start()
69 >
70 > matchingservice.RegisterMatchingServiceServer(s.server, s.handler)
71 > healthpb.RegisterHealthServer(s.server, s.healthServer)
72 > s.healthServer.SetServingStatus(serviceName, healthpb.HealthCheckResponse_SERVING)
73 >
74 > reflection.Register(s.server)
75 >
76 > go func() {
77 > s.logger.Info("Starting to serve on matching listener")
78 > if err := s.server.Serve(s.grpcListener); err != nil {
79 s.logger.Fatal("Failed to serve on matching listener", tag.Error(err))
80 }
81 }()
82
83 > go s.membershipMonitor.Start() fx.go ×44
84 }
85
86 // Stop stops the service
87 > func (s *Service) Stop() { service.go ×8
88 > // remove self from membership ring and wait for traffic to drain
89 > var err error
90 > var waitTime time.Duration
91 > if align := s.config.AlignMembershipChange(); align > 0 {
92 propagation := s.membershipMonitor.ApproximateMaxPropagationTime()
93 asOf := util.NextAlignedTime(time.Now().Add(propagation), align)
94 s.logger.Info("ShutdownHandler: Evicting self from membership ring as of", tag.Timestamp(asOf))
95 waitTime, err = s.membershipMonitor.EvictSelfAt(asOf)
96 > } else { service.go ×8
97 > s.logger.Info("ShutdownHandler: Evicting self from membership ring immediately")
98 > err = s.membershipMonitor.EvictSelf()
99 > }
100 > if err != nil {
101 s.logger.Error("ShutdownHandler: Failed to evict self from membership ring", tag.Error(err))
102 }
103 > s.healthServer.SetServingStatus(serviceName, healthpb.HealthCheckResponse_NOT_SERVING) service.go ×8
104 >
105 > s.logger.Info("ShutdownHandler: Waiting for others to discover I am unhealthy")
106 > time.Sleep(max(s.config.ShutdownDrainDuration(), waitTime))
107 >
108 > // At this point we should not get any new rpcs since we removed ourself from the ring.
109 > // Additionally, the engine will notice the membership change and stop all task queues
110 > // after a delay. However, we can do it immediately by stopping the handler (which stops
111 > // the engine which stops all task queues).
112 > s.handler.Stop()
113 >
114 > // All grpc handlers should be cancelled now. Give them a little time to return.
115 > t := time.AfterFunc(2*time.Second, func() {
116 s.logger.Info("ShutdownHandler: Drain time expired, stopping all traffic")
117 s.server.Stop()
118 })
119 > s.server.GracefulStop() service.go ×8
120 > t.Stop()
121 >
122 > s.visibilityManager.Close()
123 >
124 > s.logger.Info("matching stopped")
125 }