maintenance_test.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. // Copyright 2017 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 integration
  15. import (
  16. "bytes"
  17. "context"
  18. "fmt"
  19. "io"
  20. "io/ioutil"
  21. "math"
  22. "path/filepath"
  23. "testing"
  24. "time"
  25. "go.uber.org/zap"
  26. "google.golang.org/grpc"
  27. "go.etcd.io/etcd/clientv3"
  28. "go.etcd.io/etcd/etcdserver/api/v3rpc/rpctypes"
  29. "go.etcd.io/etcd/integration"
  30. "go.etcd.io/etcd/lease"
  31. "go.etcd.io/etcd/mvcc"
  32. "go.etcd.io/etcd/mvcc/backend"
  33. "go.etcd.io/etcd/pkg/testutil"
  34. )
  35. func TestMaintenanceHashKV(t *testing.T) {
  36. defer testutil.AfterTest(t)
  37. clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 3})
  38. defer clus.Terminate(t)
  39. for i := 0; i < 3; i++ {
  40. if _, err := clus.RandClient().Put(context.Background(), "foo", "bar"); err != nil {
  41. t.Fatal(err)
  42. }
  43. }
  44. var hv uint32
  45. for i := 0; i < 3; i++ {
  46. cli := clus.Client(i)
  47. // ensure writes are replicated
  48. if _, err := cli.Get(context.TODO(), "foo"); err != nil {
  49. t.Fatal(err)
  50. }
  51. hresp, err := cli.HashKV(context.Background(), clus.Members[i].GRPCAddr(), 0)
  52. if err != nil {
  53. t.Fatal(err)
  54. }
  55. if hv == 0 {
  56. hv = hresp.Hash
  57. continue
  58. }
  59. if hv != hresp.Hash {
  60. t.Fatalf("#%d: hash expected %d, got %d", i, hv, hresp.Hash)
  61. }
  62. }
  63. }
  64. func TestMaintenanceMoveLeader(t *testing.T) {
  65. defer testutil.AfterTest(t)
  66. clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 3})
  67. defer clus.Terminate(t)
  68. oldLeadIdx := clus.WaitLeader(t)
  69. targetIdx := (oldLeadIdx + 1) % 3
  70. target := uint64(clus.Members[targetIdx].ID())
  71. cli := clus.Client(targetIdx)
  72. _, err := cli.MoveLeader(context.Background(), target)
  73. if err != rpctypes.ErrNotLeader {
  74. t.Fatalf("error expected %v, got %v", rpctypes.ErrNotLeader, err)
  75. }
  76. cli = clus.Client(oldLeadIdx)
  77. _, err = cli.MoveLeader(context.Background(), target)
  78. if err != nil {
  79. t.Fatal(err)
  80. }
  81. leadIdx := clus.WaitLeader(t)
  82. lead := uint64(clus.Members[leadIdx].ID())
  83. if target != lead {
  84. t.Fatalf("new leader expected %d, got %d", target, lead)
  85. }
  86. }
  87. // TestMaintenanceSnapshotError ensures that context cancel/timeout
  88. // before snapshot reading returns corresponding context errors.
  89. func TestMaintenanceSnapshotError(t *testing.T) {
  90. defer testutil.AfterTest(t)
  91. clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 1})
  92. defer clus.Terminate(t)
  93. // reading snapshot with canceled context should error out
  94. ctx, cancel := context.WithCancel(context.Background())
  95. rc1, err := clus.RandClient().Snapshot(ctx)
  96. if err != nil {
  97. t.Fatal(err)
  98. }
  99. defer rc1.Close()
  100. cancel()
  101. _, err = io.Copy(ioutil.Discard, rc1)
  102. if err != context.Canceled {
  103. t.Errorf("expected %v, got %v", context.Canceled, err)
  104. }
  105. // reading snapshot with deadline exceeded should error out
  106. ctx, cancel = context.WithTimeout(context.Background(), time.Second)
  107. defer cancel()
  108. rc2, err := clus.RandClient().Snapshot(ctx)
  109. if err != nil {
  110. t.Fatal(err)
  111. }
  112. defer rc2.Close()
  113. time.Sleep(2 * time.Second)
  114. _, err = io.Copy(ioutil.Discard, rc2)
  115. if err != nil && !isClientTimeout(err) {
  116. t.Errorf("expected client timeout, got %v", err)
  117. }
  118. }
  119. // TestMaintenanceSnapshotErrorInflight ensures that inflight context cancel/timeout
  120. // fails snapshot reading with corresponding context errors.
  121. func TestMaintenanceSnapshotErrorInflight(t *testing.T) {
  122. defer testutil.AfterTest(t)
  123. clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 1})
  124. defer clus.Terminate(t)
  125. // take about 1-second to read snapshot
  126. clus.Members[0].Stop(t)
  127. dpath := filepath.Join(clus.Members[0].DataDir, "member", "snap", "db")
  128. b := backend.NewDefaultBackend(dpath)
  129. s := mvcc.NewStore(zap.NewExample(), b, &lease.FakeLessor{}, nil, mvcc.StoreConfig{CompactionBatchLimit: math.MaxInt32})
  130. rev := 100000
  131. for i := 2; i <= rev; i++ {
  132. s.Put([]byte(fmt.Sprintf("%10d", i)), bytes.Repeat([]byte("a"), 1024), lease.NoLease)
  133. }
  134. s.Close()
  135. b.Close()
  136. clus.Members[0].Restart(t)
  137. cli := clus.RandClient()
  138. // reading snapshot with canceled context should error out
  139. ctx, cancel := context.WithCancel(context.Background())
  140. rc1, err := cli.Snapshot(ctx)
  141. if err != nil {
  142. t.Fatal(err)
  143. }
  144. defer rc1.Close()
  145. donec := make(chan struct{})
  146. go func() {
  147. time.Sleep(300 * time.Millisecond)
  148. cancel()
  149. close(donec)
  150. }()
  151. _, err = io.Copy(ioutil.Discard, rc1)
  152. if err != nil && err != context.Canceled {
  153. t.Errorf("expected %v, got %v", context.Canceled, err)
  154. }
  155. <-donec
  156. // reading snapshot with deadline exceeded should error out
  157. ctx, cancel = context.WithTimeout(context.Background(), time.Second)
  158. defer cancel()
  159. rc2, err := clus.RandClient().Snapshot(ctx)
  160. if err != nil {
  161. t.Fatal(err)
  162. }
  163. defer rc2.Close()
  164. // 300ms left and expect timeout while snapshot reading is in progress
  165. time.Sleep(700 * time.Millisecond)
  166. _, err = io.Copy(ioutil.Discard, rc2)
  167. if err != nil && !isClientTimeout(err) {
  168. t.Errorf("expected client timeout, got %v", err)
  169. }
  170. }
  171. func TestMaintenanceStatus(t *testing.T) {
  172. defer testutil.AfterTest(t)
  173. clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 3})
  174. defer clus.Terminate(t)
  175. clus.WaitLeader(t)
  176. eps := make([]string, 3)
  177. for i := 0; i < 3; i++ {
  178. eps[i] = clus.Members[i].GRPCAddr()
  179. }
  180. cli, err := clientv3.New(clientv3.Config{Endpoints: eps, DialOptions: []grpc.DialOption{grpc.WithBlock()}})
  181. if err != nil {
  182. t.Fatal(err)
  183. }
  184. defer cli.Close()
  185. prevID, leaderFound := uint64(0), false
  186. for i := 0; i < 3; i++ {
  187. resp, err := cli.Status(context.TODO(), eps[i])
  188. if err != nil {
  189. t.Fatal(err)
  190. }
  191. if prevID == 0 {
  192. prevID, leaderFound = resp.Header.MemberId, resp.Header.MemberId == resp.Leader
  193. continue
  194. }
  195. if prevID == resp.Header.MemberId {
  196. t.Errorf("#%d: status returned duplicate member ID with %016x", i, prevID)
  197. }
  198. if leaderFound && resp.Header.MemberId == resp.Leader {
  199. t.Errorf("#%d: leader already found, but found another %016x", i, resp.Header.MemberId)
  200. }
  201. if !leaderFound {
  202. leaderFound = resp.Header.MemberId == resp.Leader
  203. }
  204. }
  205. if !leaderFound {
  206. t.Fatal("no leader found")
  207. }
  208. }