cubefs

Форк
0
/
metanode.go 
194 строки · 5.8 Кб
1
// Copyright 2018 The CubeFS Authors.
2
//
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
//
7
//     http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12
// implied. See the License for the specific language governing
13
// permissions and limitations under the License.
14

15
package cmd
16

17
import (
18
	"sort"
19
	"strings"
20

21
	"github.com/cubefs/cubefs/proto"
22
	"github.com/cubefs/cubefs/sdk/master"
23
	"github.com/spf13/cobra"
24
)
25

26
const (
27
	cmdMetaNodeUse   = "metanode [COMMAND]"
28
	cmdMetaNodeShort = "Manage meta nodes"
29
	mpMigrateMax     = 15
30
)
31

32
func newMetaNodeCmd(client *master.MasterClient) *cobra.Command {
33
	cmd := &cobra.Command{
34
		Use:   cmdMetaNodeUse,
35
		Short: cmdMetaNodeShort,
36
	}
37
	cmd.AddCommand(
38
		newMetaNodeListCmd(client),
39
		newMetaNodeInfoCmd(client),
40
		newMetaNodeDecommissionCmd(client),
41
		newMetaNodeMigrateCmd(client),
42
	)
43
	return cmd
44
}
45

46
const (
47
	cmdMetaNodeListShort             = "List information of meta nodes"
48
	cmdMetaNodeInfoShort             = "Show information of meta nodes"
49
	cmdMetaNodeDecommissionInfoShort = "Decommission partitions in a meta node to other nodes"
50
	cmdMetaNodeMigrateInfoShort      = "Migrate partitions from a meta node to the other node"
51
)
52

53
func newMetaNodeListCmd(client *master.MasterClient) *cobra.Command {
54
	var optFilterStatus string
55
	var optFilterWritable string
56
	cmd := &cobra.Command{
57
		Use:     CliOpList,
58
		Short:   cmdMetaNodeListShort,
59
		Aliases: []string{"ls"},
60
		Run: func(cmd *cobra.Command, args []string) {
61
			var err error
62
			defer func() {
63
				errout(err)
64
			}()
65
			var view *proto.ClusterView
66
			if view, err = client.AdminAPI().GetCluster(); err != nil {
67
				return
68
			}
69
			sort.SliceStable(view.MetaNodes, func(i, j int) bool {
70
				return view.MetaNodes[i].ID < view.MetaNodes[j].ID
71
			})
72
			stdout("[Meta nodes]\n")
73
			stdout("%v\n", formatNodeViewTableHeader())
74
			for _, node := range view.MetaNodes {
75
				if optFilterStatus != "" &&
76
					!strings.Contains(formatNodeStatus(node.IsActive), optFilterStatus) {
77
					continue
78
				}
79
				if optFilterWritable != "" &&
80
					!strings.Contains(formatYesNo(node.IsWritable), optFilterWritable) {
81
					continue
82
				}
83
				stdout("%v\n", formatNodeView(&node, true))
84
			}
85
		},
86
	}
87
	cmd.Flags().StringVar(&optFilterWritable, "filter-writable", "", "Filter node writable status")
88
	cmd.Flags().StringVar(&optFilterStatus, "filter-status", "", "Filter status [Active, Inactive")
89
	return cmd
90
}
91

92
func newMetaNodeInfoCmd(client *master.MasterClient) *cobra.Command {
93
	cmd := &cobra.Command{
94
		Use:   CliOpInfo + " [{HOST}:{PORT}]",
95
		Short: cmdMetaNodeInfoShort,
96
		Args:  cobra.MinimumNArgs(1),
97
		Run: func(cmd *cobra.Command, args []string) {
98
			var err error
99
			var nodeAddr string
100
			var metanodeInfo *proto.MetaNodeInfo
101
			defer func() {
102
				errout(err)
103
			}()
104
			nodeAddr = args[0]
105
			if metanodeInfo, err = client.NodeAPI().GetMetaNode(nodeAddr); err != nil {
106
				return
107
			}
108
			stdout("[Meta node info]\n")
109
			stdout("%v", formatMetaNodeDetail(metanodeInfo, false))
110
		},
111
		ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
112
			if len(args) != 0 {
113
				return nil, cobra.ShellCompDirectiveNoFileComp
114
			}
115
			return validMetaNodes(client, toComplete), cobra.ShellCompDirectiveNoFileComp
116
		},
117
	}
118
	return cmd
119
}
120

121
func newMetaNodeDecommissionCmd(client *master.MasterClient) *cobra.Command {
122
	var (
123
		optCount    int
124
		clientIDKey string
125
	)
126
	cmd := &cobra.Command{
127
		Use:   CliOpDecommission + " [{HOST}:{PORT}]",
128
		Short: cmdMetaNodeDecommissionInfoShort,
129
		Args:  cobra.MinimumNArgs(1),
130
		Run: func(cmd *cobra.Command, args []string) {
131
			var err error
132
			var nodeAddr string
133
			defer func() {
134
				errout(err)
135
			}()
136
			nodeAddr = args[0]
137
			if optCount < 0 {
138
				stdout("Migrate mp count should >= 0\n")
139
				return
140
			}
141
			if err = client.NodeAPI().MetaNodeDecommission(nodeAddr, optCount, clientIDKey); err != nil {
142
				return
143
			}
144
			stdout("Decommission meta node successfully\n")
145
		},
146
		ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
147
			if len(args) != 0 {
148
				return nil, cobra.ShellCompDirectiveNoFileComp
149
			}
150
			return validMetaNodes(client, toComplete), cobra.ShellCompDirectiveNoFileComp
151
		},
152
	}
153
	cmd.Flags().IntVar(&optCount, CliFlagCount, 0, "MetaNode delete mp count")
154
	cmd.Flags().StringVar(&clientIDKey, CliFlagClientIDKey, client.ClientIDKey(), CliUsageClientIDKey)
155
	return cmd
156
}
157

158
func newMetaNodeMigrateCmd(client *master.MasterClient) *cobra.Command {
159
	var (
160
		optCount    int
161
		clientIDKey string
162
	)
163
	cmd := &cobra.Command{
164
		Use:   CliOpMigrate + " src[{HOST}:{PORT}] dst[{HOST}:{PORT}]",
165
		Short: cmdMetaNodeMigrateInfoShort,
166
		Args:  cobra.MinimumNArgs(2),
167
		Run: func(cmd *cobra.Command, args []string) {
168
			var err error
169
			var src, dst string
170
			defer func() {
171
				errout(err)
172
			}()
173
			src = args[0]
174
			dst = args[1]
175
			if optCount > mpMigrateMax || optCount <= 0 {
176
				stdout("Migrate mp count should between [1-15]\n")
177
				return
178
			}
179
			if err = client.NodeAPI().MetaNodeMigrate(src, dst, optCount, clientIDKey); err != nil {
180
				return
181
			}
182
			stdout("Migrate meta node successfully\n")
183
		},
184
		ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
185
			if len(args) != 0 {
186
				return nil, cobra.ShellCompDirectiveNoFileComp
187
			}
188
			return validMetaNodes(client, toComplete), cobra.ShellCompDirectiveNoFileComp
189
		},
190
	}
191
	cmd.Flags().IntVar(&optCount, CliFlagCount, mpMigrateMax, "Migrate mp count")
192
	cmd.Flags().StringVar(&clientIDKey, CliFlagClientIDKey, client.ClientIDKey(), CliUsageClientIDKey)
193
	return cmd
194
}
195

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.