2017-04-11 02:59:45 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								// Copyright 2017 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.
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-01-02 17:34:55 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								package  tsdb  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								import  (  
						 
					
						
							
								
									
										
										
										
											2018-12-06 00:34:42 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									"context" 
							 
						 
					
						
							
								
									
										
										
										
											2020-02-25 19:12:03 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									"crypto/rand" 
							 
						 
					
						
							
								
									
										
										
										
											2023-11-17 02:54:41 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									"errors" 
							 
						 
					
						
							
								
									
										
										
										
											2018-03-15 19:26:11 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									"fmt" 
							 
						 
					
						
							
								
									
										
										
										
											2017-10-09 21:21:46 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									"io" 
							 
						 
					
						
							
								
									
										
										
										
											2024-09-10 09:41:53 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									"log/slog" 
							 
						 
					
						
							
								
									
										
										
										
											2017-01-02 17:34:55 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									"os" 
							 
						 
					
						
							
								
									
										
										
										
											2017-01-02 21:41:13 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									"path/filepath" 
							 
						 
					
						
							
								
									
										
										
										
											2024-01-16 00:24:46 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									"slices" 
							 
						 
					
						
							
								
									
										
										
										
											2017-01-03 22:43:26 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									"time" 
							 
						 
					
						
							
								
									
										
										
										
											2017-01-02 17:34:55 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-02-27 17:46:15 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									"github.com/oklog/ulid" 
							 
						 
					
						
							
								
									
										
										
										
											2017-01-03 22:43:26 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									"github.com/prometheus/client_golang/prometheus" 
							 
						 
					
						
							
								
									
										
										
										
											2024-09-10 09:41:53 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									"github.com/prometheus/common/promslog" 
							 
						 
					
						
							
								
									
										
										
										
											2020-10-22 17:00:08 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-02-06 23:58:38 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									"github.com/prometheus/prometheus/storage" 
							 
						 
					
						
							
								
									
										
										
										
											2019-08-13 16:34:14 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									"github.com/prometheus/prometheus/tsdb/chunkenc" 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									"github.com/prometheus/prometheus/tsdb/chunks" 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									tsdb_errors  "github.com/prometheus/prometheus/tsdb/errors" 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									"github.com/prometheus/prometheus/tsdb/fileutil" 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									"github.com/prometheus/prometheus/tsdb/index" 
							 
						 
					
						
							
								
									
										
										
										
											2019-09-19 17:15:41 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									"github.com/prometheus/prometheus/tsdb/tombstones" 
							 
						 
					
						
							
								
									
										
										
										
											2017-01-02 17:34:55 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								)  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-03-15 19:26:11 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								// ExponentialBlockRanges returns the time ranges based on the stepSize.
  
						 
					
						
							
								
									
										
										
										
											2017-07-07 19:46:41 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								func  ExponentialBlockRanges ( minSize  int64 ,  steps ,  stepSize  int )  [ ] int64  {  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									ranges  :=  make ( [ ] int64 ,  0 ,  steps ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									curRange  :=  minSize 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									for  i  :=  0 ;  i  <  steps ;  i ++  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										ranges  =  append ( ranges ,  curRange ) 
							 
						 
					
						
							
								
									
										
										
										
											2023-04-09 15:08:40 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										curRange  *=  int64 ( stepSize ) 
							 
						 
					
						
							
								
									
										
										
										
											2017-07-07 19:46:41 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									return  ranges 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-03-02 21:32:09 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								// Compactor provides compaction against an underlying storage
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								// of time series data.
  
						 
					
						
							
								
									
										
										
										
											2017-03-02 16:13:29 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								type  Compactor  interface  {  
						 
					
						
							
								
									
										
										
										
											2019-02-27 03:50:37 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									// Plan returns a set of directories that can be compacted concurrently.
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									// The directories can be overlapping.
 
							 
						 
					
						
							
								
									
										
										
										
											2017-03-02 21:32:09 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									// Results returned when compactions are in progress are undefined.
 
							 
						 
					
						
							
								
									
										
										
										
											2017-08-09 17:10:29 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									Plan ( dir  string )  ( [ ] string ,  error ) 
							 
						 
					
						
							
								
									
										
										
										
											2017-03-02 16:13:29 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2024-06-13 05:31:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									// Write persists one or more Blocks into a directory.
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									// No Block is written when resulting Block has 0 samples and returns an empty slice.
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									// Prometheus always return one or no block. The interface allows returning more than one
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									// block for downstream users to experiment with compactor.
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									Write ( dest  string ,  b  BlockReader ,  mint ,  maxt  int64 ,  base  * BlockMeta )  ( [ ] ulid . ULID ,  error ) 
							 
						 
					
						
							
								
									
										
										
										
											2017-03-02 16:13:29 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-03-02 21:32:09 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									// Compact runs compaction against the provided directories. Must
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									// only be called concurrently with results of Plan().
 
							 
						 
					
						
							
								
									
										
										
										
											2018-11-15 20:20:54 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									// Can optionally pass a list of already open blocks,
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									// to avoid having to reopen them.
 
							 
						 
					
						
							
								
									
										
										
										
											2024-06-13 05:31:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									// Prometheus always return one or no block. The interface allows returning more than one
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									// block for downstream users to experiment with compactor.
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									// When one resulting Block has 0 samples
 
							 
						 
					
						
							
								
									
										
										
										
											2019-01-18 16:35:16 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									//  * No block is written.
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									//  * The source dirs are marked Deletable.
 
							 
						 
					
						
							
								
									
										
										
										
											2024-06-13 05:31:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									//  * Block is not included in the result.
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									Compact ( dest  string ,  dirs  [ ] string ,  open  [ ] * Block )  ( [ ] ulid . ULID ,  error ) 
							 
						 
					
						
							
								
									
										
										
										
											2017-03-02 16:13:29 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								}  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-08-09 17:10:29 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								// LeveledCompactor implements the Compactor interface.
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								type  LeveledCompactor  struct  {  
						 
					
						
							
								
									
										
										
										
											2024-01-12 15:56:44 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									metrics                      * CompactorMetrics 
							 
						 
					
						
							
								
									
										
										
										
											2024-09-10 09:41:53 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									logger                       * slog . Logger 
							 
						 
					
						
							
								
									
										
										
										
											2024-01-12 15:56:44 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									ranges                       [ ] int64 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									chunkPool                    chunkenc . Pool 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									ctx                          context . Context 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									maxBlockChunkSegmentSize     int64 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									mergeFunc                    storage . VerticalChunkSeriesMergeFunc 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									postingsEncoder              index . PostingsEncoder 
							 
						 
					
						
							
								
									
										
										
										
											2024-11-11 14:59:24 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									postingsDecoderFactory       PostingsDecoderFactory 
							 
						 
					
						
							
								
									
										
										
										
											2024-01-12 15:56:44 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									enableOverlappingCompaction  bool 
							 
						 
					
						
							
								
									
										
										
										
											2017-01-02 17:34:55 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								}  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2023-04-04 14:31:49 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								type  CompactorMetrics  struct  {  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									Ran                prometheus . Counter 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									PopulatingBlocks   prometheus . Gauge 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									OverlappingBlocks  prometheus . Counter 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									Duration           prometheus . Histogram 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									ChunkSize          prometheus . Histogram 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									ChunkSamples       prometheus . Histogram 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									ChunkRange         prometheus . Histogram 
							 
						 
					
						
							
								
									
										
										
										
											2017-01-03 22:43:26 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								}  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2024-04-01 06:10:29 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								// NewCompactorMetrics initializes metrics for Compactor.
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								func  NewCompactorMetrics ( r  prometheus . Registerer )  * CompactorMetrics  {  
						 
					
						
							
								
									
										
										
										
											2023-04-04 14:31:49 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									m  :=  & CompactorMetrics { } 
							 
						 
					
						
							
								
									
										
										
										
											2017-01-03 22:43:26 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2023-04-04 14:31:49 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									m . Ran  =  prometheus . NewCounter ( prometheus . CounterOpts { 
							 
						 
					
						
							
								
									
										
										
										
											2018-09-19 01:17:41 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
										Name :  "prometheus_tsdb_compactions_total" , 
							 
						 
					
						
							
								
									
										
										
										
											2017-01-06 18:40:09 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
										Help :  "Total number of compactions that were executed for the partition." , 
							 
						 
					
						
							
								
									
										
										
										
											2017-01-03 22:43:26 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									} ) 
							 
						 
					
						
							
								
									
										
										
										
											2023-04-04 14:31:49 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									m . PopulatingBlocks  =  prometheus . NewGauge ( prometheus . GaugeOpts { 
							 
						 
					
						
							
								
									
										
										
										
											2018-12-07 23:49:23 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
										Name :  "prometheus_tsdb_compaction_populating_block" , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										Help :  "Set to 1 when a block is currently being written to the disk." , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									} ) 
							 
						 
					
						
							
								
									
										
										
										
											2023-04-04 14:31:49 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									m . OverlappingBlocks  =  prometheus . NewCounter ( prometheus . CounterOpts { 
							 
						 
					
						
							
								
									
										
										
										
											2019-02-14 21:29:41 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
										Name :  "prometheus_tsdb_vertical_compactions_total" , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										Help :  "Total number of compactions done on overlapping blocks." , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									} ) 
							 
						 
					
						
							
								
									
										
										
										
											2023-04-04 14:31:49 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									m . Duration  =  prometheus . NewHistogram ( prometheus . HistogramOpts { 
							 
						 
					
						
							
								
									
										
										
										
											2024-03-01 21:04:54 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										Name :                             "prometheus_tsdb_compaction_duration_seconds" , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										Help :                             "Duration of compaction runs" , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										Buckets :                          prometheus . ExponentialBuckets ( 1 ,  2 ,  14 ) , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										NativeHistogramBucketFactor :      1.1 , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										NativeHistogramMaxBucketNumber :   100 , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										NativeHistogramMinResetDuration :  1  *  time . Hour , 
							 
						 
					
						
							
								
									
										
										
										
											2017-09-01 22:10:10 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									} ) 
							 
						 
					
						
							
								
									
										
										
										
											2023-04-04 14:31:49 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									m . ChunkSize  =  prometheus . NewHistogram ( prometheus . HistogramOpts { 
							 
						 
					
						
							
								
									
										
										
										
											2018-09-19 01:17:41 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
										Name :     "prometheus_tsdb_compaction_chunk_size_bytes" , 
							 
						 
					
						
							
								
									
										
										
										
											2017-09-01 22:10:10 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
										Help :     "Final size of chunks on their first compaction" , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										Buckets :  prometheus . ExponentialBuckets ( 32 ,  1.5 ,  12 ) , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									} ) 
							 
						 
					
						
							
								
									
										
										
										
											2023-04-04 14:31:49 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									m . ChunkSamples  =  prometheus . NewHistogram ( prometheus . HistogramOpts { 
							 
						 
					
						
							
								
									
										
										
										
											2018-09-19 01:17:41 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
										Name :     "prometheus_tsdb_compaction_chunk_samples" , 
							 
						 
					
						
							
								
									
										
										
										
											2017-09-01 22:10:10 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
										Help :     "Final number of samples on their first compaction" , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										Buckets :  prometheus . ExponentialBuckets ( 4 ,  1.5 ,  12 ) , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									} ) 
							 
						 
					
						
							
								
									
										
										
										
											2023-04-04 14:31:49 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									m . ChunkRange  =  prometheus . NewHistogram ( prometheus . HistogramOpts { 
							 
						 
					
						
							
								
									
										
										
										
											2018-09-19 01:17:41 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
										Name :     "prometheus_tsdb_compaction_chunk_range_seconds" , 
							 
						 
					
						
							
								
									
										
										
										
											2017-09-01 22:10:10 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
										Help :     "Final time range of chunks on their first compaction" , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										Buckets :  prometheus . ExponentialBuckets ( 100 ,  4 ,  10 ) , 
							 
						 
					
						
							
								
									
										
										
										
											2017-01-03 22:43:26 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									} ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-01-06 18:40:09 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									if  r  !=  nil  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										r . MustRegister ( 
							 
						 
					
						
							
								
									
										
										
										
											2023-04-04 14:31:49 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
											m . Ran , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											m . PopulatingBlocks , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											m . OverlappingBlocks , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											m . Duration , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											m . ChunkRange , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											m . ChunkSamples , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											m . ChunkSize , 
							 
						 
					
						
							
								
									
										
										
										
											2017-01-06 18:40:09 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
										) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
									
										
										
										
											2017-01-03 22:43:26 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									return  m 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2024-01-08 17:48:27 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								type  LeveledCompactorOptions  struct  {  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									// PE specifies the postings encoder. It is called when compactor is writing out the postings for a label name/value pair during compaction.
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									// If it is nil then the default encoder is used. At the moment that is the "raw" encoder. See index.EncodePostingsRaw for more.
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									PE  index . PostingsEncoder 
							 
						 
					
						
							
								
									
										
										
										
											2024-11-11 14:59:24 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									// PD specifies the postings decoder factory to return different postings decoder based on BlockMeta. It is called when opening a block or opening the index file.
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									// If it is nil then a default decoder is used, compatible with Prometheus v2.
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									PD  PostingsDecoderFactory 
							 
						 
					
						
							
								
									
										
										
										
											2024-01-08 17:48:27 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									// MaxBlockChunkSegmentSize is the max block chunk segment size. If it is 0 then the default chunks.DefaultChunkSegmentSize is used.
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									MaxBlockChunkSegmentSize  int64 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									// MergeFunc is used for merging series together in vertical compaction. By default storage.NewCompactingChunkSeriesMerger(storage.ChainedSeriesMerge) is used.
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									MergeFunc  storage . VerticalChunkSeriesMergeFunc 
							 
						 
					
						
							
								
									
										
										
										
											2024-01-12 15:56:44 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									// EnableOverlappingCompaction enables compaction of overlapping blocks. In Prometheus it is always enabled.
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									// It is useful for downstream projects like Mimir, Cortex, Thanos where they have a separate component that does compaction.
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									EnableOverlappingCompaction  bool 
							 
						 
					
						
							
								
									
										
										
										
											2021-04-15 16:55:01 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								}  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2024-11-11 14:59:24 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								type  PostingsDecoderFactory  func ( meta  * BlockMeta )  index . PostingsDecoder  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								func  DefaultPostingsDecoderFactory ( _  * BlockMeta )  index . PostingsDecoder  {  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									return  index . DecodePostingsRaw 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2024-09-10 09:41:53 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								func  NewLeveledCompactorWithChunkSize ( ctx  context . Context ,  r  prometheus . Registerer ,  l  * slog . Logger ,  ranges  [ ] int64 ,  pool  chunkenc . Pool ,  maxBlockChunkSegmentSize  int64 ,  mergeFunc  storage . VerticalChunkSeriesMergeFunc )  ( * LeveledCompactor ,  error )  {  
						 
					
						
							
								
									
										
										
										
											2024-01-08 17:48:27 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									return  NewLeveledCompactorWithOptions ( ctx ,  r ,  l ,  ranges ,  pool ,  LeveledCompactorOptions { 
							 
						 
					
						
							
								
									
										
										
										
											2024-01-12 15:56:44 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										MaxBlockChunkSegmentSize :     maxBlockChunkSegmentSize , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										MergeFunc :                    mergeFunc , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										EnableOverlappingCompaction :  true , 
							 
						 
					
						
							
								
									
										
										
										
											2024-01-08 17:48:27 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									} ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2024-09-10 09:41:53 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								func  NewLeveledCompactor ( ctx  context . Context ,  r  prometheus . Registerer ,  l  * slog . Logger ,  ranges  [ ] int64 ,  pool  chunkenc . Pool ,  mergeFunc  storage . VerticalChunkSeriesMergeFunc )  ( * LeveledCompactor ,  error )  {  
						 
					
						
							
								
									
										
										
										
											2024-01-08 17:48:27 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									return  NewLeveledCompactorWithOptions ( ctx ,  r ,  l ,  ranges ,  pool ,  LeveledCompactorOptions { 
							 
						 
					
						
							
								
									
										
										
										
											2024-01-12 15:56:44 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										MergeFunc :                    mergeFunc , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										EnableOverlappingCompaction :  true , 
							 
						 
					
						
							
								
									
										
										
										
											2024-01-08 17:48:27 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									} ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2024-09-10 09:41:53 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								func  NewLeveledCompactorWithOptions ( ctx  context . Context ,  r  prometheus . Registerer ,  l  * slog . Logger ,  ranges  [ ] int64 ,  pool  chunkenc . Pool ,  opts  LeveledCompactorOptions )  ( * LeveledCompactor ,  error )  {  
						 
					
						
							
								
									
										
										
										
											2017-09-01 17:46:46 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									if  len ( ranges )  ==  0  { 
							 
						 
					
						
							
								
									
										
										
										
											2024-11-03 20:15:51 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										return  nil ,  errors . New ( "at least one range must be provided" ) 
							 
						 
					
						
							
								
									
										
										
										
											2017-08-08 23:35:34 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
									
										
										
										
											2017-09-01 17:46:46 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									if  pool  ==  nil  { 
							 
						 
					
						
							
								
									
										
										
										
											2017-11-30 22:34:49 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
										pool  =  chunkenc . NewPool ( ) 
							 
						 
					
						
							
								
									
										
										
										
											2017-01-02 17:34:55 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
									
										
										
										
											2019-02-14 21:29:41 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									if  l  ==  nil  { 
							 
						 
					
						
							
								
									
										
										
										
											2024-09-10 09:41:53 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										l  =  promslog . NewNopLogger ( ) 
							 
						 
					
						
							
								
									
										
										
										
											2019-02-14 21:29:41 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
									
										
										
										
											2024-01-11 18:07:54 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									mergeFunc  :=  opts . MergeFunc 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									if  mergeFunc  ==  nil  { 
							 
						 
					
						
							
								
									
										
										
										
											2021-05-19 00:38:37 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										mergeFunc  =  storage . NewCompactingChunkSeriesMerger ( storage . ChainedSeriesMerge ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
									
										
										
										
											2024-01-16 19:00:53 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									maxBlockChunkSegmentSize  :=  opts . MaxBlockChunkSegmentSize 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									if  maxBlockChunkSegmentSize  ==  0  { 
							 
						 
					
						
							
								
									
										
										
										
											2024-01-08 17:48:27 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										maxBlockChunkSegmentSize  =  chunks . DefaultChunkSegmentSize 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
									
										
										
										
											2024-01-16 19:00:53 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									pe  :=  opts . PE 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									if  pe  ==  nil  { 
							 
						 
					
						
							
								
									
										
										
										
											2024-01-08 17:48:27 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										pe  =  index . EncodePostingsRaw 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
									
										
										
										
											2017-09-01 17:46:46 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									return  & LeveledCompactor { 
							 
						 
					
						
							
								
									
										
										
										
											2024-01-12 15:56:44 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										ranges :                       ranges , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										chunkPool :                    pool , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										logger :                       l , 
							 
						 
					
						
							
								
									
										
										
										
											2024-04-01 06:10:29 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										metrics :                      NewCompactorMetrics ( r ) , 
							 
						 
					
						
							
								
									
										
										
										
											2024-01-12 15:56:44 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										ctx :                          ctx , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										maxBlockChunkSegmentSize :     maxBlockChunkSegmentSize , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										mergeFunc :                    mergeFunc , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										postingsEncoder :              pe , 
							 
						 
					
						
							
								
									
										
										
										
											2024-11-11 14:59:24 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										postingsDecoderFactory :       opts . PD , 
							 
						 
					
						
							
								
									
										
										
										
											2024-01-12 15:56:44 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										enableOverlappingCompaction :  opts . EnableOverlappingCompaction , 
							 
						 
					
						
							
								
									
										
										
										
											2017-09-01 17:46:46 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									} ,  nil 
							 
						 
					
						
							
								
									
										
										
										
											2017-01-20 14:58:19 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								}  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-05-18 22:09:30 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								type  dirMeta  struct  {  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									dir   string 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									meta  * BlockMeta 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-08-09 17:10:29 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								// Plan returns a list of compactable blocks in the provided directory.
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								func  ( c  * LeveledCompactor )  Plan ( dir  string )  ( [ ] string ,  error )  {  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									dirs ,  err  :=  blockDirs ( dir ) 
							 
						 
					
						
							
								
									
										
										
										
											2017-03-02 16:13:29 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									if  err  !=  nil  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										return  nil ,  err 
							 
						 
					
						
							
								
									
										
										
										
											2017-01-03 22:43:26 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
									
										
										
										
											2018-02-28 19:04:55 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									if  len ( dirs )  <  1  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										return  nil ,  nil 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
									
										
										
										
											2017-01-18 13:18:32 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-05-18 23:30:52 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									var  dms  [ ] dirMeta 
							 
						 
					
						
							
								
									
										
										
										
											2017-03-02 16:13:29 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									for  _ ,  dir  :=  range  dirs  { 
							 
						 
					
						
							
								
									
										
										
										
											2019-06-24 23:42:29 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
										meta ,  _ ,  err  :=  readMetaFile ( dir ) 
							 
						 
					
						
							
								
									
										
										
										
											2017-03-02 16:13:29 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
										if  err  !=  nil  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											return  nil ,  err 
							 
						 
					
						
							
								
									
										
										
										
											2017-01-06 20:53:05 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
										} 
							 
						 
					
						
							
								
									
										
										
										
											2017-09-01 17:46:46 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
										dms  =  append ( dms ,  dirMeta { dir ,  meta } ) 
							 
						 
					
						
							
								
									
										
										
										
											2017-03-02 16:13:29 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
									
										
										
										
											2017-08-13 16:41:08 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									return  c . plan ( dms ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								func  ( c  * LeveledCompactor )  plan ( dms  [ ] dirMeta )  ( [ ] string ,  error )  {  
						 
					
						
							
								
									
										
										
										
											2023-09-22 04:53:51 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									slices . SortFunc ( dms ,  func ( a ,  b  dirMeta )  int  { 
							 
						 
					
						
							
								
									
										
										
										
											2023-10-16 22:23:26 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										switch  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										case  a . meta . MinTime  <  b . meta . MinTime : 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											return  - 1 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										case  a . meta . MinTime  >  b . meta . MinTime : 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											return  1 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										default : 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											return  0 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										} 
							 
						 
					
						
							
								
									
										
										
										
											2017-09-01 17:46:46 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									} ) 
							 
						 
					
						
							
								
									
										
										
										
											2017-01-30 16:42:38 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2019-02-14 21:29:41 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									res  :=  c . selectOverlappingDirs ( dms ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									if  len ( res )  >  0  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										return  res ,  nil 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									// No overlapping blocks, do compaction the usual way.
 
							 
						 
					
						
							
								
									
										
										
										
											2018-03-13 22:11:02 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									// We do not include a recently created block with max(minTime), so the block which was just created from WAL.
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									// This gives users a window of a full block size to piece-wise backup new data without having to care about data overlap.
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									dms  =  dms [ : len ( dms ) - 1 ] 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-08-09 17:10:29 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									for  _ ,  dm  :=  range  c . selectDirs ( dms )  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										res  =  append ( res ,  dm . dir ) 
							 
						 
					
						
							
								
									
										
										
										
											2017-01-06 20:53:05 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
									
										
										
										
											2017-08-09 17:10:29 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									if  len ( res )  >  0  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										return  res ,  nil 
							 
						 
					
						
							
								
									
										
										
										
											2017-07-13 00:16:12 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2019-01-18 16:35:16 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									// Compact any blocks with big enough time range that have >5% tombstones.
 
							 
						 
					
						
							
								
									
										
										
										
											2017-07-13 00:16:12 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									for  i  :=  len ( dms )  -  1 ;  i  >=  0 ;  i --  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										meta  :=  dms [ i ] . meta 
							 
						 
					
						
							
								
									
										
										
										
											2017-09-01 17:46:46 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
										if  meta . MaxTime - meta . MinTime  <  c . ranges [ len ( c . ranges ) / 2 ]  { 
							 
						 
					
						
							
								
									
										
										
										
											2020-11-25 21:03:30 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
											// If the block is entirely deleted, then we don't care about the block being big enough.
 
							 
						 
					
						
							
								
									
										
										
										
											2024-05-08 23:57:09 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
											// TODO: This is assuming a single tombstone is for a distinct series, which might not be true.
 
							 
						 
					
						
							
								
									
										
										
										
											2020-11-25 21:03:30 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
											if  meta . Stats . NumTombstones  >  0  &&  meta . Stats . NumTombstones  >=  meta . Stats . NumSeries  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
												return  [ ] string { dms [ i ] . dir } ,  nil 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											} 
							 
						 
					
						
							
								
									
										
										
										
											2017-07-13 00:16:12 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
											break 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										} 
							 
						 
					
						
							
								
									
										
										
										
											2017-09-01 17:46:46 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
										if  float64 ( meta . Stats . NumTombstones ) / float64 ( meta . Stats . NumSeries + 1 )  >  0.05  { 
							 
						 
					
						
							
								
									
										
										
										
											2017-08-09 17:10:29 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
											return  [ ] string { dms [ i ] . dir } ,  nil 
							 
						 
					
						
							
								
									
										
										
										
											2017-07-13 00:16:12 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
										} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									return  nil ,  nil 
							 
						 
					
						
							
								
									
										
										
										
											2017-07-07 03:29:26 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								}  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-08-04 00:33:13 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								// selectDirs returns the dir metas that should be compacted into a single new block.
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								// If only a single block range is configured, the result is always nil.
  
						 
					
						
							
								
									
										
										
										
											2017-08-09 17:10:29 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								func  ( c  * LeveledCompactor )  selectDirs ( ds  [ ] dirMeta )  [ ] dirMeta  {  
						 
					
						
							
								
									
										
										
										
											2017-09-01 17:46:46 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									if  len ( c . ranges )  <  2  ||  len ( ds )  <  1  { 
							 
						 
					
						
							
								
									
										
										
										
											2017-07-07 19:46:41 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
										return  nil 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-08-04 00:33:13 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									highTime  :=  ds [ len ( ds ) - 1 ] . meta . MinTime 
							 
						 
					
						
							
								
									
										
										
										
											2017-07-07 03:29:26 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-09-01 17:46:46 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									for  _ ,  iv  :=  range  c . ranges [ 1 : ]  { 
							 
						 
					
						
							
								
									
										
										
										
											2017-08-04 00:33:13 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
										parts  :=  splitByRange ( ds ,  iv ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										if  len ( parts )  ==  0  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											continue 
							 
						 
					
						
							
								
									
										
										
										
											2017-01-03 22:43:26 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
										} 
							 
						 
					
						
							
								
									
										
										
										
											2017-01-30 16:42:38 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-11-21 19:15:02 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									Outer : 
							 
						 
					
						
							
								
									
										
										
										
											2017-08-04 00:33:13 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
										for  _ ,  p  :=  range  parts  { 
							 
						 
					
						
							
								
									
										
										
										
											2018-03-15 19:26:11 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
											// Do not select the range if it has a block whose compaction failed.
 
							 
						 
					
						
							
								
									
										
										
										
											2017-11-21 19:15:02 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
											for  _ ,  dm  :=  range  p  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
												if  dm . meta . Compaction . Failed  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
													continue  Outer 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
												} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-08-04 00:33:13 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
											mint  :=  p [ 0 ] . meta . MinTime 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											maxt  :=  p [ len ( p ) - 1 ] . meta . MaxTime 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											// Pick the range of blocks if it spans the full range (potentially with gaps)
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											// or is before the most recent block.
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											// This ensures we don't compact blocks prematurely when another one of the same
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											// size still fits in the range.
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											if  ( maxt - mint  ==  iv  ||  maxt  <=  highTime )  &&  len ( p )  >  1  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
												return  p 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											} 
							 
						 
					
						
							
								
									
										
										
										
											2017-07-07 03:29:26 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
										} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-08-04 00:33:13 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									return  nil 
							 
						 
					
						
							
								
									
										
										
										
											2017-01-05 04:11:15 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								}  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2019-04-08 20:27:06 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								// selectOverlappingDirs returns all dirs with overlapping time ranges.
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								// It expects sorted input by mint and returns the overlapping dirs in the same order as received.
  
						 
					
						
							
								
									
										
										
										
											2019-02-14 21:29:41 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								func  ( c  * LeveledCompactor )  selectOverlappingDirs ( ds  [ ] dirMeta )  [ ] string  {  
						 
					
						
							
								
									
										
										
										
											2024-01-12 15:56:44 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									if  ! c . enableOverlappingCompaction  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										return  nil 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
									
										
										
										
											2019-02-14 21:29:41 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									if  len ( ds )  <  2  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										return  nil 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									var  overlappingDirs  [ ] string 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									globalMaxt  :=  ds [ 0 ] . meta . MaxTime 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									for  i ,  d  :=  range  ds [ 1 : ]  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										if  d . meta . MinTime  <  globalMaxt  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											if  len ( overlappingDirs )  ==  0  {  // When it is the first overlap, need to add the last one as well.
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
												overlappingDirs  =  append ( overlappingDirs ,  ds [ i ] . dir ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											overlappingDirs  =  append ( overlappingDirs ,  d . dir ) 
							 
						 
					
						
							
								
									
										
										
										
											2019-04-08 20:27:06 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
										}  else  if  len ( overlappingDirs )  >  0  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											break 
							 
						 
					
						
							
								
									
										
										
										
											2019-02-14 21:29:41 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
										} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										if  d . meta . MaxTime  >  globalMaxt  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											globalMaxt  =  d . meta . MaxTime 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									return  overlappingDirs 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-08-04 00:33:13 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								// splitByRange splits the directories by the time range. The range sequence starts at 0.
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								//
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								// For example, if we have blocks [0-10, 10-20, 50-60, 90-100] and the split range tr is 30
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								// it returns [0-10, 10-20], [50-60], [90-100].
  
						 
					
						
							
								
									
										
										
										
											2017-07-07 03:29:26 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								func  splitByRange ( ds  [ ] dirMeta ,  tr  int64 )  [ ] [ ] dirMeta  {  
						 
					
						
							
								
									
										
										
										
											2017-07-07 19:46:41 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									var  splitDirs  [ ] [ ] dirMeta 
							 
						 
					
						
							
								
									
										
										
										
											2017-07-07 03:29:26 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-07-07 19:46:41 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									for  i  :=  0 ;  i  <  len ( ds ) ;  { 
							 
						 
					
						
							
								
									
										
										
										
											2017-08-04 00:33:13 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
										var  ( 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											group  [ ] dirMeta 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											t0     int64 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											m      =  ds [ i ] . meta 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										) 
							 
						 
					
						
							
								
									
										
										
										
											2017-07-07 19:46:41 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
										// Compute start of aligned time range of size tr closest to the current block's start.
 
							 
						 
					
						
							
								
									
										
										
										
											2017-07-13 22:13:59 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
										if  m . MinTime  >=  0  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											t0  =  tr  *  ( m . MinTime  /  tr ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										}  else  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											t0  =  tr  *  ( ( m . MinTime  -  tr  +  1 )  /  tr ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										// Skip blocks that don't fall into the range. This can happen via mis-alignment or
 
							 
						 
					
						
							
								
									
										
										
										
											2024-05-08 23:57:09 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										// by being a multiple of the intended range.
 
							 
						 
					
						
							
								
									
										
										
										
											2019-03-18 22:14:10 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
										if  m . MaxTime  >  t0 + tr  { 
							 
						 
					
						
							
								
									
										
										
										
											2017-07-13 22:13:59 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
											i ++ 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											continue 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										} 
							 
						 
					
						
							
								
									
										
										
										
											2017-01-20 02:45:52 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-07-07 19:46:41 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
										// Add all dirs to the current group that are within [t0, t0+tr].
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										for  ;  i  <  len ( ds ) ;  i ++  { 
							 
						 
					
						
							
								
									
										
										
										
											2017-07-13 22:13:59 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
											// Either the block falls into the next range or doesn't fit at all (checked above).
 
							 
						 
					
						
							
								
									
										
										
										
											2019-03-18 22:14:10 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
											if  ds [ i ] . meta . MaxTime  >  t0 + tr  { 
							 
						 
					
						
							
								
									
										
										
										
											2017-07-07 19:46:41 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
												break 
							 
						 
					
						
							
								
									
										
										
										
											2017-07-07 03:29:26 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
											} 
							 
						 
					
						
							
								
									
										
										
										
											2017-07-07 19:46:41 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
											group  =  append ( group ,  ds [ i ] ) 
							 
						 
					
						
							
								
									
										
										
										
											2017-01-20 02:45:52 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
										} 
							 
						 
					
						
							
								
									
										
										
										
											2017-07-07 03:29:26 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-07-07 19:46:41 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
										if  len ( group )  >  0  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											splitDirs  =  append ( splitDirs ,  group ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										} 
							 
						 
					
						
							
								
									
										
										
										
											2017-07-07 03:29:26 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									return  splitDirs 
							 
						 
					
						
							
								
									
										
										
										
											2017-01-03 17:09:20 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								}  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2024-05-08 23:57:09 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								// CompactBlockMetas merges many block metas into one, combining its source blocks together
  
						 
					
						
							
								
									
										
										
										
											2022-01-05 17:40:00 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								// and adjusting compaction level. Min/Max time of result block meta covers all input blocks.
  
						 
					
						
							
								
									
										
										
										
											2020-11-10 00:51:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								func  CompactBlockMetas ( uid  ulid . ULID ,  blocks  ... * BlockMeta )  * BlockMeta  {  
						 
					
						
							
								
									
										
										
										
											2017-08-29 06:39:17 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									res  :=  & BlockMeta { 
							 
						 
					
						
							
								
									
										
										
										
											2022-01-05 17:40:00 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										ULID :  uid , 
							 
						 
					
						
							
								
									
										
										
										
											2017-08-29 06:39:17 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
									
										
										
										
											2017-01-29 15:11:47 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-06-07 15:52:20 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									sources  :=  map [ ulid . ULID ] struct { } { } 
							 
						 
					
						
							
								
									
										
										
										
											2022-01-05 17:40:00 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									mint  :=  blocks [ 0 ] . MinTime 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									maxt  :=  blocks [ 0 ] . MaxTime 
							 
						 
					
						
							
								
									
										
										
										
											2017-01-03 22:43:26 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									for  _ ,  b  :=  range  blocks  { 
							 
						 
					
						
							
								
									
										
										
										
											2022-01-05 17:40:00 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										if  b . MinTime  <  mint  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											mint  =  b . MinTime 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										} 
							 
						 
					
						
							
								
									
										
										
										
											2019-02-14 21:29:41 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
										if  b . MaxTime  >  maxt  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											maxt  =  b . MaxTime 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										} 
							 
						 
					
						
							
								
									
										
										
										
											2017-08-09 17:10:29 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
										if  b . Compaction . Level  >  res . Compaction . Level  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											res . Compaction . Level  =  b . Compaction . Level 
							 
						 
					
						
							
								
									
										
										
										
											2017-06-07 15:52:20 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
										} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										for  _ ,  s  :=  range  b . Compaction . Sources  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											sources [ s ]  =  struct { } { } 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										} 
							 
						 
					
						
							
								
									
										
										
										
											2018-06-27 21:47:11 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
										res . Compaction . Parents  =  append ( res . Compaction . Parents ,  BlockDesc { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											ULID :     b . ULID , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											MinTime :  b . MinTime , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											MaxTime :  b . MaxTime , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										} ) 
							 
						 
					
						
							
								
									
										
										
										
											2017-01-02 17:34:55 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
									
										
										
										
											2017-08-09 17:10:29 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									res . Compaction . Level ++ 
							 
						 
					
						
							
								
									
										
										
										
											2017-06-07 15:52:20 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									for  s  :=  range  sources  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										res . Compaction . Sources  =  append ( res . Compaction . Sources ,  s ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
									
										
										
										
											2023-09-22 04:53:51 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									slices . SortFunc ( res . Compaction . Sources ,  func ( a ,  b  ulid . ULID )  int  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										return  a . Compare ( b ) 
							 
						 
					
						
							
								
									
										
										
										
											2017-06-07 15:52:20 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									} ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2022-01-05 17:40:00 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									res . MinTime  =  mint 
							 
						 
					
						
							
								
									
										
										
										
											2019-02-14 21:29:41 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									res . MaxTime  =  maxt 
							 
						 
					
						
							
								
									
										
										
										
											2017-01-03 22:43:26 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									return  res 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}  
						 
					
						
							
								
									
										
										
										
											2017-01-02 17:34:55 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-08-09 17:10:29 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								// Compact creates a new block in the compactor's directory from the blocks in the
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								// provided directories.
  
						 
					
						
							
								
									
										
										
										
											2024-06-13 05:31:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								func  ( c  * LeveledCompactor )  Compact ( dest  string ,  dirs  [ ] string ,  open  [ ] * Block )  ( [ ] ulid . ULID ,  error )  {  
						 
					
						
							
								
									
										
										
										
											2023-04-13 05:18:20 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									return  c . CompactWithBlockPopulator ( dest ,  dirs ,  open ,  DefaultBlockPopulator { } ) 
							 
						 
					
						
							
								
									
										
										
										
											2023-04-04 14:31:49 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								}  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2024-06-13 05:31:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								func  ( c  * LeveledCompactor )  CompactWithBlockPopulator ( dest  string ,  dirs  [ ] string ,  open  [ ] * Block ,  blockPopulator  BlockPopulator )  ( [ ] ulid . ULID ,  error )  {  
						 
					
						
							
								
									
										
										
										
											2018-03-15 19:26:11 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									var  ( 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										blocks  [ ] BlockReader 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										bs      [ ] * Block 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										metas   [ ] * BlockMeta 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										uids    [ ] string 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									) 
							 
						 
					
						
							
								
									
										
										
										
											2018-11-15 20:20:54 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									start  :=  time . Now ( ) 
							 
						 
					
						
							
								
									
										
										
										
											2017-03-02 21:32:09 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-03-02 16:13:29 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									for  _ ,  d  :=  range  dirs  { 
							 
						 
					
						
							
								
									
										
										
										
											2019-06-24 23:42:29 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
										meta ,  _ ,  err  :=  readMetaFile ( d ) 
							 
						 
					
						
							
								
									
										
										
										
											2017-03-02 16:13:29 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
										if  err  !=  nil  { 
							 
						 
					
						
							
								
									
										
										
										
											2024-06-13 05:31:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
											return  nil ,  err 
							 
						 
					
						
							
								
									
										
										
										
											2017-03-02 16:13:29 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
										} 
							 
						 
					
						
							
								
									
										
										
										
											2017-03-06 16:33:55 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-11-15 20:20:54 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
										var  b  * Block 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										// Use already open blocks if we can, to avoid
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										// having the index data in memory twice.
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										for  _ ,  o  :=  range  open  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											if  meta . ULID  ==  o . Meta ( ) . ULID  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
												b  =  o 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
												break 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										if  b  ==  nil  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											var  err  error 
							 
						 
					
						
							
								
									
										
										
										
											2024-11-11 14:59:24 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
											b ,  err  =  OpenBlock ( c . logger ,  d ,  c . chunkPool ,  c . postingsDecoderFactory ) 
							 
						 
					
						
							
								
									
										
										
										
											2018-11-15 20:20:54 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
											if  err  !=  nil  { 
							 
						 
					
						
							
								
									
										
										
										
											2024-06-13 05:31:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
												return  nil ,  err 
							 
						 
					
						
							
								
									
										
										
										
											2018-11-15 20:20:54 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
											} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											defer  b . Close ( ) 
							 
						 
					
						
							
								
									
										
										
										
											2017-08-29 06:39:17 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
										} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										metas  =  append ( metas ,  meta ) 
							 
						 
					
						
							
								
									
										
										
										
											2017-03-02 16:13:29 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
										blocks  =  append ( blocks ,  b ) 
							 
						 
					
						
							
								
									
										
										
										
											2017-11-21 19:15:02 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
										bs  =  append ( bs ,  b ) 
							 
						 
					
						
							
								
									
										
										
										
											2018-03-15 19:26:11 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
										uids  =  append ( uids ,  meta . ULID . String ( ) ) 
							 
						 
					
						
							
								
									
										
										
										
											2017-03-02 16:13:29 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2024-06-13 05:31:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									uid  :=  ulid . MustNew ( ulid . Now ( ) ,  rand . Reader ) 
							 
						 
					
						
							
								
									
										
										
										
											2017-05-18 22:09:30 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-11-10 00:51:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									meta  :=  CompactBlockMetas ( uid ,  metas ... ) 
							 
						 
					
						
							
								
									
										
										
										
											2024-06-13 05:31:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									err  :=  c . write ( dest ,  meta ,  blockPopulator ,  blocks ... ) 
							 
						 
					
						
							
								
									
										
										
										
											2017-11-21 19:15:02 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									if  err  ==  nil  { 
							 
						 
					
						
							
								
									
										
										
										
											2019-01-18 16:35:16 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
										if  meta . Stats . NumSamples  ==  0  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											for  _ ,  b  :=  range  bs  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
												b . meta . Compaction . Deletable  =  true 
							 
						 
					
						
							
								
									
										
										
										
											2019-06-24 23:42:29 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
												n ,  err  :=  writeMetaFile ( c . logger ,  b . dir ,  & b . meta ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
												if  err  !=  nil  { 
							 
						 
					
						
							
								
									
										
										
										
											2024-09-10 09:41:53 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
													c . logger . Error ( 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
														"Failed to write 'Deletable' to meta file after compaction" , 
							 
						 
					
						
							
								
									
										
										
										
											2019-01-18 16:35:16 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
														"ulid" ,  b . meta . ULID , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
													) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
												} 
							 
						 
					
						
							
								
									
										
										
										
											2019-06-24 23:42:29 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
												b . numBytesMeta  =  n 
							 
						 
					
						
							
								
									
										
										
										
											2019-01-18 16:35:16 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
											} 
							 
						 
					
						
							
								
									
										
										
										
											2024-09-10 09:41:53 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
											c . logger . Info ( 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
												"compact blocks resulted in empty block" , 
							 
						 
					
						
							
								
									
										
										
										
											2019-01-18 16:35:16 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
												"count" ,  len ( blocks ) , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
												"sources" ,  fmt . Sprintf ( "%v" ,  uids ) , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
												"duration" ,  time . Since ( start ) , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											) 
							 
						 
					
						
							
								
									
										
										
										
											2024-06-13 05:31:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
											return  nil ,  nil 
							 
						 
					
						
							
								
									
										
										
										
											2019-01-18 16:35:16 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
										} 
							 
						 
					
						
							
								
									
										
										
										
											2024-06-13 05:31:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2024-09-10 09:41:53 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										c . logger . Info ( 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											"compact blocks" , 
							 
						 
					
						
							
								
									
										
										
										
											2024-06-13 05:31:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
											"count" ,  len ( blocks ) , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											"mint" ,  meta . MinTime , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											"maxt" ,  meta . MaxTime , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											"ulid" ,  meta . ULID , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											"sources" ,  fmt . Sprintf ( "%v" ,  uids ) , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											"duration" ,  time . Since ( start ) , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										return  [ ] ulid . ULID { uid } ,  nil 
							 
						 
					
						
							
								
									
										
										
										
											2017-11-21 19:15:02 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-10-28 23:24:58 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									errs  :=  tsdb_errors . NewMulti ( err ) 
							 
						 
					
						
							
								
									
										
										
										
											2023-03-23 18:10:00 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									if  ! errors . Is ( err ,  context . Canceled )  { 
							 
						 
					
						
							
								
									
										
										
										
											2019-01-24 19:33:12 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
										for  _ ,  b  :=  range  bs  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											if  err  :=  b . setCompactionFailed ( ) ;  err  !=  nil  { 
							 
						 
					
						
							
								
									
										
										
										
											2023-11-17 02:54:41 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
												errs . Add ( fmt . Errorf ( "setting compaction failed for block: %s: %w" ,  b . Dir ( ) ,  err ) ) 
							 
						 
					
						
							
								
									
										
										
										
											2019-01-24 19:33:12 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
											} 
							 
						 
					
						
							
								
									
										
										
										
											2017-11-21 19:15:02 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
										} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2024-06-13 05:31:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									return  nil ,  errs . Err ( ) 
							 
						 
					
						
							
								
									
										
										
										
											2017-03-02 21:32:09 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								}  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2024-06-13 05:31:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								func  ( c  * LeveledCompactor )  Write ( dest  string ,  b  BlockReader ,  mint ,  maxt  int64 ,  base  * BlockMeta )  ( [ ] ulid . ULID ,  error )  {  
						 
					
						
							
								
									
										
										
										
											2019-01-29 19:23:53 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									start  :=  time . Now ( ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-02-25 19:12:03 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									uid  :=  ulid . MustNew ( ulid . Now ( ) ,  rand . Reader ) 
							 
						 
					
						
							
								
									
										
										
										
											2017-05-18 22:09:30 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-08-29 06:39:17 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									meta  :=  & BlockMeta { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										ULID :     uid , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										MinTime :  mint , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										MaxTime :  maxt , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									meta . Compaction . Level  =  1 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									meta . Compaction . Sources  =  [ ] ulid . ULID { uid } 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2024-04-08 23:34:14 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									if  base  !=  nil  { 
							 
						 
					
						
							
								
									
										
										
										
											2018-06-27 21:47:11 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
										meta . Compaction . Parents  =  [ ] BlockDesc { 
							 
						 
					
						
							
								
									
										
										
										
											2024-04-08 23:34:14 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
											{ ULID :  base . ULID ,  MinTime :  base . MinTime ,  MaxTime :  base . MaxTime } , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										if  base . Compaction . FromOutOfOrder ( )  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											meta . Compaction . SetOutOfOrder ( ) 
							 
						 
					
						
							
								
									
										
										
										
											2018-06-27 21:47:11 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
										} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2023-04-13 05:18:20 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									err  :=  c . write ( dest ,  meta ,  DefaultBlockPopulator { } ,  b ) 
							 
						 
					
						
							
								
									
										
										
										
											2018-03-15 19:26:11 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									if  err  !=  nil  { 
							 
						 
					
						
							
								
									
										
										
										
											2024-06-13 05:31:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										return  nil ,  err 
							 
						 
					
						
							
								
									
										
										
										
											2018-03-15 19:26:11 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2019-01-18 16:35:16 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									if  meta . Stats . NumSamples  ==  0  { 
							 
						 
					
						
							
								
									
										
										
										
											2024-09-10 09:41:53 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										c . logger . Info ( 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											"write block resulted in empty block" , 
							 
						 
					
						
							
								
									
										
										
										
											2020-10-14 21:29:59 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
											"mint" ,  meta . MinTime , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											"maxt" ,  meta . MaxTime , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											"duration" ,  time . Since ( start ) , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										) 
							 
						 
					
						
							
								
									
										
										
										
											2024-06-13 05:31:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										return  nil ,  nil 
							 
						 
					
						
							
								
									
										
										
										
											2019-01-18 16:35:16 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2024-09-10 09:41:53 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									c . logger . Info ( 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										"write block" , 
							 
						 
					
						
							
								
									
										
										
										
											2019-01-29 19:23:53 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
										"mint" ,  meta . MinTime , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										"maxt" ,  meta . MaxTime , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										"ulid" ,  meta . ULID , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										"duration" ,  time . Since ( start ) , 
							 
						 
					
						
							
								
									
										
										
										
											2024-04-04 20:26:13 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										"ooo" ,  meta . Compaction . FromOutOfOrder ( ) , 
							 
						 
					
						
							
								
									
										
										
										
											2019-01-29 19:23:53 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									) 
							 
						 
					
						
							
								
									
										
										
										
											2024-06-13 05:31:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									return  [ ] ulid . ULID { uid } ,  nil 
							 
						 
					
						
							
								
									
										
										
										
											2017-03-02 16:13:29 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								}  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-09-01 22:10:10 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								// instrumentedChunkWriter is used for level 1 compactions to record statistics
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								// about compacted chunks.
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								type  instrumentedChunkWriter  struct  {  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									ChunkWriter 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									size     prometheus . Histogram 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									samples  prometheus . Histogram 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									trange   prometheus . Histogram 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-11-30 22:34:49 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								func  ( w  * instrumentedChunkWriter )  WriteChunks ( chunks  ... chunks . Meta )  error  {  
						 
					
						
							
								
									
										
										
										
											2017-09-01 22:10:10 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									for  _ ,  c  :=  range  chunks  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										w . size . Observe ( float64 ( len ( c . Chunk . Bytes ( ) ) ) ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										w . samples . Observe ( float64 ( c . Chunk . NumSamples ( ) ) ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										w . trange . Observe ( float64 ( c . MaxTime  -  c . MinTime ) ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									return  w . ChunkWriter . WriteChunks ( chunks ... ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-03-02 21:32:09 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								// write creates a new block that is the union of the provided blocks into dir.
  
						 
					
						
							
								
									
										
										
										
											2023-04-13 05:18:20 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								func  ( c  * LeveledCompactor )  write ( dest  string ,  meta  * BlockMeta ,  blockPopulator  BlockPopulator ,  blocks  ... BlockReader )  ( err  error )  {  
						 
					
						
							
								
									
										
										
										
											2017-11-21 19:15:02 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									dir  :=  filepath . Join ( dest ,  meta . ULID . String ( ) ) 
							 
						 
					
						
							
								
									
										
										
										
											2020-08-11 13:56:08 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									tmp  :=  dir  +  tmpForCreationBlockDirSuffix 
							 
						 
					
						
							
								
									
										
										
										
											2019-02-07 16:09:42 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									var  closers  [ ] io . Closer 
							 
						 
					
						
							
								
									
										
										
										
											2017-03-02 16:13:29 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									defer  func ( t  time . Time )  { 
							 
						 
					
						
							
								
									
										
										
										
											2020-10-28 23:24:58 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										err  =  tsdb_errors . NewMulti ( err ,  tsdb_errors . CloseAll ( closers ) ) . Err ( ) 
							 
						 
					
						
							
								
									
										
										
										
											2019-02-06 22:09:42 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										// RemoveAll returns no error when tmp doesn't exist so it is safe to always run it.
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										if  err  :=  os . RemoveAll ( tmp ) ;  err  !=  nil  { 
							 
						 
					
						
							
								
									
										
										
										
											2024-09-10 09:41:53 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
											c . logger . Error ( "removed tmp folder after failed compaction" ,  "err" ,  err . Error ( ) ) 
							 
						 
					
						
							
								
									
										
										
										
											2019-02-06 22:09:42 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
										} 
							 
						 
					
						
							
								
									
										
										
										
											2023-04-04 14:31:49 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										c . metrics . Ran . Inc ( ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										c . metrics . Duration . Observe ( time . Since ( t ) . Seconds ( ) ) 
							 
						 
					
						
							
								
									
										
										
										
											2017-03-02 16:13:29 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									} ( time . Now ( ) ) 
							 
						 
					
						
							
								
									
										
										
										
											2017-01-03 22:43:26 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-03-02 21:32:09 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									if  err  =  os . RemoveAll ( tmp ) ;  err  !=  nil  { 
							 
						 
					
						
							
								
									
										
										
										
											2017-02-19 23:04:37 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
										return  err 
							 
						 
					
						
							
								
									
										
										
										
											2017-01-03 22:43:26 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
									
										
										
										
											2017-02-19 23:04:37 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-10-22 16:06:44 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									if  err  =  os . MkdirAll ( tmp ,  0 o777 ) ;  err  !=  nil  { 
							 
						 
					
						
							
								
									
										
										
										
											2017-01-02 17:34:55 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
										return  err 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-03-02 21:35:06 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									// Populate chunk and index files into temporary directory with
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									// data of all blocks.
 
							 
						 
					
						
							
								
									
										
										
										
											2017-09-01 22:10:10 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									var  chunkw  ChunkWriter 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-04-15 16:55:01 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									chunkw ,  err  =  chunks . NewWriterWithSegSize ( chunkDir ( tmp ) ,  c . maxBlockChunkSegmentSize ) 
							 
						 
					
						
							
								
									
										
										
										
											2017-02-23 17:50:22 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									if  err  !=  nil  { 
							 
						 
					
						
							
								
									
										
										
										
											2023-11-17 02:54:41 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										return  fmt . Errorf ( "open chunk writer: %w" ,  err ) 
							 
						 
					
						
							
								
									
										
										
										
											2017-02-23 17:50:22 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
									
										
										
										
											2019-02-07 16:09:42 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									closers  =  append ( closers ,  chunkw ) 
							 
						 
					
						
							
								
									
										
										
										
											2017-09-01 22:10:10 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									// Record written chunk sizes on level 1 compactions.
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									if  meta . Compaction . Level  ==  1  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										chunkw  =  & instrumentedChunkWriter { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											ChunkWriter :  chunkw , 
							 
						 
					
						
							
								
									
										
										
										
											2023-04-04 14:31:49 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
											size :         c . metrics . ChunkSize , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											samples :      c . metrics . ChunkSamples , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											trange :       c . metrics . ChunkRange , 
							 
						 
					
						
							
								
									
										
										
										
											2017-09-01 22:10:10 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
										} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2024-01-08 17:48:27 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									indexw ,  err  :=  index . NewWriterWithEncoder ( c . ctx ,  filepath . Join ( tmp ,  indexFilename ) ,  c . postingsEncoder ) 
							 
						 
					
						
							
								
									
										
										
										
											2017-02-25 14:24:20 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									if  err  !=  nil  { 
							 
						 
					
						
							
								
									
										
										
										
											2023-11-17 02:54:41 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										return  fmt . Errorf ( "open index writer: %w" ,  err ) 
							 
						 
					
						
							
								
									
										
										
										
											2017-02-25 14:24:20 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
									
										
										
										
											2019-02-07 16:09:42 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									closers  =  append ( closers ,  indexw ) 
							 
						 
					
						
							
								
									
										
										
										
											2017-01-03 22:43:26 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2024-06-25 16:21:48 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									if  err  :=  blockPopulator . PopulateBlock ( c . ctx ,  c . metrics ,  c . logger ,  c . chunkPool ,  c . mergeFunc ,  blocks ,  meta ,  indexw ,  chunkw ,  AllSortedPostings ) ;  err  !=  nil  { 
							 
						 
					
						
							
								
									
										
										
										
											2023-11-17 02:54:41 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										return  fmt . Errorf ( "populate block: %w" ,  err ) 
							 
						 
					
						
							
								
									
										
										
										
											2017-01-03 22:43:26 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
									
										
										
										
											2017-05-18 22:09:30 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-12-06 00:34:42 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									select  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									case  <- c . ctx . Done ( ) : 
							 
						 
					
						
							
								
									
										
										
										
											2019-02-06 20:07:35 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
										return  c . ctx . Err ( ) 
							 
						 
					
						
							
								
									
										
										
										
											2018-12-06 00:34:42 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									default : 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-09-21 13:31:22 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									// We are explicitly closing them here to check for error even
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									// though these are covered under defer. This is because in Windows,
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									// you cannot delete these unless they are closed and the defer is to
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									// make sure they are closed if the function exits due to an error above.
 
							 
						 
					
						
							
								
									
										
										
										
											2020-10-28 23:24:58 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									errs  :=  tsdb_errors . NewMulti ( ) 
							 
						 
					
						
							
								
									
										
										
										
											2019-02-07 16:09:42 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									for  _ ,  w  :=  range  closers  { 
							 
						 
					
						
							
								
									
										
										
										
											2020-10-28 23:24:58 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										errs . Add ( w . Close ( ) ) 
							 
						 
					
						
							
								
									
										
										
										
											2019-02-06 20:07:35 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
									
										
										
										
											2019-02-07 16:09:42 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									closers  =  closers [ : 0 ]  // Avoid closing the writers twice in the defer.
 
							 
						 
					
						
							
								
									
										
										
										
											2020-10-28 23:24:58 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									if  errs . Err ( )  !=  nil  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										return  errs . Err ( ) 
							 
						 
					
						
							
								
									
										
										
										
											2017-01-03 22:43:26 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
									
										
										
										
											2017-03-02 21:32:09 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2019-02-06 22:09:42 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									// Populated block is empty, so exit early.
 
							 
						 
					
						
							
								
									
										
										
										
											2019-01-18 16:35:16 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									if  meta . Stats . NumSamples  ==  0  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										return  nil 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2019-06-24 23:42:29 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									if  _ ,  err  =  writeMetaFile ( c . logger ,  tmp ,  meta ) ;  err  !=  nil  { 
							 
						 
					
						
							
								
									
										
										
										
											2023-11-17 02:54:41 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										return  fmt . Errorf ( "write merged meta: %w" ,  err ) 
							 
						 
					
						
							
								
									
										
										
										
											2019-01-18 16:35:16 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-05-14 17:06:26 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									// Create an empty tombstones file.
 
							 
						 
					
						
							
								
									
										
										
										
											2019-09-19 17:15:41 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									if  _ ,  err  :=  tombstones . WriteFile ( c . logger ,  tmp ,  tombstones . NewMemTombstones ( ) ) ;  err  !=  nil  { 
							 
						 
					
						
							
								
									
										
										
										
											2023-11-17 02:54:41 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										return  fmt . Errorf ( "write new tombstones file: %w" ,  err ) 
							 
						 
					
						
							
								
									
										
										
										
											2017-05-14 17:06:26 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-10-04 16:42:25 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									df ,  err  :=  fileutil . OpenDir ( tmp ) 
							 
						 
					
						
							
								
									
										
										
										
											2017-03-02 21:35:06 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									if  err  !=  nil  { 
							 
						 
					
						
							
								
									
										
										
										
											2023-11-17 02:54:41 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										return  fmt . Errorf ( "open temporary block dir: %w" ,  err ) 
							 
						 
					
						
							
								
									
										
										
										
											2017-03-02 21:35:06 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
									
										
										
										
											2017-10-31 22:37:41 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									defer  func ( )  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										if  df  !=  nil  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											df . Close ( ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									} ( ) 
							 
						 
					
						
							
								
									
										
										
										
											2017-06-12 06:05:04 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2019-04-03 16:16:54 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									if  err  :=  df . Sync ( ) ;  err  !=  nil  { 
							 
						 
					
						
							
								
									
										
										
										
											2023-11-17 02:54:41 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										return  fmt . Errorf ( "sync temporary dir file: %w" ,  err ) 
							 
						 
					
						
							
								
									
										
										
										
											2017-03-02 21:35:06 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
									
										
										
										
											2017-03-02 21:32:09 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-03-15 19:26:11 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									// Close temp dir before rename block dir (for windows platform).
 
							 
						 
					
						
							
								
									
										
										
										
											2017-10-31 22:37:41 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									if  err  =  df . Close ( ) ;  err  !=  nil  { 
							 
						 
					
						
							
								
									
										
										
										
											2023-11-17 02:54:41 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										return  fmt . Errorf ( "close temporary dir: %w" ,  err ) 
							 
						 
					
						
							
								
									
										
										
										
											2017-10-31 22:37:41 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									df  =  nil 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-11-10 00:51:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									// Block successfully written, make it visible in destination dir by moving it from tmp one.
 
							 
						 
					
						
							
								
									
										
										
										
											2019-04-26 16:27:36 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									if  err  :=  fileutil . Replace ( tmp ,  dir ) ;  err  !=  nil  { 
							 
						 
					
						
							
								
									
										
										
										
											2023-11-17 02:54:41 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										return  fmt . Errorf ( "rename block dir: %w" ,  err ) 
							 
						 
					
						
							
								
									
										
										
										
											2017-10-04 16:42:25 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
									
										
										
										
											2018-03-15 19:26:11 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-01-06 19:37:28 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									return  nil 
							 
						 
					
						
							
								
									
										
										
										
											2017-01-03 22:43:26 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								}  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2023-04-13 05:18:20 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								type  BlockPopulator  interface  {  
						 
					
						
							
								
									
										
										
										
											2024-09-10 09:41:53 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									PopulateBlock ( ctx  context . Context ,  metrics  * CompactorMetrics ,  logger  * slog . Logger ,  chunkPool  chunkenc . Pool ,  mergeFunc  storage . VerticalChunkSeriesMergeFunc ,  blocks  [ ] BlockReader ,  meta  * BlockMeta ,  indexw  IndexWriter ,  chunkw  ChunkWriter ,  postingsFunc  IndexReaderPostingsFunc )  error 
							 
						 
					
						
							
								
									
										
										
										
											2024-06-25 16:21:48 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								}  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								// IndexReaderPostingsFunc is a function to get a sorted posting iterator from a given index reader.
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								type  IndexReaderPostingsFunc  func ( ctx  context . Context ,  reader  IndexReader )  index . Postings  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								// AllSortedPostings returns a sorted all posting iterator from the input index reader.
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								func  AllSortedPostings ( ctx  context . Context ,  reader  IndexReader )  index . Postings  {  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									k ,  v  :=  index . AllPostingsKey ( ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									all ,  err  :=  reader . Postings ( ctx ,  k ,  v ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									if  err  !=  nil  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										return  index . ErrPostings ( err ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									return  reader . SortedPostings ( all ) 
							 
						 
					
						
							
								
									
										
										
										
											2023-04-04 14:31:49 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								}  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2023-04-13 05:18:20 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								type  DefaultBlockPopulator  struct { }  
						 
					
						
							
								
									
										
										
										
											2023-04-04 14:31:49 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								// PopulateBlock fills the index and chunk writers with new data gathered as the union
  
						 
					
						
							
								
									
										
										
										
											2017-03-02 21:32:09 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								// of the provided blocks. It returns meta information for the new block.
  
						 
					
						
							
								
									
										
										
										
											2019-02-14 21:29:41 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								// It expects sorted blocks input by mint.
  
						 
					
						
							
								
									
										
										
										
											2024-09-10 09:41:53 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								func  ( c  DefaultBlockPopulator )  PopulateBlock ( ctx  context . Context ,  metrics  * CompactorMetrics ,  logger  * slog . Logger ,  chunkPool  chunkenc . Pool ,  mergeFunc  storage . VerticalChunkSeriesMergeFunc ,  blocks  [ ] BlockReader ,  meta  * BlockMeta ,  indexw  IndexWriter ,  chunkw  ChunkWriter ,  postingsFunc  IndexReaderPostingsFunc )  ( err  error )  {  
						 
					
						
							
								
									
										
										
										
											2018-10-12 17:45:19 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									if  len ( blocks )  ==  0  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										return  errors . New ( "cannot populate block from no readers" ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-08-05 19:31:48 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									var  ( 
							 
						 
					
						
							
								
									
										
										
										
											2020-07-31 23:03:02 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										sets         [ ] storage . ChunkSeriesSet 
							 
						 
					
						
							
								
									
										
											 
										
											
												Stream symbols during compaction. (#6468)
Rather than buffer up symbols in RAM, do it one by one
during compaction. Then use the reader's symbol handling
for symbol lookups during the rest of the index write.
There is some slowdown in compaction, due to having to look through a file
rather than a hash lookup. This is noise to the overall cost of compacting
series with thousands of samples though.
benchmark                                                                                   old ns/op       new ns/op       delta
BenchmarkCompaction/type=normal,blocks=4,series=10000,samplesPerSeriesPerBlock=101-4        539917175       675341565       +25.08%
BenchmarkCompaction/type=normal,blocks=4,series=10000,samplesPerSeriesPerBlock=1001-4       2441815993      2477453524      +1.46%
BenchmarkCompaction/type=normal,blocks=4,series=10000,samplesPerSeriesPerBlock=2001-4       3978543559      3922909687      -1.40%
BenchmarkCompaction/type=normal,blocks=4,series=10000,samplesPerSeriesPerBlock=5001-4       8430219716      8586610007      +1.86%
BenchmarkCompaction/type=vertical,blocks=4,series=10000,samplesPerSeriesPerBlock=101-4      1786424591      1909552782      +6.89%
BenchmarkCompaction/type=vertical,blocks=4,series=10000,samplesPerSeriesPerBlock=1001-4     5328998202      6020839950      +12.98%
BenchmarkCompaction/type=vertical,blocks=4,series=10000,samplesPerSeriesPerBlock=2001-4     10085059958     11085278690     +9.92%
BenchmarkCompaction/type=vertical,blocks=4,series=10000,samplesPerSeriesPerBlock=5001-4     25497010155     27018079806     +5.97%
BenchmarkCompactionFromHead/labelnames=1,labelvalues=100000-4                               2427391406      2817217987      +16.06%
BenchmarkCompactionFromHead/labelnames=10,labelvalues=10000-4                               2592965497      2538805050      -2.09%
BenchmarkCompactionFromHead/labelnames=100,labelvalues=1000-4                               2437388343      2668012858      +9.46%
BenchmarkCompactionFromHead/labelnames=1000,labelvalues=100-4                               2317095324      2787423966      +20.30%
BenchmarkCompactionFromHead/labelnames=10000,labelvalues=10-4                               2600239857      2096973860      -19.35%
benchmark                                                                                   old allocs     new allocs     delta
BenchmarkCompaction/type=normal,blocks=4,series=10000,samplesPerSeriesPerBlock=101-4        500851         470794         -6.00%
BenchmarkCompaction/type=normal,blocks=4,series=10000,samplesPerSeriesPerBlock=1001-4       821527         791451         -3.66%
BenchmarkCompaction/type=normal,blocks=4,series=10000,samplesPerSeriesPerBlock=2001-4       1141562        1111508        -2.63%
BenchmarkCompaction/type=normal,blocks=4,series=10000,samplesPerSeriesPerBlock=5001-4       2141576        2111504        -1.40%
BenchmarkCompaction/type=vertical,blocks=4,series=10000,samplesPerSeriesPerBlock=101-4      871466         841424         -3.45%
BenchmarkCompaction/type=vertical,blocks=4,series=10000,samplesPerSeriesPerBlock=1001-4     1941428        1911415        -1.55%
BenchmarkCompaction/type=vertical,blocks=4,series=10000,samplesPerSeriesPerBlock=2001-4     3071573        3041510        -0.98%
BenchmarkCompaction/type=vertical,blocks=4,series=10000,samplesPerSeriesPerBlock=5001-4     6771648        6741509        -0.45%
BenchmarkCompactionFromHead/labelnames=1,labelvalues=100000-4                               731493         824888         +12.77%
BenchmarkCompactionFromHead/labelnames=10,labelvalues=10000-4                               793918         887311         +11.76%
BenchmarkCompactionFromHead/labelnames=100,labelvalues=1000-4                               811842         905204         +11.50%
BenchmarkCompactionFromHead/labelnames=1000,labelvalues=100-4                               832244         925081         +11.16%
BenchmarkCompactionFromHead/labelnames=10000,labelvalues=10-4                               921553         1019162        +10.59%
benchmark                                                                                   old bytes      new bytes      delta
BenchmarkCompaction/type=normal,blocks=4,series=10000,samplesPerSeriesPerBlock=101-4        40532648       35698276       -11.93%
BenchmarkCompaction/type=normal,blocks=4,series=10000,samplesPerSeriesPerBlock=1001-4       60340216       53409568       -11.49%
BenchmarkCompaction/type=normal,blocks=4,series=10000,samplesPerSeriesPerBlock=2001-4       81087336       72065552       -11.13%
BenchmarkCompaction/type=normal,blocks=4,series=10000,samplesPerSeriesPerBlock=5001-4       142485576      120878544      -15.16%
BenchmarkCompaction/type=vertical,blocks=4,series=10000,samplesPerSeriesPerBlock=101-4      208661368      203831136      -2.31%
BenchmarkCompaction/type=vertical,blocks=4,series=10000,samplesPerSeriesPerBlock=1001-4     347345904      340484696      -1.98%
BenchmarkCompaction/type=vertical,blocks=4,series=10000,samplesPerSeriesPerBlock=2001-4     585185856      576244648      -1.53%
BenchmarkCompaction/type=vertical,blocks=4,series=10000,samplesPerSeriesPerBlock=5001-4     1357641792     1358966528     +0.10%
BenchmarkCompactionFromHead/labelnames=1,labelvalues=100000-4                               126486664      119666744      -5.39%
BenchmarkCompactionFromHead/labelnames=10,labelvalues=10000-4                               122323192      115117224      -5.89%
BenchmarkCompactionFromHead/labelnames=100,labelvalues=1000-4                               126404504      119469864      -5.49%
BenchmarkCompactionFromHead/labelnames=1000,labelvalues=100-4                               119047832      112230408      -5.73%
BenchmarkCompactionFromHead/labelnames=10000,labelvalues=10-4                               136576016      116634800      -14.60%
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
											 
										 
										
											2019-12-18 03:49:54 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										symbols      index . StringIter 
							 
						 
					
						
							
								
									
										
										
										
											2020-07-31 23:03:02 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										closers      [ ] io . Closer 
							 
						 
					
						
							
								
									
										
										
										
											2019-02-14 21:29:41 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
										overlapping  bool 
							 
						 
					
						
							
								
									
										
										
										
											2017-08-05 19:31:48 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									) 
							 
						 
					
						
							
								
									
										
										
										
											2018-12-07 23:49:23 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									defer  func ( )  { 
							 
						 
					
						
							
								
									
										
										
										
											2020-10-28 23:24:58 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										errs  :=  tsdb_errors . NewMulti ( err ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										if  cerr  :=  tsdb_errors . CloseAll ( closers ) ;  cerr  !=  nil  { 
							 
						 
					
						
							
								
									
										
										
										
											2023-11-17 02:54:41 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
											errs . Add ( fmt . Errorf ( "close: %w" ,  cerr ) ) 
							 
						 
					
						
							
								
									
										
										
										
											2020-07-14 16:36:22 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										} 
							 
						 
					
						
							
								
									
										
										
										
											2020-10-28 23:24:58 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										err  =  errs . Err ( ) 
							 
						 
					
						
							
								
									
										
										
										
											2023-04-04 14:31:49 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										metrics . PopulatingBlocks . Set ( 0 ) 
							 
						 
					
						
							
								
									
										
										
										
											2018-12-07 23:49:23 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									} ( ) 
							 
						 
					
						
							
								
									
										
										
										
											2023-04-04 14:31:49 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									metrics . PopulatingBlocks . Set ( 1 ) 
							 
						 
					
						
							
								
									
										
										
										
											2019-01-19 00:58:17 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2019-07-23 16:04:48 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									globalMaxt  :=  blocks [ 0 ] . Meta ( ) . MaxTime 
							 
						 
					
						
							
								
									
										
										
										
											2017-01-03 22:43:26 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									for  i ,  b  :=  range  blocks  { 
							 
						 
					
						
							
								
									
										
										
										
											2018-12-06 00:34:42 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
										select  { 
							 
						 
					
						
							
								
									
										
										
										
											2023-04-04 14:31:49 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										case  <- ctx . Done ( ) : 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											return  ctx . Err ( ) 
							 
						 
					
						
							
								
									
										
										
										
											2018-12-06 00:34:42 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
										default : 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2019-02-14 21:29:41 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
										if  ! overlapping  { 
							 
						 
					
						
							
								
									
										
										
										
											2019-07-23 16:04:48 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
											if  i  >  0  &&  b . Meta ( ) . MinTime  <  globalMaxt  { 
							 
						 
					
						
							
								
									
										
										
										
											2023-04-04 14:31:49 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
												metrics . OverlappingBlocks . Inc ( ) 
							 
						 
					
						
							
								
									
										
										
										
											2019-02-14 21:29:41 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
												overlapping  =  true 
							 
						 
					
						
							
								
									
										
										
										
											2024-09-10 09:41:53 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
												logger . Info ( "Found overlapping blocks during compaction" ,  "ulid" ,  meta . ULID ) 
							 
						 
					
						
							
								
									
										
										
										
											2019-02-14 21:29:41 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
											} 
							 
						 
					
						
							
								
									
										
										
										
											2019-07-23 16:04:48 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
											if  b . Meta ( ) . MaxTime  >  globalMaxt  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
												globalMaxt  =  b . Meta ( ) . MaxTime 
							 
						 
					
						
							
								
									
										
										
										
											2019-02-14 21:29:41 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
											} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-03-26 03:13:47 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										indexr ,  err  :=  b . Index ( ) 
							 
						 
					
						
							
								
									
										
										
										
											2017-10-09 21:21:46 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
										if  err  !=  nil  { 
							 
						 
					
						
							
								
									
										
										
										
											2023-11-17 02:54:41 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
											return  fmt . Errorf ( "open index reader for block %+v: %w" ,  b . Meta ( ) ,  err ) 
							 
						 
					
						
							
								
									
										
										
										
											2017-10-09 21:21:46 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
										} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										closers  =  append ( closers ,  indexr ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										chunkr ,  err  :=  b . Chunks ( ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										if  err  !=  nil  { 
							 
						 
					
						
							
								
									
										
										
										
											2023-11-17 02:54:41 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
											return  fmt . Errorf ( "open chunk reader for block %+v: %w" ,  b . Meta ( ) ,  err ) 
							 
						 
					
						
							
								
									
										
										
										
											2017-10-09 21:21:46 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
										} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										closers  =  append ( closers ,  chunkr ) 
							 
						 
					
						
							
								
									
										
										
										
											2017-06-07 19:42:53 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-10-09 21:21:46 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
										tombsr ,  err  :=  b . Tombstones ( ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										if  err  !=  nil  { 
							 
						 
					
						
							
								
									
										
										
										
											2023-11-17 02:54:41 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
											return  fmt . Errorf ( "open tombstone reader for block %+v: %w" ,  b . Meta ( ) ,  err ) 
							 
						 
					
						
							
								
									
										
										
										
											2017-10-09 21:21:46 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
										} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										closers  =  append ( closers ,  tombsr ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2024-06-25 16:21:48 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										postings  :=  postingsFunc ( ctx ,  indexr ) 
							 
						 
					
						
							
								
									
										
										
										
											2020-07-31 23:03:02 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										// Blocks meta is half open: [min, max), so subtract 1 to ensure we don't hold samples with exact meta.MaxTime timestamp.
 
							 
						 
					
						
							
								
									
										
										
										
											2024-06-25 16:21:48 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										sets  =  append ( sets ,  NewBlockChunkSeriesSet ( b . Meta ( ) . ULID ,  indexr ,  chunkr ,  tombsr ,  postings ,  meta . MinTime ,  meta . MaxTime - 1 ,  false ) ) 
							 
						 
					
						
							
								
									
										
											 
										
											
												Stream symbols during compaction. (#6468)
Rather than buffer up symbols in RAM, do it one by one
during compaction. Then use the reader's symbol handling
for symbol lookups during the rest of the index write.
There is some slowdown in compaction, due to having to look through a file
rather than a hash lookup. This is noise to the overall cost of compacting
series with thousands of samples though.
benchmark                                                                                   old ns/op       new ns/op       delta
BenchmarkCompaction/type=normal,blocks=4,series=10000,samplesPerSeriesPerBlock=101-4        539917175       675341565       +25.08%
BenchmarkCompaction/type=normal,blocks=4,series=10000,samplesPerSeriesPerBlock=1001-4       2441815993      2477453524      +1.46%
BenchmarkCompaction/type=normal,blocks=4,series=10000,samplesPerSeriesPerBlock=2001-4       3978543559      3922909687      -1.40%
BenchmarkCompaction/type=normal,blocks=4,series=10000,samplesPerSeriesPerBlock=5001-4       8430219716      8586610007      +1.86%
BenchmarkCompaction/type=vertical,blocks=4,series=10000,samplesPerSeriesPerBlock=101-4      1786424591      1909552782      +6.89%
BenchmarkCompaction/type=vertical,blocks=4,series=10000,samplesPerSeriesPerBlock=1001-4     5328998202      6020839950      +12.98%
BenchmarkCompaction/type=vertical,blocks=4,series=10000,samplesPerSeriesPerBlock=2001-4     10085059958     11085278690     +9.92%
BenchmarkCompaction/type=vertical,blocks=4,series=10000,samplesPerSeriesPerBlock=5001-4     25497010155     27018079806     +5.97%
BenchmarkCompactionFromHead/labelnames=1,labelvalues=100000-4                               2427391406      2817217987      +16.06%
BenchmarkCompactionFromHead/labelnames=10,labelvalues=10000-4                               2592965497      2538805050      -2.09%
BenchmarkCompactionFromHead/labelnames=100,labelvalues=1000-4                               2437388343      2668012858      +9.46%
BenchmarkCompactionFromHead/labelnames=1000,labelvalues=100-4                               2317095324      2787423966      +20.30%
BenchmarkCompactionFromHead/labelnames=10000,labelvalues=10-4                               2600239857      2096973860      -19.35%
benchmark                                                                                   old allocs     new allocs     delta
BenchmarkCompaction/type=normal,blocks=4,series=10000,samplesPerSeriesPerBlock=101-4        500851         470794         -6.00%
BenchmarkCompaction/type=normal,blocks=4,series=10000,samplesPerSeriesPerBlock=1001-4       821527         791451         -3.66%
BenchmarkCompaction/type=normal,blocks=4,series=10000,samplesPerSeriesPerBlock=2001-4       1141562        1111508        -2.63%
BenchmarkCompaction/type=normal,blocks=4,series=10000,samplesPerSeriesPerBlock=5001-4       2141576        2111504        -1.40%
BenchmarkCompaction/type=vertical,blocks=4,series=10000,samplesPerSeriesPerBlock=101-4      871466         841424         -3.45%
BenchmarkCompaction/type=vertical,blocks=4,series=10000,samplesPerSeriesPerBlock=1001-4     1941428        1911415        -1.55%
BenchmarkCompaction/type=vertical,blocks=4,series=10000,samplesPerSeriesPerBlock=2001-4     3071573        3041510        -0.98%
BenchmarkCompaction/type=vertical,blocks=4,series=10000,samplesPerSeriesPerBlock=5001-4     6771648        6741509        -0.45%
BenchmarkCompactionFromHead/labelnames=1,labelvalues=100000-4                               731493         824888         +12.77%
BenchmarkCompactionFromHead/labelnames=10,labelvalues=10000-4                               793918         887311         +11.76%
BenchmarkCompactionFromHead/labelnames=100,labelvalues=1000-4                               811842         905204         +11.50%
BenchmarkCompactionFromHead/labelnames=1000,labelvalues=100-4                               832244         925081         +11.16%
BenchmarkCompactionFromHead/labelnames=10000,labelvalues=10-4                               921553         1019162        +10.59%
benchmark                                                                                   old bytes      new bytes      delta
BenchmarkCompaction/type=normal,blocks=4,series=10000,samplesPerSeriesPerBlock=101-4        40532648       35698276       -11.93%
BenchmarkCompaction/type=normal,blocks=4,series=10000,samplesPerSeriesPerBlock=1001-4       60340216       53409568       -11.49%
BenchmarkCompaction/type=normal,blocks=4,series=10000,samplesPerSeriesPerBlock=2001-4       81087336       72065552       -11.13%
BenchmarkCompaction/type=normal,blocks=4,series=10000,samplesPerSeriesPerBlock=5001-4       142485576      120878544      -15.16%
BenchmarkCompaction/type=vertical,blocks=4,series=10000,samplesPerSeriesPerBlock=101-4      208661368      203831136      -2.31%
BenchmarkCompaction/type=vertical,blocks=4,series=10000,samplesPerSeriesPerBlock=1001-4     347345904      340484696      -1.98%
BenchmarkCompaction/type=vertical,blocks=4,series=10000,samplesPerSeriesPerBlock=2001-4     585185856      576244648      -1.53%
BenchmarkCompaction/type=vertical,blocks=4,series=10000,samplesPerSeriesPerBlock=5001-4     1357641792     1358966528     +0.10%
BenchmarkCompactionFromHead/labelnames=1,labelvalues=100000-4                               126486664      119666744      -5.39%
BenchmarkCompactionFromHead/labelnames=10,labelvalues=10000-4                               122323192      115117224      -5.89%
BenchmarkCompactionFromHead/labelnames=100,labelvalues=1000-4                               126404504      119469864      -5.49%
BenchmarkCompactionFromHead/labelnames=1000,labelvalues=100-4                               119047832      112230408      -5.73%
BenchmarkCompactionFromHead/labelnames=10000,labelvalues=10-4                               136576016      116634800      -14.60%
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
											 
										 
										
											2019-12-18 03:49:54 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										syms  :=  indexr . Symbols ( ) 
							 
						 
					
						
							
								
									
										
										
										
											2017-01-03 22:43:26 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
										if  i  ==  0  { 
							 
						 
					
						
							
								
									
										
											 
										
											
												Stream symbols during compaction. (#6468)
Rather than buffer up symbols in RAM, do it one by one
during compaction. Then use the reader's symbol handling
for symbol lookups during the rest of the index write.
There is some slowdown in compaction, due to having to look through a file
rather than a hash lookup. This is noise to the overall cost of compacting
series with thousands of samples though.
benchmark                                                                                   old ns/op       new ns/op       delta
BenchmarkCompaction/type=normal,blocks=4,series=10000,samplesPerSeriesPerBlock=101-4        539917175       675341565       +25.08%
BenchmarkCompaction/type=normal,blocks=4,series=10000,samplesPerSeriesPerBlock=1001-4       2441815993      2477453524      +1.46%
BenchmarkCompaction/type=normal,blocks=4,series=10000,samplesPerSeriesPerBlock=2001-4       3978543559      3922909687      -1.40%
BenchmarkCompaction/type=normal,blocks=4,series=10000,samplesPerSeriesPerBlock=5001-4       8430219716      8586610007      +1.86%
BenchmarkCompaction/type=vertical,blocks=4,series=10000,samplesPerSeriesPerBlock=101-4      1786424591      1909552782      +6.89%
BenchmarkCompaction/type=vertical,blocks=4,series=10000,samplesPerSeriesPerBlock=1001-4     5328998202      6020839950      +12.98%
BenchmarkCompaction/type=vertical,blocks=4,series=10000,samplesPerSeriesPerBlock=2001-4     10085059958     11085278690     +9.92%
BenchmarkCompaction/type=vertical,blocks=4,series=10000,samplesPerSeriesPerBlock=5001-4     25497010155     27018079806     +5.97%
BenchmarkCompactionFromHead/labelnames=1,labelvalues=100000-4                               2427391406      2817217987      +16.06%
BenchmarkCompactionFromHead/labelnames=10,labelvalues=10000-4                               2592965497      2538805050      -2.09%
BenchmarkCompactionFromHead/labelnames=100,labelvalues=1000-4                               2437388343      2668012858      +9.46%
BenchmarkCompactionFromHead/labelnames=1000,labelvalues=100-4                               2317095324      2787423966      +20.30%
BenchmarkCompactionFromHead/labelnames=10000,labelvalues=10-4                               2600239857      2096973860      -19.35%
benchmark                                                                                   old allocs     new allocs     delta
BenchmarkCompaction/type=normal,blocks=4,series=10000,samplesPerSeriesPerBlock=101-4        500851         470794         -6.00%
BenchmarkCompaction/type=normal,blocks=4,series=10000,samplesPerSeriesPerBlock=1001-4       821527         791451         -3.66%
BenchmarkCompaction/type=normal,blocks=4,series=10000,samplesPerSeriesPerBlock=2001-4       1141562        1111508        -2.63%
BenchmarkCompaction/type=normal,blocks=4,series=10000,samplesPerSeriesPerBlock=5001-4       2141576        2111504        -1.40%
BenchmarkCompaction/type=vertical,blocks=4,series=10000,samplesPerSeriesPerBlock=101-4      871466         841424         -3.45%
BenchmarkCompaction/type=vertical,blocks=4,series=10000,samplesPerSeriesPerBlock=1001-4     1941428        1911415        -1.55%
BenchmarkCompaction/type=vertical,blocks=4,series=10000,samplesPerSeriesPerBlock=2001-4     3071573        3041510        -0.98%
BenchmarkCompaction/type=vertical,blocks=4,series=10000,samplesPerSeriesPerBlock=5001-4     6771648        6741509        -0.45%
BenchmarkCompactionFromHead/labelnames=1,labelvalues=100000-4                               731493         824888         +12.77%
BenchmarkCompactionFromHead/labelnames=10,labelvalues=10000-4                               793918         887311         +11.76%
BenchmarkCompactionFromHead/labelnames=100,labelvalues=1000-4                               811842         905204         +11.50%
BenchmarkCompactionFromHead/labelnames=1000,labelvalues=100-4                               832244         925081         +11.16%
BenchmarkCompactionFromHead/labelnames=10000,labelvalues=10-4                               921553         1019162        +10.59%
benchmark                                                                                   old bytes      new bytes      delta
BenchmarkCompaction/type=normal,blocks=4,series=10000,samplesPerSeriesPerBlock=101-4        40532648       35698276       -11.93%
BenchmarkCompaction/type=normal,blocks=4,series=10000,samplesPerSeriesPerBlock=1001-4       60340216       53409568       -11.49%
BenchmarkCompaction/type=normal,blocks=4,series=10000,samplesPerSeriesPerBlock=2001-4       81087336       72065552       -11.13%
BenchmarkCompaction/type=normal,blocks=4,series=10000,samplesPerSeriesPerBlock=5001-4       142485576      120878544      -15.16%
BenchmarkCompaction/type=vertical,blocks=4,series=10000,samplesPerSeriesPerBlock=101-4      208661368      203831136      -2.31%
BenchmarkCompaction/type=vertical,blocks=4,series=10000,samplesPerSeriesPerBlock=1001-4     347345904      340484696      -1.98%
BenchmarkCompaction/type=vertical,blocks=4,series=10000,samplesPerSeriesPerBlock=2001-4     585185856      576244648      -1.53%
BenchmarkCompaction/type=vertical,blocks=4,series=10000,samplesPerSeriesPerBlock=5001-4     1357641792     1358966528     +0.10%
BenchmarkCompactionFromHead/labelnames=1,labelvalues=100000-4                               126486664      119666744      -5.39%
BenchmarkCompactionFromHead/labelnames=10,labelvalues=10000-4                               122323192      115117224      -5.89%
BenchmarkCompactionFromHead/labelnames=100,labelvalues=1000-4                               126404504      119469864      -5.49%
BenchmarkCompactionFromHead/labelnames=1000,labelvalues=100-4                               119047832      112230408      -5.73%
BenchmarkCompactionFromHead/labelnames=10000,labelvalues=10-4                               136576016      116634800      -14.60%
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
											 
										 
										
											2019-12-18 03:49:54 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
											symbols  =  syms 
							 
						 
					
						
							
								
									
										
										
										
											2017-01-03 22:43:26 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
											continue 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										} 
							 
						 
					
						
							
								
									
										
										
										
											2020-11-10 00:51:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										symbols  =  NewMergedStringIter ( symbols ,  syms ) 
							 
						 
					
						
							
								
									
										
										
										
											2017-01-02 18:12:28 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
											 
										
											
												Stream symbols during compaction. (#6468)
Rather than buffer up symbols in RAM, do it one by one
during compaction. Then use the reader's symbol handling
for symbol lookups during the rest of the index write.
There is some slowdown in compaction, due to having to look through a file
rather than a hash lookup. This is noise to the overall cost of compacting
series with thousands of samples though.
benchmark                                                                                   old ns/op       new ns/op       delta
BenchmarkCompaction/type=normal,blocks=4,series=10000,samplesPerSeriesPerBlock=101-4        539917175       675341565       +25.08%
BenchmarkCompaction/type=normal,blocks=4,series=10000,samplesPerSeriesPerBlock=1001-4       2441815993      2477453524      +1.46%
BenchmarkCompaction/type=normal,blocks=4,series=10000,samplesPerSeriesPerBlock=2001-4       3978543559      3922909687      -1.40%
BenchmarkCompaction/type=normal,blocks=4,series=10000,samplesPerSeriesPerBlock=5001-4       8430219716      8586610007      +1.86%
BenchmarkCompaction/type=vertical,blocks=4,series=10000,samplesPerSeriesPerBlock=101-4      1786424591      1909552782      +6.89%
BenchmarkCompaction/type=vertical,blocks=4,series=10000,samplesPerSeriesPerBlock=1001-4     5328998202      6020839950      +12.98%
BenchmarkCompaction/type=vertical,blocks=4,series=10000,samplesPerSeriesPerBlock=2001-4     10085059958     11085278690     +9.92%
BenchmarkCompaction/type=vertical,blocks=4,series=10000,samplesPerSeriesPerBlock=5001-4     25497010155     27018079806     +5.97%
BenchmarkCompactionFromHead/labelnames=1,labelvalues=100000-4                               2427391406      2817217987      +16.06%
BenchmarkCompactionFromHead/labelnames=10,labelvalues=10000-4                               2592965497      2538805050      -2.09%
BenchmarkCompactionFromHead/labelnames=100,labelvalues=1000-4                               2437388343      2668012858      +9.46%
BenchmarkCompactionFromHead/labelnames=1000,labelvalues=100-4                               2317095324      2787423966      +20.30%
BenchmarkCompactionFromHead/labelnames=10000,labelvalues=10-4                               2600239857      2096973860      -19.35%
benchmark                                                                                   old allocs     new allocs     delta
BenchmarkCompaction/type=normal,blocks=4,series=10000,samplesPerSeriesPerBlock=101-4        500851         470794         -6.00%
BenchmarkCompaction/type=normal,blocks=4,series=10000,samplesPerSeriesPerBlock=1001-4       821527         791451         -3.66%
BenchmarkCompaction/type=normal,blocks=4,series=10000,samplesPerSeriesPerBlock=2001-4       1141562        1111508        -2.63%
BenchmarkCompaction/type=normal,blocks=4,series=10000,samplesPerSeriesPerBlock=5001-4       2141576        2111504        -1.40%
BenchmarkCompaction/type=vertical,blocks=4,series=10000,samplesPerSeriesPerBlock=101-4      871466         841424         -3.45%
BenchmarkCompaction/type=vertical,blocks=4,series=10000,samplesPerSeriesPerBlock=1001-4     1941428        1911415        -1.55%
BenchmarkCompaction/type=vertical,blocks=4,series=10000,samplesPerSeriesPerBlock=2001-4     3071573        3041510        -0.98%
BenchmarkCompaction/type=vertical,blocks=4,series=10000,samplesPerSeriesPerBlock=5001-4     6771648        6741509        -0.45%
BenchmarkCompactionFromHead/labelnames=1,labelvalues=100000-4                               731493         824888         +12.77%
BenchmarkCompactionFromHead/labelnames=10,labelvalues=10000-4                               793918         887311         +11.76%
BenchmarkCompactionFromHead/labelnames=100,labelvalues=1000-4                               811842         905204         +11.50%
BenchmarkCompactionFromHead/labelnames=1000,labelvalues=100-4                               832244         925081         +11.16%
BenchmarkCompactionFromHead/labelnames=10000,labelvalues=10-4                               921553         1019162        +10.59%
benchmark                                                                                   old bytes      new bytes      delta
BenchmarkCompaction/type=normal,blocks=4,series=10000,samplesPerSeriesPerBlock=101-4        40532648       35698276       -11.93%
BenchmarkCompaction/type=normal,blocks=4,series=10000,samplesPerSeriesPerBlock=1001-4       60340216       53409568       -11.49%
BenchmarkCompaction/type=normal,blocks=4,series=10000,samplesPerSeriesPerBlock=2001-4       81087336       72065552       -11.13%
BenchmarkCompaction/type=normal,blocks=4,series=10000,samplesPerSeriesPerBlock=5001-4       142485576      120878544      -15.16%
BenchmarkCompaction/type=vertical,blocks=4,series=10000,samplesPerSeriesPerBlock=101-4      208661368      203831136      -2.31%
BenchmarkCompaction/type=vertical,blocks=4,series=10000,samplesPerSeriesPerBlock=1001-4     347345904      340484696      -1.98%
BenchmarkCompaction/type=vertical,blocks=4,series=10000,samplesPerSeriesPerBlock=2001-4     585185856      576244648      -1.53%
BenchmarkCompaction/type=vertical,blocks=4,series=10000,samplesPerSeriesPerBlock=5001-4     1357641792     1358966528     +0.10%
BenchmarkCompactionFromHead/labelnames=1,labelvalues=100000-4                               126486664      119666744      -5.39%
BenchmarkCompactionFromHead/labelnames=10,labelvalues=10000-4                               122323192      115117224      -5.89%
BenchmarkCompactionFromHead/labelnames=100,labelvalues=1000-4                               126404504      119469864      -5.49%
BenchmarkCompactionFromHead/labelnames=1000,labelvalues=100-4                               119047832      112230408      -5.73%
BenchmarkCompactionFromHead/labelnames=10000,labelvalues=10-4                               136576016      116634800      -14.60%
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
											 
										 
										
											2019-12-18 03:49:54 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									for  symbols . Next ( )  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										if  err  :=  indexw . AddSymbol ( symbols . At ( ) ) ;  err  !=  nil  { 
							 
						 
					
						
							
								
									
										
										
										
											2023-11-17 02:54:41 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
											return  fmt . Errorf ( "add symbol: %w" ,  err ) 
							 
						 
					
						
							
								
									
										
											 
										
											
												Stream symbols during compaction. (#6468)
Rather than buffer up symbols in RAM, do it one by one
during compaction. Then use the reader's symbol handling
for symbol lookups during the rest of the index write.
There is some slowdown in compaction, due to having to look through a file
rather than a hash lookup. This is noise to the overall cost of compacting
series with thousands of samples though.
benchmark                                                                                   old ns/op       new ns/op       delta
BenchmarkCompaction/type=normal,blocks=4,series=10000,samplesPerSeriesPerBlock=101-4        539917175       675341565       +25.08%
BenchmarkCompaction/type=normal,blocks=4,series=10000,samplesPerSeriesPerBlock=1001-4       2441815993      2477453524      +1.46%
BenchmarkCompaction/type=normal,blocks=4,series=10000,samplesPerSeriesPerBlock=2001-4       3978543559      3922909687      -1.40%
BenchmarkCompaction/type=normal,blocks=4,series=10000,samplesPerSeriesPerBlock=5001-4       8430219716      8586610007      +1.86%
BenchmarkCompaction/type=vertical,blocks=4,series=10000,samplesPerSeriesPerBlock=101-4      1786424591      1909552782      +6.89%
BenchmarkCompaction/type=vertical,blocks=4,series=10000,samplesPerSeriesPerBlock=1001-4     5328998202      6020839950      +12.98%
BenchmarkCompaction/type=vertical,blocks=4,series=10000,samplesPerSeriesPerBlock=2001-4     10085059958     11085278690     +9.92%
BenchmarkCompaction/type=vertical,blocks=4,series=10000,samplesPerSeriesPerBlock=5001-4     25497010155     27018079806     +5.97%
BenchmarkCompactionFromHead/labelnames=1,labelvalues=100000-4                               2427391406      2817217987      +16.06%
BenchmarkCompactionFromHead/labelnames=10,labelvalues=10000-4                               2592965497      2538805050      -2.09%
BenchmarkCompactionFromHead/labelnames=100,labelvalues=1000-4                               2437388343      2668012858      +9.46%
BenchmarkCompactionFromHead/labelnames=1000,labelvalues=100-4                               2317095324      2787423966      +20.30%
BenchmarkCompactionFromHead/labelnames=10000,labelvalues=10-4                               2600239857      2096973860      -19.35%
benchmark                                                                                   old allocs     new allocs     delta
BenchmarkCompaction/type=normal,blocks=4,series=10000,samplesPerSeriesPerBlock=101-4        500851         470794         -6.00%
BenchmarkCompaction/type=normal,blocks=4,series=10000,samplesPerSeriesPerBlock=1001-4       821527         791451         -3.66%
BenchmarkCompaction/type=normal,blocks=4,series=10000,samplesPerSeriesPerBlock=2001-4       1141562        1111508        -2.63%
BenchmarkCompaction/type=normal,blocks=4,series=10000,samplesPerSeriesPerBlock=5001-4       2141576        2111504        -1.40%
BenchmarkCompaction/type=vertical,blocks=4,series=10000,samplesPerSeriesPerBlock=101-4      871466         841424         -3.45%
BenchmarkCompaction/type=vertical,blocks=4,series=10000,samplesPerSeriesPerBlock=1001-4     1941428        1911415        -1.55%
BenchmarkCompaction/type=vertical,blocks=4,series=10000,samplesPerSeriesPerBlock=2001-4     3071573        3041510        -0.98%
BenchmarkCompaction/type=vertical,blocks=4,series=10000,samplesPerSeriesPerBlock=5001-4     6771648        6741509        -0.45%
BenchmarkCompactionFromHead/labelnames=1,labelvalues=100000-4                               731493         824888         +12.77%
BenchmarkCompactionFromHead/labelnames=10,labelvalues=10000-4                               793918         887311         +11.76%
BenchmarkCompactionFromHead/labelnames=100,labelvalues=1000-4                               811842         905204         +11.50%
BenchmarkCompactionFromHead/labelnames=1000,labelvalues=100-4                               832244         925081         +11.16%
BenchmarkCompactionFromHead/labelnames=10000,labelvalues=10-4                               921553         1019162        +10.59%
benchmark                                                                                   old bytes      new bytes      delta
BenchmarkCompaction/type=normal,blocks=4,series=10000,samplesPerSeriesPerBlock=101-4        40532648       35698276       -11.93%
BenchmarkCompaction/type=normal,blocks=4,series=10000,samplesPerSeriesPerBlock=1001-4       60340216       53409568       -11.49%
BenchmarkCompaction/type=normal,blocks=4,series=10000,samplesPerSeriesPerBlock=2001-4       81087336       72065552       -11.13%
BenchmarkCompaction/type=normal,blocks=4,series=10000,samplesPerSeriesPerBlock=5001-4       142485576      120878544      -15.16%
BenchmarkCompaction/type=vertical,blocks=4,series=10000,samplesPerSeriesPerBlock=101-4      208661368      203831136      -2.31%
BenchmarkCompaction/type=vertical,blocks=4,series=10000,samplesPerSeriesPerBlock=1001-4     347345904      340484696      -1.98%
BenchmarkCompaction/type=vertical,blocks=4,series=10000,samplesPerSeriesPerBlock=2001-4     585185856      576244648      -1.53%
BenchmarkCompaction/type=vertical,blocks=4,series=10000,samplesPerSeriesPerBlock=5001-4     1357641792     1358966528     +0.10%
BenchmarkCompactionFromHead/labelnames=1,labelvalues=100000-4                               126486664      119666744      -5.39%
BenchmarkCompactionFromHead/labelnames=10,labelvalues=10000-4                               122323192      115117224      -5.89%
BenchmarkCompactionFromHead/labelnames=100,labelvalues=1000-4                               126404504      119469864      -5.49%
BenchmarkCompactionFromHead/labelnames=1000,labelvalues=100-4                               119047832      112230408      -5.73%
BenchmarkCompactionFromHead/labelnames=10000,labelvalues=10-4                               136576016      116634800      -14.60%
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
											 
										 
										
											2019-12-18 03:49:54 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
									
										
										
										
											2023-11-17 02:54:41 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									if  err  :=  symbols . Err ( ) ;  err  !=  nil  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										return  fmt . Errorf ( "next symbol: %w" ,  err ) 
							 
						 
					
						
							
								
									
										
										
										
											2017-08-05 19:31:48 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-07-31 23:03:02 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									var  ( 
							 
						 
					
						
							
								
									
										
										
										
											2022-09-21 01:16:45 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										ref       =  storage . SeriesRef ( 0 ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										chks      [ ] chunks . Meta 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										chksIter  chunks . Iterator 
							 
						 
					
						
							
								
									
										
										
										
											2020-07-31 23:03:02 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									set  :=  sets [ 0 ] 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									if  len ( sets )  >  1  { 
							 
						 
					
						
							
								
									
										
										
										
											2021-05-19 00:38:37 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										// Merge series using specified chunk series merger.
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										// The default one is the compacting series merger.
 
							 
						 
					
						
							
								
									
										
										
										
											2023-04-04 14:31:49 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										set  =  storage . NewMergeChunkSeriesSet ( sets ,  mergeFunc ) 
							 
						 
					
						
							
								
									
										
										
										
											2020-07-31 23:03:02 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									// Iterate over all sorted chunk series.
 
							 
						 
					
						
							
								
									
										
										
										
											2017-01-02 17:34:55 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									for  set . Next ( )  { 
							 
						 
					
						
							
								
									
										
										
										
											2018-12-06 00:34:42 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
										select  { 
							 
						 
					
						
							
								
									
										
										
										
											2023-04-04 14:31:49 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										case  <- ctx . Done ( ) : 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											return  ctx . Err ( ) 
							 
						 
					
						
							
								
									
										
										
										
											2018-12-06 00:34:42 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
										default : 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										} 
							 
						 
					
						
							
								
									
										
										
										
											2020-07-31 23:03:02 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										s  :=  set . At ( ) 
							 
						 
					
						
							
								
									
										
										
										
											2022-09-21 01:16:45 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										chksIter  =  s . Iterator ( chksIter ) 
							 
						 
					
						
							
								
									
										
										
										
											2020-07-31 23:03:02 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										chks  =  chks [ : 0 ] 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										for  chksIter . Next ( )  { 
							 
						 
					
						
							
								
									
										
										
										
											2024-05-08 23:57:09 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
											// We are not iterating in a streaming way over chunks as
 
							 
						 
					
						
							
								
									
										
										
										
											2022-07-06 20:34:02 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
											// it's more efficient to do bulk write for index and
 
							 
						 
					
						
							
								
									
										
										
										
											2020-07-31 23:03:02 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
											// chunk file purposes.
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											chks  =  append ( chks ,  chksIter . At ( ) ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										} 
							 
						 
					
						
							
								
									
										
										
										
											2023-11-17 02:54:41 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										if  err  :=  chksIter . Err ( ) ;  err  !=  nil  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											return  fmt . Errorf ( "chunk iter: %w" ,  err ) 
							 
						 
					
						
							
								
									
										
										
										
											2019-02-14 21:29:41 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
										} 
							 
						 
					
						
							
								
									
										
										
										
											2017-05-17 17:19:42 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2024-05-08 23:57:09 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										// Skip series with all deleted chunks.
 
							 
						 
					
						
							
								
									
										
										
										
											2017-07-13 00:16:12 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
										if  len ( chks )  ==  0  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											continue 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-07-31 23:03:02 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										if  err  :=  chunkw . WriteChunks ( chks ... ) ;  err  !=  nil  { 
							 
						 
					
						
							
								
									
										
										
										
											2023-11-17 02:54:41 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
											return  fmt . Errorf ( "write chunks: %w" ,  err ) 
							 
						 
					
						
							
								
									
										
										
										
											2017-01-02 17:34:55 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
										} 
							 
						 
					
						
							
								
									
										
										
										
											2020-07-31 23:03:02 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										if  err  :=  indexw . AddSeries ( ref ,  s . Labels ( ) ,  chks ... ) ;  err  !=  nil  { 
							 
						 
					
						
							
								
									
										
										
										
											2023-11-17 02:54:41 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
											return  fmt . Errorf ( "add series: %w" ,  err ) 
							 
						 
					
						
							
								
									
										
										
										
											2017-08-05 19:31:48 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
										} 
							 
						 
					
						
							
								
									
										
										
										
											2017-02-19 18:27:31 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-07-31 23:03:02 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										meta . Stats . NumChunks  +=  uint64 ( len ( chks ) ) 
							 
						 
					
						
							
								
									
										
										
										
											2017-01-19 18:22:47 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
										meta . Stats . NumSeries ++ 
							 
						 
					
						
							
								
									
										
										
										
											2020-07-31 23:03:02 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										for  _ ,  chk  :=  range  chks  { 
							 
						 
					
						
							
								
									
										
										
										
											2017-07-13 00:31:26 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
											meta . Stats . NumSamples  +=  uint64 ( chk . Chunk . NumSamples ( ) ) 
							 
						 
					
						
							
								
									
										
										
										
											2017-07-13 00:16:12 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
										} 
							 
						 
					
						
							
								
									
										
										
										
											2017-01-02 17:34:55 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-07-31 23:03:02 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										for  _ ,  chk  :=  range  chks  { 
							 
						 
					
						
							
								
									
										
										
										
											2023-04-04 14:31:49 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
											if  err  :=  chunkPool . Put ( chk . Chunk ) ;  err  !=  nil  { 
							 
						 
					
						
							
								
									
										
										
										
											2023-11-17 02:54:41 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
												return  fmt . Errorf ( "put chunk: %w" ,  err ) 
							 
						 
					
						
							
								
									
										
										
										
											2018-09-20 16:33:52 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
											} 
							 
						 
					
						
							
								
									
										
										
										
											2017-08-08 23:35:34 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
										} 
							 
						 
					
						
							
								
									
										
										
										
											2019-12-12 01:20:41 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										ref ++ 
							 
						 
					
						
							
								
									
										
										
										
											2017-01-02 17:34:55 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
									
										
										
										
											2023-11-17 02:54:41 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									if  err  :=  set . Err ( ) ;  err  !=  nil  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										return  fmt . Errorf ( "iterate compaction set: %w" ,  err ) 
							 
						 
					
						
							
								
									
										
										
										
											2017-01-02 17:34:55 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-08-29 06:39:17 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									return  nil 
							 
						 
					
						
							
								
									
										
										
										
											2017-01-02 17:34:55 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								}