forked from Jonathan-Rosenberg/delta-go
-
Notifications
You must be signed in to change notification settings - Fork 1
/
checkpoint.go
276 lines (227 loc) · 6.88 KB
/
checkpoint.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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
package deltago
import (
"encoding/json"
"io"
"log"
"sort"
"time"
"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/iter"
"github.com/csimplestring/delta-go/store"
"github.com/rotisserie/eris"
"github.com/samber/mo"
)
const LastCheckpointPath string = "_last_checkpoint"
type CheckpointMetaDataJSON struct {
Version int64 `json:"version,omitempty"`
Size int64 `json:"size,omitempty"`
Parts *int `json:"parts,omitempty"`
}
var MaxInstance = CheckpointInstance{Version: -1, NumParts: mo.None[int]()}
type CheckpointInstance struct {
Version int64
NumParts mo.Option[int]
}
func (t *CheckpointInstance) Compare(other CheckpointInstance) int {
if t.Version == other.Version {
return t.NumParts.OrElse(1) - other.NumParts.OrElse(1)
}
if t.Version-other.Version < 0 {
return -1
} else {
return 1
}
}
func (t *CheckpointInstance) IsEarlierThan(other CheckpointInstance) bool {
if other.Compare(MaxInstance) == 0 {
return true
}
if t.NumParts.IsAbsent() {
return t.Version <= other.Version
} else {
return t.Version < other.Version || (t.Version == other.Version && t.NumParts.MustGet() < other.NumParts.OrElse(1))
}
}
func (t *CheckpointInstance) IsNotLaterThan(other CheckpointInstance) bool {
if other.Compare(MaxInstance) == 0 {
return true
}
return t.Version <= other.Version
}
func (t *CheckpointInstance) GetCorrespondingFiles(dir string) (res []string) {
if t.NumParts.IsAbsent() {
return []string{filenames.CheckpointFileSingular(dir, t.Version)}
} else {
return filenames.CheckpointFileWithParts(dir, t.Version, t.NumParts.MustGet())
}
}
func FromPath(path string) *CheckpointInstance {
version := filenames.CheckpointVersion(path)
numParts := filenames.NumCheckpointParts(path)
return &CheckpointInstance{Version: version, NumParts: numParts}
}
func FromMetadata(metadata CheckpointMetaDataJSON) *CheckpointInstance {
i := &CheckpointInstance{Version: metadata.Version}
if metadata.Parts == nil {
i.NumParts = mo.None[int]()
} else {
i.NumParts = mo.Some(*metadata.Parts)
}
return i
}
func LastCheckpoint(s store.Store) (mo.Option[*CheckpointMetaDataJSON], error) {
return LoadMetadataFromFile(s)
}
func LoadMetadataFromFile(s store.Store) (mo.Option[*CheckpointMetaDataJSON], error) {
for i := 0; i < 3; i++ {
if i != 0 {
time.Sleep(time.Second)
}
lines, err := s.Read(LastCheckpointPath)
if err != nil {
if eris.Is(err, errno.ErrFileNotFound) {
return mo.None[*CheckpointMetaDataJSON](), nil
} else {
continue
}
}
defer lines.Close()
line, err := lines.Next()
if err == io.EOF {
log.Println("failed to read last checkpoint, end of iterator, try again")
continue
}
if err != nil {
log.Println("failed to get line from iterator when reading last checkpoint, try again")
continue
}
res := &CheckpointMetaDataJSON{}
err = json.Unmarshal([]byte(line), res)
if err != nil {
log.Println("failed to unmarshal json line when reading last checkpoint, try again")
continue
}
return mo.Some(res), nil
}
// tried 3 times, still failed, can not find last_checkpoint
// Hit a partial file. This could happen on Azure as overwriting _last_checkpoint file is
// not atomic. We will try to list all files to find the latest checkpoint and restore
// CheckpointMetaData from it.
if lastCheckpoint, err := FindLastCompleteCheckpoint(s, MaxInstance); err != nil {
return mo.None[*CheckpointMetaDataJSON](), eris.Wrap(err, "FindLastCompleteCheckpoint")
} else {
return manuallyLoadCheckpoint(lastCheckpoint), nil
}
}
func manuallyLoadCheckpoint(t mo.Option[*CheckpointInstance]) mo.Option[*CheckpointMetaDataJSON] {
if t.IsAbsent() {
return mo.None[*CheckpointMetaDataJSON]()
}
cv := t.MustGet()
if p, ok := cv.NumParts.Get(); ok {
return mo.Some(&CheckpointMetaDataJSON{Version: cv.Version, Size: -1, Parts: &p})
} else {
return mo.Some(&CheckpointMetaDataJSON{Version: cv.Version, Size: -1})
}
}
func FindLastCompleteCheckpoint(s store.Store, cv CheckpointInstance) (mo.Option[*CheckpointInstance], error) {
cur := util.MaxInt64(cv.Version, 0)
for cur >= 0 {
iter, err := s.ListFrom(filenames.CheckpointPrefix(s.Root(), util.MaxInt64(0, cur-1000)))
if err != nil {
return mo.None[*CheckpointInstance](), eris.Wrap(err, "")
}
defer iter.Close()
var (
checkpoints []*CheckpointInstance
iterErr error
f *store.FileMeta
)
for f, iterErr = iter.Next(); iterErr == nil; f, iterErr = iter.Next() {
if !filenames.IsCheckpointFile(f.Path()) {
continue
}
cp := FromPath(f.Path())
if cur == 0 || cp.Version <= cur || cp.IsEarlierThan(cv) {
checkpoints = append(checkpoints, cp)
} else {
break
}
}
if iterErr != nil && iterErr != io.EOF {
return mo.None[*CheckpointInstance](), eris.Wrap(iterErr, "")
}
lastCheckpoint := GetLatestCompleteCheckpointFromList(checkpoints, cv)
if lastCheckpoint.IsPresent() {
return lastCheckpoint, nil
} else {
cur -= 1000
}
}
return mo.None[*CheckpointInstance](), nil
}
type instanceKey struct {
Version int64
NumParts int
HasParts bool
}
func (i instanceKey) toInstance() *CheckpointInstance {
if i.HasParts {
return &CheckpointInstance{Version: i.Version, NumParts: mo.Some(i.NumParts)}
}
return &CheckpointInstance{Version: i.Version, NumParts: mo.None[int]()}
}
func GetLatestCompleteCheckpointFromList(instances []*CheckpointInstance, notLaterThan CheckpointInstance) mo.Option[*CheckpointInstance] {
grouped := make(map[instanceKey][]*CheckpointInstance)
for _, i := range instances {
if !i.IsNotLaterThan(notLaterThan) {
continue
}
k := instanceKey{Version: i.Version, NumParts: i.NumParts.OrElse(1), HasParts: i.NumParts.IsPresent()}
if vals, ok := grouped[k]; ok {
vals = append(vals, i)
grouped[k] = vals
} else {
grouped[k] = []*CheckpointInstance{i}
}
}
var res []*CheckpointInstance
for k, v := range grouped {
if k.NumParts != len(v) {
continue
}
res = append(res, k.toInstance())
}
sort.Slice(res, func(i, j int) bool {
return res[i].Compare(*res[j]) < 0
})
if len(res) != 0 {
return mo.Some(res[len(res)-1])
}
return mo.None[*CheckpointInstance]()
}
func checkpoint(logPath string, store store.Store, snapshotToCheckpoint *snapshotImp) error {
pw, err := newParquetActionWriter(logPath)
if err != nil {
return eris.Wrap(err, "can not new ParquetActionWriter")
}
writer := &checkpointWriter{
schemaText: actionSchemaDefinitionString,
pw: pw,
}
checkpointMetadata, err := writer.write(snapshotToCheckpoint)
if err != nil {
return err
}
b, err := json.Marshal(checkpointMetadata)
if err != nil {
return errno.JsonMarshalError(err)
}
if err := store.Write(LastCheckpointPath, iter.FromSlice([]string{string(b)}), true); err != nil {
return err
}
// todo: doLogCleanup()
return nil
}