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
|
package gitaly
import (
"context"
"regexp"
"runtime"
"strings"
"testing"
"github.com/dgraph-io/badger/v3"
"github.com/stretchr/testify/require"
"gitlab.com/gitlab-org/gitaly/v15/internal/git"
"gitlab.com/gitlab-org/gitaly/v15/internal/git/localrepo"
)
// hookFunc is a function that is executed at a specific point. It gets a hookContext that allows it to
// influence the execution of the test.
type hookFunc func(hookContext)
// hookContext are the control toggels available in a hook.
type hookContext struct {
// stopManager calls the calls stops the TransactionManager.
stopManager func()
// database provides access to the database for the hook handler.
database *badger.DB
tb testing.TB
}
// hooks are functions that get invoked at specific points of the TransactionManager Run method. They allow
// for hooking into the Run method at specific poins which would otherwise to do assertions that would otherwise
// not be possible.
type hooks struct {
// beforeReadLogEntry is invoked before a log entry is read from the database.
beforeReadLogEntry hookFunc
// beforeResolveRevision is invoked before ResolveRevision is invoked.
beforeResolveRevision hookFunc
// beforeDeferredStop is invoked before the deferred Stop is invoked in Run.
beforeDeferredStop hookFunc
// beforeDeleteLogEntry is invoked before a log entry is deleted from the database.
beforeDeleteLogEntry hookFunc
// beforeStoreAppliedLogIndex is invoked before a the applied log index is stored.
beforeStoreAppliedLogIndex hookFunc
}
// installHooks installs the configured hooks into the transactionManager.
func installHooks(tb testing.TB, transactionManager *TransactionManager, database *badger.DB, repository *localrepo.Repo, hooks hooks) {
hookContext := hookContext{stopManager: transactionManager.stop, database: database, tb: &testingHook{TB: tb}}
transactionManager.stop = func() {
programCounter, _, _, ok := runtime.Caller(2)
require.True(tb, ok)
isDeferredStopInRun := strings.HasSuffix(
runtime.FuncForPC(programCounter).Name(),
"gitaly.(*TransactionManager).Run",
)
if isDeferredStopInRun && hooks.beforeDeferredStop != nil {
hooks.beforeDeferredStop(hookContext)
}
hookContext.stopManager()
}
transactionManager.db = databaseHook{
database: newDatabaseAdapter(database),
hooks: hooks,
hookContext: hookContext,
}
transactionManager.repository = repositoryHook{
repository: repository,
hookContext: hookContext,
hooks: hooks,
}
}
type repositoryHook struct {
repository
hookContext
hooks
}
func (hook repositoryHook) ResolveRevision(ctx context.Context, revision git.Revision) (git.ObjectID, error) {
if hook.beforeResolveRevision != nil {
hook.hooks.beforeResolveRevision(hook.hookContext)
}
return hook.repository.ResolveRevision(ctx, revision)
}
type databaseHook struct {
database
hookContext
hooks
}
func (hook databaseHook) View(handler func(databaseTransaction) error) error {
return hook.database.View(func(transaction databaseTransaction) error {
return handler(databaseTransactionHook{
databaseTransaction: transaction,
hookContext: hook.hookContext,
hooks: hook.hooks,
})
})
}
func (hook databaseHook) Update(handler func(databaseTransaction) error) error {
return hook.database.Update(func(transaction databaseTransaction) error {
return handler(databaseTransactionHook{
databaseTransaction: transaction,
hookContext: hook.hookContext,
hooks: hook.hooks,
})
})
}
func (hook databaseHook) NewWriteBatch() writeBatch {
return writeBatchHook{
writeBatch: hook.database.NewWriteBatch(),
hookContext: hook.hookContext,
hooks: hook.hooks,
}
}
type databaseTransactionHook struct {
databaseTransaction
hookContext
hooks
}
var (
regexLogEntry = regexp.MustCompile("repository/.+/log/entry/")
regexLogIndex = regexp.MustCompile("repository/.+/log/index/applied")
)
func (hook databaseTransactionHook) Get(key []byte) (*badger.Item, error) {
if regexLogEntry.Match(key) {
if hook.hooks.beforeReadLogEntry != nil {
hook.hooks.beforeReadLogEntry(hook.hookContext)
}
}
return hook.databaseTransaction.Get(key)
}
func (hook databaseTransactionHook) NewIterator(options badger.IteratorOptions) *badger.Iterator {
return hook.databaseTransaction.NewIterator(options)
}
func (hook databaseTransactionHook) Delete(key []byte) error {
if regexLogEntry.Match(key) && hook.beforeDeleteLogEntry != nil {
hook.beforeDeleteLogEntry(hook.hookContext)
}
return hook.databaseTransaction.Delete(key)
}
type writeBatchHook struct {
writeBatch
hookContext
hooks
}
func (hook writeBatchHook) Set(key []byte, value []byte) error {
if regexLogIndex.Match(key) && hook.hooks.beforeStoreAppliedLogIndex != nil {
hook.hooks.beforeStoreAppliedLogIndex(hook.hookContext)
}
return hook.writeBatch.Set(key, value)
}
func (hook writeBatchHook) Flush() error { return hook.writeBatch.Flush() }
func (hook writeBatchHook) Cancel() { hook.writeBatch.Cancel() }
type testingHook struct {
testing.TB
}
// We override the FailNow call to the regular testing.Fail, so that it can be
// used within goroutines without replacing calls made to the `require` library.
func (t testingHook) FailNow() {
t.Fail()
}
|