-
Notifications
You must be signed in to change notification settings - Fork 651
/
meta.go
298 lines (255 loc) · 6.91 KB
/
meta.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
// 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 (
"fmt"
"sync"
"syscall"
"time"
"golang.org/x/time/rate"
"github.com/chubaofs/chubaofs/proto"
authSDK "github.com/chubaofs/chubaofs/sdk/auth"
masterSDK "github.com/chubaofs/chubaofs/sdk/master"
"github.com/chubaofs/chubaofs/util"
"github.com/chubaofs/chubaofs/util/auth"
"github.com/chubaofs/chubaofs/util/btree"
"github.com/chubaofs/chubaofs/util/errors"
)
const (
HostsSeparator = ","
RefreshMetaPartitionsInterval = time.Minute * 5
)
const (
statusUnknown int = iota
statusOK
statusExist
statusNoent
statusFull
statusAgain
statusError
statusInval
statusNotPerm
statusConflictExtents
)
const (
MaxMountRetryLimit = 5
MountRetryInterval = time.Second * 5
/*
* Minimum interval of forceUpdateMetaPartitions in seconds,
* i.e. only one force update request is allowed every 5 sec.
*/
MinForceUpdateMetaPartitionsInterval = 5
)
type AsyncTaskErrorFunc func(err error)
func (f AsyncTaskErrorFunc) OnError(err error) {
if f != nil {
f(err)
}
}
type MetaConfig struct {
Volume string
Owner string
Masters []string
Authenticate bool
TicketMess auth.TicketMess
ValidateOwner bool
OnAsyncTaskError AsyncTaskErrorFunc
EnableSummary bool
}
type MetaWrapper struct {
sync.RWMutex
cluster string
localIP string
volname string
ossSecure *OSSSecure
volCreateTime int64
owner string
ownerValidation bool
mc *masterSDK.MasterClient
ac *authSDK.AuthClient
conns *util.ConnectPool
// Callback handler for handling asynchronous task errors.
onAsyncTaskError AsyncTaskErrorFunc
// Partitions and ranges should be modified together. So do not
// use partitions and ranges directly. Use the helper functions instead.
// Partition map indexed by ID
partitions map[uint64]*MetaPartition
// Partition tree indexed by Start, in order to find a partition in which
// a specific inode locate.
ranges *btree.BTree
rwPartitions []*MetaPartition
epoch uint64
totalSize uint64
usedSize uint64
inodeCount uint64
authenticate bool
Ticket auth.Ticket
accessToken proto.APIAccessReq
sessionKey string
ticketMess auth.TicketMess
closeCh chan struct{}
closeOnce sync.Once
// Used to signal the go routines which are waiting for partition view update
partMutex sync.Mutex
partCond *sync.Cond
// Used to trigger and throttle instant partition updates
forceUpdate chan struct{}
forceUpdateLimit *rate.Limiter
EnableSummary bool
}
//the ticket from authnode
type Ticket struct {
ID string `json:"client_id"`
SessionKey string `json:"session_key"`
ServiceID string `json:"service_id"`
Ticket string `json:"ticket"`
}
func NewMetaWrapper(config *MetaConfig) (*MetaWrapper, error) {
var err error
mw := new(MetaWrapper)
mw.closeCh = make(chan struct{}, 1)
if config.Authenticate {
var ticketMess = config.TicketMess
mw.ac = authSDK.NewAuthClient(ticketMess.TicketHosts, ticketMess.EnableHTTPS, ticketMess.CertFile)
ticket, err := mw.ac.API().GetTicket(config.Owner, ticketMess.ClientKey, proto.MasterServiceID)
if err != nil {
return nil, errors.Trace(err, "Get ticket from authnode failed!")
}
mw.authenticate = config.Authenticate
mw.accessToken.Ticket = ticket.Ticket
mw.accessToken.ClientID = config.Owner
mw.accessToken.ServiceID = proto.MasterServiceID
mw.sessionKey = ticket.SessionKey
mw.ticketMess = ticketMess
}
mw.volname = config.Volume
mw.owner = config.Owner
mw.ownerValidation = config.ValidateOwner
mw.mc = masterSDK.NewMasterClient(config.Masters, false)
mw.onAsyncTaskError = config.OnAsyncTaskError
mw.conns = util.NewConnectPool()
mw.partitions = make(map[uint64]*MetaPartition)
mw.ranges = btree.New(32)
mw.rwPartitions = make([]*MetaPartition, 0)
mw.partCond = sync.NewCond(&mw.partMutex)
mw.forceUpdate = make(chan struct{}, 1)
mw.forceUpdateLimit = rate.NewLimiter(1, MinForceUpdateMetaPartitionsInterval)
mw.EnableSummary = config.EnableSummary
limit := MaxMountRetryLimit
for limit > 0 {
err = mw.initMetaWrapper()
// When initializing the volume, if the master explicitly responds that the specified
// volume does not exist, it will not retry.
if err == proto.ErrVolNotExists {
return nil, err
}
if err != nil {
limit--
time.Sleep(MountRetryInterval)
continue
}
break
}
if limit <= 0 && err != nil {
return nil, err
}
go mw.refresh()
return mw, nil
}
func (mw *MetaWrapper) initMetaWrapper() (err error) {
if err = mw.updateClusterInfo(); err != nil {
return err
}
if err = mw.updateVolStatInfo(); err != nil {
return err
}
if err = mw.updateMetaPartitions(); err != nil {
return err
}
return nil
}
func (mw *MetaWrapper) Owner() string {
return mw.owner
}
func (mw *MetaWrapper) OSSSecure() (accessKey, secretKey string) {
return mw.ossSecure.AccessKey, mw.ossSecure.SecretKey
}
func (mw *MetaWrapper) VolCreateTime() int64 {
return mw.volCreateTime
}
func (mw *MetaWrapper) Close() error {
mw.closeOnce.Do(func() {
close(mw.closeCh)
mw.conns.Close()
})
return nil
}
func (mw *MetaWrapper) Cluster() string {
return mw.cluster
}
func (mw *MetaWrapper) LocalIP() string {
return mw.localIP
}
func (mw *MetaWrapper) exporterKey(act string) string {
return fmt.Sprintf("%s_sdk_meta_%s", mw.cluster, act)
}
// Proto ResultCode to status
func parseStatus(result uint8) (status int) {
switch result {
case proto.OpOk:
status = statusOK
case proto.OpExistErr:
status = statusExist
case proto.OpNotExistErr:
status = statusNoent
case proto.OpInodeFullErr:
status = statusFull
case proto.OpAgain:
status = statusAgain
case proto.OpArgMismatchErr:
status = statusInval
case proto.OpNotPerm:
status = statusNotPerm
case proto.OpConflictExtentsErr:
status = statusConflictExtents
default:
status = statusError
}
return
}
func statusToErrno(status int) error {
switch status {
case statusOK:
// return error anyway
return syscall.EAGAIN
case statusExist:
return syscall.EEXIST
case statusNoent:
return syscall.ENOENT
case statusFull:
return syscall.ENOMEM
case statusAgain:
return syscall.EAGAIN
case statusInval:
return syscall.EINVAL
case statusNotPerm:
return syscall.EPERM
case statusError:
return syscall.EAGAIN
case statusConflictExtents:
return syscall.ENOTSUP
default:
}
return syscall.EIO
}