github.com/grafana/pyroscope@v1.18.0/pkg/phlaredb/block/global_markers.go (about)

     1  // SPDX-License-Identifier: AGPL-3.0-only
     2  // Provenance-includes-location: https://github.com/cortexproject/cortex/blob/master/pkg/storage/tsdb/bucketindex/markers.go
     3  // Provenance-includes-license: Apache-2.0
     4  // Provenance-includes-copyright: The Cortex Authors.
     5  
     6  package block
     7  
     8  import (
     9  	"context"
    10  	"fmt"
    11  	"path"
    12  	"path/filepath"
    13  	"strings"
    14  
    15  	"github.com/oklog/ulid/v2"
    16  	"github.com/pkg/errors"
    17  
    18  	"github.com/grafana/pyroscope/pkg/objstore"
    19  )
    20  
    21  const (
    22  	MarkersPathname = "markers"
    23  )
    24  
    25  func markFilepath(blockID ulid.ULID, markFilename string) string {
    26  	return fmt.Sprintf("%s/%s-%s", MarkersPathname, blockID.String(), markFilename)
    27  }
    28  
    29  func isMarkFilename(name string, markFilename string) (ulid.ULID, bool) {
    30  	parts := strings.SplitN(name, "-", 2)
    31  	if len(parts) != 2 {
    32  		return ulid.ULID{}, false
    33  	}
    34  
    35  	// Ensure the 2nd part matches the mark filename.
    36  	if parts[1] != markFilename {
    37  		return ulid.ULID{}, false
    38  	}
    39  
    40  	// Ensure the 1st part is a valid block ID.
    41  	id, err := ulid.Parse(filepath.Base(parts[0]))
    42  	return id, err == nil
    43  }
    44  
    45  // DeletionMarkFilepath returns the path, relative to the tenant's bucket location,
    46  // of a block deletion mark in the bucket markers location.
    47  func DeletionMarkFilepath(blockID ulid.ULID) string {
    48  	return markFilepath(blockID, DeletionMarkFilename)
    49  }
    50  
    51  // IsDeletionMarkFilename returns whether the input filename matches the expected pattern
    52  // of block deletion markers stored in the markers location.
    53  func IsDeletionMarkFilename(name string) (ulid.ULID, bool) {
    54  	return isMarkFilename(name, DeletionMarkFilename)
    55  }
    56  
    57  // NoCompactMarkFilepath returns the path, relative to the tenant's bucket location,
    58  // of a no-compact block mark in the bucket markers location.
    59  func NoCompactMarkFilepath(blockID ulid.ULID) string {
    60  	return markFilepath(blockID, NoCompactMarkFilename)
    61  }
    62  
    63  // IsNoCompactMarkFilename returns true if input filename matches the expected
    64  // pattern of block marker stored in the markers location.
    65  func IsNoCompactMarkFilename(name string) (ulid.ULID, bool) {
    66  	return isMarkFilename(name, NoCompactMarkFilename)
    67  }
    68  
    69  // ListBlockDeletionMarks looks for block deletion marks in the global markers location
    70  // and returns a map containing all blocks having a deletion mark and their location in the
    71  // bucket.
    72  func ListBlockDeletionMarks(ctx context.Context, bkt objstore.BucketReader) (map[ulid.ULID]struct{}, error) {
    73  	discovered := map[ulid.ULID]struct{}{}
    74  
    75  	// Find all markers in the storage.
    76  	err := bkt.Iter(ctx, MarkersPathname+"/", func(name string) error {
    77  		if err := ctx.Err(); err != nil {
    78  			return err
    79  		}
    80  
    81  		if blockID, ok := IsDeletionMarkFilename(path.Base(name)); ok {
    82  			discovered[blockID] = struct{}{}
    83  		}
    84  
    85  		return nil
    86  	})
    87  
    88  	return discovered, errors.Wrap(err, "list block deletion marks")
    89  }