-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshard_controller_manager.go
135 lines (126 loc) · 3.32 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"
"sync/atomic"
"time"
"github.com/benbjohnson/clock"
)
// The controllerManager creates and destroys replicas based on a shard tags.
type controllerManager struct {
agent *Agent
clock clock.Clock
ctx context.Context
ctxCancel context.CancelFunc
index uint64
isLeader atomic.Bool
log Logger
mutex sync.RWMutex
controller Controller
wg sync.WaitGroup
}
func newControllerManager(agent *Agent) *controllerManager {
return &controllerManager{
log: agent.log,
agent: agent,
clock: clock.New(),
controller: newShardControllerDefault(agent),
}
}
type Controller interface {
Reconcile(*State, Shard, Controls) error
}
func (c *controllerManager) 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 *controllerManager) tick() {
c.mutex.Lock()
defer c.mutex.Unlock()
var err error
var hadErr bool
var index uint64
var updated = true
var controls = newControls(c.agent)
if c.isLeader.Load() {
for updated {
updated = false
err = c.agent.State(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.controller.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 *controllerManager) LeaderUpdated(info LeaderInfo) {
c.log.Infof("[%05d:%05d] LeaderUpdated: %05d (term %d)", info.ShardID, info.ReplicaID, info.LeaderID, info.Term)
if info.ShardID == 0 {
c.isLeader.Store(info.LeaderID == info.ReplicaID)
}
if c.isLeader.Load() && c.agent.Status() == AgentStatus_Ready && info.LeaderID != 0 {
c.agent.shardLeaderSet(info.ShardID, info.LeaderID, info.Term)
c.log.Warningf("[%05d:%05d] Leader Set: %d (term %d)", info.ShardID, info.ReplicaID, info.LeaderID, info.Term)
}
}
func (c *controllerManager) Stop() {
defer c.log.Infof(`Stopped controllerManager`)
if c.ctxCancel != nil {
c.ctxCancel()
}
c.mutex.Lock()
defer c.mutex.Unlock()
c.index = 0
}