2018-10-04 20:44:06 -04:00
|
|
|
/*
|
2020-06-17 17:49:26 -04:00
|
|
|
* MinIO Cloud Storage, (C) 2018-2020 MinIO, Inc.
|
2018-10-04 20:44:06 -04: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 rest
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
2020-11-23 12:12:17 -05:00
|
|
|
"fmt"
|
2018-10-04 20:44:06 -04:00
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
2020-10-13 21:28:42 -04:00
|
|
|
"math/rand"
|
2018-10-04 20:44:06 -04:00
|
|
|
"net/http"
|
|
|
|
"net/url"
|
2020-06-16 21:59:32 -04:00
|
|
|
"sync/atomic"
|
2018-10-04 20:44:06 -04:00
|
|
|
"time"
|
|
|
|
|
|
|
|
xhttp "github.com/minio/minio/cmd/http"
|
2020-11-04 11:27:32 -05:00
|
|
|
"github.com/minio/minio/cmd/logger"
|
2020-09-08 17:22:04 -04:00
|
|
|
xnet "github.com/minio/minio/pkg/net"
|
2018-10-04 20:44:06 -04:00
|
|
|
)
|
|
|
|
|
2020-09-29 18:18:34 -04:00
|
|
|
// DefaultTimeout - default REST timeout is 10 seconds.
|
|
|
|
const DefaultTimeout = 10 * time.Second
|
2018-10-04 20:44:06 -04:00
|
|
|
|
2020-06-16 21:59:32 -04:00
|
|
|
const (
|
|
|
|
offline = iota
|
|
|
|
online
|
|
|
|
closed
|
|
|
|
)
|
|
|
|
|
2019-05-29 13:21:47 -04:00
|
|
|
// NetworkError - error type in case of errors related to http/transport
|
|
|
|
// for ex. connection refused, connection reset, dns resolution failure etc.
|
|
|
|
// All errors returned by storage-rest-server (ex errFileNotFound, errDiskNotFound) are not considered to be network errors.
|
|
|
|
type NetworkError struct {
|
|
|
|
Err error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *NetworkError) Error() string {
|
|
|
|
return n.Err.Error()
|
|
|
|
}
|
|
|
|
|
2020-06-16 21:59:32 -04:00
|
|
|
// Unwrap returns the error wrapped in NetworkError.
|
|
|
|
func (n *NetworkError) Unwrap() error {
|
|
|
|
return n.Err
|
|
|
|
}
|
|
|
|
|
2018-10-04 20:44:06 -04:00
|
|
|
// Client - http based RPC client.
|
|
|
|
type Client struct {
|
2021-02-08 11:51:12 -05:00
|
|
|
connected int32 // ref: https://golang.org/pkg/sync/atomic/#pkg-note-BUG
|
|
|
|
|
2020-06-17 17:49:26 -04:00
|
|
|
// HealthCheckFn is the function set to test for health.
|
|
|
|
// If not set the client will not keep track of health.
|
|
|
|
// Calling this returns true or false if the target
|
|
|
|
// is online or offline.
|
|
|
|
HealthCheckFn func() bool
|
2020-06-16 21:59:32 -04:00
|
|
|
|
|
|
|
// HealthCheckInterval will be the duration between re-connection attempts
|
|
|
|
// when a call has failed with a network error.
|
|
|
|
HealthCheckInterval time.Duration
|
|
|
|
|
|
|
|
// HealthCheckTimeout determines timeout for each call.
|
|
|
|
HealthCheckTimeout time.Duration
|
|
|
|
|
|
|
|
// MaxErrResponseSize is the maximum expected response size.
|
|
|
|
// Should only be modified before any calls are made.
|
|
|
|
MaxErrResponseSize int64
|
|
|
|
|
2020-10-29 12:52:11 -04:00
|
|
|
// ExpectTimeouts indicates if context timeouts are expected.
|
|
|
|
// This will not mark the client offline in these cases.
|
|
|
|
ExpectTimeouts bool
|
|
|
|
|
2020-10-12 17:19:46 -04:00
|
|
|
httpClient *http.Client
|
|
|
|
url *url.URL
|
|
|
|
newAuthToken func(audience string) string
|
2018-10-04 20:44:06 -04:00
|
|
|
}
|
|
|
|
|
2019-08-06 15:08:58 -04:00
|
|
|
// URL query separator constants
|
|
|
|
const (
|
2019-11-04 12:30:59 -05:00
|
|
|
querySep = "?"
|
2019-08-06 15:08:58 -04:00
|
|
|
)
|
|
|
|
|
2020-07-30 02:15:34 -04:00
|
|
|
type restError string
|
|
|
|
|
|
|
|
func (e restError) Error() string {
|
|
|
|
return string(e)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e restError) Timeout() bool {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2020-09-04 12:45:06 -04:00
|
|
|
// Call - make a REST call with context.
|
|
|
|
func (c *Client) Call(ctx context.Context, method string, values url.Values, body io.Reader, length int64) (reply io.ReadCloser, err error) {
|
2020-06-16 21:59:32 -04:00
|
|
|
if !c.IsOnline() {
|
2020-07-30 02:15:34 -04:00
|
|
|
return nil, &NetworkError{Err: &url.Error{Op: method, URL: c.url.String(), Err: restError("remote server offline")}}
|
2020-06-16 21:59:32 -04:00
|
|
|
}
|
2020-09-08 17:22:04 -04:00
|
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodPost, c.url.String()+method+querySep+values.Encode(), body)
|
2018-10-04 20:44:06 -04:00
|
|
|
if err != nil {
|
2019-05-29 13:21:47 -04:00
|
|
|
return nil, &NetworkError{err}
|
2018-10-04 20:44:06 -04:00
|
|
|
}
|
2020-11-02 18:15:12 -05:00
|
|
|
req.Header.Set("Authorization", "Bearer "+c.newAuthToken(req.URL.RawQuery))
|
2018-10-04 20:44:06 -04:00
|
|
|
req.Header.Set("X-Minio-Time", time.Now().UTC().Format(time.RFC3339))
|
2019-01-17 07:58:18 -05:00
|
|
|
if length > 0 {
|
|
|
|
req.ContentLength = length
|
|
|
|
}
|
2018-10-04 20:44:06 -04:00
|
|
|
resp, err := c.httpClient.Do(req)
|
|
|
|
if err != nil {
|
2020-11-10 12:28:23 -05:00
|
|
|
if c.HealthCheckFn != nil && xnet.IsNetworkOrHostDown(err, c.ExpectTimeouts) {
|
2020-12-30 17:38:54 -05:00
|
|
|
if c.MarkOffline() {
|
|
|
|
logger.LogIf(ctx, fmt.Errorf("Marking %s temporary offline; caused by %w", c.url.String(), err))
|
|
|
|
}
|
2020-06-16 21:59:32 -04:00
|
|
|
}
|
2019-05-29 13:21:47 -04:00
|
|
|
return nil, &NetworkError{err}
|
2018-10-04 20:44:06 -04:00
|
|
|
}
|
|
|
|
|
2020-03-27 00:07:39 -04:00
|
|
|
final := resp.Trailer.Get("FinalStatus")
|
|
|
|
if final != "" && final != "Success" {
|
|
|
|
defer xhttp.DrainBody(resp.Body)
|
|
|
|
return nil, errors.New(final)
|
|
|
|
}
|
|
|
|
|
2018-10-04 20:44:06 -04:00
|
|
|
if resp.StatusCode != http.StatusOK {
|
2020-06-17 17:49:26 -04:00
|
|
|
// If server returns 412 pre-condition failed, it would
|
|
|
|
// mean that authentication succeeded, but another
|
|
|
|
// side-channel check has failed, we shall take
|
|
|
|
// the client offline in such situations.
|
|
|
|
// generally all implementations should simply return
|
|
|
|
// 403, but in situations where there is a dependency
|
|
|
|
// with the caller to take the client offline purpose
|
|
|
|
// fully it should make sure to respond with '412'
|
|
|
|
// instead, see cmd/storage-rest-server.go for ideas.
|
2020-11-10 12:28:23 -05:00
|
|
|
if c.HealthCheckFn != nil && resp.StatusCode == http.StatusPreconditionFailed {
|
2020-11-23 12:12:17 -05:00
|
|
|
logger.LogIf(ctx, fmt.Errorf("Marking %s temporary offline; caused by PreconditionFailed with disk ID mismatch", c.url.String()))
|
2020-06-17 17:49:26 -04:00
|
|
|
c.MarkOffline()
|
|
|
|
}
|
2019-02-06 15:07:03 -05:00
|
|
|
defer xhttp.DrainBody(resp.Body)
|
2018-10-04 20:44:06 -04:00
|
|
|
// Limit the ReadAll(), just in case, because of a bug, the server responds with large data.
|
2020-06-16 21:59:32 -04:00
|
|
|
b, err := ioutil.ReadAll(io.LimitReader(resp.Body, c.MaxErrResponseSize))
|
2018-10-04 20:44:06 -04:00
|
|
|
if err != nil {
|
2020-11-10 12:28:23 -05:00
|
|
|
if c.HealthCheckFn != nil && xnet.IsNetworkOrHostDown(err, c.ExpectTimeouts) {
|
2020-12-30 17:38:54 -05:00
|
|
|
if c.MarkOffline() {
|
|
|
|
logger.LogIf(ctx, fmt.Errorf("Marking %s temporary offline; caused by %w", c.url.String(), err))
|
|
|
|
}
|
2020-09-25 17:35:47 -04:00
|
|
|
}
|
2018-10-04 20:44:06 -04:00
|
|
|
return nil, err
|
|
|
|
}
|
2019-04-02 15:25:34 -04:00
|
|
|
if len(b) > 0 {
|
|
|
|
return nil, errors.New(string(b))
|
|
|
|
}
|
|
|
|
return nil, errors.New(resp.Status)
|
2018-10-04 20:44:06 -04:00
|
|
|
}
|
|
|
|
return resp.Body, nil
|
|
|
|
}
|
|
|
|
|
2018-11-20 14:07:19 -05:00
|
|
|
// Close closes all idle connections of the underlying http client
|
|
|
|
func (c *Client) Close() {
|
2020-06-16 21:59:32 -04:00
|
|
|
atomic.StoreInt32(&c.connected, closed)
|
2018-11-20 14:07:19 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewClient - returns new REST client.
|
2020-11-02 10:43:11 -05:00
|
|
|
func NewClient(url *url.URL, tr http.RoundTripper, newAuthToken func(aud string) string) *Client {
|
2018-11-20 14:07:19 -05:00
|
|
|
// Transport is exactly same as Go default in https://golang.org/pkg/net/http/#RoundTripper
|
|
|
|
// except custom DialContext and TLSClientConfig.
|
2018-10-04 20:44:06 -04:00
|
|
|
return &Client{
|
2018-11-20 14:07:19 -05:00
|
|
|
httpClient: &http.Client{Transport: tr},
|
|
|
|
url: url,
|
|
|
|
newAuthToken: newAuthToken,
|
2020-06-16 21:59:32 -04:00
|
|
|
connected: online,
|
|
|
|
MaxErrResponseSize: 4096,
|
|
|
|
HealthCheckInterval: 200 * time.Millisecond,
|
|
|
|
HealthCheckTimeout: time.Second,
|
2020-07-12 01:19:38 -04:00
|
|
|
}
|
2018-10-04 20:44:06 -04:00
|
|
|
}
|
2020-06-16 21:59:32 -04:00
|
|
|
|
|
|
|
// IsOnline returns whether the client is likely to be online.
|
|
|
|
func (c *Client) IsOnline() bool {
|
|
|
|
return atomic.LoadInt32(&c.connected) == online
|
|
|
|
}
|
|
|
|
|
|
|
|
// MarkOffline - will mark a client as being offline and spawns
|
2020-06-17 17:49:26 -04:00
|
|
|
// a goroutine that will attempt to reconnect if HealthCheckFn is set.
|
2020-12-30 17:38:54 -05:00
|
|
|
// returns true if the node changed state from online to offline
|
|
|
|
func (c *Client) MarkOffline() bool {
|
2020-06-16 21:59:32 -04:00
|
|
|
// Start goroutine that will attempt to reconnect.
|
|
|
|
// If server is already trying to reconnect this will have no effect.
|
2020-06-17 17:49:26 -04:00
|
|
|
if c.HealthCheckFn != nil && atomic.CompareAndSwapInt32(&c.connected, online, offline) {
|
2020-10-13 21:28:42 -04:00
|
|
|
r := rand.New(rand.NewSource(time.Now().UnixNano()))
|
|
|
|
go func() {
|
|
|
|
for {
|
2020-09-17 00:14:35 -04:00
|
|
|
if atomic.LoadInt32(&c.connected) == closed {
|
2020-06-16 21:59:32 -04:00
|
|
|
return
|
|
|
|
}
|
2020-12-08 12:23:35 -05:00
|
|
|
if c.HealthCheckFn() {
|
2020-12-30 17:38:54 -05:00
|
|
|
if atomic.CompareAndSwapInt32(&c.connected, offline, online) {
|
|
|
|
logger.Info("Client %s online", c.url.String())
|
|
|
|
}
|
2020-12-08 12:23:35 -05:00
|
|
|
return
|
2020-06-16 21:59:32 -04:00
|
|
|
}
|
2020-10-13 21:28:42 -04:00
|
|
|
time.Sleep(time.Duration(r.Float64() * float64(c.HealthCheckInterval)))
|
2020-06-16 21:59:32 -04:00
|
|
|
}
|
2020-10-13 21:28:42 -04:00
|
|
|
}()
|
2020-12-30 17:38:54 -05:00
|
|
|
return true
|
2020-06-16 21:59:32 -04:00
|
|
|
}
|
2020-12-30 17:38:54 -05:00
|
|
|
return false
|
2020-06-16 21:59:32 -04:00
|
|
|
}
|