2021-04-18 12:41:13 -07:00
|
|
|
// Copyright (c) 2015-2021 MinIO, Inc.
|
|
|
|
//
|
|
|
|
// This file is part of MinIO Object Storage stack
|
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2016-09-09 20:53:09 +01:00
|
|
|
|
|
|
|
package cmd
|
|
|
|
|
2016-09-16 21:06:49 +01:00
|
|
|
import (
|
2020-03-19 00:19:29 +01:00
|
|
|
"context"
|
2019-01-17 04:58:18 -08:00
|
|
|
"io"
|
2016-09-16 21:06:49 +01:00
|
|
|
"sync"
|
2021-05-11 17:19:15 +01:00
|
|
|
"time"
|
2022-04-07 16:10:40 +01:00
|
|
|
|
2023-06-19 17:53:08 -07:00
|
|
|
"github.com/minio/madmin-go/v3"
|
2016-09-16 21:06:49 +01:00
|
|
|
)
|
2016-09-09 15:32:08 -07:00
|
|
|
|
2016-09-09 20:53:09 +01:00
|
|
|
// naughtyDisk wraps a POSIX disk and returns programmed errors
|
|
|
|
// specified by the developer. The purpose is to simulate errors
|
2016-10-05 12:48:07 -07:00
|
|
|
// that are hard to simulate in practice like DiskNotFound.
|
2016-09-09 20:53:09 +01:00
|
|
|
// Programmed errors are stored in errors field.
|
|
|
|
type naughtyDisk struct {
|
|
|
|
// The real disk
|
2018-02-15 17:45:57 -08:00
|
|
|
disk StorageAPI
|
2016-09-09 20:53:09 +01:00
|
|
|
// Programmed errors: API call number => error to return
|
|
|
|
errors map[int]error
|
|
|
|
// The error to return when no error value is programmed
|
|
|
|
defaultErr error
|
|
|
|
// The current API call number
|
|
|
|
callNR int
|
2016-09-16 21:06:49 +01:00
|
|
|
// Data protection
|
|
|
|
mu sync.Mutex
|
2016-09-09 20:53:09 +01:00
|
|
|
}
|
|
|
|
|
2018-02-15 17:45:57 -08:00
|
|
|
func newNaughtyDisk(d StorageAPI, errs map[int]error, defaultErr error) *naughtyDisk {
|
2016-09-09 20:53:09 +01:00
|
|
|
return &naughtyDisk{disk: d, errors: errs, defaultErr: defaultErr}
|
|
|
|
}
|
|
|
|
|
2016-10-05 12:48:07 -07:00
|
|
|
func (d *naughtyDisk) String() string {
|
2016-10-20 08:29:48 +05:30
|
|
|
return d.disk.String()
|
2016-10-05 12:48:07 -07:00
|
|
|
}
|
|
|
|
|
2018-02-15 17:45:57 -08:00
|
|
|
func (d *naughtyDisk) IsOnline() bool {
|
|
|
|
if err := d.calcError(); err != nil {
|
|
|
|
return err == errDiskNotFound
|
2016-11-23 15:48:10 -08:00
|
|
|
}
|
2018-02-15 17:45:57 -08:00
|
|
|
return d.disk.IsOnline()
|
2016-11-23 15:48:10 -08:00
|
|
|
}
|
|
|
|
|
2021-05-11 17:19:15 +01:00
|
|
|
func (d *naughtyDisk) LastConn() time.Time {
|
|
|
|
return d.disk.LastConn()
|
|
|
|
}
|
|
|
|
|
2020-05-19 22:27:20 +01:00
|
|
|
func (d *naughtyDisk) IsLocal() bool {
|
|
|
|
return d.disk.IsLocal()
|
|
|
|
}
|
|
|
|
|
2020-09-28 19:39:32 -07:00
|
|
|
func (d *naughtyDisk) Endpoint() Endpoint {
|
|
|
|
return d.disk.Endpoint()
|
|
|
|
}
|
|
|
|
|
2020-05-19 22:27:20 +01:00
|
|
|
func (d *naughtyDisk) Hostname() string {
|
|
|
|
return d.disk.Hostname()
|
2020-01-13 22:09:10 +01:00
|
|
|
}
|
|
|
|
|
2021-03-04 14:36:23 -08:00
|
|
|
func (d *naughtyDisk) Healing() *healingTracker {
|
2020-09-28 19:39:32 -07:00
|
|
|
return d.disk.Healing()
|
|
|
|
}
|
|
|
|
|
2016-11-23 15:48:10 -08:00
|
|
|
func (d *naughtyDisk) Close() (err error) {
|
|
|
|
if err = d.calcError(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return d.disk.Close()
|
|
|
|
}
|
|
|
|
|
2016-09-09 20:53:09 +01:00
|
|
|
func (d *naughtyDisk) calcError() (err error) {
|
2016-09-16 21:06:49 +01:00
|
|
|
d.mu.Lock()
|
|
|
|
defer d.mu.Unlock()
|
2016-09-09 20:53:09 +01:00
|
|
|
d.callNR++
|
|
|
|
if err, ok := d.errors[d.callNR]; ok {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if d.defaultErr != nil {
|
|
|
|
return d.defaultErr
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-03-04 14:36:23 -08:00
|
|
|
func (d *naughtyDisk) GetDiskLoc() (poolIdx, setIdx, diskIdx int) {
|
|
|
|
return -1, -1, -1
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *naughtyDisk) SetDiskLoc(poolIdx, setIdx, diskIdx int) {}
|
|
|
|
|
2020-03-27 14:48:30 -07:00
|
|
|
func (d *naughtyDisk) GetDiskID() (string, error) {
|
|
|
|
return d.disk.GetDiskID()
|
|
|
|
}
|
|
|
|
|
2019-10-25 10:37:53 -07:00
|
|
|
func (d *naughtyDisk) SetDiskID(id string) {
|
2020-03-27 14:48:30 -07:00
|
|
|
d.disk.SetDiskID(id)
|
2019-10-25 10:37:53 -07:00
|
|
|
}
|
|
|
|
|
2022-04-07 16:10:40 +01:00
|
|
|
func (d *naughtyDisk) NSScanner(ctx context.Context, cache dataUsageCache, updates chan<- dataUsageEntry, scanMode madmin.HealScanMode) (info dataUsageCache, err error) {
|
|
|
|
return d.disk.NSScanner(ctx, cache, updates, scanMode)
|
2019-12-12 15:02:37 +01:00
|
|
|
}
|
|
|
|
|
2023-07-31 15:20:48 -07:00
|
|
|
func (d *naughtyDisk) DiskInfo(ctx context.Context, metrics bool) (info DiskInfo, err error) {
|
2016-09-09 15:32:08 -07:00
|
|
|
if err := d.calcError(); err != nil {
|
|
|
|
return info, err
|
|
|
|
}
|
2023-07-31 15:20:48 -07:00
|
|
|
return d.disk.DiskInfo(ctx, metrics)
|
2016-09-09 15:32:08 -07:00
|
|
|
}
|
|
|
|
|
2020-09-04 09:45:06 -07:00
|
|
|
func (d *naughtyDisk) MakeVolBulk(ctx context.Context, volumes ...string) (err error) {
|
2019-12-23 16:31:03 -08:00
|
|
|
if err := d.calcError(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-09-04 09:45:06 -07:00
|
|
|
return d.disk.MakeVolBulk(ctx, volumes...)
|
2019-12-23 16:31:03 -08:00
|
|
|
}
|
|
|
|
|
2020-09-04 09:45:06 -07:00
|
|
|
func (d *naughtyDisk) MakeVol(ctx context.Context, volume string) (err error) {
|
2016-09-09 20:53:09 +01:00
|
|
|
if err := d.calcError(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-09-04 09:45:06 -07:00
|
|
|
return d.disk.MakeVol(ctx, volume)
|
2016-09-09 20:53:09 +01:00
|
|
|
}
|
|
|
|
|
2020-09-04 09:45:06 -07:00
|
|
|
func (d *naughtyDisk) ListVols(ctx context.Context) (vols []VolInfo, err error) {
|
2016-09-09 20:53:09 +01:00
|
|
|
if err := d.calcError(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-09-04 09:45:06 -07:00
|
|
|
return d.disk.ListVols(ctx)
|
2016-09-09 20:53:09 +01:00
|
|
|
}
|
|
|
|
|
2020-09-04 09:45:06 -07:00
|
|
|
func (d *naughtyDisk) StatVol(ctx context.Context, volume string) (vol VolInfo, err error) {
|
2016-09-09 20:53:09 +01:00
|
|
|
if err := d.calcError(); err != nil {
|
|
|
|
return VolInfo{}, err
|
|
|
|
}
|
2020-09-04 09:45:06 -07:00
|
|
|
return d.disk.StatVol(ctx, volume)
|
2016-09-09 20:53:09 +01:00
|
|
|
}
|
2022-01-02 09:15:06 -08:00
|
|
|
|
2020-09-04 09:45:06 -07:00
|
|
|
func (d *naughtyDisk) DeleteVol(ctx context.Context, volume string, forceDelete bool) (err error) {
|
2016-09-09 20:53:09 +01:00
|
|
|
if err := d.calcError(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-09-04 09:45:06 -07:00
|
|
|
return d.disk.DeleteVol(ctx, volume, forceDelete)
|
2016-09-09 20:53:09 +01:00
|
|
|
}
|
|
|
|
|
2020-10-28 09:18:35 -07:00
|
|
|
func (d *naughtyDisk) WalkDir(ctx context.Context, opts WalkDirOptions, wr io.Writer) error {
|
|
|
|
if err := d.calcError(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return d.disk.WalkDir(ctx, opts, wr)
|
|
|
|
}
|
|
|
|
|
2020-09-04 09:45:06 -07:00
|
|
|
func (d *naughtyDisk) ListDir(ctx context.Context, volume, dirPath string, count int) (entries []string, err error) {
|
2016-09-09 20:53:09 +01:00
|
|
|
if err := d.calcError(); err != nil {
|
|
|
|
return []string{}, err
|
|
|
|
}
|
2020-09-04 09:45:06 -07:00
|
|
|
return d.disk.ListDir(ctx, volume, dirPath, count)
|
2016-09-09 20:53:09 +01:00
|
|
|
}
|
|
|
|
|
2020-09-04 09:45:06 -07:00
|
|
|
func (d *naughtyDisk) ReadFile(ctx context.Context, volume string, path string, offset int64, buf []byte, verifier *BitrotVerifier) (n int64, err error) {
|
2016-09-09 20:53:09 +01:00
|
|
|
if err := d.calcError(); err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
2020-09-04 09:45:06 -07:00
|
|
|
return d.disk.ReadFile(ctx, volume, path, offset, buf, verifier)
|
2017-05-16 14:21:52 -07:00
|
|
|
}
|
|
|
|
|
2020-09-04 09:45:06 -07:00
|
|
|
func (d *naughtyDisk) ReadFileStream(ctx context.Context, volume, path string, offset, length int64) (io.ReadCloser, error) {
|
2019-01-17 04:58:18 -08:00
|
|
|
if err := d.calcError(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-09-04 09:45:06 -07:00
|
|
|
return d.disk.ReadFileStream(ctx, volume, path, offset, length)
|
2019-01-17 04:58:18 -08:00
|
|
|
}
|
|
|
|
|
2020-09-04 09:45:06 -07:00
|
|
|
func (d *naughtyDisk) CreateFile(ctx context.Context, volume, path string, size int64, reader io.Reader) error {
|
2016-10-29 20:44:44 +01:00
|
|
|
if err := d.calcError(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-09-04 09:45:06 -07:00
|
|
|
return d.disk.CreateFile(ctx, volume, path, size, reader)
|
2016-10-29 20:44:44 +01:00
|
|
|
}
|
|
|
|
|
2020-09-04 09:45:06 -07:00
|
|
|
func (d *naughtyDisk) AppendFile(ctx context.Context, volume string, path string, buf []byte) error {
|
2016-09-09 20:53:09 +01:00
|
|
|
if err := d.calcError(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-09-04 09:45:06 -07:00
|
|
|
return d.disk.AppendFile(ctx, volume, path, buf)
|
2016-09-09 20:53:09 +01:00
|
|
|
}
|
|
|
|
|
2022-09-05 16:51:37 -07:00
|
|
|
func (d *naughtyDisk) RenameData(ctx context.Context, srcVolume, srcPath string, fi FileInfo, dstVolume, dstPath string) (uint64, error) {
|
2020-06-12 20:04:01 -07:00
|
|
|
if err := d.calcError(); err != nil {
|
2022-09-05 16:51:37 -07:00
|
|
|
return 0, err
|
2020-06-12 20:04:01 -07:00
|
|
|
}
|
2021-04-20 10:44:39 -07:00
|
|
|
return d.disk.RenameData(ctx, srcVolume, srcPath, fi, dstVolume, dstPath)
|
2020-06-12 20:04:01 -07:00
|
|
|
}
|
|
|
|
|
2020-09-04 09:45:06 -07:00
|
|
|
func (d *naughtyDisk) RenameFile(ctx context.Context, srcVolume, srcPath, dstVolume, dstPath string) error {
|
2016-09-09 20:53:09 +01:00
|
|
|
if err := d.calcError(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-09-04 09:45:06 -07:00
|
|
|
return d.disk.RenameFile(ctx, srcVolume, srcPath, dstVolume, dstPath)
|
2016-09-09 20:53:09 +01:00
|
|
|
}
|
|
|
|
|
2020-09-04 09:45:06 -07:00
|
|
|
func (d *naughtyDisk) CheckParts(ctx context.Context, volume string, path string, fi FileInfo) (err error) {
|
2016-09-09 20:53:09 +01:00
|
|
|
if err := d.calcError(); err != nil {
|
2020-06-12 20:04:01 -07:00
|
|
|
return err
|
|
|
|
}
|
2020-09-04 09:45:06 -07:00
|
|
|
return d.disk.CheckParts(ctx, volume, path, fi)
|
2020-06-12 20:04:01 -07:00
|
|
|
}
|
|
|
|
|
2022-07-11 21:45:54 +05:30
|
|
|
func (d *naughtyDisk) Delete(ctx context.Context, volume string, path string, deleteOpts DeleteOptions) (err error) {
|
2016-09-09 20:53:09 +01:00
|
|
|
if err := d.calcError(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-07-11 21:45:54 +05:30
|
|
|
return d.disk.Delete(ctx, volume, path, deleteOpts)
|
2016-09-09 20:53:09 +01:00
|
|
|
}
|
|
|
|
|
2021-11-01 10:50:07 -07:00
|
|
|
func (d *naughtyDisk) DeleteVersions(ctx context.Context, volume string, versions []FileInfoVersions) []error {
|
2020-06-12 20:04:01 -07:00
|
|
|
if err := d.calcError(); err != nil {
|
|
|
|
errs := make([]error, len(versions))
|
|
|
|
for i := range errs {
|
|
|
|
errs[i] = err
|
|
|
|
}
|
|
|
|
return errs
|
2019-05-13 20:25:49 +01:00
|
|
|
}
|
2020-09-04 09:45:06 -07:00
|
|
|
return d.disk.DeleteVersions(ctx, volume, versions)
|
2019-05-13 20:25:49 +01:00
|
|
|
}
|
|
|
|
|
2020-09-04 09:45:06 -07:00
|
|
|
func (d *naughtyDisk) WriteMetadata(ctx context.Context, volume, path string, fi FileInfo) (err error) {
|
2020-03-11 08:56:36 -07:00
|
|
|
if err := d.calcError(); err != nil {
|
2020-06-12 20:04:01 -07:00
|
|
|
return err
|
|
|
|
}
|
2020-09-04 09:45:06 -07:00
|
|
|
return d.disk.WriteMetadata(ctx, volume, path, fi)
|
2020-06-12 20:04:01 -07:00
|
|
|
}
|
|
|
|
|
2023-08-25 07:58:11 -07:00
|
|
|
func (d *naughtyDisk) UpdateMetadata(ctx context.Context, volume, path string, fi FileInfo, opts UpdateMetadataOpts) (err error) {
|
2021-04-04 13:32:31 -07:00
|
|
|
if err := d.calcError(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-08-25 07:58:11 -07:00
|
|
|
return d.disk.UpdateMetadata(ctx, volume, path, fi, opts)
|
2021-04-04 13:32:31 -07:00
|
|
|
}
|
|
|
|
|
2021-02-03 19:33:43 +01:00
|
|
|
func (d *naughtyDisk) DeleteVersion(ctx context.Context, volume, path string, fi FileInfo, forceDelMarker bool) (err error) {
|
2020-06-12 20:04:01 -07:00
|
|
|
if err := d.calcError(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-02-03 19:33:43 +01:00
|
|
|
return d.disk.DeleteVersion(ctx, volume, path, fi, forceDelMarker)
|
2020-06-12 20:04:01 -07:00
|
|
|
}
|
|
|
|
|
2021-01-07 19:27:31 -08:00
|
|
|
func (d *naughtyDisk) ReadVersion(ctx context.Context, volume, path, versionID string, readData bool) (fi FileInfo, err error) {
|
2020-06-12 20:04:01 -07:00
|
|
|
if err := d.calcError(); err != nil {
|
|
|
|
return FileInfo{}, err
|
2020-03-11 08:56:36 -07:00
|
|
|
}
|
2021-01-07 19:27:31 -08:00
|
|
|
return d.disk.ReadVersion(ctx, volume, path, versionID, readData)
|
2020-03-11 08:56:36 -07:00
|
|
|
}
|
|
|
|
|
2020-11-02 16:14:31 -08:00
|
|
|
func (d *naughtyDisk) WriteAll(ctx context.Context, volume string, path string, b []byte) (err error) {
|
2018-11-14 06:18:35 -08:00
|
|
|
if err := d.calcError(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-11-02 16:14:31 -08:00
|
|
|
return d.disk.WriteAll(ctx, volume, path, b)
|
2018-11-14 06:18:35 -08:00
|
|
|
}
|
|
|
|
|
2020-09-04 09:45:06 -07:00
|
|
|
func (d *naughtyDisk) ReadAll(ctx context.Context, volume string, path string) (buf []byte, err error) {
|
2016-09-09 20:53:09 +01:00
|
|
|
if err := d.calcError(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-09-04 09:45:06 -07:00
|
|
|
return d.disk.ReadAll(ctx, volume, path)
|
2016-09-09 20:53:09 +01:00
|
|
|
}
|
2019-07-08 13:51:18 -07:00
|
|
|
|
2022-04-20 12:49:05 -07:00
|
|
|
func (d *naughtyDisk) ReadXL(ctx context.Context, volume string, path string, readData bool) (rf RawFileInfo, err error) {
|
|
|
|
if err := d.calcError(); err != nil {
|
|
|
|
return rf, err
|
|
|
|
}
|
|
|
|
return d.disk.ReadXL(ctx, volume, path, readData)
|
|
|
|
}
|
|
|
|
|
2020-09-04 09:45:06 -07:00
|
|
|
func (d *naughtyDisk) VerifyFile(ctx context.Context, volume, path string, fi FileInfo) error {
|
2019-07-08 13:51:18 -07:00
|
|
|
if err := d.calcError(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-09-04 09:45:06 -07:00
|
|
|
return d.disk.VerifyFile(ctx, volume, path, fi)
|
2019-07-08 13:51:18 -07:00
|
|
|
}
|
2021-07-09 11:29:16 -07:00
|
|
|
|
2021-10-01 11:50:00 -07:00
|
|
|
func (d *naughtyDisk) StatInfoFile(ctx context.Context, volume, path string, glob bool) (stat []StatInfo, err error) {
|
2021-07-09 11:29:16 -07:00
|
|
|
if err := d.calcError(); err != nil {
|
|
|
|
return stat, err
|
|
|
|
}
|
2021-10-01 11:50:00 -07:00
|
|
|
return d.disk.StatInfoFile(ctx, volume, path, glob)
|
2021-07-09 11:29:16 -07:00
|
|
|
}
|
2022-07-19 08:35:29 -07:00
|
|
|
|
|
|
|
func (d *naughtyDisk) ReadMultiple(ctx context.Context, req ReadMultipleReq, resp chan<- ReadMultipleResp) error {
|
|
|
|
if err := d.calcError(); err != nil {
|
|
|
|
close(resp)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return d.disk.ReadMultiple(ctx, req, resp)
|
|
|
|
}
|
2022-11-28 19:20:55 +01:00
|
|
|
|
|
|
|
func (d *naughtyDisk) CleanAbandonedData(ctx context.Context, volume string, path string) error {
|
|
|
|
if err := d.calcError(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return d.disk.CleanAbandonedData(ctx, volume, path)
|
|
|
|
}
|