/
cluster.go
210 lines (196 loc) · 6.26 KB
/
cluster.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
// Copyright 2018 The Chubao Authors.
//
// 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 cmd
import (
"fmt"
"strconv"
"github.com/chubaofs/chubaofs/proto"
"github.com/chubaofs/chubaofs/sdk/master"
"github.com/spf13/cobra"
)
const (
cmdClusterUse = "cluster [COMMAND]"
cmdClusterShort = "Manage cluster components"
)
func (cmd *ChubaoFSCmd) newClusterCmd(client *master.MasterClient) *cobra.Command {
var clusterCmd = &cobra.Command{
Use: cmdClusterUse,
Short: cmdClusterShort,
}
clusterCmd.AddCommand(
newClusterInfoCmd(client),
newClusterStatCmd(client),
newClusterFreezeCmd(client),
newClusterSetThresholdCmd(client),
newClusterDeleteParasCmd(client),
)
return clusterCmd
}
const (
cmdClusterInfoShort = "Show cluster summary information"
cmdClusterStatShort = "Show cluster status information"
cmdClusterFreezeShort = "Freeze cluster"
cmdClusterThresholdShort = "Set memory threshold of metanodes"
cmdClusterDelParaShort = "Set delete parameters"
nodeDeleteBatchCountKey = "batchCount"
nodeMarkDeleteRateKey = "markDeleteRate"
nodeDeleteWorkerSleepMs = "deleteWorkerSleepMs"
nodeAutoRepairRateKey = "autoRepairRate"
)
func newClusterInfoCmd(client *master.MasterClient) *cobra.Command {
var cmd = &cobra.Command{
Use: CliOpInfo,
Short: cmdClusterInfoShort,
Run: func(cmd *cobra.Command, args []string) {
var err error
var cv *proto.ClusterView
var delPara map[string]string
if cv, err = client.AdminAPI().GetCluster(); err != nil {
errout("Error: %v", err)
}
stdout("[Cluster]\n")
stdout(formatClusterView(cv))
if delPara, err = client.AdminAPI().GetDeleteParas(); err != nil {
errout("Error: %v", err)
}
stdout(fmt.Sprintf(" BatchCount : %v\n", delPara[nodeDeleteBatchCountKey]))
stdout(fmt.Sprintf(" MarkDeleteRate : %v\n", delPara[nodeMarkDeleteRateKey]))
stdout(fmt.Sprintf(" DeleteWorkerSleepMs: %v\n", delPara[nodeDeleteWorkerSleepMs]))
stdout(fmt.Sprintf(" AutoRepairRate : %v\n", delPara[nodeAutoRepairRateKey]))
stdout("\n")
},
}
return cmd
}
func newClusterStatCmd(client *master.MasterClient) *cobra.Command {
var cmd = &cobra.Command{
Use: CliOpStatus,
Short: cmdClusterStatShort,
Run: func(cmd *cobra.Command, args []string) {
var (
err error
cs *proto.ClusterStatInfo
)
defer func() {
if err != nil {
errout("Error: %v", err)
}
}()
if cs, err = client.AdminAPI().GetClusterStat(); err != nil {
err = fmt.Errorf("Get cluster info fail:\n%v\n", err)
return
}
stdout("[Cluster Status]\n")
stdout(formatClusterStat(cs))
stdout("\n")
},
}
return cmd
}
func newClusterFreezeCmd(client *master.MasterClient) *cobra.Command {
var cmd = &cobra.Command{
Use: CliOpFreeze + " [ENABLE]",
ValidArgs: []string{"true", "false"},
Short: cmdClusterFreezeShort,
Args: cobra.MinimumNArgs(1),
Long: `Turn on or off the automatic allocation of the data partitions.
If 'freeze=false', ChubaoFS WILL automatically allocate new data partitions for the volume when:
1. the used space is below the max capacity,
2. and the number of r&w data partition is less than 20.
If 'freeze=true', ChubaoFS WILL NOT automatically allocate new data partitions `,
Run: func(cmd *cobra.Command, args []string) {
var (
err error
enable bool
)
defer func() {
if err != nil {
errout("Error: %v", err)
}
}()
if enable, err = strconv.ParseBool(args[0]); err != nil {
err = fmt.Errorf("Parse bool fail: %v\n", err)
return
}
if err = client.AdminAPI().IsFreezeCluster(enable); err != nil {
return
}
if enable {
stdout("Freeze cluster successful!\n")
} else {
stdout("Unfreeze cluster successful!\n")
}
},
}
return cmd
}
func newClusterSetThresholdCmd(client *master.MasterClient) *cobra.Command {
var cmd = &cobra.Command{
Use: CliOpSetThreshold + " [THRESHOLD]",
Short: cmdClusterThresholdShort,
Args: cobra.MinimumNArgs(1),
Long: `Set the threshold of memory on each meta node.
If the memory usage reaches this threshold, all the mata partition will be readOnly.`,
Run: func(cmd *cobra.Command, args []string) {
var (
err error
threshold float64
)
defer func() {
if err != nil {
errout("Error: %v", err)
}
}()
if threshold, err = strconv.ParseFloat(args[0], 64); err != nil {
err = fmt.Errorf("Parse Float fail: %v\n", err)
return
}
if threshold > 1.0 {
err = fmt.Errorf("Threshold too big\n")
return
}
if err = client.AdminAPI().SetMetaNodeThreshold(threshold); err != nil {
return
}
stdout("MetaNode threshold is set to %v!\n", threshold)
},
}
return cmd
}
func newClusterDeleteParasCmd(client *master.MasterClient) *cobra.Command {
var optAutoRepairRate, optMarkDeleteRate, optDelBatchCount, optDelWorkerSleepMs string
var cmd = &cobra.Command{
Use: CliOpSetDelRate,
Short: cmdClusterDelParaShort,
Run: func(cmd *cobra.Command, args []string) {
var (
err error
)
defer func() {
if err != nil {
errout("Error: %v", err)
}
}()
if err = client.AdminAPI().SetDeleteParas(optDelBatchCount, optMarkDeleteRate, optDelWorkerSleepMs, optAutoRepairRate); err != nil {
return
}
stdout("Delete parameters has been set successfully. \n")
},
}
cmd.Flags().StringVar(&optAutoRepairRate, CliFlagAutoRepairRate, "", "DataNode auto repair rate")
cmd.Flags().StringVar(&optDelBatchCount, CliFlagDelBatchCount, "", "MetaNode delete batch count")
cmd.Flags().StringVar(&optDelWorkerSleepMs, CliFlagDelWorkerSleepMs, "", "MetaNode delete worker sleep time with millisecond. if 0 for no sleep")
cmd.Flags().StringVar(&optMarkDeleteRate, CliFlagMarkDelRate, "", "DataNode batch mark delete limit rate. if 0 for no infinity limit")
return cmd
}