Replace admin-heal-list with admin-heal --fake (#2101)

* Replace admin-heal-list with admin-heal --fake

* Improve admin-heal message for partial healing

* Vendorize pkg/madmin changes to include improved messaging for
  partial healing of objects/uploads.

* Modify message strings to include additional heal result information
  like number of disks where heal was successful.
This commit is contained in:
Krishnan Parthasarathi
2017-04-03 12:16:21 -07:00
committed by Harshavardhana
parent a009299433
commit 7084a4361e
5 changed files with 272 additions and 299 deletions
-256
View File
@@ -1,256 +0,0 @@
/*
* Minio Client (C) 2017 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.
*/
package cmd
import (
"encoding/json"
"fmt"
"path/filepath"
"strings"
"github.com/fatih/color"
"github.com/minio/cli"
"github.com/minio/mc/pkg/console"
"github.com/minio/minio/pkg/madmin"
"github.com/minio/minio/pkg/probe"
)
var (
adminHealListFlags = []cli.Flag{
cli.BoolFlag{
Name: "recursive, r",
Usage: "List recursively.",
},
cli.BoolFlag{
Name: "incomplete, I",
Usage: "List incomplete uploads",
},
}
)
var adminHealListCmd = cli.Command{
Name: "list",
Usage: "Get the list of buckets or objects that need to be healed.",
Before: setGlobalsFromContext,
Action: mainAdminHealList,
Flags: append(adminHealListFlags, globalFlags...),
CustomHelpTemplate: `NAME:
{{.HelpName}} - {{.Usage}}
USAGE:
{{.HelpName}} ALIAS/[BUCKET/][PREFIX/]
FLAGS:
{{range .VisibleFlags}}{{.}}
{{end}}
EXAMPLES:
1. List objects than need to be healed related to 'testbucket' in a Minio server represented by its alias 'play'.
$ {{.HelpName}} play/testbucket/
2. Recursively list objects than need to be healed.
$ {{.HelpName}} --recursive play/
3. Recursively list incomplete uploads than need to be healed.
$ {{.HelpName}} --incomplete --recursive play/
`,
}
// healListMessage container to hold heal information.
type healListMessage struct {
Status string `json:"status"`
Bucket string `json:"bucket"`
Object *madmin.ObjectInfo `json:"object"`
Upload *madmin.UploadInfo `json:"upload"`
}
// String colorized service status message.
func (u healListMessage) String() string {
msg := ""
var healStatus madmin.HealStatus
// Check if we have object heal information
if u.Object != nil {
msg += fmt.Sprintf("Object: %s/%s, ", u.Bucket, u.Object.Key)
healStatus = u.Object.HealObjectInfo.Status
} else {
msg += fmt.Sprintf("Upload: %s/%s, ", u.Bucket, u.Upload.Key)
healStatus = u.Upload.HealUploadInfo.Status
}
// Print heal status
switch healStatus {
case madmin.CanHeal:
msg += "can be healed."
case madmin.Corrupted:
msg += "cannot be healed."
case madmin.QuorumUnavailable:
msg += "quorum not available for healing."
}
return console.Colorize("Heal", msg)
}
// JSON jsonified service status Message message.
func (u healListMessage) JSON() string {
u.Status = "success"
statusJSONBytes, e := json.Marshal(u)
fatalIf(probe.NewError(e), "Unable to marshal into JSON.")
return string(statusJSONBytes)
}
// healBucketListMessage container to hold heal information.
type healBucketListMessage struct {
Status string `json:"status"`
Bucket madmin.BucketInfo `json:"bucket"`
}
// String colorized service status message.
func (u healBucketListMessage) String() string {
msg := fmt.Sprintf("Bucket: `%s`, ", u.Bucket.Name)
switch u.Bucket.HealBucketInfo.Status {
case madmin.CanHeal:
msg += "can be healed"
case madmin.Corrupted:
msg += "cannot be healed"
case madmin.QuorumUnavailable:
msg += "quorum not available for healing"
}
msg += ".\n"
return console.Colorize("Heal", msg)
}
// JSON jsonified service status Message message.
func (u healBucketListMessage) JSON() string {
u.Status = "success"
statusJSONBytes, e := json.Marshal(u)
fatalIf(probe.NewError(e), "Unable to marshal into JSON.")
return string(statusJSONBytes)
}
// checkAdminHealListSyntax - validate all the passed arguments
func checkAdminHealListSyntax(ctx *cli.Context) {
if len(ctx.Args()) == 0 || len(ctx.Args()) > 2 {
cli.ShowCommandHelpAndExit(ctx, "list", 1) // last argument is exit code
}
}
func mainAdminHealList(ctx *cli.Context) error {
// Check for heal list syntax
checkAdminHealListSyntax(ctx)
// Set console theme for heal command
console.SetColor("Heal", color.New(color.FgGreen, color.Bold))
// Get the alias parameter from cli
args := ctx.Args()
aliasedURL := args.Get(0)
isRecursive := ctx.Bool("recursive")
isIncomplete := ctx.Bool("incomplete")
// Create a new Minio Admin Client
client, err := newAdminClient(aliasedURL)
if err != nil {
return err.ToGoError()
}
// Transform windows backslash to the regular slash in the aliased url
aliasedURL = filepath.ToSlash(aliasedURL)
// Compute bucket and object from aliased url
splits := splitStr(aliasedURL, "/", 3)
bucket, object := splits[1], splits[2]
// If bucket is not specified, list buckets that need to be healed
if bucket == "" {
bucketsToHeal, e := client.ListBucketsHeal()
fatalIf(probe.NewError(e), "Cannot list buckets that need healing.")
// Iterate over buckets that need healing and print them
for _, bucketHeal := range bucketsToHeal {
printMsg(healBucketListMessage{Bucket: bucketHeal})
}
}
// When bucket is not specified and recursive flag is not activated,
// it means we will not scan inside buckets
if bucket == "" && !isRecursive {
return nil
}
// Now, it is time to examine for objects that need to be healed
var buckets []string
if bucket == "" {
// Bucket is not specified, so we target all buckets so we need to list them.
s3Client, err := newClient(aliasedURL)
if err != nil {
fatalIf(err, "Cannot resolve the provided path.")
}
recursive := false
incomplete := false
listCh := s3Client.List(recursive, incomplete, DirFirst)
for content := range listCh {
// Trim the leading slash and add to the list of buckets
buckets = append(buckets,
strings.TrimPrefix(content.URL.Path, string(content.URL.Separator)))
}
} else {
// Bucket is passed in argument, so we will only search for objects in the specified bucket
buckets = append(buckets, bucket)
}
// Iterate over all computed buckets
for _, currBucket := range buckets {
// Search for objects that need to be healed in the current bucket
doneCh := make(chan struct{})
if isIncomplete {
listCh, e := client.ListUploadsHeal(currBucket, object, isRecursive, doneCh)
fatalIf(probe.NewError(e), "Cannot list heal uploads.")
// Iterate over uploads and print them when not errors
for upload := range listCh {
if upload.Err != nil {
errorIf(probe.NewError(upload.Err), "Cannot heal upload `"+upload.Key+"`.")
continue
}
// Skip for non-recursive use case.
if upload.HealUploadInfo == nil {
continue
}
printMsg(healListMessage{Bucket: currBucket, Upload: &upload})
}
} else {
listCh, e := client.ListObjectsHeal(currBucket, object, isRecursive, doneCh)
fatalIf(probe.NewError(e), "Cannot list heal objects.")
// Iterate over objects and print them when not errors
for obj := range listCh {
if obj.Err != nil {
errorIf(probe.NewError(obj.Err), "Cannot heal object `"+obj.Key+"`.")
continue
}
// Skip for non-recursive use case.
if obj.HealObjectInfo == nil {
continue
}
printMsg(healListMessage{Bucket: currBucket, Object: &obj})
}
}
}
return nil
}
+205 -21
View File
@@ -21,6 +21,7 @@ import (
"fmt"
"os"
"path/filepath"
"strings"
"github.com/fatih/color"
"github.com/minio/cli"
@@ -59,34 +60,44 @@ FLAGS:
{{range .VisibleFlags}}{{.}}
{{end}}
EXAMPLES:
1. Heal 'testbucket' in a Minio server represented by its alias 'play'.
1. To format newly replaced disks in a Minio server with alias 'play'
$ {{.HelpName}} play
2. Heal 'testbucket' in a Minio server with alias 'play'
$ {{.HelpName}} play/testbucket/
2. Heal all objects under 'dir' prefix
3. Heal all objects under 'dir' prefix
$ {{.HelpName}} --recursive play/testbucket/dir/
3. Heal all uploads under 'dir' prefix
4. Heal all uploads under 'dir' prefix
$ {{.HelpName}} --incomplete --recursive play/testbucket/dir/
4. Issue a fake heal operation to see what the server could report
$ {{.HelpName}} --fake play/testbucket/dir/
5. Issue a fake heal operation to list all objects to be healed
$ {{.HelpName}} --fake play
6. Issue a fake heal operation to list all uploads to be healed
$ {{.HelpName}} --fake --incomplete play
7. Issue a fake heal operation to list all objects to be healed under 'dir' prefix
$ {{.HelpName}} --recursive --fake play/testbucket/dir/
8. Issue a fake heal operation to list all uploads to be healed under 'dir' prefix
$ {{.HelpName}} --incomplete --recursive --fake play/testbucket/dir/
`
var adminHealCmd = cli.Command{
Name: "heal",
Usage: "Manage heal tasks",
Before: setGlobalsFromContext,
Action: mainAdminHeal,
Flags: append(adminHealFlags, globalFlags...),
Subcommands: []cli.Command{
adminHealListCmd,
},
Name: "heal",
Usage: "Manage heal tasks",
Before: setGlobalsFromContext,
Action: mainAdminHeal,
Flags: append(adminHealFlags, globalFlags...),
HideHelpCommand: true,
}
// healMessage container to hold repair information.
type healMessage struct {
Status string `json:"status"`
Result madmin.HealResult `json:"result"`
Bucket string `json:"bucket"`
Object *madmin.ObjectInfo `json:"object"`
Upload *madmin.UploadInfo `json:"upload"`
@@ -94,12 +105,27 @@ type healMessage struct {
// String colorized service status message.
func (u healMessage) String() string {
allOfflineTmpl := "isn't healed since %d disks were offline"
allHealedTmpl := "is healed on all %d disks"
someHealedTmpl := "is healed on %d disks while %d disks were offline"
msg := ""
if u.Object != nil {
msg += fmt.Sprintf("Object %s/%s is healed.", u.Bucket, u.Object.Key)
msg = fmt.Sprintf("Object %s/%s ", u.Bucket, u.Object.Key)
} else {
msg += fmt.Sprintf("Upload %s/%s/%s is healed.", u.Bucket, u.Upload.Key, u.Upload.UploadID)
msg = fmt.Sprintf("Upload %s/%s/%s ", u.Bucket, u.Upload.Key, u.Upload.UploadID)
}
healed, offline := u.Result.HealedCount, u.Result.OfflineCount
switch {
case healed == 0:
msg += fmt.Sprintf(allOfflineTmpl, offline)
case offline == 0:
msg += fmt.Sprintf(allHealedTmpl, healed)
case healed > 0 && offline > 0:
msg += fmt.Sprintf(someHealedTmpl, healed, offline)
}
return console.Colorize("Heal", msg)
}
@@ -112,6 +138,81 @@ func (u healMessage) JSON() string {
return string(statusJSONBytes)
}
// healListMessage container to hold heal information.
type healListMessage struct {
Status string `json:"status"`
Bucket string `json:"bucket"`
Object *madmin.ObjectInfo `json:"object"`
Upload *madmin.UploadInfo `json:"upload"`
}
// String colorized service status message.
func (u healListMessage) String() string {
msg := ""
var healStatus madmin.HealStatus
// Check if we have object heal information
if u.Object != nil {
msg += fmt.Sprintf("Object: %s/%s, ", u.Bucket, u.Object.Key)
healStatus = u.Object.HealObjectInfo.Status
} else {
msg += fmt.Sprintf("Upload: %s/%s, ", u.Bucket, u.Upload.Key)
healStatus = u.Upload.HealUploadInfo.Status
}
// Print heal status
switch healStatus {
case madmin.CanHeal:
msg += "can be healed."
case madmin.CanPartiallyHeal:
msg += "can be partially healed."
case madmin.Corrupted:
msg += "cannot be healed."
case madmin.QuorumUnavailable:
msg += "quorum not available for healing."
}
return console.Colorize("Heal", msg)
}
// JSON jsonified service status Message message.
func (u healListMessage) JSON() string {
u.Status = "success"
statusJSONBytes, e := json.Marshal(u)
fatalIf(probe.NewError(e), "Unable to marshal into JSON.")
return string(statusJSONBytes)
}
// healBucketListMessage container to hold heal information.
type healBucketListMessage struct {
Status string `json:"status"`
Bucket madmin.BucketInfo `json:"bucket"`
}
// String colorized service status message.
func (u healBucketListMessage) String() string {
msg := fmt.Sprintf("Bucket: `%s`, ", u.Bucket.Name)
switch u.Bucket.HealBucketInfo.Status {
case madmin.CanHeal:
msg += "can be healed"
case madmin.Corrupted:
msg += "cannot be healed"
case madmin.QuorumUnavailable:
msg += "quorum not available for healing"
}
msg += ".\n"
return console.Colorize("Heal", msg)
}
// JSON jsonified service status Message message.
func (u healBucketListMessage) JSON() string {
u.Status = "success"
statusJSONBytes, e := json.Marshal(u)
fatalIf(probe.NewError(e), "Unable to marshal into JSON.")
return string(statusJSONBytes)
}
func healObjects(client *madmin.AdminClient, bucket, object string, isRecursive, isFake bool) {
// Search for objects that need healing
doneCh := make(chan struct{})
@@ -132,15 +233,20 @@ func healObjects(client *madmin.AdminClient, bucket, object string, isRecursive,
// Check the heal status, and call heal object API only when an object can be healed
switch healInfo := *obj.HealObjectInfo; healInfo.Status {
case madmin.CanHeal:
case madmin.CanHeal, madmin.CanPartiallyHeal:
// Heal Object
if e = client.HealObject(bucket, obj.Key, isFake); e != nil {
var healResult madmin.HealResult
if healResult, e = client.HealObject(bucket, obj.Key, isFake); e != nil {
errorIf(probe.NewError(e), "Cannot repair object: `"+obj.Key+"`")
continue
}
// Print successful message
printMsg(healMessage{Bucket: bucket, Object: &obj})
if isFake {
printMsg(healListMessage{Bucket: bucket, Object: &obj})
} else {
printMsg(healMessage{Bucket: bucket, Object: &obj, Result: healResult})
}
case madmin.QuorumUnavailable:
errorIf(errDummy().Trace(), obj.Key+" cannot be healed until quorum is available.")
case madmin.Corrupted:
@@ -169,14 +275,19 @@ func healUploads(client *madmin.AdminClient, bucket, object string, isRecursive,
// Check the heal status, and call heal upload API only when an upload can be healed
switch healInfo := *upload.HealUploadInfo; healInfo.Status {
case madmin.CanHeal:
case madmin.CanHeal, madmin.CanPartiallyHeal:
// Heal Upload
if e = client.HealUpload(bucket, upload.Key, upload.UploadID, isFake); e != nil {
var healResult madmin.HealResult
if healResult, e = client.HealUpload(bucket, upload.Key, upload.UploadID, isFake); e != nil {
errorIf(probe.NewError(e), "Cannot repair upload: `"+upload.Key+"`")
continue
}
// Print successful message
printMsg(healMessage{Bucket: bucket, Upload: &upload})
if isFake {
printMsg(healListMessage{Bucket: bucket, Upload: &upload})
} else {
printMsg(healMessage{Bucket: bucket, Upload: &upload, Result: healResult})
}
case madmin.QuorumUnavailable:
errorIf(errDummy().Trace(), upload.Key+" cannot be healed until quorum is available.")
case madmin.Corrupted:
@@ -193,6 +304,72 @@ func checkAdminHealSyntax(ctx *cli.Context) {
}
}
// listAllToBeHealed - list objects/uploads to be healed in the object
// store with alias `aliasedURL`
// - isIncomplete - if true we list uploads to be healed otherwise
// list objects to be healed
func listAllToBeHealed(client *madmin.AdminClient, aliasedURL string, isIncomplete bool) *probe.Error {
s3Client, err := newClient(aliasedURL)
if err != nil {
return err
}
recursive := false
incomplete := false
listCh := s3Client.List(recursive, incomplete, DirFirst)
var buckets []string
for content := range listCh {
// Trim the leading slash and add to the list of buckets
buckets = append(buckets,
strings.TrimPrefix(content.URL.Path, string(content.URL.Separator)))
}
// isRecursive is always true since we have empty object names
// when `mc admin heal --fake [-r] s1/` is invoked.
isRecursive := true
// Iterate over all computed buckets
for _, currBucket := range buckets {
// Search for objects that need to be healed in the current bucket
doneCh := make(chan struct{})
if isIncomplete {
listCh, e := client.ListUploadsHeal(currBucket, "", isRecursive, doneCh)
fatalIf(probe.NewError(e), "Cannot list heal uploads.")
// Iterate over uploads and print them when not errors
for upload := range listCh {
if upload.Err != nil {
errorIf(probe.NewError(upload.Err), "Cannot heal upload `"+upload.Key+"`.")
continue
}
// Skip for non-recursive use case.
if upload.HealUploadInfo == nil {
continue
}
printMsg(healListMessage{Bucket: currBucket, Upload: &upload})
}
} else {
listCh, e := client.ListObjectsHeal(currBucket, "", isRecursive, doneCh)
fatalIf(probe.NewError(e), "Cannot list heal objects.")
// Iterate over objects and print them when not errors
for obj := range listCh {
if obj.Err != nil {
errorIf(probe.NewError(obj.Err), "Cannot heal object `"+obj.Key+"`.")
continue
}
// Skip for non-recursive use case.
if obj.HealObjectInfo == nil {
continue
}
printMsg(healListMessage{Bucket: currBucket, Object: &obj})
}
}
}
return nil
}
// mainAdminHeal - the entry function of heal command
func mainAdminHeal(ctx *cli.Context) error {
@@ -225,6 +402,13 @@ func mainAdminHeal(ctx *cli.Context) error {
// Heal format if bucket is not specified and quit immediately
if bucket == "" {
// If --fake was given, print all objects/uploads that
// need to be healed.
if isFake {
lErr := listAllToBeHealed(client, aliasedURL, isIncomplete)
fatalIf(lErr, "Unable to list all objects to be healed")
return nil
}
e = client.HealFormat(isFake)
fatalIf(probe.NewError(e), "Cannot heal the disks.")
return nil
+8 -7
View File
@@ -242,18 +242,19 @@ __Example__
```
<a name="HealObject"></a>
### HealObject(bucket, object string, isDryRun bool) error
### HealObject(bucket, object string, isDryRun bool) (HealResult, error)
If object is successfully healed returns nil, otherwise returns error indicating the reason for failure. If isDryRun is true, then the object is not healed, but heal object request is validated by the server. e.g, if the object exists, if object name is valid etc.
__Example__
``` go
isDryRun := false
err := madmClnt.HealObject("mybucket", "myobject", isDryRun)
isDryRun = false
healResult, err := madmClnt.HealObject("mybucket", "myobject", isDryRun)
if err != nil {
log.Fatalln(err)
}
log.Println("successfully healed mybucket/myobject")
log.Println("Heal-object result: ", healResult)
```
@@ -323,17 +324,17 @@ __Example__
```
<a name="HealUpload"></a>
### HealUpload(bucket, object, uploadID string, isDryRun bool) error
### HealUpload(bucket, object, uploadID string, isDryRun bool) (HealResult, error)
If upload is successfully healed returns nil, otherwise returns error indicating the reason for failure. If isDryRun is true, then the upload is not healed, but heal upload request is validated by the server. e.g, if the upload exists, if upload name is valid etc.
``` go
isDryRun = false
err = madmClnt.HealUpload("mybucket", "myobject", "myuploadID", isDryRun)
healResult, err := madmClnt.HealUpload("mybucket", "myobject", "myUploadID", isDryRun)
if err != nil {
log.Fatalln(err)
}
log.Println("successfully healed mybucket/myobject/myuploadID")
log.Println("Heal-upload result: ", healResult)
```
## 5. Config operations
+56 -12
View File
@@ -20,8 +20,10 @@
package madmin
import (
"encoding/json"
"encoding/xml"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"time"
@@ -104,8 +106,12 @@ const (
CanHeal
// Corrupted - Object can't be healed
Corrupted
// QuorumUnavailable - Object can't be healed until read quorum is available
// QuorumUnavailable - Object can't be healed until read
// quorum is available
QuorumUnavailable
// CanPartiallyHeal - Object can't be healed completely until
// disks with missing parts come online
CanPartiallyHeal
)
// HealBucketInfo - represents healing related information of a bucket.
@@ -127,9 +133,9 @@ type BucketInfo struct {
// HealObjectInfo - represents healing related information of an object.
type HealObjectInfo struct {
Status HealStatus
MissingDataCount int
MissingPartityCount int
Status HealStatus
MissingDataCount int
MissingParityCount int
}
// ObjectInfo container for object metadata.
@@ -434,7 +440,7 @@ func (adm *AdminClient) HealBucket(bucket string, dryrun bool) error {
}
// HealUpload - Heal the given upload.
func (adm *AdminClient) HealUpload(bucket, object, uploadID string, dryrun bool) error {
func (adm *AdminClient) HealUpload(bucket, object, uploadID string, dryrun bool) (HealResult, error) {
// Construct query params.
queryVal := url.Values{}
queryVal.Set("heal", "")
@@ -460,18 +466,40 @@ func (adm *AdminClient) HealUpload(bucket, object, uploadID string, dryrun bool)
defer closeResponse(resp)
if err != nil {
return err
return HealResult{}, err
}
if resp.StatusCode != http.StatusOK {
return httpRespToErrorResponse(resp)
return HealResult{}, httpRespToErrorResponse(resp)
}
return nil
// Healing is not performed so heal object result is empty.
if dryrun {
return HealResult{}, nil
}
jsonBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
return HealResult{}, err
}
healResult := HealResult{}
err = json.Unmarshal(jsonBytes, &healResult)
if err != nil {
return HealResult{}, err
}
return healResult, nil
}
// HealResult - represents result of heal-object admin API.
type HealResult struct {
HealedCount int // number of disks that were healed.
OfflineCount int // number of disks that needed healing but were offline.
}
// HealObject - Heal the given object.
func (adm *AdminClient) HealObject(bucket, object string, dryrun bool) error {
func (adm *AdminClient) HealObject(bucket, object string, dryrun bool) (HealResult, error) {
// Construct query params.
queryVal := url.Values{}
queryVal.Set("heal", "")
@@ -494,14 +522,30 @@ func (adm *AdminClient) HealObject(bucket, object string, dryrun bool) error {
defer closeResponse(resp)
if err != nil {
return err
return HealResult{}, err
}
if resp.StatusCode != http.StatusOK {
return httpRespToErrorResponse(resp)
return HealResult{}, httpRespToErrorResponse(resp)
}
return nil
// Healing is not performed so heal object result is empty.
if dryrun {
return HealResult{}, nil
}
jsonBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
return HealResult{}, err
}
healResult := HealResult{}
err = json.Unmarshal(jsonBytes, &healResult)
if err != nil {
return HealResult{}, err
}
return healResult, nil
}
// HealFormat - heal storage format on available disks.
+3 -3
View File
@@ -80,10 +80,10 @@
"revisionTime": "2016-08-18T00:31:20Z"
},
{
"checksumSHA1": "M7IzPNZncWn7ve14/YPF2HKhCko=",
"checksumSHA1": "KLEdjpItmkn1LDY/2xTQHSzebnU=",
"path": "github.com/minio/minio/pkg/madmin",
"revision": "97dc34fe06274894316a68e0e86e6c8cf498e21a",
"revisionTime": "2017-03-21T19:02:39Z"
"revision": "522b307ea67ed04f68fcbf98b67549532a6ac424",
"revisionTime": "2017-04-03T11:36:28Z"
},
{
"path": "github.com/minio/minio/pkg/probe",