-
Notifications
You must be signed in to change notification settings - Fork 11
/
storage_shard_aware.go
executable file
·229 lines (196 loc) · 4.98 KB
/
storage_shard_aware.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
package storage
import (
"bytes"
"context"
"time"
bhmetapb "github.com/deepfabric/beehive/pb/metapb"
"github.com/deepfabric/beehive/raftstore"
"github.com/deepfabric/busybee/pkg/pb/metapb"
"github.com/deepfabric/busybee/pkg/pb/rpcpb"
"github.com/fagongzi/goetty"
"github.com/fagongzi/log"
"github.com/fagongzi/util/protoc"
)
const (
becomeLeader = iota
becomeFollower
)
type shardCycle struct {
shard bhmetapb.Shard
action int
}
func (h *beeStorage) addShardCallback(shard bhmetapb.Shard) error {
if len(shard.Data) == 0 {
return nil
}
action := &metapb.CallbackAction{}
protoc.MustUnmarshal(action, shard.Data)
if shard.Group == uint64(metapb.TenantRunnerGroup) &&
h.store.MaybeLeader(shard.ID) {
h.shardC <- shardCycle{
shard: shard,
action: becomeLeader,
}
}
if action.SetKV != nil {
for {
req := rpcpb.AcquireSetRequest()
req.Key = action.SetKV.KV.Key
req.Value = action.SetKV.KV.Value
_, err := h.ExecCommandWithGroup(req, action.SetKV.Group)
if err != nil {
log.Errorf("shard %d set kv failed with %+v, retry after 5s",
shard.ID,
err)
time.Sleep(time.Second * 5)
continue
}
break
}
log.Infof("shard %d set kv completed",
shard.ID)
}
if action.UpdateTenantInitState != nil {
for {
req := rpcpb.AcquireTenantInitStateUpdateRequest()
req.ID = action.UpdateTenantInitState.ID
req.Index = action.UpdateTenantInitState.Index
req.Group = action.UpdateTenantInitState.Group
_, err := h.ExecCommand(req)
if err != nil {
log.Errorf("shard %d update tenant init state failed with %+v, retry after 5s",
shard.ID,
err)
time.Sleep(time.Second * 5)
continue
}
break
}
log.Infof("shard %d update tenant init state completed",
shard.ID)
}
return nil
}
func (h *beeStorage) Created(shard bhmetapb.Shard) {
}
func (h *beeStorage) Splited(shard bhmetapb.Shard) {
}
func (h *beeStorage) Destory(shard bhmetapb.Shard) {
if shard.Group == uint64(metapb.DefaultGroup) ||
shard.Group == uint64(metapb.TenantRunnerGroup) {
h.shardC <- shardCycle{
shard: shard,
action: becomeFollower,
}
}
}
func (h *beeStorage) BecomeLeader(shard bhmetapb.Shard) {
if shard.Group == uint64(metapb.DefaultGroup) ||
shard.Group == uint64(metapb.TenantRunnerGroup) {
h.shardC <- shardCycle{
shard: shard,
action: becomeLeader,
}
}
}
func (h *beeStorage) BecomeFollower(shard bhmetapb.Shard) {
if shard.Group == uint64(metapb.DefaultGroup) ||
shard.Group == uint64(metapb.TenantRunnerGroup) {
h.shardC <- shardCycle{
shard: shard,
action: becomeFollower,
}
}
}
func (h *beeStorage) handleShardCycle(ctx context.Context) {
for {
select {
case <-ctx.Done():
log.Infof("handle shard cycle task stopped")
return
case shard, ok := <-h.shardC:
if ok {
switch shard.action {
case becomeLeader:
h.doLoadEvent(shard.shard, true)
h.doLoadWorkerRunnerEvent(shard.shard, true)
case becomeFollower:
h.doLoadEvent(shard.shard, false)
h.doLoadWorkerRunnerEvent(shard.shard, false)
}
}
}
}
}
func (h *beeStorage) doLoadWorkerRunnerEvent(shard bhmetapb.Shard, leader bool) {
if shard.Group != uint64(metapb.TenantRunnerGroup) {
return
}
h.doRunnerEvent(shard, leader)
}
func (h *beeStorage) doLoadEvent(shard bhmetapb.Shard, leader bool) {
if shard.Group != uint64(metapb.DefaultGroup) {
return
}
from := raftstore.EncodeDataKey(shard.Group, []byte{workflowCurrentPrefix})
end := raftstore.EncodeDataKey(shard.Group, []byte{workflowCurrentPrefix + 1})
err := h.getStoreByGroup(shard.Group).Scan(from, end, func(key, value []byte) (bool, error) {
decodedKey := raftstore.DecodeDataKey(key)
if bytes.Compare(decodedKey, shard.Start) >= 0 {
if len(shard.End) == 0 || bytes.Compare(decodedKey, shard.End) < 0 {
switch decodedKey[0] {
case workflowCurrentPrefix:
if leader {
h.doWorkflowEvent(value)
}
}
} else {
return false, nil
}
}
return true, nil
}, false)
if err != nil {
log.Fatalf("scan shard data for loading failed with %+v", err)
}
}
func (h *beeStorage) doWorkflowEvent(value []byte) {
instance := &metapb.WorkflowInstance{}
protoc.MustUnmarshal(instance, value)
switch instance.State {
case metapb.Starting:
h.eventC <- Event{
EventType: StartingInstanceEvent,
Data: instance,
}
case metapb.Running:
h.eventC <- Event{
EventType: RunningInstanceEvent,
Data: instance,
}
case metapb.Stopping:
h.eventC <- Event{
EventType: StoppingInstanceEvent,
Data: instance,
}
}
}
func (h *beeStorage) doRunnerEvent(shard bhmetapb.Shard, leader bool) {
tid := goetty.Byte2UInt64(shard.Start[1:])
runner := goetty.Byte2UInt64(shard.Start[9:])
state := &metapb.WorkerRunner{
ID: tid,
Index: runner,
}
et := StartRunnerEvent
if !leader {
et = StopRunnerEvent
log.Infof("raft trigger WR[%d-%d] stop", tid, runner)
} else {
log.Infof("raft trigger WR[%d-%d] start", tid, runner)
}
h.eventC <- Event{
EventType: et,
Data: state,
}
}