/
pool.go
444 lines (382 loc) · 15.1 KB
/
pool.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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
/*
Copyright 2016 The Rook Authors. All rights reserved.
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 client
import (
"encoding/json"
"fmt"
"strconv"
"strings"
"github.com/pkg/errors"
cephv1 "github.com/rook/rook/pkg/apis/ceph.rook.io/v1"
"github.com/rook/rook/pkg/clusterd"
"github.com/rook/rook/pkg/daemon/ceph/model"
)
const (
confirmFlag = "--yes-i-really-mean-it"
reallyConfirmFlag = "--yes-i-really-really-mean-it"
)
type CephStoragePoolSummary struct {
Name string `json:"poolname"`
Number int `json:"poolnum"`
}
type CephStoragePoolDetails struct {
Name string `json:"pool"`
Number int `json:"pool_id"`
Size uint `json:"size"`
ErasureCodeProfile string `json:"erasure_code_profile"`
FailureDomain string `json:"failureDomain"`
CrushRoot string `json:"crushRoot"`
DeviceClass string `json:"deviceClass"`
NotEnableAppPool bool `json:"notEnableAppPool"`
TargetSizeRatio float64 `json:"target_size_ratio,omitempty"`
}
type CephStoragePoolStats struct {
Pools []struct {
Name string `json:"name"`
ID int `json:"id"`
Stats struct {
BytesUsed float64 `json:"bytes_used"`
RawBytesUsed float64 `json:"raw_bytes_used"`
MaxAvail float64 `json:"max_avail"`
Objects float64 `json:"objects"`
DirtyObjects float64 `json:"dirty"`
ReadIO float64 `json:"rd"`
ReadBytes float64 `json:"rd_bytes"`
WriteIO float64 `json:"wr"`
WriteBytes float64 `json:"wr_bytes"`
} `json:"stats"`
} `json:"pools"`
}
type PoolStatistics struct {
Images struct {
Count int `json:"count"`
ProvisionedBytes int `json:"provisioned_bytes"`
SnapCount int `json:"snap_count"`
} `json:"images"`
Trash struct {
Count int `json:"count"`
ProvisionedBytes int `json:"provisioned_bytes"`
SnapCount int `json:"snap_count"`
} `json:"trash"`
}
func ListPoolSummaries(context *clusterd.Context, clusterName string) ([]CephStoragePoolSummary, error) {
args := []string{"osd", "lspools"}
buf, err := NewCephCommand(context, clusterName, args).Run()
if err != nil {
return nil, errors.Wrapf(err, "failed to list pools")
}
var pools []CephStoragePoolSummary
err = json.Unmarshal(buf, &pools)
if err != nil {
return nil, errors.Wrapf(err, "unmarshal failed raw buffer response %s", string(buf))
}
return pools, nil
}
func GetPoolNamesByID(context *clusterd.Context, clusterName string) (map[int]string, error) {
pools, err := ListPoolSummaries(context, clusterName)
if err != nil {
return nil, errors.Wrapf(err, "failed to list pools")
}
names := map[int]string{}
for _, p := range pools {
names[p.Number] = p.Name
}
return names, nil
}
func GetPoolDetails(context *clusterd.Context, clusterName, name string) (CephStoragePoolDetails, error) {
args := []string{"osd", "pool", "get", name, "all"}
buf, err := NewCephCommand(context, clusterName, args).Run()
if err != nil {
return CephStoragePoolDetails{}, errors.Wrapf(err, "failed to get pool %s details", name)
}
// The response for osd pool get when passing var=all is actually malformed JSON similar to:
// {"pool":"rbd","size":1}{"pool":"rbd","min_size":2}...
// Note the multiple top level entities, one for each property returned. To workaround this,
// we split the JSON response string into its top level entities, then iterate through them, cleaning
// up the JSON. A single pool details object is repeatedly used to unmarshal each JSON snippet into.
// Since previously set fields remain intact if they are not overwritten, the result is the JSON
// unmarshalling of all properties in the response.
var poolDetails CephStoragePoolDetails
poolDetailsUnits := strings.Split(string(buf), "}{")
for i := range poolDetailsUnits {
pdu := poolDetailsUnits[i]
if !strings.HasPrefix(pdu, "{") {
pdu = "{" + pdu
}
if !strings.HasSuffix(pdu, "}") {
pdu += "}"
}
err := json.Unmarshal([]byte(pdu), &poolDetails)
if err != nil {
return CephStoragePoolDetails{}, errors.Wrapf(err, "unmarshal failed raw buffer response %s", string(buf))
}
}
return poolDetails, nil
}
func CreatePoolWithProfile(context *clusterd.Context, clusterName string, newPoolReq model.Pool, appName string) error {
newPool := ModelPoolToCephPool(newPoolReq)
if newPoolReq.Type == model.ErasureCoded {
// create a new erasure code profile for the new pool
if err := CreateErasureCodeProfile(context, clusterName, newPoolReq.ErasureCodedConfig, newPool.ErasureCodeProfile,
newPoolReq.FailureDomain, newPoolReq.CrushRoot, newPoolReq.DeviceClass); err != nil {
return errors.Wrapf(err, "failed to create erasure code profile for pool %q", newPoolReq.Name)
}
}
isReplicatedPool := newPool.ErasureCodeProfile == "" && newPool.Size > 0
if isReplicatedPool {
return CreateReplicatedPoolForApp(context, clusterName, newPool, DefaultPGCount, appName)
}
// If the pool is not a replicated pool, then the only other option is an erasure coded pool.
return CreateECPoolForApp(
context,
clusterName,
newPool,
DefaultPGCount,
appName,
true, /* enableECOverwrite */
newPoolReq.ErasureCodedConfig,
)
}
func checkForImagesInPool(context *clusterd.Context, name, namespace string) error {
var err error
var stats = new(PoolStatistics)
logger.Infof("checking any images/snapshosts present in pool %s", name)
stats, err = GetPoolStatistics(context, name, namespace)
if err != nil {
if strings.Contains(err.Error(), "No such file or directory") {
return nil
}
return errors.Wrapf(err, "failed to list images/snapshosts in pool %s", name)
}
if stats.Images.Count == 0 && stats.Images.SnapCount == 0 {
logger.Infof("no images/snapshosts present in pool %s", name)
return nil
}
return errors.Errorf("pool %s contains images/snapshosts", name)
}
func DeletePool(context *clusterd.Context, clusterName string, name string) error {
// check if the pool exists
pool, err := GetPoolDetails(context, clusterName, name)
if err != nil {
logger.Infof("pool %q not found for deletion. %v", name, err)
return nil
}
err = checkForImagesInPool(context, name, clusterName)
if err != nil {
return errors.Wrapf(err, "failed to delete pool %q", name)
}
logger.Infof("purging pool %s (id=%d)", name, pool.Number)
args := []string{"osd", "pool", "delete", name, name, reallyConfirmFlag}
_, err = NewCephCommand(context, clusterName, args).Run()
if err != nil {
return errors.Wrapf(err, "failed to delete pool %q", name)
}
// remove the crush rule for this pool and ignore the error in case the rule is still in use or not found
args = []string{"osd", "crush", "rule", "rm", name}
_, err = NewCephCommand(context, clusterName, args).Run()
if err != nil {
logger.Infof("did not delete crush rule %q. %v", name, err)
}
logger.Infof("purge completed for pool %q", name)
return nil
}
func givePoolAppTag(context *clusterd.Context, clusterName string, poolName string, appName string) error {
args := []string{"osd", "pool", "application", "enable", poolName, appName, confirmFlag}
_, err := NewCephCommand(context, clusterName, args).Run()
if err != nil {
return errors.Wrapf(err, "failed to enable application %s on pool %s", appName, poolName)
}
return nil
}
func CreateECPoolForApp(context *clusterd.Context, clusterName string, newPool CephStoragePoolDetails, pgCount, appName string, enableECOverwrite bool, erasureCodedConfig model.ErasureCodedPoolConfig) error {
args := []string{"osd", "pool", "create", newPool.Name, pgCount, "erasure", newPool.ErasureCodeProfile}
buf, err := NewCephCommand(context, clusterName, args).Run()
if err != nil {
return errors.Wrapf(err, "failed to create EC pool %s", newPool.Name)
}
if enableECOverwrite {
if err = SetPoolProperty(context, clusterName, newPool.Name, "allow_ec_overwrites", "true"); err != nil {
return errors.Wrapf(err, "failed to allow EC overwrite for pool %s", newPool.Name)
}
}
if !newPool.NotEnableAppPool {
err = givePoolAppTag(context, clusterName, newPool.Name, appName)
if err != nil {
return err
}
}
logger.Infof("creating EC pool %s succeeded, buf: %s", newPool.Name, string(buf))
return nil
}
func CreateReplicatedPoolForApp(context *clusterd.Context, clusterName string, newPool CephStoragePoolDetails, pgCount, appName string) error {
// create a crush rule for a replicated pool, if a failure domain is specified
if err := createReplicationCrushRule(context, clusterName, newPool, newPool.Name); err != nil {
return err
}
args := []string{"osd", "pool", "create", newPool.Name, pgCount, "replicated", newPool.Name}
buf, err := NewCephCommand(context, clusterName, args).Run()
if err != nil {
return errors.Wrapf(err, "failed to create replicated pool %s", newPool.Name)
}
// the pool is type replicated, set the size for the pool now that it's been created
sizeProperty := "size"
if err = SetPoolProperty(context, clusterName, newPool.Name, sizeProperty, strconv.FormatUint(uint64(newPool.Size), 10)); err != nil {
return errors.Wrapf(err, "failed to set property %q to replicated pool %q to %d", sizeProperty, newPool.Name, newPool.Size)
}
targetSizeRatioProperty := "target_size_ratio"
if newPool.TargetSizeRatio != 0 {
err = SetPoolProperty(context, clusterName, newPool.Name, targetSizeRatioProperty, strconv.FormatFloat(newPool.TargetSizeRatio, 'f', -1, 32))
if err != nil {
return errors.Wrapf(err, "failed to set property %q to replicated pool %q to %b", targetSizeRatioProperty, newPool.Name, newPool.TargetSizeRatio)
}
}
// ensure that the newly created pool gets an application tag
if !newPool.NotEnableAppPool {
err = givePoolAppTag(context, clusterName, newPool.Name, appName)
if err != nil {
return err
}
}
logger.Infof("creating replicated pool %s succeeded, buf: %s", newPool.Name, string(buf))
return nil
}
func createReplicationCrushRule(context *clusterd.Context, clusterName string, newPool CephStoragePoolDetails, ruleName string) error {
failureDomain := newPool.FailureDomain
if failureDomain == "" {
failureDomain = cephv1.DefaultFailureDomain
}
// set the crush root to the default if not already specified
crushRoot := "default"
if newPool.CrushRoot != "" {
crushRoot = newPool.CrushRoot
}
args := []string{"osd", "crush", "rule", "create-replicated", ruleName, crushRoot, failureDomain}
var deviceClass string
if newPool.DeviceClass != "" {
deviceClass = newPool.DeviceClass
args = append(args, deviceClass)
}
_, err := NewCephCommand(context, clusterName, args).Run()
if err != nil {
return errors.Wrapf(err, "failed to create crush rule %s", ruleName)
}
return nil
}
// SetPoolProperty sets a property to a given pool
func SetPoolProperty(context *clusterd.Context, clusterName, name, propName string, propVal string) error {
args := []string{"osd", "pool", "set", name, propName, propVal}
_, err := NewCephCommand(context, clusterName, args).Run()
if err != nil {
return errors.Wrapf(err, "failed to set pool property %s on pool %s", propName, name)
}
return nil
}
func GetPoolStats(context *clusterd.Context, clusterName string) (*CephStoragePoolStats, error) {
args := []string{"df", "detail"}
buf, err := NewCephCommand(context, clusterName, args).Run()
if err != nil {
return nil, errors.Wrapf(err, "failed to get pool stats")
}
var poolStats CephStoragePoolStats
if err := json.Unmarshal(buf, &poolStats); err != nil {
return nil, errors.Wrapf(err, "failed to unmarshal pool stats response")
}
return &poolStats, nil
}
func GetPoolStatistics(context *clusterd.Context, name, clusterName string) (*PoolStatistics, error) {
args := []string{"pool", "stats", name}
cmd := NewRBDCommand(context, clusterName, args)
cmd.JsonOutput = true
buf, err := cmd.Run()
if err != nil {
return nil, errors.Wrapf(err, "failed to get pool stats")
}
var poolStats PoolStatistics
if err := json.Unmarshal(buf, &poolStats); err != nil {
return nil, errors.Wrapf(err, "failed to unmarshal pool stats response")
}
return &poolStats, nil
}
func GetPools(context *clusterd.Context, clusterName string) ([]model.Pool, error) {
// list pool summaries using the ceph client
cephPoolSummaries, err := ListPoolSummaries(context, clusterName)
if err != nil {
return nil, errors.Wrapf(err, "failed to list pools")
}
// get the details for each pool from its summary information
cephPools := make([]CephStoragePoolDetails, len(cephPoolSummaries))
for i := range cephPoolSummaries {
poolDetails, err := GetPoolDetails(context, clusterName, cephPoolSummaries[i].Name)
if err != nil {
return nil, errors.Wrapf(err, "failed to get details for pool %s", cephPoolSummaries[i].Name)
}
cephPools[i] = poolDetails
}
var ecProfileDetails map[string]CephErasureCodeProfile
lookupECProfileDetails := false
for i := range cephPools {
if cephPools[i].ErasureCodeProfile != "" {
// at least one pool is erasure coded, we'll need to look up erasure code profile details
lookupECProfileDetails = true
break
}
}
if lookupECProfileDetails {
// list each erasure code profile
ecProfileNames, err := ListErasureCodeProfiles(context, clusterName)
if err != nil {
return nil, errors.Wrapf(err, "failed to list erasure code profiles")
}
// get the details of each erasure code profile and store them in the map
ecProfileDetails = make(map[string]CephErasureCodeProfile, len(ecProfileNames))
for _, name := range ecProfileNames {
ecp, err := GetErasureCodeProfileDetails(context, clusterName, name)
if err != nil {
return nil, errors.Wrapf(err, "failed to get erasure code profile details for %q", name)
}
ecProfileDetails[name] = ecp
}
}
// convert the ceph pools details to model pools
pools := make([]model.Pool, len(cephPools))
for i, p := range cephPools {
pool, err := cephPoolToModelPool(p, ecProfileDetails)
if err != nil {
return nil, errors.Wrapf(err, "failed to convert ceph pool to model")
}
pools[i] = pool
}
return pools, nil
}
func cephPoolToModelPool(cephPool CephStoragePoolDetails, ecpDetails map[string]CephErasureCodeProfile) (model.Pool, error) {
pool := model.Pool{
Name: cephPool.Name,
Number: cephPool.Number,
}
if cephPool.ErasureCodeProfile != "" {
ecpDetails, ok := ecpDetails[cephPool.ErasureCodeProfile]
if !ok {
return model.Pool{}, errors.Errorf("failed to look up erasure code profile details for %q", cephPool.ErasureCodeProfile)
}
pool.Type = model.ErasureCoded
pool.ErasureCodedConfig.DataChunkCount = ecpDetails.DataChunkCount
pool.ErasureCodedConfig.CodingChunkCount = ecpDetails.CodingChunkCount
pool.ErasureCodedConfig.Algorithm = fmt.Sprintf("%s::%s", ecpDetails.Plugin, ecpDetails.Technique)
} else if cephPool.Size > 0 {
pool.Type = model.Replicated
pool.ReplicatedConfig.Size = cephPool.Size
} else {
pool.Type = model.PoolTypeUnknown
}
return pool, nil
}