github.com/minio/madmin-go@v1.7.5/decommission-commands.go (about)

     1  //
     2  // MinIO Object Storage (c) 2021 MinIO, Inc.
     3  //
     4  // Licensed under the Apache License, Version 2.0 (the "License");
     5  // you may not use this file except in compliance with the License.
     6  // You may obtain a copy of the License at
     7  //
     8  //      http://www.apache.org/licenses/LICENSE-2.0
     9  //
    10  // Unless required by applicable law or agreed to in writing, software
    11  // distributed under the License is distributed on an "AS IS" BASIS,
    12  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  // See the License for the specific language governing permissions and
    14  // limitations under the License.
    15  //
    16  
    17  package madmin
    18  
    19  import (
    20  	"context"
    21  	"encoding/json"
    22  	"net/http"
    23  	"net/url"
    24  	"time"
    25  )
    26  
    27  // PoolDecommissionInfo currently draining information
    28  type PoolDecommissionInfo struct {
    29  	StartTime   time.Time `json:"startTime"`
    30  	StartSize   int64     `json:"startSize"`
    31  	TotalSize   int64     `json:"totalSize"`
    32  	CurrentSize int64     `json:"currentSize"`
    33  	Complete    bool      `json:"complete"`
    34  	Failed      bool      `json:"failed"`
    35  	Canceled    bool      `json:"canceled"`
    36  }
    37  
    38  // PoolStatus captures current pool status
    39  type PoolStatus struct {
    40  	ID           int                   `json:"id"`
    41  	CmdLine      string                `json:"cmdline"`
    42  	LastUpdate   time.Time             `json:"lastUpdate"`
    43  	Decommission *PoolDecommissionInfo `json:"decommissionInfo,omitempty"`
    44  }
    45  
    46  // DecommissionPool - starts moving data from specified pool to all other existing pools.
    47  // Decommissioning if successfully started this function will return `nil`, to check
    48  // for on-going draining cycle use StatusPool.
    49  func (adm *AdminClient) DecommissionPool(ctx context.Context, pool string) error {
    50  	values := url.Values{}
    51  	values.Set("pool", pool)
    52  	resp, err := adm.executeMethod(ctx, http.MethodPost, requestData{
    53  		// POST <endpoint>/<admin-API>/pools/decommission?pool=http://server{1...4}/disk{1...4}
    54  		relPath:     adminAPIPrefix + "/pools/decommission",
    55  		queryValues: values,
    56  	})
    57  	if err != nil {
    58  		return err
    59  	}
    60  	defer closeResponse(resp)
    61  	if resp.StatusCode != http.StatusOK {
    62  		return httpRespToErrorResponse(resp)
    63  	}
    64  	return nil
    65  }
    66  
    67  // CancelDecommissionPool - cancels an on-going decommissioning process,
    68  // this automatically makes the pool available for writing once canceled.
    69  func (adm *AdminClient) CancelDecommissionPool(ctx context.Context, pool string) error {
    70  	values := url.Values{}
    71  	values.Set("pool", pool)
    72  	resp, err := adm.executeMethod(ctx, http.MethodPost, requestData{
    73  		// POST <endpoint>/<admin-API>/pools/cancel?pool=http://server{1...4}/disk{1...4}
    74  		relPath:     adminAPIPrefix + "/pools/cancel",
    75  		queryValues: values,
    76  	})
    77  	if err != nil {
    78  		return err
    79  	}
    80  	defer closeResponse(resp)
    81  	if resp.StatusCode != http.StatusOK {
    82  		return httpRespToErrorResponse(resp)
    83  	}
    84  	return nil
    85  }
    86  
    87  // StatusPool return current status about pool, reports any draining activity in progress
    88  // and elapsed time.
    89  func (adm *AdminClient) StatusPool(ctx context.Context, pool string) (PoolStatus, error) {
    90  	values := url.Values{}
    91  	values.Set("pool", pool)
    92  	resp, err := adm.executeMethod(ctx, http.MethodGet, requestData{
    93  		// GET <endpoint>/<admin-API>/pools/status?pool=http://server{1...4}/disk{1...4}
    94  		relPath:     adminAPIPrefix + "/pools/status",
    95  		queryValues: values,
    96  	})
    97  	if err != nil {
    98  		return PoolStatus{}, err
    99  	}
   100  	defer closeResponse(resp)
   101  
   102  	if resp.StatusCode != http.StatusOK {
   103  		return PoolStatus{}, httpRespToErrorResponse(resp)
   104  	}
   105  
   106  	var info PoolStatus
   107  	if err = json.NewDecoder(resp.Body).Decode(&info); err != nil {
   108  		return PoolStatus{}, err
   109  	}
   110  
   111  	return info, nil
   112  }
   113  
   114  // ListPoolsStatus returns list of pools currently configured and being used
   115  // on the cluster.
   116  func (adm *AdminClient) ListPoolsStatus(ctx context.Context) ([]PoolStatus, error) {
   117  	resp, err := adm.executeMethod(ctx, http.MethodGet, requestData{
   118  		relPath: adminAPIPrefix + "/pools/list", // GET <endpoint>/<admin-API>/pools/list
   119  	})
   120  	if err != nil {
   121  		return nil, err
   122  	}
   123  	defer closeResponse(resp)
   124  	if resp.StatusCode != http.StatusOK {
   125  		return nil, httpRespToErrorResponse(resp)
   126  	}
   127  	var pools []PoolStatus
   128  	if err = json.NewDecoder(resp.Body).Decode(&pools); err != nil {
   129  		return nil, err
   130  	}
   131  	return pools, nil
   132  }