@ -3,9 +3,6 @@ package pb
import (
import (
"context"
"context"
"fmt"
"fmt"
"github.com/seaweedfs/seaweedfs/weed/glog"
"github.com/seaweedfs/seaweedfs/weed/pb/volume_server_pb"
"github.com/seaweedfs/seaweedfs/weed/util"
"math/rand"
"math/rand"
"net/http"
"net/http"
"strconv"
"strconv"
@ -13,6 +10,10 @@ import (
"sync"
"sync"
"time"
"time"
"github.com/seaweedfs/seaweedfs/weed/glog"
"github.com/seaweedfs/seaweedfs/weed/pb/volume_server_pb"
"github.com/seaweedfs/seaweedfs/weed/util"
"google.golang.org/grpc"
"google.golang.org/grpc"
"google.golang.org/grpc/keepalive"
"google.golang.org/grpc/keepalive"
@ -65,15 +66,17 @@ func NewGrpcServer(opts ...grpc.ServerOption) *grpc.Server {
return grpc . NewServer ( options ... )
return grpc . NewServer ( options ... )
}
}
func GrpcDial ( ctx context . Context , address string , opts ... grpc . DialOption ) ( * grpc . ClientConn , error ) {
func GrpcDial ( ctx context . Context , address string , waitForReady bool , opts ... grpc . DialOption ) ( * grpc . ClientConn , error ) {
// opts = append(opts, grpc.WithBlock())
// opts = append(opts, grpc.WithBlock())
// opts = append(opts, grpc.WithTimeout(time.Duration(5*time.Second)))
// opts = append(opts, grpc.WithTimeout(time.Duration(5*time.Second)))
var options [ ] grpc . DialOption
var options [ ] grpc . DialOption
options = append ( options ,
options = append ( options ,
// grpc.WithTransportCredentials(insecure.NewCredentials()),
// grpc.WithTransportCredentials(insecure.NewCredentials()),
grpc . WithDefaultCallOptions (
grpc . WithDefaultCallOptions (
grpc . MaxCallSendMsgSize ( Max_Message_Size ) ,
grpc . MaxCallSendMsgSize ( Max_Message_Size ) ,
grpc . MaxCallRecvMsgSize ( Max_Message_Size ) ,
grpc . MaxCallRecvMsgSize ( Max_Message_Size ) ,
grpc . WaitForReady ( waitForReady ) ,
) ,
) ,
grpc . WithKeepaliveParams ( keepalive . ClientParameters {
grpc . WithKeepaliveParams ( keepalive . ClientParameters {
Time : 30 * time . Second , // client ping server if no activity for this long
Time : 30 * time . Second , // client ping server if no activity for this long
@ -88,7 +91,7 @@ func GrpcDial(ctx context.Context, address string, opts ...grpc.DialOption) (*gr
return grpc . DialContext ( ctx , address , options ... )
return grpc . DialContext ( ctx , address , options ... )
}
}
func getOrCreateConnection ( address string , opts ... grpc . DialOption ) ( * versionedGrpcClient , error ) {
func getOrCreateConnection ( address string , waitForReady bool , opts ... grpc . DialOption ) ( * versionedGrpcClient , error ) {
grpcClientsLock . Lock ( )
grpcClientsLock . Lock ( )
defer grpcClientsLock . Unlock ( )
defer grpcClientsLock . Unlock ( )
@ -99,7 +102,7 @@ func getOrCreateConnection(address string, opts ...grpc.DialOption) (*versionedG
}
}
ctx := context . Background ( )
ctx := context . Background ( )
grpcConnection , err := GrpcDial ( ctx , address , opts ... )
grpcConnection , err := GrpcDial ( ctx , address , waitForReady , opts ... )
if err != nil {
if err != nil {
return nil , fmt . Errorf ( "fail to dial %s: %v" , address , err )
return nil , fmt . Errorf ( "fail to dial %s: %v" , address , err )
}
}
@ -115,10 +118,10 @@ func getOrCreateConnection(address string, opts ...grpc.DialOption) (*versionedG
}
}
// WithGrpcClient In streamingMode, always use a fresh connection. Otherwise, try to reuse an existing connection.
// WithGrpcClient In streamingMode, always use a fresh connection. Otherwise, try to reuse an existing connection.
func WithGrpcClient ( streamingMode bool , fn func ( * grpc . ClientConn ) error , address string , opts ... grpc . DialOption ) error {
func WithGrpcClient ( streamingMode bool , fn func ( * grpc . ClientConn ) error , address string , waitForReady bool , opts ... grpc . DialOption ) error {
if ! streamingMode {
if ! streamingMode {
vgc , err := getOrCreateConnection ( address , opts ... )
vgc , err := getOrCreateConnection ( address , waitForReady , opts ... )
if err != nil {
if err != nil {
return fmt . Errorf ( "getOrCreateConnection %s: %v" , address , err )
return fmt . Errorf ( "getOrCreateConnection %s: %v" , address , err )
}
}
@ -138,7 +141,7 @@ func WithGrpcClient(streamingMode bool, fn func(*grpc.ClientConn) error, address
}
}
return executionErr
return executionErr
} else {
} else {
grpcConnection , err := GrpcDial ( context . Background ( ) , address , opts ... )
grpcConnection , err := GrpcDial ( context . Background ( ) , address , waitForReady , opts ... )
if err != nil {
if err != nil {
return fmt . Errorf ( "fail to dial %s: %v" , address , err )
return fmt . Errorf ( "fail to dial %s: %v" , address , err )
}
}
@ -200,11 +203,11 @@ func GrpcAddressToServerAddress(grpcAddress string) (serverAddress string) {
return util . JoinHostPort ( host , port )
return util . JoinHostPort ( host , port )
}
}
func WithMasterClient ( streamingMode bool , master ServerAddress , grpcDialOption grpc . DialOption , fn func ( client master_pb . SeaweedClient ) error ) error {
func WithMasterClient ( streamingMode bool , master ServerAddress , grpcDialOption grpc . DialOption , waitForReady bool , fn func ( client master_pb . SeaweedClient ) error ) error {
return WithGrpcClient ( streamingMode , func ( grpcConnection * grpc . ClientConn ) error {
return WithGrpcClient ( streamingMode , func ( grpcConnection * grpc . ClientConn ) error {
client := master_pb . NewSeaweedClient ( grpcConnection )
client := master_pb . NewSeaweedClient ( grpcConnection )
return fn ( client )
return fn ( client )
} , master . ToGrpcAddress ( ) , grpcDialOption )
} , master . ToGrpcAddress ( ) , waitForReady , grpcDialOption )
}
}
@ -212,7 +215,7 @@ func WithVolumeServerClient(streamingMode bool, volumeServer ServerAddress, grpc
return WithGrpcClient ( streamingMode , func ( grpcConnection * grpc . ClientConn ) error {
return WithGrpcClient ( streamingMode , func ( grpcConnection * grpc . ClientConn ) error {
client := volume_server_pb . NewVolumeServerClient ( grpcConnection )
client := volume_server_pb . NewVolumeServerClient ( grpcConnection )
return fn ( client )
return fn ( client )
} , volumeServer . ToGrpcAddress ( ) , grpcDialOption )
} , volumeServer . ToGrpcAddress ( ) , false , grpcDialOption )
}
}
@ -220,7 +223,7 @@ func WithBrokerClient(streamingMode bool, broker ServerAddress, grpcDialOption g
return WithGrpcClient ( streamingMode , func ( grpcConnection * grpc . ClientConn ) error {
return WithGrpcClient ( streamingMode , func ( grpcConnection * grpc . ClientConn ) error {
client := mq_pb . NewSeaweedMessagingClient ( grpcConnection )
client := mq_pb . NewSeaweedMessagingClient ( grpcConnection )
return fn ( client )
return fn ( client )
} , broker . ToGrpcAddress ( ) , grpcDialOption )
} , broker . ToGrpcAddress ( ) , false , grpcDialOption )
}
}
@ -230,7 +233,7 @@ func WithOneOfGrpcMasterClients(streamingMode bool, masterGrpcAddresses map[stri
err = WithGrpcClient ( streamingMode , func ( grpcConnection * grpc . ClientConn ) error {
err = WithGrpcClient ( streamingMode , func ( grpcConnection * grpc . ClientConn ) error {
client := master_pb . NewSeaweedClient ( grpcConnection )
client := master_pb . NewSeaweedClient ( grpcConnection )
return fn ( client )
return fn ( client )
} , masterGrpcAddress . ToGrpcAddress ( ) , grpcDialOption )
} , masterGrpcAddress . ToGrpcAddress ( ) , false , grpcDialOption )
if err == nil {
if err == nil {
return nil
return nil
}
}
@ -244,7 +247,7 @@ func WithBrokerGrpcClient(streamingMode bool, brokerGrpcAddress string, grpcDial
return WithGrpcClient ( streamingMode , func ( grpcConnection * grpc . ClientConn ) error {
return WithGrpcClient ( streamingMode , func ( grpcConnection * grpc . ClientConn ) error {
client := mq_pb . NewSeaweedMessagingClient ( grpcConnection )
client := mq_pb . NewSeaweedMessagingClient ( grpcConnection )
return fn ( client )
return fn ( client )
} , brokerGrpcAddress , grpcDialOption )
} , brokerGrpcAddress , false , grpcDialOption )
}
}
@ -259,7 +262,7 @@ func WithGrpcFilerClient(streamingMode bool, filerGrpcAddress ServerAddress, grp
return WithGrpcClient ( streamingMode , func ( grpcConnection * grpc . ClientConn ) error {
return WithGrpcClient ( streamingMode , func ( grpcConnection * grpc . ClientConn ) error {
client := filer_pb . NewSeaweedFilerClient ( grpcConnection )
client := filer_pb . NewSeaweedFilerClient ( grpcConnection )
return fn ( client )
return fn ( client )
} , filerGrpcAddress . ToGrpcAddress ( ) , grpcDialOption )
} , filerGrpcAddress . ToGrpcAddress ( ) , false , grpcDialOption )
}
}
@ -269,7 +272,7 @@ func WithOneOfGrpcFilerClients(streamingMode bool, filerAddresses []ServerAddres
err = WithGrpcClient ( streamingMode , func ( grpcConnection * grpc . ClientConn ) error {
err = WithGrpcClient ( streamingMode , func ( grpcConnection * grpc . ClientConn ) error {
client := filer_pb . NewSeaweedFilerClient ( grpcConnection )
client := filer_pb . NewSeaweedFilerClient ( grpcConnection )
return fn ( client )
return fn ( client )
} , filerAddress . ToGrpcAddress ( ) , grpcDialOption )
} , filerAddress . ToGrpcAddress ( ) , false , grpcDialOption )
if err == nil {
if err == nil {
return nil
return nil
}
}