2021-04-18 15:41:13 -04: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 15:53:09 -04:00
|
|
|
|
|
|
|
package cmd
|
|
|
|
|
2016-09-16 16:06:49 -04:00
|
|
|
import (
|
2020-03-18 19:19:29 -04:00
|
|
|
"context"
|
2019-01-17 07:58:18 -05:00
|
|
|
"io"
|
2016-09-16 16:06:49 -04:00
|
|
|
"sync"
|
2021-05-11 12:19:15 -04:00
|
|
|
"time"
|
2022-04-07 11:10:40 -04:00
|
|
|
|
|
|
|
"github.com/minio/madmin-go"
|
2016-09-16 16:06:49 -04:00
|
|
|
)
|
2016-09-09 18:32:08 -04:00
|
|
|
|
2016-09-09 15:53:09 -04:00
|
|
|
// naughtyDisk wraps a POSIX disk and returns programmed errors
|
|
|
|
// specified by the developer. The purpose is to simulate errors
|
2016-10-05 15:48:07 -04:00
|
|
|
// that are hard to simulate in practice like DiskNotFound.
|
2016-09-09 15:53:09 -04:00
|
|
|
// Programmed errors are stored in errors field.
|
|
|
|
type naughtyDisk struct {
|
|
|
|
// The real disk
|
2018-02-15 20:45:57 -05:00
|
|
|
disk StorageAPI
|
2016-09-09 15:53:09 -04: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 16:06:49 -04:00
|
|
|
// Data protection
|
|
|
|
mu sync.Mutex
|
2016-09-09 15:53:09 -04:00
|
|
|
}
|
|
|
|
|
2018-02-15 20:45:57 -05:00
|
|
|
func newNaughtyDisk(d StorageAPI, errs map[int]error, defaultErr error) *naughtyDisk {
|
2016-09-09 15:53:09 -04:00
|
|
|
return &naughtyDisk{disk: d, errors: errs, defaultErr: defaultErr}
|
|
|
|
}
|
|
|
|
|
2016-10-05 15:48:07 -04:00
|
|
|
func (d *naughtyDisk) String() string {
|
2016-10-19 22:59:48 -04:00
|
|
|
return d.disk.String()
|
2016-10-05 15:48:07 -04:00
|
|
|
}
|
|
|
|
|
2018-02-15 20:45:57 -05:00
|
|
|
func (d *naughtyDisk) IsOnline() bool {
|
|
|
|
if err := d.calcError(); err != nil {
|
|
|
|
return err == errDiskNotFound
|
2016-11-23 18:48:10 -05:00
|
|
|
}
|
2018-02-15 20:45:57 -05:00
|
|
|
return d.disk.IsOnline()
|
2016-11-23 18:48:10 -05:00
|
|
|
}
|
|
|
|
|
2021-05-11 12:19:15 -04:00
|
|
|
func (d *naughtyDisk) LastConn() time.Time {
|
|
|
|
return d.disk.LastConn()
|
|
|
|
}
|
|
|
|
|
2020-05-19 17:27:20 -04:00
|
|
|
func (d *naughtyDisk) IsLocal() bool {
|
|
|
|
return d.disk.IsLocal()
|
|
|
|
}
|
|
|
|
|
2020-09-28 22:39:32 -04:00
|
|
|
func (d *naughtyDisk) Endpoint() Endpoint {
|
|
|
|
return d.disk.Endpoint()
|
|
|
|
}
|
|
|
|
|
2020-05-19 17:27:20 -04:00
|
|
|
func (d *naughtyDisk) Hostname() string {
|
|
|
|
return d.disk.Hostname()
|
2020-01-13 16:09:10 -05:00
|
|
|
}
|
|
|
|
|
2021-03-04 17:36:23 -05:00
|
|
|
func (d *naughtyDisk) Healing() *healingTracker {
|
2020-09-28 22:39:32 -04:00
|
|
|
return d.disk.Healing()
|
|
|
|
}
|
|
|
|
|
2016-11-23 18:48:10 -05:00
|
|
|
func (d *naughtyDisk) Close() (err error) {
|
|
|
|
if err = d.calcError(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return d.disk.Close()
|
|
|
|
}
|
|
|
|
|
2016-09-09 15:53:09 -04:00
|
|
|
func (d *naughtyDisk) calcError() (err error) {
|
2016-09-16 16:06:49 -04:00
|
|
|
d.mu.Lock()
|
|
|
|
defer d.mu.Unlock()
|
2016-09-09 15:53:09 -04: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 17:36:23 -05:00
|
|
|
func (d *naughtyDisk) GetDiskLoc() (poolIdx, setIdx, diskIdx int) {
|
|
|
|
return -1, -1, -1
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *naughtyDisk) SetDiskLoc(poolIdx, setIdx, diskIdx int) {}
|
|
|
|
|
2020-03-27 17:48:30 -04:00
|
|
|
func (d *naughtyDisk) GetDiskID() (string, error) {
|
|
|
|
return d.disk.GetDiskID()
|
|
|
|
}
|
|
|
|
|
2019-10-25 13:37:53 -04:00
|
|
|
func (d *naughtyDisk) SetDiskID(id string) {
|
2020-03-27 17:48:30 -04:00
|
|
|
d.disk.SetDiskID(id)
|
2019-10-25 13:37:53 -04:00
|
|
|
}
|
|
|
|
|
2022-04-07 11:10:40 -04: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 09:02:37 -05:00
|
|
|
}
|
|
|
|
|
2020-09-04 12:45:06 -04:00
|
|
|
func (d *naughtyDisk) DiskInfo(ctx context.Context) (info DiskInfo, err error) {
|
2016-09-09 18:32:08 -04:00
|
|
|
if err := d.calcError(); err != nil {
|
|
|
|
return info, err
|
|
|
|
}
|
2020-09-04 12:45:06 -04:00
|
|
|
return d.disk.DiskInfo(ctx)
|
2016-09-09 18:32:08 -04:00
|
|
|
}
|
|
|
|
|
2020-09-04 12:45:06 -04:00
|
|
|
func (d *naughtyDisk) MakeVolBulk(ctx context.Context, volumes ...string) (err error) {
|
2019-12-23 19:31:03 -05:00
|
|
|
if err := d.calcError(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-09-04 12:45:06 -04:00
|
|
|
return d.disk.MakeVolBulk(ctx, volumes...)
|
2019-12-23 19:31:03 -05:00
|
|
|
}
|
|
|
|
|
2020-09-04 12:45:06 -04:00
|
|
|
func (d *naughtyDisk) MakeVol(ctx context.Context, volume string) (err error) {
|
2016-09-09 15:53:09 -04:00
|
|
|
if err := d.calcError(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-09-04 12:45:06 -04:00
|
|
|
return d.disk.MakeVol(ctx, volume)
|
2016-09-09 15:53:09 -04:00
|
|
|
}
|
|
|
|
|
2020-09-04 12:45:06 -04:00
|
|
|
func (d *naughtyDisk) ListVols(ctx context.Context) (vols []VolInfo, err error) {
|
2016-09-09 15:53:09 -04:00
|
|
|
if err := d.calcError(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-09-04 12:45:06 -04:00
|
|
|
return d.disk.ListVols(ctx)
|
2016-09-09 15:53:09 -04:00
|
|
|
}
|
|
|
|
|
2020-09-04 12:45:06 -04:00
|
|
|
func (d *naughtyDisk) StatVol(ctx context.Context, volume string) (vol VolInfo, err error) {
|
2016-09-09 15:53:09 -04:00
|
|
|
if err := d.calcError(); err != nil {
|
|
|
|
return VolInfo{}, err
|
|
|
|
}
|
2020-09-04 12:45:06 -04:00
|
|
|
return d.disk.StatVol(ctx, volume)
|
2016-09-09 15:53:09 -04:00
|
|
|
}
|
2022-01-02 12:15:06 -05:00
|
|
|
|
2020-09-04 12:45:06 -04:00
|
|
|
func (d *naughtyDisk) DeleteVol(ctx context.Context, volume string, forceDelete bool) (err error) {
|
2016-09-09 15:53:09 -04:00
|
|
|
if err := d.calcError(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-09-04 12:45:06 -04:00
|
|
|
return d.disk.DeleteVol(ctx, volume, forceDelete)
|
2016-09-09 15:53:09 -04:00
|
|
|
}
|
|
|
|
|
2020-10-28 12:18:35 -04: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 12:45:06 -04:00
|
|
|
func (d *naughtyDisk) ListDir(ctx context.Context, volume, dirPath string, count int) (entries []string, err error) {
|
2016-09-09 15:53:09 -04:00
|
|
|
if err := d.calcError(); err != nil {
|
|
|
|
return []string{}, err
|
|
|
|
}
|
2020-09-04 12:45:06 -04:00
|
|
|
return d.disk.ListDir(ctx, volume, dirPath, count)
|
2016-09-09 15:53:09 -04:00
|
|
|
}
|
|
|
|
|
2020-09-04 12:45:06 -04: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 15:53:09 -04:00
|
|
|
if err := d.calcError(); err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
2020-09-04 12:45:06 -04:00
|
|
|
return d.disk.ReadFile(ctx, volume, path, offset, buf, verifier)
|
2017-05-16 17:21:52 -04:00
|
|
|
}
|
|
|
|
|
2020-09-04 12:45:06 -04:00
|
|
|
func (d *naughtyDisk) ReadFileStream(ctx context.Context, volume, path string, offset, length int64) (io.ReadCloser, error) {
|
2019-01-17 07:58:18 -05:00
|
|
|
if err := d.calcError(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-09-04 12:45:06 -04:00
|
|
|
return d.disk.ReadFileStream(ctx, volume, path, offset, length)
|
2019-01-17 07:58:18 -05:00
|
|
|
}
|
|
|
|
|
2020-09-04 12:45:06 -04:00
|
|
|
func (d *naughtyDisk) CreateFile(ctx context.Context, volume, path string, size int64, reader io.Reader) error {
|
2016-10-29 15:44:44 -04:00
|
|
|
if err := d.calcError(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-09-04 12:45:06 -04:00
|
|
|
return d.disk.CreateFile(ctx, volume, path, size, reader)
|
2016-10-29 15:44:44 -04:00
|
|
|
}
|
|
|
|
|
2020-09-04 12:45:06 -04:00
|
|
|
func (d *naughtyDisk) AppendFile(ctx context.Context, volume string, path string, buf []byte) error {
|
2016-09-09 15:53:09 -04:00
|
|
|
if err := d.calcError(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-09-04 12:45:06 -04:00
|
|
|
return d.disk.AppendFile(ctx, volume, path, buf)
|
2016-09-09 15:53:09 -04:00
|
|
|
}
|
|
|
|
|
2022-09-05 19:51:37 -04:00
|
|
|
func (d *naughtyDisk) RenameData(ctx context.Context, srcVolume, srcPath string, fi FileInfo, dstVolume, dstPath string) (uint64, error) {
|
2020-06-12 23:04:01 -04:00
|
|
|
if err := d.calcError(); err != nil {
|
2022-09-05 19:51:37 -04:00
|
|
|
return 0, err
|
2020-06-12 23:04:01 -04:00
|
|
|
}
|
2021-04-20 13:44:39 -04:00
|
|
|
return d.disk.RenameData(ctx, srcVolume, srcPath, fi, dstVolume, dstPath)
|
2020-06-12 23:04:01 -04:00
|
|
|
}
|
|
|
|
|
2020-09-04 12:45:06 -04:00
|
|
|
func (d *naughtyDisk) RenameFile(ctx context.Context, srcVolume, srcPath, dstVolume, dstPath string) error {
|
2016-09-09 15:53:09 -04:00
|
|
|
if err := d.calcError(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-09-04 12:45:06 -04:00
|
|
|
return d.disk.RenameFile(ctx, srcVolume, srcPath, dstVolume, dstPath)
|
2016-09-09 15:53:09 -04:00
|
|
|
}
|
|
|
|
|
2020-09-04 12:45:06 -04:00
|
|
|
func (d *naughtyDisk) CheckParts(ctx context.Context, volume string, path string, fi FileInfo) (err error) {
|
2016-09-09 15:53:09 -04:00
|
|
|
if err := d.calcError(); err != nil {
|
2020-06-12 23:04:01 -04:00
|
|
|
return err
|
|
|
|
}
|
2020-09-04 12:45:06 -04:00
|
|
|
return d.disk.CheckParts(ctx, volume, path, fi)
|
2020-06-12 23:04:01 -04:00
|
|
|
}
|
|
|
|
|
2022-07-11 12:15:54 -04:00
|
|
|
func (d *naughtyDisk) Delete(ctx context.Context, volume string, path string, deleteOpts DeleteOptions) (err error) {
|
2016-09-09 15:53:09 -04:00
|
|
|
if err := d.calcError(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-07-11 12:15:54 -04:00
|
|
|
return d.disk.Delete(ctx, volume, path, deleteOpts)
|
2016-09-09 15:53:09 -04:00
|
|
|
}
|
|
|
|
|
2021-11-01 13:50:07 -04:00
|
|
|
func (d *naughtyDisk) DeleteVersions(ctx context.Context, volume string, versions []FileInfoVersions) []error {
|
2020-06-12 23:04:01 -04:00
|
|
|
if err := d.calcError(); err != nil {
|
|
|
|
errs := make([]error, len(versions))
|
|
|
|
for i := range errs {
|
|
|
|
errs[i] = err
|
|
|
|
}
|
|
|
|
return errs
|
2019-05-13 15:25:49 -04:00
|
|
|
}
|
2020-09-04 12:45:06 -04:00
|
|
|
return d.disk.DeleteVersions(ctx, volume, versions)
|
2019-05-13 15:25:49 -04:00
|
|
|
}
|
|
|
|
|
2020-09-04 12:45:06 -04:00
|
|
|
func (d *naughtyDisk) WriteMetadata(ctx context.Context, volume, path string, fi FileInfo) (err error) {
|
2020-03-11 11:56:36 -04:00
|
|
|
if err := d.calcError(); err != nil {
|
2020-06-12 23:04:01 -04:00
|
|
|
return err
|
|
|
|
}
|
2020-09-04 12:45:06 -04:00
|
|
|
return d.disk.WriteMetadata(ctx, volume, path, fi)
|
2020-06-12 23:04:01 -04:00
|
|
|
}
|
|
|
|
|
2021-04-04 16:32:31 -04:00
|
|
|
func (d *naughtyDisk) UpdateMetadata(ctx context.Context, volume, path string, fi FileInfo) (err error) {
|
|
|
|
if err := d.calcError(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return d.disk.UpdateMetadata(ctx, volume, path, fi)
|
|
|
|
}
|
|
|
|
|
2021-02-03 13:33:43 -05:00
|
|
|
func (d *naughtyDisk) DeleteVersion(ctx context.Context, volume, path string, fi FileInfo, forceDelMarker bool) (err error) {
|
2020-06-12 23:04:01 -04:00
|
|
|
if err := d.calcError(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-02-03 13:33:43 -05:00
|
|
|
return d.disk.DeleteVersion(ctx, volume, path, fi, forceDelMarker)
|
2020-06-12 23:04:01 -04:00
|
|
|
}
|
|
|
|
|
2021-01-07 22:27:31 -05:00
|
|
|
func (d *naughtyDisk) ReadVersion(ctx context.Context, volume, path, versionID string, readData bool) (fi FileInfo, err error) {
|
2020-06-12 23:04:01 -04:00
|
|
|
if err := d.calcError(); err != nil {
|
|
|
|
return FileInfo{}, err
|
2020-03-11 11:56:36 -04:00
|
|
|
}
|
2021-01-07 22:27:31 -05:00
|
|
|
return d.disk.ReadVersion(ctx, volume, path, versionID, readData)
|
2020-03-11 11:56:36 -04:00
|
|
|
}
|
|
|
|
|
2020-11-02 19:14:31 -05:00
|
|
|
func (d *naughtyDisk) WriteAll(ctx context.Context, volume string, path string, b []byte) (err error) {
|
2018-11-14 09:18:35 -05:00
|
|
|
if err := d.calcError(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-11-02 19:14:31 -05:00
|
|
|
return d.disk.WriteAll(ctx, volume, path, b)
|
2018-11-14 09:18:35 -05:00
|
|
|
}
|
|
|
|
|
2020-09-04 12:45:06 -04:00
|
|
|
func (d *naughtyDisk) ReadAll(ctx context.Context, volume string, path string) (buf []byte, err error) {
|
2016-09-09 15:53:09 -04:00
|
|
|
if err := d.calcError(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-09-04 12:45:06 -04:00
|
|
|
return d.disk.ReadAll(ctx, volume, path)
|
2016-09-09 15:53:09 -04:00
|
|
|
}
|
2019-07-08 16:51:18 -04:00
|
|
|
|
2022-04-20 15:49:05 -04: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 12:45:06 -04:00
|
|
|
func (d *naughtyDisk) VerifyFile(ctx context.Context, volume, path string, fi FileInfo) error {
|
2019-07-08 16:51:18 -04:00
|
|
|
if err := d.calcError(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-09-04 12:45:06 -04:00
|
|
|
return d.disk.VerifyFile(ctx, volume, path, fi)
|
2019-07-08 16:51:18 -04:00
|
|
|
}
|
2021-07-09 14:29:16 -04:00
|
|
|
|
2021-10-01 14:50:00 -04:00
|
|
|
func (d *naughtyDisk) StatInfoFile(ctx context.Context, volume, path string, glob bool) (stat []StatInfo, err error) {
|
2021-07-09 14:29:16 -04:00
|
|
|
if err := d.calcError(); err != nil {
|
|
|
|
return stat, err
|
|
|
|
}
|
2021-10-01 14:50:00 -04:00
|
|
|
return d.disk.StatInfoFile(ctx, volume, path, glob)
|
2021-07-09 14:29:16 -04:00
|
|
|
}
|
2022-07-19 11:35:29 -04: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 13:20:55 -05: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)
|
|
|
|
}
|