| 260 | } |
| 261 | |
| 262 | func CreateNHBlock( |
| 263 | ctx context.Context, |
| 264 | rnd *rand.Rand, |
| 265 | dir string, |
| 266 | series []labels.Labels, |
| 267 | numNHSamples int, |
| 268 | mint, maxt int64, |
| 269 | scrapeInterval int64, |
| 270 | seriesSize int64, |
| 271 | ) (id ulid.ULID, err error) { |
| 272 | headOpts := tsdb.DefaultHeadOptions() |
| 273 | headOpts.ChunkDirRoot = filepath.Join(dir, "chunks") |
| 274 | headOpts.ChunkRange = 10000000000 |
| 275 | random := rand.New(rand.NewSource(time.Now().UnixNano())) |
| 276 | h, err := tsdb.NewHead(nil, nil, nil, nil, headOpts, nil) |
| 277 | if err != nil { |
| 278 | return id, errors.Wrap(err, "create head block") |
| 279 | } |
| 280 | defer func() { |
| 281 | runutil.CloseWithErrCapture(&err, h, "TSDB Head") |
| 282 | if e := os.RemoveAll(headOpts.ChunkDirRoot); e != nil { |
| 283 | err = errors.Wrap(e, "delete chunks dir") |
| 284 | } |
| 285 | }() |
| 286 | |
| 287 | app := h.Appender(ctx) |
| 288 | for i := range series { |
| 289 | num := random.Intn(i + 1) |
| 290 | var ref storage.SeriesRef |
| 291 | start := RandRange(rnd, mint, maxt) |
| 292 | for j := range numNHSamples { |
| 293 | switch num % 4 { |
| 294 | case 0: |
| 295 | // append float histogram |
| 296 | ref, err = app.AppendHistogram(ref, series[i], start, nil, tsdbutil.GenerateTestFloatHistogram(int64(i+j))) |
| 297 | case 1: |
| 298 | // append int histogram |
| 299 | ref, err = app.AppendHistogram(ref, series[i], start, tsdbutil.GenerateTestHistogram(int64(i+j)), nil) |
| 300 | case 2: |
| 301 | // append float histogram with custom bucket |
| 302 | ref, err = app.AppendHistogram(ref, series[i], start, nil, tsdbutil.GenerateTestCustomBucketsFloatHistogram(int64(i+j))) |
| 303 | case 3: |
| 304 | // append int histogram with custom bucket |
| 305 | ref, err = app.AppendHistogram(ref, series[i], start, tsdbutil.GenerateTestCustomBucketsHistogram(int64(i+j)), nil) |
| 306 | } |
| 307 | if err != nil { |
| 308 | if rerr := app.Rollback(); rerr != nil { |
| 309 | err = errors.Wrapf(err, "rollback failed: %v", rerr) |
| 310 | } |
| 311 | return id, errors.Wrap(err, "add NH sample") |
| 312 | } |
| 313 | start += scrapeInterval |
| 314 | if start > maxt { |
| 315 | break |
| 316 | } |
| 317 | } |
| 318 | } |
| 319 | if err := app.Commit(); err != nil { |