-
Notifications
You must be signed in to change notification settings - Fork 0
/
shard_controller_manager.go
135 lines (126 loc) · 3.23 KB
/
shard_controller_manager.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
package zongzi
import (
"context"
"fmt"
"sync"
"time"
"github.com/benbjohnson/clock"
)
// The shardControllerManager creates and destroys replicas based on a shard tags.
type shardControllerManager struct {
agent *Agent
clock clock.Clock
ctx context.Context
ctxCancel context.CancelFunc
index uint64
isLeader bool
lastHostID string
leaderIndex uint64
log Logger
mutex sync.RWMutex
shardController ShardController
wg sync.WaitGroup
}
func newShardControllerManager(agent *Agent) *shardControllerManager {
return &shardControllerManager{
log: agent.log,
agent: agent,
clock: clock.New(),
shardController: newShardControllerDefault(agent),
}
}
type ShardController interface {
Reconcile(*State, Shard, ShardControls) error
}
func (c *shardControllerManager) Start() (err error) {
c.mutex.Lock()
defer c.mutex.Unlock()
c.ctx, c.ctxCancel = context.WithCancel(context.Background())
c.wg.Add(1)
go func() {
defer c.wg.Done()
t := c.clock.Ticker(500 * time.Millisecond)
defer t.Stop()
for {
select {
case <-c.ctx.Done():
c.log.Infof("Shard controller manager stopped")
return
case <-t.C:
c.tick()
}
}
}()
return
}
func (c *shardControllerManager) tick() {
c.mutex.Lock()
defer c.mutex.Unlock()
var err error
var hadErr bool
var index uint64
var updated = true
var controls = newShardControls(c.agent)
if c.isLeader {
for updated {
updated = false
err = c.agent.Read(c.ctx, func(state *State) {
index = state.Index()
state.ShardIterateUpdatedAfter(c.index, func(shard Shard) bool {
select {
case <-c.ctx.Done():
return false
default:
}
if shard.ID == 0 {
return true
}
controls.updated = false
err = c.shardController.Reconcile(state, shard, controls)
if err != nil {
hadErr = true
c.log.Warningf("Error resolving shard %d %s %s", shard.ID, shard.Name, err.Error())
c.agent.tagsSet(shard, fmt.Sprintf(`zongzi:controller:error=%s`, err.Error()))
} else if _, ok := shard.Tags[`zongzi:controller:error`]; ok {
c.agent.tagsRemove(shard, `zongzi:controller:error`)
}
if controls.updated {
// We break the iterator on update in order to catch a fresh snapshot for the next shard.
// This ensures that changes applied during reconciliation of this shard will be visible to
// reconciliation of the next shard.
return false
}
return true
})
updated = controls.updated
})
if err != nil {
hadErr = true
}
}
}
if !hadErr && index > c.index {
c.log.Debugf("%s Finished processing %d", c.agent.HostID(), index)
// c.agent.dumpState()
c.index = index
}
return
}
func (c *shardControllerManager) LeaderUpdated(info LeaderInfo) {
c.log.Infof("[%05d:%05d] LeaderUpdated: %05d", info.ShardID, info.ReplicaID, info.LeaderID)
if info.ShardID == 0 {
c.mutex.Lock()
c.isLeader = info.LeaderID == info.ReplicaID
c.mutex.Unlock()
return
}
}
func (c *shardControllerManager) Stop() {
defer c.log.Infof(`Stopped shardControllerManager`)
if c.ctxCancel != nil {
c.ctxCancel()
}
c.mutex.Lock()
defer c.mutex.Unlock()
c.index = 0
}