| 
									
										
										
										
											2019-11-21 20:10:25 +08:00
										 |  |  | // Copyright 2019 The Prometheus Authors
 | 
					
						
							|  |  |  | // 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 tsdb | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							|  |  |  | 	"context" | 
					
						
							|  |  |  | 	"fmt" | 
					
						
							|  |  |  | 	"path/filepath" | 
					
						
							| 
									
										
										
										
											2020-01-30 15:12:43 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-12 00:17:59 +08:00
										 |  |  | 	"github.com/go-kit/log" | 
					
						
							| 
									
										
										
										
											2020-10-22 17:00:08 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-13 00:04:20 +08:00
										 |  |  | 	"github.com/prometheus/prometheus/storage" | 
					
						
							| 
									
										
										
										
											2021-11-29 15:54:23 +08:00
										 |  |  | 	"github.com/prometheus/prometheus/tsdb/chunkenc" | 
					
						
							| 
									
										
										
										
											2019-11-21 20:10:25 +08:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-23 22:47:11 +08:00
										 |  |  | var ErrInvalidTimes = fmt.Errorf("max time is lesser than min time") | 
					
						
							| 
									
										
										
										
											2019-11-21 20:10:25 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | // CreateBlock creates a chunkrange block from the samples passed to it, and writes it to disk.
 | 
					
						
							| 
									
										
										
										
											2020-10-13 00:04:20 +08:00
										 |  |  | func CreateBlock(series []storage.Series, dir string, chunkRange int64, logger log.Logger) (string, error) { | 
					
						
							| 
									
										
										
										
											2019-11-21 20:10:25 +08:00
										 |  |  | 	if chunkRange == 0 { | 
					
						
							|  |  |  | 		chunkRange = DefaultBlockDuration | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if chunkRange < 0 { | 
					
						
							| 
									
										
										
										
											2020-03-23 22:47:11 +08:00
										 |  |  | 		return "", ErrInvalidTimes | 
					
						
							| 
									
										
										
										
											2019-11-21 20:10:25 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2020-10-13 00:04:20 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	w, err := NewBlockWriter(logger, dir, chunkRange) | 
					
						
							| 
									
										
										
										
											2019-11-21 20:10:25 +08:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return "", err | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2020-10-13 00:04:20 +08:00
										 |  |  | 	defer func() { | 
					
						
							|  |  |  | 		if err := w.Close(); err != nil { | 
					
						
							|  |  |  | 			logger.Log("err closing blockwriter", err.Error()) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	}() | 
					
						
							| 
									
										
										
										
											2019-11-21 20:10:25 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-17 13:56:19 +08:00
										 |  |  | 	sampleCount := 0 | 
					
						
							|  |  |  | 	const commitAfter = 10000 | 
					
						
							| 
									
										
										
										
											2020-10-13 00:04:20 +08:00
										 |  |  | 	ctx := context.Background() | 
					
						
							|  |  |  | 	app := w.Appender(ctx) | 
					
						
							| 
									
										
										
										
											2022-09-21 01:16:45 +08:00
										 |  |  | 	var it chunkenc.Iterator | 
					
						
							| 
									
										
										
										
											2020-10-13 00:04:20 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	for _, s := range series { | 
					
						
							| 
									
										
										
										
											2021-11-06 18:10:04 +08:00
										 |  |  | 		ref := storage.SeriesRef(0) | 
					
						
							| 
									
										
										
										
											2022-09-21 01:16:45 +08:00
										 |  |  | 		it = s.Iterator(it) | 
					
						
							| 
									
										
										
										
											2021-02-18 20:07:00 +08:00
										 |  |  | 		lset := s.Labels() | 
					
						
							| 
									
										
										
										
											2022-08-29 18:05:03 +08:00
										 |  |  | 		typ := it.Next() | 
					
						
							|  |  |  | 		lastTyp := typ | 
					
						
							|  |  |  | 		for ; typ != chunkenc.ValNone; typ = it.Next() { | 
					
						
							|  |  |  | 			if lastTyp != typ { | 
					
						
							|  |  |  | 				// The behaviour of appender is undefined if samples of different types
 | 
					
						
							|  |  |  | 				// are appended to the same series in a single Commit().
 | 
					
						
							|  |  |  | 				if err = app.Commit(); err != nil { | 
					
						
							|  |  |  | 					return "", err | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 				app = w.Appender(ctx) | 
					
						
							|  |  |  | 				sampleCount = 0 | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			switch typ { | 
					
						
							|  |  |  | 			case chunkenc.ValFloat: | 
					
						
							|  |  |  | 				t, v := it.At() | 
					
						
							|  |  |  | 				ref, err = app.Append(ref, lset, t, v) | 
					
						
							|  |  |  | 			case chunkenc.ValHistogram: | 
					
						
							| 
									
										
										
										
											2024-01-24 00:02:14 +08:00
										 |  |  | 				t, h := it.AtHistogram(nil) | 
					
						
							| 
									
										
										
										
											2022-12-28 16:55:07 +08:00
										 |  |  | 				ref, err = app.AppendHistogram(ref, lset, t, h, nil) | 
					
						
							|  |  |  | 			case chunkenc.ValFloatHistogram: | 
					
						
							| 
									
										
										
										
											2024-01-24 00:02:14 +08:00
										 |  |  | 				t, fh := it.AtFloatHistogram(nil) | 
					
						
							| 
									
										
										
										
											2022-12-28 16:55:07 +08:00
										 |  |  | 				ref, err = app.AppendHistogram(ref, lset, t, nil, fh) | 
					
						
							| 
									
										
										
										
											2022-08-29 18:05:03 +08:00
										 |  |  | 			default: | 
					
						
							|  |  |  | 				return "", fmt.Errorf("unknown sample type %s", typ.String()) | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2020-10-13 00:04:20 +08:00
										 |  |  | 			if err != nil { | 
					
						
							|  |  |  | 				return "", err | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2022-06-17 13:56:19 +08:00
										 |  |  | 			sampleCount++ | 
					
						
							| 
									
										
										
										
											2022-08-29 18:05:03 +08:00
										 |  |  | 			lastTyp = typ | 
					
						
							| 
									
										
										
										
											2020-10-13 00:04:20 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 		if it.Err() != nil { | 
					
						
							|  |  |  | 			return "", it.Err() | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2022-06-17 13:56:19 +08:00
										 |  |  | 		// Commit and make a new appender periodically, to avoid building up data in memory.
 | 
					
						
							|  |  |  | 		if sampleCount > commitAfter { | 
					
						
							|  |  |  | 			if err = app.Commit(); err != nil { | 
					
						
							|  |  |  | 				return "", err | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			app = w.Appender(ctx) | 
					
						
							|  |  |  | 			sampleCount = 0 | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2019-11-21 20:10:25 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-13 00:04:20 +08:00
										 |  |  | 	if err = app.Commit(); err != nil { | 
					
						
							| 
									
										
										
										
											2019-11-21 20:10:25 +08:00
										 |  |  | 		return "", err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-13 00:04:20 +08:00
										 |  |  | 	ulid, err := w.Flush(ctx) | 
					
						
							| 
									
										
										
										
											2019-11-21 20:10:25 +08:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return "", err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return filepath.Join(dir, ulid.String()), nil | 
					
						
							|  |  |  | } |