-
Notifications
You must be signed in to change notification settings - Fork 769
/
controller.go
547 lines (471 loc) · 16.3 KB
/
controller.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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
// 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.
//go:generate mockgen -package $GOPACKAGE -source $GOFILE -destination controller_mock.go -self_package github.com/uber/cadence/service/history/shard
package shard
import (
"fmt"
"sync"
"sync/atomic"
"time"
"github.com/uber/cadence/common/service"
workflow "github.com/uber/cadence/.gen/go/shared"
"github.com/uber/cadence/common"
"github.com/uber/cadence/common/log"
"github.com/uber/cadence/common/log/tag"
"github.com/uber/cadence/common/membership"
"github.com/uber/cadence/common/metrics"
"github.com/uber/cadence/common/persistence"
"github.com/uber/cadence/common/types"
"github.com/uber/cadence/service/history/config"
"github.com/uber/cadence/service/history/engine"
"github.com/uber/cadence/service/history/resource"
)
const (
shardControllerMembershipUpdateListenerName = "ShardController"
)
var (
errShardIDOutOfBoundary = &workflow.BadRequestError{Message: "shard ID is out of boundary"}
)
type (
// EngineFactory is used to create an instance of sharded history engine
EngineFactory interface {
CreateEngine(Context) engine.Engine
}
// Controller controls history service shards
Controller interface {
common.Daemon
// PrepareToStop starts the graceful shutdown process for controller
PrepareToStop()
GetEngine(workflowID string) (engine.Engine, error)
GetEngineForShard(shardID int) (engine.Engine, error)
RemoveEngineForShard(shardID int)
// Following methods describes the current status of the controller
// TODO: consider converting to a unified describe method
Status() int32
NumShards() int
ShardIDs() []int32
}
controller struct {
resource.Resource
membershipUpdateCh chan *membership.ChangedEvent
engineFactory EngineFactory
status int32
shuttingDown int32
shutdownWG sync.WaitGroup
shutdownCh chan struct{}
logger log.Logger
throttledLogger log.Logger
config *config.Config
metricsScope metrics.Scope
sync.RWMutex
historyShards map[int]*historyShardsItem
}
historyShardsItemStatus int
historyShardsItem struct {
resource.Resource
shardID int
config *config.Config
logger log.Logger
throttledLogger log.Logger
engineFactory EngineFactory
sync.RWMutex
status historyShardsItemStatus
engine engine.Engine
}
)
const (
historyShardsItemStatusInitialized = iota
historyShardsItemStatusStarted
historyShardsItemStatusStopped
)
// NewShardController creates a new shard controller
func NewShardController(
resource resource.Resource,
factory EngineFactory,
config *config.Config,
) Controller {
hostAddress := resource.GetHostInfo().GetAddress()
return &controller{
Resource: resource,
status: common.DaemonStatusInitialized,
membershipUpdateCh: make(chan *membership.ChangedEvent, 10),
engineFactory: factory,
historyShards: make(map[int]*historyShardsItem),
shutdownCh: make(chan struct{}),
logger: resource.GetLogger().WithTags(tag.ComponentShardController, tag.Address(hostAddress)),
throttledLogger: resource.GetThrottledLogger().WithTags(tag.ComponentShardController, tag.Address(hostAddress)),
config: config,
metricsScope: resource.GetMetricsClient().Scope(metrics.HistoryShardControllerScope),
}
}
func newHistoryShardsItem(
resource resource.Resource,
shardID int,
factory EngineFactory,
config *config.Config,
) (*historyShardsItem, error) {
hostAddress := resource.GetHostInfo().GetAddress()
return &historyShardsItem{
Resource: resource,
shardID: shardID,
status: historyShardsItemStatusInitialized,
engineFactory: factory,
config: config,
logger: resource.GetLogger().WithTags(tag.ShardID(shardID), tag.Address(hostAddress)),
throttledLogger: resource.GetThrottledLogger().WithTags(tag.ShardID(shardID), tag.Address(hostAddress)),
}, nil
}
func (c *controller) Start() {
if !atomic.CompareAndSwapInt32(&c.status, common.DaemonStatusInitialized, common.DaemonStatusStarted) {
return
}
c.acquireShards()
c.shutdownWG.Add(1)
go c.shardManagementPump()
err := c.GetMembershipResolver().Subscribe(service.History, shardControllerMembershipUpdateListenerName, c.membershipUpdateCh)
if err != nil {
c.logger.Error("subscribing to membership resolver", tag.Error(err))
}
c.logger.Info("Shard controller state changed", tag.LifeCycleStarted)
}
func (c *controller) Stop() {
if !atomic.CompareAndSwapInt32(&c.status, common.DaemonStatusStarted, common.DaemonStatusStopped) {
return
}
c.PrepareToStop()
if err := c.GetMembershipResolver().Unsubscribe(service.History, shardControllerMembershipUpdateListenerName); err != nil {
c.logger.Error("unsubscribing from membership resolver", tag.Error(err), tag.OperationFailed)
}
close(c.shutdownCh)
if success := common.AwaitWaitGroup(&c.shutdownWG, time.Minute); !success {
c.logger.Warn("", tag.LifeCycleStopTimedout)
}
c.logger.Info("Shard controller state changed", tag.LifeCycleStopped)
}
func (c *controller) PrepareToStop() {
atomic.StoreInt32(&c.shuttingDown, 1)
}
func (c *controller) GetEngine(workflowID string) (engine.Engine, error) {
shardID := c.config.GetShardID(workflowID)
return c.GetEngineForShard(shardID)
}
func (c *controller) GetEngineForShard(shardID int) (engine.Engine, error) {
sw := c.metricsScope.StartTimer(metrics.GetEngineForShardLatency)
defer sw.Stop()
item, err := c.getOrCreateHistoryShardItem(shardID)
if err != nil {
return nil, err
}
return item.getOrCreateEngine(c.shardClosedCallback)
}
func (c *controller) RemoveEngineForShard(shardID int) {
c.removeEngineForShard(shardID, nil)
}
func (c *controller) Status() int32 {
return atomic.LoadInt32(&c.status)
}
func (c *controller) NumShards() int {
nShards := 0
c.RLock()
nShards = len(c.historyShards)
c.RUnlock()
return nShards
}
func (c *controller) ShardIDs() []int32 {
c.RLock()
ids := []int32{}
for id := range c.historyShards {
id32 := int32(id)
ids = append(ids, id32)
}
c.RUnlock()
return ids
}
func (c *controller) removeEngineForShard(shardID int, shardItem *historyShardsItem) {
sw := c.metricsScope.StartTimer(metrics.RemoveEngineForShardLatency)
defer sw.Stop()
currentShardItem, _ := c.removeHistoryShardItem(shardID, shardItem)
if shardItem != nil {
// if shardItem is not nil, then currentShardItem either equals to shardItem or is nil
// in both cases, we need to stop the engine in shardItem
shardItem.stopEngine()
return
}
// if shardItem is nil, then stop the engine for the current shardItem, if exists
if currentShardItem != nil {
currentShardItem.stopEngine()
}
}
func (c *controller) shardClosedCallback(shardID int, shardItem *historyShardsItem) {
c.metricsScope.IncCounter(metrics.ShardClosedCounter)
c.logger.Info("Shard controller state changed", tag.LifeCycleStopping, tag.ComponentShard, tag.ShardID(shardID))
c.removeEngineForShard(shardID, shardItem)
}
func (c *controller) getOrCreateHistoryShardItem(shardID int) (*historyShardsItem, error) {
if shardID >= c.config.NumberOfShards || shardID < 0 { // zero based shard ID
c.logger.Error(fmt.Sprintf("Received shard ID: %v is larger than supported shard number %v",
shardID,
c.config.NumberOfShards,
),
)
return nil, errShardIDOutOfBoundary
}
c.RLock()
if item, ok := c.historyShards[shardID]; ok {
if item.isValid() {
c.RUnlock()
return item, nil
}
// if item not valid then process to create a new one
}
c.RUnlock()
c.Lock()
defer c.Unlock()
if item, ok := c.historyShards[shardID]; ok {
if item.isValid() {
return item, nil
}
// if item not valid then process to create a new one
}
if c.isShuttingDown() || atomic.LoadInt32(&c.status) == common.DaemonStatusStopped {
return nil, fmt.Errorf("controller for host '%v' shutting down", c.GetHostInfo().Identity())
}
info, err := c.GetMembershipResolver().Lookup(service.History, string(rune(shardID)))
if err != nil {
return nil, err
}
if info.Identity() == c.GetHostInfo().Identity() {
shardItem, err := newHistoryShardsItem(
c.Resource,
shardID,
c.engineFactory,
c.config,
)
if err != nil {
return nil, err
}
c.historyShards[shardID] = shardItem
c.metricsScope.IncCounter(metrics.ShardItemCreatedCounter)
shardItem.logger.Info("Shard item state changed", tag.LifeCycleStarted, tag.ComponentShardItem)
return shardItem, nil
}
// for backwards compatibility, always return tchannel port
return nil, CreateShardOwnershipLostError(c.GetHostInfo(), info)
}
func (c *controller) removeHistoryShardItem(shardID int, shardItem *historyShardsItem) (*historyShardsItem, error) {
nShards := 0
c.Lock()
defer c.Unlock()
currentShardItem, ok := c.historyShards[shardID]
if !ok {
return nil, fmt.Errorf("no item found to remove for shard: %v", shardID)
}
if shardItem != nil && currentShardItem != shardItem {
// the shardItem comparison is a defensive check to make sure we are deleting
// what we intend to delete.
return nil, fmt.Errorf("current shardItem doesn't match the one we intend to delete for shard: %v", shardID)
}
delete(c.historyShards, shardID)
nShards = len(c.historyShards)
c.metricsScope.IncCounter(metrics.ShardItemRemovedCounter)
currentShardItem.logger.Info("Shard item state changed", tag.LifeCycleStopped, tag.ComponentShardItem, tag.Number(int64(nShards)))
return currentShardItem, nil
}
// shardManagementPump is the main event loop for
// controller. It is responsible for acquiring /
// releasing shards in response to any event that can
// change the shard ownership. These events are
//
// a. Ring membership change
// b. Periodic ticker
// c. ShardOwnershipLostError and subsequent ShardClosedEvents from engine
func (c *controller) shardManagementPump() {
defer c.shutdownWG.Done()
acquireTicker := time.NewTicker(c.config.AcquireShardInterval())
defer acquireTicker.Stop()
for {
select {
case <-c.shutdownCh:
c.doShutdown()
return
case <-acquireTicker.C:
c.acquireShards()
case changedEvent := <-c.membershipUpdateCh:
c.metricsScope.IncCounter(metrics.MembershipChangedCounter)
c.logger.Info("Ring membership changed", tag.ValueRingMembershipChangedEvent,
tag.NumberProcessed(len(changedEvent.HostsAdded)),
tag.NumberDeleted(len(changedEvent.HostsRemoved)),
tag.Number(int64(len(changedEvent.HostsUpdated))))
c.acquireShards()
}
}
}
func (c *controller) acquireShards() {
c.metricsScope.IncCounter(metrics.AcquireShardsCounter)
sw := c.metricsScope.StartTimer(metrics.AcquireShardsLatency)
defer sw.Stop()
concurrency := common.MaxInt(c.config.AcquireShardConcurrency(), 1)
shardActionCh := make(chan int, concurrency)
var wg sync.WaitGroup
wg.Add(concurrency)
// Spawn workers that would lookup and add/remove shards concurrently.
for i := 0; i < concurrency; i++ {
go func() {
defer wg.Done()
for shardID := range shardActionCh {
if c.isShuttingDown() {
return
}
info, err := c.GetMembershipResolver().Lookup(service.History, string(rune(shardID)))
if err != nil {
c.logger.Error("Error looking up host for shardID", tag.Error(err), tag.OperationFailed, tag.ShardID(shardID))
} else {
if info.Identity() == c.GetHostInfo().Identity() {
_, err1 := c.GetEngineForShard(shardID)
if err1 != nil {
c.metricsScope.IncCounter(metrics.GetEngineForShardErrorCounter)
c.logger.Error("Unable to create history shard engine", tag.Error(err1), tag.OperationFailed, tag.ShardID(shardID))
}
}
}
}
}()
}
// Submit tasks to the channel.
for shardID := 0; shardID < c.config.NumberOfShards; shardID++ {
shardActionCh <- shardID
if c.isShuttingDown() {
return
}
}
close(shardActionCh)
// Wait until all shards are processed.
wg.Wait()
c.metricsScope.UpdateGauge(metrics.NumShardsGauge, float64(c.NumShards()))
}
func (c *controller) doShutdown() {
c.logger.Info("Shard controller state changed", tag.LifeCycleStopping)
c.Lock()
defer c.Unlock()
for _, item := range c.historyShards {
item.stopEngine()
}
c.historyShards = nil
}
func (c *controller) isShuttingDown() bool {
return atomic.LoadInt32(&c.shuttingDown) != 0
}
func (i *historyShardsItem) getOrCreateEngine(
closeCallback func(int, *historyShardsItem),
) (engine.Engine, error) {
i.RLock()
if i.status == historyShardsItemStatusStarted {
defer i.RUnlock()
return i.engine, nil
}
i.RUnlock()
i.Lock()
defer i.Unlock()
switch i.status {
case historyShardsItemStatusInitialized:
i.logger.Info("Shard engine state changed", tag.LifeCycleStarting, tag.ComponentShardEngine)
context, err := acquireShard(i, closeCallback)
if err != nil {
// invalidate the shardItem so that the same shardItem won't be
// used to create another shardContext
i.logger.Info("Shard engine state changed", tag.LifeCycleStopped, tag.ComponentShardEngine)
i.status = historyShardsItemStatusStopped
return nil, err
}
if context.PreviousShardOwnerWasDifferent() {
i.GetMetricsClient().RecordTimer(metrics.ShardInfoScope, metrics.ShardItemAcquisitionLatency,
context.GetCurrentTime(i.GetClusterMetadata().GetCurrentClusterName()).Sub(context.GetLastUpdatedTime()))
}
i.engine = i.engineFactory.CreateEngine(context)
i.engine.Start()
i.logger.Info("Shard engine state changed", tag.LifeCycleStarted, tag.ComponentShardEngine)
i.status = historyShardsItemStatusStarted
return i.engine, nil
case historyShardsItemStatusStarted:
return i.engine, nil
case historyShardsItemStatusStopped:
return nil, fmt.Errorf("shard %v for host '%v' is shut down", i.shardID, i.GetHostInfo().Identity())
default:
panic(i.logInvalidStatus())
}
}
func (i *historyShardsItem) stopEngine() {
i.Lock()
defer i.Unlock()
switch i.status {
case historyShardsItemStatusInitialized:
i.status = historyShardsItemStatusStopped
case historyShardsItemStatusStarted:
i.logger.Info("Shard engine state changed", tag.LifeCycleStopping, tag.ComponentShardEngine)
i.engine.Stop()
i.engine = nil
i.logger.Info("Shard engine state changed", tag.LifeCycleStopped, tag.ComponentShardEngine)
i.status = historyShardsItemStatusStopped
case historyShardsItemStatusStopped:
// no op
default:
panic(i.logInvalidStatus())
}
}
func (i *historyShardsItem) isValid() bool {
i.RLock()
defer i.RUnlock()
switch i.status {
case historyShardsItemStatusInitialized, historyShardsItemStatusStarted:
return true
case historyShardsItemStatusStopped:
return false
default:
panic(i.logInvalidStatus())
}
}
func (i *historyShardsItem) logInvalidStatus() string {
msg := fmt.Sprintf("Host '%v' encounter invalid status %v for shard item for shardID '%v'.",
i.GetHostInfo().Identity(), i.status, i.shardID)
i.logger.Error(msg)
return msg
}
// IsShardOwnershiptLostError checks if a given error is shard ownership lost error
func IsShardOwnershiptLostError(err error) bool {
switch err.(type) {
case *persistence.ShardOwnershipLostError:
return true
}
return false
}
// CreateShardOwnershipLostError creates a new shard ownership lost error
func CreateShardOwnershipLostError(
currentHost membership.HostInfo,
ownerHost membership.HostInfo,
) *types.ShardOwnershipLostError {
address, err := ownerHost.GetNamedAddress(membership.PortTchannel)
if err != nil {
address = ownerHost.Identity()
}
return &types.ShardOwnershipLostError{
Message: fmt.Sprintf("Shard is not owned by host: %v", currentHost.Identity()),
Owner: address,
}
}