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 78086ee
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 10 deletions.
2 changes: 2 additions & 0 deletions .bingo/gotesplit.sum
@@ -1,4 +1,6 @@
github.com/Songmu/gotesplit v0.2.1 h1:qJFvR75nJpeKyMQFwyDtFrcc6zDWhrHAkks7DvM8oLo=
github.com/Songmu/gotesplit v0.2.1/go.mod h1:sVBfmLT26b1H5VhUpq8cRhCVK75GAmW9c8r2NiK0gzk=
github.com/jstemmer/go-junit-report v1.0.0 h1:8X1gzZpR+nVQLAht+L/foqOeX2l9DTZoaIPbEQHxsds=
github.com/jstemmer/go-junit-report v1.0.0/go.mod h1:Brl9GWCQeLvo8nXZwPNNblvFj/XSXhF0NWZEnDohbsk=
golang.org/x/sync v0.0.0-20220513210516-0976fa681c29 h1:w8s32wxx3sY+OjLlv9qltkLU5yvJzxjjgiHWLjdIcw4=
golang.org/x/sync v0.0.0-20220513210516-0976fa681c29/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
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
36 changes: 27 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 @@ -443,13 +452,22 @@ func (fw *FileWriter) Flush() error {
}

func (fw *FileWriter) Close() error {
if fw.f == nil {
// Already closed
return nil
}

if err := fw.Flush(); err != nil {
return err
}
if err := fw.f.Sync(); err != nil {
return err
}
return fw.f.Close()

err := fw.Close()
// Mark file descriptor as closed
fw.f = nil
return err
}

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

0 comments on commit 78086ee

Please sign in to comment.