v3_grpc_inflight_test.go 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. // Copyright 2016 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. "sync"
  17. "testing"
  18. "time"
  19. pb "github.com/coreos/etcd/etcdserver/etcdserverpb"
  20. "github.com/coreos/etcd/pkg/testutil"
  21. "golang.org/x/net/context"
  22. "google.golang.org/grpc"
  23. )
  24. // TestV3MaintenanceDefragmentInflightRange ensures inflight range requests
  25. // does not panic the mvcc backend while defragment is running.
  26. func TestV3MaintenanceDefragmentInflightRange(t *testing.T) {
  27. defer testutil.AfterTest(t)
  28. clus := NewClusterV3(t, &ClusterConfig{Size: 1})
  29. defer clus.Terminate(t)
  30. cli := clus.RandClient()
  31. kvc := toGRPC(cli).KV
  32. if _, err := kvc.Put(context.Background(), &pb.PutRequest{Key: []byte("foo"), Value: []byte("bar")}); err != nil {
  33. t.Fatal(err)
  34. }
  35. ctx, cancel := context.WithTimeout(context.Background(), time.Second)
  36. donec := make(chan struct{})
  37. go func() {
  38. defer close(donec)
  39. kvc.Range(ctx, &pb.RangeRequest{Key: []byte("foo")})
  40. }()
  41. mvc := toGRPC(cli).Maintenance
  42. mvc.Defragment(context.Background(), &pb.DefragmentRequest{})
  43. cancel()
  44. <-donec
  45. }
  46. // TestV3KVInflightRangeRequests ensures that inflight requests
  47. // (sent before server shutdown) are gracefully handled by server-side.
  48. // They are either finished or canceled, but never crash the backend.
  49. // See https://github.com/coreos/etcd/issues/7322 for more detail.
  50. func TestV3KVInflightRangeRequests(t *testing.T) {
  51. defer testutil.AfterTest(t)
  52. clus := NewClusterV3(t, &ClusterConfig{Size: 1})
  53. defer clus.Terminate(t)
  54. cli := clus.RandClient()
  55. kvc := toGRPC(cli).KV
  56. if _, err := kvc.Put(context.Background(), &pb.PutRequest{Key: []byte("foo"), Value: []byte("bar")}); err != nil {
  57. t.Fatal(err)
  58. }
  59. ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
  60. reqN := 10 // use 500+ for fast machine
  61. var wg sync.WaitGroup
  62. wg.Add(reqN)
  63. for i := 0; i < reqN; i++ {
  64. go func() {
  65. defer wg.Done()
  66. _, err := kvc.Range(ctx, &pb.RangeRequest{Key: []byte("foo"), Serializable: true}, grpc.FailFast(false))
  67. if err != nil && grpc.ErrorDesc(err) != context.Canceled.Error() {
  68. t.Fatalf("inflight request should be canceld with %v, got %v", context.Canceled, err)
  69. }
  70. }()
  71. }
  72. clus.Members[0].Stop(t)
  73. cancel()
  74. wg.Wait()
  75. }