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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
|
// +build postgres
package datastore
import (
"fmt"
"strings"
"testing"
"github.com/prometheus/client_golang/prometheus/testutil"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/require"
"gitlab.com/gitlab-org/gitaly/internal/testhelper"
)
// HealthCheckerFunc is an adapter to turn a conforming function in to a HealthChecker.
type StaticHealthChecker map[string][]string
func (hc StaticHealthChecker) HealthyNodes() map[string][]string { return hc }
func TestRepositoryStoreCollector(t *testing.T) {
ctx, cancel := testhelper.Context()
defer cancel()
db := getDB(t)
rs := NewPostgresRepositoryStore(db, nil)
state := map[string]map[string]map[string]int{
"some-read-only": {
"read-only": {
"vs-primary": 0,
"repo-primary": 0,
"secondary": 1,
},
"writable": {
"vs-primary": 1,
"repo-primary": 1,
"secondary": 1,
},
"repo-writable": {
"vs-primary": 0,
"repo-primary": 1,
"secondary": 1,
},
},
"all-writable": {
"writable": {
"vs-primary": 0,
"repo-primary": 0,
},
},
"unconfigured": {
"read-only": {
"secondary": 1,
},
},
"no-records": {},
"no-primary": {
"read-only": {
"secondary": 0,
},
},
}
for virtualStorage, relativePaths := range state {
demoted := false
if virtualStorage == "no-primary" {
demoted = true
}
_, err := db.ExecContext(ctx, `
INSERT INTO shard_primaries (shard_name, node_name, elected_by_praefect, elected_at, demoted)
VALUES ($1, 'vs-primary', 'not-needed', now(), $2)
`, virtualStorage, demoted,
)
require.NoError(t, err)
for relativePath, storages := range relativePaths {
if virtualStorage != "no-primary" {
_, err := db.ExecContext(ctx, `
INSERT INTO repositories (virtual_storage, relative_path, "primary")
VALUES ($1, $2, 'repo-primary')
`, virtualStorage, relativePath,
)
require.NoError(t, err)
}
for storage, generation := range storages {
require.NoError(t, rs.SetGeneration(ctx, virtualStorage, relativePath, storage, generation))
}
}
}
var virtualStorages []string
for vs := range state {
if vs == "unconfigured" {
continue
}
virtualStorages = append(virtualStorages, vs)
}
for _, tc := range []struct {
desc string
repositoryScoped bool
someReadOnlyCount int
}{
{
desc: "repository scoped",
someReadOnlyCount: 1,
repositoryScoped: true,
},
{
desc: "virtual storage scoped",
someReadOnlyCount: 2,
},
} {
t.Run(tc.desc, func(t *testing.T) {
c := NewRepositoryStoreCollector(logrus.New(), virtualStorages, db, tc.repositoryScoped, nil)
require.NoError(t, testutil.CollectAndCompare(c, strings.NewReader(fmt.Sprintf(`
# HELP gitaly_praefect_read_only_repositories Number of repositories in read-only mode within a virtual storage.
# TYPE gitaly_praefect_read_only_repositories gauge
gitaly_praefect_read_only_repositories{virtual_storage="all-writable"} 0
gitaly_praefect_read_only_repositories{virtual_storage="no-records"} 0
gitaly_praefect_read_only_repositories{virtual_storage="no-primary"} 1
gitaly_praefect_read_only_repositories{virtual_storage="some-read-only"} %d
`, tc.someReadOnlyCount))))
})
}
}
func TestRepositoryStoreCollector_lazy_failover(t *testing.T) {
for _, tc := range []struct {
desc string
existingGenerations map[string]int
existingAssignments []string
noPrimary bool
healthyNodes StaticHealthChecker
repositoryScopedCount int
virtualStorageScopedCount int
}{
{
desc: "no records",
},
{
desc: "outdated secondary",
existingGenerations: map[string]int{
"primary": 0,
},
},
{
desc: "no primary with a candidate",
noPrimary: true,
existingGenerations: map[string]int{
"secondary": 1,
},
repositoryScopedCount: 0,
virtualStorageScopedCount: 1,
},
{
desc: "no primary with no candidate",
noPrimary: true,
healthyNodes: StaticHealthChecker{"virtual-storage": {"primary"}},
existingGenerations: map[string]int{
"secondary": 1,
},
repositoryScopedCount: 1,
virtualStorageScopedCount: 1,
},
{
desc: "no record unhealthy primary",
healthyNodes: StaticHealthChecker{"virtual-storage": {"secondary"}},
existingGenerations: map[string]int{
"secondary": 0,
},
repositoryScopedCount: 0,
virtualStorageScopedCount: 1,
},
{
desc: "no record healthy primary",
existingGenerations: map[string]int{
"secondary": 0,
},
repositoryScopedCount: 1,
virtualStorageScopedCount: 1,
},
{
desc: "outdated unhealthy primary",
healthyNodes: StaticHealthChecker{"virtual-storage": {"secondary"}},
existingGenerations: map[string]int{
"primary": 0,
"secondary": 1,
},
repositoryScopedCount: 0,
virtualStorageScopedCount: 1,
},
{
desc: "outdated healthy primary",
existingGenerations: map[string]int{
"primary": 0,
"secondary": 1,
},
repositoryScopedCount: 1,
virtualStorageScopedCount: 1,
},
{
desc: "in-sync unhealthy primary with a candidate",
healthyNodes: StaticHealthChecker{"virtual-storage": {"secondary"}},
existingGenerations: map[string]int{
"primary": 0,
"secondary": 0,
},
repositoryScopedCount: 0,
virtualStorageScopedCount: 0,
},
{
desc: "in-sync unhealthy primary without a candidate",
healthyNodes: StaticHealthChecker{"virtual-storage": {}},
existingGenerations: map[string]int{
"primary": 0,
"secondary": 0,
},
repositoryScopedCount: 1,
virtualStorageScopedCount: 0,
},
{
desc: "secondary is unassigned",
healthyNodes: StaticHealthChecker{"virtual-storage": {"secondary"}},
existingGenerations: map[string]int{
"primary": 1,
"secondary": 1,
},
existingAssignments: []string{"primary"},
repositoryScopedCount: 1,
virtualStorageScopedCount: 0,
},
{
desc: "failsover to assigned",
healthyNodes: StaticHealthChecker{"virtual-storage": {"secondary"}},
existingGenerations: map[string]int{
"primary": 1,
"secondary": 1,
},
existingAssignments: []string{"primary", "secondary"},
repositoryScopedCount: 0,
virtualStorageScopedCount: 0,
},
} {
t.Run(tc.desc, func(t *testing.T) {
ctx, cancel := testhelper.Context()
defer cancel()
db := getDB(t)
staticQueries := []string{
`
INSERT INTO repositories (virtual_storage, relative_path)
VALUES ('virtual-storage', 'repository')
`,
}
if !tc.noPrimary {
staticQueries = append(staticQueries,
`
UPDATE repositories SET "primary" = 'primary'
`,
`
INSERT INTO shard_primaries (shard_name, node_name, elected_by_praefect, elected_at)
VALUES ('virtual-storage', 'primary', 'ignored', now())
`,
)
}
for _, q := range staticQueries {
_, err := db.ExecContext(ctx, q)
require.NoError(t, err)
}
for storage, generation := range tc.existingGenerations {
_, err := db.ExecContext(ctx, `
INSERT INTO storage_repositories (virtual_storage, relative_path, storage, generation)
VALUES ('virtual-storage', 'repository', $1, $2)
`, storage, generation)
require.NoError(t, err)
}
for _, storage := range tc.existingAssignments {
_, err := db.ExecContext(ctx, `
INSERT INTO repository_assignments (virtual_storage, relative_path, storage)
VALUES ('virtual-storage', 'repository', $1)
`, storage)
require.NoError(t, err)
}
for _, scope := range []struct {
desc string
repositoryScoped bool
readOnlyCount int
}{
{
desc: "virtual storage primaries",
repositoryScoped: false,
readOnlyCount: tc.virtualStorageScopedCount,
},
{
desc: "repository primaries",
repositoryScoped: true,
readOnlyCount: tc.repositoryScopedCount,
},
} {
t.Run(scope.desc, func(t *testing.T) {
healthyNodes := StaticHealthChecker{"virtual-storage": {"primary", "secondary"}}
if tc.healthyNodes != nil {
healthyNodes = tc.healthyNodes
}
c := NewRepositoryStoreCollector(
logrus.New(),
[]string{"virtual-storage"},
db,
scope.repositoryScoped,
healthyNodes,
)
require.NoError(t, testutil.CollectAndCompare(c, strings.NewReader(fmt.Sprintf(`
# HELP gitaly_praefect_read_only_repositories Number of repositories in read-only mode within a virtual storage.
# TYPE gitaly_praefect_read_only_repositories gauge
gitaly_praefect_read_only_repositories{virtual_storage="virtual-storage"} %d
`, scope.readOnlyCount))))
})
}
})
}
}
|