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
|
package datastore
import (
"sync"
"testing"
"github.com/stretchr/testify/require"
"gitlab.com/gitlab-org/gitaly/v14/internal/praefect/config"
"gitlab.com/gitlab-org/gitaly/v14/internal/testhelper"
)
func TestMemoryReplicationEventQueue(t *testing.T) {
ctx, cancel := testhelper.Context()
defer cancel()
queue := NewMemoryReplicationEventQueue(config.Config{})
noEvents, err := queue.Dequeue(ctx, "praefect", "storage-1", 100500)
require.NoError(t, err)
require.Empty(t, noEvents, "no events as queue is empty")
noAcknowledged, err := queue.Acknowledge(ctx, JobStateCompleted, []uint64{1, 2, 3})
require.NoError(t, err)
require.Empty(t, noAcknowledged, "no acknowledged ids as queue is empty")
job1 := ReplicationJob{
Change: UpdateRepo,
RelativePath: "/project/path-1",
VirtualStorage: "praefect",
TargetNodeStorage: "storage-1",
SourceNodeStorage: "storage-0",
Params: nil,
}
eventType1 := ReplicationEvent{Job: job1}
event1, err := queue.Enqueue(ctx, eventType1)
require.NoError(t, err)
expEvent1 := ReplicationEvent{
ID: 1,
State: JobStateReady,
Attempt: 3,
LockID: "praefect|storage-1|/project/path-1",
CreatedAt: event1.CreatedAt, // it is a hack to have same time for both
Job: job1,
}
require.Equal(t, expEvent1, event1)
notAcknowledged1, err := queue.Acknowledge(ctx, JobStateCompleted, []uint64{event1.ID})
require.NoError(t, err)
require.Empty(t, notAcknowledged1, "no acknowledged ids as events were not dequeued")
job2 := ReplicationJob{
Change: UpdateRepo,
RelativePath: "/project/path-1",
VirtualStorage: "praefect",
TargetNodeStorage: "storage-2",
SourceNodeStorage: "storage-0",
Params: nil,
}
eventType2 := ReplicationEvent{Job: job2}
event2, err := queue.Enqueue(ctx, eventType2)
require.NoError(t, err)
expEvent2 := ReplicationEvent{
ID: 2,
State: JobStateReady,
Attempt: 3,
LockID: "praefect|storage-2|/project/path-1",
CreatedAt: event2.CreatedAt, // it is a hack to have same time for both
Job: job2,
}
require.Equal(t, expEvent2, event2)
dequeuedAttempt1, err := queue.Dequeue(ctx, "praefect", "storage-1", 100500)
require.NoError(t, err)
require.Len(t, dequeuedAttempt1, 1, "only single event must be fetched for this storage")
expAttempt1 := ReplicationEvent{
ID: 1,
State: JobStateInProgress,
Attempt: 2,
LockID: "praefect|storage-1|/project/path-1",
CreatedAt: event1.CreatedAt, // it is a hack to have same time for both
UpdatedAt: dequeuedAttempt1[0].UpdatedAt, // it is a hack to have same time for both
Job: job1,
}
require.Equal(t, expAttempt1, dequeuedAttempt1[0])
acknowledgedAttempt1, err := queue.Acknowledge(ctx, JobStateFailed, []uint64{event1.ID, event2.ID})
require.NoError(t, err)
require.Equal(t, []uint64{event1.ID}, acknowledgedAttempt1, "one event must be acknowledged")
dequeuedAttempt2, err := queue.Dequeue(ctx, "praefect", "storage-1", 100500)
require.NoError(t, err)
require.Len(t, dequeuedAttempt2, 1, "only single event must be fetched for this storage")
expAttempt2 := ReplicationEvent{
ID: 1,
State: JobStateInProgress,
Attempt: 1,
LockID: "praefect|storage-1|/project/path-1",
CreatedAt: event1.CreatedAt, // it is a hack to have same time for both
UpdatedAt: dequeuedAttempt2[0].UpdatedAt, // it is a hack to have same time for both
Job: job1,
}
require.Equal(t, expAttempt2, dequeuedAttempt2[0])
acknowledgedAttempt2, err := queue.Acknowledge(ctx, JobStateFailed, []uint64{event1.ID})
require.NoError(t, err)
require.Equal(t, []uint64{event1.ID}, acknowledgedAttempt2, "one event must be acknowledged")
dequeuedAttempt3, err := queue.Dequeue(ctx, "praefect", "storage-1", 100500)
require.NoError(t, err)
require.Len(t, dequeuedAttempt3, 1, "only single event must be fetched for this storage")
expAttempt3 := ReplicationEvent{
ID: 1,
State: JobStateInProgress,
Attempt: 0,
LockID: "praefect|storage-1|/project/path-1",
CreatedAt: event1.CreatedAt, // it is a hack to have same time for both
UpdatedAt: dequeuedAttempt3[0].UpdatedAt, // it is a hack to have same time for both
Job: job1,
}
require.Equal(t, expAttempt3, dequeuedAttempt3[0])
ackFailedNoAttemptsLeft, err := queue.Acknowledge(ctx, JobStateFailed, []uint64{event1.ID})
require.Equal(t, errDeadAckedAsFailed, err)
require.Empty(t, ackFailedNoAttemptsLeft)
acknowledgedAttempt3, err := queue.Acknowledge(ctx, JobStateDead, []uint64{event1.ID})
require.NoError(t, err)
require.Equal(t, []uint64{event1.ID}, acknowledgedAttempt3, "one event must be acknowledged")
dequeuedAttempt4, err := queue.Dequeue(ctx, "praefect", "storage-1", 100500)
require.NoError(t, err)
require.Empty(t, dequeuedAttempt4, "all attempts to process job were used")
dequeuedAttempt5, err := queue.Dequeue(ctx, "praefect", "storage-2", 100500)
require.NoError(t, err)
require.Len(t, dequeuedAttempt5, 1, "only single event must be fetched for this storage")
expAttempt5 := ReplicationEvent{
ID: 2,
State: JobStateInProgress,
Attempt: 2,
LockID: "praefect|storage-2|/project/path-1",
CreatedAt: event2.CreatedAt, // it is a hack to have same time for both
UpdatedAt: dequeuedAttempt5[0].UpdatedAt, // it is a hack to have same time for both
Job: job2,
}
require.Equal(t, expAttempt5, dequeuedAttempt5[0])
acknowledgedAttempt5, err := queue.Acknowledge(ctx, JobStateDead, []uint64{event2.ID})
require.NoError(t, err)
require.Equal(t, []uint64{event2.ID}, acknowledgedAttempt5, "one event must be acknowledged")
dequeuedAttempt6, err := queue.Dequeue(ctx, "praefect", "storage-2", 100500)
require.NoError(t, err)
require.Empty(t, dequeuedAttempt6, "all jobs marked as completed for this storage")
}
func TestMemoryReplicationEventQueue_ConcurrentAccess(t *testing.T) {
ctx, cancel := testhelper.Context()
defer cancel()
queue := NewMemoryReplicationEventQueue(config.Config{})
job1 := ReplicationJob{
Change: UpdateRepo,
RelativePath: "/project/path-1",
VirtualStorage: "praefect",
TargetNodeStorage: "storage-1",
SourceNodeStorage: "storage-0",
}
job2 := ReplicationJob{
Change: UpdateRepo,
RelativePath: "/project/path-1",
VirtualStorage: "praefect",
TargetNodeStorage: "storage-2",
SourceNodeStorage: "storage-0",
}
eventType1 := ReplicationEvent{Job: job1}
eventType2 := ReplicationEvent{Job: job2}
var checkScenario = func(wg *sync.WaitGroup, event ReplicationEvent, state JobState) {
defer wg.Done()
created, err := queue.Enqueue(ctx, event)
require.NoError(t, err)
dequeued, err := queue.Dequeue(ctx, "praefect", created.Job.TargetNodeStorage, 100500)
require.NoError(t, err)
require.Len(t, dequeued, 1)
require.Equal(t, created.Job, dequeued[0].Job)
ackIDs, err := queue.Acknowledge(ctx, state, []uint64{created.ID})
require.NoError(t, err)
require.Len(t, ackIDs, 1)
require.Equal(t, created.ID, ackIDs[0])
nothing, err := queue.Dequeue(ctx, "praefect", created.Job.TargetNodeStorage, 100500)
require.NoError(t, err)
require.Len(t, nothing, 0)
}
wg := &sync.WaitGroup{}
wg.Add(2)
go checkScenario(wg, eventType1, JobStateCompleted)
go checkScenario(wg, eventType2, JobStateCancelled)
wg.Wait()
}
|