/
service.go
149 lines (128 loc) · 5.2 KB
/
service.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// 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 history
import (
"net"
"time"
"google.golang.org/grpc"
"google.golang.org/grpc/health"
healthpb "google.golang.org/grpc/health/grpc_health_v1"
"go.temporal.io/server/api/historyservice/v1"
"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"
"go.temporal.io/server/service/history/configs"
)
// Service represents the history service
type (
Service struct {
handler *Handler
visibilityManager manager.VisibilityManager
config *configs.Config
server *grpc.Server
logger log.Logger
grpcListener net.Listener
membershipMonitor membership.Monitor
faultInjectionDataStoreFactory *client.FaultInjectionDataStoreFactory
metricsHandler metrics.Handler
healthServer *health.Server
}
)
func NewService(
grpcServerOptions []grpc.ServerOption,
serviceConfig *configs.Config,
visibilityMgr manager.VisibilityManager,
handler *Handler,
logger log.Logger,
grpcListener net.Listener,
membershipMonitor membership.Monitor,
metricsHandler metrics.Handler,
faultInjectionDataStoreFactory *client.FaultInjectionDataStoreFactory,
healthServer *health.Server,
) *Service {
return &Service{
server: grpc.NewServer(grpcServerOptions...),
handler: handler,
visibilityManager: visibilityMgr,
config: serviceConfig,
logger: logger,
grpcListener: grpcListener,
membershipMonitor: membershipMonitor,
metricsHandler: metricsHandler,
faultInjectionDataStoreFactory: faultInjectionDataStoreFactory,
healthServer: healthServer,
}
}
// Start starts the service
func (s *Service) Start() {
s.logger.Info("history starting")
s.metricsHandler.Counter(metrics.RestartCount).Record(1)
s.handler.Start()
historyservice.RegisterHistoryServiceServer(s.server, s.handler)
healthpb.RegisterHealthServer(s.server, s.healthServer)
s.healthServer.SetServingStatus(serviceName, healthpb.HealthCheckResponse_SERVING)
go func() {
s.logger.Info("Starting to serve on history listener")
if err := s.server.Serve(s.grpcListener); err != nil {
s.logger.Fatal("Failed to serve on history listener", tag.Error(err))
}
}()
// As soon as we join membership, other hosts will send requests for shards that we own,
// so we should try to start this after starting the gRPC server.
go func() {
if delay := s.config.StartupMembershipJoinDelay(); delay > 0 {
// In some situations, like rolling upgrades of the history service,
// pausing before joining membership can help separate the shard movement
// caused by another history instance terminating with this instance starting.
s.logger.Info("history start: delaying before membership start",
tag.NewDurationTag("startupMembershipJoinDelay", delay))
time.Sleep(delay)
}
s.membershipMonitor.Start()
}()
}
// Stop stops the service
func (s *Service) Stop() {
s.logger.Info("ShutdownHandler: Evicting self from membership ring")
_ = s.membershipMonitor.EvictSelf()
if delay := s.config.ShutdownDrainDuration(); delay > 0 {
s.logger.Info("ShutdownHandler: delaying for shutdown drain",
tag.NewDurationTag("shutdownDrainDuration", delay))
time.Sleep(delay)
}
s.healthServer.SetServingStatus(serviceName, healthpb.HealthCheckResponse_NOT_SERVING)
s.logger.Info("ShutdownHandler: Initiating shardController shutdown")
s.handler.controller.Stop()
// TODO: Change this to GracefulStop when integration tests are refactored.
s.server.Stop()
s.handler.Stop()
s.visibilityManager.Close()
s.logger.Info("history stopped")
}
func (s *Service) GetFaultInjection() *client.FaultInjectionDataStoreFactory {
return s.faultInjectionDataStoreFactory
}