1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
|
package cache
import (
"context"
"io"
"net"
"sync"
"testing"
"github.com/stretchr/testify/require"
diskcache "gitlab.com/gitlab-org/gitaly/v16/internal/cache"
"gitlab.com/gitlab-org/gitaly/v16/internal/praefect/protoregistry"
"gitlab.com/gitlab-org/gitaly/v16/internal/testhelper"
"gitlab.com/gitlab-org/gitaly/v16/proto/go/gitalypb"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/health/grpc_health_v1"
)
func TestInvalidators(t *testing.T) {
t.Parallel()
ctx := testhelper.Context(t)
repo := &gitalypb.Repository{
GitAlternateObjectDirectories: []string{"1"},
GitObjectDirectory: "1",
GlProjectPath: "1",
GlRepository: "1",
RelativePath: "1",
StorageName: "1",
}
for _, tc := range []struct {
desc string
invokeRPC func(*testing.T, *grpc.ClientConn)
expectedInvalidations []*gitalypb.Repository
}{
{
desc: "streaming accessor does not invalidate cache",
invokeRPC: func(t *testing.T, conn *grpc.ClientConn) {
stream, err := gitalypb.NewRepositoryServiceClient(conn).GetConfig(ctx, &gitalypb.GetConfigRequest{
Repository: repo,
})
require.NoError(t, err)
_, err = stream.Recv()
require.Equal(t, err, io.EOF)
},
},
{
desc: "streaming mutator invalidates cache",
invokeRPC: func(t *testing.T, conn *grpc.ClientConn) {
stream, err := gitalypb.NewSSHServiceClient(conn).SSHReceivePack(ctx)
require.NoError(t, err)
require.NoError(t, stream.Send(&gitalypb.SSHReceivePackRequest{
Repository: repo,
}))
_, err = stream.Recv()
require.Equal(t, err, io.EOF)
},
expectedInvalidations: []*gitalypb.Repository{repo},
},
{
desc: "unary accessor does not invalidate cache",
invokeRPC: func(t *testing.T, conn *grpc.ClientConn) {
_, err := gitalypb.NewRepositoryServiceClient(conn).RepositoryExists(ctx, &gitalypb.RepositoryExistsRequest{
Repository: repo,
})
require.NoError(t, err)
},
},
{
desc: "unary maintainer does not invalidate cache",
invokeRPC: func(t *testing.T, conn *grpc.ClientConn) {
_, err := gitalypb.NewRepositoryServiceClient(conn).OptimizeRepository(ctx, &gitalypb.OptimizeRepositoryRequest{
Repository: repo,
})
require.NoError(t, err)
},
},
{
desc: "unary mutator invalidates cache",
invokeRPC: func(t *testing.T, conn *grpc.ClientConn) {
_, err := gitalypb.NewRepositoryServiceClient(conn).WriteRef(ctx, &gitalypb.WriteRefRequest{
Repository: repo,
})
require.NoError(t, err)
},
expectedInvalidations: []*gitalypb.Repository{repo},
},
{
desc: "health check does not invalidate cache",
invokeRPC: func(t *testing.T, conn *grpc.ClientConn) {
_, err := grpc_health_v1.NewHealthClient(conn).Check(ctx, &grpc_health_v1.HealthCheckRequest{
Service: "TestService",
})
require.NoError(t, err)
},
},
{
desc: "intercepted method does not invalidate cache",
invokeRPC: func(t *testing.T, conn *grpc.ClientConn) {
_, err := gitalypb.NewPraefectInfoServiceClient(conn).GetRepositoryMetadata(ctx, &gitalypb.GetRepositoryMetadataRequest{})
require.NoError(t, err)
},
},
} {
t.Run(tc.desc, func(t *testing.T) {
mockCache := newMockCache()
server := grpc.NewServer(
grpc.StreamInterceptor(StreamInvalidator(mockCache, protoregistry.GitalyProtoPreregistered)),
grpc.UnaryInterceptor(UnaryInvalidator(mockCache, protoregistry.GitalyProtoPreregistered)),
)
service := &testService{
requestCh: make(chan bool, 1),
}
gitalypb.RegisterSSHServiceServer(server, service)
gitalypb.RegisterRepositoryServiceServer(server, service)
gitalypb.RegisterPraefectInfoServiceServer(server, service)
grpc_health_v1.RegisterHealthServer(server, service)
listener, err := net.Listen("tcp", ":0")
require.NoError(t, err)
go func() {
testhelper.MustServe(t, server, listener)
}()
t.Cleanup(server.Stop)
conn, err := grpc.DialContext(
ctx,
listener.Addr().String(),
grpc.WithBlock(),
grpc.WithTransportCredentials(insecure.NewCredentials()),
)
require.NoError(t, err)
tc.invokeRPC(t, conn)
// Check that the RPC has been executed.
require.True(t, <-service.requestCh)
// Verify that the repository's cache was invalidated as expected.
testhelper.ProtoEqual(t, tc.expectedInvalidations, mockCache.invalidatedRepos)
// Verify that all leases that were created have ended.
require.Equal(t, len(mockCache.invalidatedRepos), mockCache.endedLeases.count)
// And furthermore, verify that we didn't see an error.
require.Empty(t, MethodErrCount.Method)
})
}
}
// mockCache allows us to relay back via channel which repos are being
// invalidated in the cache
type mockCache struct {
invalidatedRepos []*gitalypb.Repository
endedLeases *struct {
sync.RWMutex
count int
}
}
func newMockCache() *mockCache {
return &mockCache{
endedLeases: &struct {
sync.RWMutex
count int
}{},
}
}
func (mc *mockCache) EndLease(_ context.Context) error {
mc.endedLeases.Lock()
defer mc.endedLeases.Unlock()
mc.endedLeases.count++
return nil
}
func (mc *mockCache) StartLease(repo *gitalypb.Repository) (diskcache.LeaseEnder, error) {
mc.invalidatedRepos = append(mc.invalidatedRepos, repo)
return mc, nil
}
type testService struct {
gitalypb.UnimplementedSSHServiceServer
gitalypb.UnimplementedRepositoryServiceServer
gitalypb.UnimplementedPraefectInfoServiceServer
grpc_health_v1.UnimplementedHealthServer
requestCh chan bool
}
func (ts *testService) SSHReceivePack(server gitalypb.SSHService_SSHReceivePackServer) error {
_, err := server.Recv()
ts.requestCh <- true
return err
}
func (ts *testService) GetConfig(*gitalypb.GetConfigRequest, gitalypb.RepositoryService_GetConfigServer) error {
ts.requestCh <- true
return nil
}
func (ts *testService) WriteRef(context.Context, *gitalypb.WriteRefRequest) (*gitalypb.WriteRefResponse, error) {
ts.requestCh <- true
return &gitalypb.WriteRefResponse{}, nil
}
func (ts *testService) RepositoryExists(context.Context, *gitalypb.RepositoryExistsRequest) (*gitalypb.RepositoryExistsResponse, error) {
ts.requestCh <- true
return &gitalypb.RepositoryExistsResponse{}, nil
}
func (ts *testService) OptimizeRepository(context.Context, *gitalypb.OptimizeRepositoryRequest) (*gitalypb.OptimizeRepositoryResponse, error) {
ts.requestCh <- true
return &gitalypb.OptimizeRepositoryResponse{}, nil
}
func (ts *testService) Check(context.Context, *grpc_health_v1.HealthCheckRequest) (*grpc_health_v1.HealthCheckResponse, error) {
ts.requestCh <- true
return &grpc_health_v1.HealthCheckResponse{}, nil
}
func (ts *testService) GetRepositoryMetadata(context.Context, *gitalypb.GetRepositoryMetadataRequest) (*gitalypb.GetRepositoryMetadataResponse, error) {
ts.requestCh <- true
return &gitalypb.GetRepositoryMetadataResponse{}, nil
}
|