github.com/minio/madmin-go/v2@v2.2.1/rebalance.go (about)

     1  //
     2  // Copyright (c) 2015-2022 MinIO, Inc.
     3  //
     4  // This file is part of MinIO Object Storage stack
     5  //
     6  // This program is free software: you can redistribute it and/or modify
     7  // it under the terms of the GNU Affero General Public License as
     8  // published by the Free Software Foundation, either version 3 of the
     9  // License, or (at your option) any later version.
    10  //
    11  // This program is distributed in the hope that it will be useful,
    12  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    13  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    14  // GNU Affero General Public License for more details.
    15  //
    16  // You should have received a copy of the GNU Affero General Public License
    17  // along with this program. If not, see <http://www.gnu.org/licenses/>.
    18  //
    19  
    20  package madmin
    21  
    22  import (
    23  	"context"
    24  	"encoding/json"
    25  	"io/ioutil"
    26  	"net/http"
    27  	"time"
    28  )
    29  
    30  // RebalPoolProgress contains metrics like number of objects, versions, etc rebalanced so far.
    31  type RebalPoolProgress struct {
    32  	NumObjects  uint64        `json:"objects"`
    33  	NumVersions uint64        `json:"versions"`
    34  	Bytes       uint64        `json:"bytes"`
    35  	Bucket      string        `json:"bucket"`
    36  	Object      string        `json:"object"`
    37  	Elapsed     time.Duration `json:"elapsed"`
    38  	ETA         time.Duration `json:"eta"`
    39  }
    40  
    41  // RebalancePoolStatus contains metrics of a rebalance operation on a given pool
    42  type RebalancePoolStatus struct {
    43  	ID       int               `json:"id"`                 // Pool index (zero-based)
    44  	Status   string            `json:"status"`             // Active if rebalance is running, empty otherwise
    45  	Used     float64           `json:"used"`               // Percentage used space
    46  	Progress RebalPoolProgress `json:"progress,omitempty"` // is empty when rebalance is not running
    47  }
    48  
    49  // RebalanceStatus contains metrics and progress related information on all pools
    50  type RebalanceStatus struct {
    51  	ID        string                // identifies the ongoing rebalance operation by a uuid
    52  	StoppedAt time.Time             `json:"stoppedAt,omitempty"`
    53  	Pools     []RebalancePoolStatus `json:"pools"` // contains all pools, including inactive
    54  }
    55  
    56  // RebalanceStart starts a rebalance operation if one isn't in progress already
    57  func (adm *AdminClient) RebalanceStart(ctx context.Context) (id string, err error) {
    58  	// Execute POST on /minio/admin/v3/rebalance/start to start a rebalance operation.
    59  	var resp *http.Response
    60  	resp, err = adm.executeMethod(ctx,
    61  		http.MethodPost,
    62  		requestData{relPath: adminAPIPrefix + "/rebalance/start"})
    63  	defer closeResponse(resp)
    64  	if err != nil {
    65  		return id, err
    66  	}
    67  
    68  	if resp.StatusCode != http.StatusOK {
    69  		return id, httpRespToErrorResponse(resp)
    70  	}
    71  
    72  	var rebalInfo struct {
    73  		ID string `json:"id"`
    74  	}
    75  	respBytes, err := ioutil.ReadAll(resp.Body)
    76  	if err != nil {
    77  		return id, err
    78  	}
    79  
    80  	err = json.Unmarshal(respBytes, &rebalInfo)
    81  	if err != nil {
    82  		return id, err
    83  	}
    84  
    85  	return rebalInfo.ID, nil
    86  }
    87  
    88  func (adm *AdminClient) RebalanceStatus(ctx context.Context) (r RebalanceStatus, err error) {
    89  	// Execute GET on /minio/admin/v3/rebalance/status to get status of an ongoing rebalance operation.
    90  	resp, err := adm.executeMethod(ctx,
    91  		http.MethodGet,
    92  		requestData{relPath: adminAPIPrefix + "/rebalance/status"})
    93  	defer closeResponse(resp)
    94  	if err != nil {
    95  		return r, err
    96  	}
    97  
    98  	if resp.StatusCode != http.StatusOK {
    99  		return r, httpRespToErrorResponse(resp)
   100  	}
   101  
   102  	respBytes, err := ioutil.ReadAll(resp.Body)
   103  	if err != nil {
   104  		return r, err
   105  	}
   106  
   107  	err = json.Unmarshal(respBytes, &r)
   108  	if err != nil {
   109  		return r, err
   110  	}
   111  
   112  	return r, nil
   113  }
   114  
   115  func (adm *AdminClient) RebalanceStop(ctx context.Context) error {
   116  	// Execute POST on /minio/admin/v3/rebalance/stop to stop an ongoing rebalance operation.
   117  	resp, err := adm.executeMethod(ctx,
   118  		http.MethodPost,
   119  		requestData{relPath: adminAPIPrefix + "/rebalance/stop"})
   120  	defer closeResponse(resp)
   121  	if err != nil {
   122  		return err
   123  	}
   124  
   125  	if resp.StatusCode != http.StatusOK {
   126  		return httpRespToErrorResponse(resp)
   127  	}
   128  
   129  	return nil
   130  }