Skip to content

Commit 55f3cfe

Browse files
committed
Added binary index header implementation.
Signed-off-by: Bartlomiej Plotka <bwplotka@gmail.com>
1 parent 718e51a commit 55f3cfe

File tree

12 files changed

+997
-87
lines changed

12 files changed

+997
-87
lines changed

docs/components/store.md

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,3 +221,62 @@ While the remaining settings are **optional**:
221221
- `max_get_multi_concurrency`: maximum number of concurrent connections when fetching keys. If set to `0`, the concurrency is unlimited.
222222
- `max_get_multi_batch_size`: maximum number of keys a single underlying operation should fetch. If more keys are specified, internally keys are splitted into multiple batches and fetched concurrently, honoring `max_get_multi_concurrency`. If set to `0`, the batch size is unlimited.
223223
- `dns_provider_update_interval`: the DNS discovery update interval.
224+
225+
226+
## Index Header
227+
228+
In order to query series inside blocks from object storage, Store Gateway has to know certain initial info about each block such as:
229+
230+
* symbols table to unintern string values
231+
* postings offset for posting lookup
232+
233+
In order to achieve so, on startup for each block `index-header` is built from pieces of original block's index and stored on disk.
234+
Such `index-header` file is then mmaped and used by Store Gateway.
235+
236+
### Format (version 1)
237+
238+
The following describes the format of the `index-header` file found in each block store gateway local directory.
239+
It is terminated by a table of contents which serves as an entry point into the index.
240+
241+
```
242+
┌─────────────────────────────┬───────────────────────────────┐
243+
│ magic(0xBAAAD792) <4b> │ version(1) <1 byte> │
244+
├─────────────────────────────┬───────────────────────────────┤
245+
│ index version(2) <1 byte> │ index PostingOffsetTable <8b> │
246+
├─────────────────────────────┴───────────────────────────────┤
247+
│ ┌─────────────────────────────────────────────────────────┐ │
248+
│ │ Symbol Table (exact copy from original index) │ │
249+
│ ├─────────────────────────────────────────────────────────┤ │
250+
│ │ Posting Offset Table (exact copy from index) │ │
251+
│ ├─────────────────────────────────────────────────────────┤ │
252+
│ │ TOC │ │
253+
│ └─────────────────────────────────────────────────────────┘ │
254+
└─────────────────────────────────────────────────────────────┘
255+
```
256+
257+
When the index is written, an arbitrary number of padding bytes may be added between the lined out main sections above. When sequentially scanning through the file, any zero bytes after a section's specified length must be skipped.
258+
259+
Most of the sections described below start with a `len` field. It always specifies the number of bytes just before the trailing CRC32 checksum. The checksum is always calculated over those `len` bytes.
260+
261+
### Symbol Table
262+
263+
See [Symbols](https://github.com/prometheus/prometheus/blob/d782387f814753b0118d402ec8cdbdef01bf9079/tsdb/docs/format/index.md#symbol-table)
264+
265+
### Postings Offset Table
266+
267+
See [Posting Offset Table](https://github.com/prometheus/prometheus/blob/d782387f814753b0118d402ec8cdbdef01bf9079/tsdb/docs/format/index.md#postings-offset-table)
268+
269+
### TOC
270+
271+
The table of contents serves as an entry point to the entire index and points to various sections in the file.
272+
If a reference is zero, it indicates the respective section does not exist and empty results should be returned upon lookup.
273+
274+
```
275+
┌─────────────────────────────────────────┐
276+
│ ref(symbols) <8b> │
277+
├─────────────────────────────────────────┤
278+
│ ref(postings offset table) <8b> │
279+
├─────────────────────────────────────────┤
280+
│ CRC32 <4b> │
281+
└─────────────────────────────────────────┘
282+
```

pkg/block/block.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,10 @@ const (
2727
MetaFilename = "meta.json"
2828
// IndexFilename is the known index file for block index.
2929
IndexFilename = "index"
30-
// IndexCacheFilename is the canonical name for index cache file that stores essential information needed.
30+
// IndexCacheFilename is the canonical name for json index cache file that stores essential information.
3131
IndexCacheFilename = "index.cache.json"
32+
// IndexHeaderFilename is the canonical name for binary index header file that stores essential information.
33+
IndexHeaderFilename = "index-header.json"
3234
// ChunksDirname is the known dir name for chunks with compressed samples.
3335
ChunksDirname = "chunks"
3436

pkg/block/block_test.go

Lines changed: 4 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package block
22

33
import (
44
"context"
5-
"io"
65
"io/ioutil"
76
"os"
87
"path"
@@ -12,7 +11,6 @@ import (
1211

1312
"github.com/fortytw2/leaktest"
1413
"github.com/go-kit/kit/log"
15-
"github.com/pkg/errors"
1614
"github.com/prometheus/prometheus/pkg/labels"
1715
"github.com/thanos-io/thanos/pkg/objstore/inmem"
1816
"github.com/thanos-io/thanos/pkg/testutil"
@@ -104,7 +102,7 @@ func TestUpload(t *testing.T) {
104102
testutil.NotOk(t, err)
105103
testutil.Assert(t, strings.HasSuffix(err.Error(), "/meta.json: no such file or directory"), "")
106104
}
107-
testutil.Ok(t, cpy(path.Join(tmpDir, b1.String(), MetaFilename), path.Join(tmpDir, "test", b1.String(), MetaFilename)))
105+
testutil.Copy(t, path.Join(tmpDir, b1.String(), MetaFilename), path.Join(tmpDir, "test", b1.String(), MetaFilename))
108106
{
109107
// Missing chunks.
110108
err := Upload(ctx, log.NewNopLogger(), bkt, path.Join(tmpDir, "test", b1.String()))
@@ -115,7 +113,7 @@ func TestUpload(t *testing.T) {
115113
testutil.Equals(t, 1, len(bkt.Objects()))
116114
}
117115
testutil.Ok(t, os.MkdirAll(path.Join(tmpDir, "test", b1.String(), ChunksDirname), os.ModePerm))
118-
testutil.Ok(t, cpy(path.Join(tmpDir, b1.String(), ChunksDirname, "000001"), path.Join(tmpDir, "test", b1.String(), ChunksDirname, "000001")))
116+
testutil.Copy(t, path.Join(tmpDir, b1.String(), ChunksDirname, "000001"), path.Join(tmpDir, "test", b1.String(), ChunksDirname, "000001"))
119117
{
120118
// Missing index file.
121119
err := Upload(ctx, log.NewNopLogger(), bkt, path.Join(tmpDir, "test", b1.String()))
@@ -125,7 +123,7 @@ func TestUpload(t *testing.T) {
125123
// Only debug meta.json present.
126124
testutil.Equals(t, 1, len(bkt.Objects()))
127125
}
128-
testutil.Ok(t, cpy(path.Join(tmpDir, b1.String(), IndexFilename), path.Join(tmpDir, "test", b1.String(), IndexFilename)))
126+
testutil.Copy(t, path.Join(tmpDir, b1.String(), IndexFilename), path.Join(tmpDir, "test", b1.String(), IndexFilename))
129127
testutil.Ok(t, os.Remove(path.Join(tmpDir, "test", b1.String(), MetaFilename)))
130128
{
131129
// Missing meta.json file.
@@ -136,7 +134,7 @@ func TestUpload(t *testing.T) {
136134
// Only debug meta.json present.
137135
testutil.Equals(t, 1, len(bkt.Objects()))
138136
}
139-
testutil.Ok(t, cpy(path.Join(tmpDir, b1.String(), MetaFilename), path.Join(tmpDir, "test", b1.String(), MetaFilename)))
137+
testutil.Copy(t, path.Join(tmpDir, b1.String(), MetaFilename), path.Join(tmpDir, "test", b1.String(), MetaFilename))
140138
{
141139
// Full block.
142140
testutil.Ok(t, Upload(ctx, log.NewNopLogger(), bkt, path.Join(tmpDir, "test", b1.String())))
@@ -170,31 +168,6 @@ func TestUpload(t *testing.T) {
170168
}
171169
}
172170

173-
func cpy(src, dst string) error {
174-
sourceFileStat, err := os.Stat(src)
175-
if err != nil {
176-
return err
177-
}
178-
179-
if !sourceFileStat.Mode().IsRegular() {
180-
return errors.Errorf("%s is not a regular file", src)
181-
}
182-
183-
source, err := os.Open(src)
184-
if err != nil {
185-
return err
186-
}
187-
defer source.Close()
188-
189-
destination, err := os.Create(dst)
190-
if err != nil {
191-
return err
192-
}
193-
defer destination.Close()
194-
_, err = io.Copy(destination, source)
195-
return err
196-
}
197-
198171
func TestDelete(t *testing.T) {
199172
defer leaktest.CheckTimeout(t, 10*time.Second)()
200173

0 commit comments

Comments
 (0)