Skip to content

Commit

Permalink
rename param to checkExternalLabels
Browse files Browse the repository at this point in the history
Signed-off-by: yeya24 <[email protected]>
  • Loading branch information
yeya24 committed Feb 26, 2021
1 parent 9514d1c commit 7e51790
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 14 deletions.
24 changes: 12 additions & 12 deletions pkg/block/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func Download(ctx context.Context, logger log.Logger, bucket objstore.Bucket, id
}

// Upload uploads a TSDB block to the object storage. It verifies basic
// features of Thanos block and updates `meta.Thanos.File` section.
// features of Thanos block.
func Upload(ctx context.Context, logger log.Logger, bkt objstore.Bucket, bdir string) error {
return upload(ctx, logger, bkt, bdir, true)
}
Expand All @@ -78,7 +78,8 @@ func UploadPromBlock(ctx context.Context, logger log.Logger, bkt objstore.Bucket
// upload uploads block from given block dir that ends with block id.
// It makes sure cleanup is done on error to avoid partial block uploads.
// TODO(bplotka): Ensure bucket operations have reasonable backoff retries.
func upload(ctx context.Context, logger log.Logger, bkt objstore.Bucket, bdir string, thanosBlocks bool) error {
// NOTE: Upload updates `meta.Thanos.File` section.
func upload(ctx context.Context, logger log.Logger, bkt objstore.Bucket, bdir string, checkExternalLabels bool) error {
df, err := os.Stat(bdir)
if err != nil {
return err
Expand All @@ -99,6 +100,12 @@ func upload(ctx context.Context, logger log.Logger, bkt objstore.Bucket, bdir st
return errors.Wrap(err, "read meta")
}

if checkExternalLabels {
if meta.Thanos.Labels == nil || len(meta.Thanos.Labels) == 0 {
return errors.New("empty external labels are not allowed for Thanos block.")
}
}

metaEncoded := strings.Builder{}
meta.Thanos.Files, err = gatherFileStats(bdir)
if err != nil {
Expand All @@ -109,16 +116,9 @@ func upload(ctx context.Context, logger log.Logger, bkt objstore.Bucket, bdir st
return errors.Wrap(err, "encode meta file")
}

// If we are going to upload Thanos blocks, we need to check external labels.
if thanosBlocks {
if meta.Thanos.Labels == nil || len(meta.Thanos.Labels) == 0 {
return errors.New("empty external labels are not allowed for Thanos block.")
}

// TODO(yeya24): Remove this step.
if err := bkt.Upload(ctx, path.Join(DebugMetas, fmt.Sprintf("%s.json", id)), strings.NewReader(metaEncoded.String())); err != nil {
return cleanUp(logger, bkt, id, errors.Wrap(err, "upload debug meta file"))
}
// TODO(yeya24): Remove this step.
if err := bkt.Upload(ctx, path.Join(DebugMetas, fmt.Sprintf("%s.json", id)), strings.NewReader(metaEncoded.String())); err != nil {
return cleanUp(logger, bkt, id, errors.Wrap(err, "upload debug meta file"))
}

if err := objstore.UploadDir(ctx, logger, bkt, path.Join(bdir, ChunksDirname), path.Join(id.String(), ChunksDirname)); err != nil {
Expand Down
3 changes: 1 addition & 2 deletions pkg/block/block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,8 +222,7 @@ func TestUpload(t *testing.T) {
testutil.Ok(t, err)
err = UploadPromBlock(ctx, log.NewNopLogger(), bkt, path.Join(tmpDir, b2.String()))
testutil.Ok(t, err)
// # of files increases from 4 -> 7 because prom blocks doesn't upload debug meta.
testutil.Equals(t, 7, len(bkt.Objects()))
testutil.Equals(t, 8, len(bkt.Objects()))
testutil.Equals(t, 3736, len(bkt.Objects()[path.Join(b2.String(), ChunksDirname, "000001")]))
testutil.Equals(t, 401, len(bkt.Objects()[path.Join(b2.String(), IndexFilename)]))
testutil.Equals(t, 525, len(bkt.Objects()[path.Join(b2.String(), MetaFilename)]))
Expand Down

0 comments on commit 7e51790

Please sign in to comment.