2016-11-05 17:09:05 -07:00
|
|
|
/*
|
2018-03-16 08:31:34 -07:00
|
|
|
Copyright 2018 The Rook Authors. All rights reserved.
|
2016-11-05 17:09:05 -07:00
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
*/
|
2016-09-30 12:56:56 -07:00
|
|
|
package client
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"encoding/json"
|
2020-12-09 12:18:03 -07:00
|
|
|
"strings"
|
2020-10-23 11:17:18 -06:00
|
|
|
"syscall"
|
2017-06-02 13:44:54 -07:00
|
|
|
|
2019-10-08 12:51:36 +02:00
|
|
|
"github.com/pkg/errors"
|
2020-10-23 11:17:18 -06:00
|
|
|
cephv1 "github.com/rook/rook/pkg/apis/ceph.rook.io/v1"
|
2017-06-02 13:44:54 -07:00
|
|
|
"github.com/rook/rook/pkg/clusterd"
|
2020-10-23 11:17:18 -06:00
|
|
|
"github.com/rook/rook/pkg/util/exec"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
const (
|
|
|
|
|
defaultStretchCrushRuleName = "default_stretch_cluster_rule"
|
2016-09-30 12:56:56 -07:00
|
|
|
)
|
|
|
|
|
|
2019-10-24 11:27:52 +02:00
|
|
|
// MonStatusResponse represents the response from a quorum_status mon_command (subset of all available fields, only
|
2016-09-30 12:56:56 -07:00
|
|
|
// marshal ones we care about)
|
|
|
|
|
type MonStatusResponse struct {
|
|
|
|
|
Quorum []int `json:"quorum"`
|
|
|
|
|
MonMap struct {
|
|
|
|
|
Mons []MonMapEntry `json:"mons"`
|
|
|
|
|
} `json:"monmap"`
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-23 22:34:19 +02:00
|
|
|
// MonMapEntry represents an entry in the monitor map
|
2016-09-30 12:56:56 -07:00
|
|
|
type MonMapEntry struct {
|
2019-07-23 22:34:19 +02:00
|
|
|
Name string `json:"name"`
|
|
|
|
|
Rank int `json:"rank"`
|
|
|
|
|
Address string `json:"addr"`
|
2019-10-02 10:45:21 +02:00
|
|
|
PublicAddr string `json:"public_addr"`
|
2019-07-23 22:34:19 +02:00
|
|
|
PublicAddrs struct {
|
|
|
|
|
Addrvec []AddrvecEntry `json:"addrvec"`
|
|
|
|
|
} `json:"public_addrs"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// AddrvecEntry represents an entry type for a given messenger version
|
|
|
|
|
type AddrvecEntry struct {
|
|
|
|
|
Type string `json:"type"`
|
|
|
|
|
Addr string `json:"addr"`
|
|
|
|
|
Nonce int `json:"nonce"`
|
2016-09-30 12:56:56 -07:00
|
|
|
}
|
|
|
|
|
|
2020-12-09 12:18:03 -07:00
|
|
|
// MonDump represents the response from a mon dump
|
|
|
|
|
type MonDump struct {
|
|
|
|
|
StretchMode bool `json:"stretch_mode"`
|
|
|
|
|
ElectionStrategy int `json:"election_strategy"`
|
|
|
|
|
FSID string `json:"fsid"`
|
|
|
|
|
Mons []MonDumpEntry `json:"mons"`
|
|
|
|
|
Quorum []int `json:"quorum"`
|
2021-11-30 17:50:47 -07:00
|
|
|
TiebreakerMon string `json:"tiebreaker_mon"`
|
2020-12-09 12:18:03 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type MonDumpEntry struct {
|
|
|
|
|
Name string `json:"name"`
|
|
|
|
|
Rank int `json:"rank"`
|
|
|
|
|
CrushLocation string `json:"crush_location"`
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-24 11:27:52 +02:00
|
|
|
// GetMonQuorumStatus calls quorum_status mon_command
|
2020-07-20 16:26:20 -06:00
|
|
|
func GetMonQuorumStatus(context *clusterd.Context, clusterInfo *ClusterInfo) (MonStatusResponse, error) {
|
2019-10-24 11:27:52 +02:00
|
|
|
args := []string{"quorum_status"}
|
2020-07-10 16:40:16 -06:00
|
|
|
cmd := NewCephCommand(context, clusterInfo, args)
|
2019-05-01 11:43:17 -07:00
|
|
|
buf, err := cmd.Run()
|
2017-06-02 13:44:54 -07:00
|
|
|
if err != nil {
|
2020-04-29 15:59:41 +05:30
|
|
|
return MonStatusResponse{}, errors.Wrap(err, "mon quorum status failed")
|
2017-06-02 13:44:54 -07:00
|
|
|
}
|
2016-09-30 12:56:56 -07:00
|
|
|
|
|
|
|
|
var resp MonStatusResponse
|
|
|
|
|
err = json.Unmarshal(buf, &resp)
|
|
|
|
|
if err != nil {
|
2019-10-08 12:51:36 +02:00
|
|
|
return MonStatusResponse{}, errors.Wrapf(err, "unmarshal failed. raw buffer response: %s", buf)
|
2016-09-30 12:56:56 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return resp, nil
|
|
|
|
|
}
|
2020-10-23 11:17:18 -06:00
|
|
|
|
2020-12-09 12:18:03 -07:00
|
|
|
// GetMonDump calls mon dump command
|
|
|
|
|
func GetMonDump(context *clusterd.Context, clusterInfo *ClusterInfo) (MonDump, error) {
|
|
|
|
|
args := []string{"mon", "dump"}
|
|
|
|
|
cmd := NewCephCommand(context, clusterInfo, args)
|
|
|
|
|
buf, err := cmd.Run()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return MonDump{}, errors.Wrap(err, "mon dump failed")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var response MonDump
|
|
|
|
|
err = json.Unmarshal(buf, &response)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return MonDump{}, errors.Wrapf(err, "unmarshal failed. raw buffer response: %s", buf)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return response, nil
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-23 11:17:18 -06:00
|
|
|
// EnableStretchElectionStrategy enables the mon connectivity algorithm for stretch clusters
|
|
|
|
|
func EnableStretchElectionStrategy(context *clusterd.Context, clusterInfo *ClusterInfo) error {
|
|
|
|
|
args := []string{"mon", "set", "election_strategy", "connectivity"}
|
|
|
|
|
buf, err := NewCephCommand(context, clusterInfo, args).Run()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return errors.Wrap(err, "failed to enable stretch cluster election strategy")
|
|
|
|
|
}
|
|
|
|
|
logger.Infof("successfully enabled stretch cluster election strategy. %s", string(buf))
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// CreateDefaultStretchCrushRule creates the default CRUSH rule for the stretch cluster
|
2020-10-26 19:16:18 -06:00
|
|
|
func CreateDefaultStretchCrushRule(context *clusterd.Context, clusterInfo *ClusterInfo, clusterSpec *cephv1.ClusterSpec, failureDomain string) error {
|
2020-10-23 11:17:18 -06:00
|
|
|
pool := cephv1.PoolSpec{
|
|
|
|
|
FailureDomain: failureDomain,
|
2020-10-26 19:16:18 -06:00
|
|
|
Replicated: cephv1.ReplicatedSpec{SubFailureDomain: clusterSpec.Mon.StretchCluster.SubFailureDomain},
|
2020-10-23 11:17:18 -06:00
|
|
|
}
|
2021-06-17 15:16:46 +05:30
|
|
|
if err := createStretchCrushRule(context, clusterInfo, clusterSpec, defaultStretchCrushRuleName, pool); err != nil {
|
2020-10-23 11:17:18 -06:00
|
|
|
return errors.Wrap(err, "failed to create default stretch crush rule")
|
|
|
|
|
}
|
|
|
|
|
logger.Info("successfully created the default stretch crush rule")
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// SetMonStretchTiebreaker sets the tiebreaker mon in the stretch cluster
|
|
|
|
|
func SetMonStretchTiebreaker(context *clusterd.Context, clusterInfo *ClusterInfo, monName, bucketType string) error {
|
|
|
|
|
logger.Infof("enabling stretch mode with mon arbiter %q with crush rule %q in failure domain %q", monName, defaultStretchCrushRuleName, bucketType)
|
|
|
|
|
args := []string{"mon", "enable_stretch_mode", monName, defaultStretchCrushRuleName, bucketType}
|
|
|
|
|
buf, err := NewCephCommand(context, clusterInfo, args).Run()
|
|
|
|
|
if err != nil {
|
|
|
|
|
if code, ok := exec.ExitStatus(err); ok && code == int(syscall.EINVAL) {
|
2020-12-09 12:18:03 -07:00
|
|
|
// TODO: Get a more distinctive error from ceph so we don't have to compare the error message
|
|
|
|
|
if strings.Contains(string(buf), "stretch mode is already engaged") {
|
2021-11-30 17:50:47 -07:00
|
|
|
logger.Info("stretch mode is already enabled")
|
2020-12-09 12:18:03 -07:00
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
return errors.Wrapf(err, "stretch mode failed to be enabled. %s", string(buf))
|
2020-10-23 11:17:18 -06:00
|
|
|
}
|
|
|
|
|
return errors.Wrap(err, "failed to set mon stretch zone")
|
|
|
|
|
}
|
|
|
|
|
logger.Debug(string(buf))
|
|
|
|
|
logger.Infof("successfully set mon tiebreaker %q in failure domain %q", monName, bucketType)
|
|
|
|
|
return nil
|
|
|
|
|
}
|
2021-10-14 19:13:36 -06:00
|
|
|
|
|
|
|
|
// SetNewTiebreaker sets the new tiebreaker mon in the stretch cluster during a failover
|
|
|
|
|
func SetNewTiebreaker(context *clusterd.Context, clusterInfo *ClusterInfo, monName string) error {
|
|
|
|
|
logger.Infof("setting new mon tiebreaker %q in arbiter zone", monName)
|
|
|
|
|
args := []string{"mon", "set_new_tiebreaker", monName}
|
|
|
|
|
if _, err := NewCephCommand(context, clusterInfo, args).Run(); err != nil {
|
|
|
|
|
return errors.Wrapf(err, "failed to set new mon tiebreaker %q", monName)
|
|
|
|
|
}
|
|
|
|
|
logger.Infof("successfully set new mon tiebreaker %q in arbiter zone", monName)
|
|
|
|
|
return nil
|
|
|
|
|
}
|