policies_test.go 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. // Copyright (c) 2015 The gocql Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package gocql
  5. import (
  6. "fmt"
  7. "net"
  8. "testing"
  9. "github.com/hailocab/go-hostpool"
  10. )
  11. // Tests of the round-robin host selection policy implementation
  12. func TestRoundRobinHostPolicy(t *testing.T) {
  13. policy := RoundRobinHostPolicy()
  14. hosts := [...]*HostInfo{
  15. {hostId: "0", peer: net.IPv4(0, 0, 0, 1)},
  16. {hostId: "1", peer: net.IPv4(0, 0, 0, 2)},
  17. }
  18. for _, host := range hosts {
  19. policy.AddHost(host)
  20. }
  21. // interleaved iteration should always increment the host
  22. iterA := policy.Pick(nil)
  23. if actual := iterA(); actual.Info() != hosts[0] {
  24. t.Errorf("Expected hosts[0] but was hosts[%s]", actual.Info().HostID())
  25. }
  26. iterB := policy.Pick(nil)
  27. if actual := iterB(); actual.Info() != hosts[1] {
  28. t.Errorf("Expected hosts[1] but was hosts[%s]", actual.Info().HostID())
  29. }
  30. if actual := iterB(); actual.Info() != hosts[0] {
  31. t.Errorf("Expected hosts[0] but was hosts[%s]", actual.Info().HostID())
  32. }
  33. if actual := iterA(); actual.Info() != hosts[1] {
  34. t.Errorf("Expected hosts[1] but was hosts[%s]", actual.Info().HostID())
  35. }
  36. iterC := policy.Pick(nil)
  37. if actual := iterC(); actual.Info() != hosts[0] {
  38. t.Errorf("Expected hosts[0] but was hosts[%s]", actual.Info().HostID())
  39. }
  40. if actual := iterC(); actual.Info() != hosts[1] {
  41. t.Errorf("Expected hosts[1] but was hosts[%s]", actual.Info().HostID())
  42. }
  43. }
  44. // Tests of the token-aware host selection policy implementation with a
  45. // round-robin host selection policy fallback.
  46. func TestTokenAwareHostPolicy(t *testing.T) {
  47. policy := TokenAwareHostPolicy(RoundRobinHostPolicy())
  48. query := &Query{}
  49. iter := policy.Pick(nil)
  50. if iter == nil {
  51. t.Fatal("host iterator was nil")
  52. }
  53. actual := iter()
  54. if actual != nil {
  55. t.Fatalf("expected nil from iterator, but was %v", actual)
  56. }
  57. // set the hosts
  58. hosts := [...]*HostInfo{
  59. {peer: net.IPv4(10, 0, 0, 1), tokens: []string{"00"}},
  60. {peer: net.IPv4(10, 0, 0, 2), tokens: []string{"25"}},
  61. {peer: net.IPv4(10, 0, 0, 3), tokens: []string{"50"}},
  62. {peer: net.IPv4(10, 0, 0, 4), tokens: []string{"75"}},
  63. }
  64. for _, host := range hosts {
  65. policy.AddHost(host)
  66. }
  67. // the token ring is not setup without the partitioner, but the fallback
  68. // should work
  69. if actual := policy.Pick(nil)(); !actual.Info().Peer().Equal(hosts[0].peer) {
  70. t.Errorf("Expected peer 0 but was %s", actual.Info().Peer())
  71. }
  72. query.RoutingKey([]byte("30"))
  73. if actual := policy.Pick(query)(); !actual.Info().Peer().Equal(hosts[1].peer) {
  74. t.Errorf("Expected peer 1 but was %s", actual.Info().Peer())
  75. }
  76. policy.SetPartitioner("OrderedPartitioner")
  77. // now the token ring is configured
  78. query.RoutingKey([]byte("20"))
  79. iter = policy.Pick(query)
  80. if actual := iter(); !actual.Info().Peer().Equal(hosts[1].peer) {
  81. t.Errorf("Expected peer 1 but was %s", actual.Info().Peer())
  82. }
  83. // rest are round robin
  84. if actual := iter(); !actual.Info().Peer().Equal(hosts[2].peer) {
  85. t.Errorf("Expected peer 2 but was %s", actual.Info().Peer())
  86. }
  87. if actual := iter(); !actual.Info().Peer().Equal(hosts[3].peer) {
  88. t.Errorf("Expected peer 3 but was %s", actual.Info().Peer())
  89. }
  90. if actual := iter(); !actual.Info().Peer().Equal(hosts[0].peer) {
  91. t.Errorf("Expected peer 0 but was %s", actual.Info().Peer())
  92. }
  93. }
  94. // Tests of the host pool host selection policy implementation
  95. func TestHostPoolHostPolicy(t *testing.T) {
  96. policy := HostPoolHostPolicy(hostpool.New(nil))
  97. hosts := []*HostInfo{
  98. {hostId: "0", peer: net.IPv4(10, 0, 0, 0)},
  99. {hostId: "1", peer: net.IPv4(10, 0, 0, 1)},
  100. }
  101. // Using set host to control the ordering of the hosts as calling "AddHost" iterates the map
  102. // which will result in an unpredictable ordering
  103. policy.(*hostPoolHostPolicy).SetHosts(hosts)
  104. // the first host selected is actually at [1], but this is ok for RR
  105. // interleaved iteration should always increment the host
  106. iter := policy.Pick(nil)
  107. actualA := iter()
  108. if actualA.Info().HostID() != "0" {
  109. t.Errorf("Expected hosts[0] but was hosts[%s]", actualA.Info().HostID())
  110. }
  111. actualA.Mark(nil)
  112. actualB := iter()
  113. if actualB.Info().HostID() != "1" {
  114. t.Errorf("Expected hosts[1] but was hosts[%s]", actualB.Info().HostID())
  115. }
  116. actualB.Mark(fmt.Errorf("error"))
  117. actualC := iter()
  118. if actualC.Info().HostID() != "0" {
  119. t.Errorf("Expected hosts[0] but was hosts[%s]", actualC.Info().HostID())
  120. }
  121. actualC.Mark(nil)
  122. actualD := iter()
  123. if actualD.Info().HostID() != "0" {
  124. t.Errorf("Expected hosts[0] but was hosts[%s]", actualD.Info().HostID())
  125. }
  126. actualD.Mark(nil)
  127. }
  128. func TestRoundRobinNilHostInfo(t *testing.T) {
  129. policy := RoundRobinHostPolicy()
  130. host := &HostInfo{hostId: "host-1"}
  131. policy.AddHost(host)
  132. iter := policy.Pick(nil)
  133. next := iter()
  134. if next == nil {
  135. t.Fatal("got nil host")
  136. } else if v := next.Info(); v == nil {
  137. t.Fatal("got nil HostInfo")
  138. } else if v.HostID() != host.HostID() {
  139. t.Fatalf("expected host %v got %v", host, v)
  140. }
  141. next = iter()
  142. if next != nil {
  143. t.Errorf("expected to get nil host got %+v", next)
  144. if next.Info() == nil {
  145. t.Fatalf("HostInfo is nil")
  146. }
  147. }
  148. }
  149. func TestTokenAwareNilHostInfo(t *testing.T) {
  150. policy := TokenAwareHostPolicy(RoundRobinHostPolicy())
  151. hosts := [...]*HostInfo{
  152. {peer: net.IPv4(10, 0, 0, 0), tokens: []string{"00"}},
  153. {peer: net.IPv4(10, 0, 0, 1), tokens: []string{"25"}},
  154. {peer: net.IPv4(10, 0, 0, 2), tokens: []string{"50"}},
  155. {peer: net.IPv4(10, 0, 0, 3), tokens: []string{"75"}},
  156. }
  157. for _, host := range hosts {
  158. policy.AddHost(host)
  159. }
  160. policy.SetPartitioner("OrderedPartitioner")
  161. query := &Query{}
  162. query.RoutingKey([]byte("20"))
  163. iter := policy.Pick(query)
  164. next := iter()
  165. if next == nil {
  166. t.Fatal("got nil host")
  167. } else if v := next.Info(); v == nil {
  168. t.Fatal("got nil HostInfo")
  169. } else if !v.Peer().Equal(hosts[1].peer) {
  170. t.Fatalf("expected peer 1 got %v", v.Peer())
  171. }
  172. // Empty the hosts to trigger the panic when using the fallback.
  173. for _, host := range hosts {
  174. policy.RemoveHost(host)
  175. }
  176. next = iter()
  177. if next != nil {
  178. t.Errorf("expected to get nil host got %+v", next)
  179. if next.Info() == nil {
  180. t.Fatalf("HostInfo is nil")
  181. }
  182. }
  183. }
  184. func TestCOWList_Add(t *testing.T) {
  185. var cow cowHostList
  186. toAdd := [...]net.IP{net.IPv4(0, 0, 0, 0), net.IPv4(1, 0, 0, 0), net.IPv4(2, 0, 0, 0)}
  187. for _, addr := range toAdd {
  188. if !cow.add(&HostInfo{peer: addr}) {
  189. t.Fatal("did not add peer which was not in the set")
  190. }
  191. }
  192. hosts := cow.get()
  193. if len(hosts) != len(toAdd) {
  194. t.Fatalf("expected to have %d hosts got %d", len(toAdd), len(hosts))
  195. }
  196. set := make(map[string]bool)
  197. for _, host := range hosts {
  198. set[string(host.Peer())] = true
  199. }
  200. for _, addr := range toAdd {
  201. if !set[string(addr)] {
  202. t.Errorf("addr was not in the host list: %q", addr)
  203. }
  204. }
  205. }
  206. func TestSimpleRetryPolicy(t *testing.T) {
  207. q := &Query{}
  208. rt := &SimpleRetryPolicy{NumRetries: 2}
  209. if !rt.Attempt(q) {
  210. t.Fatal("should allow retry after 0 attempts")
  211. }
  212. q.attempts = 5
  213. if rt.Attempt(q) {
  214. t.Fatal("should not allow retry after passing threshold")
  215. }
  216. }