| 
									
										
										
										
											2021-04-20 01:30:42 +08:00
										 |  |  | // Copyright (c) 2015-2021 MinIO, Inc.
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // This file is part of MinIO Object Storage stack
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // This program is free software: you can redistribute it and/or modify
 | 
					
						
							|  |  |  | // it under the terms of the GNU Affero General Public License as published by
 | 
					
						
							|  |  |  | // the Free Software Foundation, either version 3 of the License, or
 | 
					
						
							|  |  |  | // (at your option) any later version.
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // This program is distributed in the hope that it will be useful
 | 
					
						
							|  |  |  | // but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
					
						
							|  |  |  | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | 
					
						
							|  |  |  | // GNU Affero General Public License for more details.
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // You should have received a copy of the GNU Affero General Public License
 | 
					
						
							|  |  |  | // along with this program.  If not, see <http://www.gnu.org/licenses/>.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | package cmd | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							|  |  |  | 	"bytes" | 
					
						
							|  |  |  | 	"context" | 
					
						
							|  |  |  | 	"errors" | 
					
						
							|  |  |  | 	"fmt" | 
					
						
							|  |  |  | 	"io" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-06 23:52:02 +08:00
										 |  |  | 	"github.com/minio/madmin-go" | 
					
						
							| 
									
										
										
										
											2021-04-20 01:30:42 +08:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // WarmBackendGetOpts is used to express byte ranges within an object. The zero
 | 
					
						
							|  |  |  | // value represents the entire byte range of an object.
 | 
					
						
							|  |  |  | type WarmBackendGetOpts struct { | 
					
						
							|  |  |  | 	startOffset int64 | 
					
						
							|  |  |  | 	length      int64 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // WarmBackend provides interface to be implemented by remote tier backends
 | 
					
						
							|  |  |  | type WarmBackend interface { | 
					
						
							| 
									
										
										
										
											2021-06-04 05:26:51 +08:00
										 |  |  | 	Put(ctx context.Context, object string, r io.Reader, length int64) (remoteVersionID, error) | 
					
						
							|  |  |  | 	Get(ctx context.Context, object string, rv remoteVersionID, opts WarmBackendGetOpts) (io.ReadCloser, error) | 
					
						
							|  |  |  | 	Remove(ctx context.Context, object string, rv remoteVersionID) error | 
					
						
							| 
									
										
										
										
											2021-04-20 01:30:42 +08:00
										 |  |  | 	InUse(ctx context.Context) (bool, error) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const probeObject = "probeobject" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // checkWarmBackend checks if tier config credentials have sufficient privileges
 | 
					
						
							|  |  |  | // to perform all operations defined in the WarmBackend interface.
 | 
					
						
							|  |  |  | func checkWarmBackend(ctx context.Context, w WarmBackend) error { | 
					
						
							|  |  |  | 	var empty bytes.Reader | 
					
						
							| 
									
										
										
										
											2021-06-04 05:26:51 +08:00
										 |  |  | 	rv, err := w.Put(ctx, probeObject, &empty, 0) | 
					
						
							| 
									
										
										
										
											2021-04-20 01:30:42 +08:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2021-11-11 14:33:17 +08:00
										 |  |  | 		switch err.(type) { | 
					
						
							|  |  |  | 		case BackendDown: | 
					
						
							|  |  |  | 			return err | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2021-04-20 01:30:42 +08:00
										 |  |  | 		return tierPermErr{ | 
					
						
							|  |  |  | 			Op:  tierPut, | 
					
						
							|  |  |  | 			Err: err, | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-04 05:26:51 +08:00
										 |  |  | 	_, err = w.Get(ctx, probeObject, rv, WarmBackendGetOpts{}) | 
					
						
							| 
									
										
										
										
											2021-04-20 01:30:42 +08:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2021-11-11 14:33:17 +08:00
										 |  |  | 		switch err.(type) { | 
					
						
							|  |  |  | 		case BackendDown: | 
					
						
							|  |  |  | 			return err | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2021-04-20 01:30:42 +08:00
										 |  |  | 		switch { | 
					
						
							|  |  |  | 		case isErrBucketNotFound(err): | 
					
						
							|  |  |  | 			return errTierBucketNotFound | 
					
						
							|  |  |  | 		case isErrSignatureDoesNotMatch(err): | 
					
						
							|  |  |  | 			return errTierInvalidCredentials | 
					
						
							|  |  |  | 		default: | 
					
						
							|  |  |  | 			return tierPermErr{ | 
					
						
							|  |  |  | 				Op:  tierGet, | 
					
						
							|  |  |  | 				Err: err, | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-04 05:26:51 +08:00
										 |  |  | 	if err = w.Remove(ctx, probeObject, rv); err != nil { | 
					
						
							| 
									
										
										
										
											2021-11-11 14:33:17 +08:00
										 |  |  | 		switch err.(type) { | 
					
						
							|  |  |  | 		case BackendDown: | 
					
						
							|  |  |  | 			return err | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2021-04-20 01:30:42 +08:00
										 |  |  | 		return tierPermErr{ | 
					
						
							|  |  |  | 			Op:  tierDelete, | 
					
						
							|  |  |  | 			Err: err, | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return err | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | type tierOp uint8 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const ( | 
					
						
							|  |  |  | 	_ tierOp = iota | 
					
						
							|  |  |  | 	tierGet | 
					
						
							|  |  |  | 	tierPut | 
					
						
							|  |  |  | 	tierDelete | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (op tierOp) String() string { | 
					
						
							|  |  |  | 	switch op { | 
					
						
							|  |  |  | 	case tierGet: | 
					
						
							|  |  |  | 		return "GET" | 
					
						
							|  |  |  | 	case tierPut: | 
					
						
							|  |  |  | 		return "PUT" | 
					
						
							|  |  |  | 	case tierDelete: | 
					
						
							|  |  |  | 		return "DELETE" | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return "UNKNOWN" | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | type tierPermErr struct { | 
					
						
							|  |  |  | 	Op  tierOp | 
					
						
							|  |  |  | 	Err error | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (te tierPermErr) Error() string { | 
					
						
							|  |  |  | 	return fmt.Sprintf("failed to perform %s %v", te.Op, te.Err) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func errIsTierPermError(err error) bool { | 
					
						
							|  |  |  | 	var tpErr tierPermErr | 
					
						
							|  |  |  | 	return errors.As(err, &tpErr) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-04 05:26:51 +08:00
										 |  |  | // remoteVersionID represents the version id of an object in the remote tier.
 | 
					
						
							|  |  |  | // Its usage is remote tier cloud implementation specific.
 | 
					
						
							|  |  |  | type remoteVersionID string | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-20 01:30:42 +08:00
										 |  |  | // newWarmBackend instantiates the tier type specific WarmBackend, runs
 | 
					
						
							|  |  |  | // checkWarmBackend on it.
 | 
					
						
							|  |  |  | func newWarmBackend(ctx context.Context, tier madmin.TierConfig) (d WarmBackend, err error) { | 
					
						
							|  |  |  | 	switch tier.Type { | 
					
						
							|  |  |  | 	case madmin.S3: | 
					
						
							|  |  |  | 		d, err = newWarmBackendS3(*tier.S3) | 
					
						
							|  |  |  | 	case madmin.Azure: | 
					
						
							|  |  |  | 		d, err = newWarmBackendAzure(*tier.Azure) | 
					
						
							|  |  |  | 	case madmin.GCS: | 
					
						
							|  |  |  | 		d, err = newWarmBackendGCS(*tier.GCS) | 
					
						
							| 
									
										
										
										
											2022-04-12 04:24:40 +08:00
										 |  |  | 	case madmin.MinIO: | 
					
						
							|  |  |  | 		d, err = newWarmBackendMinIO(*tier.MinIO) | 
					
						
							| 
									
										
										
										
											2021-04-20 01:30:42 +08:00
										 |  |  | 	default: | 
					
						
							|  |  |  | 		return nil, errTierTypeUnsupported | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return nil, errTierTypeUnsupported | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	err = checkWarmBackend(ctx, d) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return nil, err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return d, nil | 
					
						
							|  |  |  | } |