2016-05-21 05:48:47 +02:00
|
|
|
/*
|
2019-04-09 20:39:42 +02:00
|
|
|
* MinIO Cloud Storage, (C) 2016, 2017, 2018 MinIO, Inc.
|
2016-05-21 05:48:47 +02:00
|
|
|
*
|
|
|
|
* 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-19 01:23:42 +02:00
|
|
|
package cmd
|
2016-05-21 05:48:47 +02:00
|
|
|
|
|
|
|
import (
|
2018-03-14 20:01:47 +01:00
|
|
|
"context"
|
2016-05-26 23:13:10 +02:00
|
|
|
"sort"
|
2019-10-23 06:01:14 +02:00
|
|
|
"strings"
|
2019-12-12 15:02:37 +01:00
|
|
|
"sync"
|
2016-05-26 23:13:10 +02:00
|
|
|
|
2018-04-06 00:04:40 +02:00
|
|
|
"github.com/minio/minio/cmd/logger"
|
2018-02-16 02:45:57 +01:00
|
|
|
"github.com/minio/minio/pkg/bpool"
|
2019-11-13 21:17:45 +01:00
|
|
|
"github.com/minio/minio/pkg/dsync"
|
2019-10-23 06:01:14 +02:00
|
|
|
"github.com/minio/minio/pkg/madmin"
|
|
|
|
xnet "github.com/minio/minio/pkg/net"
|
2019-10-14 18:44:51 +02:00
|
|
|
"github.com/minio/minio/pkg/sync/errgroup"
|
2016-05-21 05:48:47 +02:00
|
|
|
)
|
|
|
|
|
2016-05-31 01:51:59 +02:00
|
|
|
// XL constants.
|
2016-05-21 05:48:47 +02:00
|
|
|
const (
|
2016-05-31 01:51:59 +02:00
|
|
|
// XL metadata file carries per object metadata.
|
|
|
|
xlMetaJSONFile = "xl.json"
|
2016-05-21 05:48:47 +02:00
|
|
|
)
|
|
|
|
|
2018-08-24 08:35:37 +02:00
|
|
|
// OfflineDisk represents an unavailable disk.
|
|
|
|
var OfflineDisk StorageAPI // zero value is nil
|
|
|
|
|
2016-05-31 01:51:59 +02:00
|
|
|
// xlObjects - Implements XL object layer.
|
2016-05-21 05:48:47 +02:00
|
|
|
type xlObjects struct {
|
2018-02-16 02:45:57 +01:00
|
|
|
// getDisks returns list of storageAPIs.
|
|
|
|
getDisks func() []StorageAPI
|
2016-05-21 05:48:47 +02:00
|
|
|
|
2019-11-13 21:17:45 +01:00
|
|
|
// getLockers returns list of remote and local lockers.
|
|
|
|
getLockers func() []dsync.NetLocker
|
|
|
|
|
|
|
|
// Locker mutex map.
|
|
|
|
nsMutex *nsLockMap
|
|
|
|
|
2018-02-16 02:45:57 +01:00
|
|
|
// Byte pools used for temporary i/o buffers.
|
|
|
|
bp *bpool.BytePoolCap
|
2018-02-12 10:46:12 +01:00
|
|
|
|
2018-02-16 02:45:57 +01:00
|
|
|
// TODO: ListObjects pool management, should be removed in future.
|
2019-04-17 18:52:08 +02:00
|
|
|
listPool *TreeWalkPool
|
2017-01-17 02:05:00 +01:00
|
|
|
}
|
|
|
|
|
2019-11-13 21:17:45 +01:00
|
|
|
// NewNSLock - initialize a new namespace RWLocker instance.
|
|
|
|
func (xl xlObjects) NewNSLock(ctx context.Context, bucket string, object string) RWLocker {
|
2019-11-20 02:42:27 +01:00
|
|
|
return xl.nsMutex.NewNSLock(ctx, xl.getLockers, bucket, object)
|
2019-11-13 21:17:45 +01:00
|
|
|
}
|
|
|
|
|
2016-08-15 08:55:48 +02:00
|
|
|
// Shutdown function for object storage interface.
|
2018-03-14 20:01:47 +01:00
|
|
|
func (xl xlObjects) Shutdown(ctx context.Context) error {
|
2016-08-15 08:55:48 +02:00
|
|
|
// Add any object layer shutdown activities here.
|
2018-04-04 06:58:48 +02:00
|
|
|
closeStorageDisks(xl.getDisks())
|
2019-11-13 21:17:45 +01:00
|
|
|
closeLockers(xl.getLockers())
|
2016-08-15 08:55:48 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-05-26 23:13:10 +02:00
|
|
|
// byDiskTotal is a collection satisfying sort.Interface.
|
2018-05-23 12:11:29 +02:00
|
|
|
type byDiskTotal []DiskInfo
|
2016-05-26 23:13:10 +02:00
|
|
|
|
|
|
|
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 21:48:07 +02:00
|
|
|
// getDisksInfo - fetch disks info across all other storage API.
|
2019-10-23 06:01:14 +02:00
|
|
|
func getDisksInfo(disks []StorageAPI) (disksInfo []DiskInfo, onlineDisks, offlineDisks madmin.BackendDisks) {
|
2018-05-23 12:11:29 +02:00
|
|
|
disksInfo = make([]DiskInfo, len(disks))
|
2019-10-14 18:44:51 +02:00
|
|
|
|
|
|
|
g := errgroup.WithNErrs(len(disks))
|
|
|
|
for index := range disks {
|
|
|
|
index := index
|
|
|
|
g.Go(func() error {
|
|
|
|
if disks[index] == nil {
|
|
|
|
// Storage disk is empty, perhaps ignored disk or not available.
|
|
|
|
return errDiskNotFound
|
|
|
|
}
|
|
|
|
info, err := disks[index].DiskInfo()
|
2019-08-23 05:02:40 +02:00
|
|
|
if err != nil {
|
|
|
|
if IsErr(err, baseErrs...) {
|
2019-10-14 18:44:51 +02:00
|
|
|
return err
|
2019-08-23 05:02:40 +02:00
|
|
|
}
|
2019-10-14 18:44:51 +02:00
|
|
|
reqInfo := (&logger.ReqInfo{}).AppendTags("disk", disks[index].String())
|
|
|
|
ctx := logger.SetReqInfo(context.Background(), reqInfo)
|
|
|
|
logger.LogIf(ctx, err)
|
2016-10-05 21:48:07 +02:00
|
|
|
}
|
2019-10-14 18:44:51 +02:00
|
|
|
disksInfo[index] = info
|
|
|
|
return nil
|
|
|
|
}, index)
|
2016-05-26 23:13:10 +02:00
|
|
|
}
|
2019-08-24 02:03:15 +02:00
|
|
|
|
2019-10-23 06:01:14 +02:00
|
|
|
getPeerAddress := func(diskPath string) (string, error) {
|
|
|
|
hostPort := strings.Split(diskPath, SlashSeparator)[0]
|
2019-10-31 17:13:54 +01:00
|
|
|
// Host will be empty for xl/fs disk paths.
|
|
|
|
if hostPort == "" {
|
|
|
|
return "", nil
|
|
|
|
}
|
2019-10-23 06:01:14 +02:00
|
|
|
thisAddr, err := xnet.ParseHost(hostPort)
|
2019-08-24 02:03:15 +02:00
|
|
|
if err != nil {
|
2019-10-23 06:01:14 +02:00
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return thisAddr.String(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
onlineDisks = make(madmin.BackendDisks)
|
|
|
|
offlineDisks = make(madmin.BackendDisks)
|
|
|
|
// Wait for the routines.
|
|
|
|
for i, err := range g.Wait() {
|
|
|
|
peerAddr, pErr := getPeerAddress(disksInfo[i].RelativePath)
|
|
|
|
if pErr != nil {
|
2019-08-30 19:41:02 +02:00
|
|
|
continue
|
2019-08-24 02:03:15 +02:00
|
|
|
}
|
2019-10-23 06:01:14 +02:00
|
|
|
if _, ok := offlineDisks[peerAddr]; !ok {
|
|
|
|
offlineDisks[peerAddr] = 0
|
|
|
|
}
|
|
|
|
if _, ok := onlineDisks[peerAddr]; !ok {
|
|
|
|
onlineDisks[peerAddr] = 0
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
offlineDisks[peerAddr]++
|
2019-10-31 17:13:54 +01:00
|
|
|
continue
|
2019-10-23 06:01:14 +02:00
|
|
|
}
|
|
|
|
onlineDisks[peerAddr]++
|
2019-08-24 02:03:15 +02:00
|
|
|
}
|
|
|
|
|
2016-10-05 21:48:07 +02:00
|
|
|
// Success.
|
|
|
|
return disksInfo, onlineDisks, offlineDisks
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get an aggregated storage info across all disks.
|
|
|
|
func getStorageInfo(disks []StorageAPI) StorageInfo {
|
|
|
|
disksInfo, onlineDisks, offlineDisks := getDisksInfo(disks)
|
2016-10-17 23:31:33 +02:00
|
|
|
|
|
|
|
// Sort so that the first element is the smallest.
|
2020-01-10 11:35:06 +01:00
|
|
|
sort.Sort(byDiskTotal(disksInfo))
|
2016-10-17 23:31:33 +02:00
|
|
|
|
2019-04-05 06:21:50 +02:00
|
|
|
// Combine all disks to get total usage
|
2020-01-10 11:35:06 +01:00
|
|
|
usedList := make([]uint64, len(disksInfo))
|
|
|
|
totalList := make([]uint64, len(disksInfo))
|
|
|
|
availableList := make([]uint64, len(disksInfo))
|
|
|
|
mountPaths := make([]string, len(disksInfo))
|
2019-10-23 06:01:14 +02:00
|
|
|
|
2020-01-10 11:35:06 +01:00
|
|
|
for i, di := range disksInfo {
|
2019-10-23 06:01:14 +02:00
|
|
|
usedList[i] = di.Used
|
|
|
|
totalList[i] = di.Total
|
|
|
|
availableList[i] = di.Free
|
|
|
|
mountPaths[i] = di.RelativePath
|
2018-05-23 12:11:29 +02:00
|
|
|
}
|
|
|
|
|
2019-02-13 13:59:36 +01:00
|
|
|
storageInfo := StorageInfo{
|
2019-10-23 06:01:14 +02:00
|
|
|
Used: usedList,
|
|
|
|
Total: totalList,
|
|
|
|
Available: availableList,
|
|
|
|
MountPaths: mountPaths,
|
2019-02-13 13:59:36 +01:00
|
|
|
}
|
2019-04-05 06:21:50 +02:00
|
|
|
|
2018-08-24 08:35:37 +02:00
|
|
|
storageInfo.Backend.Type = BackendErasure
|
2016-10-05 21:48:07 +02:00
|
|
|
storageInfo.Backend.OnlineDisks = onlineDisks
|
|
|
|
storageInfo.Backend.OfflineDisks = offlineDisks
|
2017-12-22 12:28:13 +01:00
|
|
|
|
2016-10-05 21:48:07 +02:00
|
|
|
return storageInfo
|
|
|
|
}
|
|
|
|
|
|
|
|
// StorageInfo - returns underlying storage statistics.
|
2018-03-14 20:01:47 +01:00
|
|
|
func (xl xlObjects) StorageInfo(ctx context.Context) StorageInfo {
|
2018-02-16 02:45:57 +01:00
|
|
|
return getStorageInfo(xl.getDisks())
|
2016-05-26 23:13:10 +02:00
|
|
|
}
|
2019-12-06 08:16:06 +01:00
|
|
|
|
|
|
|
// GetMetrics - no op
|
|
|
|
func (xl xlObjects) GetMetrics(ctx context.Context) (*Metrics, error) {
|
|
|
|
logger.LogIf(ctx, NotImplemented{})
|
|
|
|
return &Metrics{}, NotImplemented{}
|
|
|
|
}
|
2019-12-12 15:02:37 +01:00
|
|
|
|
|
|
|
func (xl xlObjects) crawlAndGetDataUsage(ctx context.Context, endCh <-chan struct{}) DataUsageInfo {
|
|
|
|
var randomDisks []StorageAPI
|
|
|
|
for _, d := range xl.getLoadBalancedDisks() {
|
|
|
|
if d == nil || !d.IsOnline() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if len(randomDisks) > 3 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
randomDisks = append(randomDisks, d)
|
|
|
|
}
|
|
|
|
|
|
|
|
var dataUsageResults = make([]DataUsageInfo, len(randomDisks))
|
|
|
|
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
for i := 0; i < len(randomDisks); i++ {
|
|
|
|
wg.Add(1)
|
|
|
|
go func(index int, disk StorageAPI) {
|
|
|
|
defer wg.Done()
|
|
|
|
var err error
|
|
|
|
dataUsageResults[index], err = disk.CrawlAndGetDataUsage(endCh)
|
|
|
|
if err != nil {
|
|
|
|
logger.LogIf(ctx, err)
|
|
|
|
}
|
|
|
|
}(i, randomDisks[i])
|
|
|
|
}
|
|
|
|
wg.Wait()
|
|
|
|
|
|
|
|
var dataUsageInfo DataUsageInfo
|
|
|
|
for i := 0; i < len(dataUsageResults); i++ {
|
|
|
|
if dataUsageResults[i].ObjectsCount > dataUsageInfo.ObjectsCount {
|
|
|
|
dataUsageInfo = dataUsageResults[i]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return dataUsageInfo
|
|
|
|
}
|
2019-12-28 17:54:43 +01:00
|
|
|
|
|
|
|
// IsReady - No Op.
|
|
|
|
func (xl xlObjects) IsReady(ctx context.Context) bool {
|
|
|
|
logger.CriticalIf(ctx, NotImplemented{})
|
|
|
|
return true
|
|
|
|
}
|