mon.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*
  2. Copyright 2018 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. "strings"
  17. "syscall"
  18. "github.com/pkg/errors"
  19. cephv1 "github.com/rook/rook/pkg/apis/ceph.rook.io/v1"
  20. "github.com/rook/rook/pkg/clusterd"
  21. "github.com/rook/rook/pkg/util/exec"
  22. )
  23. const (
  24. defaultStretchCrushRuleName = "default_stretch_cluster_rule"
  25. )
  26. // MonStatusResponse represents the response from a quorum_status mon_command (subset of all available fields, only
  27. // marshal ones we care about)
  28. type MonStatusResponse struct {
  29. Quorum []int `json:"quorum"`
  30. MonMap struct {
  31. Mons []MonMapEntry `json:"mons"`
  32. } `json:"monmap"`
  33. }
  34. // MonMapEntry represents an entry in the monitor map
  35. type MonMapEntry struct {
  36. Name string `json:"name"`
  37. Rank int `json:"rank"`
  38. Address string `json:"addr"`
  39. PublicAddr string `json:"public_addr"`
  40. PublicAddrs struct {
  41. Addrvec []AddrvecEntry `json:"addrvec"`
  42. } `json:"public_addrs"`
  43. }
  44. // AddrvecEntry represents an entry type for a given messenger version
  45. type AddrvecEntry struct {
  46. Type string `json:"type"`
  47. Addr string `json:"addr"`
  48. Nonce int `json:"nonce"`
  49. }
  50. // MonDump represents the response from a mon dump
  51. type MonDump struct {
  52. StretchMode bool `json:"stretch_mode"`
  53. ElectionStrategy int `json:"election_strategy"`
  54. FSID string `json:"fsid"`
  55. Mons []MonDumpEntry `json:"mons"`
  56. Quorum []int `json:"quorum"`
  57. TiebreakerMon string `json:"tiebreaker_mon"`
  58. }
  59. type MonDumpEntry struct {
  60. Name string `json:"name"`
  61. Rank int `json:"rank"`
  62. CrushLocation string `json:"crush_location"`
  63. }
  64. // GetMonQuorumStatus calls quorum_status mon_command
  65. func GetMonQuorumStatus(context *clusterd.Context, clusterInfo *ClusterInfo) (MonStatusResponse, error) {
  66. args := []string{"quorum_status"}
  67. cmd := NewCephCommand(context, clusterInfo, args)
  68. buf, err := cmd.Run()
  69. if err != nil {
  70. return MonStatusResponse{}, errors.Wrap(err, "mon quorum status failed")
  71. }
  72. var resp MonStatusResponse
  73. err = json.Unmarshal(buf, &resp)
  74. if err != nil {
  75. return MonStatusResponse{}, errors.Wrapf(err, "unmarshal failed. raw buffer response: %s", buf)
  76. }
  77. return resp, nil
  78. }
  79. // GetMonDump calls mon dump command
  80. func GetMonDump(context *clusterd.Context, clusterInfo *ClusterInfo) (MonDump, error) {
  81. args := []string{"mon", "dump"}
  82. cmd := NewCephCommand(context, clusterInfo, args)
  83. buf, err := cmd.Run()
  84. if err != nil {
  85. return MonDump{}, errors.Wrap(err, "mon dump failed")
  86. }
  87. var response MonDump
  88. err = json.Unmarshal(buf, &response)
  89. if err != nil {
  90. return MonDump{}, errors.Wrapf(err, "unmarshal failed. raw buffer response: %s", buf)
  91. }
  92. return response, nil
  93. }
  94. // EnableStretchElectionStrategy enables the mon connectivity algorithm for stretch clusters
  95. func EnableStretchElectionStrategy(context *clusterd.Context, clusterInfo *ClusterInfo) error {
  96. args := []string{"mon", "set", "election_strategy", "connectivity"}
  97. buf, err := NewCephCommand(context, clusterInfo, args).Run()
  98. if err != nil {
  99. return errors.Wrap(err, "failed to enable stretch cluster election strategy")
  100. }
  101. logger.Infof("successfully enabled stretch cluster election strategy. %s", string(buf))
  102. return nil
  103. }
  104. // CreateDefaultStretchCrushRule creates the default CRUSH rule for the stretch cluster
  105. func CreateDefaultStretchCrushRule(context *clusterd.Context, clusterInfo *ClusterInfo, clusterSpec *cephv1.ClusterSpec, failureDomain string) error {
  106. pool := cephv1.PoolSpec{
  107. FailureDomain: failureDomain,
  108. Replicated: cephv1.ReplicatedSpec{SubFailureDomain: clusterSpec.Mon.StretchCluster.SubFailureDomain},
  109. }
  110. if err := createStretchCrushRule(context, clusterInfo, clusterSpec, defaultStretchCrushRuleName, pool); err != nil {
  111. return errors.Wrap(err, "failed to create default stretch crush rule")
  112. }
  113. logger.Info("successfully created the default stretch crush rule")
  114. return nil
  115. }
  116. // SetMonStretchTiebreaker sets the tiebreaker mon in the stretch cluster
  117. func SetMonStretchTiebreaker(context *clusterd.Context, clusterInfo *ClusterInfo, monName, bucketType string) error {
  118. logger.Infof("enabling stretch mode with mon arbiter %q with crush rule %q in failure domain %q", monName, defaultStretchCrushRuleName, bucketType)
  119. args := []string{"mon", "enable_stretch_mode", monName, defaultStretchCrushRuleName, bucketType}
  120. buf, err := NewCephCommand(context, clusterInfo, args).Run()
  121. if err != nil {
  122. if code, ok := exec.ExitStatus(err); ok && code == int(syscall.EINVAL) {
  123. // TODO: Get a more distinctive error from ceph so we don't have to compare the error message
  124. if strings.Contains(string(buf), "stretch mode is already engaged") {
  125. logger.Info("stretch mode is already enabled")
  126. return nil
  127. }
  128. return errors.Wrapf(err, "stretch mode failed to be enabled. %s", string(buf))
  129. }
  130. return errors.Wrap(err, "failed to set mon stretch zone")
  131. }
  132. logger.Debug(string(buf))
  133. logger.Infof("successfully set mon tiebreaker %q in failure domain %q", monName, bucketType)
  134. return nil
  135. }
  136. // SetNewTiebreaker sets the new tiebreaker mon in the stretch cluster during a failover
  137. func SetNewTiebreaker(context *clusterd.Context, clusterInfo *ClusterInfo, monName string) error {
  138. logger.Infof("setting new mon tiebreaker %q in arbiter zone", monName)
  139. args := []string{"mon", "set_new_tiebreaker", monName}
  140. if _, err := NewCephCommand(context, clusterInfo, args).Run(); err != nil {
  141. return errors.Wrapf(err, "failed to set new mon tiebreaker %q", monName)
  142. }
  143. logger.Infof("successfully set new mon tiebreaker %q in arbiter zone", monName)
  144. return nil
  145. }