node_test.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. // Copyright 2015 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 rafttest
  15. import (
  16. "context"
  17. "testing"
  18. "time"
  19. "go.etcd.io/etcd/raft"
  20. )
  21. func TestBasicProgress(t *testing.T) {
  22. peers := []raft.Peer{{ID: 1, Context: nil}, {ID: 2, Context: nil}, {ID: 3, Context: nil}, {ID: 4, Context: nil}, {ID: 5, Context: nil}}
  23. nt := newRaftNetwork(1, 2, 3, 4, 5)
  24. nodes := make([]*node, 0)
  25. for i := 1; i <= 5; i++ {
  26. n := startNode(uint64(i), peers, nt.nodeNetwork(uint64(i)))
  27. nodes = append(nodes, n)
  28. }
  29. waitLeader(nodes)
  30. for i := 0; i < 100; i++ {
  31. nodes[0].Propose(context.TODO(), []byte("somedata"))
  32. }
  33. if !waitCommitConverge(nodes, 100) {
  34. t.Errorf("commits failed to converge!")
  35. }
  36. for _, n := range nodes {
  37. n.stop()
  38. }
  39. }
  40. func TestRestart(t *testing.T) {
  41. peers := []raft.Peer{{ID: 1, Context: nil}, {ID: 2, Context: nil}, {ID: 3, Context: nil}, {ID: 4, Context: nil}, {ID: 5, Context: nil}}
  42. nt := newRaftNetwork(1, 2, 3, 4, 5)
  43. nodes := make([]*node, 0)
  44. for i := 1; i <= 5; i++ {
  45. n := startNode(uint64(i), peers, nt.nodeNetwork(uint64(i)))
  46. nodes = append(nodes, n)
  47. }
  48. l := waitLeader(nodes)
  49. k1, k2 := (l+1)%5, (l+2)%5
  50. for i := 0; i < 30; i++ {
  51. nodes[l].Propose(context.TODO(), []byte("somedata"))
  52. }
  53. nodes[k1].stop()
  54. for i := 0; i < 30; i++ {
  55. nodes[(l+3)%5].Propose(context.TODO(), []byte("somedata"))
  56. }
  57. nodes[k2].stop()
  58. for i := 0; i < 30; i++ {
  59. nodes[(l+4)%5].Propose(context.TODO(), []byte("somedata"))
  60. }
  61. nodes[k2].restart()
  62. for i := 0; i < 30; i++ {
  63. nodes[l].Propose(context.TODO(), []byte("somedata"))
  64. }
  65. nodes[k1].restart()
  66. if !waitCommitConverge(nodes, 120) {
  67. t.Errorf("commits failed to converge!")
  68. }
  69. for _, n := range nodes {
  70. n.stop()
  71. }
  72. }
  73. func TestPause(t *testing.T) {
  74. peers := []raft.Peer{{ID: 1, Context: nil}, {ID: 2, Context: nil}, {ID: 3, Context: nil}, {ID: 4, Context: nil}, {ID: 5, Context: nil}}
  75. nt := newRaftNetwork(1, 2, 3, 4, 5)
  76. nodes := make([]*node, 0)
  77. for i := 1; i <= 5; i++ {
  78. n := startNode(uint64(i), peers, nt.nodeNetwork(uint64(i)))
  79. nodes = append(nodes, n)
  80. }
  81. waitLeader(nodes)
  82. for i := 0; i < 30; i++ {
  83. nodes[0].Propose(context.TODO(), []byte("somedata"))
  84. }
  85. nodes[1].pause()
  86. for i := 0; i < 30; i++ {
  87. nodes[0].Propose(context.TODO(), []byte("somedata"))
  88. }
  89. nodes[2].pause()
  90. for i := 0; i < 30; i++ {
  91. nodes[0].Propose(context.TODO(), []byte("somedata"))
  92. }
  93. nodes[2].resume()
  94. for i := 0; i < 30; i++ {
  95. nodes[0].Propose(context.TODO(), []byte("somedata"))
  96. }
  97. nodes[1].resume()
  98. if !waitCommitConverge(nodes, 120) {
  99. t.Errorf("commits failed to converge!")
  100. }
  101. for _, n := range nodes {
  102. n.stop()
  103. }
  104. }
  105. func waitLeader(ns []*node) int {
  106. var l map[uint64]struct{}
  107. var lindex int
  108. for {
  109. l = make(map[uint64]struct{})
  110. for i, n := range ns {
  111. lead := n.Status().SoftState.Lead
  112. if lead != 0 {
  113. l[lead] = struct{}{}
  114. if n.id == lead {
  115. lindex = i
  116. }
  117. }
  118. }
  119. if len(l) == 1 {
  120. return lindex
  121. }
  122. }
  123. }
  124. func waitCommitConverge(ns []*node, target uint64) bool {
  125. var c map[uint64]struct{}
  126. for i := 0; i < 50; i++ {
  127. c = make(map[uint64]struct{})
  128. var good int
  129. for _, n := range ns {
  130. commit := n.Node.Status().HardState.Commit
  131. c[commit] = struct{}{}
  132. if commit > target {
  133. good++
  134. }
  135. }
  136. if len(c) == 1 && good == len(ns) {
  137. return true
  138. }
  139. time.Sleep(100 * time.Millisecond)
  140. }
  141. return false
  142. }