forked from colinmarc/hdfs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile_writer.go
191 lines (160 loc) · 5.04 KB
/
file_writer.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
package hdfs
import (
"io"
"os"
hdfs "github.com/colinmarc/hdfs/protocol/hadoop_hdfs"
"github.com/colinmarc/hdfs/rpc"
"github.com/golang/protobuf/proto"
)
// A FileWriter represents a writer for an open file in HDFS. It implements
// Writer and Closer, and can only be used for writes. For reads, see
// FileReader and Client.Open.
type FileWriter struct {
client *Client
name string
replication int
blockSize int64
block *hdfs.LocatedBlockProto
blockWriter *rpc.BlockWriter
closed bool
}
// Create opens a new file in HDFS with the default replication, block size,
// and permissions (0644), and returns an io.WriteCloser for writing
// to it. Because of the way that HDFS writes are buffered and acknowledged
// asynchronously, it is very important that Close is called after all data has
// been written.
func (c *Client) Create(name string) (*FileWriter, error) {
_, err := c.getFileInfo(name)
if err == nil {
return nil, &os.PathError{"create", name, os.ErrExist}
} else if !os.IsNotExist(err) {
return nil, &os.PathError{"create", name, err}
}
defaults, err := c.fetchDefaults()
if err != nil {
return nil, err
}
replication := int(defaults.GetReplication())
blockSize := int64(defaults.GetBlockSize())
return c.CreateFile(name, replication, blockSize, 0644)
}
// CreateFile opens a new file in HDFS with the given replication, block size,
// and permissions, and returns an io.WriteCloser for writing to it. Because of
// the way that HDFS writes are buffered and acknowledged asynchronously, it is
// very important that Close is called after all data has been written.
func (c *Client) CreateFile(name string, replication int, blockSize int64, perm os.FileMode) (*FileWriter, error) {
createReq := &hdfs.CreateRequestProto{
Src: proto.String(name),
Masked: &hdfs.FsPermissionProto{Perm: proto.Uint32(uint32(perm))},
ClientName: proto.String(c.namenode.ClientName()),
CreateFlag: proto.Uint32(1),
CreateParent: proto.Bool(false),
Replication: proto.Uint32(uint32(replication)),
BlockSize: proto.Uint64(uint64(blockSize)),
}
createResp := &hdfs.CreateResponseProto{}
err := c.namenode.Execute("create", createReq, createResp)
if err != nil {
if nnErr, ok := err.(*rpc.NamenodeError); ok {
err = interpretException(nnErr.Exception, err)
}
return nil, &os.PathError{"create", name, err}
}
return &FileWriter{
client: c,
name: name,
replication: replication,
blockSize: blockSize,
}, nil
}
// CreateEmptyFile creates a empty file at the given name, with the
// permissions 0644.
func (c *Client) CreateEmptyFile(name string) error {
f, err := c.Create(name)
if err != nil {
return err
}
return f.Close()
}
// Write implements io.Writer for writing to a file in HDFS. Internally, it
// writes data to an internal buffer first, and then later out to HDFS. Because
// of this, it is important that Close is called after all data has been
// written.
func (f *FileWriter) Write(b []byte) (int, error) {
if f.closed {
return 0, io.ErrClosedPipe
}
if f.blockWriter == nil {
err := f.startNewBlock()
if err != nil {
return 0, err
}
}
off := 0
for off < len(b) {
n, err := f.blockWriter.Write(b[off:])
off += n
if err == rpc.ErrEndOfBlock {
err = f.startNewBlock()
}
if err != nil {
return off, err
}
}
return off, nil
}
// Close closes the file, writing any remaining data out to disk and waiting
// for acknowledgements from the datanodes. It is important that Close is called
// after all data has been written.
func (f *FileWriter) Close() error {
if f.closed {
return io.ErrClosedPipe
}
var lastBlock *hdfs.ExtendedBlockProto
if f.blockWriter != nil {
// Close the blockWriter, flushing any buffered packets.
err := f.blockWriter.Close()
if err != nil {
return err
}
lastBlock = f.block.GetB()
}
completeReq := &hdfs.CompleteRequestProto{
Src: proto.String(f.name),
ClientName: proto.String(f.client.namenode.ClientName()),
Last: lastBlock,
}
completeResp := &hdfs.CompleteResponseProto{}
err := f.client.namenode.Execute("complete", completeReq, completeResp)
if err != nil {
return &os.PathError{"create", f.name, err}
}
return nil
}
func (f *FileWriter) startNewBlock() error {
// TODO: we don't need to wait for previous blocks to ack before continuing
var previous *hdfs.ExtendedBlockProto
if f.blockWriter != nil {
err := f.blockWriter.Close()
if err != nil {
return err
}
previous = f.block.GetB()
}
addBlockReq := &hdfs.AddBlockRequestProto{
Src: proto.String(f.name),
ClientName: proto.String(f.client.namenode.ClientName()),
Previous: previous,
}
addBlockResp := &hdfs.AddBlockResponseProto{}
err := f.client.namenode.Execute("addBlock", addBlockReq, addBlockResp)
if err != nil {
if nnErr, ok := err.(*rpc.NamenodeError); ok {
err = interpretException(nnErr.Exception, err)
}
return &os.PathError{"create", f.name, err}
}
f.block = addBlockResp.GetBlock()
f.blockWriter = rpc.NewBlockWriter(f.block, f.client.namenode, f.blockSize)
return nil
}