maintenance_test.go 5.2 KB

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