-
Notifications
You must be signed in to change notification settings - Fork 44
/
resource_option.go
286 lines (250 loc) · 7.51 KB
/
resource_option.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
// Copyright 2020 PingCAP, Inc.
// Modifications copyright (C) 2021 MatrixOrigin.
//
// 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
//
// http://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,
// See the License for the specific language governing permissions and
// limitations under the License.
package core
import (
"sort"
"github.com/matrixorigin/matrixcube/components/prophet/metadata"
"github.com/matrixorigin/matrixcube/components/prophet/pb/metapb"
)
// ResourceOption is used to select resource.
type ResourceOption func(res *CachedResource) bool
// ResourceCreateOption used to create resource.
type ResourceCreateOption func(res *CachedResource)
// WithState sets state for the resource.
func WithState(state metapb.ResourceState) ResourceCreateOption {
return func(res *CachedResource) {
res.Meta.SetState(state)
}
}
// WithDownPeers sets the down peers for the resource.
func WithDownPeers(downReplicas []metapb.ReplicaStats) ResourceCreateOption {
return func(res *CachedResource) {
res.downReplicas = append(downReplicas[:0:0], downReplicas...)
sort.Sort(peerStatsSlice(res.downReplicas))
}
}
// WithPendingPeers sets the pending peers for the resource.
func WithPendingPeers(pendingReplicas []metapb.Replica) ResourceCreateOption {
return func(res *CachedResource) {
res.pendingReplicas = append(pendingReplicas[:0:0], pendingReplicas...)
sort.Sort(peerSlice(res.pendingReplicas))
}
}
// WithLearners sets the learners for the resource.
func WithLearners(learners []metapb.Replica) ResourceCreateOption {
return func(res *CachedResource) {
peers := res.Meta.Peers()
for i := range peers {
for _, l := range learners {
if peers[i].ID == l.ID {
peers[i] = metapb.Replica{ID: l.ID, ContainerID: l.ContainerID, Role: metapb.ReplicaRole_Learner}
break
}
}
}
}
}
// WithLeader sets the leader for the resource.
func WithLeader(leader *metapb.Replica) ResourceCreateOption {
return func(res *CachedResource) {
res.leader = leader
}
}
// WithStartKey sets the start key for the resource.
func WithStartKey(key []byte) ResourceCreateOption {
return func(res *CachedResource) {
res.Meta.SetStartKey(key)
}
}
// WithEndKey sets the end key for the resource.
func WithEndKey(key []byte) ResourceCreateOption {
return func(res *CachedResource) {
res.Meta.SetEndKey(key)
}
}
// WithNewResourceID sets new id for the resource.
func WithNewResourceID(id uint64) ResourceCreateOption {
return func(res *CachedResource) {
res.Meta.SetID(id)
}
}
// WithNewPeerIds sets new ids for peers.
func WithNewPeerIds(peerIDs ...uint64) ResourceCreateOption {
return func(res *CachedResource) {
if len(peerIDs) != len(res.Meta.Peers()) {
return
}
peers := res.Meta.Peers()
for i := range peers {
peers[i].ID = peerIDs[i]
}
}
}
// WithIncVersion increases the version of the resource.
func WithIncVersion() ResourceCreateOption {
return func(res *CachedResource) {
e := res.Meta.Epoch()
e.Version++
res.Meta.SetEpoch(e)
}
}
// WithDecVersion decreases the version of the resource.
func WithDecVersion() ResourceCreateOption {
return func(res *CachedResource) {
e := res.Meta.Epoch()
e.Version--
res.Meta.SetEpoch(e)
}
}
// WithIncConfVer increases the config version of the resource.
func WithIncConfVer() ResourceCreateOption {
return func(res *CachedResource) {
e := res.Meta.Epoch()
e.ConfVer++
res.Meta.SetEpoch(e)
}
}
// WithDecConfVer decreases the config version of the resource.
func WithDecConfVer() ResourceCreateOption {
return func(res *CachedResource) {
e := res.Meta.Epoch()
e.ConfVer--
res.Meta.SetEpoch(e)
}
}
// SetWrittenBytes sets the written bytes for the resource.
func SetWrittenBytes(v uint64) ResourceCreateOption {
return func(res *CachedResource) {
res.stats.WrittenBytes = v
}
}
// SetWrittenKeys sets the written keys for the resource.
func SetWrittenKeys(v uint64) ResourceCreateOption {
return func(res *CachedResource) {
res.stats.WrittenKeys = v
}
}
// WithRemoveContainerPeer removes the specified peer for the resource.
func WithRemoveContainerPeer(containerID uint64) ResourceCreateOption {
return func(res *CachedResource) {
var peers []metapb.Replica
for _, peer := range res.Meta.Peers() {
if peer.ContainerID != containerID {
peers = append(peers, peer)
}
}
res.Meta.SetPeers(peers)
}
}
// SetReadBytes sets the read bytes for the resource.
func SetReadBytes(v uint64) ResourceCreateOption {
return func(res *CachedResource) {
res.stats.ReadBytes = v
}
}
// SetReadKeys sets the read keys for the resource.
func SetReadKeys(v uint64) ResourceCreateOption {
return func(res *CachedResource) {
res.stats.ReadKeys = v
}
}
// SetApproximateSize sets the approximate size for the resource.
func SetApproximateSize(v int64) ResourceCreateOption {
return func(res *CachedResource) {
res.stats.ApproximateSize = uint64(v)
}
}
// SetApproximateKeys sets the approximate keys for the resource.
func SetApproximateKeys(v int64) ResourceCreateOption {
return func(res *CachedResource) {
res.stats.ApproximateKeys = uint64(v)
}
}
// SetReportInterval sets the report interval for the resource.
func SetReportInterval(v uint64) ResourceCreateOption {
return func(res *CachedResource) {
res.stats.Interval = &metapb.TimeInterval{Start: 0, End: v}
}
}
// SetResourceConfVer sets the config version for the resource.
func SetResourceConfVer(confVer uint64) ResourceCreateOption {
return func(res *CachedResource) {
e := res.Meta.Epoch()
if e.Version == 0 {
res.Meta.SetEpoch(metapb.ResourceEpoch{ConfVer: confVer, Version: 1})
} else {
e.ConfVer = confVer
res.Meta.SetEpoch(e)
}
}
}
// SetResourceVersion sets the version for the resource.
func SetResourceVersion(version uint64) ResourceCreateOption {
return func(res *CachedResource) {
e := res.Meta.Epoch()
if e.Version == 0 {
res.Meta.SetEpoch(metapb.ResourceEpoch{ConfVer: 1, Version: version})
} else {
e.Version = version
res.Meta.SetEpoch(e)
}
}
}
// SetPeers sets the peers for the resource.
func SetPeers(peers []metapb.Replica) ResourceCreateOption {
return func(res *CachedResource) {
res.Meta.SetPeers(peers)
}
}
// WithAddPeer adds a peer for the resource.
func WithAddPeer(peer metapb.Replica) ResourceCreateOption {
return func(res *CachedResource) {
peers := res.Meta.Peers()
peers = append(peers, peer)
res.Meta.SetPeers(peers)
if metadata.IsLearner(peer) {
res.learners = append(res.learners, peer)
} else {
res.voters = append(res.voters, peer)
}
}
}
// WithPromoteLearner promotes the learner.
func WithPromoteLearner(peerID uint64) ResourceCreateOption {
return func(res *CachedResource) {
peers := res.Meta.Peers()
for i := range res.Meta.Peers() {
if peers[i].ID == peerID {
peers[i].Role = metapb.ReplicaRole_Voter
}
}
}
}
// WithReplacePeerContainer replaces a peer's containerID with another ID.
func WithReplacePeerContainer(oldContainerID, newContainerID uint64) ResourceCreateOption {
return func(res *CachedResource) {
peers := res.Meta.Peers()
for i := range peers {
if peers[i].ContainerID == oldContainerID {
peers[i].ContainerID = newContainerID
}
}
}
}
// WithInterval sets the interval
func WithInterval(interval *metapb.TimeInterval) ResourceCreateOption {
return func(res *CachedResource) {
res.stats.Interval = interval
}
}