forked from microsoft/hdfs-mount
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFaultTolerantHdfsAccessor.go
174 lines (158 loc) · 5.34 KB
/
FaultTolerantHdfsAccessor.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
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for details.
package main
import (
"os"
)
// Adds automatic retry capability to HdfsAccessor with respect to RetryPolicy
type FaultTolerantHdfsAccessor struct {
Impl HdfsAccessor
RetryPolicy *RetryPolicy
}
var _ HdfsAccessor = (*FaultTolerantHdfsAccessor)(nil) // ensure FaultTolerantHdfsAccessor implements HdfsAccessor
// Creates an instance of FaultTolerantHdfsAccessor
func NewFaultTolerantHdfsAccessor(impl HdfsAccessor, retryPolicy *RetryPolicy) *FaultTolerantHdfsAccessor {
return &FaultTolerantHdfsAccessor{
Impl: impl,
RetryPolicy: retryPolicy}
}
// Ensures HDFS accessor is connected to the HDFS name node
func (this *FaultTolerantHdfsAccessor) EnsureConnected() error {
op := this.RetryPolicy.StartOperation()
for {
err := this.Impl.EnsureConnected()
if IsSuccessOrBenignError(err) || !op.ShouldRetry("Connect: %s", err) {
return err
}
}
}
// Opens HDFS file for reading
func (this *FaultTolerantHdfsAccessor) OpenRead(path string) (ReadSeekCloser, error) {
op := this.RetryPolicy.StartOperation()
for {
result, err := this.Impl.OpenRead(path)
if err == nil {
// wrapping returned HdfsReader with FaultTolerantHdfsReader
return NewFaultTolerantHdfsReader(path, result, this.Impl, this.RetryPolicy), nil
}
if IsSuccessOrBenignError(err) || !op.ShouldRetry("[%s] OpenRead: %s", path, err) {
return nil, err
} else {
// Clean up the bad connection, to let underline connection to get automatic refresh
this.Impl.Close()
}
}
}
// Opens HDFS file for writing
func (this *FaultTolerantHdfsAccessor) CreateFile(path string, mode os.FileMode) (HdfsWriter, error) {
// TODO: implement fault-tolerance. For now re-try-loop is implemented inside FileHandleWriter
return this.Impl.CreateFile(path, mode)
}
// Enumerates HDFS directory
func (this *FaultTolerantHdfsAccessor) ReadDir(path string) ([]Attrs, error) {
op := this.RetryPolicy.StartOperation()
for {
result, err := this.Impl.ReadDir(path)
if IsSuccessOrBenignError(err) || !op.ShouldRetry("[%s] ReadDir: %s", path, err) {
return result, err
} else {
// Clean up the bad connection, to let underline connection to get automatic refresh
this.Impl.Close()
}
}
}
// Retrieves file/directory attributes
func (this *FaultTolerantHdfsAccessor) Stat(path string) (Attrs, error) {
op := this.RetryPolicy.StartOperation()
for {
result, err := this.Impl.Stat(path)
if IsSuccessOrBenignError(err) || !op.ShouldRetry("[%s] Stat: %s", path, err) {
return result, err
} else {
// Clean up the bad connection, to let underline connection to get automatic refresh
this.Impl.Close()
}
}
}
// Retrieves HDFS usage
func (this *FaultTolerantHdfsAccessor) StatFs() (FsInfo, error) {
op := this.RetryPolicy.StartOperation()
for {
result, err := this.Impl.StatFs()
if IsSuccessOrBenignError(err) || !op.ShouldRetry("StatFs: %s", err) {
return result, err
} else {
// Clean up the bad connection, to let underline connection to get automatic refresh
this.Impl.Close()
}
}
}
// Creates a directory
func (this *FaultTolerantHdfsAccessor) Mkdir(path string, mode os.FileMode) error {
op := this.RetryPolicy.StartOperation()
for {
err := this.Impl.Mkdir(path, mode)
if IsSuccessOrBenignError(err) || !op.ShouldRetry("[%s] Mkdir %s: %s", path, mode, err) {
return err
} else {
// Clean up the bad connection, to let underline connection to get automatic refresh
this.Impl.Close()
}
}
}
// Removes a file or directory
func (this *FaultTolerantHdfsAccessor) Remove(path string) error {
op := this.RetryPolicy.StartOperation()
for {
err := this.Impl.Remove(path)
if IsSuccessOrBenignError(err) || !op.ShouldRetry("[%s] Remove: %s", path, err) {
return err
} else {
// Clean up the bad connection, to let underline connection to get automatic refresh
this.Impl.Close()
}
}
}
// Renames file or directory
func (this *FaultTolerantHdfsAccessor) Rename(oldPath string, newPath string) error {
op := this.RetryPolicy.StartOperation()
for {
err := this.Impl.Rename(oldPath, newPath)
if IsSuccessOrBenignError(err) || !op.ShouldRetry("[%s] Rename to %s: %s", oldPath, newPath, err) {
return err
} else {
// Clean up the bad connection, to let underline connection to get automatic refresh
this.Impl.Close()
}
}
}
// Chmod file or directory
func (this *FaultTolerantHdfsAccessor) Chmod(path string, mode os.FileMode) error {
op := this.RetryPolicy.StartOperation()
for {
err := this.Impl.Chmod(path, mode)
if IsSuccessOrBenignError(err) || !op.ShouldRetry("Chmod [%s] to [%d]: %s", path, mode, err) {
return err
} else {
// Clean up the bad connection, to let underline connection to get automatic refresh
this.Impl.Close()
}
}
}
// Chown file or directory
func (this *FaultTolerantHdfsAccessor) Chown(path string, user, group string) error {
op := this.RetryPolicy.StartOperation()
for {
err := this.Impl.Chown(path, user, group)
if IsSuccessOrBenignError(err) || !op.ShouldRetry("Chown [%s] to [%s:%s]: %s", path, user, group, err) {
return err
} else {
// Clean up the bad connection, to let underline connection to get automatic refresh
this.Impl.Close()
}
}
}
// Close underline connection if needed
func (this *FaultTolerantHdfsAccessor) Close() error {
return this.Impl.Close()
}