Files
seaweedfs/weed/shell/command_volume_server_state.go
T
Chris LuandClaude Opus 5.5 80e0bf97dd shell: dial volume servers on their registered gRPC port in volume.scrub, ec.scrub, volume.server.state (#12315)
* shell: dial scrubbed volume servers on their registered gRPC port

volume.scrub, ec.scrub and volume.server.state built each data node's
address from its HTTP address alone, so a volume server whose gRPC port
is not the HTTP port + 10000 was dialed on the wrong port.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* shell: resolve -node volume servers to their registered gRPC port

An explicit -node/-nodes list was dialed on HTTP port + 10000 too.
Match each entry against the topology and take the data node's gRPC
port; an address the master does not know is used as given.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* shell: keep a gRPC port given in -node

A node given as host:port.grpcPort was rewritten to the registered gRPC
port; only a bare host:port takes it from the topology.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* shell: dial -node volume servers when the topology is unavailable

An explicit node list did not need the master before; keep using it as
given if the volume list cannot be fetched. Selecting every node still
requires the topology.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* shell: bound the topology lookup for an explicit -node list

With the master down, volumeServer.state -nodes waited on the master
forever instead of reaching the given volume servers. Give the lookup a
30s deadline and fall back to the addresses as given.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* shell: resolve volume server addresses from a topology-only listing

Only the data nodes are needed to find each server's gRPC port, so ask
the master for the topology without its volumes and ec shards. A large
cluster no longer risks running past the -node lookup deadline and
falling back to the HTTP port + 10000.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* shell: skip the topology lookup when every -node names its gRPC port

A host:port.grpcPort entry is dialed as given, so a list made only of
those does not need the master and no longer waits on it during an
outage.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-01 18:50:48 -08:00

165 lines
4.5 KiB
Go

package shell
import (
"context"
"flag"
"fmt"
"io"
"slices"
"strings"
"github.com/seaweedfs/seaweedfs/weed/operation"
"github.com/seaweedfs/seaweedfs/weed/pb"
"github.com/seaweedfs/seaweedfs/weed/pb/volume_server_pb"
"google.golang.org/protobuf/proto"
)
func init() {
Commands = append(Commands, &commandVolumeServerState{})
}
type commandVolumeServerState struct {
env *CommandEnv
writer io.Writer
}
func (c *commandVolumeServerState) Name() string {
return "volumeServer.state"
}
func (c *commandVolumeServerState) Help() string {
return `query/update volume server state settings
volumeServer.state [-nodes <host:port>] [..state flags...]
This command display volume server state flags for the provided
list of nodes; if empty, all nodes in the topology are queried.
For example:
volumeServer.state --nodes 192.168.10.111:9000,192.168.10.112:9000
Additionally, if any flags are provided, these are applied
to the selected node(s). The command will display the resulting
state for each node *after* the state is updated. For example...
volumeServer.state --nodes 192.168.10.111:9000 --maintenanceOn
...will set the specified volume server to maintenance mode.
`
}
func (c *commandVolumeServerState) HasTag(CommandTag) bool {
return false
}
func (c *commandVolumeServerState) write(format string, a ...any) {
format = strings.TrimRight(format, " ")
if len(format) == 0 {
format = "\n"
}
fmt.Fprintf(c.writer, format, a...)
last := format[len(format)-1:]
if last != "\n" && last != "\r" {
fmt.Fprint(c.writer, "\n")
}
}
func (c *commandVolumeServerState) bool2Str(b bool) string {
if b {
return "yes"
}
return "no"
}
func (c *commandVolumeServerState) Do(args []string, commandEnv *CommandEnv, writer io.Writer) (err error) {
vsStateCommand := flag.NewFlagSet(c.Name(), flag.ContinueOnError)
nodesStr := vsStateCommand.String("nodes", "", "comma-separated list of volume servers <host>:<port> (optional)")
maintenanceOn := vsStateCommand.Bool("maintenanceOn", false, "enables maintenance mode on the server")
maintenanceOff := vsStateCommand.Bool("maintenanceOff", false, "disables maintenance mode on the server")
if err = vsStateCommand.Parse(args); err != nil {
return err
}
if *maintenanceOn && *maintenanceOff {
return fmt.Errorf("--maintenanceOn and --maintenanceOff are mutually exclusive")
}
volumeServerAddrs, err := collectVolumeServerAddresses(commandEnv, *nodesStr)
if err != nil {
return err
}
slices.Sort(volumeServerAddrs)
if len(volumeServerAddrs) == 0 {
return fmt.Errorf("no volume servers specified")
}
c.env = commandEnv
c.writer = writer
for _, addr := range volumeServerAddrs {
state, err := c.getVolumeServerState(addr)
if err != nil {
return fmt.Errorf("failed to load state from %v: %v", addr, err)
}
stateOrig := &volume_server_pb.VolumeServerState{}
proto.Merge(stateOrig, state)
// apply updates, if any
switch {
case *maintenanceOn:
state.Maintenance = true
case *maintenanceOff:
state.Maintenance = false
}
// update volume server state if settings changed
if !proto.Equal(state, stateOrig) {
state, err = c.setVolumeServerState(addr, state)
if err != nil {
return fmt.Errorf("failed to update state for %v: %v", addr, err)
}
}
c.write("%v\t -> Maintenance mode: %s\n", addr.String(), c.bool2Str(state.GetMaintenance()))
}
return
}
func (c *commandVolumeServerState) getVolumeServerState(volumeServerAddress pb.ServerAddress) (*volume_server_pb.VolumeServerState, error) {
var state *volume_server_pb.VolumeServerState
err := operation.WithVolumeServerClient(false, volumeServerAddress, c.env.option.GrpcDialOption, func(volumeServerClient volume_server_pb.VolumeServerClient) error {
res, err := volumeServerClient.GetState(context.Background(), &volume_server_pb.GetStateRequest{})
if err != nil {
return err
}
state = res.GetState()
return nil
})
return state, err
}
func (c *commandVolumeServerState) setVolumeServerState(volumeServerAddress pb.ServerAddress, state *volume_server_pb.VolumeServerState) (*volume_server_pb.VolumeServerState, error) {
var stateAfter *volume_server_pb.VolumeServerState
err := operation.WithVolumeServerClient(false, volumeServerAddress, c.env.option.GrpcDialOption, func(volumeServerClient volume_server_pb.VolumeServerClient) error {
res, err := volumeServerClient.SetState(context.Background(), &volume_server_pb.SetStateRequest{
State: state,
})
if err != nil {
return err
}
stateAfter = res.GetState()
return nil
})
return stateAfter, err
}