/
service.go
355 lines (306 loc) · 10 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
// Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
package wire
import (
"errors"
"sync"
)
// serviceStatus specifies the current status of the service. The order of the
// values reflects the lifecycle of services. Note that some statuses may be
// skipped.
type serviceStatus int
const (
// Service has not been started.
serviceUninitialized serviceStatus = iota
// Service is starting up.
serviceStarting
// Service is active and accepting new data. Note that the underlying stream
// may be reconnecting due to retryable errors.
serviceActive
// Service is gracefully shutting down by flushing all pending data. No new
// data is accepted.
serviceTerminating
// Service has terminated. No new data is accepted.
serviceTerminated
)
// serviceHandle is used to compare pointers to service instances.
type serviceHandle interface{}
// serviceStatusChangeFunc notifies the parent of service status changes.
// `serviceTerminating` and `serviceTerminated` have an associated error. This
// error may be nil if the user called Stop().
type serviceStatusChangeFunc func(serviceHandle, serviceStatus, error)
// service is the interface that must be implemented by services (essentially
// gRPC client stream wrappers, e.g. subscriber, publisher) that can be
// dependencies of a compositeService.
type service interface {
Start()
Stop()
// Methods below are implemented by abstractService.
AddStatusChangeReceiver(serviceHandle, serviceStatusChangeFunc)
RemoveStatusChangeReceiver(serviceHandle)
Handle() serviceHandle
Status() serviceStatus
Error() error
}
// abstractService can be embedded into other structs to provide common
// functionality for managing service status and status change receivers.
type abstractService struct {
mu sync.Mutex
statusChangeReceivers []*statusChangeReceiver
status serviceStatus
// The error that cause the service to terminate.
err error
}
type statusChangeReceiver struct {
handle serviceHandle // For removing the receiver.
onStatusChange serviceStatusChangeFunc
}
func (as *abstractService) AddStatusChangeReceiver(handle serviceHandle, onStatusChange serviceStatusChangeFunc) {
as.mu.Lock()
defer as.mu.Unlock()
as.statusChangeReceivers = append(
as.statusChangeReceivers,
&statusChangeReceiver{handle, onStatusChange})
}
func (as *abstractService) RemoveStatusChangeReceiver(handle serviceHandle) {
as.mu.Lock()
defer as.mu.Unlock()
for i := len(as.statusChangeReceivers) - 1; i >= 0; i-- {
r := as.statusChangeReceivers[i]
if r.handle == handle {
// Swap with last element, erase last element and truncate the slice.
lastIdx := len(as.statusChangeReceivers) - 1
if i != lastIdx {
as.statusChangeReceivers[i] = as.statusChangeReceivers[lastIdx]
}
as.statusChangeReceivers[lastIdx] = nil
as.statusChangeReceivers = as.statusChangeReceivers[:lastIdx]
}
}
}
// Handle identifies this service instance, even when there are multiple layers
// of embedding.
func (as *abstractService) Handle() serviceHandle {
return as
}
func (as *abstractService) Error() error {
as.mu.Lock()
defer as.mu.Unlock()
return as.err
}
func (as *abstractService) Status() serviceStatus {
as.mu.Lock()
defer as.mu.Unlock()
return as.status
}
func (as *abstractService) unsafeCheckServiceStatus() error {
switch {
case as.status == serviceUninitialized:
return ErrServiceUninitialized
case as.status == serviceStarting:
return ErrServiceStarting
case as.status >= serviceTerminating:
return ErrServiceStopped
default:
return nil
}
}
// unsafeUpdateStatus assumes the service is already holding a mutex when
// called, as it often needs to be atomic with other operations.
func (as *abstractService) unsafeUpdateStatus(targetStatus serviceStatus, err error) bool {
if as.status >= targetStatus {
// Already at the same or later stage of the service lifecycle.
return false
}
as.status = targetStatus
if as.err == nil {
// Prevent clobbering original error.
as.err = err
}
for _, receiver := range as.statusChangeReceivers {
// Notify in a goroutine to prevent deadlocks if the receiver is holding a
// locked mutex.
go receiver.onStatusChange(as.Handle(), as.status, as.err)
}
return true
}
var errChildServiceStarted = errors.New("pubsublite: dependent service must not be started")
// compositeService can be embedded into other structs to manage child services.
// It implements the service interface and can itself be a dependency of another
// compositeService.
//
// If one child service terminates due to a permanent failure, all other child
// services are stopped. Child services can be added and removed dynamically.
type compositeService struct {
// Used to block until all dependencies have started or terminated.
waitStarted chan struct{}
waitTerminated chan struct{}
// Current dependencies.
dependencies map[serviceHandle]service
// Removed dependencies that are in the process of terminating.
removed map[serviceHandle]service
abstractService
}
// init must be called after creation of the derived struct.
func (cs *compositeService) init() {
cs.waitStarted = make(chan struct{})
cs.waitTerminated = make(chan struct{})
cs.dependencies = make(map[serviceHandle]service)
cs.removed = make(map[serviceHandle]service)
}
// Start up dependencies.
func (cs *compositeService) Start() {
cs.mu.Lock()
defer cs.mu.Unlock()
if cs.abstractService.unsafeUpdateStatus(serviceStarting, nil) {
for _, s := range cs.dependencies {
s.Start()
}
}
}
// WaitStarted waits for all dependencies to start.
func (cs *compositeService) WaitStarted() error {
<-cs.waitStarted
return cs.Error()
}
// Stop all dependencies.
func (cs *compositeService) Stop() {
cs.mu.Lock()
defer cs.mu.Unlock()
cs.unsafeInitiateShutdown(serviceTerminating, nil)
}
// WaitStopped waits for all dependencies to stop.
func (cs *compositeService) WaitStopped() error {
<-cs.waitTerminated
return cs.Error()
}
func (cs *compositeService) unsafeAddServices(services ...service) error {
if cs.status >= serviceTerminating {
return ErrServiceStopped
}
for _, s := range services {
// Adding dependent services which have already started not currently
// supported. Requires updating logic to handle the compositeService state.
if s.Status() > serviceUninitialized {
return errChildServiceStarted
}
s.AddStatusChangeReceiver(cs.Handle(), cs.onServiceStatusChange)
cs.dependencies[s.Handle()] = s
if cs.status > serviceUninitialized {
s.Start()
}
}
return nil
}
func (cs *compositeService) unsafeRemoveService(remove service) {
if _, present := cs.dependencies[remove.Handle()]; !present {
return
}
delete(cs.dependencies, remove.Handle())
// The service will be completely removed after it has terminated.
cs.removed[remove.Handle()] = remove
if remove.Status() < serviceTerminating {
remove.Stop()
}
}
func (cs *compositeService) unsafeInitiateShutdown(targetStatus serviceStatus, err error) {
if cs.unsafeUpdateStatus(targetStatus, err) {
for _, s := range cs.dependencies {
if s.Status() < serviceTerminating {
s.Stop()
}
}
}
}
func (cs *compositeService) unsafeUpdateStatus(targetStatus serviceStatus, err error) (ret bool) {
previousStatus := cs.status
if ret = cs.abstractService.unsafeUpdateStatus(targetStatus, err); ret {
// Note: the waitStarted channel must be closed when the service fails to
// start.
if previousStatus < serviceActive && targetStatus >= serviceActive {
close(cs.waitStarted)
}
if targetStatus == serviceTerminated {
close(cs.waitTerminated)
}
}
return
}
func (cs *compositeService) onServiceStatusChange(handle serviceHandle, status serviceStatus, err error) {
cs.mu.Lock()
defer cs.mu.Unlock()
if removedService, present := cs.removed[handle]; present {
if status == serviceTerminated {
removedService.RemoveStatusChangeReceiver(cs.Handle())
delete(cs.removed, handle)
}
}
// Note: we cannot rely on the service not being in the `removed` map to
// determine whether it is an active dependency. The notification may be for a
// service that is no longer in cs.removed or cs.dependencies, because status
// changes are notified asynchronously and may be received out of order.
_, isDependency := cs.dependencies[handle]
// If a single service terminates, stop them all, but allow the others to
// flush pending data. Ignore removed services that are stopping.
shouldTerminate := status >= serviceTerminating && isDependency
numStarted := 0
numTerminated := 0
for _, s := range cs.dependencies {
if shouldTerminate && s.Status() < serviceTerminating {
s.Stop()
}
if s.Status() >= serviceActive {
numStarted++
}
if s.Status() == serviceTerminated {
numTerminated++
}
}
switch {
case numTerminated == len(cs.dependencies) && len(cs.removed) == 0:
cs.unsafeUpdateStatus(serviceTerminated, err)
case shouldTerminate:
cs.unsafeUpdateStatus(serviceTerminating, err)
case numStarted == len(cs.dependencies):
cs.unsafeUpdateStatus(serviceActive, err)
}
}
type apiClient interface {
Close() error
}
type apiClients []apiClient
func (ac apiClients) Close() (retErr error) {
for _, c := range ac {
if err := c.Close(); retErr == nil {
retErr = err
}
}
return
}
// A compositeService that handles closing API clients on shutdown.
type apiClientService struct {
clients apiClients
compositeService
}
func (acs *apiClientService) WaitStarted() error {
err := acs.compositeService.WaitStarted()
if err != nil {
acs.WaitStopped()
}
return err
}
func (acs *apiClientService) WaitStopped() error {
err := acs.compositeService.WaitStopped()
acs.clients.Close()
return err
}