-
Notifications
You must be signed in to change notification settings - Fork 0
/
storage.go
409 lines (350 loc) · 11.1 KB
/
storage.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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
// Copyright 2015 The LUCI Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package bigtable
import (
"bytes"
"context"
"errors"
"fmt"
"go.chromium.org/luci/common/data/recordio"
log "go.chromium.org/luci/common/logging"
"go.chromium.org/luci/logdog/common/storage"
"go.chromium.org/luci/logdog/common/types"
"cloud.google.com/go/bigtable"
"google.golang.org/api/option"
"google.golang.org/grpc"
"google.golang.org/grpc/metadata"
)
var (
// StorageScopes is the set of OAuth scopes needed to use the storage
// functionality.
StorageScopes = []string{
bigtable.Scope,
}
// StorageReadOnlyScopes is the set of OAuth scopes needed to use the storage
// functionality.
StorageReadOnlyScopes = []string{
bigtable.ReadonlyScope,
}
)
const (
// tailRowCount is the size of the block of rows that tail read operations
// pull from BigTable. This is designed to be large enough for efficient
// buffering while staying small enough to avoid wasteful reads or
// excessive in-memory buffering.
//
// This is simply the maximum number of rows (limit). The actual number of
// rows will be further constrained by tailRowMaxSize.
tailRowCount = 128
// tailRowMaxSize is the maximum number of bytes of tail row data that will be
// buffered during Tail row reading.
tailRowMaxSize = 1024 * 1024 * 16
)
var (
// errStop is an internal sentinel error used to indicate "stop iteration"
// to btTable.getLogData iterator.
errStop = errors.New("bigtable: stop iteration")
)
// DefaultClientOptions returns a function set of ClientOptions to apply to a
// BigTable client.
func DefaultClientOptions() []option.ClientOption {
return []option.ClientOption{
option.WithGRPCDialOption(grpc.WithDefaultCallOptions(
grpc.MaxCallRecvMsgSize(1024*1024*16),
grpc.MaxCallSendMsgSize(1024*1024*16),
)),
}
}
// Storage is a BigTable storage configuration client.
type Storage struct {
// Client, if not nil, is the BigTable client to use for BigTable accesses.
Client *bigtable.Client
// AdminClient, if not nil, is the BigTable admin client to use for BigTable
// administrator operations.
AdminClient *bigtable.AdminClient
// LogTable is the name of the BigTable table to use for logs.
LogTable string
// Cache, if not nil, will be used to cache data.
Cache storage.Cache
// testBTInterface, if not nil, is the BigTable interface to use. This is
// useful for testing. If nil, this will default to the production instance.
testBTInterface btIface
}
func (s *Storage) getIface() btIface {
if s.testBTInterface != nil {
return s.testBTInterface
}
return prodBTIface{s}
}
// Close implements storage.Storage.
func (s *Storage) Close() {}
// Config implements storage.Storage.
func (s *Storage) Config(c context.Context, cfg storage.Config) error {
if err := s.getIface().setMaxLogAge(c, cfg.MaxLogAge); err != nil {
log.WithError(err).Errorf(c, "Failed to set 'log' GC policy.")
return err
}
log.Fields{
"maxLogAge": cfg.MaxLogAge,
}.Infof(c, "Set maximum log age.")
return nil
}
// Put implements storage.Storage.
func (s *Storage) Put(c context.Context, r storage.PutRequest) error {
c = prepareContext(c)
iface := s.getIface()
rw := rowWriter{
threshold: iface.getMaxRowSize(),
}
for len(r.Values) > 0 {
// Add the next entry to the writer.
if appended := rw.append(r.Values[0]); !appended {
// We have failed to append our maximum BigTable row size. Flush any
// currently-buffered row data and try again with an empty buffer.
count, err := rw.flush(c, iface, r.Index, r.Project, r.Path)
if err != nil {
return err
}
if count == 0 {
// Nothing was buffered, but we still couldn't append an entry. The
// current entry is too large by itself, so we must fail.
return fmt.Errorf("single row entry exceeds maximum size (%d > %d)", len(r.Values[0]), rw.threshold)
}
r.Index += types.MessageIndex(count)
continue
}
// We successfully appended this entry, so advance.
r.Values = r.Values[1:]
}
// Flush any buffered rows.
if _, err := rw.flush(c, iface, r.Index, r.Project, r.Path); err != nil {
return err
}
return nil
}
// Get implements storage.Storage.
func (s *Storage) Get(c context.Context, r storage.GetRequest, cb storage.GetCallback) error {
c = prepareContext(c)
startKey := newRowKey(string(r.Project), string(r.Path), int64(r.Index), 0)
c = log.SetFields(c, log.Fields{
"project": r.Project,
"path": r.Path,
"index": r.Index,
"limit": r.Limit,
"startRowKey": startKey,
"keysOnly": r.KeysOnly,
})
// If we issue a query and get back a legacy row, it will have no count
// associated with it. We will fast-exit
limit := r.Limit
err := s.getIface().getLogData(c, startKey, r.Limit, r.KeysOnly, func(rk *rowKey, data []byte) error {
// Does this key match our requested log stream? If not, we've moved past
// this stream's records and must stop iteration.
if !rk.sharesPathWith(startKey) {
return errStop
}
// Calculate the start index of the contiguous row. Since we index the row
// on the LAST entry in the row, count backwards to get the index of the
// first entry.
startIndex := rk.firstIndex()
if startIndex < 0 {
return storage.ErrBadData
}
// Split our data into records. Leave the records slice nil if we're doing
// a keys-only get.
var records [][]byte
if !r.KeysOnly {
var err error
if records, err = recordio.Split(data); err != nil {
return storage.ErrBadData
}
if rk.count != int64(len(records)) {
log.Fields{
"count": rk.count,
"recordCount": len(records),
}.Errorf(c, "Record count doesn't match declared count.")
return storage.ErrBadData
}
}
// If we are indexed somewhere within this entry's records, discard any
// records before our index.
if discard := int64(r.Index) - startIndex; discard > 0 {
if discard > rk.count {
// This should never happen unless there is corrupt or conflicting data.
return nil
}
startIndex += discard
if !r.KeysOnly {
records = records[discard:]
}
}
log.Fields{
"rk": rk.encode(),
"rkIndex": rk.index,
"rkCount": rk.count,
"startIndex": startIndex,
}.Debugf(c, "Punting row key range [%d - %d]...", startIndex, rk.index)
for index := startIndex; index <= rk.index; index++ {
// If we're not doing keys-only, consume the row.
var row []byte
if !r.KeysOnly {
row, records = records[0], records[1:]
}
if !cb(storage.MakeEntry(row, types.MessageIndex(index))) {
return errStop
}
r.Index = types.MessageIndex(index + 1)
// Artificially apply limit within our row records.
if limit > 0 {
limit--
if limit == 0 {
return errStop
}
}
}
return nil
})
switch err {
case nil, errStop:
return nil
default:
log.WithError(err).Errorf(c, "Failed to retrieve row range.")
return err
}
}
// Tail implements storage.Storage.
func (s *Storage) Tail(c context.Context, project string, path types.StreamPath) (*storage.Entry, error) {
c = prepareContext(c)
c = log.SetFields(c, log.Fields{
"project": project,
"path": path,
})
iface := s.getIface()
// Load the "last tail index" from cache. If we have no cache, start at 0.
var startIdx int64
if s.Cache != nil {
startIdx = getLastTailIndex(c, s.Cache, project, path)
}
// Iterate through all log keys in the stream. Record the latest contiguous
// one.
var (
rk = newRowKey(project, string(path), startIdx, 0)
latest *rowKey
nextIndex = startIdx
)
err := iface.getLogData(c, rk, 0, true, func(rk *rowKey, data []byte) error {
// If this record is non-contiguous, we're done iterating.
if rk.firstIndex() != nextIndex {
return errStop
}
latest, nextIndex = rk, rk.index+1
return nil
})
if err != nil && err != errStop {
log.Fields{
log.ErrorKey: err,
"table": s.LogTable,
}.Errorf(c, "Failed to scan for tail.")
}
if latest == nil {
// No rows for the specified stream.
return nil, storage.ErrDoesNotExist
}
// Update our cache if the tail index has changed.
if s.Cache != nil && startIdx != latest.index {
// We cache the first index in the row so that subsequent cached fetches
// have the correct "startIdx" expectations.
putLastTailIndex(c, s.Cache, project, path, latest.firstIndex())
}
// Fetch the latest row's data.
var d []byte
err = iface.getLogData(c, latest, 1, false, func(rk *rowKey, data []byte) error {
records, err := recordio.Split(data)
if err != nil || len(records) == 0 {
return storage.ErrBadData
}
d = records[len(records)-1]
return errStop
})
if err != nil && err != errStop {
log.Fields{
log.ErrorKey: err,
"table": s.LogTable,
}.Errorf(c, "Failed to retrieve tail row.")
}
return storage.MakeEntry(d, types.MessageIndex(latest.index)), nil
}
// rowWriter facilitates writing several consecutive data values to a single
// BigTable row.
type rowWriter struct {
// buf is the current set of buffered data.
buf bytes.Buffer
// count is the number of rows in the writer.
count int
// threshold is the maximum number of bytes that we can write.
threshold int
}
func (w *rowWriter) append(d []byte) (appended bool) {
origSize := w.buf.Len()
defer func() {
// Restore our previous buffer state if we are reporting the write as
// failed.
if !appended {
w.buf.Truncate(origSize)
}
}()
// Serialize the next entry as a recordio blob.
if _, err := recordio.WriteFrame(&w.buf, d); err != nil {
return
}
// If we have exceeded our threshold, report a failure.
appended = (w.buf.Len() <= w.threshold)
if appended {
w.count++
}
return
}
func (w *rowWriter) flush(c context.Context, iface btIface, index types.MessageIndex,
project string, path types.StreamPath) (int, error) {
flushCount := w.count
if flushCount == 0 {
return 0, nil
}
// Write the current set of buffered rows to the table. Index on the LAST
// row index.
lastIndex := int64(index) + int64(flushCount) - 1
rk := newRowKey(string(project), string(path), lastIndex, int64(w.count))
log.Fields{
"rowKey": rk,
"project": project,
"path": path,
"index": index,
"lastIndex": lastIndex,
"count": w.count,
"size": w.buf.Len(),
}.Debugf(c, "Adding entries to BigTable.")
if err := iface.putLogData(c, rk, w.buf.Bytes()); err != nil {
return 0, err
}
// Reset our buffer state.
w.buf.Reset()
w.count = 0
return flushCount, nil
}
func prepareContext(c context.Context) context.Context {
// Explicitly clear gRPC metadata from the Context. It is forwarded to
// delegate calls by default, and standard request metadata can break BigTable
// calls.
return metadata.NewOutgoingContext(c, nil)
}