Skip to content

Commit

Permalink
Fixed windows compatibility issue, when storage gateway syncs index f…
Browse files Browse the repository at this point in the history
…iles to local disk.

Signed-off-by: henrikschristensen <51989221+henrikschristensen@users.noreply.github.com>
  • Loading branch information
henrikschristensen committed Feb 21, 2024
1 parent 1723d1d commit 3718883
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 10 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Expand Up @@ -11,9 +11,10 @@ We use *breaking :warning:* to mark changes that are not backward compatible (re
## Unreleased

### Fixed

- [#7083](https://github.com/thanos-io/thanos/pull/7083) Store Gateway: Fix lazy expanded postings with 0 length failed to be cached.
- [#7080](https://github.com/thanos-io/thanos/pull/7080) Receive: race condition in handler Close() when stopped early
- [#7148](https://github.com/thanos-io/thanos/pull/7148) Store Gateway: Fix windows compatibility in index-header sync.
- [#7132](https://github.com/thanos-io/thanos/pull/7132) Documentation: fix broken helm installation instruction

### Added
Expand Down
28 changes: 19 additions & 9 deletions pkg/block/indexheader/binary_reader.go
Expand Up @@ -13,6 +13,7 @@ import (
"io"
"math"
"os"
"path"
"path/filepath"
"sort"
"sync"
Expand Down Expand Up @@ -112,6 +113,20 @@ func WriteBinary(ctx context.Context, bkt objstore.BucketReader, id ulid.ULID, f
tmpFilename = filename + ".tmp"
}

bw, err := writeAndClose(id, tmpFilename, indexVersion, ir)
if err != nil {
return nil, err
}

if tmpFilename != "" {
// Create index-header in atomic way, to avoid partial writes (e.g during restart or crash of store GW).
return nil, os.Rename(tmpFilename, filename)
}

return bw.Buffer(), nil
}

func writeAndClose(id ulid.ULID, tmpFilename string, indexVersion int, ir *chunkedIndexReader) (*binaryWriter, error) {
// Buffer for copying and encbuffers.
// This also will control the size of file writer buffer.
buf := make([]byte, 32*1024)
Expand Down Expand Up @@ -152,13 +167,7 @@ func WriteBinary(ctx context.Context, bkt objstore.BucketReader, id ulid.ULID, f
if err := bw.writer.Sync(); err != nil {
return nil, errors.Wrap(err, "sync")
}

if tmpFilename != "" {
// Create index-header in atomic way, to avoid partial writes (e.g during restart or crash of store GW).
return nil, os.Rename(tmpFilename, filename)
}

return bw.Buffer(), nil
return bw, nil
}

type chunkedIndexReader struct {
Expand All @@ -170,7 +179,7 @@ type chunkedIndexReader struct {
}

func newChunkedIndexReader(ctx context.Context, bkt objstore.BucketReader, id ulid.ULID) (*chunkedIndexReader, int, error) {
indexFilepath := filepath.Join(id.String(), block.IndexFilename)
indexFilepath := path.Join(id.String(), block.IndexFilename)
attrs, err := bkt.Attributes(ctx, indexFilepath)
if err != nil {
return nil, 0, errors.Wrapf(err, "get object attributes of %s", indexFilepath)
Expand Down Expand Up @@ -449,7 +458,8 @@ func (fw *FileWriter) Close() error {
if err := fw.f.Sync(); err != nil {
return err
}
return fw.f.Close()

return fw.Close()
}

func (fw *FileWriter) Sync() error {
Expand Down

0 comments on commit 3718883

Please sign in to comment.