pool.go 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807
  1. /*
  2. Copyright 2016 The Rook Authors. All rights reserved.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package client
  14. import (
  15. "encoding/json"
  16. "fmt"
  17. "os"
  18. "path/filepath"
  19. "strconv"
  20. "strings"
  21. "github.com/pkg/errors"
  22. cephv1 "github.com/rook/rook/pkg/apis/ceph.rook.io/v1"
  23. "github.com/rook/rook/pkg/clusterd"
  24. "k8s.io/apimachinery/pkg/api/resource"
  25. )
  26. const (
  27. confirmFlag = "--yes-i-really-mean-it"
  28. reallyConfirmFlag = "--yes-i-really-really-mean-it"
  29. targetSizeRatioProperty = "target_size_ratio"
  30. CompressionModeProperty = "compression_mode"
  31. PgAutoscaleModeProperty = "pg_autoscale_mode"
  32. PgAutoscaleModeOn = "on"
  33. )
  34. type CephStoragePoolSummary struct {
  35. Name string `json:"poolname"`
  36. Number int `json:"poolnum"`
  37. }
  38. type CephStoragePoolDetails struct {
  39. Name string `json:"pool"`
  40. Number int `json:"pool_id"`
  41. Size uint `json:"size"`
  42. ErasureCodeProfile string `json:"erasure_code_profile"`
  43. CrushRoot string `json:"crushRoot"`
  44. DeviceClass string `json:"deviceClass"`
  45. CompressionMode string `json:"compression_mode"`
  46. TargetSizeRatio float64 `json:"target_size_ratio,omitempty"`
  47. RequireSafeReplicaSize bool `json:"requireSafeReplicaSize,omitempty"`
  48. CrushRule string `json:"crush_rule"`
  49. }
  50. type CephStoragePoolStats struct {
  51. Pools []struct {
  52. Name string `json:"name"`
  53. ID int `json:"id"`
  54. Stats struct {
  55. BytesUsed float64 `json:"bytes_used"`
  56. RawBytesUsed float64 `json:"raw_bytes_used"`
  57. MaxAvail float64 `json:"max_avail"`
  58. Objects float64 `json:"objects"`
  59. DirtyObjects float64 `json:"dirty"`
  60. ReadIO float64 `json:"rd"`
  61. ReadBytes float64 `json:"rd_bytes"`
  62. WriteIO float64 `json:"wr"`
  63. WriteBytes float64 `json:"wr_bytes"`
  64. } `json:"stats"`
  65. } `json:"pools"`
  66. }
  67. type PoolStatistics struct {
  68. Images struct {
  69. Count int `json:"count"`
  70. ProvisionedBytes int `json:"provisioned_bytes"`
  71. SnapCount int `json:"snap_count"`
  72. } `json:"images"`
  73. Trash struct {
  74. Count int `json:"count"`
  75. ProvisionedBytes int `json:"provisioned_bytes"`
  76. SnapCount int `json:"snap_count"`
  77. } `json:"trash"`
  78. }
  79. func ListPoolSummaries(context *clusterd.Context, clusterInfo *ClusterInfo) ([]CephStoragePoolSummary, error) {
  80. args := []string{"osd", "lspools"}
  81. output, err := NewCephCommand(context, clusterInfo, args).Run()
  82. if err != nil {
  83. return nil, errors.Wrap(err, "failed to list pools")
  84. }
  85. var pools []CephStoragePoolSummary
  86. err = json.Unmarshal(output, &pools)
  87. if err != nil {
  88. return nil, errors.Wrapf(err, "unmarshal failed raw buffer response %s", string(output))
  89. }
  90. return pools, nil
  91. }
  92. func GetPoolNamesByID(context *clusterd.Context, clusterInfo *ClusterInfo) (map[int]string, error) {
  93. pools, err := ListPoolSummaries(context, clusterInfo)
  94. if err != nil {
  95. return nil, errors.Wrap(err, "failed to list pools")
  96. }
  97. names := map[int]string{}
  98. for _, p := range pools {
  99. names[p.Number] = p.Name
  100. }
  101. return names, nil
  102. }
  103. func getPoolApplication(context *clusterd.Context, clusterInfo *ClusterInfo, poolName string) (string, error) {
  104. args := []string{"osd", "pool", "application", "get", poolName}
  105. appDetails, err := NewCephCommand(context, clusterInfo, args).Run()
  106. if err != nil {
  107. return "", errors.Wrapf(err, "failed to get current application for pool %s", poolName)
  108. }
  109. if len(appDetails) == 0 {
  110. // no application name
  111. return "", nil
  112. }
  113. var application map[string]interface{}
  114. err = json.Unmarshal([]byte(appDetails), &application)
  115. if err != nil {
  116. return "", errors.Wrapf(err, "unmarshal failed raw buffer response %s", string(appDetails))
  117. }
  118. for name := range application {
  119. // Return the first application name in the list since only one is expected
  120. return name, nil
  121. }
  122. // No application name assigned
  123. return "", nil
  124. }
  125. // GetPoolDetails gets all the details of a given pool
  126. func GetPoolDetails(context *clusterd.Context, clusterInfo *ClusterInfo, name string) (CephStoragePoolDetails, error) {
  127. args := []string{"osd", "pool", "get", name, "all"}
  128. output, err := NewCephCommand(context, clusterInfo, args).Run()
  129. if err != nil {
  130. return CephStoragePoolDetails{}, errors.Wrapf(err, "failed to get pool %s details. %s", name, string(output))
  131. }
  132. return ParsePoolDetails(output)
  133. }
  134. func ParsePoolDetails(in []byte) (CephStoragePoolDetails, error) {
  135. // The response for osd pool get when passing var=all is actually malformed JSON similar to:
  136. // {"pool":"rbd","size":1}{"pool":"rbd","min_size":2}...
  137. // Note the multiple top level entities, one for each property returned. To workaround this,
  138. // we split the JSON response string into its top level entities, then iterate through them, cleaning
  139. // up the JSON. A single pool details object is repeatedly used to unmarshal each JSON snippet into.
  140. // Since previously set fields remain intact if they are not overwritten, the result is the JSON
  141. // unmarshalling of all properties in the response.
  142. var poolDetails CephStoragePoolDetails
  143. poolDetailsUnits := strings.Split(string(in), "}{")
  144. for i := range poolDetailsUnits {
  145. pdu := poolDetailsUnits[i]
  146. if !strings.HasPrefix(pdu, "{") {
  147. pdu = "{" + pdu
  148. }
  149. if !strings.HasSuffix(pdu, "}") {
  150. pdu += "}"
  151. }
  152. err := json.Unmarshal([]byte(pdu), &poolDetails)
  153. if err != nil {
  154. return CephStoragePoolDetails{}, errors.Wrapf(err, "unmarshal failed raw buffer response %s", string(in))
  155. }
  156. }
  157. return poolDetails, nil
  158. }
  159. func CreatePool(context *clusterd.Context, clusterInfo *ClusterInfo, clusterSpec *cephv1.ClusterSpec, pool cephv1.NamedPoolSpec, appName string) error {
  160. return CreatePoolWithPGs(context, clusterInfo, clusterSpec, pool, appName, DefaultPGCount)
  161. }
  162. func CreatePoolWithPGs(context *clusterd.Context, clusterInfo *ClusterInfo, clusterSpec *cephv1.ClusterSpec, pool cephv1.NamedPoolSpec, appName, pgCount string) error {
  163. if pool.Name == "" {
  164. return errors.New("pool name must be specified")
  165. }
  166. if pool.IsReplicated() {
  167. return createReplicatedPoolForApp(context, clusterInfo, clusterSpec, pool, pgCount, appName)
  168. }
  169. if !pool.IsErasureCoded() {
  170. // neither a replicated or EC pool
  171. return errors.Errorf("pool %q type is not defined as replicated or erasure coded", pool.Name)
  172. }
  173. // create a new erasure code profile for the new pool
  174. ecProfileName := GetErasureCodeProfileForPool(pool.Name)
  175. if err := CreateErasureCodeProfile(context, clusterInfo, ecProfileName, pool.PoolSpec); err != nil {
  176. return errors.Wrapf(err, "failed to create erasure code profile for pool %q", pool.Name)
  177. }
  178. // If the pool is not a replicated pool, then the only other option is an erasure coded pool.
  179. return createECPoolForApp(
  180. context,
  181. clusterInfo,
  182. ecProfileName,
  183. pool,
  184. pgCount,
  185. appName,
  186. true /* enableECOverwrite */)
  187. }
  188. func checkForImagesInPool(context *clusterd.Context, clusterInfo *ClusterInfo, name string) error {
  189. var err error
  190. logger.Debugf("checking any images/snapshots present in pool %q", name)
  191. stats, err := GetPoolStatistics(context, clusterInfo, name)
  192. if err != nil {
  193. if strings.Contains(err.Error(), "No such file or directory") {
  194. return nil
  195. }
  196. return errors.Wrapf(err, "failed to list images/snapshots in pool %s", name)
  197. }
  198. if stats.Images.Count == 0 && stats.Images.SnapCount == 0 {
  199. logger.Infof("no images/snapshots present in pool %q", name)
  200. return nil
  201. }
  202. return errors.Errorf("pool %q contains images/snapshots", name)
  203. }
  204. // DeletePool purges a pool from Ceph
  205. func DeletePool(context *clusterd.Context, clusterInfo *ClusterInfo, name string) error {
  206. // check if the pool exists
  207. pool, err := GetPoolDetails(context, clusterInfo, name)
  208. if err != nil {
  209. return errors.Wrapf(err, "failed to get pool %q details", name)
  210. }
  211. err = checkForImagesInPool(context, clusterInfo, name)
  212. if err != nil {
  213. return errors.Wrapf(err, "failed to check if pool %q has rbd images", name)
  214. }
  215. logger.Infof("purging pool %q (id=%d)", name, pool.Number)
  216. args := []string{"osd", "pool", "delete", name, name, reallyConfirmFlag}
  217. _, err = NewCephCommand(context, clusterInfo, args).Run()
  218. if err != nil {
  219. return errors.Wrapf(err, "failed to delete pool %q", name)
  220. }
  221. // remove the crush rule for this pool and ignore the error in case the rule is still in use or not found
  222. args = []string{"osd", "crush", "rule", "rm", name}
  223. _, err = NewCephCommand(context, clusterInfo, args).Run()
  224. if err != nil {
  225. logger.Errorf("failed to delete crush rule %q. %v", name, err)
  226. }
  227. logger.Infof("purge completed for pool %q", name)
  228. return nil
  229. }
  230. func givePoolAppTag(context *clusterd.Context, clusterInfo *ClusterInfo, poolName, appName string) error {
  231. currentAppName, err := getPoolApplication(context, clusterInfo, poolName)
  232. if err != nil {
  233. return errors.Wrapf(err, "failed to get application for pool %q", poolName)
  234. }
  235. if currentAppName == appName {
  236. logger.Infof("application %q is already set on pool %q", appName, poolName)
  237. return nil
  238. }
  239. args := []string{"osd", "pool", "application", "enable", poolName, appName, confirmFlag}
  240. _, err = NewCephCommand(context, clusterInfo, args).Run()
  241. if err != nil {
  242. return errors.Wrapf(err, "failed to enable application %q on pool %q", appName, poolName)
  243. }
  244. return nil
  245. }
  246. func setCommonPoolProperties(context *clusterd.Context, clusterInfo *ClusterInfo, pool cephv1.NamedPoolSpec, appName string) error {
  247. if len(pool.Parameters) == 0 {
  248. pool.Parameters = make(map[string]string)
  249. }
  250. if pool.Replicated.IsTargetRatioEnabled() {
  251. pool.Parameters[targetSizeRatioProperty] = strconv.FormatFloat(pool.Replicated.TargetSizeRatio, 'f', -1, 32)
  252. }
  253. if pool.IsCompressionEnabled() {
  254. pool.Parameters[CompressionModeProperty] = pool.CompressionMode
  255. }
  256. // Apply properties
  257. for propName, propValue := range pool.Parameters {
  258. err := SetPoolProperty(context, clusterInfo, pool.Name, propName, propValue)
  259. if err != nil {
  260. logger.Errorf("failed to set property %q to pool %q to %q. %v", propName, pool.Name, propValue, err)
  261. }
  262. }
  263. // ensure that the newly created pool gets an application tag
  264. if appName != "" {
  265. err := givePoolAppTag(context, clusterInfo, pool.Name, appName)
  266. if err != nil {
  267. return errors.Wrapf(err, "failed to tag pool %q for application %q", pool.Name, appName)
  268. }
  269. }
  270. // If the pool is mirrored, let's enable mirroring
  271. // we don't need to check if the pool is erasure coded or not, mirroring will still work, it will simply be slow
  272. if pool.Mirroring.Enabled {
  273. err := enablePoolMirroring(context, clusterInfo, pool)
  274. if err != nil {
  275. return errors.Wrapf(err, "failed to enable mirroring for pool %q", pool.Name)
  276. }
  277. // Schedule snapshots
  278. if pool.Mirroring.SnapshotSchedulesEnabled() {
  279. err = enableSnapshotSchedules(context, clusterInfo, pool)
  280. if err != nil {
  281. return errors.Wrapf(err, "failed to enable snapshot scheduling for pool %q", pool.Name)
  282. }
  283. }
  284. } else {
  285. if pool.Mirroring.Mode == "pool" {
  286. // Remove storage cluster peers
  287. mirrorInfo, err := GetPoolMirroringInfo(context, clusterInfo, pool.Name)
  288. if err != nil {
  289. return errors.Wrapf(err, "failed to get mirroring info for the pool %q", pool.Name)
  290. }
  291. for _, peer := range mirrorInfo.Peers {
  292. if peer.UUID != "" {
  293. err := removeClusterPeer(context, clusterInfo, pool.Name, peer.UUID)
  294. if err != nil {
  295. return errors.Wrapf(err, "failed to remove cluster peer with UUID %q for the pool %q", peer.UUID, pool.Name)
  296. }
  297. }
  298. }
  299. // Disable mirroring
  300. err = disablePoolMirroring(context, clusterInfo, pool.Name)
  301. if err != nil {
  302. return errors.Wrapf(err, "failed to disable mirroring for pool %q", pool.Name)
  303. }
  304. } else if pool.Mirroring.Mode == "image" {
  305. logger.Warningf("manually disable mirroring on images in the pool %q", pool.Name)
  306. }
  307. }
  308. // set maxSize quota
  309. if pool.Quotas.MaxSize != nil {
  310. // check for format errors
  311. maxBytesQuota, err := resource.ParseQuantity(*pool.Quotas.MaxSize)
  312. if err != nil {
  313. if err == resource.ErrFormatWrong {
  314. return errors.Wrapf(err, "maxSize quota incorrectly formatted for pool %q, valid units include k, M, G, T, P, E, Ki, Mi, Gi, Ti, Pi, Ei", pool.Name)
  315. }
  316. return errors.Wrapf(err, "failed setting quota for pool %q, maxSize quota parse error", pool.Name)
  317. }
  318. // set max_bytes quota, 0 value disables quota
  319. err = setPoolQuota(context, clusterInfo, pool.Name, "max_bytes", strconv.FormatInt(maxBytesQuota.Value(), 10))
  320. if err != nil {
  321. return errors.Wrapf(err, "failed to set max_bytes quota for pool %q", pool.Name)
  322. }
  323. } else if pool.Quotas.MaxBytes != nil {
  324. // set max_bytes quota, 0 value disables quota
  325. err := setPoolQuota(context, clusterInfo, pool.Name, "max_bytes", strconv.FormatUint(*pool.Quotas.MaxBytes, 10))
  326. if err != nil {
  327. return errors.Wrapf(err, "failed to set max_bytes quota for pool %q", pool.Name)
  328. }
  329. }
  330. // set max_objects quota
  331. if pool.Quotas.MaxObjects != nil {
  332. // set max_objects quota, 0 value disables quota
  333. err := setPoolQuota(context, clusterInfo, pool.Name, "max_objects", strconv.FormatUint(*pool.Quotas.MaxObjects, 10))
  334. if err != nil {
  335. return errors.Wrapf(err, "failed to set max_objects quota for pool %q", pool.Name)
  336. }
  337. }
  338. return nil
  339. }
  340. func GetErasureCodeProfileForPool(baseName string) string {
  341. return fmt.Sprintf("%s_ecprofile", baseName)
  342. }
  343. func createECPoolForApp(context *clusterd.Context, clusterInfo *ClusterInfo, ecProfileName string, pool cephv1.NamedPoolSpec, pgCount, appName string, enableECOverwrite bool) error {
  344. args := []string{"osd", "pool", "create", pool.Name, pgCount, "erasure", ecProfileName}
  345. output, err := NewCephCommand(context, clusterInfo, args).Run()
  346. if err != nil {
  347. return errors.Wrapf(err, "failed to create EC pool %s. %s", pool.Name, string(output))
  348. }
  349. if enableECOverwrite {
  350. if err = SetPoolProperty(context, clusterInfo, pool.Name, "allow_ec_overwrites", "true"); err != nil {
  351. return errors.Wrapf(err, "failed to allow EC overwrite for pool %s", pool.Name)
  352. }
  353. }
  354. if err = setCommonPoolProperties(context, clusterInfo, pool, appName); err != nil {
  355. return err
  356. }
  357. logger.Infof("creating EC pool %s succeeded", pool.Name)
  358. return nil
  359. }
  360. func createReplicatedPoolForApp(context *clusterd.Context, clusterInfo *ClusterInfo, clusterSpec *cephv1.ClusterSpec, pool cephv1.NamedPoolSpec, pgCount, appName string) error {
  361. // If it's a replicated pool, ensure the failure domain is desired
  362. checkFailureDomain := false
  363. // The crush rule name is the same as the pool unless we have a stretch cluster.
  364. crushRuleName := pool.Name
  365. if clusterSpec.IsStretchCluster() {
  366. // A stretch cluster enforces using the same crush rule for all pools.
  367. // The stretch cluster rule is created initially by the operator when the stretch cluster is configured
  368. // so there is no need to create a new crush rule for the pools here.
  369. crushRuleName = defaultStretchCrushRuleName
  370. } else if pool.IsHybridStoragePool() {
  371. // Create hybrid crush rule
  372. err := createHybridCrushRule(context, clusterInfo, clusterSpec, crushRuleName, pool.PoolSpec)
  373. if err != nil {
  374. return errors.Wrapf(err, "failed to create hybrid crush rule %q", crushRuleName)
  375. }
  376. } else {
  377. if pool.Replicated.ReplicasPerFailureDomain > 1 {
  378. // Create a two-step CRUSH rule for pools other than stretch clusters
  379. err := createStretchCrushRule(context, clusterInfo, clusterSpec, crushRuleName, pool.PoolSpec)
  380. if err != nil {
  381. return errors.Wrapf(err, "failed to create two-step crush rule %q", crushRuleName)
  382. }
  383. } else {
  384. // create a crush rule for a replicated pool, if a failure domain is specified
  385. checkFailureDomain = true
  386. if err := createReplicationCrushRule(context, clusterInfo, clusterSpec, crushRuleName, pool); err != nil {
  387. return errors.Wrapf(err, "failed to create replicated crush rule %q", crushRuleName)
  388. }
  389. }
  390. }
  391. poolDetails, err := GetPoolDetails(context, clusterInfo, pool.Name)
  392. if err != nil {
  393. // Create the pool since it doesn't exist yet
  394. // If there was some error other than ENOENT (not exists), go ahead and ensure the pool is created anyway
  395. args := []string{"osd", "pool", "create", pool.Name, pgCount, "replicated", crushRuleName, "--size", strconv.FormatUint(uint64(pool.Replicated.Size), 10)}
  396. if strings.HasPrefix(pool.Name, ".") && clusterInfo.CephVersion.IsAtLeastReef() {
  397. args = append(args, "--yes-i-really-mean-it")
  398. }
  399. output, err := NewCephCommand(context, clusterInfo, args).Run()
  400. if err != nil {
  401. return errors.Wrapf(err, "failed to create replicated pool %s. %s", pool.Name, string(output))
  402. }
  403. } else {
  404. // If the pool is type replicated, set the size for the pool if it changed
  405. if !clusterSpec.IsStretchCluster() && pool.IsReplicated() && poolDetails.Size != pool.Replicated.Size {
  406. logger.Infof("pool size is changed from %d to %d", poolDetails.Size, pool.Replicated.Size)
  407. if err := SetPoolReplicatedSizeProperty(context, clusterInfo, pool.Name, strconv.FormatUint(uint64(pool.Replicated.Size), 10)); err != nil {
  408. return errors.Wrapf(err, "failed to set size property to replicated pool %q to %d", pool.Name, pool.Replicated.Size)
  409. }
  410. }
  411. }
  412. // update the common pool properties
  413. if err := setCommonPoolProperties(context, clusterInfo, pool, appName); err != nil {
  414. return err
  415. }
  416. logger.Infof("reconciling replicated pool %s succeeded", pool.Name)
  417. if checkFailureDomain || pool.PoolSpec.DeviceClass != "" {
  418. if err = updatePoolCrushRule(context, clusterInfo, clusterSpec, pool); err != nil {
  419. return nil
  420. }
  421. }
  422. return nil
  423. }
  424. func updatePoolCrushRule(context *clusterd.Context, clusterInfo *ClusterInfo, clusterSpec *cephv1.ClusterSpec, pool cephv1.NamedPoolSpec) error {
  425. if pool.FailureDomain == "" && pool.DeviceClass == "" {
  426. logger.Debugf("skipping check for failure domain and deviceClass on pool %q as it is not specified", pool.Name)
  427. return nil
  428. }
  429. logger.Debugf("checking that pool %q has the failure domain %q and deviceClass %q", pool.Name, pool.FailureDomain, pool.DeviceClass)
  430. details, err := GetPoolDetails(context, clusterInfo, pool.Name)
  431. if err != nil {
  432. return errors.Wrapf(err, "failed to get pool %q details", pool.Name)
  433. }
  434. // Find the failure domain for the current crush rule
  435. rule, err := getCrushRule(context, clusterInfo, details.CrushRule)
  436. if err != nil {
  437. return errors.Wrapf(err, "failed to get crush rule %q", details.CrushRule)
  438. }
  439. currentFailureDomain, currentDeviceClass := extractPoolDetails(rule)
  440. if currentFailureDomain == pool.FailureDomain && currentDeviceClass == pool.DeviceClass {
  441. logger.Infof("pool %q has the expected failure domain %q and deviceClass %q", pool.Name, pool.FailureDomain, pool.DeviceClass)
  442. return nil
  443. }
  444. if currentFailureDomain != pool.FailureDomain {
  445. logger.Infof("creating a new crush rule for changed failure domain on crush rule %q", details.CrushRule)
  446. }
  447. if currentDeviceClass != pool.DeviceClass {
  448. logger.Infof("creating a new crush rule for changed deviceClass on crush rule %q", details.CrushRule)
  449. }
  450. // Use a crush rule name that is unique to the desired failure domain
  451. crushRuleName := fmt.Sprintf("%s_%s", pool.Name, pool.FailureDomain)
  452. if pool.DeviceClass != "" {
  453. crushRuleName = fmt.Sprintf("%s_%s_%s", pool.Name, pool.FailureDomain, pool.DeviceClass)
  454. }
  455. logger.Infof("updating pool %q failure domain from %q to %q with new crush rule %q", pool.Name, currentFailureDomain, pool.FailureDomain, crushRuleName)
  456. logger.Infof("crush rule %q will no longer be used by pool %q", details.CrushRule, pool.Name)
  457. // Create a new crush rule for the expected failure domain
  458. if err := createReplicationCrushRule(context, clusterInfo, clusterSpec, crushRuleName, pool); err != nil {
  459. return errors.Wrapf(err, "failed to create replicated crush rule %q", crushRuleName)
  460. }
  461. // Update the crush rule on the pool
  462. if err := setCrushRule(context, clusterInfo, pool.Name, crushRuleName); err != nil {
  463. return errors.Wrapf(err, "failed to set crush rule on pool %q", pool.Name)
  464. }
  465. logger.Infof("Successfully updated pool %q failure domain to %q", pool.Name, pool.FailureDomain)
  466. return nil
  467. }
  468. func extractPoolDetails(rule ruleSpec) (string, string) {
  469. // find the failure domain in the crush rule, which is the first step where the
  470. // "type" property is set
  471. var failureDomain, deviceClass string
  472. for i, step := range rule.Steps {
  473. if step.Type != "" {
  474. failureDomain = step.Type
  475. }
  476. if step.ItemName != "" {
  477. deviceClass = step.ItemName
  478. }
  479. // We expect the rule to be found by the second step, or else it is a more
  480. // complex rule that would not be supported for updating the failure domain
  481. if i == 1 {
  482. break
  483. }
  484. }
  485. return failureDomain, deviceClass
  486. }
  487. func setCrushRule(context *clusterd.Context, clusterInfo *ClusterInfo, poolName, crushRule string) error {
  488. args := []string{"osd", "pool", "set", poolName, "crush_rule", crushRule}
  489. _, err := NewCephCommand(context, clusterInfo, args).Run()
  490. if err != nil {
  491. return errors.Wrapf(err, "failed to set crush rule %q", crushRule)
  492. }
  493. return nil
  494. }
  495. func createStretchCrushRule(context *clusterd.Context, clusterInfo *ClusterInfo, clusterSpec *cephv1.ClusterSpec, ruleName string, pool cephv1.PoolSpec) error {
  496. // set the crush root to the default if not already specified
  497. if pool.CrushRoot == "" {
  498. pool.CrushRoot = GetCrushRootFromSpec(clusterSpec)
  499. }
  500. // set the crush failure domain to the "host" if not already specified
  501. if pool.FailureDomain == "" {
  502. pool.FailureDomain = cephv1.DefaultFailureDomain
  503. }
  504. // set the crush failure sub domain to the "host" if not already specified
  505. if pool.Replicated.SubFailureDomain == "" {
  506. pool.Replicated.SubFailureDomain = cephv1.DefaultFailureDomain
  507. }
  508. if pool.FailureDomain == pool.Replicated.SubFailureDomain {
  509. return errors.Errorf("failure and subfailure domains cannot be identical, current is %q", pool.FailureDomain)
  510. }
  511. crushMap, err := getCurrentCrushMap(context, clusterInfo)
  512. if err != nil {
  513. return errors.Wrap(err, "failed to get current crush map")
  514. }
  515. if crushRuleExists(crushMap, ruleName) {
  516. logger.Debugf("CRUSH rule %q already exists", ruleName)
  517. return nil
  518. }
  519. // Build plain text rule
  520. ruleset := buildTwoStepPlainCrushRule(crushMap, ruleName, pool)
  521. return updateCrushMap(context, clusterInfo, ruleset)
  522. }
  523. func createHybridCrushRule(context *clusterd.Context, clusterInfo *ClusterInfo, clusterSpec *cephv1.ClusterSpec, ruleName string, pool cephv1.PoolSpec) error {
  524. // set the crush root to the default if not already specified
  525. if pool.CrushRoot == "" {
  526. pool.CrushRoot = GetCrushRootFromSpec(clusterSpec)
  527. }
  528. // set the crush failure domain to the "host" if not already specified
  529. if pool.FailureDomain == "" {
  530. pool.FailureDomain = cephv1.DefaultFailureDomain
  531. }
  532. crushMap, err := getCurrentCrushMap(context, clusterInfo)
  533. if err != nil {
  534. return errors.Wrap(err, "failed to get current crush map")
  535. }
  536. if crushRuleExists(crushMap, ruleName) {
  537. logger.Debugf("CRUSH rule %q already exists", ruleName)
  538. return nil
  539. }
  540. ruleset := buildTwoStepHybridCrushRule(crushMap, ruleName, pool)
  541. return updateCrushMap(context, clusterInfo, ruleset)
  542. }
  543. func updateCrushMap(context *clusterd.Context, clusterInfo *ClusterInfo, ruleset string) error {
  544. // Fetch the compiled crush map
  545. compiledCRUSHMapFilePath, err := GetCompiledCrushMap(context, clusterInfo)
  546. if err != nil {
  547. return errors.Wrap(err, "failed to get crush map")
  548. }
  549. defer func() {
  550. err := os.Remove(compiledCRUSHMapFilePath)
  551. if err != nil {
  552. logger.Errorf("failed to remove file %q. %v", compiledCRUSHMapFilePath, err)
  553. }
  554. }()
  555. // Decompile the plain text to CRUSH binary format
  556. err = decompileCRUSHMap(context, compiledCRUSHMapFilePath)
  557. if err != nil {
  558. return errors.Wrap(err, "failed to compile crush map")
  559. }
  560. decompiledCRUSHMapFilePath := buildDecompileCRUSHFileName(compiledCRUSHMapFilePath)
  561. defer func() {
  562. err := os.Remove(decompiledCRUSHMapFilePath)
  563. if err != nil {
  564. logger.Errorf("failed to remove file %q. %v", decompiledCRUSHMapFilePath, err)
  565. }
  566. }()
  567. // Append plain rule to the decompiled crush map
  568. f, err := os.OpenFile(filepath.Clean(decompiledCRUSHMapFilePath), os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0400)
  569. if err != nil {
  570. return errors.Wrapf(err, "failed to open decompiled crush map %q", decompiledCRUSHMapFilePath)
  571. }
  572. defer func() {
  573. err := f.Close()
  574. if err != nil {
  575. logger.Errorf("failed to close file %q. %v", f.Name(), err)
  576. }
  577. }()
  578. // Append the new crush rule into the crush map
  579. if _, err := f.WriteString(ruleset); err != nil {
  580. return errors.Wrapf(err, "failed to append replicated plain crush rule to decompiled crush map %q", decompiledCRUSHMapFilePath)
  581. }
  582. // Compile the plain text to CRUSH binary format
  583. err = compileCRUSHMap(context, decompiledCRUSHMapFilePath)
  584. if err != nil {
  585. return errors.Wrap(err, "failed to compile crush map")
  586. }
  587. defer func() {
  588. err := os.Remove(buildCompileCRUSHFileName(decompiledCRUSHMapFilePath))
  589. if err != nil {
  590. logger.Errorf("failed to remove file %q. %v", buildCompileCRUSHFileName(decompiledCRUSHMapFilePath), err)
  591. }
  592. }()
  593. // Inject the new CRUSH Map
  594. err = injectCRUSHMap(context, clusterInfo, buildCompileCRUSHFileName(decompiledCRUSHMapFilePath))
  595. if err != nil {
  596. return errors.Wrap(err, "failed to inject crush map")
  597. }
  598. return nil
  599. }
  600. func createReplicationCrushRule(context *clusterd.Context, clusterInfo *ClusterInfo, clusterSpec *cephv1.ClusterSpec, ruleName string, pool cephv1.NamedPoolSpec) error {
  601. failureDomain := pool.FailureDomain
  602. if failureDomain == "" {
  603. failureDomain = cephv1.DefaultFailureDomain
  604. }
  605. // set the crush root to the default if not already specified
  606. crushRoot := pool.CrushRoot
  607. if pool.CrushRoot == "" {
  608. crushRoot = GetCrushRootFromSpec(clusterSpec)
  609. }
  610. args := []string{"osd", "crush", "rule", "create-replicated", ruleName, crushRoot, failureDomain}
  611. var deviceClass string
  612. if pool.DeviceClass != "" {
  613. deviceClass = pool.DeviceClass
  614. args = append(args, deviceClass)
  615. }
  616. _, err := NewCephCommand(context, clusterInfo, args).Run()
  617. if err != nil {
  618. return errors.Wrapf(err, "failed to create crush rule %s", ruleName)
  619. }
  620. return nil
  621. }
  622. // SetPoolProperty sets a property to a given pool
  623. func SetPoolProperty(context *clusterd.Context, clusterInfo *ClusterInfo, name, propName, propVal string) error {
  624. args := []string{"osd", "pool", "set", name, propName, propVal}
  625. logger.Infof("setting pool property %q to %q on pool %q", propName, propVal, name)
  626. _, err := NewCephCommand(context, clusterInfo, args).Run()
  627. if err != nil {
  628. return errors.Wrapf(err, "failed to set pool property %q on pool %q", propName, name)
  629. }
  630. return nil
  631. }
  632. // setPoolQuota sets quotas on a given pool
  633. func setPoolQuota(context *clusterd.Context, clusterInfo *ClusterInfo, poolName, quotaType, quotaVal string) error {
  634. args := []string{"osd", "pool", "set-quota", poolName, quotaType, quotaVal}
  635. logger.Infof("setting quota %q=%q on pool %q", quotaType, quotaVal, poolName)
  636. _, err := NewCephCommand(context, clusterInfo, args).Run()
  637. if err != nil {
  638. return errors.Wrapf(err, "failed to set %q quota on pool %q", quotaType, poolName)
  639. }
  640. return nil
  641. }
  642. // SetPoolReplicatedSizeProperty sets the replica size of a pool
  643. func SetPoolReplicatedSizeProperty(context *clusterd.Context, clusterInfo *ClusterInfo, poolName, size string) error {
  644. propName := "size"
  645. args := []string{"osd", "pool", "set", poolName, propName, size}
  646. if size == "1" {
  647. args = append(args, "--yes-i-really-mean-it")
  648. }
  649. _, err := NewCephCommand(context, clusterInfo, args).Run()
  650. if err != nil {
  651. return errors.Wrapf(err, "failed to set pool property %q on pool %q", propName, poolName)
  652. }
  653. return nil
  654. }
  655. func GetPoolStats(context *clusterd.Context, clusterInfo *ClusterInfo) (*CephStoragePoolStats, error) {
  656. args := []string{"df", "detail"}
  657. output, err := NewCephCommand(context, clusterInfo, args).Run()
  658. if err != nil {
  659. return nil, errors.Wrap(err, "failed to get pool stats")
  660. }
  661. var poolStats CephStoragePoolStats
  662. if err := json.Unmarshal(output, &poolStats); err != nil {
  663. return nil, errors.Wrap(err, "failed to unmarshal pool stats response")
  664. }
  665. return &poolStats, nil
  666. }
  667. func GetPoolStatistics(context *clusterd.Context, clusterInfo *ClusterInfo, name string) (*PoolStatistics, error) {
  668. args := []string{"pool", "stats", name}
  669. cmd := NewRBDCommand(context, clusterInfo, args)
  670. cmd.JsonOutput = true
  671. output, err := cmd.Run()
  672. if err != nil {
  673. return nil, errors.Wrap(err, "failed to get pool stats")
  674. }
  675. var poolStats PoolStatistics
  676. if err := json.Unmarshal(output, &poolStats); err != nil {
  677. return nil, errors.Wrap(err, "failed to unmarshal pool stats response")
  678. }
  679. return &poolStats, nil
  680. }
  681. func crushRuleExists(crushMap CrushMap, ruleName string) bool {
  682. // Check if the crush rule already exists
  683. for _, rule := range crushMap.Rules {
  684. if rule.Name == ruleName {
  685. return true
  686. }
  687. }
  688. return false
  689. }
  690. func getCurrentCrushMap(context *clusterd.Context, clusterInfo *ClusterInfo) (CrushMap, error) {
  691. crushMap, err := GetCrushMap(context, clusterInfo)
  692. if err != nil {
  693. return CrushMap{}, errors.Wrap(err, "failed to get crush map")
  694. }
  695. return crushMap, nil
  696. }