From 73564e6a01770316f5ab57e7f4ba8227cedbf1dd Mon Sep 17 00:00:00 2001 From: Chris Lu Date: Thu, 23 Apr 2020 13:37:31 -0700 Subject: [PATCH] master: add cluster wide lock/unlock operation in weed shell fix https://github.com/chrislusf/seaweedfs/issues/1286 --- weed/server/master_server.go | 4 ++++ weed/server/master_server_handlers_ui.go | 3 ++- weed/shell/command_ec_balance.go | 4 ++++ weed/shell/command_ec_decode.go | 4 ++++ weed/shell/command_ec_encode.go | 4 ++++ weed/shell/command_ec_rebuild.go | 4 ++++ weed/shell/command_volume_balance.go | 4 ++++ weed/shell/command_volume_configure_replication.go | 4 ++++ weed/shell/command_volume_copy.go | 4 ++++ weed/shell/command_volume_delete.go | 4 ++++ weed/shell/command_volume_fix_replication.go | 4 ++++ weed/shell/command_volume_fsck.go | 4 ++++ weed/shell/command_volume_mount.go | 4 ++++ weed/shell/command_volume_move.go | 4 ++++ weed/shell/command_volume_tier_download.go | 4 ++++ weed/shell/command_volume_tier_upload.go | 4 ++++ weed/shell/command_volume_unmount.go | 4 ++++ weed/shell/commands.go | 10 ++++++++++ 18 files changed, 76 insertions(+), 1 deletion(-) diff --git a/weed/server/master_server.go b/weed/server/master_server.go index a7bea8905..ba17748bd 100644 --- a/weed/server/master_server.go +++ b/weed/server/master_server.go @@ -205,6 +205,10 @@ func (ms *MasterServer) startAdminScripts() { filerHostPort := v.GetString("master.filer.default") scriptLines := strings.Split(adminScripts, "\n") + if !strings.Contains(adminScripts, "lock") { + scriptLines = append(append([]string{}, "lock"), scriptLines...) + scriptLines = append(scriptLines, "unlock") + } masterAddress := "localhost:" + strconv.Itoa(ms.option.Port) diff --git a/weed/server/master_server_handlers_ui.go b/weed/server/master_server_handlers_ui.go index f241df87f..994f0ffb3 100644 --- a/weed/server/master_server_handlers_ui.go +++ b/weed/server/master_server_handlers_ui.go @@ -2,6 +2,7 @@ package weed_server import ( "net/http" + "time" "github.com/chrislusf/raft" ui "github.com/chrislusf/seaweedfs/weed/server/master_ui" @@ -11,7 +12,7 @@ import ( func (ms *MasterServer) uiStatusHandler(w http.ResponseWriter, r *http.Request) { infos := make(map[string]interface{}) - infos["Version"] = util.VERSION + infos["Up Time"] = time.Now().Sub(startTime).String() args := struct { Version string Topology interface{} diff --git a/weed/shell/command_ec_balance.go b/weed/shell/command_ec_balance.go index 299d44fed..1ddb6a490 100644 --- a/weed/shell/command_ec_balance.go +++ b/weed/shell/command_ec_balance.go @@ -98,6 +98,10 @@ func (c *commandEcBalance) Help() string { func (c *commandEcBalance) Do(args []string, commandEnv *CommandEnv, writer io.Writer) (err error) { + if err = commandEnv.confirmIsLocked(); err != nil { + return + } + balanceCommand := flag.NewFlagSet(c.Name(), flag.ContinueOnError) collection := balanceCommand.String("collection", "EACH_COLLECTION", "collection name, or \"EACH_COLLECTION\" for each collection") dc := balanceCommand.String("dataCenter", "", "only apply the balancing for this dataCenter") diff --git a/weed/shell/command_ec_decode.go b/weed/shell/command_ec_decode.go index b69e403cb..5f03df58c 100644 --- a/weed/shell/command_ec_decode.go +++ b/weed/shell/command_ec_decode.go @@ -36,6 +36,10 @@ func (c *commandEcDecode) Help() string { func (c *commandEcDecode) Do(args []string, commandEnv *CommandEnv, writer io.Writer) (err error) { + if err = commandEnv.confirmIsLocked(); err != nil { + return + } + encodeCommand := flag.NewFlagSet(c.Name(), flag.ContinueOnError) volumeId := encodeCommand.Int("volumeId", 0, "the volume id") collection := encodeCommand.String("collection", "", "the collection name") diff --git a/weed/shell/command_ec_encode.go b/weed/shell/command_ec_encode.go index 6efb05488..8f11f8480 100644 --- a/weed/shell/command_ec_encode.go +++ b/weed/shell/command_ec_encode.go @@ -54,6 +54,10 @@ func (c *commandEcEncode) Help() string { func (c *commandEcEncode) Do(args []string, commandEnv *CommandEnv, writer io.Writer) (err error) { + if err = commandEnv.confirmIsLocked(); err != nil { + return + } + encodeCommand := flag.NewFlagSet(c.Name(), flag.ContinueOnError) volumeId := encodeCommand.Int("volumeId", 0, "the volume id") collection := encodeCommand.String("collection", "", "the collection name") diff --git a/weed/shell/command_ec_rebuild.go b/weed/shell/command_ec_rebuild.go index d9d943e6d..df28681fe 100644 --- a/weed/shell/command_ec_rebuild.go +++ b/weed/shell/command_ec_rebuild.go @@ -56,6 +56,10 @@ func (c *commandEcRebuild) Help() string { func (c *commandEcRebuild) Do(args []string, commandEnv *CommandEnv, writer io.Writer) (err error) { + if err = commandEnv.confirmIsLocked(); err != nil { + return + } + fixCommand := flag.NewFlagSet(c.Name(), flag.ContinueOnError) collection := fixCommand.String("collection", "EACH_COLLECTION", "collection name, or \"EACH_COLLECTION\" for each collection") applyChanges := fixCommand.Bool("force", false, "apply the changes") diff --git a/weed/shell/command_volume_balance.go b/weed/shell/command_volume_balance.go index 349f52f1c..69e3c7fd9 100644 --- a/weed/shell/command_volume_balance.go +++ b/weed/shell/command_volume_balance.go @@ -60,6 +60,10 @@ func (c *commandVolumeBalance) Help() string { func (c *commandVolumeBalance) Do(args []string, commandEnv *CommandEnv, writer io.Writer) (err error) { + if err = commandEnv.confirmIsLocked(); err != nil { + return + } + balanceCommand := flag.NewFlagSet(c.Name(), flag.ContinueOnError) collection := balanceCommand.String("collection", "EACH_COLLECTION", "collection name, or use \"ALL_COLLECTIONS\" across collections, \"EACH_COLLECTION\" for each collection") dc := balanceCommand.String("dataCenter", "", "only apply the balancing for this dataCenter") diff --git a/weed/shell/command_volume_configure_replication.go b/weed/shell/command_volume_configure_replication.go index 133ec62c6..ff976c345 100644 --- a/weed/shell/command_volume_configure_replication.go +++ b/weed/shell/command_volume_configure_replication.go @@ -35,6 +35,10 @@ func (c *commandVolumeConfigureReplication) Help() string { func (c *commandVolumeConfigureReplication) Do(args []string, commandEnv *CommandEnv, writer io.Writer) (err error) { + if err = commandEnv.confirmIsLocked(); err != nil { + return + } + configureReplicationCommand := flag.NewFlagSet(c.Name(), flag.ContinueOnError) volumeIdInt := configureReplicationCommand.Int("volumeId", 0, "the volume id") replicationString := configureReplicationCommand.String("replication", "", "the intended replication value") diff --git a/weed/shell/command_volume_copy.go b/weed/shell/command_volume_copy.go index aecc071ad..cdd10863f 100644 --- a/weed/shell/command_volume_copy.go +++ b/weed/shell/command_volume_copy.go @@ -31,6 +31,10 @@ func (c *commandVolumeCopy) Help() string { func (c *commandVolumeCopy) Do(args []string, commandEnv *CommandEnv, writer io.Writer) (err error) { + if err = commandEnv.confirmIsLocked(); err != nil { + return + } + if len(args) != 3 { fmt.Fprintf(writer, "received args: %+v\n", args) return fmt.Errorf("need 3 args of ") diff --git a/weed/shell/command_volume_delete.go b/weed/shell/command_volume_delete.go index 5869b1621..c5cc9e277 100644 --- a/weed/shell/command_volume_delete.go +++ b/weed/shell/command_volume_delete.go @@ -30,6 +30,10 @@ func (c *commandVolumeDelete) Help() string { func (c *commandVolumeDelete) Do(args []string, commandEnv *CommandEnv, writer io.Writer) (err error) { + if err = commandEnv.confirmIsLocked(); err != nil { + return + } + if len(args) != 2 { fmt.Fprintf(writer, "received args: %+v\n", args) return fmt.Errorf("need 2 args of ") diff --git a/weed/shell/command_volume_fix_replication.go b/weed/shell/command_volume_fix_replication.go index 5bfe1edc9..d16a89434 100644 --- a/weed/shell/command_volume_fix_replication.go +++ b/weed/shell/command_volume_fix_replication.go @@ -44,6 +44,10 @@ func (c *commandVolumeFixReplication) Help() string { func (c *commandVolumeFixReplication) Do(args []string, commandEnv *CommandEnv, writer io.Writer) (err error) { + if err = commandEnv.confirmIsLocked(); err != nil { + return + } + takeAction := true if len(args) > 0 && args[0] == "-n" { takeAction = false diff --git a/weed/shell/command_volume_fsck.go b/weed/shell/command_volume_fsck.go index 07e2bee5b..69a1a63b4 100644 --- a/weed/shell/command_volume_fsck.go +++ b/weed/shell/command_volume_fsck.go @@ -48,6 +48,10 @@ func (c *commandVolumeFsck) Help() string { func (c *commandVolumeFsck) Do(args []string, commandEnv *CommandEnv, writer io.Writer) (err error) { + if err = commandEnv.confirmIsLocked(); err != nil { + return + } + fsckCommand := flag.NewFlagSet(c.Name(), flag.ContinueOnError) verbose := fsckCommand.Bool("v", false, "verbose mode") applyPurging := fsckCommand.Bool("reallyDeleteFromVolume", false, " delete data not referenced by the filer") diff --git a/weed/shell/command_volume_mount.go b/weed/shell/command_volume_mount.go index cffc7136b..ded7b7e66 100644 --- a/weed/shell/command_volume_mount.go +++ b/weed/shell/command_volume_mount.go @@ -34,6 +34,10 @@ func (c *commandVolumeMount) Help() string { func (c *commandVolumeMount) Do(args []string, commandEnv *CommandEnv, writer io.Writer) (err error) { + if err = commandEnv.confirmIsLocked(); err != nil { + return + } + if len(args) != 2 { fmt.Fprintf(writer, "received args: %+v\n", args) return fmt.Errorf("need 2 args of ") diff --git a/weed/shell/command_volume_move.go b/weed/shell/command_volume_move.go index c25b953a5..392b947e7 100644 --- a/weed/shell/command_volume_move.go +++ b/weed/shell/command_volume_move.go @@ -44,6 +44,10 @@ func (c *commandVolumeMove) Help() string { func (c *commandVolumeMove) Do(args []string, commandEnv *CommandEnv, writer io.Writer) (err error) { + if err = commandEnv.confirmIsLocked(); err != nil { + return + } + if len(args) != 3 { fmt.Fprintf(writer, "received args: %+v\n", args) return fmt.Errorf("need 3 args of ") diff --git a/weed/shell/command_volume_tier_download.go b/weed/shell/command_volume_tier_download.go index 756dc4686..d31c8c031 100644 --- a/weed/shell/command_volume_tier_download.go +++ b/weed/shell/command_volume_tier_download.go @@ -42,6 +42,10 @@ func (c *commandVolumeTierDownload) Help() string { func (c *commandVolumeTierDownload) Do(args []string, commandEnv *CommandEnv, writer io.Writer) (err error) { + if err = commandEnv.confirmIsLocked(); err != nil { + return + } + tierCommand := flag.NewFlagSet(c.Name(), flag.ContinueOnError) volumeId := tierCommand.Int("volumeId", 0, "the volume id") collection := tierCommand.String("collection", "", "the collection name") diff --git a/weed/shell/command_volume_tier_upload.go b/weed/shell/command_volume_tier_upload.go index 5131e8f85..f92cdc3e4 100644 --- a/weed/shell/command_volume_tier_upload.go +++ b/weed/shell/command_volume_tier_upload.go @@ -56,6 +56,10 @@ func (c *commandVolumeTierUpload) Help() string { func (c *commandVolumeTierUpload) Do(args []string, commandEnv *CommandEnv, writer io.Writer) (err error) { + if err = commandEnv.confirmIsLocked(); err != nil { + return + } + tierCommand := flag.NewFlagSet(c.Name(), flag.ContinueOnError) volumeId := tierCommand.Int("volumeId", 0, "the volume id") collection := tierCommand.String("collection", "", "the collection name") diff --git a/weed/shell/command_volume_unmount.go b/weed/shell/command_volume_unmount.go index 6e5bef485..7596bb4c8 100644 --- a/weed/shell/command_volume_unmount.go +++ b/weed/shell/command_volume_unmount.go @@ -34,6 +34,10 @@ func (c *commandVolumeUnmount) Help() string { func (c *commandVolumeUnmount) Do(args []string, commandEnv *CommandEnv, writer io.Writer) (err error) { + if err = commandEnv.confirmIsLocked(); err != nil { + return + } + if len(args) != 2 { fmt.Fprintf(writer, "received args: %+v\n", args) return fmt.Errorf("need 2 args of ") diff --git a/weed/shell/commands.go b/weed/shell/commands.go index 2faba4280..a43196b62 100644 --- a/weed/shell/commands.go +++ b/weed/shell/commands.go @@ -68,6 +68,16 @@ func (ce *CommandEnv) isDirectory(path string) bool { } +func (ce *CommandEnv) confirmIsLocked() error { + + if ce.locker.isLocking { + return nil + } + + return fmt.Errorf("need to lock to continue") + +} + func (ce *CommandEnv) checkDirectory(path string) error { dir, name := util.FullPath(path).DirAndName()