-
Notifications
You must be signed in to change notification settings - Fork 439
/
Copy pathbigtable_common.go
295 lines (246 loc) · 8.2 KB
/
bigtable_common.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
package db
import (
"context"
"fmt"
"sort"
"strings"
"time"
"github.com/gobitfly/eth2-beaconchain-explorer/types"
"github.com/gobitfly/eth2-beaconchain-explorer/utils"
gcp_bigtable "cloud.google.com/go/bigtable"
"github.com/sirupsen/logrus"
)
func (bigtable *Bigtable) WriteBulk(mutations *types.BulkMutations, table *gcp_bigtable.Table, batchSize int) error {
callingFunctionName := utils.GetParentFuncName()
ctx, done := context.WithTimeout(context.Background(), time.Minute*5)
defer done()
numMutations := len(mutations.Muts)
numKeys := len(mutations.Keys)
if numKeys != numMutations {
return fmt.Errorf("error expected same number of keys as mutations keys: %v mutations: %v", numKeys, numMutations)
}
// pre-sort mutations for efficient bulk inserts
sort.Sort(mutations)
length := batchSize
if length > MAX_BATCH_MUTATIONS {
logger.Infof("WriteBulk: capping provided batchSize %v to %v", length, MAX_BATCH_MUTATIONS)
length = MAX_BATCH_MUTATIONS
}
iterations := numKeys / length
for offset := 0; offset < iterations; offset++ {
start := offset * length
end := offset*length + length
startTime := time.Now()
errs, err := table.ApplyBulk(ctx, mutations.Keys[start:end], mutations.Muts[start:end])
for _, e := range errs {
if e != nil {
return e
}
}
if err != nil {
return err
}
logger.Infof("%s: wrote from %v to %v rows to bigtable in %.1f s", callingFunctionName, start, end, time.Since(startTime).Seconds())
}
if (iterations * length) < numKeys {
start := iterations * length
startTime := time.Now()
errs, err := table.ApplyBulk(ctx, mutations.Keys[start:], mutations.Muts[start:])
if err != nil {
return err
}
for _, e := range errs {
if e != nil {
return e
}
}
logger.Infof("%s: wrote from %v to %v rows to bigtable in %.1fs", callingFunctionName, start, numKeys, time.Since(startTime).Seconds())
return nil
}
return nil
}
func (bigtable *Bigtable) ClearByPrefix(table string, family, columns, prefix string, dryRun bool) error {
if family == "" || prefix == "" || columns == "" {
return fmt.Errorf("please provide family [%v], columns [%v] and prefix [%v]", family, columns, prefix)
}
rowRange := gcp_bigtable.PrefixRange(prefix)
var btTable *gcp_bigtable.Table
switch table {
case "data":
btTable = bigtable.tableData
case "blocks":
btTable = bigtable.tableBlocks
case "metadata_updates":
btTable = bigtable.tableMetadataUpdates
case "metadata":
btTable = bigtable.tableMetadata
case "beaconchain":
btTable = bigtable.tableBeaconchain
case "machine_metrics":
btTable = bigtable.tableMachineMetrics
case "beaconchain_validators":
btTable = bigtable.tableValidators
case "beaconchain_validators_history":
btTable = bigtable.tableValidatorsHistory
default:
return fmt.Errorf("unknown table %v provided", table)
}
mutsDelete := types.NewBulkMutations(MAX_BATCH_MUTATIONS)
var filter gcp_bigtable.Filter
columnsSlice := strings.Split(columns, ",")
if len(columnsSlice) > 1 {
columnNames := make([]gcp_bigtable.Filter, len(columnsSlice))
for i, f := range columnsSlice {
columnNames[i] = gcp_bigtable.ColumnFilter(f)
}
filter = gcp_bigtable.InterleaveFilters(columnNames...)
} else {
filter = gcp_bigtable.ColumnFilter(columnsSlice[0])
}
keysCount := 0
deleteFunc := func(row gcp_bigtable.Row) bool {
var row_ string
if family == "*" {
row_ = row.Key()
} else {
row_ = row[family][0].Row
}
if dryRun {
logger.Infof("would delete key %v", row_)
}
mutDelete := gcp_bigtable.NewMutation()
if columns == "*" {
mutDelete.DeleteRow()
} else {
for _, column := range columnsSlice {
mutDelete.DeleteCellsInColumn(family, column)
}
}
mutsDelete.Keys = append(mutsDelete.Keys, row_)
mutsDelete.Muts = append(mutsDelete.Muts, mutDelete)
keysCount++
// we still need to commit in batches here (instead of just calling WriteBulk only once) as loading all keys to be deleted in memory first is not feasible as the delete function could be used to delete millions of rows
if mutsDelete.Len() == MAX_BATCH_MUTATIONS {
logrus.Infof("deleting %v keys (first key %v, last key %v)", len(mutsDelete.Keys), mutsDelete.Keys[0], mutsDelete.Keys[len(mutsDelete.Keys)-1])
if !dryRun {
err := bigtable.WriteBulk(mutsDelete, btTable, DEFAULT_BATCH_INSERTS)
if err != nil {
logger.Errorf("error writing bulk mutations: %v", err)
return false
}
}
mutsDelete = types.NewBulkMutations(MAX_BATCH_MUTATIONS)
}
return true
}
var err error
if columns == "*" {
err = btTable.ReadRows(context.Background(), rowRange, deleteFunc)
} else {
err = btTable.ReadRows(context.Background(), rowRange, deleteFunc, gcp_bigtable.RowFilter(filter))
}
if err != nil {
return err
}
if !dryRun && mutsDelete.Len() > 0 {
logger.Infof("deleting %v keys (first key %v, last key %v)", len(mutsDelete.Keys), mutsDelete.Keys[0], mutsDelete.Keys[len(mutsDelete.Keys)-1])
err := bigtable.WriteBulk(mutsDelete, btTable, DEFAULT_BATCH_INSERTS)
if err != nil {
return err
}
}
logger.Infof("deleted %v keys", keysCount)
return nil
}
func (bigtable *Bigtable) ClearByRowRange(table string, family, columns string, rowRange gcp_bigtable.RowRangeList, dryRun bool) error {
if family == "" || len(rowRange) == 0 || columns == "" {
return fmt.Errorf("please provide family [%v], columns [%v] and prefix [%v]", family, columns, rowRange)
}
var btTable *gcp_bigtable.Table
switch table {
case "data":
btTable = bigtable.tableData
case "blocks":
btTable = bigtable.tableBlocks
case "metadata_updates":
btTable = bigtable.tableMetadataUpdates
case "metadata":
btTable = bigtable.tableMetadata
case "beaconchain":
btTable = bigtable.tableBeaconchain
case "machine_metrics":
btTable = bigtable.tableMachineMetrics
case "beaconchain_validators":
btTable = bigtable.tableValidators
case "beaconchain_validators_history":
btTable = bigtable.tableValidatorsHistory
default:
return fmt.Errorf("unknown table %v provided", table)
}
mutsDelete := types.NewBulkMutations(MAX_BATCH_MUTATIONS)
var filter gcp_bigtable.Filter
columnsSlice := strings.Split(columns, ",")
if len(columnsSlice) > 1 {
columnNames := make([]gcp_bigtable.Filter, len(columnsSlice))
for i, f := range columnsSlice {
columnNames[i] = gcp_bigtable.ColumnFilter(f)
}
filter = gcp_bigtable.InterleaveFilters(columnNames...)
} else {
filter = gcp_bigtable.ColumnFilter(columnsSlice[0])
}
keysCount := 0
deleteFunc := func(row gcp_bigtable.Row) bool {
var row_ string
if family == "*" {
row_ = row.Key()
} else {
row_ = row[family][0].Row
}
if dryRun {
logger.Infof("would delete key %v", row_)
}
mutDelete := gcp_bigtable.NewMutation()
if columns == "*" {
mutDelete.DeleteRow()
} else {
for _, column := range columnsSlice {
mutDelete.DeleteCellsInColumn(family, column)
}
}
mutsDelete.Keys = append(mutsDelete.Keys, row_)
mutsDelete.Muts = append(mutsDelete.Muts, mutDelete)
keysCount++
// we still need to commit in batches here (instead of just calling WriteBulk only once) as loading all keys to be deleted in memory first is not feasible as the delete function could be used to delete millions of rows
if mutsDelete.Len() == MAX_BATCH_MUTATIONS {
logrus.Infof("deleting %v keys (first key %v, last key %v)", len(mutsDelete.Keys), mutsDelete.Keys[0], mutsDelete.Keys[len(mutsDelete.Keys)-1])
if !dryRun {
err := bigtable.WriteBulk(mutsDelete, btTable, DEFAULT_BATCH_INSERTS)
if err != nil {
logger.Errorf("error writing bulk mutations: %v", err)
return false
}
}
mutsDelete = types.NewBulkMutations(MAX_BATCH_MUTATIONS)
}
return true
}
var err error
if columns == "*" {
err = btTable.ReadRows(context.Background(), rowRange, deleteFunc)
} else {
err = btTable.ReadRows(context.Background(), rowRange, deleteFunc, gcp_bigtable.RowFilter(filter))
}
if err != nil {
return err
}
if !dryRun && mutsDelete.Len() > 0 {
logger.Infof("deleting %v keys (first key %v, last key %v)", len(mutsDelete.Keys), mutsDelete.Keys[0], mutsDelete.Keys[len(mutsDelete.Keys)-1])
err := bigtable.WriteBulk(mutsDelete, btTable, DEFAULT_BATCH_INSERTS)
if err != nil {
return err
}
}
logger.Infof("deleted %v keys", keysCount)
return nil
}