-
Notifications
You must be signed in to change notification settings - Fork 0
/
yog.go
369 lines (309 loc) · 8.08 KB
/
yog.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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
package yog
import (
"bufio"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"os"
"strings"
)
type Yog struct {
taskID string // 方舟接口返回的
path string // 我们把 gsg 文件下载到哪里
apiKey string // google key
offset int // 记录读数据进度
outputAddr string
taskMeta TaskMeta
storage ICloudStorage
}
func removeGsorS3Prefix(outputAddr string) string {
outputAddr = strings.TrimPrefix(outputAddr, "gs://")
outputAddr = strings.TrimPrefix(outputAddr, "s3://")
return outputAddr
}
func NewInternal(taskID string, path string, outputAddr string, apiKeyPath string) *Yog {
outputAddrWithoutPrefix := removeGsorS3Prefix(outputAddr)
arrays := strings.Split(outputAddrWithoutPrefix, "/")
return &Yog{
taskID: taskID,
path: path,
apiKey: apiKeyPath,
offset: 0,
storage: NewGCSClient(arrays[0], strings.Join(arrays[1:], "/"), apiKeyPath, outputAddr),
}
}
func New(taskID string, path string, outputAddr string) *Yog {
if path != "" && path[len(path)-1] != '/' {
path = path + "/"
}
outputAddr = "https://" + removeGsorS3Prefix(outputAddr)
return &Yog{
taskID: taskID,
path: path,
apiKey: "",
offset: 0,
storage: NewHTTPGetClient(outputAddr),
}
}
// Load download file & check meta
func (y *Yog) Load() error {
if y.path[len(y.path)-1] != '/' {
y.path = y.path + "/"
}
err := os.MkdirAll(y.path, os.ModePerm)
if err != nil {
return err
}
ctx := context.TODO()
// download meta
err = y.storage.DownloadSingleFile(ctx, META_FILE_NAME, y.path)
if err != nil {
return fmt.Errorf("meta file download failed filename:%v error:%v", META_FILE_NAME, err)
}
// download chunk by meta content
metaContent, err := bufferReaderFile(y.path+META_FILE_NAME, 10240)
if err != nil {
return err
}
if len(metaContent) == 0 || metaContent[0] != '{' {
return errors.New("meta file not exist in remote storage")
}
// meta.json unmarshal TaskMeta
err = json.Unmarshal(metaContent, &y.taskMeta)
if err != nil {
return fmt.Errorf("meta file illegal file format %v", err)
}
for chunkFileName, chunkInfo := range y.taskMeta.Index {
err := y.storage.DownloadSingleFile(ctx, chunkFileName, y.path)
if err != nil {
if err.Error() == "file not exist" {
chunkInfo.Status = FFailed
y.taskMeta.Index[chunkFileName] = chunkInfo
continue
}
return fmt.Errorf("chunk file download failed filename:%v error:%v", chunkFileName, err)
}
chunkInfo.Status = FSucceeded
y.taskMeta.Index[chunkFileName] = chunkInfo
}
return y.ReloadMeta()
}
func (y *Yog) Check() error {
// check meta.json exist
metaFilePath := y.path + "/" + META_FILE_NAME
if !fileExists(metaFilePath) {
return errors.New("meta file does not exist")
}
// download chunk by meta content
metaContent, err := bufferReaderFile(y.path+META_FILE_NAME, 10240)
if err != nil {
return err
}
if len(metaContent) == 0 || metaContent[0] != '{' {
return errors.New("meta file not exist in remote storage")
}
// meta.json unmarshal TaskMeta
err = json.Unmarshal(metaContent, &y.taskMeta)
if err != nil {
return fmt.Errorf("meta file illegal file format %v", err)
}
// check meta data
if len(y.taskMeta.Index) == 0 {
return errors.New("meta file does not include index")
}
for chunkName, chunkInfo := range y.taskMeta.Index {
chunkPath := y.path + "/" + chunkName
if !fileExists(chunkPath) {
chunkInfo.Status = FFailed
} else {
chunkInfo.Status = FSucceeded
}
y.taskMeta.Index[chunkName] = chunkInfo
}
return nil
}
// ReloadMeta download file & check meta
func (y *Yog) ReloadMeta() error {
if len(y.taskMeta.Index) == 0 {
err := y.Check()
if err != nil {
return err
}
}
oc, dc := 0, 0
for _, data := range y.taskMeta.Index {
data.Offset = []int{}
for _, o := range data.Origin {
if oc < o {
oc = o
}
}
for _, d := range data.Destination {
if dc < d {
dc = d
}
}
}
y.taskMeta.MatrixInfo.OriginCount = oc + 1
y.taskMeta.MatrixInfo.DestinationCount = dc + 1
for i, data := range y.taskMeta.Index {
for _, o := range data.Origin {
for _, d := range data.Destination {
data.Offset = append(data.Offset, o*y.taskMeta.MatrixInfo.DestinationCount+d)
}
}
y.taskMeta.Index[i] = data
}
// check meta data
if len(y.taskMeta.Index) == 0 {
return errors.New("meta file does not include index")
}
return nil
}
type page struct {
status FileState
file string
offset int64
length int64
originalIndex IndexItem
start int
end int
}
func (p *page) read() ([]byte, error) {
if p.status == FFailed {
return make([]byte, p.length), nil
}
file, err := os.OpenFile(p.file, os.O_RDONLY, os.ModePerm)
if err != nil {
return nil, err
}
defer func(file *os.File) {
_ = file.Close()
}(file)
_, err = file.Seek(p.offset, io.SeekStart)
if err != nil {
return nil, err
}
bufReader := bufio.NewReader(file)
block := make([]byte, p.length)
_, err = bufReader.Read(block)
if err != nil {
return nil, err
}
return block, nil
}
func (y *Yog) Reset() {
y.offset = 0
}
// ReadChunk read a chunk of data in order
// i = o * d.length + d
func (y *Yog) ReadChunk(chunkSize int) (durations []uint32, distances []uint32, err error) {
if y.offset+chunkSize > y.taskMeta.MatrixInfo.OriginCount*y.taskMeta.MatrixInfo.DestinationCount {
chunkSize = y.taskMeta.MatrixInfo.OriginCount*y.taskMeta.MatrixInfo.DestinationCount - y.offset
if chunkSize <= 0 {
return nil, nil, io.EOF
}
}
// read file chunk
var pages = make([]page, 0)
for indexName, indexData := range y.taskMeta.Index {
startOffset, endOffset := findSubset(y.offset, y.offset+chunkSize-1, indexData.Offset)
if startOffset < 0 || endOffset < 0 {
continue
}
pages = append(pages, page{
file: y.path + "/" + indexName,
offset: int64(startOffset*8 + HEADER_LENGTH),
length: int64(endOffset-startOffset+1) * 8,
originalIndex: indexData,
start: startOffset,
end: endOffset,
status: indexData.Status,
})
}
// allocate space
durations = make([]uint32, chunkSize)
distances = make([]uint32, chunkSize)
// fill the data in order
for _, p := range pages {
data, err := p.read()
if err != nil {
return nil, nil, fmt.Errorf("read chunk failed %v", err)
}
chunkDurations, chunkDistances, err := decodeChunk(data)
if err != nil {
return nil, nil, fmt.Errorf("decode binary chunk failed %v", err)
}
for i := 0; i < p.end-p.start+1; i++ {
ir := p.originalIndex.Offset[p.start+i] - y.offset
durations[ir] = chunkDurations[i]
distances[ir] = chunkDistances[i]
}
}
y.offset += chunkSize
return durations, distances, err
}
func findSubset(min, max int, array []int) (int, int) {
begin, end := -1, -1
for i, v := range array {
if begin == -1 && v >= min && v <= max {
begin = i
}
if v >= min && v <= max {
end = i
}
}
return begin, end
}
// Read the data with the specified index
func (y *Yog) Read(o, d int) (duration uint32, distance uint32, err error) {
var p page
oindex, dindex := -1, -1
for indexName, indexData := range y.taskMeta.Index {
for i, oo := range indexData.Origin {
if o == oo {
oindex = i
}
}
for i, dd := range indexData.Destination {
if d == dd {
dindex = i
}
}
if oindex >= 0 && dindex >= 0 {
p.file = y.path + "/" + indexName
p.offset = int64(oindex*len(indexData.Destination)+dindex)*8 + HEADER_LENGTH
p.length = 8
p.status = indexData.Status
break
}
oindex, dindex = -1, -1
}
data, err := p.read()
if err != nil {
return 0, 0, fmt.Errorf("read chunk failed %v", err)
}
return decode(data)
}
// MatrixInfo get MatrixInfo from meta
func (y *Yog) MatrixInfo() MatrixInfo {
return y.taskMeta.MatrixInfo
}
// Clear remove task file
func (y *Yog) Clear() error {
for name := range y.taskMeta.Index {
chunkFilePath := y.path + name
err := os.Remove(chunkFilePath)
if err != nil {
return fmt.Errorf("remove chunk file failed %v", err)
}
}
indexFilePath := y.path + META_FILE_NAME
err := os.Remove(indexFilePath)
if err != nil {
return fmt.Errorf("remove index file failed %v", err)
}
return nil
}