-
Notifications
You must be signed in to change notification settings - Fork 646
/
view.go
337 lines (302 loc) · 8.86 KB
/
view.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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
// Copyright 2018 The Chubao Authors.
//
// 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,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
// implied. See the License for the specific language governing
// permissions and limitations under the License.
package meta
import (
"crypto/md5"
"encoding/base64"
"encoding/hex"
"encoding/json"
"fmt"
"os"
"strings"
"sync/atomic"
"time"
"github.com/chubaofs/chubaofs/proto"
"github.com/chubaofs/chubaofs/sdk/master"
"github.com/chubaofs/chubaofs/util/cryptoutil"
"github.com/chubaofs/chubaofs/util/errors"
"github.com/chubaofs/chubaofs/util/log"
"github.com/jacobsa/daemonize"
)
const (
MaxSendToMaster = 3
)
type VolumeView struct {
Name string
Owner string
MetaPartitions []*MetaPartition
OSSSecure *OSSSecure
CreateTime int64
}
type OSSSecure struct {
AccessKey string
SecretKey string
}
type VolStatInfo = proto.VolStatInfo
func (mw *MetaWrapper) fetchVolumeView() (view *VolumeView, err error) {
var vv *proto.VolView
if mw.ownerValidation {
var authKey string
if authKey, err = calculateAuthKey(mw.owner); err != nil {
return
}
if mw.authenticate {
var (
tokenMessage string
ts int64
)
mw.accessToken.Type = proto.MsgMasterFetchVolViewReq
if tokenMessage, ts, err = genMasterToken(mw.accessToken, mw.sessionKey); err != nil {
log.LogWarnf("fetchVolumeView generate token failed: err(%v)", err)
return nil, err
}
var decoder master.Decoder = func(raw []byte) ([]byte, error) {
return mw.parseAndVerifyResp(raw, ts)
}
if vv, err = mw.mc.ClientAPI().GetVolumeWithAuthnode(mw.volname, authKey, tokenMessage, decoder); err != nil {
return
}
} else {
if vv, err = mw.mc.ClientAPI().GetVolume(mw.volname, authKey); err != nil {
return
}
}
} else {
if vv, err = mw.mc.ClientAPI().GetVolumeWithoutAuthKey(mw.volname); err != nil {
return
}
}
var convert = func(volView *proto.VolView) *VolumeView {
result := &VolumeView{
Name: volView.Name,
Owner: volView.Owner,
MetaPartitions: make([]*MetaPartition, len(volView.MetaPartitions)),
OSSSecure: &OSSSecure{},
CreateTime: volView.CreateTime,
}
if volView.OSSSecure != nil {
result.OSSSecure.AccessKey = volView.OSSSecure.AccessKey
result.OSSSecure.SecretKey = volView.OSSSecure.SecretKey
}
for i, mp := range volView.MetaPartitions {
result.MetaPartitions[i] = &MetaPartition{
PartitionID: mp.PartitionID,
Start: mp.Start,
End: mp.End,
Members: mp.Members,
LeaderAddr: mp.LeaderAddr,
Status: mp.Status,
}
}
return result
}
view = convert(vv)
return
}
// fetch and update cluster info if successful
func (mw *MetaWrapper) updateClusterInfo() (err error) {
var info *proto.ClusterInfo
if info, err = mw.mc.AdminAPI().GetClusterInfo(); err != nil {
log.LogWarnf("updateClusterInfo: get cluster info fail: err(%v)", err)
return
}
log.LogInfof("updateClusterInfo: get cluster info: cluster(%v) localIP(%v)",
info.Cluster, info.Ip)
mw.cluster = info.Cluster
mw.localIP = info.Ip
return
}
func (mw *MetaWrapper) updateVolStatInfo() (err error) {
var info *proto.VolStatInfo
if info, err = mw.mc.ClientAPI().GetVolumeStat(mw.volname); err != nil {
log.LogWarnf("updateVolStatInfo: get volume status fail: volume(%v) err(%v)", mw.volname, err)
return
}
atomic.StoreUint64(&mw.totalSize, info.TotalSize)
atomic.StoreUint64(&mw.usedSize, info.UsedSize)
atomic.StoreUint64(&mw.inodeCount, info.InodeCount)
log.LogInfof("VolStatInfo: info(%v)", info)
return
}
func (mw *MetaWrapper) updateMetaPartitions() error {
view, err := mw.fetchVolumeView()
if err != nil {
log.LogInfof("error: %v", err.Error())
switch err {
case proto.ErrExpiredTicket:
// TODO: bad logic, remove later (Mofei Zhang)
if e := mw.updateTicket(); e != nil {
log.LogFlush()
daemonize.SignalOutcome(err)
os.Exit(1)
}
log.LogInfof("updateTicket: ok!")
return err
case proto.ErrInvalidTicket:
// TODO: bad logic, remove later (Mofei Zhang)
log.LogFlush()
daemonize.SignalOutcome(err)
os.Exit(1)
default:
return err
}
}
rwPartitions := make([]*MetaPartition, 0)
for _, mp := range view.MetaPartitions {
mw.replaceOrInsertPartition(mp)
log.LogInfof("updateMetaPartition: mp(%v)", mp)
if mp.Status == proto.ReadWrite {
rwPartitions = append(rwPartitions, mp)
}
}
mw.ossSecure = view.OSSSecure
mw.volCreateTime = view.CreateTime
if len(rwPartitions) == 0 {
log.LogInfof("updateMetaPartition: no valid partitions")
return nil
}
mw.Lock()
mw.rwPartitions = rwPartitions
mw.Unlock()
return nil
}
func (mw *MetaWrapper) forceUpdateMetaPartitions() error {
// Only one forceUpdateMetaPartition is allowed in a specific period of time.
if ok := mw.forceUpdateLimit.AllowN(time.Now(), MinForceUpdateMetaPartitionsInterval); !ok {
return errors.New("Force update meta partitions throttled!")
}
return mw.updateMetaPartitions()
}
// Should be protected by partMutex, otherwise the caller might not be signaled.
func (mw *MetaWrapper) triggerAndWaitForceUpdate() {
mw.partMutex.Lock()
select {
case mw.forceUpdate <- struct{}{}:
default:
}
mw.partCond.Wait()
mw.partMutex.Unlock()
}
func (mw *MetaWrapper) refresh() {
var err error
t := time.NewTimer(RefreshMetaPartitionsInterval)
defer t.Stop()
for {
select {
case <-t.C:
if err = mw.updateMetaPartitions(); err != nil {
mw.onAsyncTaskError.OnError(err)
log.LogErrorf("updateMetaPartition fail cause: %v", err)
}
if err = mw.updateVolStatInfo(); err != nil {
mw.onAsyncTaskError.OnError(err)
log.LogErrorf("updateVolStatInfo fail cause: %v", err)
}
t.Reset(RefreshMetaPartitionsInterval)
case <-mw.forceUpdate:
log.LogInfof("Start forceUpdateMetaPartitions")
mw.partMutex.Lock()
if err = mw.forceUpdateMetaPartitions(); err == nil {
if err = mw.updateVolStatInfo(); err == nil {
t.Reset(RefreshMetaPartitionsInterval)
}
}
mw.partMutex.Unlock()
mw.partCond.Broadcast()
log.LogInfof("End forceUpdateMetaPartitions: err(%v)", err)
case <-mw.closeCh:
return
}
}
}
func calculateAuthKey(key string) (authKey string, err error) {
h := md5.New()
_, err = h.Write([]byte(key))
if err != nil {
log.LogErrorf("action[calculateAuthKey] calculate auth key[%v] failed,err[%v]", key, err)
return
}
cipherStr := h.Sum(nil)
return strings.ToLower(hex.EncodeToString(cipherStr)), nil
}
func genMasterToken(req proto.APIAccessReq, key string) (message string, ts int64, err error) {
var (
sessionKey []byte
data []byte
)
if sessionKey, err = cryptoutil.Base64Decode(key); err != nil {
return
}
if req.Verifier, ts, err = cryptoutil.GenVerifier(sessionKey); err != nil {
return
}
if data, err = json.Marshal(req); err != nil {
return
}
message = base64.StdEncoding.EncodeToString(data)
return
}
func (mw *MetaWrapper) updateTicket() error {
ticket, err := mw.ac.API().GetTicket(mw.owner, mw.ticketMess.ClientKey, proto.MasterServiceID)
if err != nil {
return errors.Trace(err, "Update ticket from authnode failed!")
}
mw.accessToken.Ticket = ticket.Ticket
mw.sessionKey = ticket.SessionKey
return nil
}
func (mw *MetaWrapper) parseAndVerifyResp(body []byte, ts int64) (dataBody []byte, err error) {
var resp proto.MasterAPIAccessResp
if resp, err = mw.parseRespWithAuth(body); err != nil {
log.LogWarnf("fetchVolumeView parse response failed: err(%v) body(%v)", err, string(body))
return nil, err
}
if err = proto.VerifyAPIRespComm(&(resp.APIResp), mw.accessToken.Type, mw.owner, proto.MasterServiceID, ts); err != nil {
log.LogWarnf("fetchVolumeView verify response: err(%v)", err)
return nil, err
}
var viewBody = &struct {
Code int32 `json:"code"`
Msg string `json:"msg"`
Data json.RawMessage
}{}
if err = json.Unmarshal(resp.Data, viewBody); err != nil {
log.LogWarnf("VolViewCache unmarshal: err(%v) body(%v)", err, viewBody)
return nil, err
}
if viewBody.Code != 0 {
return nil, fmt.Errorf("request error, code[%d], msg[%s]", viewBody.Code, viewBody.Msg)
}
return viewBody.Data, err
}
func (mw *MetaWrapper) parseRespWithAuth(body []byte) (resp proto.MasterAPIAccessResp, err error) {
var (
message string
sessionKey []byte
plaintext []byte
)
if err = json.Unmarshal(body, &message); err != nil {
return
}
if sessionKey, err = cryptoutil.Base64Decode(mw.sessionKey); err != nil {
return
}
if plaintext, err = cryptoutil.DecodeMessage(message, sessionKey); err != nil {
return
}
if err = json.Unmarshal(plaintext, &resp); err != nil {
return
}
return
}