| 
									
										
										
										
											2021-04-19 03:41:13 +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/>.
 | 
					
						
							| 
									
										
										
										
											2016-06-02 07:43:31 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-19 07:23:42 +08:00
										 |  |  | package cmd | 
					
						
							| 
									
										
										
										
											2016-06-02 07:43:31 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							| 
									
										
										
										
											2018-04-06 06:04:40 +08:00
										 |  |  | 	"context" | 
					
						
							| 
									
										
										
										
											2023-05-26 00:39:06 +08:00
										 |  |  | 	"fmt" | 
					
						
							| 
									
										
										
										
											2016-06-02 07:43:31 +08:00
										 |  |  | 	"io" | 
					
						
							| 
									
										
										
										
											2018-08-07 06:14:08 +08:00
										 |  |  | 	"sync" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-14 17:57:40 +08:00
										 |  |  | 	"github.com/minio/minio/internal/hash" | 
					
						
							| 
									
										
										
										
											2021-06-02 05:59:40 +08:00
										 |  |  | 	"github.com/minio/minio/internal/logger" | 
					
						
							| 
									
										
										
										
											2016-06-02 07:43:31 +08:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-17 20:58:18 +08:00
										 |  |  | // Writes in parallel to writers
 | 
					
						
							| 
									
										
										
										
											2018-08-07 06:14:08 +08:00
										 |  |  | type parallelWriter struct { | 
					
						
							| 
									
										
										
										
											2019-01-17 20:58:18 +08:00
										 |  |  | 	writers     []io.Writer | 
					
						
							| 
									
										
										
										
											2018-08-07 06:14:08 +08:00
										 |  |  | 	writeQuorum int | 
					
						
							|  |  |  | 	errs        []error | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2016-06-17 15:17:15 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-17 20:58:18 +08:00
										 |  |  | // Write writes data to writers in parallel.
 | 
					
						
							|  |  |  | func (p *parallelWriter) Write(ctx context.Context, blocks [][]byte) error { | 
					
						
							| 
									
										
										
										
											2018-08-07 06:14:08 +08:00
										 |  |  | 	var wg sync.WaitGroup | 
					
						
							| 
									
										
										
										
											2017-08-15 09:08:42 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-07 06:14:08 +08:00
										 |  |  | 	for i := range p.writers { | 
					
						
							|  |  |  | 		if p.writers[i] == nil { | 
					
						
							|  |  |  | 			p.errs[i] = errDiskNotFound | 
					
						
							|  |  |  | 			continue | 
					
						
							| 
									
										
										
										
											2016-06-02 07:43:31 +08:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2021-05-10 23:20:23 +08:00
										 |  |  | 		if p.errs[i] != nil { | 
					
						
							|  |  |  | 			continue | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2018-08-07 06:14:08 +08:00
										 |  |  | 		wg.Add(1) | 
					
						
							|  |  |  | 		go func(i int) { | 
					
						
							|  |  |  | 			defer wg.Done() | 
					
						
							| 
									
										
										
										
											2021-05-10 23:20:23 +08:00
										 |  |  | 			var n int | 
					
						
							|  |  |  | 			n, p.errs[i] = p.writers[i].Write(blocks[i]) | 
					
						
							|  |  |  | 			if p.errs[i] == nil { | 
					
						
							|  |  |  | 				if n != len(blocks[i]) { | 
					
						
							|  |  |  | 					p.errs[i] = io.ErrShortWrite | 
					
						
							| 
									
										
										
										
											2022-01-13 10:49:01 +08:00
										 |  |  | 					p.writers[i] = nil | 
					
						
							| 
									
										
										
										
											2021-05-10 23:20:23 +08:00
										 |  |  | 				} | 
					
						
							| 
									
										
										
										
											2022-01-13 10:49:01 +08:00
										 |  |  | 			} else { | 
					
						
							|  |  |  | 				p.writers[i] = nil | 
					
						
							| 
									
										
										
										
											2018-08-07 06:14:08 +08:00
										 |  |  | 			} | 
					
						
							|  |  |  | 		}(i) | 
					
						
							| 
									
										
										
										
											2016-06-02 07:43:31 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-08-07 06:14:08 +08:00
										 |  |  | 	wg.Wait() | 
					
						
							| 
									
										
										
										
											2016-06-02 07:43:31 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-07 06:14:08 +08:00
										 |  |  | 	// If nilCount >= p.writeQuorum, we return nil. This is because HealFile() uses
 | 
					
						
							|  |  |  | 	// CreateFile with p.writeQuorum=1 to accommodate healing of single disk.
 | 
					
						
							|  |  |  | 	// i.e if we do no return here in such a case, reduceWriteQuorumErrs() would
 | 
					
						
							|  |  |  | 	// return a quorum error to HealFile().
 | 
					
						
							| 
									
										
										
										
											2021-05-10 23:20:23 +08:00
										 |  |  | 	nilCount := countErrs(p.errs, nil) | 
					
						
							| 
									
										
										
										
											2018-08-07 06:14:08 +08:00
										 |  |  | 	if nilCount >= p.writeQuorum { | 
					
						
							|  |  |  | 		return nil | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2023-05-26 00:39:06 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	writeErr := reduceWriteQuorumErrs(ctx, p.errs, objectOpIgnoredErrs, p.writeQuorum) | 
					
						
							|  |  |  | 	return fmt.Errorf("%w (offline-disks=%d/%d)", writeErr, countErrs(p.errs, errDiskNotFound), len(p.writers)) | 
					
						
							| 
									
										
										
										
											2016-06-02 07:43:31 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-24 14:35:37 +08:00
										 |  |  | // Encode reads from the reader, erasure-encodes the data and writes to the writers.
 | 
					
						
							| 
									
										
										
										
											2019-01-17 20:58:18 +08:00
										 |  |  | func (e *Erasure) Encode(ctx context.Context, src io.Reader, writers []io.Writer, buf []byte, quorum int) (total int64, err error) { | 
					
						
							| 
									
										
										
										
											2018-08-07 06:14:08 +08:00
										 |  |  | 	writer := ¶llelWriter{ | 
					
						
							|  |  |  | 		writers:     writers, | 
					
						
							|  |  |  | 		writeQuorum: quorum, | 
					
						
							|  |  |  | 		errs:        make([]error, len(writers)), | 
					
						
							| 
									
										
										
										
											2016-06-02 16:49:46 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-08-07 06:14:08 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	for { | 
					
						
							|  |  |  | 		var blocks [][]byte | 
					
						
							|  |  |  | 		n, err := io.ReadFull(src, buf) | 
					
						
							| 
									
										
										
										
											2022-12-14 17:57:40 +08:00
										 |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			if !IsErrIgnored(err, []error{ | 
					
						
							|  |  |  | 				io.EOF, | 
					
						
							|  |  |  | 				io.ErrUnexpectedEOF, | 
					
						
							|  |  |  | 			}...) { | 
					
						
							|  |  |  | 				if !hash.IsChecksumMismatch(err) { | 
					
						
							|  |  |  | 					logger.LogIf(ctx, err) | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 				return 0, err | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2018-08-07 06:14:08 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 		eof := err == io.EOF || err == io.ErrUnexpectedEOF | 
					
						
							|  |  |  | 		if n == 0 && total != 0 { | 
					
						
							|  |  |  | 			// Reached EOF, nothing more to be done.
 | 
					
						
							|  |  |  | 			break | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		// We take care of the situation where if n == 0 and total == 0 by creating empty data and parity files.
 | 
					
						
							| 
									
										
										
										
											2018-08-24 14:35:37 +08:00
										 |  |  | 		blocks, err = e.EncodeData(ctx, buf[:n]) | 
					
						
							| 
									
										
										
										
											2018-08-07 06:14:08 +08:00
										 |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			logger.LogIf(ctx, err) | 
					
						
							| 
									
										
										
										
											2022-01-13 10:49:01 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-07 06:14:08 +08:00
										 |  |  | 			return 0, err | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-17 20:58:18 +08:00
										 |  |  | 		if err = writer.Write(ctx, blocks); err != nil { | 
					
						
							| 
									
										
										
										
											2018-08-07 06:14:08 +08:00
										 |  |  | 			logger.LogIf(ctx, err) | 
					
						
							|  |  |  | 			return 0, err | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		total += int64(n) | 
					
						
							|  |  |  | 		if eof { | 
					
						
							|  |  |  | 			break | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2016-06-02 16:49:46 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-08-07 06:14:08 +08:00
										 |  |  | 	return total, nil | 
					
						
							| 
									
										
										
										
											2016-06-02 07:43:31 +08:00
										 |  |  | } |