v3_grpc_inflight_test.go 3.0 KB

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