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
|
//go:build !gitaly_test_sha256
package middleware
import (
"context"
"net"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gitlab.com/gitlab-org/gitaly/v16/internal/git/gittest"
"gitlab.com/gitlab-org/gitaly/v16/internal/grpc/proxy"
"gitlab.com/gitlab-org/gitaly/v16/internal/praefect/nodes/tracker"
"gitlab.com/gitlab-org/gitaly/v16/internal/praefect/protoregistry"
"gitlab.com/gitlab-org/gitaly/v16/internal/structerr"
"gitlab.com/gitlab-org/gitaly/v16/internal/testhelper"
"gitlab.com/gitlab-org/gitaly/v16/internal/testhelper/testcfg"
"gitlab.com/gitlab-org/gitaly/v16/proto/go/gitalypb"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
)
type repositoryService struct {
gitalypb.UnimplementedRepositoryServiceServer
}
func (s *repositoryService) RepositoryExists(ctx context.Context, req *gitalypb.RepositoryExistsRequest) (*gitalypb.RepositoryExistsResponse, error) {
if req.GetRepository() == nil {
return nil, structerr.NewInternal("error")
}
return &gitalypb.RepositoryExistsResponse{}, nil
}
func (s *repositoryService) WriteRef(ctx context.Context, req *gitalypb.WriteRefRequest) (*gitalypb.WriteRefResponse, error) {
if req.GetRepository() == nil {
return nil, structerr.NewInternal("error")
}
return &gitalypb.WriteRefResponse{}, nil
}
func TestStreamInterceptor(t *testing.T) {
ctx := testhelper.Context(t)
isInErrorWindow := true
threshold := 5
errTracker, err := tracker.NewErrors(ctx, func(_, _ time.Time) bool {
return isInErrorWindow
}, uint32(threshold), uint32(threshold))
require.NoError(t, err)
nodeName := "node-1"
internalSrv := grpc.NewServer()
internalServerSocketPath := testhelper.GetTemporaryGitalySocketFileName(t)
lis, err := net.Listen("unix", internalServerSocketPath)
require.NoError(t, err)
gitalypb.RegisterRepositoryServiceServer(internalSrv, &repositoryService{})
go testhelper.MustServe(t, internalSrv, lis)
defer internalSrv.Stop()
srvOptions := []grpc.ServerOption{
grpc.ForceServerCodec(proxy.NewCodec()),
grpc.UnknownServiceHandler(proxy.TransparentHandler(func(ctx context.Context,
fullMethodName string,
peeker proxy.StreamPeeker,
) (*proxy.StreamParameters, error) {
cc, err := grpc.Dial("unix://"+internalServerSocketPath,
grpc.WithDefaultCallOptions(grpc.ForceCodec(proxy.NewCodec())),
grpc.WithTransportCredentials(insecure.NewCredentials()),
grpc.WithStreamInterceptor(StreamErrorHandler(protoregistry.GitalyProtoPreregistered, errTracker, nodeName)),
)
require.NoError(t, err)
t.Cleanup(func() { testhelper.MustClose(t, cc) })
f, err := peeker.Peek()
require.NoError(t, err)
return proxy.NewStreamParameters(proxy.Destination{Conn: cc, Ctx: ctx, Msg: f}, nil, func() error { return nil }, nil), nil
})),
}
praefectSocket := testhelper.GetTemporaryGitalySocketFileName(t)
praefectLis, err := net.Listen("unix", praefectSocket)
require.NoError(t, err)
praefectSrv := grpc.NewServer(srvOptions...)
defer praefectSrv.Stop()
go testhelper.MustServe(t, praefectSrv, praefectLis)
praefectCC, err := grpc.Dial("unix://"+praefectSocket, grpc.WithTransportCredentials(insecure.NewCredentials()))
defer testhelper.MustClose(t, praefectCC)
require.NoError(t, err)
client := gitalypb.NewRepositoryServiceClient(praefectCC)
cfg := testcfg.Build(t)
repo, _ := gittest.CreateRepository(t, ctx, cfg, gittest.CreateRepositoryConfig{
SkipCreationViaService: true,
Seed: gittest.SeedGitLabTest,
})
for i := 0; i < threshold; i++ {
_, err = client.RepositoryExists(ctx, &gitalypb.RepositoryExistsRequest{
Repository: repo,
})
require.NoError(t, err)
_, err = client.WriteRef(ctx, &gitalypb.WriteRefRequest{
Repository: repo,
})
require.NoError(t, err)
}
assert.False(t, errTracker.WriteThresholdReached(nodeName))
assert.False(t, errTracker.ReadThresholdReached(nodeName))
for i := 0; i < threshold; i++ {
_, err = client.RepositoryExists(ctx, &gitalypb.RepositoryExistsRequest{
Repository: nil,
})
require.Error(t, err)
_, err = client.WriteRef(ctx, &gitalypb.WriteRefRequest{
Repository: nil,
})
require.Error(t, err)
}
assert.True(t, errTracker.WriteThresholdReached(nodeName))
assert.True(t, errTracker.ReadThresholdReached(nodeName))
isInErrorWindow = false
for i := 0; i < threshold; i++ {
_, err = client.RepositoryExists(ctx, &gitalypb.RepositoryExistsRequest{
Repository: repo,
})
require.NoError(t, err)
_, err = client.WriteRef(ctx, &gitalypb.WriteRefRequest{
Repository: repo,
})
require.NoError(t, err)
}
assert.False(t, errTracker.WriteThresholdReached(nodeName))
assert.False(t, errTracker.ReadThresholdReached(nodeName))
}
|