-
Notifications
You must be signed in to change notification settings - Fork 351
/
import.go
250 lines (222 loc) · 5.28 KB
/
import.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
package catalog
import (
"context"
"errors"
"os"
"sync"
"time"
"github.com/cockroachdb/pebble"
"github.com/hashicorp/go-multierror"
"github.com/treeverse/lakefs/pkg/graveler"
"github.com/treeverse/lakefs/pkg/kv"
"github.com/treeverse/lakefs/pkg/logging"
"google.golang.org/protobuf/proto"
)
const ImportCanceled = "Canceled"
var ErrImportClosed = errors.New("import closed")
type Import struct {
db *pebble.DB
dbPath string
kvStore kv.Store
logger logging.Logger
repoPartition string
wg multierror.Group
status graveler.ImportStatus
closed bool
mu sync.Mutex
}
func NewImport(ctx context.Context, cancel context.CancelFunc, logger logging.Logger, kvStore kv.Store, repository *graveler.RepositoryRecord, importID string) (*Import, error) {
status := graveler.ImportStatus{
ID: graveler.ImportID(importID),
UpdatedAt: time.Now(),
}
repoPartition := graveler.RepoPartition(repository)
// Must be set first
err := kv.SetMsg(ctx, kvStore, repoPartition, []byte(graveler.ImportsPath(importID)), graveler.ProtoFromImportStatus(&status))
if err != nil {
return nil, err
}
dbPath, err := os.MkdirTemp("", "import_"+importID)
if err != nil {
return nil, err
}
importDB, err := pebble.Open(dbPath, nil)
if err != nil {
return nil, err
}
i := Import{
db: importDB,
dbPath: dbPath,
kvStore: kvStore,
status: status,
logger: logger,
repoPartition: repoPartition,
mu: sync.Mutex{},
}
i.wg.Go(func() error {
err := i.importStatusAsync(ctx, cancel)
if err != nil {
i.logger.WithError(err).Error("failed to update import status")
}
return err
})
return &i, nil
}
func (i *Import) set(record EntryRecord) error {
if i.Closed() {
return ErrImportClosed
}
key := []byte(record.Path)
value, err := EntryToValue(record.Entry)
if err != nil {
return err
}
pb := graveler.ProtoFromStagedEntry(key, value)
data, err := proto.Marshal(pb)
if err != nil {
return err
}
return i.db.Set(key, data, &pebble.WriteOptions{
Sync: false,
})
}
func (i *Import) Ingest(it *walkEntryIterator) error {
if i.Closed() {
return ErrImportClosed
}
i.logger.WithField("itr", it).Debug("Ingest start")
for it.Next() {
if err := i.set(*it.Value()); err != nil {
return err
}
i.mu.Lock()
i.status.Progress += 1
i.mu.Unlock()
}
i.logger.WithField("itr", it).Debug("Ingest finished")
return it.Err()
}
func (i *Import) Status() graveler.ImportStatus {
i.mu.Lock()
defer i.mu.Unlock()
status := i.status
return status
}
func (i *Import) SetStatus(status graveler.ImportStatus) {
i.mu.Lock()
defer i.mu.Unlock()
i.status = status
i.status.UpdatedAt = time.Now()
}
func (i *Import) SetError(err error) {
i.mu.Lock()
defer i.mu.Unlock()
i.status.Error = err
i.status.UpdatedAt = time.Now()
}
func (i *Import) NewItr() (*importIterator, error) {
if i.Closed() {
return nil, ErrImportClosed
}
return newImportIterator(i.db.NewIter(nil)), nil
}
func (i *Import) Closed() bool {
i.mu.Lock()
defer i.mu.Unlock()
closed := i.closed
return closed
}
func (i *Import) Close() {
i.mu.Lock()
i.closed = true
i.mu.Unlock()
_ = i.wg.Wait().ErrorOrNil()
_ = i.db.Close()
_ = os.RemoveAll(i.dbPath)
}
func (i *Import) importStatusAsync(ctx context.Context, cancel context.CancelFunc) error {
const statusUpdateInterval = 1 * time.Second
statusData := graveler.ImportStatusData{}
ticker := time.NewTicker(statusUpdateInterval)
defer ticker.Stop()
done := false
for range ticker.C {
pred, err := kv.GetMsg(ctx, i.kvStore, i.repoPartition, []byte(graveler.ImportsPath(i.status.ID.String())), &statusData)
if err != nil {
cancel()
return err
}
if statusData.Error != "" || statusData.Completed {
cancel()
return nil
}
currStatus := i.Status()
currStatus.UpdatedAt = time.Now()
err = kv.SetMsgIf(ctx, i.kvStore, i.repoPartition, []byte(graveler.ImportsPath(i.status.ID.String())), graveler.ProtoFromImportStatus(&currStatus), pred)
if errors.Is(err, kv.ErrPredicateFailed) {
i.logger.Warning("Import status update failed")
} else if err != nil {
cancel()
return err
}
// Ensure one last status update after import closed
if done {
return nil
}
if i.Closed() {
done = true
}
}
return nil
}
type importIterator struct {
it *pebble.Iterator
hasMore bool
value *graveler.ValueRecord
err error
}
func newImportIterator(it *pebble.Iterator) *importIterator {
itr := &importIterator{
it: it,
}
itr.hasMore = it.First()
return itr
}
func (it *importIterator) Next() bool {
if it.err != nil {
return false
}
if !it.hasMore {
it.value = nil
return false
}
var value []byte
value, it.err = it.it.ValueAndErr()
if it.err != nil {
return false
}
data := graveler.StagedEntryData{}
it.err = proto.Unmarshal(value, &data)
if it.err != nil {
return false
}
it.value = &graveler.ValueRecord{
Key: data.Key,
Value: graveler.StagedEntryFromProto(&data),
}
it.hasMore = it.it.Next()
return true
}
func (it *importIterator) SeekGE(id graveler.Key) {
it.value = nil
it.hasMore = it.it.SeekGE(id)
}
func (it *importIterator) Value() *graveler.ValueRecord {
return it.value
}
func (it *importIterator) Err() error {
return it.err
}
func (it *importIterator) Close() {
_ = it.it.Close()
}