Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/gitlab-org/gitaly.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaul Okstad <pokstad@gitlab.com>2019-07-19 02:05:26 +0300
committerPaul Okstad <pokstad@gitlab.com>2019-07-19 02:05:26 +0300
commita3047689e849690c7c36b85d6092433df6732422 (patch)
tree5dd9c91812a0e7440bc9654e66a282d25c365510
parent2212d1aff8ed94c23d08377ea67ab626bc9f0c97 (diff)
parentd882ac830951e0ca861ff357921ea0ff8a0a793c (diff)
Merge branch 'po-cache-unary-intercept' into 'master'
Unary gRPC interceptor for cache invalidation See merge request gitlab-org/gitaly!1371
-rw-r--r--changelogs/unreleased/po-cache-unary-intercept.yml5
-rw-r--r--internal/middleware/cache/cache.go42
-rw-r--r--internal/middleware/cache/cache_test.go35
-rw-r--r--internal/middleware/cache/testdata/stream.pb.go106
-rw-r--r--internal/middleware/cache/testdata/stream.proto13
5 files changed, 178 insertions, 23 deletions
diff --git a/changelogs/unreleased/po-cache-unary-intercept.yml b/changelogs/unreleased/po-cache-unary-intercept.yml
new file mode 100644
index 000000000..6aa547554
--- /dev/null
+++ b/changelogs/unreleased/po-cache-unary-intercept.yml
@@ -0,0 +1,5 @@
+---
+title: Unary gRPC interceptor for cache invalidation
+merge_request: 1371
+author:
+type: performance
diff --git a/internal/middleware/cache/cache.go b/internal/middleware/cache/cache.go
index 6ad16a59c..af0dd38e5 100644
--- a/internal/middleware/cache/cache.go
+++ b/internal/middleware/cache/cache.go
@@ -1,6 +1,7 @@
package cache
import (
+ "context"
"fmt"
"sync"
@@ -75,6 +76,47 @@ func StreamInvalidator(ci Invalidator, reg *protoregistry.Registry) grpc.StreamS
}
}
+// UnaryInvalidator will invalidate any mutating RPC that targets a
+// repository in a gRPC unary RPC
+func UnaryInvalidator(ci Invalidator, reg *protoregistry.Registry) grpc.UnaryServerInterceptor {
+ return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp interface{}, err error) {
+ mInfo, err := reg.LookupMethod(info.FullMethod)
+ countRPCType(mInfo)
+ if err != nil {
+ logrus.WithField("full_method_name", info.FullMethod).Errorf("unable to lookup method information for %+v", info)
+ }
+
+ if mInfo.Operation == protoregistry.OpAccessor {
+ return handler(ctx, req)
+ }
+
+ pbReq, ok := req.(proto.Message)
+ if !ok {
+ return nil, fmt.Errorf("cache invalidation expected protobuf request, but got %T", req)
+ }
+
+ target, err := mInfo.TargetRepo(pbReq)
+ if err != nil {
+ return nil, err
+ }
+
+ le, err := ci.StartLease(target)
+ if err != nil {
+ return nil, err
+ }
+
+ // wrap the handler to ensure the lease is always ended
+ return func() (resp interface{}, err error) {
+ defer func() {
+ if err := le.EndLease(ctx); err != nil {
+ logrus.Errorf("unable to end lease: %q", err)
+ }
+ }()
+ return handler(ctx, req)
+ }()
+ }
+}
+
type recvMsgCallback func(interface{}, error) error
func invalidateCache(ci Invalidator, mInfo protoregistry.MethodInfo, handler grpc.StreamHandler) (grpc.StreamHandler, recvMsgCallback) {
diff --git a/internal/middleware/cache/cache_test.go b/internal/middleware/cache/cache_test.go
index c04cb4d6a..332c9316a 100644
--- a/internal/middleware/cache/cache_test.go
+++ b/internal/middleware/cache/cache_test.go
@@ -21,7 +21,7 @@ import (
)
//go:generate make testdata/stream.pb.go
-func TestStreamInvalidator(t *testing.T) {
+func TestInvalidators(t *testing.T) {
mCache := newMockCache()
reg := protoregistry.New()
@@ -33,6 +33,11 @@ func TestStreamInvalidator(t *testing.T) {
cache.StreamInvalidator(mCache, reg),
),
),
+ grpc.UnaryInterceptor(
+ grpc.UnaryServerInterceptor(
+ cache.UnaryInvalidator(mCache, reg),
+ ),
+ ),
)
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
@@ -70,8 +75,8 @@ func TestStreamInvalidator(t *testing.T) {
StorageName: "3",
}
- expectedSvcRequests := []gitalypb.Repository{*repo1, *repo2, *repo3}
- expectedInvalidations := []gitalypb.Repository{*repo2, *repo3}
+ expectedSvcRequests := []gitalypb.Repository{*repo1, *repo2, *repo3, *repo1, *repo2}
+ expectedInvalidations := []gitalypb.Repository{*repo2, *repo3, *repo1}
// Should NOT trigger cache invalidation
c, err := cli.ClientStreamRepoAccessor(ctx, &testdata.Request{
@@ -97,9 +102,21 @@ func TestStreamInvalidator(t *testing.T) {
_, err = c.Recv() // make client call synchronous by waiting for close
assert.Equal(t, err, io.EOF)
+ // Should trigger cache invalidation
+ _, err = cli.ClientUnaryRepoMutator(ctx, &testdata.Request{
+ Destination: repo1,
+ })
+ require.NoError(t, err)
+
+ // Should NOT trigger cache invalidation
+ _, err = cli.ClientUnaryRepoAccessor(ctx, &testdata.Request{
+ Destination: repo2,
+ })
+ require.NoError(t, err)
+
require.Equal(t, expectedInvalidations, mCache.(*mockCache).invalidatedRepos)
require.Equal(t, expectedSvcRequests, svc.repoRequests)
- require.Equal(t, 2, mCache.(*mockCache).endedLeases.count)
+ require.Equal(t, 3, mCache.(*mockCache).endedLeases.count)
}
// mockCache allows us to relay back via channel which repos are being
@@ -181,3 +198,13 @@ func (ts *testSvc) ClientStreamRepoAccessor(req *testdata.Request, _ testdata.Te
ts.repoRequests = append(ts.repoRequests, *req.GetDestination())
return nil
}
+
+func (ts *testSvc) ClientUnaryRepoMutator(_ context.Context, req *testdata.Request) (*testdata.Response, error) {
+ ts.repoRequests = append(ts.repoRequests, *req.GetDestination())
+ return &testdata.Response{}, nil
+}
+
+func (ts *testSvc) ClientUnaryRepoAccessor(_ context.Context, req *testdata.Request) (*testdata.Response, error) {
+ ts.repoRequests = append(ts.repoRequests, *req.GetDestination())
+ return &testdata.Response{}, nil
+}
diff --git a/internal/middleware/cache/testdata/stream.pb.go b/internal/middleware/cache/testdata/stream.pb.go
index cbce32ff3..f7e052bc6 100644
--- a/internal/middleware/cache/testdata/stream.pb.go
+++ b/internal/middleware/cache/testdata/stream.pb.go
@@ -101,24 +101,25 @@ func init() {
func init() { proto.RegisterFile("stream.proto", fileDescriptor_bb17ef3f514bfe54) }
var fileDescriptor_bb17ef3f514bfe54 = []byte{
- // 257 bytes of a gzipped FileDescriptorProto
- 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x8c, 0xd0, 0xb1, 0x4e, 0xc3, 0x30,
- 0x10, 0x06, 0x60, 0xb9, 0x12, 0x25, 0xb8, 0x5d, 0xf0, 0x42, 0x95, 0x09, 0x65, 0xca, 0x82, 0x5d,
- 0x0a, 0x7b, 0x55, 0x18, 0x51, 0x97, 0x94, 0x89, 0xed, 0xea, 0x9c, 0x52, 0x4b, 0x8e, 0x1d, 0x7c,
- 0x57, 0x50, 0x9f, 0xa4, 0xcf, 0xc0, 0x2b, 0x76, 0x42, 0x34, 0x54, 0xaa, 0x98, 0xd8, 0xac, 0x5f,
- 0xd6, 0x77, 0xff, 0x9d, 0x1c, 0x13, 0x27, 0x84, 0x56, 0x77, 0x29, 0x72, 0x54, 0x19, 0x23, 0x71,
- 0x0d, 0x0c, 0xf9, 0x98, 0x36, 0x90, 0xb0, 0xee, 0xf3, 0x62, 0x2e, 0x2f, 0x2b, 0x7c, 0xdf, 0x22,
- 0xb1, 0x7a, 0x94, 0xa3, 0x1a, 0x89, 0x5d, 0x00, 0x76, 0x31, 0x4c, 0xc4, 0xad, 0x28, 0x47, 0x33,
- 0xa5, 0x1b, 0xc7, 0xe0, 0x77, 0xba, 0xc2, 0x2e, 0x92, 0xe3, 0x98, 0x76, 0xd5, 0xf9, 0xb7, 0x42,
- 0xca, 0xac, 0x42, 0xea, 0x62, 0x20, 0x9c, 0x7d, 0x09, 0x39, 0x7a, 0x45, 0xe2, 0x15, 0xa6, 0x0f,
- 0x67, 0x51, 0x2d, 0xe5, 0xcd, 0xb3, 0x77, 0x18, 0x78, 0x75, 0xac, 0xf2, 0x43, 0x2c, 0xb7, 0x0c,
- 0x1c, 0x93, 0xba, 0xd6, 0xa7, 0x42, 0xfa, 0x77, 0x7e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, 0x8b, 0xbd, 0x7c, 0x0f, 0x00, 0x00, 0xff,
+ 0xff, 0xa3, 0x8a, 0x30, 0x23, 0x00, 0x02, 0x00, 0x00,
}
// Reference imports to suppress errors if they are not otherwise used.
@@ -135,6 +136,8 @@ const _ = grpc.SupportPackageIsVersion4
type TestServiceClient interface {
ClientStreamRepoMutator(ctx context.Context, in *Request, opts ...grpc.CallOption) (TestService_ClientStreamRepoMutatorClient, error)
ClientStreamRepoAccessor(ctx context.Context, in *Request, opts ...grpc.CallOption) (TestService_ClientStreamRepoAccessorClient, error)
+ ClientUnaryRepoMutator(ctx context.Context, in *Request, opts ...grpc.CallOption) (*Response, error)
+ ClientUnaryRepoAccessor(ctx context.Context, in *Request, opts ...grpc.CallOption) (*Response, error)
}
type testServiceClient struct {
@@ -209,10 +212,30 @@ func (x *testServiceClientStreamRepoAccessorClient) Recv() (*Response, error) {
return m, nil
}
+func (c *testServiceClient) ClientUnaryRepoMutator(ctx context.Context, in *Request, opts ...grpc.CallOption) (*Response, error) {
+ out := new(Response)
+ err := c.cc.Invoke(ctx, "/testdata.TestService/ClientUnaryRepoMutator", in, out, opts...)
+ if err != nil {
+ return nil, err
+ }
+ return out, nil
+}
+
+func (c *testServiceClient) ClientUnaryRepoAccessor(ctx context.Context, in *Request, opts ...grpc.CallOption) (*Response, error) {
+ out := new(Response)
+ err := c.cc.Invoke(ctx, "/testdata.TestService/ClientUnaryRepoAccessor", in, out, opts...)
+ if err != nil {
+ return nil, err
+ }
+ return out, nil
+}
+
// TestServiceServer is the server API for TestService service.
type TestServiceServer interface {
ClientStreamRepoMutator(*Request, TestService_ClientStreamRepoMutatorServer) error
ClientStreamRepoAccessor(*Request, TestService_ClientStreamRepoAccessorServer) error
+ ClientUnaryRepoMutator(context.Context, *Request) (*Response, error)
+ ClientUnaryRepoAccessor(context.Context, *Request) (*Response, error)
}
func RegisterTestServiceServer(s *grpc.Server, srv TestServiceServer) {
@@ -261,10 +284,55 @@ func (x *testServiceClientStreamRepoAccessorServer) Send(m *Response) error {
return x.ServerStream.SendMsg(m)
}
+func _TestService_ClientUnaryRepoMutator_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
+ in := new(Request)
+ if err := dec(in); err != nil {
+ return nil, err
+ }
+ if interceptor == nil {
+ return srv.(TestServiceServer).ClientUnaryRepoMutator(ctx, in)
+ }
+ info := &grpc.UnaryServerInfo{
+ Server: srv,
+ FullMethod: "/testdata.TestService/ClientUnaryRepoMutator",
+ }
+ handler := func(ctx context.Context, req interface{}) (interface{}, error) {
+ return srv.(TestServiceServer).ClientUnaryRepoMutator(ctx, req.(*Request))
+ }
+ return interceptor(ctx, in, info, handler)
+}
+
+func _TestService_ClientUnaryRepoAccessor_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
+ in := new(Request)
+ if err := dec(in); err != nil {
+ return nil, err
+ }
+ if interceptor == nil {
+ return srv.(TestServiceServer).ClientUnaryRepoAccessor(ctx, in)
+ }
+ info := &grpc.UnaryServerInfo{
+ Server: srv,
+ FullMethod: "/testdata.TestService/ClientUnaryRepoAccessor",
+ }
+ handler := func(ctx context.Context, req interface{}) (interface{}, error) {
+ return srv.(TestServiceServer).ClientUnaryRepoAccessor(ctx, req.(*Request))
+ }
+ return interceptor(ctx, in, info, handler)
+}
+
var _TestService_serviceDesc = grpc.ServiceDesc{
ServiceName: "testdata.TestService",
HandlerType: (*TestServiceServer)(nil),
- Methods: []grpc.MethodDesc{},
+ Methods: []grpc.MethodDesc{
+ {
+ MethodName: "ClientUnaryRepoMutator",
+ Handler: _TestService_ClientUnaryRepoMutator_Handler,
+ },
+ {
+ MethodName: "ClientUnaryRepoAccessor",
+ Handler: _TestService_ClientUnaryRepoAccessor_Handler,
+ },
+ },
Streams: []grpc.StreamDesc{
{
StreamName: "ClientStreamRepoMutator",
diff --git a/internal/middleware/cache/testdata/stream.proto b/internal/middleware/cache/testdata/stream.proto
index f10aa08af..20f2c5dcb 100644
--- a/internal/middleware/cache/testdata/stream.proto
+++ b/internal/middleware/cache/testdata/stream.proto
@@ -25,4 +25,17 @@ service TestService {
op: ACCESSOR
};
}
+
+ rpc ClientUnaryRepoMutator(Request) returns (Response) {
+ option (gitaly.op_type) = {
+ op: MUTATOR
+ target_repository_field: "1"
+ };
+ }
+
+ rpc ClientUnaryRepoAccessor(Request) returns (Response) {
+ option (gitaly.op_type) = {
+ op: ACCESSOR
+ };
+ }
}