forked from Jonathan-Rosenberg/delta-go
-
Notifications
You must be signed in to change notification settings - Fork 1
/
conflict_checker.go
257 lines (225 loc) · 7.73 KB
/
conflict_checker.go
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
package deltago
import (
"github.com/csimplestring/delta-go/action"
"github.com/csimplestring/delta-go/errno"
"github.com/csimplestring/delta-go/internal/util"
"github.com/csimplestring/delta-go/internal/util/filenames"
"github.com/csimplestring/delta-go/isolation"
"github.com/csimplestring/delta-go/iter"
"github.com/csimplestring/delta-go/store"
"github.com/csimplestring/delta-go/types"
mapset "github.com/deckarep/golang-set/v2"
"github.com/repeale/fp-go"
"github.com/samber/mo"
)
type currentTransactionInfo struct {
readPredicates []types.Expression
readFiles mapset.Set[*action.AddFile]
readWholeTable bool
readAppIds mapset.Set[string]
metadata *action.Metadata
actions []action.Action
logStore store.Store
logPath string
}
type winningCommitSummary struct {
metadataUpdates []*action.Metadata
appLevelTransactions []*action.SetTransaction
protocol []*action.Protocol
commitInfo mo.Option[*action.CommitInfo]
removedFiles []*action.RemoveFile
addedFiles []*action.AddFile
isBlindAppendOption mo.Option[bool]
blindAppendAddedFiles []*action.AddFile
changedDataAddedFiles []*action.AddFile
onlyAddFiles bool
}
func newWinningCommitSummary(actions []action.Action, commitVersion int64) *winningCommitSummary {
w := &winningCommitSummary{}
w.metadataUpdates = action.UtilFnCollect[*action.Metadata](actions)
w.appLevelTransactions = action.UtilFnCollect[*action.SetTransaction](actions)
w.protocol = action.UtilFnCollect[*action.Protocol](actions)
w.commitInfo = action.UtilFnCollectFirst[*action.CommitInfo](actions).Map(func(value *action.CommitInfo) (*action.CommitInfo, bool) {
return value.Copy(commitVersion), true
})
w.removedFiles = action.UtilFnCollect[*action.RemoveFile](actions)
w.addedFiles = action.UtilFnCollect[*action.AddFile](actions)
w.isBlindAppendOption = util.MapOptional(w.commitInfo, func(v *action.CommitInfo) bool {
return *v.IsBlindAppend
})
if w.isBlindAppendOption.OrElse(false) {
w.blindAppendAddedFiles = w.addedFiles
}
if !w.isBlindAppendOption.OrElse(false) {
w.changedDataAddedFiles = w.addedFiles
}
w.onlyAddFiles = fp.Every(func(t action.FileAction) bool {
_, ok := t.(*action.AddFile)
return ok
})(action.UtilFnCollect[action.FileAction](actions))
return w
}
type conflictChecker struct {
currentTransactionInfo *currentTransactionInfo
winningCommitVersion int64
isolationLevel isolation.Level
winningCommitSummary *winningCommitSummary
}
func newConflictChecker(currentTransactionInfo *currentTransactionInfo, winningCommitVersion int64, isolationLevel isolation.Level) (*conflictChecker, error) {
c := &conflictChecker{
currentTransactionInfo: currentTransactionInfo,
winningCommitVersion: winningCommitVersion,
isolationLevel: isolationLevel,
}
w, err := c.createWinningCommitSummary()
if err != nil {
return nil, err
}
c.winningCommitSummary = w
return c, nil
}
// must call it when crreating conflictChecker
func (c *conflictChecker) createWinningCommitSummary() (*winningCommitSummary, error) {
trx := c.currentTransactionInfo
siter, err := trx.logStore.Read(filenames.DeltaFile(trx.logPath, c.winningCommitVersion))
if err != nil {
return nil, err
}
defer siter.Close()
winningCommitActions, err := iter.Map(siter, action.FromJson)
if err != nil {
return nil, err
}
return newWinningCommitSummary(winningCommitActions, c.winningCommitVersion), nil
}
func (c *conflictChecker) checkConflicts() error {
checks := []func() error{
c.checkProtocolCompatibility,
c.checkNoMetadataUpdates,
c.checkForAddedFilesThatShouldHaveBeenReadByCurrentTxn,
c.checkForDeletedFilesAgainstCurrentTxnReadFiles,
c.checkForDeletedFilesAgainstCurrentTxnDeletedFiles,
c.checkForUpdatedApplicationTransactionIdsThatCurrentTxnDependsOn,
}
for _, check := range checks {
if err := check(); err != nil {
return err
}
}
return nil
}
func (c *conflictChecker) checkProtocolCompatibility() error {
if len(c.winningCommitSummary.protocol) == 0 {
return nil
}
for _, p := range c.winningCommitSummary.protocol {
if err := assertProtocolRead(p); err != nil {
return err
}
if err := assertProtocolWrite(p); err != nil {
return err
}
}
for _, a := range c.currentTransactionInfo.actions {
if _, ok := a.(*action.Protocol); ok {
return errno.ProtocolChangedException("")
}
}
return nil
}
func (c *conflictChecker) checkNoMetadataUpdates() error {
if len(c.winningCommitSummary.metadataUpdates) != 0 {
return errno.MetadataChangedError()
}
return nil
}
func filterFileList(paritionSchema *types.StructType, files []*action.AddFile, filter types.Expression) ([]*action.AddFile, error) {
var res []*action.AddFile
for _, file := range files {
r := &PartitionRowRecord{partitionSchema: paritionSchema, partitionValues: file.PartitionValues}
v, err := filter.Eval(r)
if err != nil {
return nil, err
}
if v.(bool) {
res = append(res, file)
}
}
return res, nil
}
func (c *conflictChecker) checkForAddedFilesThatShouldHaveBeenReadByCurrentTxn() error {
addedFilesToCheckForConflicts := []*action.AddFile{}
if c.isolationLevel == isolation.Serializable {
addedFilesToCheckForConflicts = append(c.winningCommitSummary.changedDataAddedFiles, c.winningCommitSummary.blindAppendAddedFiles...)
}
mSchema, err := c.currentTransactionInfo.metadata.PartitionSchema()
if err != nil {
return err
}
for _, p := range c.currentTransactionInfo.readPredicates {
files, err := filterFileList(mSchema, addedFilesToCheckForConflicts, p)
if err != nil {
return err
}
if len(files) != 0 {
return errno.ConcurrentAppend("")
}
}
return nil
}
func (c *conflictChecker) checkForDeletedFilesAgainstCurrentTxnReadFiles() error {
readFilePaths := make(map[string]map[string]string)
for f := range c.currentTransactionInfo.readFiles.Iterator().C {
readFilePaths[f.Path] = f.PartitionValues
}
var deleteReadOverlap *action.RemoveFile
for _, f := range c.winningCommitSummary.removedFiles {
if _, ok := readFilePaths[f.Path]; ok {
deleteReadOverlap = f
}
}
if deleteReadOverlap != nil {
return errno.ConcurrentDeleteRead(deleteReadOverlap.Path)
}
if len(c.winningCommitSummary.removedFiles) != 0 && c.currentTransactionInfo.readWholeTable {
return errno.ConcurrentDeleteRead(c.winningCommitSummary.removedFiles[0].Path)
}
return nil
}
func (c *conflictChecker) checkForDeletedFilesAgainstCurrentTxnDeletedFiles() error {
deletedPaths := fp.Map(func(t *action.RemoveFile) string {
return t.Path
})(action.UtilFnCollect[*action.RemoveFile](c.currentTransactionInfo.actions))
txnDeletes := mapset.NewSet(deletedPaths...)
winningRemoved := fp.Map(func(t *action.RemoveFile) string {
return t.Path
})(c.winningCommitSummary.removedFiles)
winningRemovedPaths := mapset.NewSet(winningRemoved...)
deleteOverlap := winningRemovedPaths.Intersect(txnDeletes)
if deleteOverlap.Cardinality() != 0 {
file, _ := deleteOverlap.Pop()
return errno.ConcurrentDeleteDelete(file)
}
return nil
}
func (c *conflictChecker) checkForUpdatedApplicationTransactionIdsThatCurrentTxnDependsOn() error {
appIds := fp.Map(func(t *action.SetTransaction) string {
return t.AppId
})(c.winningCommitSummary.appLevelTransactions)
if mapset.NewSet(appIds...).Intersect(c.currentTransactionInfo.readAppIds).Cardinality() != 0 {
return &errno.ConcurrentTransactionError{}
}
return nil
}
func assertProtocolRead(protocol *action.Protocol) error {
if protocol != nil && action.ReaderVersion < protocol.MinReaderVersion {
return errno.InvalidProtocolVersionError()
}
return nil
}
func assertProtocolWrite(protocol *action.Protocol) error {
if protocol != nil && action.WriterVersion < protocol.MinWriterVersion {
return errno.InvalidProtocolVersionError()
}
return nil
}