Browse Source

shell: optionally read filer address from master

pull/2427/head
Chris Lu 3 years ago
parent
commit
5160eb08f7
  1. 9
      weed/command/shell.go
  2. 6
      weed/messaging/broker/broker_grpc_server_discovery.go
  3. 15
      weed/pb/master.proto
  4. 998
      weed/pb/master_pb/master.pb.go
  5. 22
      weed/server/master_grpc_server.go
  6. 20
      weed/server/master_grpc_server_cluster.go
  7. 3
      weed/server/master_server.go
  8. 70
      weed/server/master_server_cluster.go
  9. 24
      weed/shell/shell_liner.go
  10. 6
      weed/wdclient/masterclient.go

9
weed/command/shell.go

@ -37,7 +37,7 @@ func runShell(command *Command, args []string) bool {
util.LoadConfiguration("security", false)
shellOptions.GrpcDialOption = security.LoadClientTLS(util.GetViper(), "grpc.client")
if *shellOptions.Masters == "" && *shellInitialFiler == "" {
if *shellOptions.Masters == "" {
util.LoadConfiguration("shell", false)
v := util.GetViper()
cluster := v.GetString("cluster.default")
@ -45,15 +45,16 @@ func runShell(command *Command, args []string) bool {
cluster = *shellCluster
}
if cluster == "" {
*shellOptions.Masters, *shellInitialFiler = "localhost:9333", "localhost:8888"
*shellOptions.Masters = "localhost:9333"
fmt.Printf("master: %s\n", *shellOptions.Masters)
} else {
*shellOptions.Masters = v.GetString("cluster." + cluster + ".master")
*shellInitialFiler = v.GetString("cluster." + cluster + ".filer")
fmt.Printf("master: %s filer: %s\n", *shellOptions.Masters, *shellInitialFiler)
}
}
fmt.Printf("master: %s filer: %s\n", *shellOptions.Masters, *shellInitialFiler)
shellOptions.FilerAddress = pb.ServerAddress(*shellInitialFiler)
shellOptions.Directory = "/"

6
weed/messaging/broker/broker_grpc_server_discovery.go

@ -93,14 +93,16 @@ func (broker *MessageBroker) checkFilers() {
for !found {
for _, master := range masters {
err := broker.withMasterClient(master, func(client master_pb.SeaweedClient) error {
resp, err := client.ListMasterClients(context.Background(), &master_pb.ListMasterClientsRequest{
resp, err := client.ListClusterNodes(context.Background(), &master_pb.ListClusterNodesRequest{
ClientType: "filer",
})
if err != nil {
return err
}
filers = append(filers, pb.FromAddressStrings(resp.GrpcAddresses)...)
for _, clusterNode := range resp.ClusterNodes {
filers = append(filers, pb.ServerAddress(clusterNode.Address))
}
return nil
})

15
weed/pb/master.proto

@ -29,7 +29,7 @@ service Seaweed {
}
rpc GetMasterConfiguration (GetMasterConfigurationRequest) returns (GetMasterConfigurationResponse) {
}
rpc ListMasterClients (ListMasterClientsRequest) returns (ListMasterClientsResponse) {
rpc ListClusterNodes (ListClusterNodesRequest) returns (ListClusterNodesResponse) {
}
rpc LeaseAdminToken (LeaseAdminTokenRequest) returns (LeaseAdminTokenResponse) {
}
@ -127,8 +127,9 @@ message SuperBlockExtra {
}
message KeepConnectedRequest {
string name = 1;
string client_type = 1;
string client_address = 3;
string version = 4;
}
message VolumeLocation {
@ -284,11 +285,15 @@ message GetMasterConfigurationResponse {
bool volume_preallocate = 7;
}
message ListMasterClientsRequest {
message ListClusterNodesRequest {
string client_type = 1;
}
message ListMasterClientsResponse {
repeated string grpc_addresses = 1;
message ListClusterNodesResponse {
message ClusterNode {
string address = 1;
string version = 2;
}
repeated ClusterNode cluster_nodes = 1;
}
message LeaseAdminTokenRequest {

998
weed/pb/master_pb/master.pb.go
File diff suppressed because it is too large
View File

22
weed/server/master_grpc_server.go

@ -6,7 +6,6 @@ import (
"github.com/chrislusf/seaweedfs/weed/storage/backend"
"github.com/chrislusf/seaweedfs/weed/util"
"net"
"strings"
"time"
"github.com/chrislusf/raft"
@ -195,9 +194,13 @@ func (ms *MasterServer) KeepConnected(stream master_pb.Seaweed_KeepConnectedServ
// buffer by 1 so we don't end up getting stuck writing to stopChan forever
stopChan := make(chan bool, 1)
clientName, messageChan := ms.addClient(req.Name, peerAddress)
clientName, messageChan := ms.addClient(req.ClientType, peerAddress)
ms.Cluster.AddClusterNode(req.ClientType, peerAddress, req.Version)
defer ms.deleteClient(clientName)
defer func() {
ms.Cluster.RemoveClusterNode(req.ClientType, peerAddress)
ms.deleteClient(clientName)
}()
for _, message := range ms.Topo.ToVolumeLocations() {
if sendErr := stream.Send(message); sendErr != nil {
@ -295,19 +298,6 @@ func findClientAddress(ctx context.Context, grpcPort uint32) string {
}
func (ms *MasterServer) ListMasterClients(ctx context.Context, req *master_pb.ListMasterClientsRequest) (*master_pb.ListMasterClientsResponse, error) {
resp := &master_pb.ListMasterClientsResponse{}
ms.clientChansLock.RLock()
defer ms.clientChansLock.RUnlock()
for k := range ms.clientChans {
if strings.HasPrefix(k, req.ClientType+"@") {
resp.GrpcAddresses = append(resp.GrpcAddresses, k[len(req.ClientType)+1:])
}
}
return resp, nil
}
func (ms *MasterServer) GetMasterConfiguration(ctx context.Context, req *master_pb.GetMasterConfigurationRequest) (*master_pb.GetMasterConfigurationResponse, error) {
// tell the volume servers about the leader

20
weed/server/master_grpc_server_cluster.go

@ -0,0 +1,20 @@
package weed_server
import (
"context"
"github.com/chrislusf/seaweedfs/weed/pb/master_pb"
)
func (ms *MasterServer) ListClusterNodes(ctx context.Context, req *master_pb.ListClusterNodesRequest) (*master_pb.ListClusterNodesResponse, error) {
resp := &master_pb.ListClusterNodesResponse{}
clusterNodes := ms.Cluster.ListClusterNode(req.ClientType)
for _, node := range clusterNodes {
resp.ClusterNodes = append(resp.ClusterNodes, &master_pb.ListClusterNodesResponse_ClusterNode{
Address: string(node.address),
Version: node.version,
})
}
return resp, nil
}

3
weed/server/master_server.go

@ -67,6 +67,8 @@ type MasterServer struct {
MasterClient *wdclient.MasterClient
adminLocks *AdminLocks
Cluster *Cluster
}
func NewMasterServer(r *mux.Router, option *MasterOption, peers []pb.ServerAddress) *MasterServer {
@ -103,6 +105,7 @@ func NewMasterServer(r *mux.Router, option *MasterOption, peers []pb.ServerAddre
grpcDialOption: grpcDialOption,
MasterClient: wdclient.NewMasterClient(grpcDialOption, "master", option.Master, "", peers),
adminLocks: NewAdminLocks(),
Cluster: NewCluster(),
}
ms.boundedLeaderChan = make(chan int, 16)

70
weed/server/master_server_cluster.go

@ -0,0 +1,70 @@
package weed_server
import (
"github.com/chrislusf/seaweedfs/weed/pb"
"sync"
)
type NodeType int
const (
filerNodeType NodeType = iota
)
type ClusterNode struct {
address pb.ServerAddress
version string
}
type Cluster struct {
filers map[pb.ServerAddress]*ClusterNode
filersLock sync.RWMutex
}
func NewCluster() *Cluster {
return &Cluster{
filers: make(map[pb.ServerAddress]*ClusterNode),
}
}
func (cluster *Cluster) AddClusterNode(nodeType string, address pb.ServerAddress, version string) {
switch nodeType {
case "filer":
cluster.filersLock.Lock()
defer cluster.filersLock.Unlock()
if _, found := cluster.filers[address]; found {
return
}
cluster.filers[address] = &ClusterNode{
address: address,
version: version,
}
case "master":
}
}
func (cluster *Cluster) RemoveClusterNode(nodeType string, address pb.ServerAddress) {
switch nodeType {
case "filer":
cluster.filersLock.Lock()
defer cluster.filersLock.Unlock()
if _, found := cluster.filers[address]; !found {
return
}
delete(cluster.filers, address)
case "master":
}
}
func (cluster *Cluster) ListClusterNode(nodeType string) (nodes []*ClusterNode){
switch nodeType {
case "filer":
cluster.filersLock.RLock()
defer cluster.filersLock.RUnlock()
for _, node := range cluster.filers {
nodes = append(nodes, node)
}
case "master":
}
return
}

24
weed/shell/shell_liner.go

@ -3,9 +3,12 @@ package shell
import (
"context"
"fmt"
"github.com/chrislusf/seaweedfs/weed/pb"
"github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
"github.com/chrislusf/seaweedfs/weed/pb/master_pb"
"github.com/chrislusf/seaweedfs/weed/util/grace"
"io"
"math/rand"
"os"
"path"
"regexp"
@ -47,6 +50,27 @@ func RunShell(options ShellOptions) {
go commandEnv.MasterClient.KeepConnectedToMaster()
commandEnv.MasterClient.WaitUntilConnected()
if commandEnv.option.FilerAddress == "" {
var filers []pb.ServerAddress
commandEnv.MasterClient.WithClient(func(client master_pb.SeaweedClient) error {
resp, err := client.ListClusterNodes(context.Background(), &master_pb.ListClusterNodesRequest{
ClientType: "filer",
})
if err != nil {
return err
}
for _, clusterNode := range resp.ClusterNodes {
filers = append(filers, pb.ServerAddress(clusterNode.Address))
}
return nil
})
if len(filers) > 0 {
fmt.Printf("filers: %v\n", filers)
commandEnv.option.FilerAddress = filers[rand.Intn(len(filers))]
}
}
if commandEnv.option.FilerAddress != "" {
commandEnv.WithFilerClient(func(filerClient filer_pb.SeaweedFilerClient) error {
resp, err := filerClient.GetFilerConfiguration(context.Background(), &filer_pb.GetFilerConfigurationRequest{})

6
weed/wdclient/masterclient.go

@ -105,7 +105,11 @@ func (mc *MasterClient) tryConnectToMaster(master pb.ServerAddress) (nextHintedL
return err
}
if err = stream.Send(&master_pb.KeepConnectedRequest{Name: mc.clientType, ClientAddress: string(mc.clientHost)}); err != nil {
if err = stream.Send(&master_pb.KeepConnectedRequest{
ClientType: mc.clientType,
ClientAddress: string(mc.clientHost),
Version: util.Version(),
}); err != nil {
glog.V(0).Infof("%s masterClient failed to send to %s: %v", mc.clientType, master, err)
return err
}

Loading…
Cancel
Save