Skip to content

Commit

Permalink
enhancement: introduce log to cli tool and format error message
Browse files Browse the repository at this point in the history
Signed-off-by: xuxihao1 <[email protected]>
  • Loading branch information
xuxihao1 committed Jul 16, 2020
1 parent 6937cc7 commit f3cfd46
Show file tree
Hide file tree
Showing 18 changed files with 320 additions and 182 deletions.
8 changes: 0 additions & 8 deletions cli/api/metaapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,6 @@ type MetaHttpClient struct {
// NewMasterHelper returns a new MasterClient instance.
func NewMetaHttpClient(host string, useSSL bool) *MetaHttpClient {
mc := &MetaHttpClient{host: host, useSSL: useSSL}
var err error
_, err = log.InitLog("/tmp/cfs", "cli", log.DebugLevel, nil)
if err != nil {
fmt.Printf("init cli log err[%v]", err)
}
return mc
}

Expand Down Expand Up @@ -218,7 +213,6 @@ func (mc *MetaHttpClient) GetMetaPartition(pid uint64) (cursor uint64, err error
if err != nil {
log.LogErrorf("action[GetMetaPartition],pid:%v,err:%v", pid, err)
}
log.LogFlush()
}()
request := newAPIRequest(http.MethodGet, "/getPartitionById")
request.params["pid"] = fmt.Sprintf("%v", pid)
Expand All @@ -242,7 +236,6 @@ func (mc *MetaHttpClient) GetAllDentry(pid uint64) (dentryMap map[string]*metano
if err != nil {
log.LogErrorf("action[GetAllDentry],pid:%v,err:%v", pid, err)
}
log.LogFlush()
}()
dentryMap = make(map[string]*metanode.Dentry, 0)
request := newAPIRequest(http.MethodGet, "/getAllDentry")
Expand Down Expand Up @@ -293,7 +286,6 @@ func (mc *MetaHttpClient) GetAllInodes(pid uint64) (rstMap map[uint64]*Inode, er
if err != nil {
log.LogErrorf("action[GetAllInodes],pid:%v,err:%v", pid, err)
}
log.LogFlush()
}()
reqURL := fmt.Sprintf("http://%v%v?pid=%v", mc.host, "/getAllInodes", pid)
log.LogDebugf("reqURL=%v", reqURL)
Expand Down
14 changes: 10 additions & 4 deletions cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ package main

import (
"fmt"
"os"

"github.com/chubaofs/chubaofs/cli/cmd"
"github.com/chubaofs/chubaofs/sdk/master"
"github.com/chubaofs/chubaofs/util/log"
"github.com/spf13/cobra"
"os"
)

var (
Expand All @@ -32,10 +32,13 @@ var (
func runCLI() (err error) {
var cfg *cmd.Config
if cfg, err = cmd.LoadConfig(); err != nil {
fmt.Printf("init cli log err[%v]", err)
return
}
cfscli := setupCommands(cfg)
err = cfscli.Execute()
cfsCli := setupCommands(cfg)
if err = cfsCli.Execute(); err != nil {
log.LogErrorf("Command fail, err:%v", err)
}
return
}

Expand Down Expand Up @@ -73,7 +76,10 @@ following command to execute:

func main() {
var err error
_, err = log.InitLog("/tmp/cfs", "cli", log.DebugLevel, nil)
defer log.LogFlush()
if err = runCLI(); err != nil {
log.LogFlush()
_, _ = fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(1)
}
Expand Down
62 changes: 42 additions & 20 deletions cli/cmd/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
package cmd

import (
"os"
"fmt"
"strconv"

"github.com/chubaofs/chubaofs/proto"
Expand Down Expand Up @@ -58,7 +58,7 @@ func newClusterInfoCmd(client *master.MasterClient) *cobra.Command {
var cv *proto.ClusterView
if cv, err = client.AdminAPI().GetCluster(); err != nil {
errout("Get cluster info fail:\n%v\n", err)
os.Exit(1)
OsExitWithLogFlush()
}
stdout("[Cluster]\n")
stdout(formatClusterView(cv))
Expand All @@ -73,11 +73,19 @@ func newClusterStatCmd(client *master.MasterClient) *cobra.Command {
Use: CliOpStatus,
Short: cmdClusterStatShort,
Run: func(cmd *cobra.Command, args []string) {
var err error
var cs *proto.ClusterStatInfo
var (
err error
cs *proto.ClusterStatInfo
)
defer func() {
if err != nil {
errout("Error:%v", err)
OsExitWithLogFlush()
}
}()
if cs, err = client.AdminAPI().GetClusterStat(); err != nil {
errout("Get cluster info fail:\n%v\n", err)
os.Exit(1)
err = fmt.Errorf("Get cluster info fail:\n%v\n", err)
return
}
stdout("[Cluster Status]\n")
stdout(formatClusterStat(cs))
Expand All @@ -100,15 +108,22 @@ If 'freeze=false', ChubaoFS WILL automatically allocate new data partitions for
If 'freeze=true', ChubaoFS WILL NOT automatically allocate new data partitions `,
Run: func(cmd *cobra.Command, args []string) {
var err error
var enable bool
var (
err error
enable bool
)
defer func() {
if err != nil {
errout("Error:%v", err)
OsExitWithLogFlush()
}
}()
if enable, err = strconv.ParseBool(args[0]); err != nil {
errout("Parse bool fail: %v\n", err)
os.Exit(1)
err = fmt.Errorf("Parse bool fail: %v\n", err)
return
}
if err = client.AdminAPI().IsFreezeCluster(enable); err != nil {
errout("Failed: %v\n", err)
os.Exit(1)
return
}
if enable {
stdout("Freeze cluster successful!\n")
Expand All @@ -128,19 +143,26 @@ func newClusterSetThresholdCmd(client *master.MasterClient) *cobra.Command {
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
var threshold float64
var (
err error
threshold float64
)
defer func() {
if err != nil {
errout("Error:%v", err)
OsExitWithLogFlush()
}
}()
if threshold, err = strconv.ParseFloat(args[0], 64); err != nil {
errout("Parse Float fail: %v\n", err)
os.Exit(1)
err = fmt.Errorf("Parse Float fail: %v\n", err)
return
}
if threshold > 1.0 {
errout("Threshold too big\n")
os.Exit(1)
err = fmt.Errorf("Threshold too big\n")
return
}
if err = client.AdminAPI().SetMetaNodeThreshold(threshold); err != nil {
errout("Failed: %v\n", err)
os.Exit(1)
return
}
stdout("MetaNode threshold is set to %v!\n", threshold)
},
Expand Down
20 changes: 7 additions & 13 deletions cli/cmd/compatibility.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,13 @@
package cmd

import (
"os"
"fmt"
"github.com/chubaofs/chubaofs/cli/api"
"github.com/spf13/cobra"
"github.com/chubaofs/chubaofs/metanode"
"fmt"
"strconv"
"github.com/chubaofs/chubaofs/util/log"
"github.com/chubaofs/chubaofs/proto"
"github.com/spf13/cobra"
"reflect"
"strconv"
)

const (
Expand Down Expand Up @@ -55,24 +53,22 @@ func newMetaCompatibilityCmd() *cobra.Command {
Aliases: []string{"meta"},
Args: cobra.MinimumNArgs(3),
Run: func(cmd *cobra.Command, args []string) {
var err error
var (
err error
snapshotPath = args[0]
host = args[1]
pid = args[2]
)
client := api.NewMetaHttpClient(host, false)
defer func() {
if err != nil {
errout("Verify metadata consistency failed: %v\n", err)
log.LogError(err)
log.LogFlush()
os.Exit(1)
errout("Error:%v", err)
OsExitWithLogFlush()
}
}()
id, err := strconv.ParseUint(pid, 10, 64)
if err != nil {
errout("parse pid[%v] failed: %v\n", pid, err)
err = fmt.Errorf("parse pid[%v] failed: %v\n", pid, err)
return
}
cursor, err := client.GetMetaPartition(id)
Expand All @@ -90,11 +86,9 @@ func newMetaCompatibilityCmd() *cobra.Command {
}
stdout("[Meta partition is %v, verify result]\n", id)
if err = verifyDentry(client, mp); err != nil {
stdout("%v\n", err)
return
}
if err = verifyInode(client, mp); err != nil {
stdout("%v\n", err)
return
}
stdout("All meta has checked\n")
Expand Down
18 changes: 13 additions & 5 deletions cli/cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,21 +67,29 @@ func newConfigSetCmd() *cobra.Command {
Long: `Set the config file`,
Run: func(cmd *cobra.Command, args []string) {
var (
err error
masterHosts []string
)
defer func() {
if err != nil {
errout("Error:%v", err)
OsExitWithLogFlush()
}
}()
var masterHost string
stdout(fmt.Sprintf("Please input master host:\n"))
_, _ = fmt.Scanln(&masterHost)
if _, err = fmt.Scanln(&masterHost); err != nil {
return
}
if len(masterHost) == 0 {
stdout("Abort by user.\n")
err = fmt.Errorf("Abort by user.\n")
return
}
masterHosts = append(masterHosts, masterHost)
config := &Config{
MasterAddr: masterHosts,
}
if _, err := setConfig(config); err != nil {
stdout("error: %v\n", err)
if _, err = setConfig(config); err != nil {
return
}
stdout(fmt.Sprintf("Config has been set successfully!\n"))
Expand All @@ -99,7 +107,7 @@ func newConfigInfoCmd() *cobra.Command {
config, err := LoadConfig()
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(1)
OsExitWithLogFlush()
}
stdout(fmt.Sprintf("Config info:\n %v\n", config.MasterAddr))

Expand Down
11 changes: 5 additions & 6 deletions cli/cmd/datanode.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
package cmd

import (
"os"
"sort"
"strings"

Expand Down Expand Up @@ -59,7 +58,7 @@ func newDataNodeListCmd(client *master.MasterClient) *cobra.Command {
defer func() {
if err != nil {
errout("List cluster data nodes failed: %v\n", err)
os.Exit(1)
OsExitWithLogFlush()
}
}()
var view *proto.ClusterView
Expand Down Expand Up @@ -100,8 +99,8 @@ func newDataNodeInfoCmd(client *master.MasterClient) *cobra.Command {
var datanodeInfo *proto.DataNodeInfo
defer func() {
if err != nil {
errout("Show data node info failed: %v\n", err)
os.Exit(1)
errout("Error:%v", err)
OsExitWithLogFlush()
}
}()
nodeAddr = args[0]
Expand Down Expand Up @@ -132,8 +131,8 @@ func newDataNodeDecommissionCmd(client *master.MasterClient) *cobra.Command {
var nodeAddr string
defer func() {
if err != nil {
errout("decommission data node failed, err[%v]\n", err)
os.Exit(1)
errout("Error:%v", err)
OsExitWithLogFlush()
}
}()
nodeAddr = args[0]
Expand Down
Loading

0 comments on commit f3cfd46

Please sign in to comment.