server.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. "github.com/coreos/etcd/functional/rpcpb"
  22. "github.com/coreos/etcd/pkg/proxy"
  23. "go.uber.org/zap"
  24. "google.golang.org/grpc"
  25. )
  26. // Server implements "rpcpb.TransportServer"
  27. // and other etcd operations as an agent
  28. // no need to lock fields since request operations are
  29. // serialized in tester-side
  30. type Server struct {
  31. grpcServer *grpc.Server
  32. lg *zap.Logger
  33. network string
  34. address string
  35. ln net.Listener
  36. rpcpb.TransportServer
  37. last rpcpb.Operation
  38. *rpcpb.Member
  39. *rpcpb.Tester
  40. etcdCmd *exec.Cmd
  41. etcdLogFile *os.File
  42. // forward incoming advertise URLs traffic to listen URLs
  43. advertiseClientPortToProxy map[int]proxy.Server
  44. advertisePeerPortToProxy map[int]proxy.Server
  45. }
  46. // NewServer returns a new agent server.
  47. func NewServer(
  48. lg *zap.Logger,
  49. network string,
  50. address string,
  51. ) *Server {
  52. return &Server{
  53. lg: lg,
  54. network: network,
  55. address: address,
  56. last: rpcpb.Operation_NOT_STARTED,
  57. advertiseClientPortToProxy: make(map[int]proxy.Server),
  58. advertisePeerPortToProxy: make(map[int]proxy.Server),
  59. }
  60. }
  61. const (
  62. maxRequestBytes = 1.5 * 1024 * 1024
  63. grpcOverheadBytes = 512 * 1024
  64. maxStreams = math.MaxUint32
  65. maxSendBytes = math.MaxInt32
  66. )
  67. // StartServe starts serving agent server.
  68. func (srv *Server) StartServe() error {
  69. var err error
  70. srv.ln, err = net.Listen(srv.network, srv.address)
  71. if err != nil {
  72. return err
  73. }
  74. var opts []grpc.ServerOption
  75. opts = append(opts, grpc.MaxRecvMsgSize(int(maxRequestBytes+grpcOverheadBytes)))
  76. opts = append(opts, grpc.MaxSendMsgSize(maxSendBytes))
  77. opts = append(opts, grpc.MaxConcurrentStreams(maxStreams))
  78. srv.grpcServer = grpc.NewServer(opts...)
  79. rpcpb.RegisterTransportServer(srv.grpcServer, srv)
  80. srv.lg.Info(
  81. "gRPC server started",
  82. zap.String("address", srv.address),
  83. zap.String("listener-address", srv.ln.Addr().String()),
  84. )
  85. err = srv.grpcServer.Serve(srv.ln)
  86. if err != nil && strings.Contains(err.Error(), "use of closed network connection") {
  87. srv.lg.Info(
  88. "gRPC server is shut down",
  89. zap.String("address", srv.address),
  90. zap.Error(err),
  91. )
  92. } else {
  93. srv.lg.Warn(
  94. "gRPC server returned with error",
  95. zap.String("address", srv.address),
  96. zap.Error(err),
  97. )
  98. }
  99. return err
  100. }
  101. // Stop stops serving gRPC server.
  102. func (srv *Server) Stop() {
  103. srv.lg.Info("gRPC server stopping", zap.String("address", srv.address))
  104. srv.grpcServer.Stop()
  105. srv.lg.Info("gRPC server stopped", zap.String("address", srv.address))
  106. }
  107. // Transport communicates with etcd tester.
  108. func (srv *Server) Transport(stream rpcpb.Transport_TransportServer) (err error) {
  109. errc := make(chan error)
  110. go func() {
  111. for {
  112. var req *rpcpb.Request
  113. req, err = stream.Recv()
  114. if err != nil {
  115. errc <- err
  116. // TODO: handle error and retry
  117. return
  118. }
  119. if req.Member != nil {
  120. srv.Member = req.Member
  121. }
  122. if req.Tester != nil {
  123. srv.Tester = req.Tester
  124. }
  125. var resp *rpcpb.Response
  126. resp, err = srv.handleTesterRequest(req)
  127. if err != nil {
  128. errc <- err
  129. // TODO: handle error and retry
  130. return
  131. }
  132. if err = stream.Send(resp); err != nil {
  133. errc <- err
  134. // TODO: handle error and retry
  135. return
  136. }
  137. }
  138. }()
  139. select {
  140. case err = <-errc:
  141. case <-stream.Context().Done():
  142. err = stream.Context().Err()
  143. }
  144. return err
  145. }