server.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. // Copyright 2018 The etcd Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package agent
  15. import (
  16. "math"
  17. "net"
  18. "os"
  19. "os/exec"
  20. "strings"
  21. "go.etcd.io/etcd/embed"
  22. "go.etcd.io/etcd/functional/rpcpb"
  23. "go.etcd.io/etcd/pkg/proxy"
  24. "go.uber.org/zap"
  25. "google.golang.org/grpc"
  26. )
  27. // Server implements "rpcpb.TransportServer"
  28. // and other etcd operations as an agent
  29. // no need to lock fields since request operations are
  30. // serialized in tester-side
  31. type Server struct {
  32. lg *zap.Logger
  33. grpcServer *grpc.Server
  34. network string
  35. address string
  36. ln net.Listener
  37. rpcpb.TransportServer
  38. last rpcpb.Operation
  39. *rpcpb.Member
  40. *rpcpb.Tester
  41. etcdServer *embed.Etcd
  42. etcdCmd *exec.Cmd
  43. etcdLogFile *os.File
  44. // forward incoming advertise URLs traffic to listen URLs
  45. advertiseClientPortToProxy map[int]proxy.Server
  46. advertisePeerPortToProxy map[int]proxy.Server
  47. }
  48. // NewServer returns a new agent server.
  49. func NewServer(
  50. lg *zap.Logger,
  51. network string,
  52. address string,
  53. ) *Server {
  54. return &Server{
  55. lg: lg,
  56. network: network,
  57. address: address,
  58. last: rpcpb.Operation_NOT_STARTED,
  59. advertiseClientPortToProxy: make(map[int]proxy.Server),
  60. advertisePeerPortToProxy: make(map[int]proxy.Server),
  61. }
  62. }
  63. const (
  64. maxRequestBytes = 1.5 * 1024 * 1024
  65. grpcOverheadBytes = 512 * 1024
  66. maxStreams = math.MaxUint32
  67. maxSendBytes = math.MaxInt32
  68. )
  69. // StartServe starts serving agent server.
  70. func (srv *Server) StartServe() error {
  71. var err error
  72. srv.ln, err = net.Listen(srv.network, srv.address)
  73. if err != nil {
  74. return err
  75. }
  76. var opts []grpc.ServerOption
  77. opts = append(opts, grpc.MaxRecvMsgSize(int(maxRequestBytes+grpcOverheadBytes)))
  78. opts = append(opts, grpc.MaxSendMsgSize(maxSendBytes))
  79. opts = append(opts, grpc.MaxConcurrentStreams(maxStreams))
  80. srv.grpcServer = grpc.NewServer(opts...)
  81. rpcpb.RegisterTransportServer(srv.grpcServer, srv)
  82. srv.lg.Info(
  83. "gRPC server started",
  84. zap.String("address", srv.address),
  85. zap.String("listener-address", srv.ln.Addr().String()),
  86. )
  87. err = srv.grpcServer.Serve(srv.ln)
  88. if err != nil && strings.Contains(err.Error(), "use of closed network connection") {
  89. srv.lg.Info(
  90. "gRPC server is shut down",
  91. zap.String("address", srv.address),
  92. zap.Error(err),
  93. )
  94. } else {
  95. srv.lg.Warn(
  96. "gRPC server returned with error",
  97. zap.String("address", srv.address),
  98. zap.Error(err),
  99. )
  100. }
  101. return err
  102. }
  103. // Stop stops serving gRPC server.
  104. func (srv *Server) Stop() {
  105. srv.lg.Info("gRPC server stopping", zap.String("address", srv.address))
  106. srv.grpcServer.Stop()
  107. srv.lg.Info("gRPC server stopped", zap.String("address", srv.address))
  108. }
  109. // Transport communicates with etcd tester.
  110. func (srv *Server) Transport(stream rpcpb.Transport_TransportServer) (reterr error) {
  111. errc := make(chan error, 1)
  112. go func() {
  113. for {
  114. var req *rpcpb.Request
  115. var err error
  116. req, err = stream.Recv()
  117. if err != nil {
  118. errc <- err
  119. // TODO: handle error and retry
  120. return
  121. }
  122. if req.Member != nil {
  123. srv.Member = req.Member
  124. }
  125. if req.Tester != nil {
  126. srv.Tester = req.Tester
  127. }
  128. var resp *rpcpb.Response
  129. resp, err = srv.handleTesterRequest(req)
  130. if err != nil {
  131. errc <- err
  132. // TODO: handle error and retry
  133. return
  134. }
  135. if err = stream.Send(resp); err != nil {
  136. errc <- err
  137. // TODO: handle error and retry
  138. return
  139. }
  140. }
  141. }()
  142. select {
  143. case reterr = <-errc:
  144. case <-stream.Context().Done():
  145. reterr = stream.Context().Err()
  146. }
  147. return reterr
  148. }