v3_grpc_inflight_test.go 2.8 KB

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