github.com/m3db/m3@v1.5.1-0.20231129193456-75a402aa583b/src/aggregator/tools/deploy/client.go (about)

     1  // Copyright (c) 2017 Uber Technologies, Inc.
     2  //
     3  // Permission is hereby granted, free of charge, to any person obtaining a copy
     4  // of this software and associated documentation files (the "Software"), to deal
     5  // in the Software without restriction, including without limitation the rights
     6  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     7  // copies of the Software, and to permit persons to whom the Software is
     8  // furnished to do so, subject to the following conditions:
     9  //
    10  // The above copyright notice and this permission notice shall be included in
    11  // all copies or substantial portions of the Software.
    12  //
    13  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    14  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    15  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    16  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    17  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    18  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    19  // THE SOFTWARE.
    20  
    21  package deploy
    22  
    23  import (
    24  	"encoding/json"
    25  	"fmt"
    26  	"io"
    27  	"io/ioutil"
    28  	"net/http"
    29  
    30  	"github.com/m3db/m3/src/aggregator/aggregator"
    31  	httpserver "github.com/m3db/m3/src/aggregator/server/http"
    32  )
    33  
    34  // AggregatorClient interacts with aggregator instances via aggregator endpoints.
    35  type AggregatorClient interface {
    36  	// IsHealthy determines whether an instance is healthy.
    37  	IsHealthy(instance string) error
    38  
    39  	// Status returns the instance status.
    40  	Status(instance string) (aggregator.RuntimeStatus, error)
    41  
    42  	// Resign asks an aggregator instance to give up its current leader role if applicable.
    43  	// The instance however still participates in leader election as a follower after
    44  	// resiganation succeeds.
    45  	Resign(instance string) error
    46  }
    47  
    48  type doRequestFn func(*http.Request) (*http.Response, error)
    49  
    50  type client struct {
    51  	httpClient *http.Client
    52  
    53  	doRequestFn doRequestFn
    54  }
    55  
    56  // NewAggregatorClient creates a new aggregator client.
    57  func NewAggregatorClient(httpClient *http.Client) AggregatorClient {
    58  	return &client{
    59  		httpClient:  httpClient,
    60  		doRequestFn: httpClient.Do,
    61  	}
    62  }
    63  
    64  func (c *client) IsHealthy(instance string) error {
    65  	response := httpserver.NewResponse()
    66  	return c.doRequest(instance, http.MethodGet, httpserver.HealthPath, &response)
    67  }
    68  
    69  func (c *client) Status(instance string) (aggregator.RuntimeStatus, error) {
    70  	var (
    71  		status   aggregator.RuntimeStatus
    72  		response = httpserver.NewStatusResponse()
    73  	)
    74  	err := c.doRequest(instance, http.MethodGet, httpserver.StatusPath, &response)
    75  	if err != nil {
    76  		return status, err
    77  	}
    78  	return response.Status, nil
    79  }
    80  
    81  func (c *client) Resign(instance string) error {
    82  	response := httpserver.NewResponse()
    83  	return c.doRequest(instance, http.MethodPost, httpserver.ResignPath, &response)
    84  }
    85  
    86  func (c *client) doRequest(
    87  	hostPort, method, path string,
    88  	response interface{},
    89  ) error {
    90  	url := fmt.Sprintf("http://%s%s", hostPort, path)
    91  	req, err := http.NewRequest(method, url, nil)
    92  	if err != nil {
    93  		return fmt.Errorf("unable to create request: %v", err)
    94  	}
    95  	req.Header.Add("Content-Type", "application/json")
    96  
    97  	// NB(xichen): need to read and discard all the data in the response body
    98  	// before closing the body or otherwise the underlying connections are not
    99  	// eligible for reuse.
   100  	resp, err := c.doRequestFn(req)
   101  	if err != nil {
   102  		return err
   103  	}
   104  	defer func() {
   105  		io.Copy(ioutil.Discard, resp.Body)
   106  		resp.Body.Close()
   107  	}()
   108  
   109  	statusCode := resp.StatusCode
   110  	if statusCode != http.StatusOK {
   111  		return fmt.Errorf("response code is %d instead of 200", statusCode)
   112  	}
   113  
   114  	b, err := ioutil.ReadAll(resp.Body)
   115  	if err != nil {
   116  		return fmt.Errorf("unable to read response body: %v", err)
   117  	}
   118  
   119  	if err := json.Unmarshal(b, response); err != nil {
   120  		return fmt.Errorf("unable to unmarshal response body: %v", err)
   121  	}
   122  
   123  	var responseErr string
   124  	switch t := response.(type) {
   125  	case *httpserver.Response:
   126  		responseErr = t.Error
   127  	case *httpserver.StatusResponse:
   128  		responseErr = t.Error
   129  	}
   130  	if responseErr != "" {
   131  		return fmt.Errorf("received error response: %v", response)
   132  	}
   133  
   134  	return nil
   135  }