2016-09-20 01:11:57 +03:00
|
|
|
/*
|
|
|
|
*
|
2016-09-22 03:54:48 +03:00
|
|
|
* Copyright 2016, Google Inc.
|
2016-09-20 01:11:57 +03:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are
|
|
|
|
* met:
|
|
|
|
*
|
|
|
|
* * Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* * Redistributions in binary form must reproduce the above
|
|
|
|
* copyright notice, this list of conditions and the following disclaimer
|
|
|
|
* in the documentation and/or other materials provided with the
|
|
|
|
* distribution.
|
|
|
|
* * Neither the name of Google Inc. nor the names of its
|
|
|
|
* contributors may be used to endorse or promote products derived from
|
|
|
|
* this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
package grpclb
|
|
|
|
|
|
|
|
import (
|
2016-10-06 01:51:45 +03:00
|
|
|
"errors"
|
2016-09-20 01:11:57 +03:00
|
|
|
"fmt"
|
2016-10-06 01:51:45 +03:00
|
|
|
"io"
|
2016-09-20 01:11:57 +03:00
|
|
|
"net"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
2016-10-08 02:57:44 +03:00
|
|
|
"time"
|
2016-09-20 01:11:57 +03:00
|
|
|
|
|
|
|
"golang.org/x/net/context"
|
|
|
|
"google.golang.org/grpc"
|
|
|
|
"google.golang.org/grpc/codes"
|
2016-10-06 01:51:45 +03:00
|
|
|
"google.golang.org/grpc/credentials"
|
2016-10-14 02:44:27 +03:00
|
|
|
hwpb "google.golang.org/grpc/examples/helloworld/helloworld"
|
2016-09-20 01:11:57 +03:00
|
|
|
lbpb "google.golang.org/grpc/grpclb/grpc_lb_v1"
|
2016-10-14 02:44:27 +03:00
|
|
|
"google.golang.org/grpc/metadata"
|
2016-09-20 01:11:57 +03:00
|
|
|
"google.golang.org/grpc/naming"
|
|
|
|
)
|
|
|
|
|
2016-10-06 01:51:45 +03:00
|
|
|
var (
|
2016-10-14 02:44:27 +03:00
|
|
|
lbsn = "bar.com"
|
|
|
|
besn = "foo.com"
|
|
|
|
lbToken = "iamatoken"
|
2016-10-06 01:51:45 +03:00
|
|
|
)
|
|
|
|
|
2016-09-20 01:11:57 +03:00
|
|
|
type testWatcher struct {
|
|
|
|
// the channel to receives name resolution updates
|
|
|
|
update chan *naming.Update
|
|
|
|
// the side channel to get to know how many updates in a batch
|
|
|
|
side chan int
|
|
|
|
// the channel to notifiy update injector that the update reading is done
|
|
|
|
readDone chan int
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *testWatcher) Next() (updates []*naming.Update, err error) {
|
2016-09-22 03:54:48 +03:00
|
|
|
n, ok := <-w.side
|
|
|
|
if !ok {
|
2016-09-20 01:11:57 +03:00
|
|
|
return nil, fmt.Errorf("w.side is closed")
|
|
|
|
}
|
|
|
|
for i := 0; i < n; i++ {
|
2016-09-22 03:54:48 +03:00
|
|
|
u, ok := <-w.update
|
|
|
|
if !ok {
|
|
|
|
break
|
|
|
|
}
|
2016-09-20 01:11:57 +03:00
|
|
|
if u != nil {
|
|
|
|
updates = append(updates, u)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
w.readDone <- 0
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *testWatcher) Close() {
|
|
|
|
}
|
|
|
|
|
|
|
|
// Inject naming resolution updates to the testWatcher.
|
|
|
|
func (w *testWatcher) inject(updates []*naming.Update) {
|
|
|
|
w.side <- len(updates)
|
|
|
|
for _, u := range updates {
|
|
|
|
w.update <- u
|
|
|
|
}
|
|
|
|
<-w.readDone
|
|
|
|
}
|
|
|
|
|
|
|
|
type testNameResolver struct {
|
|
|
|
w *testWatcher
|
|
|
|
addr string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *testNameResolver) Resolve(target string) (naming.Watcher, error) {
|
|
|
|
r.w = &testWatcher{
|
|
|
|
update: make(chan *naming.Update, 1),
|
|
|
|
side: make(chan int, 1),
|
|
|
|
readDone: make(chan int),
|
|
|
|
}
|
|
|
|
r.w.side <- 1
|
|
|
|
r.w.update <- &naming.Update{
|
|
|
|
Op: naming.Add,
|
|
|
|
Addr: r.addr,
|
2016-10-06 01:51:45 +03:00
|
|
|
Metadata: &Metadata{
|
|
|
|
AddrType: GRPCLB,
|
|
|
|
ServerName: lbsn,
|
|
|
|
},
|
2016-09-20 01:11:57 +03:00
|
|
|
}
|
|
|
|
go func() {
|
|
|
|
<-r.w.readDone
|
|
|
|
}()
|
|
|
|
return r.w, nil
|
|
|
|
}
|
|
|
|
|
2016-10-06 01:51:45 +03:00
|
|
|
type serverNameCheckCreds struct {
|
|
|
|
expected string
|
|
|
|
sn string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *serverNameCheckCreds) ServerHandshake(rawConn net.Conn) (net.Conn, credentials.AuthInfo, error) {
|
|
|
|
if _, err := io.WriteString(rawConn, c.sn); err != nil {
|
2016-10-08 02:57:44 +03:00
|
|
|
fmt.Printf("Failed to write the server name %s to the client %v", c.sn, err)
|
2016-10-06 01:51:45 +03:00
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
return rawConn, nil, nil
|
|
|
|
}
|
|
|
|
func (c *serverNameCheckCreds) ClientHandshake(ctx context.Context, addr string, rawConn net.Conn) (net.Conn, credentials.AuthInfo, error) {
|
|
|
|
b := make([]byte, len(c.expected))
|
|
|
|
if _, err := rawConn.Read(b); err != nil {
|
2016-10-08 02:57:44 +03:00
|
|
|
fmt.Printf("Failed to read the server name from the server %v", err)
|
2016-10-06 01:51:45 +03:00
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
if c.expected != string(b) {
|
2016-10-08 02:57:44 +03:00
|
|
|
fmt.Printf("Read the server name %s want %s", string(b), c.expected)
|
2016-10-06 01:51:45 +03:00
|
|
|
return nil, nil, errors.New("received unexpected server name")
|
|
|
|
}
|
|
|
|
return rawConn, nil, nil
|
|
|
|
}
|
|
|
|
func (c *serverNameCheckCreds) Info() credentials.ProtocolInfo {
|
|
|
|
return credentials.ProtocolInfo{}
|
|
|
|
}
|
|
|
|
func (c *serverNameCheckCreds) Clone() credentials.TransportCredentials {
|
|
|
|
return &serverNameCheckCreds{
|
|
|
|
expected: c.expected,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
func (c *serverNameCheckCreds) OverrideServerName(s string) error {
|
|
|
|
c.expected = s
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-09-20 01:11:57 +03:00
|
|
|
type remoteBalancer struct {
|
2016-11-17 02:26:18 +03:00
|
|
|
sls []*lbpb.ServerList
|
|
|
|
intervals []time.Duration
|
|
|
|
done chan struct{}
|
2016-09-20 01:11:57 +03:00
|
|
|
}
|
|
|
|
|
2016-11-17 02:26:18 +03:00
|
|
|
func newRemoteBalancer(sls []*lbpb.ServerList, intervals []time.Duration) *remoteBalancer {
|
2016-09-20 01:11:57 +03:00
|
|
|
return &remoteBalancer{
|
2016-11-17 02:26:18 +03:00
|
|
|
sls: sls,
|
|
|
|
intervals: intervals,
|
|
|
|
done: make(chan struct{}),
|
2016-09-20 01:11:57 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *remoteBalancer) stop() {
|
|
|
|
close(b.done)
|
|
|
|
}
|
2016-10-06 01:51:45 +03:00
|
|
|
|
2016-09-20 01:11:57 +03:00
|
|
|
func (b *remoteBalancer) BalanceLoad(stream lbpb.LoadBalancer_BalanceLoadServer) error {
|
2017-03-28 22:10:54 +03:00
|
|
|
req, err := stream.Recv()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
initReq := req.GetInitialRequest()
|
|
|
|
if initReq.Name != besn {
|
|
|
|
return grpc.Errorf(codes.InvalidArgument, "invalid service name: %v", initReq.Name)
|
|
|
|
}
|
2016-09-20 01:11:57 +03:00
|
|
|
resp := &lbpb.LoadBalanceResponse{
|
|
|
|
LoadBalanceResponseType: &lbpb.LoadBalanceResponse_InitialResponse{
|
|
|
|
InitialResponse: new(lbpb.InitialLoadBalanceResponse),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
if err := stream.Send(resp); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-11-17 02:26:18 +03:00
|
|
|
for k, v := range b.sls {
|
|
|
|
time.Sleep(b.intervals[k])
|
|
|
|
resp = &lbpb.LoadBalanceResponse{
|
|
|
|
LoadBalanceResponseType: &lbpb.LoadBalanceResponse_ServerList{
|
|
|
|
ServerList: v,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
if err := stream.Send(resp); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-09-20 01:11:57 +03:00
|
|
|
}
|
|
|
|
<-b.done
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-10-14 02:44:27 +03:00
|
|
|
type helloServer struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *helloServer) SayHello(ctx context.Context, in *hwpb.HelloRequest) (*hwpb.HelloReply, error) {
|
2017-04-07 21:54:56 +03:00
|
|
|
md, ok := metadata.FromIncomingContext(ctx)
|
2016-10-14 02:44:27 +03:00
|
|
|
if !ok {
|
|
|
|
return nil, grpc.Errorf(codes.Internal, "failed to receive metadata")
|
|
|
|
}
|
|
|
|
if md == nil || md["lb-token"][0] != lbToken {
|
|
|
|
return nil, grpc.Errorf(codes.Internal, "received unexpected metadata: %v", md)
|
|
|
|
}
|
|
|
|
return &hwpb.HelloReply{
|
|
|
|
Message: "Hello " + in.Name,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2016-10-06 01:51:45 +03:00
|
|
|
func startBackends(t *testing.T, sn string, lis ...net.Listener) (servers []*grpc.Server) {
|
2016-09-20 01:11:57 +03:00
|
|
|
for _, l := range lis {
|
2016-10-06 01:51:45 +03:00
|
|
|
creds := &serverNameCheckCreds{
|
|
|
|
sn: sn,
|
|
|
|
}
|
|
|
|
s := grpc.NewServer(grpc.Creds(creds))
|
2016-10-14 02:44:27 +03:00
|
|
|
hwpb.RegisterGreeterServer(s, &helloServer{})
|
2016-09-20 01:11:57 +03:00
|
|
|
servers = append(servers, s)
|
|
|
|
go func(s *grpc.Server, l net.Listener) {
|
|
|
|
s.Serve(l)
|
|
|
|
}(s, l)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func stopBackends(servers []*grpc.Server) {
|
|
|
|
for _, s := range servers {
|
|
|
|
s.Stop()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-22 03:54:48 +03:00
|
|
|
func TestGRPCLB(t *testing.T) {
|
2016-09-20 01:11:57 +03:00
|
|
|
// Start a backend.
|
|
|
|
beLis, err := net.Listen("tcp", "localhost:0")
|
|
|
|
if err != nil {
|
2016-09-22 03:54:48 +03:00
|
|
|
t.Fatalf("Failed to listen %v", err)
|
2016-09-20 01:11:57 +03:00
|
|
|
}
|
2016-10-06 01:51:45 +03:00
|
|
|
beAddr := strings.Split(beLis.Addr().String(), ":")
|
|
|
|
bePort, err := strconv.Atoi(beAddr[1])
|
|
|
|
backends := startBackends(t, besn, beLis)
|
2016-09-20 01:11:57 +03:00
|
|
|
defer stopBackends(backends)
|
2016-10-06 01:51:45 +03:00
|
|
|
|
2016-09-20 01:11:57 +03:00
|
|
|
// Start a load balancer.
|
2016-10-06 01:51:45 +03:00
|
|
|
lbLis, err := net.Listen("tcp", "localhost:0")
|
2016-09-20 01:11:57 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to create the listener for the load balancer %v", err)
|
|
|
|
}
|
2016-10-06 01:51:45 +03:00
|
|
|
lbCreds := &serverNameCheckCreds{
|
|
|
|
sn: lbsn,
|
|
|
|
}
|
|
|
|
lb := grpc.NewServer(grpc.Creds(lbCreds))
|
2016-09-20 01:11:57 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to generate the port number %v", err)
|
|
|
|
}
|
|
|
|
be := &lbpb.Server{
|
2017-03-28 22:10:54 +03:00
|
|
|
IpAddress: beLis.Addr().(*net.TCPAddr).IP,
|
2016-10-14 02:44:27 +03:00
|
|
|
Port: int32(bePort),
|
|
|
|
LoadBalanceToken: lbToken,
|
2016-09-20 01:11:57 +03:00
|
|
|
}
|
|
|
|
var bes []*lbpb.Server
|
|
|
|
bes = append(bes, be)
|
|
|
|
sl := &lbpb.ServerList{
|
|
|
|
Servers: bes,
|
|
|
|
}
|
2016-11-17 02:26:18 +03:00
|
|
|
sls := []*lbpb.ServerList{sl}
|
|
|
|
intervals := []time.Duration{0}
|
|
|
|
ls := newRemoteBalancer(sls, intervals)
|
2016-09-20 01:11:57 +03:00
|
|
|
lbpb.RegisterLoadBalancerServer(lb, ls)
|
|
|
|
go func() {
|
2016-10-06 01:51:45 +03:00
|
|
|
lb.Serve(lbLis)
|
2016-09-20 01:11:57 +03:00
|
|
|
}()
|
|
|
|
defer func() {
|
|
|
|
ls.stop()
|
|
|
|
lb.Stop()
|
|
|
|
}()
|
2016-10-06 01:51:45 +03:00
|
|
|
creds := serverNameCheckCreds{
|
|
|
|
expected: besn,
|
|
|
|
}
|
2016-10-08 02:57:44 +03:00
|
|
|
ctx, _ := context.WithTimeout(context.Background(), 10*time.Second)
|
|
|
|
cc, err := grpc.DialContext(ctx, besn, grpc.WithBalancer(Balancer(&testNameResolver{
|
2016-10-06 01:51:45 +03:00
|
|
|
addr: lbLis.Addr().String(),
|
|
|
|
})), grpc.WithBlock(), grpc.WithTransportCredentials(&creds))
|
2016-09-20 01:11:57 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to dial to the backend %v", err)
|
|
|
|
}
|
2016-10-14 02:44:27 +03:00
|
|
|
helloC := hwpb.NewGreeterClient(cc)
|
|
|
|
if _, err := helloC.SayHello(context.Background(), &hwpb.HelloRequest{Name: "grpc"}); err != nil {
|
|
|
|
t.Fatalf("%v.SayHello(_, _) = _, %v, want _, <nil>", helloC, err)
|
2016-09-20 01:11:57 +03:00
|
|
|
}
|
|
|
|
cc.Close()
|
|
|
|
}
|
2016-10-18 23:42:15 +03:00
|
|
|
|
|
|
|
func TestDropRequest(t *testing.T) {
|
|
|
|
// Start 2 backends.
|
|
|
|
beLis1, err := net.Listen("tcp", "localhost:0")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to listen %v", err)
|
|
|
|
}
|
|
|
|
beAddr1 := strings.Split(beLis1.Addr().String(), ":")
|
|
|
|
bePort1, err := strconv.Atoi(beAddr1[1])
|
|
|
|
|
|
|
|
beLis2, err := net.Listen("tcp", "localhost:0")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to listen %v", err)
|
|
|
|
}
|
|
|
|
beAddr2 := strings.Split(beLis2.Addr().String(), ":")
|
|
|
|
bePort2, err := strconv.Atoi(beAddr2[1])
|
|
|
|
|
|
|
|
backends := startBackends(t, besn, beLis1, beLis2)
|
|
|
|
defer stopBackends(backends)
|
|
|
|
|
|
|
|
// Start a load balancer.
|
|
|
|
lbLis, err := net.Listen("tcp", "localhost:0")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to create the listener for the load balancer %v", err)
|
|
|
|
}
|
|
|
|
lbCreds := &serverNameCheckCreds{
|
|
|
|
sn: lbsn,
|
|
|
|
}
|
|
|
|
lb := grpc.NewServer(grpc.Creds(lbCreds))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to generate the port number %v", err)
|
|
|
|
}
|
2017-03-21 21:35:53 +03:00
|
|
|
sls := []*lbpb.ServerList{{
|
|
|
|
Servers: []*lbpb.Server{{
|
2017-03-28 22:10:54 +03:00
|
|
|
IpAddress: beLis1.Addr().(*net.TCPAddr).IP,
|
2017-03-21 21:35:53 +03:00
|
|
|
Port: int32(bePort1),
|
|
|
|
LoadBalanceToken: lbToken,
|
|
|
|
DropRequest: true,
|
|
|
|
}, {
|
2017-03-28 22:10:54 +03:00
|
|
|
IpAddress: beLis2.Addr().(*net.TCPAddr).IP,
|
2017-03-21 21:35:53 +03:00
|
|
|
Port: int32(bePort2),
|
|
|
|
LoadBalanceToken: lbToken,
|
|
|
|
DropRequest: false,
|
|
|
|
}},
|
|
|
|
}}
|
2016-11-17 02:26:18 +03:00
|
|
|
intervals := []time.Duration{0}
|
|
|
|
ls := newRemoteBalancer(sls, intervals)
|
2016-10-18 23:42:15 +03:00
|
|
|
lbpb.RegisterLoadBalancerServer(lb, ls)
|
|
|
|
go func() {
|
|
|
|
lb.Serve(lbLis)
|
|
|
|
}()
|
|
|
|
defer func() {
|
|
|
|
ls.stop()
|
|
|
|
lb.Stop()
|
|
|
|
}()
|
|
|
|
creds := serverNameCheckCreds{
|
|
|
|
expected: besn,
|
|
|
|
}
|
|
|
|
ctx, _ := context.WithTimeout(context.Background(), 10*time.Second)
|
|
|
|
cc, err := grpc.DialContext(ctx, besn, grpc.WithBalancer(Balancer(&testNameResolver{
|
|
|
|
addr: lbLis.Addr().String(),
|
|
|
|
})), grpc.WithBlock(), grpc.WithTransportCredentials(&creds))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to dial to the backend %v", err)
|
|
|
|
}
|
|
|
|
helloC := hwpb.NewGreeterClient(cc)
|
2017-03-21 21:35:53 +03:00
|
|
|
// The 1st, non-fail-fast RPC should succeed. This ensures both server
|
|
|
|
// connections are made, because the first one has DropRequest set to true.
|
2016-10-18 23:42:15 +03:00
|
|
|
if _, err := helloC.SayHello(context.Background(), &hwpb.HelloRequest{Name: "grpc"}, grpc.FailFast(false)); err != nil {
|
|
|
|
t.Fatalf("%v.SayHello(_, _) = _, %v, want _, <nil>", helloC, err)
|
|
|
|
}
|
2017-03-21 21:35:53 +03:00
|
|
|
for i := 0; i < 3; i++ {
|
|
|
|
// Odd fail-fast RPCs should fail, because the 1st backend has DropRequest
|
|
|
|
// set to true.
|
|
|
|
if _, err := helloC.SayHello(context.Background(), &hwpb.HelloRequest{Name: "grpc"}); grpc.Code(err) != codes.Unavailable {
|
|
|
|
t.Fatalf("%v.SayHello(_, _) = _, %v, want _, %s", helloC, err, codes.Unavailable)
|
|
|
|
}
|
|
|
|
// Even fail-fast RPCs should succeed since they choose the
|
|
|
|
// non-drop-request backend according to the round robin policy.
|
|
|
|
if _, err := helloC.SayHello(context.Background(), &hwpb.HelloRequest{Name: "grpc"}); err != nil {
|
|
|
|
t.Fatalf("%v.SayHello(_, _) = _, %v, want _, <nil>", helloC, err)
|
|
|
|
}
|
|
|
|
}
|
2016-10-18 23:42:15 +03:00
|
|
|
cc.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDropRequestFailedNonFailFast(t *testing.T) {
|
|
|
|
// Start a backend.
|
|
|
|
beLis, err := net.Listen("tcp", "localhost:0")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to listen %v", err)
|
|
|
|
}
|
|
|
|
beAddr := strings.Split(beLis.Addr().String(), ":")
|
|
|
|
bePort, err := strconv.Atoi(beAddr[1])
|
|
|
|
backends := startBackends(t, besn, beLis)
|
|
|
|
defer stopBackends(backends)
|
|
|
|
|
|
|
|
// Start a load balancer.
|
|
|
|
lbLis, err := net.Listen("tcp", "localhost:0")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to create the listener for the load balancer %v", err)
|
|
|
|
}
|
|
|
|
lbCreds := &serverNameCheckCreds{
|
|
|
|
sn: lbsn,
|
|
|
|
}
|
|
|
|
lb := grpc.NewServer(grpc.Creds(lbCreds))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to generate the port number %v", err)
|
|
|
|
}
|
|
|
|
be := &lbpb.Server{
|
2017-03-28 22:10:54 +03:00
|
|
|
IpAddress: beLis.Addr().(*net.TCPAddr).IP,
|
2016-10-18 23:42:15 +03:00
|
|
|
Port: int32(bePort),
|
|
|
|
LoadBalanceToken: lbToken,
|
|
|
|
DropRequest: true,
|
|
|
|
}
|
|
|
|
var bes []*lbpb.Server
|
|
|
|
bes = append(bes, be)
|
|
|
|
sl := &lbpb.ServerList{
|
|
|
|
Servers: bes,
|
|
|
|
}
|
2016-11-17 02:26:18 +03:00
|
|
|
sls := []*lbpb.ServerList{sl}
|
|
|
|
intervals := []time.Duration{0}
|
|
|
|
ls := newRemoteBalancer(sls, intervals)
|
2016-10-18 23:42:15 +03:00
|
|
|
lbpb.RegisterLoadBalancerServer(lb, ls)
|
|
|
|
go func() {
|
|
|
|
lb.Serve(lbLis)
|
|
|
|
}()
|
|
|
|
defer func() {
|
|
|
|
ls.stop()
|
|
|
|
lb.Stop()
|
|
|
|
}()
|
|
|
|
creds := serverNameCheckCreds{
|
|
|
|
expected: besn,
|
|
|
|
}
|
|
|
|
ctx, _ := context.WithTimeout(context.Background(), 10*time.Second)
|
|
|
|
cc, err := grpc.DialContext(ctx, besn, grpc.WithBalancer(Balancer(&testNameResolver{
|
|
|
|
addr: lbLis.Addr().String(),
|
|
|
|
})), grpc.WithBlock(), grpc.WithTransportCredentials(&creds))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to dial to the backend %v", err)
|
|
|
|
}
|
|
|
|
helloC := hwpb.NewGreeterClient(cc)
|
|
|
|
ctx, _ = context.WithTimeout(context.Background(), 10*time.Millisecond)
|
|
|
|
if _, err := helloC.SayHello(ctx, &hwpb.HelloRequest{Name: "grpc"}, grpc.FailFast(false)); grpc.Code(err) != codes.DeadlineExceeded {
|
|
|
|
t.Fatalf("%v.SayHello(_, _) = _, %v, want _, %s", helloC, err, codes.DeadlineExceeded)
|
|
|
|
}
|
|
|
|
cc.Close()
|
|
|
|
}
|
2016-11-17 02:26:18 +03:00
|
|
|
|
|
|
|
func TestServerExpiration(t *testing.T) {
|
|
|
|
// Start a backend.
|
|
|
|
beLis, err := net.Listen("tcp", "localhost:0")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to listen %v", err)
|
|
|
|
}
|
|
|
|
beAddr := strings.Split(beLis.Addr().String(), ":")
|
|
|
|
bePort, err := strconv.Atoi(beAddr[1])
|
|
|
|
backends := startBackends(t, besn, beLis)
|
|
|
|
defer stopBackends(backends)
|
|
|
|
|
|
|
|
// Start a load balancer.
|
|
|
|
lbLis, err := net.Listen("tcp", "localhost:0")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to create the listener for the load balancer %v", err)
|
|
|
|
}
|
|
|
|
lbCreds := &serverNameCheckCreds{
|
|
|
|
sn: lbsn,
|
|
|
|
}
|
|
|
|
lb := grpc.NewServer(grpc.Creds(lbCreds))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to generate the port number %v", err)
|
|
|
|
}
|
|
|
|
be := &lbpb.Server{
|
2017-03-28 22:10:54 +03:00
|
|
|
IpAddress: beLis.Addr().(*net.TCPAddr).IP,
|
2016-11-17 02:26:18 +03:00
|
|
|
Port: int32(bePort),
|
|
|
|
LoadBalanceToken: lbToken,
|
|
|
|
}
|
|
|
|
var bes []*lbpb.Server
|
|
|
|
bes = append(bes, be)
|
|
|
|
exp := &lbpb.Duration{
|
|
|
|
Seconds: 0,
|
|
|
|
Nanos: 100000000, // 100ms
|
|
|
|
}
|
|
|
|
var sls []*lbpb.ServerList
|
|
|
|
sl := &lbpb.ServerList{
|
|
|
|
Servers: bes,
|
|
|
|
ExpirationInterval: exp,
|
|
|
|
}
|
|
|
|
sls = append(sls, sl)
|
|
|
|
sl = &lbpb.ServerList{
|
|
|
|
Servers: bes,
|
|
|
|
}
|
|
|
|
sls = append(sls, sl)
|
|
|
|
var intervals []time.Duration
|
|
|
|
intervals = append(intervals, 0)
|
|
|
|
intervals = append(intervals, 500*time.Millisecond)
|
|
|
|
ls := newRemoteBalancer(sls, intervals)
|
|
|
|
lbpb.RegisterLoadBalancerServer(lb, ls)
|
|
|
|
go func() {
|
|
|
|
lb.Serve(lbLis)
|
|
|
|
}()
|
|
|
|
defer func() {
|
|
|
|
ls.stop()
|
|
|
|
lb.Stop()
|
|
|
|
}()
|
|
|
|
creds := serverNameCheckCreds{
|
|
|
|
expected: besn,
|
|
|
|
}
|
|
|
|
ctx, _ := context.WithTimeout(context.Background(), 10*time.Second)
|
|
|
|
cc, err := grpc.DialContext(ctx, besn, grpc.WithBalancer(Balancer(&testNameResolver{
|
|
|
|
addr: lbLis.Addr().String(),
|
|
|
|
})), grpc.WithBlock(), grpc.WithTransportCredentials(&creds))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to dial to the backend %v", err)
|
|
|
|
}
|
|
|
|
helloC := hwpb.NewGreeterClient(cc)
|
|
|
|
if _, err := helloC.SayHello(context.Background(), &hwpb.HelloRequest{Name: "grpc"}); err != nil {
|
|
|
|
t.Fatalf("%v.SayHello(_, _) = _, %v, want _, <nil>", helloC, err)
|
|
|
|
}
|
|
|
|
// Sleep and wake up when the first server list gets expired.
|
|
|
|
time.Sleep(150 * time.Millisecond)
|
|
|
|
if _, err := helloC.SayHello(context.Background(), &hwpb.HelloRequest{Name: "grpc"}); grpc.Code(err) != codes.Unavailable {
|
|
|
|
t.Fatalf("%v.SayHello(_, _) = _, %v, want _, %s", helloC, err, codes.Unavailable)
|
|
|
|
}
|
|
|
|
// A non-failfast rpc should be succeeded after the second server list is received from
|
|
|
|
// the remote load balancer.
|
|
|
|
if _, err := helloC.SayHello(context.Background(), &hwpb.HelloRequest{Name: "grpc"}, grpc.FailFast(false)); err != nil {
|
|
|
|
t.Fatalf("%v.SayHello(_, _) = _, %v, want _, <nil>", helloC, err)
|
|
|
|
}
|
|
|
|
cc.Close()
|
|
|
|
}
|