-
Notifications
You must be signed in to change notification settings - Fork 781
/
bigtable_object_client.go
182 lines (155 loc) · 4.51 KB
/
bigtable_object_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
package gcp
import (
"context"
"fmt"
"cloud.google.com/go/bigtable"
ot "github.com/opentracing/opentracing-go"
otlog "github.com/opentracing/opentracing-go/log"
"github.com/pkg/errors"
"github.com/cortexproject/cortex/pkg/chunk"
"github.com/cortexproject/cortex/pkg/util/math"
)
type bigtableObjectClient struct {
cfg Config
schemaCfg chunk.SchemaConfig
client *bigtable.Client
}
// NewBigtableObjectClient makes a new chunk.Client that stores chunks in
// Bigtable.
func NewBigtableObjectClient(ctx context.Context, cfg Config, schemaCfg chunk.SchemaConfig) (chunk.Client, error) {
dialOpts, err := cfg.GRPCClientConfig.DialOption(bigtableInstrumentation())
if err != nil {
return nil, err
}
client, err := bigtable.NewClient(ctx, cfg.Project, cfg.Instance, toOptions(dialOpts)...)
if err != nil {
return nil, err
}
return newBigtableObjectClient(cfg, schemaCfg, client), nil
}
func newBigtableObjectClient(cfg Config, schemaCfg chunk.SchemaConfig, client *bigtable.Client) chunk.Client {
return &bigtableObjectClient{
cfg: cfg,
schemaCfg: schemaCfg,
client: client,
}
}
func (s *bigtableObjectClient) Stop() {
s.client.Close()
}
func (s *bigtableObjectClient) PutChunks(ctx context.Context, chunks []chunk.Chunk) error {
keys := map[string][]string{}
muts := map[string][]*bigtable.Mutation{}
for i := range chunks {
buf, err := chunks[i].Encoded()
if err != nil {
return err
}
key := chunks[i].ExternalKey()
tableName, err := s.schemaCfg.ChunkTableFor(chunks[i].From)
if err != nil {
return err
}
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 *bigtableObjectClient) GetChunks(ctx context.Context, input []chunk.Chunk) ([]chunk.Chunk, error) {
sp, ctx := ot.StartSpanFromContext(ctx, "GetChunks")
defer sp.Finish()
sp.LogFields(otlog.Int("chunks requested", len(input)))
chunks := map[string]map[string]chunk.Chunk{}
keys := map[string]bigtable.RowList{}
for _, c := range input {
tableName, err := s.schemaCfg.ChunkTableFor(c.From)
if err != nil {
return nil, err
}
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:math.Min(i+maxRowReads, len(keys))]
go func(page bigtable.RowList) {
decodeContext := chunk.NewDecodeContext()
var processingErr error
var receivedChunks = 0
// rows are returned in key order, not order in row list
err := table.ReadRows(ctx, page, func(row bigtable.Row) bool {
chunk, ok := chunks[row.Key()]
if !ok {
processingErr = errors.WithStack(fmt.Errorf("Got row for unknown chunk: %s", row.Key()))
return false
}
err := chunk.Decode(decodeContext, row[columnFamily][0].Value)
if err != nil {
processingErr = err
return false
}
receivedChunks++
outs <- chunk
return true
})
if processingErr != nil {
errs <- processingErr
} else if err != nil {
errs <- errors.WithStack(err)
} else if receivedChunks < len(page) {
errs <- errors.WithStack(fmt.Errorf("Asked for %d chunks for Bigtable, received %d", len(page), receivedChunks))
}
}(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
case <-ctx.Done():
return nil, ctx.Err()
}
}
return output, nil
}
func (s *bigtableObjectClient) DeleteChunk(ctx context.Context, userID, chunkID string) error {
chunkRef, err := chunk.ParseExternalKey(userID, chunkID)
if err != nil {
return err
}
tableName, err := s.schemaCfg.ChunkTableFor(chunkRef.From)
if err != nil {
return err
}
mut := bigtable.NewMutation()
mut.DeleteCellsInColumn(columnFamily, column)
return s.client.Open(tableName).Apply(ctx, chunkID, mut)
}