2016-05-20 23:48:47 -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.
|
|
|
|
*/
|
|
|
|
|
2016-08-18 19:23:42 -04:00
|
|
|
package cmd
|
2016-05-20 23:48:47 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2016-12-08 23:35:07 -05:00
|
|
|
"runtime/debug"
|
2016-05-26 17:13:10 -04:00
|
|
|
"sort"
|
2016-11-16 19:42:23 -05:00
|
|
|
"sync"
|
2016-05-26 17:13:10 -04:00
|
|
|
|
2016-11-22 21:18:22 -05:00
|
|
|
humanize "github.com/dustin/go-humanize"
|
2016-05-26 17:13:10 -04:00
|
|
|
"github.com/minio/minio/pkg/disk"
|
2016-07-03 19:58:21 -04:00
|
|
|
"github.com/minio/minio/pkg/objcache"
|
2016-05-20 23:48:47 -04:00
|
|
|
)
|
|
|
|
|
2016-05-30 19:51:59 -04:00
|
|
|
// XL constants.
|
2016-05-20 23:48:47 -04:00
|
|
|
const (
|
2016-05-30 19:51:59 -04:00
|
|
|
// Format config file carries backend format specific details.
|
2016-05-20 23:48:47 -04:00
|
|
|
formatConfigFile = "format.json"
|
2016-06-22 20:18:31 -04:00
|
|
|
|
|
|
|
// Format config tmp file carries backend format.
|
|
|
|
formatConfigFileTmp = "format.json.tmp"
|
|
|
|
|
2016-05-30 19:51:59 -04:00
|
|
|
// XL metadata file carries per object metadata.
|
|
|
|
xlMetaJSONFile = "xl.json"
|
2016-06-22 20:18:31 -04:00
|
|
|
|
2016-05-30 19:51:59 -04:00
|
|
|
// Uploads metadata file carries per multipart object metadata.
|
|
|
|
uploadsJSONFile = "uploads.json"
|
2016-07-03 19:58:21 -04:00
|
|
|
|
2016-12-08 23:35:07 -05:00
|
|
|
// Represents the minimum required RAM size before
|
|
|
|
// we enable caching.
|
|
|
|
minRAMSize = 8 * humanize.GiByte
|
2016-07-03 19:58:21 -04:00
|
|
|
|
|
|
|
// Maximum erasure blocks.
|
|
|
|
maxErasureBlocks = 16
|
|
|
|
|
|
|
|
// Minimum erasure blocks.
|
2016-07-19 22:24:32 -04:00
|
|
|
minErasureBlocks = 4
|
2016-05-20 23:48:47 -04:00
|
|
|
)
|
|
|
|
|
2016-05-30 19:51:59 -04:00
|
|
|
// xlObjects - Implements XL object layer.
|
2016-05-20 23:48:47 -04:00
|
|
|
type xlObjects struct {
|
2016-11-16 19:42:23 -05:00
|
|
|
mutex *sync.Mutex
|
2016-08-25 20:16:34 -04:00
|
|
|
storageDisks []StorageAPI // Collection of initialized backend disks.
|
|
|
|
dataBlocks int // dataBlocks count caculated for erasure.
|
|
|
|
parityBlocks int // parityBlocks count calculated for erasure.
|
|
|
|
readQuorum int // readQuorum minimum required disks to read data.
|
|
|
|
writeQuorum int // writeQuorum minimum required disks to write data.
|
2016-05-30 19:51:59 -04:00
|
|
|
|
2016-07-03 19:58:21 -04:00
|
|
|
// ListObjects pool management.
|
2016-06-05 14:55:45 -04:00
|
|
|
listPool *treeWalkPool
|
2016-05-20 23:48:47 -04:00
|
|
|
|
2016-07-03 19:58:21 -04:00
|
|
|
// Object cache for caching objects.
|
|
|
|
objCache *objcache.Cache
|
2016-05-20 23:48:47 -04:00
|
|
|
|
2016-07-03 19:58:21 -04:00
|
|
|
// Object cache enabled.
|
|
|
|
objCacheEnabled bool
|
|
|
|
}
|
2016-05-20 23:48:47 -04:00
|
|
|
|
2016-09-15 16:43:40 -04:00
|
|
|
// list of all errors that can be ignored in tree walk operation in XL
|
2016-11-23 23:05:04 -05:00
|
|
|
var xlTreeWalkIgnoredErrs = append(baseIgnoredErrs, errDiskAccessDenied, errVolumeNotFound, errFileNotFound)
|
2016-09-15 16:43:40 -04:00
|
|
|
|
2017-01-16 20:05:00 -05:00
|
|
|
// newXLObjectLayer - initialize any object layer depending on the number of disks.
|
|
|
|
func newXLObjectLayer(storageDisks []StorageAPI) (ObjectLayer, error) {
|
|
|
|
// Initialize XL object layer.
|
|
|
|
objAPI, err := newXLObjects(storageDisks)
|
|
|
|
fatalIf(err, "Unable to initialize XL object layer.")
|
|
|
|
|
|
|
|
// Initialize and load bucket policies.
|
|
|
|
err = initBucketPolicies(objAPI)
|
|
|
|
fatalIf(err, "Unable to load all bucket policies.")
|
|
|
|
|
|
|
|
// Initialize a new event notifier.
|
|
|
|
err = initEventNotifier(objAPI)
|
|
|
|
fatalIf(err, "Unable to initialize event notification.")
|
|
|
|
|
|
|
|
// Success.
|
|
|
|
return objAPI, nil
|
|
|
|
}
|
|
|
|
|
2016-08-28 22:31:59 -04:00
|
|
|
// newXLObjects - initialize new xl object layer.
|
2016-10-05 15:48:07 -04:00
|
|
|
func newXLObjects(storageDisks []StorageAPI) (ObjectLayer, error) {
|
|
|
|
if storageDisks == nil {
|
2016-08-28 22:31:59 -04:00
|
|
|
return nil, errInvalidArgument
|
2016-06-02 19:34:15 -04:00
|
|
|
}
|
|
|
|
|
2016-10-14 22:57:40 -04:00
|
|
|
readQuorum := len(storageDisks) / 2
|
|
|
|
writeQuorum := len(storageDisks)/2 + 1
|
|
|
|
|
2016-05-20 23:48:47 -04:00
|
|
|
// Load saved XL format.json and validate.
|
2016-10-14 22:57:40 -04:00
|
|
|
newStorageDisks, err := loadFormatXL(storageDisks, readQuorum)
|
2016-05-20 23:48:47 -04:00
|
|
|
if err != nil {
|
2016-06-02 19:34:15 -04:00
|
|
|
return nil, fmt.Errorf("Unable to recognize backend format, %s", err)
|
2016-05-20 23:48:47 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Calculate data and parity blocks.
|
2016-10-14 22:57:40 -04:00
|
|
|
dataBlocks, parityBlocks := len(newStorageDisks)/2, len(newStorageDisks)/2
|
2016-05-20 23:48:47 -04:00
|
|
|
|
2016-07-08 23:34:27 -04:00
|
|
|
// Initialize list pool.
|
|
|
|
listPool := newTreeWalkPool(globalLookupTimeout)
|
|
|
|
|
2016-06-01 19:43:31 -04:00
|
|
|
// Initialize xl objects.
|
2016-11-16 19:42:23 -05:00
|
|
|
xl := &xlObjects{
|
2016-12-08 23:35:07 -05:00
|
|
|
mutex: &sync.Mutex{},
|
|
|
|
storageDisks: newStorageDisks,
|
|
|
|
dataBlocks: dataBlocks,
|
|
|
|
parityBlocks: parityBlocks,
|
|
|
|
listPool: listPool,
|
|
|
|
}
|
|
|
|
|
2017-03-02 13:34:37 -05:00
|
|
|
// Get cache size if _MINIO_CACHE environment variable is set.
|
|
|
|
var maxCacheSize uint64
|
2017-03-30 14:21:19 -04:00
|
|
|
if !globalXLObjCacheDisabled {
|
2017-03-02 13:34:37 -05:00
|
|
|
maxCacheSize, err = GetMaxCacheSize()
|
|
|
|
errorIf(err, "Unable to get maximum cache size")
|
|
|
|
|
|
|
|
// Enable object cache if cache size is more than zero
|
|
|
|
xl.objCacheEnabled = maxCacheSize > 0
|
|
|
|
}
|
2016-12-08 23:35:07 -05:00
|
|
|
|
|
|
|
// Check if object cache is enabled.
|
|
|
|
if xl.objCacheEnabled {
|
|
|
|
// Initialize object cache.
|
2017-03-08 23:42:45 -05:00
|
|
|
objCache, oerr := objcache.New(maxCacheSize, objcache.DefaultExpiry)
|
|
|
|
if oerr != nil {
|
|
|
|
return nil, oerr
|
2017-03-02 13:34:37 -05:00
|
|
|
}
|
2016-12-08 23:35:07 -05:00
|
|
|
objCache.OnEviction = func(key string) {
|
|
|
|
debug.FreeOSMemory()
|
|
|
|
}
|
|
|
|
xl.objCache = objCache
|
2016-05-20 23:48:47 -04:00
|
|
|
}
|
|
|
|
|
2016-11-25 21:11:50 -05:00
|
|
|
// Initialize meta volume, if volume already exists ignores it.
|
2017-03-06 05:00:15 -05:00
|
|
|
if err = initMetaVolume(xl.storageDisks); err != nil {
|
2016-11-25 21:11:50 -05:00
|
|
|
return nil, fmt.Errorf("Unable to initialize '.minio.sys' meta volume, %s", err)
|
|
|
|
}
|
|
|
|
|
2016-05-20 23:48:47 -04:00
|
|
|
// Figure out read and write quorum based on number of storage disks.
|
2016-07-19 22:24:32 -04:00
|
|
|
// READ and WRITE quorum is always set to (N/2) number of disks.
|
2016-10-14 22:57:40 -04:00
|
|
|
xl.readQuorum = readQuorum
|
|
|
|
xl.writeQuorum = writeQuorum
|
2016-05-20 23:48:47 -04:00
|
|
|
|
2017-04-09 03:28:27 -04:00
|
|
|
// If the number of offline servers is equal to the readQuorum
|
|
|
|
// (i.e. the number of online servers also equals the
|
|
|
|
// readQuorum), we cannot perform quick-heal (no
|
|
|
|
// write-quorum). However reads may still be possible, so we
|
|
|
|
// skip quick-heal in this case, and continue.
|
|
|
|
offlineCount := len(newStorageDisks) - diskCount(newStorageDisks)
|
|
|
|
if offlineCount == readQuorum {
|
|
|
|
return xl, nil
|
|
|
|
}
|
|
|
|
|
2016-11-16 19:42:23 -05:00
|
|
|
// Do a quick heal on the buckets themselves for any discrepancies.
|
2017-03-08 13:00:47 -05:00
|
|
|
return xl, quickHeal(xl.storageDisks, xl.writeQuorum, xl.readQuorum)
|
2016-05-20 23:48:47 -04:00
|
|
|
}
|
2016-05-26 17:13:10 -04:00
|
|
|
|
2016-08-15 02:55:48 -04:00
|
|
|
// Shutdown function for object storage interface.
|
|
|
|
func (xl xlObjects) Shutdown() error {
|
|
|
|
// Add any object layer shutdown activities here.
|
2017-01-23 03:32:55 -05:00
|
|
|
for _, disk := range xl.storageDisks {
|
|
|
|
// This closes storage rpc client connections if any.
|
|
|
|
// Otherwise this is a no-op.
|
|
|
|
if disk == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
disk.Close()
|
|
|
|
}
|
2016-08-15 02:55:48 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-05-26 17:13:10 -04:00
|
|
|
// byDiskTotal is a collection satisfying sort.Interface.
|
|
|
|
type byDiskTotal []disk.Info
|
|
|
|
|
|
|
|
func (d byDiskTotal) Len() int { return len(d) }
|
|
|
|
func (d byDiskTotal) Swap(i, j int) { d[i], d[j] = d[j], d[i] }
|
|
|
|
func (d byDiskTotal) Less(i, j int) bool {
|
|
|
|
return d[i].Total < d[j].Total
|
|
|
|
}
|
|
|
|
|
2016-10-05 15:48:07 -04:00
|
|
|
// getDisksInfo - fetch disks info across all other storage API.
|
|
|
|
func getDisksInfo(disks []StorageAPI) (disksInfo []disk.Info, onlineDisks int, offlineDisks int) {
|
2016-10-17 17:31:33 -04:00
|
|
|
disksInfo = make([]disk.Info, len(disks))
|
|
|
|
for i, storageDisk := range disks {
|
2016-09-15 02:53:42 -04:00
|
|
|
if storageDisk == nil {
|
|
|
|
// Storage disk is empty, perhaps ignored disk or not available.
|
2016-10-05 15:48:07 -04:00
|
|
|
offlineDisks++
|
2016-09-15 02:53:42 -04:00
|
|
|
continue
|
|
|
|
}
|
2016-08-25 20:16:34 -04:00
|
|
|
info, err := storageDisk.DiskInfo()
|
2016-05-26 17:13:10 -04:00
|
|
|
if err != nil {
|
2016-08-25 20:16:34 -04:00
|
|
|
errorIf(err, "Unable to fetch disk info for %#v", storageDisk)
|
2017-01-02 13:43:56 -05:00
|
|
|
if isErr(err, baseErrs...) {
|
2016-10-05 15:48:07 -04:00
|
|
|
offlineDisks++
|
2017-01-02 13:43:56 -05:00
|
|
|
continue
|
2016-10-05 15:48:07 -04:00
|
|
|
}
|
2016-05-26 17:13:10 -04:00
|
|
|
}
|
2016-10-05 15:48:07 -04:00
|
|
|
onlineDisks++
|
2016-10-17 17:31:33 -04:00
|
|
|
disksInfo[i] = info
|
2016-05-26 17:13:10 -04:00
|
|
|
}
|
|
|
|
|
2016-10-05 15:48:07 -04:00
|
|
|
// Success.
|
|
|
|
return disksInfo, onlineDisks, offlineDisks
|
|
|
|
}
|
|
|
|
|
2016-10-17 17:31:33 -04:00
|
|
|
// returns sorted disksInfo slice which has only valid entries.
|
|
|
|
// i.e the entries where the total size of the disk is not stated
|
|
|
|
// as 0Bytes, this means that the disk is not online or ignored.
|
|
|
|
func sortValidDisksInfo(disksInfo []disk.Info) []disk.Info {
|
|
|
|
var validDisksInfo []disk.Info
|
|
|
|
for _, diskInfo := range disksInfo {
|
|
|
|
if diskInfo.Total == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
validDisksInfo = append(validDisksInfo, diskInfo)
|
|
|
|
}
|
|
|
|
sort.Sort(byDiskTotal(validDisksInfo))
|
|
|
|
return validDisksInfo
|
|
|
|
}
|
|
|
|
|
2016-10-05 15:48:07 -04:00
|
|
|
// Get an aggregated storage info across all disks.
|
|
|
|
func getStorageInfo(disks []StorageAPI) StorageInfo {
|
|
|
|
disksInfo, onlineDisks, offlineDisks := getDisksInfo(disks)
|
2016-10-17 17:31:33 -04:00
|
|
|
|
|
|
|
// Sort so that the first element is the smallest.
|
|
|
|
validDisksInfo := sortValidDisksInfo(disksInfo)
|
|
|
|
if len(validDisksInfo) == 0 {
|
2016-09-22 19:35:12 -04:00
|
|
|
return StorageInfo{
|
|
|
|
Total: -1,
|
|
|
|
Free: -1,
|
|
|
|
}
|
|
|
|
}
|
2016-10-17 17:31:33 -04:00
|
|
|
|
2016-05-26 17:13:10 -04:00
|
|
|
// Return calculated storage info, choose the lowest Total and
|
|
|
|
// Free as the total aggregated values. Total capacity is always
|
|
|
|
// the multiple of smallest disk among the disk list.
|
2016-10-05 15:48:07 -04:00
|
|
|
storageInfo := StorageInfo{
|
2016-10-17 17:31:33 -04:00
|
|
|
Total: validDisksInfo[0].Total * int64(onlineDisks) / 2,
|
|
|
|
Free: validDisksInfo[0].Free * int64(onlineDisks) / 2,
|
2016-05-26 17:13:10 -04:00
|
|
|
}
|
2016-10-17 17:31:33 -04:00
|
|
|
|
2017-02-10 02:26:44 -05:00
|
|
|
storageInfo.Backend.Type = Erasure
|
2016-10-05 15:48:07 -04:00
|
|
|
storageInfo.Backend.OnlineDisks = onlineDisks
|
|
|
|
storageInfo.Backend.OfflineDisks = offlineDisks
|
|
|
|
return storageInfo
|
|
|
|
}
|
|
|
|
|
|
|
|
// StorageInfo - returns underlying storage statistics.
|
|
|
|
func (xl xlObjects) StorageInfo() StorageInfo {
|
2016-10-10 02:03:10 -04:00
|
|
|
storageInfo := getStorageInfo(xl.storageDisks)
|
|
|
|
storageInfo.Backend.ReadQuorum = xl.readQuorum
|
|
|
|
storageInfo.Backend.WriteQuorum = xl.writeQuorum
|
|
|
|
return storageInfo
|
2016-05-26 17:13:10 -04:00
|
|
|
}
|