balancer_test.go 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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 balancer
  15. import (
  16. "context"
  17. "fmt"
  18. "strings"
  19. "testing"
  20. "time"
  21. "github.com/coreos/etcd/clientv3/balancer/picker"
  22. "github.com/coreos/etcd/clientv3/balancer/resolver/endpoint"
  23. pb "github.com/coreos/etcd/etcdserver/etcdserverpb"
  24. "github.com/coreos/etcd/pkg/mock/mockserver"
  25. "go.uber.org/zap"
  26. "google.golang.org/grpc"
  27. "google.golang.org/grpc/codes"
  28. "google.golang.org/grpc/peer"
  29. "google.golang.org/grpc/status"
  30. )
  31. // TestRoundRobinBalancedResolvableNoFailover ensures that
  32. // requests to a resolvable endpoint can be balanced between
  33. // multiple, if any, nodes. And there needs be no failover.
  34. func TestRoundRobinBalancedResolvableNoFailover(t *testing.T) {
  35. testCases := []struct {
  36. name string
  37. serverCount int
  38. reqN int
  39. network string
  40. }{
  41. {name: "rrBalanced_1", serverCount: 1, reqN: 5, network: "tcp"},
  42. {name: "rrBalanced_1_unix_sockets", serverCount: 1, reqN: 5, network: "unix"},
  43. {name: "rrBalanced_3", serverCount: 3, reqN: 7, network: "tcp"},
  44. {name: "rrBalanced_5", serverCount: 5, reqN: 10, network: "tcp"},
  45. }
  46. for _, tc := range testCases {
  47. t.Run(tc.name, func(t *testing.T) {
  48. ms, err := mockserver.StartMockServersOnNetwork(tc.serverCount, tc.network)
  49. if err != nil {
  50. t.Fatalf("failed to start mock servers: %v", err)
  51. }
  52. defer ms.Stop()
  53. var eps []string
  54. for _, svr := range ms.Servers {
  55. eps = append(eps, svr.ResolverAddress().Addr)
  56. }
  57. rsv, err := endpoint.NewResolverGroup("nofailover")
  58. if err != nil {
  59. t.Fatal(err)
  60. }
  61. defer rsv.Close()
  62. rsv.SetEndpoints(eps)
  63. name := genName()
  64. cfg := Config{
  65. Policy: picker.RoundrobinBalanced,
  66. Name: name,
  67. Logger: zap.NewExample(),
  68. }
  69. RegisterBuilder(cfg)
  70. conn, err := grpc.Dial(fmt.Sprintf("endpoint://nofailover/*"), grpc.WithInsecure(), grpc.WithBalancerName(name))
  71. if err != nil {
  72. t.Fatalf("failed to dial mock server: %v", err)
  73. }
  74. defer conn.Close()
  75. cli := pb.NewKVClient(conn)
  76. reqFunc := func(ctx context.Context) (picked string, err error) {
  77. var p peer.Peer
  78. _, err = cli.Range(ctx, &pb.RangeRequest{Key: []byte("/x")}, grpc.Peer(&p))
  79. if p.Addr != nil {
  80. picked = p.Addr.String()
  81. }
  82. return picked, err
  83. }
  84. prev, switches := "", 0
  85. for i := 0; i < tc.reqN; i++ {
  86. picked, err := reqFunc(context.Background())
  87. if err != nil {
  88. t.Fatalf("#%d: unexpected failure %v", i, err)
  89. }
  90. if prev == "" {
  91. prev = picked
  92. continue
  93. }
  94. if prev != picked {
  95. switches++
  96. }
  97. prev = picked
  98. }
  99. if tc.serverCount > 1 && switches < tc.reqN-3 { // -3 for initial resolutions
  100. // TODO: FIX ME
  101. t.Skipf("expected balanced loads for %d requests, got switches %d", tc.reqN, switches)
  102. }
  103. })
  104. }
  105. }
  106. // TestRoundRobinBalancedResolvableFailoverFromServerFail ensures that
  107. // loads be rebalanced while one server goes down and comes back.
  108. func TestRoundRobinBalancedResolvableFailoverFromServerFail(t *testing.T) {
  109. serverCount := 5
  110. ms, err := mockserver.StartMockServers(serverCount)
  111. if err != nil {
  112. t.Fatalf("failed to start mock servers: %s", err)
  113. }
  114. defer ms.Stop()
  115. var eps []string
  116. for _, svr := range ms.Servers {
  117. eps = append(eps, svr.ResolverAddress().Addr)
  118. }
  119. rsv, err := endpoint.NewResolverGroup("serverfail")
  120. if err != nil {
  121. t.Fatal(err)
  122. }
  123. defer rsv.Close()
  124. rsv.SetEndpoints(eps)
  125. name := genName()
  126. cfg := Config{
  127. Policy: picker.RoundrobinBalanced,
  128. Name: name,
  129. Logger: zap.NewExample(),
  130. }
  131. RegisterBuilder(cfg)
  132. conn, err := grpc.Dial(fmt.Sprintf("endpoint://serverfail/mock.server"), grpc.WithInsecure(), grpc.WithBalancerName(name))
  133. if err != nil {
  134. t.Fatalf("failed to dial mock server: %s", err)
  135. }
  136. defer conn.Close()
  137. cli := pb.NewKVClient(conn)
  138. reqFunc := func(ctx context.Context) (picked string, err error) {
  139. var p peer.Peer
  140. _, err = cli.Range(ctx, &pb.RangeRequest{Key: []byte("/x")}, grpc.Peer(&p))
  141. if p.Addr != nil {
  142. picked = p.Addr.String()
  143. }
  144. return picked, err
  145. }
  146. // stop first server, loads should be redistributed
  147. // stopped server should never be picked
  148. ms.StopAt(0)
  149. available := make(map[string]struct{})
  150. for i := 1; i < serverCount; i++ {
  151. available[eps[i]] = struct{}{}
  152. }
  153. reqN := 10
  154. prev, switches := "", 0
  155. for i := 0; i < reqN; i++ {
  156. picked, err := reqFunc(context.Background())
  157. if err != nil && strings.Contains(err.Error(), "transport is closing") {
  158. continue
  159. }
  160. if prev == "" { // first failover
  161. if eps[0] == picked {
  162. t.Fatalf("expected failover from %q, picked %q", eps[0], picked)
  163. }
  164. prev = picked
  165. continue
  166. }
  167. if _, ok := available[picked]; !ok {
  168. t.Fatalf("picked unavailable address %q (available %v)", picked, available)
  169. }
  170. if prev != picked {
  171. switches++
  172. }
  173. prev = picked
  174. }
  175. if switches < reqN-3 { // -3 for initial resolutions + failover
  176. // TODO: FIX ME!
  177. t.Skipf("expected balanced loads for %d requests, got switches %d", reqN, switches)
  178. }
  179. // now failed server comes back
  180. ms.StartAt(0)
  181. // enough time for reconnecting to recovered server
  182. time.Sleep(time.Second)
  183. prev, switches = "", 0
  184. recoveredAddr, recovered := eps[0], 0
  185. available[recoveredAddr] = struct{}{}
  186. for i := 0; i < 2*reqN; i++ {
  187. picked, err := reqFunc(context.Background())
  188. if err != nil {
  189. t.Fatalf("#%d: unexpected failure %v", i, err)
  190. }
  191. if prev == "" {
  192. prev = picked
  193. continue
  194. }
  195. if _, ok := available[picked]; !ok {
  196. t.Fatalf("#%d: picked unavailable address %q (available %v)", i, picked, available)
  197. }
  198. if prev != picked {
  199. switches++
  200. }
  201. if picked == recoveredAddr {
  202. recovered++
  203. }
  204. prev = picked
  205. }
  206. if switches < reqN-3 { // -3 for initial resolutions
  207. t.Fatalf("expected balanced loads for %d requests, got switches %d", reqN, switches)
  208. }
  209. if recovered < reqN/serverCount {
  210. t.Fatalf("recovered server %q got only %d requests", recoveredAddr, recovered)
  211. }
  212. }
  213. // TestRoundRobinBalancedResolvableFailoverFromRequestFail ensures that
  214. // loads be rebalanced while some requests are failed.
  215. func TestRoundRobinBalancedResolvableFailoverFromRequestFail(t *testing.T) {
  216. serverCount := 5
  217. ms, err := mockserver.StartMockServers(serverCount)
  218. if err != nil {
  219. t.Fatalf("failed to start mock servers: %s", err)
  220. }
  221. defer ms.Stop()
  222. var eps []string
  223. available := make(map[string]struct{})
  224. for _, svr := range ms.Servers {
  225. eps = append(eps, svr.ResolverAddress().Addr)
  226. available[svr.Address] = struct{}{}
  227. }
  228. rsv, err := endpoint.NewResolverGroup("requestfail")
  229. if err != nil {
  230. t.Fatal(err)
  231. }
  232. defer rsv.Close()
  233. rsv.SetEndpoints(eps)
  234. name := genName()
  235. cfg := Config{
  236. Policy: picker.RoundrobinBalanced,
  237. Name: name,
  238. Logger: zap.NewExample(),
  239. }
  240. RegisterBuilder(cfg)
  241. conn, err := grpc.Dial(fmt.Sprintf("endpoint://requestfail/mock.server"), grpc.WithInsecure(), grpc.WithBalancerName(name))
  242. if err != nil {
  243. t.Fatalf("failed to dial mock server: %s", err)
  244. }
  245. defer conn.Close()
  246. cli := pb.NewKVClient(conn)
  247. reqFunc := func(ctx context.Context) (picked string, err error) {
  248. var p peer.Peer
  249. _, err = cli.Range(ctx, &pb.RangeRequest{Key: []byte("/x")}, grpc.Peer(&p))
  250. if p.Addr != nil {
  251. picked = p.Addr.String()
  252. }
  253. return picked, err
  254. }
  255. reqN := 20
  256. prev, switches := "", 0
  257. for i := 0; i < reqN; i++ {
  258. ctx, cancel := context.WithCancel(context.Background())
  259. defer cancel()
  260. if i%2 == 0 {
  261. cancel()
  262. }
  263. picked, err := reqFunc(ctx)
  264. if i%2 == 0 {
  265. if s, ok := status.FromError(err); ok && s.Code() != codes.Canceled || picked != "" {
  266. t.Fatalf("#%d: expected %v, got %v", i, context.Canceled, err)
  267. }
  268. continue
  269. }
  270. if prev == "" && picked != "" {
  271. prev = picked
  272. continue
  273. }
  274. if _, ok := available[picked]; !ok {
  275. t.Fatalf("#%d: picked unavailable address %q (available %v)", i, picked, available)
  276. }
  277. if prev != picked {
  278. switches++
  279. }
  280. prev = picked
  281. }
  282. if switches < reqN/2-3 { // -3 for initial resolutions + failover
  283. t.Fatalf("expected balanced loads for %d requests, got switches %d", reqN, switches)
  284. }
  285. }