forked from cortexproject/cortex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstorage_client.go
259 lines (223 loc) · 6.67 KB
/
storage_client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
package gcp
import (
"flag"
"fmt"
"strings"
"cloud.google.com/go/bigtable"
"golang.org/x/net/context"
"github.com/pkg/errors"
"github.com/weaveworks/cortex/pkg/chunk"
"github.com/weaveworks/cortex/pkg/util"
)
const (
columnFamily = "f"
column = "c"
separator = "\000"
maxRowReads = 100
)
// Config for a StorageClient
type Config struct {
project string
instance string
}
// RegisterFlags adds the flags required to config this to the given FlagSet
func (cfg *Config) RegisterFlags(f *flag.FlagSet) {
f.StringVar(&cfg.project, "bigtable.project", "", "Bigtable project ID.")
f.StringVar(&cfg.instance, "bigtable.instance", "", "Bigtable instance ID.")
}
// storageClient implements chunk.storageClient for GCP.
type storageClient struct {
cfg Config
schemaCfg chunk.SchemaConfig
client *bigtable.Client
}
// NewStorageClient returns a new StorageClient.
func NewStorageClient(ctx context.Context, cfg Config, schemaCfg chunk.SchemaConfig) (chunk.StorageClient, error) {
client, err := bigtable.NewClient(ctx, cfg.project, cfg.instance, instrumentation()...)
if err != nil {
return nil, err
}
return &storageClient{
cfg: cfg,
schemaCfg: schemaCfg,
client: client,
}, nil
}
func (s *storageClient) NewWriteBatch() chunk.WriteBatch {
return bigtableWriteBatch{
tables: map[string]map[string]*bigtable.Mutation{},
}
}
type bigtableWriteBatch struct {
tables map[string]map[string]*bigtable.Mutation
}
func (b bigtableWriteBatch) Add(tableName, hashValue string, rangeValue []byte, value []byte) {
rows, ok := b.tables[tableName]
if !ok {
rows = map[string]*bigtable.Mutation{}
b.tables[tableName] = rows
}
// TODO the hashValue should actually be hashed - but I have data written in
// this format, so we need to do a proper migration.
rowKey := hashValue + separator + string(rangeValue)
mutation, ok := rows[rowKey]
if !ok {
mutation = bigtable.NewMutation()
rows[rowKey] = mutation
}
mutation.Set(columnFamily, column, 0, value)
}
func (s *storageClient) BatchWrite(ctx context.Context, batch chunk.WriteBatch) error {
bigtableBatch := batch.(bigtableWriteBatch)
for tableName, rows := range bigtableBatch.tables {
table := s.client.Open(tableName)
rowKeys := make([]string, 0, len(rows))
muts := make([]*bigtable.Mutation, 0, len(rows))
for rowKey, mut := range rows {
rowKeys = append(rowKeys, rowKey)
muts = append(muts, mut)
}
errs, err := table.ApplyBulk(ctx, rowKeys, muts)
if err != nil {
return err
}
for _, err := range errs {
if err != nil {
return err
}
}
}
return nil
}
func (s *storageClient) QueryPages(ctx context.Context, query chunk.IndexQuery, callback func(result chunk.ReadBatch, lastPage bool) (shouldContinue bool)) error {
table := s.client.Open(query.TableName)
var rowRange bigtable.RowRange
if len(query.RangeValuePrefix) > 0 {
rowRange = bigtable.PrefixRange(query.HashValue + separator + string(query.RangeValuePrefix))
} else if len(query.RangeValueStart) > 0 {
rowRange = bigtable.InfiniteRange(query.HashValue + separator + string(query.RangeValueStart))
} else {
rowRange = bigtable.PrefixRange(query.HashValue + separator)
}
err := table.ReadRows(ctx, rowRange, func(r bigtable.Row) bool {
// Bigtable doesn't know when to stop, as we're reading "until the end of the
// row" in DynamoDB. So we need to check the prefix of the row is still correct.
if !strings.HasPrefix(r.Key(), query.HashValue+separator) {
return false
}
return callback(bigtableReadBatch(r), false)
}, bigtable.RowFilter(bigtable.FamilyFilter(columnFamily)))
if err != nil {
return errors.WithStack(err)
}
return nil
}
// bigtableReadBatch represents a batch of rows read from Bigtable. As the
// bigtable interface gives us rows one-by-one, a batch always only contains
// a single row.
type bigtableReadBatch bigtable.Row
func (bigtableReadBatch) Len() int {
return 1
}
func (b bigtableReadBatch) RangeValue(index int) []byte {
if index != 0 {
panic("index != 0")
}
// String before the first separator is the hashkey
parts := strings.SplitN(bigtable.Row(b).Key(), separator, 2)
return []byte(parts[1])
}
func (b bigtableReadBatch) Value(index int) []byte {
if index != 0 {
panic("index != 0")
}
cf, ok := b[columnFamily]
if !ok || len(cf) != 1 {
panic("bad response from bigtable")
}
return cf[0].Value
}
func (s *storageClient) PutChunks(ctx context.Context, chunks []chunk.Chunk) error {
keys := map[string][]string{}
muts := map[string][]*bigtable.Mutation{}
for i := range chunks {
// Encode the chunk first - checksum is calculated as a side effect.
buf, err := chunks[i].Encode()
if err != nil {
return err
}
key := chunks[i].ExternalKey()
tableName := s.schemaCfg.ChunkTables.TableFor(chunks[i].From)
keys[tableName] = append(keys[tableName], key)
mut := bigtable.NewMutation()
mut.Set(columnFamily, column, 0, buf)
muts[tableName] = append(muts[tableName], mut)
}
for tableName := range keys {
table := s.client.Open(tableName)
errs, err := table.ApplyBulk(ctx, keys[tableName], muts[tableName])
if err != nil {
return err
}
for _, err := range errs {
if err != nil {
return err
}
}
}
return nil
}
func (s *storageClient) GetChunks(ctx context.Context, input []chunk.Chunk) ([]chunk.Chunk, error) {
chunks := map[string]map[string]chunk.Chunk{}
keys := map[string]bigtable.RowList{}
for _, c := range input {
tableName := s.schemaCfg.ChunkTables.TableFor(c.From)
key := c.ExternalKey()
keys[tableName] = append(keys[tableName], key)
if _, ok := chunks[tableName]; !ok {
chunks[tableName] = map[string]chunk.Chunk{}
}
chunks[tableName][key] = c
}
outs := make(chan chunk.Chunk, len(input))
errs := make(chan error, len(input))
for tableName := range keys {
var (
table = s.client.Open(tableName)
keys = keys[tableName]
chunks = chunks[tableName]
)
for i := 0; i < len(keys); i += maxRowReads {
page := keys[i:util.Min(i+maxRowReads, len(keys))]
go func(page bigtable.RowList) {
// rows are returned in key order, not order in row list
if err := table.ReadRows(ctx, page, func(row bigtable.Row) bool {
chunk, ok := chunks[row.Key()]
if !ok {
errs <- fmt.Errorf("Got row for unknown chunk: %s", row.Key())
return false
}
err := chunk.Decode(row[columnFamily][0].Value)
if err != nil {
errs <- err
return false
}
outs <- chunk
return true
}); err != nil {
errs <- errors.WithStack(err)
}
}(page)
}
}
output := make([]chunk.Chunk, 0, len(input))
for i := 0; i < len(input); i++ {
select {
case c := <-outs:
output = append(output, c)
case err := <-errs:
return nil, err
}
}
return output, nil
}