-
Notifications
You must be signed in to change notification settings - Fork 0
/
bigtable.go
240 lines (206 loc) · 6.49 KB
/
bigtable.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
// 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 (
"context"
"fmt"
"time"
"go.chromium.org/luci/logdog/common/storage"
"go.chromium.org/luci/common/errors"
"go.chromium.org/luci/common/retry/transient"
"go.chromium.org/luci/grpc/grpcutil"
"cloud.google.com/go/bigtable"
"google.golang.org/grpc/codes"
)
const (
logColumnFamily = "log"
// The data column stores raw low row data (RecordIO blob).
logColumn = "data"
logColName = logColumnFamily + ":" + logColumn
)
// Limits taken from here:
// https://cloud.google.com/bigtable/docs/schema-design
const (
// bigTableRowMaxBytes is the maximum number of bytes that a single BigTable
// row may hold.
bigTableRowMaxBytes = 1024 * 1024 * 10 // 10MB
)
// btGetCallback is a callback that is invoked for each log data row returned
// by getLogData.
//
// If an error is encountered, no more log data will be fetched. The error will
// be propagated to the getLogData call.
type btGetCallback func(*rowKey, []byte) error
// btIface is a general interface for BigTable operations intended to enable
// unit tests to stub out BigTable without adding runtime inefficiency.
type btIface interface {
// putLogData adds new log data to BigTable.
//
// If data already exists for the named row, it will return storage.ErrExists
// and not add the data.
putLogData(context.Context, *rowKey, []byte) error
// getLogData retrieves rows belonging to the supplied stream record, starting
// with the first index owned by that record. The supplied callback is invoked
// once per retrieved row.
//
// rk is the starting row key.
//
// If the supplied limit is nonzero, no more than limit rows will be
// retrieved.
//
// If keysOnly is true, then the callback will return nil row data.
getLogData(c context.Context, rk *rowKey, limit int, keysOnly bool, cb btGetCallback) error
// setMaxLogAge updates the maximum log age policy for the log family.
setMaxLogAge(context.Context, time.Duration) error
// getMaxRowSize returns the maximum row size that this implementation
// supports.
getMaxRowSize() int
}
// prodBTIface is a production implementation of a "btIface".
type prodBTIface struct {
*Storage
}
func (bti prodBTIface) getLogTable() (*bigtable.Table, error) {
if bti.Client == nil {
return nil, errors.New("no client configured")
}
return bti.Client.Open(bti.LogTable), nil
}
func (bti prodBTIface) putLogData(c context.Context, rk *rowKey, data []byte) error {
logTable, err := bti.getLogTable()
if err != nil {
return err
}
m := bigtable.NewMutation()
m.Set(logColumnFamily, logColumn, bigtable.ServerTime, data)
cm := bigtable.NewCondMutation(bigtable.RowKeyFilter(rk.encode()), nil, m)
rowExists := false
if err := logTable.Apply(c, rk.encode(), cm, bigtable.GetCondMutationResult(&rowExists)); err != nil {
return wrapIfTransientForApply(err)
}
if rowExists {
return storage.ErrExists
}
return nil
}
func (bti prodBTIface) getLogData(c context.Context, rk *rowKey, limit int, keysOnly bool, cb btGetCallback) error {
logTable, err := bti.getLogTable()
if err != nil {
return err
}
// Construct read options based on Get request.
ropts := []bigtable.ReadOption{
bigtable.RowFilter(bigtable.FamilyFilter(logColumnFamily)),
bigtable.RowFilter(bigtable.ColumnFilter(logColumn)),
nil,
}[:2]
if keysOnly {
ropts = append(ropts, bigtable.RowFilter(bigtable.StripValueFilter()))
}
if limit > 0 {
ropts = append(ropts, bigtable.LimitRows(int64(limit)))
}
// This will limit the range to the immediate row key ("ASDF~INDEX") to
// immediately after the row key ("ASDF~~"). See rowKey for more information.
rng := bigtable.NewRange(rk.encode(), rk.pathPrefixUpperBound())
var innerErr error
err = logTable.ReadRows(c, rng, func(row bigtable.Row) bool {
data, err := getLogRowData(row)
if err != nil {
innerErr = storage.ErrBadData
return false
}
drk, err := decodeRowKey(row.Key())
if err != nil {
innerErr = err
return false
}
if err := cb(drk, data); err != nil {
innerErr = err
return false
}
return true
}, ropts...)
if err != nil {
return grpcutil.WrapIfTransient(err)
}
if innerErr != nil {
return innerErr
}
return nil
}
func (bti prodBTIface) setMaxLogAge(c context.Context, d time.Duration) error {
if bti.AdminClient == nil {
return errors.New("no admin client configured")
}
var logGCPolicy bigtable.GCPolicy
if d > 0 {
logGCPolicy = bigtable.MaxAgePolicy(d)
}
if err := bti.AdminClient.SetGCPolicy(c, bti.LogTable, logColumnFamily, logGCPolicy); err != nil {
return grpcutil.WrapIfTransient(err)
}
return nil
}
func (bti prodBTIface) getMaxRowSize() int { return bigTableRowMaxBytes }
// getLogRowData loads the []byte contents of the supplied log row.
//
// If the row doesn't exist, storage.ErrDoesNotExist will be returned.
func getLogRowData(row bigtable.Row) (data []byte, err error) {
items, ok := row[logColumnFamily]
if !ok {
err = storage.ErrDoesNotExist
return
}
for _, item := range items {
switch item.Column {
case logColName:
data = item.Value
return
}
}
// If no fields could be extracted, the rows does not exist.
err = storage.ErrDoesNotExist
return
}
// getReadItem retrieves a specific RowItem from the supplied Row.
func getReadItem(row bigtable.Row, family, column string) *bigtable.ReadItem {
// Get the row for our family.
items, ok := row[logColumnFamily]
if !ok {
return nil
}
// Get the specific ReadItem for our column
colName := fmt.Sprintf("%s:%s", family, column)
for _, item := range items {
if item.Column == colName {
return &item
}
}
return nil
}
func wrapIfTransientForApply(err error) error {
if err == nil {
return nil
}
// For Apply, assume that anything other than InvalidArgument (bad data) is
// transient. We exempt InvalidArgument because our data construction is
// deterministic, and so this request can never succeed.
switch code := grpcutil.Code(err); code {
case codes.InvalidArgument:
return err
default:
return transient.Tag.Apply(err)
}
}