policies_test.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  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. "time"
  10. "github.com/hailocab/go-hostpool"
  11. )
  12. // Tests of the round-robin host selection policy implementation
  13. func TestHostPolicy_RoundRobin(t *testing.T) {
  14. policy := RoundRobinHostPolicy()
  15. hosts := [...]*HostInfo{
  16. {hostId: "0", connectAddress: net.IPv4(0, 0, 0, 1)},
  17. {hostId: "1", connectAddress: net.IPv4(0, 0, 0, 2)},
  18. }
  19. for _, host := range hosts {
  20. policy.AddHost(host)
  21. }
  22. // interleaved iteration should always increment the host
  23. iterA := policy.Pick(nil)
  24. if actual := iterA(); actual.Info() != hosts[0] {
  25. t.Errorf("Expected hosts[0] but was hosts[%s]", actual.Info().HostID())
  26. }
  27. iterB := policy.Pick(nil)
  28. if actual := iterB(); actual.Info() != hosts[1] {
  29. t.Errorf("Expected hosts[1] but was hosts[%s]", actual.Info().HostID())
  30. }
  31. if actual := iterB(); actual.Info() != hosts[0] {
  32. t.Errorf("Expected hosts[0] but was hosts[%s]", actual.Info().HostID())
  33. }
  34. if actual := iterA(); actual.Info() != hosts[1] {
  35. t.Errorf("Expected hosts[1] but was hosts[%s]", actual.Info().HostID())
  36. }
  37. iterC := policy.Pick(nil)
  38. if actual := iterC(); actual.Info() != hosts[0] {
  39. t.Errorf("Expected hosts[0] but was hosts[%s]", actual.Info().HostID())
  40. }
  41. if actual := iterC(); actual.Info() != hosts[1] {
  42. t.Errorf("Expected hosts[1] but was hosts[%s]", actual.Info().HostID())
  43. }
  44. }
  45. // Tests of the token-aware host selection policy implementation with a
  46. // round-robin host selection policy fallback.
  47. func TestHostPolicy_TokenAware(t *testing.T) {
  48. policy := TokenAwareHostPolicy(RoundRobinHostPolicy())
  49. query := &Query{}
  50. iter := policy.Pick(nil)
  51. if iter == nil {
  52. t.Fatal("host iterator was nil")
  53. }
  54. actual := iter()
  55. if actual != nil {
  56. t.Fatalf("expected nil from iterator, but was %v", actual)
  57. }
  58. // set the hosts
  59. hosts := [...]*HostInfo{
  60. {connectAddress: net.IPv4(10, 0, 0, 1), tokens: []string{"00"}},
  61. {connectAddress: net.IPv4(10, 0, 0, 2), tokens: []string{"25"}},
  62. {connectAddress: net.IPv4(10, 0, 0, 3), tokens: []string{"50"}},
  63. {connectAddress: net.IPv4(10, 0, 0, 4), tokens: []string{"75"}},
  64. }
  65. for _, host := range hosts {
  66. policy.AddHost(host)
  67. }
  68. // the token ring is not setup without the partitioner, but the fallback
  69. // should work
  70. if actual := policy.Pick(nil)(); !actual.Info().ConnectAddress().Equal(hosts[0].ConnectAddress()) {
  71. t.Errorf("Expected peer 0 but was %s", actual.Info().ConnectAddress())
  72. }
  73. query.RoutingKey([]byte("30"))
  74. if actual := policy.Pick(query)(); !actual.Info().ConnectAddress().Equal(hosts[1].ConnectAddress()) {
  75. t.Errorf("Expected peer 1 but was %s", actual.Info().ConnectAddress())
  76. }
  77. policy.SetPartitioner("OrderedPartitioner")
  78. // now the token ring is configured
  79. query.RoutingKey([]byte("20"))
  80. iter = policy.Pick(query)
  81. if actual := iter(); !actual.Info().ConnectAddress().Equal(hosts[1].ConnectAddress()) {
  82. t.Errorf("Expected peer 1 but was %s", actual.Info().ConnectAddress())
  83. }
  84. // rest are round robin
  85. if actual := iter(); !actual.Info().ConnectAddress().Equal(hosts[2].ConnectAddress()) {
  86. t.Errorf("Expected peer 2 but was %s", actual.Info().ConnectAddress())
  87. }
  88. if actual := iter(); !actual.Info().ConnectAddress().Equal(hosts[3].ConnectAddress()) {
  89. t.Errorf("Expected peer 3 but was %s", actual.Info().ConnectAddress())
  90. }
  91. if actual := iter(); !actual.Info().ConnectAddress().Equal(hosts[0].ConnectAddress()) {
  92. t.Errorf("Expected peer 0 but was %s", actual.Info().ConnectAddress())
  93. }
  94. }
  95. // Tests of the host pool host selection policy implementation
  96. func TestHostPolicy_HostPool(t *testing.T) {
  97. policy := HostPoolHostPolicy(hostpool.New(nil))
  98. hosts := []*HostInfo{
  99. {hostId: "0", connectAddress: net.IPv4(10, 0, 0, 0)},
  100. {hostId: "1", connectAddress: net.IPv4(10, 0, 0, 1)},
  101. }
  102. // Using set host to control the ordering of the hosts as calling "AddHost" iterates the map
  103. // which will result in an unpredictable ordering
  104. policy.(*hostPoolHostPolicy).SetHosts(hosts)
  105. // the first host selected is actually at [1], but this is ok for RR
  106. // interleaved iteration should always increment the host
  107. iter := policy.Pick(nil)
  108. actualA := iter()
  109. if actualA.Info().HostID() != "0" {
  110. t.Errorf("Expected hosts[0] but was hosts[%s]", actualA.Info().HostID())
  111. }
  112. actualA.Mark(nil)
  113. actualB := iter()
  114. if actualB.Info().HostID() != "1" {
  115. t.Errorf("Expected hosts[1] but was hosts[%s]", actualB.Info().HostID())
  116. }
  117. actualB.Mark(fmt.Errorf("error"))
  118. actualC := iter()
  119. if actualC.Info().HostID() != "0" {
  120. t.Errorf("Expected hosts[0] but was hosts[%s]", actualC.Info().HostID())
  121. }
  122. actualC.Mark(nil)
  123. actualD := iter()
  124. if actualD.Info().HostID() != "0" {
  125. t.Errorf("Expected hosts[0] but was hosts[%s]", actualD.Info().HostID())
  126. }
  127. actualD.Mark(nil)
  128. }
  129. func TestHostPolicy_RoundRobin_NilHostInfo(t *testing.T) {
  130. policy := RoundRobinHostPolicy()
  131. host := &HostInfo{hostId: "host-1"}
  132. policy.AddHost(host)
  133. iter := policy.Pick(nil)
  134. next := iter()
  135. if next == nil {
  136. t.Fatal("got nil host")
  137. } else if v := next.Info(); v == nil {
  138. t.Fatal("got nil HostInfo")
  139. } else if v.HostID() != host.HostID() {
  140. t.Fatalf("expected host %v got %v", host, v)
  141. }
  142. next = iter()
  143. if next != nil {
  144. t.Errorf("expected to get nil host got %+v", next)
  145. if next.Info() == nil {
  146. t.Fatalf("HostInfo is nil")
  147. }
  148. }
  149. }
  150. func TestHostPolicy_TokenAware_NilHostInfo(t *testing.T) {
  151. policy := TokenAwareHostPolicy(RoundRobinHostPolicy())
  152. hosts := [...]*HostInfo{
  153. {connectAddress: net.IPv4(10, 0, 0, 0), tokens: []string{"00"}},
  154. {connectAddress: net.IPv4(10, 0, 0, 1), tokens: []string{"25"}},
  155. {connectAddress: net.IPv4(10, 0, 0, 2), tokens: []string{"50"}},
  156. {connectAddress: net.IPv4(10, 0, 0, 3), tokens: []string{"75"}},
  157. }
  158. for _, host := range hosts {
  159. policy.AddHost(host)
  160. }
  161. policy.SetPartitioner("OrderedPartitioner")
  162. query := &Query{}
  163. query.RoutingKey([]byte("20"))
  164. iter := policy.Pick(query)
  165. next := iter()
  166. if next == nil {
  167. t.Fatal("got nil host")
  168. } else if v := next.Info(); v == nil {
  169. t.Fatal("got nil HostInfo")
  170. } else if !v.ConnectAddress().Equal(hosts[1].ConnectAddress()) {
  171. t.Fatalf("expected peer 1 got %v", v.ConnectAddress())
  172. }
  173. // Empty the hosts to trigger the panic when using the fallback.
  174. for _, host := range hosts {
  175. policy.RemoveHost(host)
  176. }
  177. next = iter()
  178. if next != nil {
  179. t.Errorf("expected to get nil host got %+v", next)
  180. if next.Info() == nil {
  181. t.Fatalf("HostInfo is nil")
  182. }
  183. }
  184. }
  185. func TestCOWList_Add(t *testing.T) {
  186. var cow cowHostList
  187. toAdd := [...]net.IP{net.IPv4(10, 0, 0, 1), net.IPv4(10, 0, 0, 2), net.IPv4(10, 0, 0, 3)}
  188. for _, addr := range toAdd {
  189. if !cow.add(&HostInfo{connectAddress: addr}) {
  190. t.Fatal("did not add peer which was not in the set")
  191. }
  192. }
  193. hosts := cow.get()
  194. if len(hosts) != len(toAdd) {
  195. t.Fatalf("expected to have %d hosts got %d", len(toAdd), len(hosts))
  196. }
  197. set := make(map[string]bool)
  198. for _, host := range hosts {
  199. set[string(host.ConnectAddress())] = true
  200. }
  201. for _, addr := range toAdd {
  202. if !set[string(addr)] {
  203. t.Errorf("addr was not in the host list: %q", addr)
  204. }
  205. }
  206. }
  207. // TestSimpleRetryPolicy makes sure that we only allow 1 + numRetries attempts
  208. func TestSimpleRetryPolicy(t *testing.T) {
  209. q := &Query{}
  210. // this should allow a total of 3 tries.
  211. rt := &SimpleRetryPolicy{NumRetries: 2}
  212. cases := []struct {
  213. attempts int
  214. allow bool
  215. }{
  216. {0, true},
  217. {1, true},
  218. {2, true},
  219. {3, false},
  220. {4, false},
  221. {5, false},
  222. }
  223. q.metrics = make(map[string]*queryMetrics)
  224. for _, c := range cases {
  225. q.metrics["127.0.0.1"] = &queryMetrics{Attempts: c.attempts}
  226. if c.allow && !rt.Attempt(q) {
  227. t.Fatalf("should allow retry after %d attempts", c.attempts)
  228. }
  229. if !c.allow && rt.Attempt(q) {
  230. t.Fatalf("should not allow retry after %d attempts", c.attempts)
  231. }
  232. }
  233. }
  234. func TestExponentialBackoffPolicy(t *testing.T) {
  235. // test with defaults
  236. sut := &ExponentialBackoffRetryPolicy{NumRetries: 2}
  237. cases := []struct {
  238. attempts int
  239. delay time.Duration
  240. }{
  241. {1, 100 * time.Millisecond},
  242. {2, (2) * 100 * time.Millisecond},
  243. {3, (2 * 2) * 100 * time.Millisecond},
  244. {4, (2 * 2 * 2) * 100 * time.Millisecond},
  245. }
  246. for _, c := range cases {
  247. // test 100 times for each case
  248. for i := 0; i < 100; i++ {
  249. d := sut.napTime(c.attempts)
  250. if d < c.delay-(100*time.Millisecond)/2 {
  251. t.Fatalf("Delay %d less than jitter min of %d", d, c.delay-100*time.Millisecond/2)
  252. }
  253. if d > c.delay+(100*time.Millisecond)/2 {
  254. t.Fatalf("Delay %d greater than jitter max of %d", d, c.delay+100*time.Millisecond/2)
  255. }
  256. }
  257. }
  258. }
  259. func TestDowngradingConsistencyRetryPolicy(t *testing.T) {
  260. q := &Query{cons: LocalQuorum}
  261. rewt0 := &RequestErrWriteTimeout{
  262. Received: 0,
  263. WriteType: "SIMPLE",
  264. }
  265. rewt1 := &RequestErrWriteTimeout{
  266. Received: 1,
  267. WriteType: "BATCH",
  268. }
  269. rewt2 := &RequestErrWriteTimeout{
  270. WriteType: "UNLOGGED_BATCH",
  271. }
  272. rert := &RequestErrReadTimeout{}
  273. reu0 := &RequestErrUnavailable{
  274. Alive: 0,
  275. }
  276. reu1 := &RequestErrUnavailable{
  277. Alive: 1,
  278. }
  279. // this should allow a total of 3 tries.
  280. consistencyLevels := []Consistency{Three, Two, One}
  281. rt := &DowngradingConsistencyRetryPolicy{ConsistencyLevelsToTry: consistencyLevels}
  282. cases := []struct {
  283. attempts int
  284. allow bool
  285. err error
  286. retryType RetryType
  287. }{
  288. {0, true, rewt0, Rethrow},
  289. {3, true, rewt1, Ignore},
  290. {1, true, rewt2, Retry},
  291. {2, true, rert, Retry},
  292. {4, false, reu0, Rethrow},
  293. {16, false, reu1, Retry},
  294. }
  295. q.metrics = make(map[string]*queryMetrics)
  296. for _, c := range cases {
  297. q.metrics["127.0.0.1"] = &queryMetrics{Attempts: c.attempts}
  298. if c.retryType != rt.GetRetryType(c.err) {
  299. t.Fatalf("retry type should be %v", c.retryType)
  300. }
  301. if c.allow && !rt.Attempt(q) {
  302. t.Fatalf("should allow retry after %d attempts", c.attempts)
  303. }
  304. if !c.allow && rt.Attempt(q) {
  305. t.Fatalf("should not allow retry after %d attempts", c.attempts)
  306. }
  307. }
  308. }
  309. func TestHostPolicy_DCAwareRR(t *testing.T) {
  310. p := DCAwareRoundRobinPolicy("local")
  311. hosts := [...]*HostInfo{
  312. {hostId: "0", connectAddress: net.ParseIP("10.0.0.1"), dataCenter: "local"},
  313. {hostId: "1", connectAddress: net.ParseIP("10.0.0.2"), dataCenter: "local"},
  314. {hostId: "2", connectAddress: net.ParseIP("10.0.0.3"), dataCenter: "remote"},
  315. {hostId: "3", connectAddress: net.ParseIP("10.0.0.4"), dataCenter: "remote"},
  316. }
  317. for _, host := range hosts {
  318. p.AddHost(host)
  319. }
  320. // interleaved iteration should always increment the host
  321. iterA := p.Pick(nil)
  322. if actual := iterA(); actual.Info() != hosts[0] {
  323. t.Errorf("Expected hosts[0] but was hosts[%s]", actual.Info().HostID())
  324. }
  325. iterB := p.Pick(nil)
  326. if actual := iterB(); actual.Info() != hosts[1] {
  327. t.Errorf("Expected hosts[1] but was hosts[%s]", actual.Info().HostID())
  328. }
  329. if actual := iterB(); actual.Info() != hosts[0] {
  330. t.Errorf("Expected hosts[0] but was hosts[%s]", actual.Info().HostID())
  331. }
  332. if actual := iterA(); actual.Info() != hosts[1] {
  333. t.Errorf("Expected hosts[1] but was hosts[%s]", actual.Info().HostID())
  334. }
  335. iterC := p.Pick(nil)
  336. if actual := iterC(); actual.Info() != hosts[0] {
  337. t.Errorf("Expected hosts[0] but was hosts[%s]", actual.Info().HostID())
  338. }
  339. p.RemoveHost(hosts[0])
  340. if actual := iterC(); actual.Info() != hosts[1] {
  341. t.Errorf("Expected hosts[1] but was hosts[%s]", actual.Info().HostID())
  342. }
  343. p.RemoveHost(hosts[1])
  344. iterD := p.Pick(nil)
  345. if actual := iterD(); actual.Info() != hosts[2] {
  346. t.Errorf("Expected hosts[2] but was hosts[%s]", actual.Info().HostID())
  347. }
  348. if actual := iterD(); actual.Info() != hosts[3] {
  349. t.Errorf("Expected hosts[3] but was hosts[%s]", actual.Info().HostID())
  350. }
  351. }