2017-01-17 13:02:58 -05:00
|
|
|
/*
|
2020-07-13 12:51:07 -04:00
|
|
|
* MinIO Cloud Storage, (C) 2017-2020 MinIO, Inc.
|
2017-01-17 13:02:58 -05: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.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
package madmin
|
|
|
|
|
|
|
|
import (
|
2020-03-20 18:00:44 -04:00
|
|
|
"context"
|
2017-03-31 20:55:15 -04:00
|
|
|
"encoding/json"
|
2017-01-17 13:02:58 -05:00
|
|
|
"fmt"
|
2017-03-31 20:55:15 -04:00
|
|
|
"io/ioutil"
|
2017-01-17 13:02:58 -05:00
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2019-03-14 16:08:51 -04:00
|
|
|
// HealScanMode represents the type of healing scan
|
|
|
|
type HealScanMode int
|
|
|
|
|
|
|
|
const (
|
2021-01-27 13:21:14 -05:00
|
|
|
// HealUnknownScan default is unknown
|
|
|
|
HealUnknownScan HealScanMode = iota
|
|
|
|
|
2019-03-14 16:08:51 -04:00
|
|
|
// HealNormalScan checks if parts are present and not outdated
|
2021-01-27 13:21:14 -05:00
|
|
|
HealNormalScan
|
|
|
|
|
2019-03-14 16:08:51 -04:00
|
|
|
// HealDeepScan checks for parts bitrot checksums
|
|
|
|
HealDeepScan
|
|
|
|
)
|
|
|
|
|
2018-01-22 17:54:55 -05:00
|
|
|
// HealOpts - collection of options for a heal sequence
|
|
|
|
type HealOpts struct {
|
2019-03-14 16:08:51 -04:00
|
|
|
Recursive bool `json:"recursive"`
|
|
|
|
DryRun bool `json:"dryRun"`
|
|
|
|
Remove bool `json:"remove"`
|
2021-01-05 16:24:22 -05:00
|
|
|
Recreate bool `json:"recreate"` // only used when bucket needs to be healed
|
2019-03-14 16:08:51 -04:00
|
|
|
ScanMode HealScanMode `json:"scanMode"`
|
2017-01-19 12:34:18 -05:00
|
|
|
}
|
|
|
|
|
2020-04-30 23:23:00 -04:00
|
|
|
// Equal returns true if no is same as o.
|
|
|
|
func (o HealOpts) Equal(no HealOpts) bool {
|
|
|
|
if o.Recursive != no.Recursive {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if o.DryRun != no.DryRun {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if o.Remove != no.Remove {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return o.ScanMode == no.ScanMode
|
|
|
|
}
|
|
|
|
|
2018-01-22 17:54:55 -05:00
|
|
|
// HealStartSuccess - holds information about a successfully started
|
|
|
|
// heal operation
|
|
|
|
type HealStartSuccess struct {
|
|
|
|
ClientToken string `json:"clientToken"`
|
|
|
|
ClientAddress string `json:"clientAddress"`
|
|
|
|
StartTime time.Time `json:"startTime"`
|
2017-01-19 12:34:18 -05:00
|
|
|
}
|
|
|
|
|
2018-11-04 22:24:16 -05:00
|
|
|
// HealStopSuccess - holds information about a successfully stopped
|
|
|
|
// heal operation.
|
|
|
|
type HealStopSuccess HealStartSuccess
|
|
|
|
|
2018-01-22 17:54:55 -05:00
|
|
|
// HealTaskStatus - status struct for a heal task
|
|
|
|
type HealTaskStatus struct {
|
|
|
|
Summary string `json:"summary"`
|
|
|
|
FailureDetail string `json:"detail"`
|
|
|
|
StartTime time.Time `json:"startTime"`
|
|
|
|
HealSettings HealOpts `json:"settings"`
|
2017-01-19 12:34:18 -05:00
|
|
|
|
2018-01-22 17:54:55 -05:00
|
|
|
Items []HealResultItem `json:"items,omitempty"`
|
2017-01-19 12:34:18 -05:00
|
|
|
}
|
|
|
|
|
2018-01-22 17:54:55 -05:00
|
|
|
// HealItemType - specify the type of heal operation in a healing
|
|
|
|
// result
|
|
|
|
type HealItemType string
|
2017-01-17 13:02:58 -05:00
|
|
|
|
2018-01-22 17:54:55 -05:00
|
|
|
// HealItemType constants
|
2017-01-17 13:02:58 -05:00
|
|
|
const (
|
2018-01-22 17:54:55 -05:00
|
|
|
HealItemMetadata HealItemType = "metadata"
|
|
|
|
HealItemBucket = "bucket"
|
|
|
|
HealItemBucketMetadata = "bucket-metadata"
|
|
|
|
HealItemObject = "object"
|
2017-01-17 13:02:58 -05:00
|
|
|
)
|
|
|
|
|
2018-01-22 17:54:55 -05:00
|
|
|
// Drive state constants
|
2017-01-17 13:02:58 -05:00
|
|
|
const (
|
2020-03-27 17:48:30 -04:00
|
|
|
DriveStateOk string = "ok"
|
|
|
|
DriveStateOffline = "offline"
|
|
|
|
DriveStateCorrupt = "corrupt"
|
|
|
|
DriveStateMissing = "missing"
|
2020-07-13 12:51:07 -04:00
|
|
|
DriveStatePermission = "permission-denied"
|
|
|
|
DriveStateFaulty = "faulty"
|
|
|
|
DriveStateUnknown = "unknown"
|
2020-03-27 17:48:30 -04:00
|
|
|
DriveStateUnformatted = "unformatted" // only returned by disk
|
2017-01-17 13:02:58 -05:00
|
|
|
)
|
|
|
|
|
2018-02-15 20:45:57 -05:00
|
|
|
// HealDriveInfo - struct for an individual drive info item.
|
|
|
|
type HealDriveInfo struct {
|
|
|
|
UUID string `json:"uuid"`
|
|
|
|
Endpoint string `json:"endpoint"`
|
|
|
|
State string `json:"state"`
|
|
|
|
}
|
|
|
|
|
2018-01-22 17:54:55 -05:00
|
|
|
// HealResultItem - struct for an individual heal result item
|
|
|
|
type HealResultItem struct {
|
|
|
|
ResultIndex int64 `json:"resultId"`
|
|
|
|
Type HealItemType `json:"type"`
|
|
|
|
Bucket string `json:"bucket"`
|
|
|
|
Object string `json:"object"`
|
2020-11-23 12:12:17 -05:00
|
|
|
VersionID string `json:"versionId"`
|
2018-01-22 17:54:55 -05:00
|
|
|
Detail string `json:"detail"`
|
|
|
|
ParityBlocks int `json:"parityBlocks,omitempty"`
|
|
|
|
DataBlocks int `json:"dataBlocks,omitempty"`
|
|
|
|
DiskCount int `json:"diskCount"`
|
2018-02-15 20:45:57 -05:00
|
|
|
SetCount int `json:"setCount"`
|
|
|
|
// below slices are from drive info.
|
|
|
|
Before struct {
|
|
|
|
Drives []HealDriveInfo `json:"drives"`
|
|
|
|
} `json:"before"`
|
|
|
|
After struct {
|
|
|
|
Drives []HealDriveInfo `json:"drives"`
|
|
|
|
} `json:"after"`
|
2018-01-22 17:54:55 -05:00
|
|
|
ObjectSize int64 `json:"objectSize"`
|
|
|
|
}
|
|
|
|
|
2018-02-15 20:45:57 -05:00
|
|
|
// GetMissingCounts - returns the number of missing disks before
|
|
|
|
// and after heal
|
|
|
|
func (hri *HealResultItem) GetMissingCounts() (b, a int) {
|
|
|
|
if hri == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for _, v := range hri.Before.Drives {
|
|
|
|
if v.State == DriveStateMissing {
|
|
|
|
b++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, v := range hri.After.Drives {
|
|
|
|
if v.State == DriveStateMissing {
|
|
|
|
a++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetOfflineCounts - returns the number of offline disks before
|
|
|
|
// and after heal
|
|
|
|
func (hri *HealResultItem) GetOfflineCounts() (b, a int) {
|
|
|
|
if hri == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for _, v := range hri.Before.Drives {
|
|
|
|
if v.State == DriveStateOffline {
|
|
|
|
b++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, v := range hri.After.Drives {
|
|
|
|
if v.State == DriveStateOffline {
|
|
|
|
a++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetCorruptedCounts - returns the number of corrupted disks before
|
|
|
|
// and after heal
|
|
|
|
func (hri *HealResultItem) GetCorruptedCounts() (b, a int) {
|
|
|
|
if hri == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for _, v := range hri.Before.Drives {
|
|
|
|
if v.State == DriveStateCorrupt {
|
|
|
|
b++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, v := range hri.After.Drives {
|
|
|
|
if v.State == DriveStateCorrupt {
|
|
|
|
a++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
2018-01-22 17:54:55 -05:00
|
|
|
}
|
|
|
|
|
2018-02-15 20:45:57 -05:00
|
|
|
// GetOnlineCounts - returns the number of online disks before
|
|
|
|
// and after heal
|
2018-01-22 17:54:55 -05:00
|
|
|
func (hri *HealResultItem) GetOnlineCounts() (b, a int) {
|
|
|
|
if hri == nil {
|
|
|
|
return
|
|
|
|
}
|
2018-02-15 20:45:57 -05:00
|
|
|
for _, v := range hri.Before.Drives {
|
|
|
|
if v.State == DriveStateOk {
|
2018-01-22 17:54:55 -05:00
|
|
|
b++
|
2017-01-17 13:02:58 -05:00
|
|
|
}
|
2017-01-19 12:34:18 -05:00
|
|
|
}
|
2018-02-15 20:45:57 -05:00
|
|
|
for _, v := range hri.After.Drives {
|
|
|
|
if v.State == DriveStateOk {
|
2018-01-22 17:54:55 -05:00
|
|
|
a++
|
2017-01-19 12:34:18 -05:00
|
|
|
}
|
|
|
|
}
|
2018-01-22 17:54:55 -05:00
|
|
|
return
|
2017-01-19 12:34:18 -05:00
|
|
|
}
|
|
|
|
|
2018-01-22 17:54:55 -05:00
|
|
|
// Heal - API endpoint to start heal and to fetch status
|
2018-11-04 22:24:16 -05:00
|
|
|
// forceStart and forceStop are mutually exclusive, you can either
|
|
|
|
// set one of them to 'true'. If both are set 'forceStart' will be
|
|
|
|
// honored.
|
2020-03-20 18:00:44 -04:00
|
|
|
func (adm *AdminClient) Heal(ctx context.Context, bucket, prefix string,
|
|
|
|
healOpts HealOpts, clientToken string, forceStart, forceStop bool) (
|
2018-01-22 17:54:55 -05:00
|
|
|
healStart HealStartSuccess, healTaskStatus HealTaskStatus, err error) {
|
2017-01-17 13:02:58 -05:00
|
|
|
|
2018-11-04 22:24:16 -05:00
|
|
|
if forceStart && forceStop {
|
|
|
|
return healStart, healTaskStatus, ErrInvalidArgument("forceStart and forceStop set to true is not allowed")
|
|
|
|
}
|
|
|
|
|
2018-01-22 17:54:55 -05:00
|
|
|
body, err := json.Marshal(healOpts)
|
2017-01-17 13:02:58 -05:00
|
|
|
if err != nil {
|
2018-01-22 17:54:55 -05:00
|
|
|
return healStart, healTaskStatus, err
|
2017-01-17 13:02:58 -05:00
|
|
|
}
|
|
|
|
|
2019-10-23 00:01:14 -04:00
|
|
|
path := fmt.Sprintf(adminAPIPrefix+"/heal/%s", bucket)
|
2018-01-22 17:54:55 -05:00
|
|
|
if bucket != "" && prefix != "" {
|
|
|
|
path += "/" + prefix
|
2017-01-17 13:02:58 -05:00
|
|
|
}
|
|
|
|
|
2018-01-22 17:54:55 -05:00
|
|
|
// execute POST request to heal api
|
|
|
|
queryVals := make(url.Values)
|
|
|
|
if clientToken != "" {
|
|
|
|
queryVals.Set("clientToken", clientToken)
|
2018-01-25 08:54:00 -05:00
|
|
|
body = []byte{}
|
2017-01-17 13:02:58 -05:00
|
|
|
}
|
2018-11-04 22:24:16 -05:00
|
|
|
|
|
|
|
// Anyone can be set, either force start or forceStop.
|
2018-01-22 17:54:55 -05:00
|
|
|
if forceStart {
|
|
|
|
queryVals.Set("forceStart", "true")
|
2018-11-04 22:24:16 -05:00
|
|
|
} else if forceStop {
|
|
|
|
queryVals.Set("forceStop", "true")
|
2017-01-17 13:02:58 -05:00
|
|
|
}
|
|
|
|
|
2020-03-20 18:00:44 -04:00
|
|
|
resp, err := adm.executeMethod(ctx,
|
|
|
|
http.MethodPost, requestData{
|
|
|
|
relPath: path,
|
|
|
|
content: body,
|
|
|
|
queryValues: queryVals,
|
|
|
|
})
|
2017-01-17 13:02:58 -05:00
|
|
|
defer closeResponse(resp)
|
|
|
|
if err != nil {
|
2018-01-22 17:54:55 -05:00
|
|
|
return healStart, healTaskStatus, err
|
2017-01-17 13:02:58 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
2018-01-22 17:54:55 -05:00
|
|
|
return healStart, healTaskStatus, httpRespToErrorResponse(resp)
|
2017-03-31 20:55:15 -04:00
|
|
|
}
|
|
|
|
|
2018-01-22 17:54:55 -05:00
|
|
|
respBytes, err := ioutil.ReadAll(resp.Body)
|
2017-01-23 03:32:55 -05:00
|
|
|
if err != nil {
|
2018-01-22 17:54:55 -05:00
|
|
|
return healStart, healTaskStatus, err
|
2017-01-23 03:32:55 -05:00
|
|
|
}
|
|
|
|
|
2018-01-22 17:54:55 -05:00
|
|
|
// Was it a status request?
|
|
|
|
if clientToken == "" {
|
2018-11-04 22:24:16 -05:00
|
|
|
// As a special operation forceStop would return a
|
|
|
|
// similar struct as healStart will have the
|
|
|
|
// heal sequence information about the heal which
|
|
|
|
// was stopped.
|
2018-01-22 17:54:55 -05:00
|
|
|
err = json.Unmarshal(respBytes, &healStart)
|
|
|
|
} else {
|
|
|
|
err = json.Unmarshal(respBytes, &healTaskStatus)
|
2017-01-23 03:32:55 -05:00
|
|
|
}
|
2018-11-04 22:24:16 -05:00
|
|
|
if err != nil {
|
|
|
|
// May be the server responded with error after success
|
|
|
|
// message, handle it separately here.
|
|
|
|
var errResp ErrorResponse
|
|
|
|
err = json.Unmarshal(respBytes, &errResp)
|
|
|
|
if err != nil {
|
|
|
|
// Unknown structure return error anyways.
|
|
|
|
return healStart, healTaskStatus, err
|
|
|
|
}
|
|
|
|
return healStart, healTaskStatus, errResp
|
|
|
|
}
|
|
|
|
return healStart, healTaskStatus, nil
|
2017-01-23 03:32:55 -05:00
|
|
|
}
|
2019-06-25 19:42:24 -04:00
|
|
|
|
|
|
|
// BgHealState represents the status of the background heal
|
|
|
|
type BgHealState struct {
|
|
|
|
ScannedItemsCount int64
|
|
|
|
LastHealActivity time.Time
|
2020-03-12 02:00:31 -04:00
|
|
|
NextHealRound time.Time
|
2020-08-07 16:22:53 -04:00
|
|
|
HealDisks []string
|
2019-06-25 19:42:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// BackgroundHealStatus returns the background heal status of the
|
|
|
|
// current server or cluster.
|
2020-03-20 18:00:44 -04:00
|
|
|
func (adm *AdminClient) BackgroundHealStatus(ctx context.Context) (BgHealState, error) {
|
2019-06-25 19:42:24 -04:00
|
|
|
// Execute POST request to background heal status api
|
2020-03-20 18:00:44 -04:00
|
|
|
resp, err := adm.executeMethod(ctx,
|
|
|
|
http.MethodPost,
|
|
|
|
requestData{relPath: adminAPIPrefix + "/background-heal/status"})
|
2019-06-25 19:42:24 -04:00
|
|
|
if err != nil {
|
|
|
|
return BgHealState{}, err
|
|
|
|
}
|
|
|
|
defer closeResponse(resp)
|
|
|
|
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
|
|
return BgHealState{}, httpRespToErrorResponse(resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
respBytes, err := ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
return BgHealState{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var healState BgHealState
|
|
|
|
|
|
|
|
err = json.Unmarshal(respBytes, &healState)
|
|
|
|
if err != nil {
|
|
|
|
return BgHealState{}, err
|
|
|
|
}
|
|
|
|
return healState, nil
|
|
|
|
}
|