forked from microsoft/hdfs-mount
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFaultTolerantHdfsReader.go
83 lines (76 loc) · 2.58 KB
/
FaultTolerantHdfsReader.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
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for details.
package main
// Implements ReadSeekCloser interface with automatic retries (acts as a proxy to HdfsReader)
type FaultTolerantHdfsReader struct {
Path string
Impl ReadSeekCloser
HdfsAccessor HdfsAccessor
RetryPolicy *RetryPolicy
Offset int64
}
var _ ReadSeekCloser = (*FaultTolerantHdfsReader)(nil) // ensure FaultTolerantHdfsReaderImpl implements ReadSeekCloser
// Creates new instance of FaultTolerantHdfsReader
func NewFaultTolerantHdfsReader(path string, impl ReadSeekCloser, hdfsAccessor HdfsAccessor, retryPolicy *RetryPolicy) *FaultTolerantHdfsReader {
return &FaultTolerantHdfsReader{Path: path, Impl: impl, HdfsAccessor: hdfsAccessor, RetryPolicy: retryPolicy}
}
// Read a chunk of data
func (this *FaultTolerantHdfsReader) Read(buffer []byte) (int, error) {
op := this.RetryPolicy.StartOperation()
for {
var err error
if this.Impl == nil {
// Re-opening the file for read
this.Impl, err = this.HdfsAccessor.OpenRead(this.Path)
if err != nil {
if op.ShouldRetry("[%s] OpenRead: %s", this.Path, err.Error()) {
continue
} else {
return 0, err
}
}
// Seeking to the right offset
if err = this.Impl.Seek(this.Offset); err != nil {
// Those errors are non-recoverable propagating right away
this.Close()
return 0, err
}
}
// Performing the read
var nr int
nr, err = this.Impl.Read(buffer)
if IsSuccessOrBenignError(err) || !op.ShouldRetry("[%s] Read @%d: %s", this.Path, this.Offset, err.Error()) {
if err == nil {
// On successful read, adjusting offset to the actual number of bytes read
this.Offset += int64(nr)
}
return nr, err
}
// On failure, we need to close the reader
this.Close()
}
}
// Seeks to a given position
func (this *FaultTolerantHdfsReader) Seek(pos int64) error {
// Seek is implemented as virtual operation on which doesn't involve communication,
// passing that through without retires and promptly propagate errors
// (which will be non-recoverable in this case)
err := this.Impl.Seek(pos)
if err == nil {
// On success, updating current readng position
this.Offset = pos
}
return err
}
// Returns current position
func (this *FaultTolerantHdfsReader) Position() (int64, error) {
// This fault-tolerant wrapper keeps track the position on its own, no need
// to query the backend
return this.Offset, nil
}
// Closes the stream
func (this *FaultTolerantHdfsReader) Close() error {
err := this.Impl.Close()
this.Impl = nil
return err
}