2016-03-28 00:52:38 -04:00
|
|
|
/*
|
|
|
|
* Minio Cloud Storage, (C) 2016 Minio, Inc.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
slashpath "path"
|
|
|
|
"time"
|
|
|
|
|
2016-04-25 02:12:54 -04:00
|
|
|
"github.com/Sirupsen/logrus"
|
2016-03-28 00:52:38 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// Erasure block size.
|
|
|
|
const erasureBlockSize = 4 * 1024 * 1024 // 4MiB.
|
|
|
|
|
2016-04-19 15:30:10 -04:00
|
|
|
// cleanupCreateFileOps - cleans up all the temporary files and other
|
|
|
|
// temporary data upon any failure.
|
|
|
|
func (xl XL) cleanupCreateFileOps(volume, path string, writers ...io.WriteCloser) {
|
|
|
|
closeAndRemoveWriters(writers...)
|
|
|
|
for _, disk := range xl.storageDisks {
|
2016-04-25 19:00:22 -04:00
|
|
|
if err := disk.DeleteFile(volume, path); err != nil {
|
|
|
|
log.WithFields(logrus.Fields{
|
|
|
|
"volume": volume,
|
|
|
|
"path": path,
|
|
|
|
}).Errorf("DeleteFile failed with %s", err)
|
|
|
|
}
|
2016-04-19 15:30:10 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-28 00:52:38 -04:00
|
|
|
// Close and remove writers if they are safeFile.
|
|
|
|
func closeAndRemoveWriters(writers ...io.WriteCloser) {
|
|
|
|
for _, writer := range writers {
|
2016-04-25 19:00:22 -04:00
|
|
|
if err := safeCloseAndRemove(writer); err != nil {
|
|
|
|
log.Errorf("Closing writer failed with %s", err)
|
|
|
|
}
|
2016-03-28 00:52:38 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WriteErasure reads predefined blocks, encodes them and writes to
|
|
|
|
// configured storage disks.
|
2016-04-25 19:00:22 -04:00
|
|
|
func (xl XL) writeErasure(volume, path string, reader *io.PipeReader, wcloser *waitCloser) {
|
2016-04-25 03:27:04 -04:00
|
|
|
// Release the block writer upon function return.
|
2016-04-25 19:00:22 -04:00
|
|
|
defer wcloser.release()
|
2016-04-25 03:27:04 -04:00
|
|
|
|
2016-04-26 16:03:37 -04:00
|
|
|
// Lock right before reading from disk.
|
2016-04-29 04:29:09 -04:00
|
|
|
nsMutex.RLock(volume, path)
|
2016-04-26 16:03:37 -04:00
|
|
|
partsMetadata, errs := xl.getPartsMetadata(volume, path)
|
2016-04-29 04:29:09 -04:00
|
|
|
nsMutex.RUnlock(volume, path)
|
2016-04-26 16:03:37 -04:00
|
|
|
|
|
|
|
// Count errors other than fileNotFound, bigger than the allowed
|
|
|
|
// readQuorum, if yes throw an error.
|
|
|
|
metadataReadErrCount := 0
|
|
|
|
for _, err := range errs {
|
|
|
|
if err != nil && err != errFileNotFound {
|
|
|
|
metadataReadErrCount++
|
|
|
|
if metadataReadErrCount > xl.readQuorum {
|
|
|
|
log.WithFields(logrus.Fields{
|
|
|
|
"volume": volume,
|
|
|
|
"path": path,
|
|
|
|
}).Errorf("%s", err)
|
|
|
|
reader.CloseWithError(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-30 06:21:54 -04:00
|
|
|
var err error
|
2016-04-26 16:03:37 -04:00
|
|
|
// List all the file versions on existing files.
|
2016-04-30 06:21:54 -04:00
|
|
|
versions := listFileVersions(partsMetadata, errs)
|
2016-04-26 16:03:37 -04:00
|
|
|
// Get highest file version.
|
|
|
|
higherVersion := highestInt(versions)
|
2016-04-20 11:32:47 -04:00
|
|
|
// Increment to have next higher version.
|
2016-04-20 13:14:30 -04:00
|
|
|
higherVersion++
|
|
|
|
|
|
|
|
writers := make([]io.WriteCloser, len(xl.storageDisks))
|
|
|
|
|
2016-04-28 22:27:02 -04:00
|
|
|
xlMetaV1FilePath := slashpath.Join(path, xlMetaV1File)
|
2016-04-20 13:14:30 -04:00
|
|
|
metadataWriters := make([]io.WriteCloser, len(xl.storageDisks))
|
2016-03-28 00:52:38 -04:00
|
|
|
|
2016-04-20 00:58:11 -04:00
|
|
|
// Save additional erasureMetadata.
|
|
|
|
modTime := time.Now().UTC()
|
|
|
|
|
2016-04-20 13:14:30 -04:00
|
|
|
createFileError := 0
|
2016-03-28 00:52:38 -04:00
|
|
|
for index, disk := range xl.storageDisks {
|
2016-04-28 22:27:02 -04:00
|
|
|
erasurePart := slashpath.Join(path, fmt.Sprintf("file.%d", index))
|
2016-04-26 16:03:37 -04:00
|
|
|
var writer io.WriteCloser
|
|
|
|
writer, err = disk.CreateFile(volume, erasurePart)
|
2016-03-28 00:52:38 -04:00
|
|
|
if err != nil {
|
2016-04-25 02:12:54 -04:00
|
|
|
log.WithFields(logrus.Fields{
|
|
|
|
"volume": volume,
|
|
|
|
"path": path,
|
2016-04-26 16:03:37 -04:00
|
|
|
}).Errorf("CreateFile failed with %s", err)
|
2016-04-20 13:14:30 -04:00
|
|
|
createFileError++
|
|
|
|
|
2016-04-20 11:32:47 -04:00
|
|
|
// We can safely allow CreateFile errors up to len(xl.storageDisks) - xl.writeQuorum
|
|
|
|
// otherwise return failure.
|
2016-04-20 13:14:30 -04:00
|
|
|
if createFileError <= len(xl.storageDisks)-xl.writeQuorum {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2016-03-28 00:52:38 -04:00
|
|
|
// Remove previous temp writers for any failure.
|
2016-04-19 15:30:10 -04:00
|
|
|
xl.cleanupCreateFileOps(volume, path, append(writers, metadataWriters...)...)
|
2016-04-20 20:23:23 -04:00
|
|
|
reader.CloseWithError(errWriteQuorum)
|
2016-03-28 00:52:38 -04:00
|
|
|
return
|
|
|
|
}
|
2016-04-20 13:14:30 -04:00
|
|
|
|
|
|
|
// create meta data file
|
|
|
|
var metadataWriter io.WriteCloser
|
2016-04-28 22:27:02 -04:00
|
|
|
metadataWriter, err = disk.CreateFile(volume, xlMetaV1FilePath)
|
2016-03-28 00:52:38 -04:00
|
|
|
if err != nil {
|
2016-04-25 02:12:54 -04:00
|
|
|
log.WithFields(logrus.Fields{
|
|
|
|
"volume": volume,
|
|
|
|
"path": path,
|
2016-04-26 16:03:37 -04:00
|
|
|
}).Errorf("CreateFile failed with %s", err)
|
2016-04-20 13:14:30 -04:00
|
|
|
createFileError++
|
|
|
|
|
2016-04-20 11:32:47 -04:00
|
|
|
// We can safely allow CreateFile errors up to
|
|
|
|
// len(xl.storageDisks) - xl.writeQuorum otherwise return failure.
|
2016-04-20 13:14:30 -04:00
|
|
|
if createFileError <= len(xl.storageDisks)-xl.writeQuorum {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2016-03-28 00:52:38 -04:00
|
|
|
// Remove previous temp writers for any failure.
|
2016-04-19 15:30:10 -04:00
|
|
|
xl.cleanupCreateFileOps(volume, path, append(writers, metadataWriters...)...)
|
2016-04-20 20:23:23 -04:00
|
|
|
reader.CloseWithError(errWriteQuorum)
|
2016-03-28 00:52:38 -04:00
|
|
|
return
|
|
|
|
}
|
2016-04-20 13:14:30 -04:00
|
|
|
|
2016-04-20 11:32:47 -04:00
|
|
|
writers[index] = writer
|
|
|
|
metadataWriters[index] = metadataWriter
|
2016-03-28 00:52:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Allocate 4MiB block size buffer for reading.
|
2016-04-20 11:32:47 -04:00
|
|
|
dataBuffer := make([]byte, erasureBlockSize)
|
2016-03-28 00:52:38 -04:00
|
|
|
var totalSize int64 // Saves total incoming stream size.
|
|
|
|
for {
|
|
|
|
// Read up to allocated block size.
|
2016-04-26 16:03:37 -04:00
|
|
|
var n int
|
|
|
|
n, err = io.ReadFull(reader, dataBuffer)
|
2016-03-28 00:52:38 -04:00
|
|
|
if err != nil {
|
|
|
|
// Any unexpected errors, close the pipe reader with error.
|
|
|
|
if err != io.ErrUnexpectedEOF && err != io.EOF {
|
2016-04-26 16:03:37 -04:00
|
|
|
log.WithFields(logrus.Fields{
|
|
|
|
"volume": volume,
|
|
|
|
"path": path,
|
|
|
|
}).Errorf("io.ReadFull failed with %s", err)
|
2016-03-28 00:52:38 -04:00
|
|
|
// Remove all temp writers.
|
2016-04-19 15:30:10 -04:00
|
|
|
xl.cleanupCreateFileOps(volume, path, append(writers, metadataWriters...)...)
|
2016-03-28 00:52:38 -04:00
|
|
|
reader.CloseWithError(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// At EOF break out.
|
|
|
|
if err == io.EOF {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if n > 0 {
|
|
|
|
// Split the input buffer into data and parity blocks.
|
2016-04-20 11:32:47 -04:00
|
|
|
var dataBlocks [][]byte
|
|
|
|
dataBlocks, err = xl.ReedSolomon.Split(dataBuffer[0:n])
|
2016-03-28 00:52:38 -04:00
|
|
|
if err != nil {
|
2016-04-26 16:03:37 -04:00
|
|
|
log.WithFields(logrus.Fields{
|
|
|
|
"volume": volume,
|
|
|
|
"path": path,
|
|
|
|
}).Errorf("Splitting data buffer into erasure data blocks failed with %s", err)
|
2016-03-28 00:52:38 -04:00
|
|
|
// Remove all temp writers.
|
2016-04-19 15:30:10 -04:00
|
|
|
xl.cleanupCreateFileOps(volume, path, append(writers, metadataWriters...)...)
|
2016-03-28 00:52:38 -04:00
|
|
|
reader.CloseWithError(err)
|
|
|
|
return
|
|
|
|
}
|
2016-04-20 11:32:47 -04:00
|
|
|
|
2016-03-28 00:52:38 -04:00
|
|
|
// Encode parity blocks using data blocks.
|
2016-04-20 11:32:47 -04:00
|
|
|
err = xl.ReedSolomon.Encode(dataBlocks)
|
2016-03-28 00:52:38 -04:00
|
|
|
if err != nil {
|
2016-04-26 16:03:37 -04:00
|
|
|
log.WithFields(logrus.Fields{
|
|
|
|
"volume": volume,
|
|
|
|
"path": path,
|
|
|
|
}).Errorf("Encoding erasure data blocks failed with %s", err)
|
2016-03-28 00:52:38 -04:00
|
|
|
// Remove all temp writers upon error.
|
2016-04-19 15:30:10 -04:00
|
|
|
xl.cleanupCreateFileOps(volume, path, append(writers, metadataWriters...)...)
|
2016-03-28 00:52:38 -04:00
|
|
|
reader.CloseWithError(err)
|
|
|
|
return
|
|
|
|
}
|
2016-04-20 13:14:30 -04:00
|
|
|
|
|
|
|
// Loop through and write encoded data to quorum disks.
|
2016-04-20 11:32:47 -04:00
|
|
|
for index, writer := range writers {
|
|
|
|
if writer == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
encodedData := dataBlocks[index]
|
|
|
|
_, err = writers[index].Write(encodedData)
|
2016-03-28 00:52:38 -04:00
|
|
|
if err != nil {
|
2016-04-26 16:03:37 -04:00
|
|
|
log.WithFields(logrus.Fields{
|
|
|
|
"volume": volume,
|
|
|
|
"path": path,
|
|
|
|
"diskIndex": index,
|
|
|
|
}).Errorf("Writing encoded blocks failed with %s", err)
|
2016-03-28 00:52:38 -04:00
|
|
|
// Remove all temp writers upon error.
|
2016-04-19 15:30:10 -04:00
|
|
|
xl.cleanupCreateFileOps(volume, path, append(writers, metadataWriters...)...)
|
2016-03-28 00:52:38 -04:00
|
|
|
reader.CloseWithError(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2016-04-20 11:32:47 -04:00
|
|
|
|
2016-03-28 00:52:38 -04:00
|
|
|
// Update total written.
|
|
|
|
totalSize += int64(n)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize metadata map, save all erasure related metadata.
|
2016-04-28 22:27:02 -04:00
|
|
|
metadata := xlMetaV1{}
|
|
|
|
metadata.Version = "1"
|
|
|
|
metadata.Stat.Size = totalSize
|
|
|
|
metadata.Stat.ModTime = modTime
|
|
|
|
metadata.Minio.Release = minioReleaseTag
|
2016-04-20 13:14:30 -04:00
|
|
|
if len(xl.storageDisks) > len(writers) {
|
2016-04-20 11:32:47 -04:00
|
|
|
// Save file.version only if we wrote to less disks than all
|
|
|
|
// storage disks.
|
2016-04-28 22:27:02 -04:00
|
|
|
metadata.Stat.Version = higherVersion
|
2016-04-20 13:14:30 -04:00
|
|
|
}
|
2016-04-28 22:27:02 -04:00
|
|
|
metadata.Erasure.DataBlocks = xl.DataBlocks
|
|
|
|
metadata.Erasure.ParityBlocks = xl.ParityBlocks
|
|
|
|
metadata.Erasure.BlockSize = erasureBlockSize
|
2016-03-28 00:52:38 -04:00
|
|
|
|
|
|
|
// Write all the metadata.
|
2016-04-20 13:14:30 -04:00
|
|
|
// below case is not handled here
|
|
|
|
// Case: when storageDisks is 16 and write quorumDisks is 13,
|
|
|
|
// meta data write failure up to 2 can be considered.
|
|
|
|
// currently we fail for any meta data writes
|
2016-04-20 11:32:47 -04:00
|
|
|
for index, metadataWriter := range metadataWriters {
|
|
|
|
if metadataWriter == nil {
|
|
|
|
continue
|
|
|
|
}
|
2016-03-28 00:52:38 -04:00
|
|
|
|
2016-04-21 18:33:26 -04:00
|
|
|
// Write metadata.
|
2016-04-26 16:03:37 -04:00
|
|
|
err = metadata.Write(metadataWriter)
|
2016-03-28 00:52:38 -04:00
|
|
|
if err != nil {
|
2016-04-26 16:03:37 -04:00
|
|
|
log.WithFields(logrus.Fields{
|
|
|
|
"volume": volume,
|
|
|
|
"path": path,
|
|
|
|
"diskIndex": index,
|
|
|
|
}).Errorf("Writing metadata failed with %s", err)
|
2016-04-19 15:30:10 -04:00
|
|
|
// Remove temporary files.
|
|
|
|
xl.cleanupCreateFileOps(volume, path, append(writers, metadataWriters...)...)
|
2016-03-28 00:52:38 -04:00
|
|
|
reader.CloseWithError(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-22 21:16:02 -04:00
|
|
|
// Lock right before commit to disk.
|
2016-04-29 04:29:09 -04:00
|
|
|
nsMutex.Lock(volume, path)
|
|
|
|
defer nsMutex.Unlock(volume, path)
|
2016-04-22 21:16:02 -04:00
|
|
|
|
2016-03-28 00:52:38 -04:00
|
|
|
// Close all writers and metadata writers in routines.
|
2016-04-20 11:32:47 -04:00
|
|
|
for index, writer := range writers {
|
|
|
|
if writer == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// Safely wrote, now rename to its actual location.
|
2016-04-26 16:03:37 -04:00
|
|
|
if err = writer.Close(); err != nil {
|
|
|
|
log.WithFields(logrus.Fields{
|
|
|
|
"volume": volume,
|
|
|
|
"path": path,
|
|
|
|
"diskIndex": index,
|
|
|
|
}).Errorf("Safely committing part failed with %s", err)
|
|
|
|
// Remove all temp writers upon error.
|
|
|
|
xl.cleanupCreateFileOps(volume, path, append(writers, metadataWriters...)...)
|
|
|
|
reader.CloseWithError(err)
|
|
|
|
return
|
|
|
|
}
|
2016-04-20 11:32:47 -04:00
|
|
|
|
|
|
|
if metadataWriters[index] == nil {
|
|
|
|
continue
|
|
|
|
}
|
2016-03-28 00:52:38 -04:00
|
|
|
// Safely wrote, now rename to its actual location.
|
2016-04-26 16:03:37 -04:00
|
|
|
if err = metadataWriters[index].Close(); err != nil {
|
|
|
|
log.WithFields(logrus.Fields{
|
|
|
|
"volume": volume,
|
|
|
|
"path": path,
|
|
|
|
"diskIndex": index,
|
|
|
|
}).Errorf("Safely committing metadata failed with %s", err)
|
|
|
|
// Remove all temp writers upon error.
|
|
|
|
xl.cleanupCreateFileOps(volume, path, append(writers, metadataWriters...)...)
|
|
|
|
reader.CloseWithError(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-03-28 00:52:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Close the pipe reader and return.
|
|
|
|
reader.Close()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreateFile - create a file.
|
|
|
|
func (xl XL) CreateFile(volume, path string) (writeCloser io.WriteCloser, err error) {
|
|
|
|
if !isValidVolname(volume) {
|
|
|
|
return nil, errInvalidArgument
|
|
|
|
}
|
|
|
|
if !isValidPath(path) {
|
|
|
|
return nil, errInvalidArgument
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize pipe for data pipe line.
|
|
|
|
pipeReader, pipeWriter := io.Pipe()
|
|
|
|
|
2016-04-25 19:00:22 -04:00
|
|
|
// Initialize a new wait closer, implements both Write and Close.
|
|
|
|
wcloser := newWaitCloser(pipeWriter)
|
2016-04-22 21:16:02 -04:00
|
|
|
|
2016-03-28 00:52:38 -04:00
|
|
|
// Start erasure encoding in routine, reading data block by block from pipeReader.
|
2016-04-25 19:00:22 -04:00
|
|
|
go xl.writeErasure(volume, path, pipeReader, wcloser)
|
2016-03-28 00:52:38 -04:00
|
|
|
|
2016-04-25 19:00:22 -04:00
|
|
|
// Return the writer, caller should start writing to this.
|
|
|
|
return wcloser, nil
|
2016-03-28 00:52:38 -04:00
|
|
|
}
|