crush.go 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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. "strconv"
  19. "strings"
  20. "github.com/pkg/errors"
  21. cephv1 "github.com/rook/rook/pkg/apis/ceph.rook.io/v1"
  22. "github.com/rook/rook/pkg/clusterd"
  23. )
  24. const (
  25. CrushRootConfigKey = "crushRoot"
  26. )
  27. // CrushMap is the go representation of a CRUSH map
  28. type CrushMap struct {
  29. Devices []struct {
  30. ID int `json:"id"`
  31. Name string `json:"name"`
  32. Class string `json:"class"`
  33. } `json:"devices"`
  34. Types []struct {
  35. ID int `json:"type_id"`
  36. Name string `json:"name"`
  37. } `json:"types"`
  38. Buckets []struct {
  39. ID int `json:"id"`
  40. Name string `json:"name"`
  41. TypeID int `json:"type_id"`
  42. TypeName string `json:"type_name"`
  43. Weight int `json:"weight"`
  44. Alg string `json:"alg"`
  45. Hash string `json:"hash"`
  46. Items []struct {
  47. ID int `json:"id"`
  48. Weight int `json:"weight"`
  49. Pos int `json:"pos"`
  50. } `json:"items"`
  51. } `json:"buckets"`
  52. Rules []ruleSpec `json:"rules"`
  53. Tunables struct {
  54. // Add if necessary
  55. } `json:"tunables"`
  56. }
  57. type ruleSpec struct {
  58. ID int `json:"rule_id"`
  59. Name string `json:"rule_name"`
  60. Ruleset int `json:"ruleset"`
  61. Type int `json:"type"`
  62. MinSize int `json:"min_size"`
  63. MaxSize int `json:"max_size"`
  64. Steps []stepSpec `json:"steps"`
  65. }
  66. type stepSpec struct {
  67. Operation string `json:"op"`
  68. Number uint `json:"num"`
  69. Item int `json:"item"`
  70. ItemName string `json:"item_name"`
  71. Type string `json:"type"`
  72. }
  73. // CrushFindResult is go representation of the Ceph osd find command output
  74. type CrushFindResult struct {
  75. ID int `json:"osd"`
  76. IP string `json:"ip"`
  77. Host string `json:"host,omitempty"`
  78. Location map[string]string `json:"crush_location"`
  79. }
  80. // GetCrushMap fetches the Ceph CRUSH map
  81. func GetCrushMap(context *clusterd.Context, clusterInfo *ClusterInfo) (CrushMap, error) {
  82. var c CrushMap
  83. args := []string{"osd", "crush", "dump"}
  84. buf, err := NewCephCommand(context, clusterInfo, args).Run()
  85. if err != nil {
  86. return c, errors.Wrapf(err, "failed to get crush map. %s", string(buf))
  87. }
  88. err = json.Unmarshal(buf, &c)
  89. if err != nil {
  90. return c, errors.Wrap(err, "failed to unmarshal crush map")
  91. }
  92. return c, nil
  93. }
  94. // GetCompiledCrushMap fetches the Ceph compiled version of the CRUSH map
  95. func GetCompiledCrushMap(context *clusterd.Context, clusterInfo *ClusterInfo) (string, error) {
  96. compiledCrushMapFile, err := os.CreateTemp("", "")
  97. if err != nil {
  98. return "", errors.Wrap(err, "failed to generate temporarily file")
  99. }
  100. args := []string{"osd", "getcrushmap", "--out-file", compiledCrushMapFile.Name()}
  101. exec := NewCephCommand(context, clusterInfo, args)
  102. exec.JsonOutput = false
  103. buf, err := exec.Run()
  104. if err != nil {
  105. return "", errors.Wrapf(err, "failed to get compiled crush map. %s", string(buf))
  106. }
  107. return compiledCrushMapFile.Name(), nil
  108. }
  109. // FindOSDInCrushMap finds an OSD in the CRUSH map
  110. func FindOSDInCrushMap(context *clusterd.Context, clusterInfo *ClusterInfo, osdID int) (*CrushFindResult, error) {
  111. args := []string{"osd", "find", strconv.Itoa(osdID)}
  112. buf, err := NewCephCommand(context, clusterInfo, args).Run()
  113. if err != nil {
  114. return nil, errors.Wrapf(err, "failed to find osd.%d in crush map: %s", osdID, string(buf))
  115. }
  116. var result CrushFindResult
  117. if err := json.Unmarshal(buf, &result); err != nil {
  118. return nil, errors.Wrapf(err, "failed to unmarshal crush find result: %s", string(buf))
  119. }
  120. return &result, nil
  121. }
  122. // GetCrushHostName gets the hostname where an OSD is running on
  123. func GetCrushHostName(context *clusterd.Context, clusterInfo *ClusterInfo, osdID int) (string, error) {
  124. result, err := FindOSDInCrushMap(context, clusterInfo, osdID)
  125. if err != nil {
  126. return "", err
  127. }
  128. return result.Location["host"], nil
  129. }
  130. // NormalizeCrushName replaces . with -
  131. func NormalizeCrushName(name string) string {
  132. return strings.Replace(name, ".", "-", -1)
  133. }
  134. // Obtain the cluster-wide default crush root from the cluster spec
  135. func GetCrushRootFromSpec(c *cephv1.ClusterSpec) string {
  136. if c.Storage.Config == nil {
  137. return cephv1.DefaultCRUSHRoot
  138. }
  139. if v, ok := c.Storage.Config[CrushRootConfigKey]; ok {
  140. return v
  141. }
  142. return cephv1.DefaultCRUSHRoot
  143. }
  144. // IsNormalizedCrushNameEqual returns true if normalized is either equal to or the normalized version of notNormalized
  145. // a crush name is normalized if it comes from the crushmap or has passed through the NormalizeCrushName function.
  146. func IsNormalizedCrushNameEqual(notNormalized, normalized string) bool {
  147. if notNormalized == normalized || NormalizeCrushName(notNormalized) == normalized {
  148. return true
  149. }
  150. return false
  151. }
  152. // UpdateCrushMapValue is for updating the location in the crush map
  153. // this is not safe for incorrectly formatted strings
  154. func UpdateCrushMapValue(pairs *[]string, key, value string) {
  155. found := false
  156. property := formatProperty(key, value)
  157. for i, pair := range *pairs {
  158. entry := strings.Split(pair, "=")
  159. if key == entry[0] {
  160. (*pairs)[i] = property
  161. found = true
  162. }
  163. }
  164. if !found {
  165. *pairs = append(*pairs, property)
  166. }
  167. }
  168. func formatProperty(name, value string) string {
  169. return fmt.Sprintf("%s=%s", name, value)
  170. }
  171. // GetOSDOnHost returns the list of osds running on a given host
  172. func GetOSDOnHost(context *clusterd.Context, clusterInfo *ClusterInfo, node string) (string, error) {
  173. node = NormalizeCrushName(node)
  174. args := []string{"osd", "crush", "ls", node}
  175. buf, err := NewCephCommand(context, clusterInfo, args).Run()
  176. if err != nil {
  177. return "", errors.Wrapf(err, "failed to get osd list on host. %s", string(buf))
  178. }
  179. return string(buf), nil
  180. }
  181. func compileCRUSHMap(context *clusterd.Context, crushMapPath string) error {
  182. mapFile := buildCompileCRUSHFileName(crushMapPath)
  183. args := []string{"--compile", crushMapPath, "--outfn", mapFile}
  184. output, err := context.Executor.ExecuteCommandWithOutput("crushtool", args...)
  185. if err != nil {
  186. return errors.Wrapf(err, "failed to compile crush map %q. %s", mapFile, output)
  187. }
  188. return nil
  189. }
  190. func decompileCRUSHMap(context *clusterd.Context, crushMapPath string) error {
  191. mapFile := buildDecompileCRUSHFileName(crushMapPath)
  192. args := []string{"--decompile", crushMapPath, "--outfn", mapFile}
  193. output, err := context.Executor.ExecuteCommandWithOutput("crushtool", args...)
  194. if err != nil {
  195. return errors.Wrapf(err, "failed to decompile crush map %q. %s", mapFile, output)
  196. }
  197. return nil
  198. }
  199. func injectCRUSHMap(context *clusterd.Context, clusterInfo *ClusterInfo, crushMapPath string) error {
  200. args := []string{"osd", "setcrushmap", "--in-file", crushMapPath}
  201. exec := NewCephCommand(context, clusterInfo, args)
  202. exec.JsonOutput = false
  203. buf, err := exec.Run()
  204. if err != nil {
  205. return errors.Wrapf(err, "failed to inject crush map %q. %s", crushMapPath, string(buf))
  206. }
  207. return nil
  208. }
  209. func setCRUSHMap(context *clusterd.Context, clusterInfo *ClusterInfo, crushMapPath string) error {
  210. args := []string{"osd", "crush", "set", crushMapPath}
  211. exec := NewCephCommand(context, clusterInfo, args)
  212. exec.JsonOutput = false
  213. buf, err := exec.Run()
  214. if err != nil {
  215. return errors.Wrapf(err, "failed to set crush map %q. %s", crushMapPath, string(buf))
  216. }
  217. return nil
  218. }
  219. func buildDecompileCRUSHFileName(crushMapPath string) string {
  220. return fmt.Sprintf("%s.decompiled", crushMapPath)
  221. }
  222. func buildCompileCRUSHFileName(crushMapPath string) string {
  223. return fmt.Sprintf("%s.compiled", crushMapPath)
  224. }