maintenance_test.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. "path/filepath"
  22. "testing"
  23. "time"
  24. "go.uber.org/zap"
  25. "github.com/coreos/etcd/etcdserver/api/v3rpc/rpctypes"
  26. "github.com/coreos/etcd/integration"
  27. "github.com/coreos/etcd/lease"
  28. "github.com/coreos/etcd/mvcc"
  29. "github.com/coreos/etcd/mvcc/backend"
  30. "github.com/coreos/etcd/pkg/testutil"
  31. )
  32. func TestMaintenanceHashKV(t *testing.T) {
  33. defer testutil.AfterTest(t)
  34. clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 3})
  35. defer clus.Terminate(t)
  36. for i := 0; i < 3; i++ {
  37. if _, err := clus.RandClient().Put(context.Background(), "foo", "bar"); err != nil {
  38. t.Fatal(err)
  39. }
  40. }
  41. var hv uint32
  42. for i := 0; i < 3; i++ {
  43. cli := clus.Client(i)
  44. // ensure writes are replicated
  45. if _, err := cli.Get(context.TODO(), "foo"); err != nil {
  46. t.Fatal(err)
  47. }
  48. hresp, err := cli.HashKV(context.Background(), clus.Members[i].GRPCAddr(), 0)
  49. if err != nil {
  50. t.Fatal(err)
  51. }
  52. if hv == 0 {
  53. hv = hresp.Hash
  54. continue
  55. }
  56. if hv != hresp.Hash {
  57. t.Fatalf("#%d: hash expected %d, got %d", i, hv, hresp.Hash)
  58. }
  59. }
  60. }
  61. func TestMaintenanceMoveLeader(t *testing.T) {
  62. defer testutil.AfterTest(t)
  63. clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 3})
  64. defer clus.Terminate(t)
  65. oldLeadIdx := clus.WaitLeader(t)
  66. targetIdx := (oldLeadIdx + 1) % 3
  67. target := uint64(clus.Members[targetIdx].ID())
  68. cli := clus.Client(targetIdx)
  69. _, err := cli.MoveLeader(context.Background(), target)
  70. if err != rpctypes.ErrNotLeader {
  71. t.Fatalf("error expected %v, got %v", rpctypes.ErrNotLeader, err)
  72. }
  73. cli = clus.Client(oldLeadIdx)
  74. _, err = cli.MoveLeader(context.Background(), target)
  75. if err != nil {
  76. t.Fatal(err)
  77. }
  78. leadIdx := clus.WaitLeader(t)
  79. lead := uint64(clus.Members[leadIdx].ID())
  80. if target != lead {
  81. t.Fatalf("new leader expected %d, got %d", target, lead)
  82. }
  83. }
  84. // TestMaintenanceSnapshotError ensures that context cancel/timeout
  85. // before snapshot reading returns corresponding context errors.
  86. func TestMaintenanceSnapshotError(t *testing.T) {
  87. defer testutil.AfterTest(t)
  88. clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 1})
  89. defer clus.Terminate(t)
  90. // reading snapshot with canceled context should error out
  91. ctx, cancel := context.WithCancel(context.Background())
  92. rc1, err := clus.RandClient().Snapshot(ctx)
  93. if err != nil {
  94. t.Fatal(err)
  95. }
  96. defer rc1.Close()
  97. cancel()
  98. _, err = io.Copy(ioutil.Discard, rc1)
  99. if err != context.Canceled {
  100. t.Errorf("expected %v, got %v", context.Canceled, err)
  101. }
  102. // reading snapshot with deadline exceeded should error out
  103. ctx, cancel = context.WithTimeout(context.Background(), time.Second)
  104. defer cancel()
  105. rc2, err := clus.RandClient().Snapshot(ctx)
  106. if err != nil {
  107. t.Fatal(err)
  108. }
  109. defer rc2.Close()
  110. time.Sleep(2 * time.Second)
  111. _, err = io.Copy(ioutil.Discard, rc2)
  112. if err != nil && err != context.DeadlineExceeded {
  113. t.Errorf("expected %v, got %v", context.DeadlineExceeded, err)
  114. }
  115. }
  116. // TestMaintenanceSnapshotErrorInflight ensures that inflight context cancel/timeout
  117. // fails snapshot reading with corresponding context errors.
  118. func TestMaintenanceSnapshotErrorInflight(t *testing.T) {
  119. defer testutil.AfterTest(t)
  120. clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 1})
  121. defer clus.Terminate(t)
  122. // take about 1-second to read snapshot
  123. clus.Members[0].Stop(t)
  124. dpath := filepath.Join(clus.Members[0].DataDir, "member", "snap", "db")
  125. b := backend.NewDefaultBackend(dpath)
  126. s := mvcc.NewStore(zap.NewExample(), b, &lease.FakeLessor{}, nil)
  127. rev := 100000
  128. for i := 2; i <= rev; i++ {
  129. s.Put([]byte(fmt.Sprintf("%10d", i)), bytes.Repeat([]byte("a"), 1024), lease.NoLease)
  130. }
  131. s.Close()
  132. b.Close()
  133. clus.Members[0].Restart(t)
  134. // reading snapshot with canceled context should error out
  135. ctx, cancel := context.WithCancel(context.Background())
  136. rc1, err := clus.RandClient().Snapshot(ctx)
  137. if err != nil {
  138. t.Fatal(err)
  139. }
  140. defer rc1.Close()
  141. donec := make(chan struct{})
  142. go func() {
  143. time.Sleep(300 * time.Millisecond)
  144. cancel()
  145. close(donec)
  146. }()
  147. _, err = io.Copy(ioutil.Discard, rc1)
  148. if err != nil && err != context.Canceled {
  149. t.Errorf("expected %v, got %v", context.Canceled, err)
  150. }
  151. <-donec
  152. // reading snapshot with deadline exceeded should error out
  153. ctx, cancel = context.WithTimeout(context.Background(), time.Second)
  154. defer cancel()
  155. rc2, err := clus.RandClient().Snapshot(ctx)
  156. if err != nil {
  157. t.Fatal(err)
  158. }
  159. defer rc2.Close()
  160. // 300ms left and expect timeout while snapshot reading is in progress
  161. time.Sleep(700 * time.Millisecond)
  162. _, err = io.Copy(ioutil.Discard, rc2)
  163. if err != nil && err != context.DeadlineExceeded {
  164. t.Errorf("expected %v, got %v", context.DeadlineExceeded, err)
  165. }
  166. }