v3_grpc_inflight_test.go 2.9 KB

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