Repository: prometheus/prometheus
File: tsdb/compact_test.go (Go)

Code before:
						}
					}

					require.NoError(t, oldApp.Commit())

					// Old head compaction.
					mint := oldHead.MinTime()
					maxt := oldHead.MaxTime() + 1 // Block intervals are half-open: [b.MinTime, b.MaxTime).
					compactor, err := NewLeveledCompactor(context.Background(), nil, nil, []int64{DefaultBlockDuration}, chunkenc.NewPool(), nil)
					require.NoError(t, err)
					oldULIDs, err = compactor.Write(oldHead.opts.ChunkDirRoot, oldHead, mint, maxt, nil)
					require.NoError(t, err)
					require.Len(t, oldULIDs, 1)
				}(c)

				wg.Wait()

				oldBlockDir := filepath.Join(oldHead.opts.ChunkDirRoot, oldULIDs[0].String())
				sparseBlockDir := filepath.Join(sparseHead.opts.ChunkDirRoot, sparseULIDs[0].String())

				oldSize, err := fileutil.DirSize(oldBlockDir)
				require.NoError(t, err)
				oldIndexSize, err := fileutil.DirSize(filepath.Join(oldBlockDir, "index"))
				require.NoError(t, err)
				oldChunksSize, err := fileutil.DirSize(filepath.Join(oldBlockDir, "chunks"))
				require.NoError(t, err)

				sparseSize, err := fileutil.DirSize(sparseBlockDir)
				require.NoError(t, err)
				sparseIndexSize, err := fileutil.DirSize(filepath.Join(sparseBlockDir, "index"))
				require.NoError(t, err)
				sparseChunksSize, err := fileutil.DirSize(filepath.Join(sparseBlockDir, "chunks"))
				require.NoError(t, err)

				summaries = append(summaries, testSummary{
					oldBlockTotalSeries:    len(allSchemas) * c.numSeriesPerSchema * numOldSeriesPerHistogram,
					oldBlockIndexSize:      oldIndexSize,
					oldBlockChunksSize:     oldChunksSize,
					oldBlockTotalSize:      oldSize,
					sparseBlockTotalSeries: len(allSchemas) * c.numSeriesPerSchema,
					sparseBlockIndexSize:   sparseIndexSize,
					sparseBlockChunksSize:  sparseChunksSize,
					sparseBlockTotalSize:   sparseSize,
					numBuckets:             c.numBuckets,
					numSpans:               c.numSpans,
					gapBetweenSpans:        c.gapBetweenSpans,
				})
			})
	}

	for _, s := range summaries {

Review comment (bug):
Is this necessary now that Prometheus requires Go v1.22, which [prevents](https://tip.golang.org/doc/go1.22) the loop variable capture bug?

Code after:
					// Ingest sparse histograms.
					for _, ah := range allSparseSeries {
						var (
							ref storage.SeriesRef
							err error
						)
						for i := 0; i < numHistograms; i++ {
							ts := int64(i) * timeStep
							ref, err = sparseApp.AppendHistogram(ref, ah.baseLabels, ts, ah.hists[i], nil)
							require.NoError(t, err)
						}
					}
					require.NoError(t, sparseApp.Commit())

					// Sparse head compaction.
					mint := sparseHead.MinTime()
					maxt := sparseHead.MaxTime() + 1 // Block intervals are half-open: [b.MinTime, b.MaxTime).
					compactor, err := NewLeveledCompactor(context.Background(), nil, nil, []int64{DefaultBlockDuration}, chunkenc.NewPool(), nil)
					require.NoError(t, err)
					sparseULIDs, err = compactor.Write(sparseHead.opts.ChunkDirRoot, sparseHead, mint, maxt, nil)
					require.NoError(t, err)
					require.Len(t, sparseULIDs, 1)
				}()

				wg.Add(1)
				go func(c testcase) {
					defer wg.Done()

					// Ingest histograms the old way.
					for _, ah := range allSparseSeries {
						refs := make([]storage.SeriesRef, c.numBuckets+((c.numSpans-1)*c.gapBetweenSpans))
						for i := 0; i < numHistograms; i++ {
							ts := int64(i) * timeStep

							h := ah.hists[i]

							numOldSeriesPerHistogram = 0
							it := h.CumulativeBucketIterator()
							itIdx := 0
							var err error
							for it.Next() {
								numOldSeriesPerHistogram++
								b := it.At()
								lbls := labels.NewBuilder(ah.baseLabels).Set("le", fmt.Sprintf("%.16f", b.Upper)).Labels()
								refs[itIdx], err = oldApp.Append(refs[itIdx], lbls, ts, float64(b.Count))
								require.NoError(t, err)
								itIdx++
							}
							baseName := ah.baseLabels.Get(labels.MetricName)
							// _count metric.
							countLbls := labels.NewBuilder(ah.baseLabels).Set(labels.MetricName, baseName+"_count").Labels()