-
Notifications
You must be signed in to change notification settings - Fork 993
/
storage.go
239 lines (207 loc) · 6.51 KB
/
storage.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
// Copyright 2017 PingCAP, Inc.
//
// 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 (
"fmt"
"math"
"path"
"strconv"
"strings"
"github.com/gogo/protobuf/proto"
"github.com/pingcap-incubator/tinykv/proto/pkg/metapb"
"github.com/pingcap-incubator/tinykv/scheduler/server/kv"
"github.com/pkg/errors"
"go.etcd.io/etcd/clientv3"
)
const (
clusterPath = "raft"
schedulePath = "schedule"
gcPath = "gc"
customScheduleConfigPath = "scheduler_config"
)
const (
maxKVRangeLimit = 10000
minKVRangeLimit = 100
)
// Storage wraps all kv operations, keep it stateless.
type Storage struct {
kv.Base
}
// NewStorage creates Storage instance with Base.
func NewStorage(base kv.Base) *Storage {
return &Storage{
Base: base,
}
}
func (s *Storage) storePath(storeID uint64) string {
return path.Join(clusterPath, "s", fmt.Sprintf("%020d", storeID))
}
func regionPath(regionID uint64) string {
return path.Join(clusterPath, "r", fmt.Sprintf("%020d", regionID))
}
// ClusterStatePath returns the path to save an option.
func (s *Storage) ClusterStatePath(option string) string {
return path.Join(clusterPath, "status", option)
}
func (s *Storage) storeLeaderWeightPath(storeID uint64) string {
return path.Join(schedulePath, "store_weight", fmt.Sprintf("%020d", storeID), "leader")
}
func (s *Storage) storeRegionWeightPath(storeID uint64) string {
return path.Join(schedulePath, "store_weight", fmt.Sprintf("%020d", storeID), "region")
}
// SaveScheduleConfig saves the config of scheduler.
func (s *Storage) SaveScheduleConfig(scheduleName string, data []byte) error {
configPath := path.Join(customScheduleConfigPath, scheduleName)
return s.Save(configPath, string(data))
}
// RemoveScheduleConfig remvoes the config of scheduler.
func (s *Storage) RemoveScheduleConfig(scheduleName string) error {
configPath := path.Join(customScheduleConfigPath, scheduleName)
return s.Remove(configPath)
}
// LoadScheduleConfig loads the config of scheduler.
func (s *Storage) LoadScheduleConfig(scheduleName string) (string, error) {
configPath := path.Join(customScheduleConfigPath, scheduleName)
return s.Load(configPath)
}
// LoadMeta loads cluster meta from storage.
func (s *Storage) LoadMeta(meta *metapb.Cluster) (bool, error) {
return loadProto(s.Base, clusterPath, meta)
}
// SaveMeta save cluster meta to storage.
func (s *Storage) SaveMeta(meta *metapb.Cluster) error {
return saveProto(s.Base, clusterPath, meta)
}
// LoadStore loads one store from storage.
func (s *Storage) LoadStore(storeID uint64, store *metapb.Store) (bool, error) {
return loadProto(s.Base, s.storePath(storeID), store)
}
// SaveStore saves one store to storage.
func (s *Storage) SaveStore(store *metapb.Store) error {
return saveProto(s.Base, s.storePath(store.GetId()), store)
}
// DeleteStore deletes one store from storage.
func (s *Storage) DeleteStore(store *metapb.Store) error {
return s.Remove(s.storePath(store.GetId()))
}
// LoadStores loads all stores from storage to StoresInfo.
func (s *Storage) LoadStores(f func(store *StoreInfo)) error {
nextID := uint64(0)
endKey := s.storePath(math.MaxUint64)
for {
key := s.storePath(nextID)
_, res, err := s.LoadRange(key, endKey, minKVRangeLimit)
if err != nil {
return err
}
for _, str := range res {
store := &metapb.Store{}
if err := store.Unmarshal([]byte(str)); err != nil {
return errors.WithStack(err)
}
leaderWeight, err := s.loadFloatWithDefaultValue(s.storeLeaderWeightPath(store.GetId()), 1.0)
if err != nil {
return err
}
regionWeight, err := s.loadFloatWithDefaultValue(s.storeRegionWeightPath(store.GetId()), 1.0)
if err != nil {
return err
}
newStoreInfo := NewStoreInfo(store, SetLeaderWeight(leaderWeight), SetRegionWeight(regionWeight))
nextID = store.GetId() + 1
f(newStoreInfo)
}
if len(res) < minKVRangeLimit {
return nil
}
}
}
// SaveStoreWeight saves a store's leader and region weight to storage.
func (s *Storage) SaveStoreWeight(storeID uint64, leader, region float64) error {
leaderValue := strconv.FormatFloat(leader, 'f', -1, 64)
if err := s.Save(s.storeLeaderWeightPath(storeID), leaderValue); err != nil {
return err
}
regionValue := strconv.FormatFloat(region, 'f', -1, 64)
return s.Save(s.storeRegionWeightPath(storeID), regionValue)
}
func (s *Storage) loadFloatWithDefaultValue(path string, def float64) (float64, error) {
res, err := s.Load(path)
if err != nil {
return 0, err
}
if res == "" {
return def, nil
}
val, err := strconv.ParseFloat(res, 64)
if err != nil {
return 0, errors.WithStack(err)
}
return val, nil
}
// Flush flushes the dirty region to storage.
func (s *Storage) Flush() error {
return nil
}
// Close closes the s.
func (s *Storage) Close() error {
return nil
}
// SaveGCSafePoint saves new GC safe point to storage.
func (s *Storage) SaveGCSafePoint(safePoint uint64) error {
key := path.Join(gcPath, "safe_point")
value := strconv.FormatUint(safePoint, 16)
return s.Save(key, value)
}
// LoadGCSafePoint loads current GC safe point from storage.
func (s *Storage) LoadGCSafePoint() (uint64, error) {
key := path.Join(gcPath, "safe_point")
value, err := s.Load(key)
if err != nil {
return 0, err
}
if value == "" {
return 0, nil
}
safePoint, err := strconv.ParseUint(value, 16, 64)
if err != nil {
return 0, err
}
return safePoint, nil
}
// LoadAllScheduleConfig loads all schedulers' config.
func (s *Storage) LoadAllScheduleConfig() ([]string, []string, error) {
keys, values, err := s.LoadRange(customScheduleConfigPath, clientv3.GetPrefixRangeEnd(customScheduleConfigPath), 1000)
for i, key := range keys {
keys[i] = strings.TrimPrefix(key, customScheduleConfigPath+"/")
}
return keys, values, err
}
func loadProto(s kv.Base, key string, msg proto.Message) (bool, error) {
value, err := s.Load(key)
if err != nil {
return false, err
}
if value == "" {
return false, nil
}
err = proto.Unmarshal([]byte(value), msg)
return true, errors.WithStack(err)
}
func saveProto(s kv.Base, key string, msg proto.Message) error {
value, err := proto.Marshal(msg)
if err != nil {
return errors.WithStack(err)
}
return s.Save(key, string(value))
}