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
|
package hdfs
import (
"errors"
"os"
"time"
hdfs "github.com/colinmarc/hdfs/v2/internal/protocol/hadoop_hdfs"
"github.com/colinmarc/hdfs/v2/internal/transfer"
"google.golang.org/protobuf/proto"
)
var ErrReplicating = errors.New("replication in progress")
// IsErrReplicating returns true if the passed error is an os.PathError wrapping
// ErrReplicating.
func IsErrReplicating(err error) bool {
pe, ok := err.(*os.PathError)
return ok && pe.Err == ErrReplicating
}
// 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
blockWriter *transfer.BlockWriter
deadline time.Time
}
// 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)
err = interpretException(err)
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 {
return nil, &os.PathError{"create", name, interpretCreateException(err)}
}
return &FileWriter{
client: c,
name: name,
replication: replication,
blockSize: blockSize,
}, nil
}
// Append opens an existing file in HDFS 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) Append(name string) (*FileWriter, error) {
_, err := c.getFileInfo(name)
if err != nil {
return nil, &os.PathError{"append", name, interpretException(err)}
}
appendReq := &hdfs.AppendRequestProto{
Src: proto.String(name),
ClientName: proto.String(c.namenode.ClientName),
}
appendResp := &hdfs.AppendResponseProto{}
err = c.namenode.Execute("append", appendReq, appendResp)
if err != nil {
return nil, &os.PathError{"append", name, interpretException(err)}
}
f := &FileWriter{
client: c,
name: name,
replication: int(appendResp.Stat.GetBlockReplication()),
blockSize: int64(appendResp.Stat.GetBlocksize()),
}
// This returns nil if there are no blocks (it's an empty file) or if the
// last block is full (so we have to start a fresh block).
block := appendResp.GetBlock()
if block == nil {
return f, nil
}
dialFunc, err := f.client.wrapDatanodeDial(
f.client.options.DatanodeDialFunc,
block.GetBlockToken())
if err != nil {
return nil, err
}
f.blockWriter = &transfer.BlockWriter{
ClientName: f.client.namenode.ClientName,
Block: block,
BlockSize: f.blockSize,
Offset: int64(block.B.GetNumBytes()),
Append: true,
UseDatanodeHostname: f.client.options.UseDatanodeHostname,
DialFunc: dialFunc,
}
err = f.blockWriter.SetDeadline(f.deadline)
if err != nil {
return nil, err
}
return f, 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()
}
// SetDeadline sets the deadline for future Write, Flush, and Close calls. A
// zero value for t means those calls will not time out.
//
// Note that because of buffering, Write calls that do not result in a blocking
// network call may still succeed after the deadline.
func (f *FileWriter) SetDeadline(t time.Time) error {
f.deadline = t
if f.blockWriter != nil {
return f.blockWriter.SetDeadline(t)
}
// Return the error at connection time.
return nil
}
// 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.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 == transfer.ErrEndOfBlock {
err = f.startNewBlock()
}
if err != nil {
return off, err
}
}
return off, nil
}
// Flush flushes any buffered data out to the datanodes. Even immediately after
// a call to Flush, it is still necessary to call Close once all data has been
// written.
func (f *FileWriter) Flush() error {
if f.blockWriter != nil {
return f.blockWriter.Flush()
}
return 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.
//
// If the datanodes have acknowledged all writes but not yet to the namenode,
// it can return ErrReplicating (wrapped in an os.PathError). This indicates
// that all data has been written, but the lease is still open for the file.
// It is safe in this case to either ignore the error (and let the lease expire
// on its own) or to call Close multiple times until it completes without an
// error. The Java client, for context, always chooses to retry, with
// exponential backoff.
func (f *FileWriter) Close() error {
var lastBlock *hdfs.ExtendedBlockProto
if f.blockWriter != nil {
lastBlock = f.blockWriter.Block.GetB()
// Close the blockWriter, flushing any buffered packets.
err := f.finalizeBlock()
if err != nil {
return err
}
}
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}
} else if completeResp.GetResult() == false {
return &os.PathError{"create", f.name, ErrReplicating}
}
return nil
}
func (f *FileWriter) startNewBlock() error {
var previous *hdfs.ExtendedBlockProto
if f.blockWriter != nil {
previous = f.blockWriter.Block.GetB()
// TODO: We don't actually need to wait for previous blocks to ack before
// continuing.
err := f.finalizeBlock()
if err != nil {
return err
}
}
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 {
return &os.PathError{"create", f.name, interpretException(err)}
}
block := addBlockResp.GetBlock()
dialFunc, err := f.client.wrapDatanodeDial(
f.client.options.DatanodeDialFunc, block.GetBlockToken())
if err != nil {
return err
}
f.blockWriter = &transfer.BlockWriter{
ClientName: f.client.namenode.ClientName,
Block: block,
BlockSize: f.blockSize,
UseDatanodeHostname: f.client.options.UseDatanodeHostname,
DialFunc: dialFunc,
}
return f.blockWriter.SetDeadline(f.deadline)
}
func (f *FileWriter) finalizeBlock() error {
err := f.blockWriter.Close()
if err != nil {
return err
}
// Finalize the block on the namenode.
lastBlock := f.blockWriter.Block.GetB()
lastBlock.NumBytes = proto.Uint64(uint64(f.blockWriter.Offset))
updateReq := &hdfs.UpdateBlockForPipelineRequestProto{
Block: lastBlock,
ClientName: proto.String(f.client.namenode.ClientName),
}
updateResp := &hdfs.UpdateBlockForPipelineResponseProto{}
err = f.client.namenode.Execute("updateBlockForPipeline", updateReq, updateResp)
if err != nil {
return err
}
f.blockWriter = nil
return nil
}
|