-
Notifications
You must be signed in to change notification settings - Fork 0
/
streamer.go
288 lines (260 loc) · 8.11 KB
/
streamer.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
package bqworker
import (
"context"
"fmt"
"math/rand"
"sync"
"time"
"cloud.google.com/go/bigquery"
"github.com/gjbae1212/go-bqworker/util"
"github.com/rotisserie/eris"
"golang.org/x/oauth2/google"
"google.golang.org/api/option"
)
type (
Row interface {
bigquery.ValueSaver
ProjectId() string // GCP ProjectId
Schema() *TableSchema // BigQuery Table Schema
PublishedAt() time.Time // PublishedAt is time, such as sends row to Bigquery.
InsertId() string // InsertId is unique id in BigQuery table.
}
Streamer interface {
AddRow(ctx context.Context, row Row) error // Add bigquery row to dispatcher
AddRowSync(ctx context.Context, row Row) error // Add bigquery row to dispatcher sync.
Start() error // Start Streamer
Stop() error // Stop Streamer
}
streamer struct {
sync.Mutex
clientMap map[string]*bigquery.Client // map[gcp project id]client
tableSchemaMap map[string][]*TableSchema // map[gcp project id]client
dispatcher *workerDispatcher
loopDone chan struct{}
cfg *config
}
)
var (
defaultOptions = []Option{
WithQueueSize(defaultQueueSize),
WithWorkerSize(defaultWorkerSize),
WithWorkerStack(defaultWorkerStack),
WithWorkerWaitDuration(defaultWorkerWaitDuration),
WithDispatcherLoopWaitDuration(defaultDispatcherLoopWaitDuration),
WithMaxRetry(defaultMaxRetry),
WithErrorHandler(defaultErrorHandler),
}
)
// AddRow adds row to dispatcher.
func (st *streamer) AddRow(ctx context.Context, row Row) error {
if row == nil || row.PublishedAt().IsZero() || row.ProjectId() == "" || row.InsertId() == "" {
return eris.Wrap(ErrInvalidParams, "")
}
schema := row.Schema()
if schema == nil {
return eris.Wrap(ErrNotFoundBigQueryTableSchema, "")
}
if _, ok := st.clientMap[row.ProjectId()]; !ok {
return eris.Wrap(ErrNotFoundBigQueryClient, "")
}
if err := st.dispatcher.addQueue(ctx, &message{
projectId: row.ProjectId(),
datasetId: schema.datasetId,
tableId: st.getTableId(schema, row.PublishedAt()),
row: row,
}); err != nil {
return eris.Wrap(err, "")
}
return nil
}
// AddRowSync adds row to dispatcher based on sync.
// It's to wait until it is completed.
func (st *streamer) AddRowSync(ctx context.Context, row Row) error {
if row == nil || row.PublishedAt().IsZero() || row.ProjectId() == "" || row.InsertId() == "" {
return eris.Wrap(ErrInvalidParams, "")
}
schema := row.Schema()
if schema == nil {
return eris.Wrap(ErrNotFoundBigQueryTableSchema, "")
}
client, ok := st.clientMap[row.ProjectId()]
if !ok {
return eris.Wrap(ErrNotFoundBigQueryClient, "")
}
tableId := st.getTableId(schema, row.PublishedAt())
if err := putRowsToBigQuery(ctx, client, schema.datasetId, tableId, []*bqRow{&bqRow{row: row}}); err != nil {
return eris.Wrap(err, "")
}
return nil
}
// Start starts streamer.
func (st *streamer) Start() error {
st.Lock()
defer st.Unlock()
st.dispatcher.start()
st.runLoop()
return nil
}
// Stop stops streamer.
func (st *streamer) Stop() error {
st.Lock()
defer st.Unlock()
st.loopDone <- struct{}{}
return nil
}
func (st *streamer) runLoop() error {
go func() {
// distributes time tick.
seed := time.Duration(rand.Int63()%600) * time.Second
ticker := time.NewTicker(st.cfg.dispatcherLoopWaitDuration + seed)
if err := st.createOrUpdateTable(); err != nil {
st.cfg.errFunc(eris.Wrap(err, ""))
}
for {
select {
case <-st.loopDone:
return
case <-ticker.C:
if err := st.createOrUpdateTable(); err != nil {
st.cfg.errFunc(eris.Wrap(err, ""))
}
}
}
}()
return nil
}
// createOrUpdateTable creates or updates bigquery table.
func (st *streamer) createOrUpdateTable() error {
for projectId, schemaList := range st.tableSchemaMap {
for _, schema := range schemaList {
// get bigquery client
client, ok := st.clientMap[projectId]
if !ok {
st.cfg.errFunc(eris.Wrap(ErrNotFoundBigQueryClient, ""))
continue
}
// create or update table.
// today and tomorrow table will be created, if create table action is executed.
for _, t := range []time.Time{time.Now(), time.Now().Add(24 * time.Hour)} {
func() {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
tableId := st.getTableId(schema, t)
table := client.Dataset(schema.datasetId).Table(tableId)
md, err := table.Metadata(ctx)
if err != nil || md == nil { // table not exists
if suberr := table.Create(ctx, schema.meta); suberr != nil {
st.cfg.errFunc(eris.Wrap(suberr, ""))
return
}
fmt.Printf("[%s][bq-table][%s] create %s\n", util.GetHostname(), util.TimeToString(time.Now()), tableId)
return
} else {
// Add new columns to table, if a table exists,
fields := make(map[string]*bigquery.FieldSchema)
for _, field := range schema.meta.Schema {
fields[field.Name] = field
}
for _, field := range md.Schema {
delete(fields, field.Name)
}
if len(fields) > 0 {
newSchemaList := md.Schema
for _, field := range fields {
newSchemaList = append(newSchemaList, field)
}
update := bigquery.TableMetadataToUpdate{Schema: newSchemaList}
if _, suberr := table.Update(ctx, update, ""); suberr != nil {
st.cfg.errFunc(eris.Wrap(suberr, ""))
return
}
fmt.Printf("[%s][bq-table][%s] update %s (%v)\n", util.GetHostname(), util.TimeToString(time.Now()), tableId, fields)
}
return
}
}()
}
}
}
return nil
}
// getTableId get bigquery table id.
func (st *streamer) getTableId(schema *TableSchema, t time.Time) string {
switch schema.period {
case NotExist:
return schema.prefix
case Daily:
return schema.prefix + util.TimeToDailyStringFormat(t)
case Monthly:
return schema.prefix + util.TimeToMonthlyStringFormat(t)
case Yearly:
return schema.prefix + util.TimeToYearlyStringFormat(t)
}
return ""
}
// NewStreamer returns bigquery streamer which inserts data with bulk parallel.
func NewStreamer(tableSchemaGroup []*TableSchemaGroup, opts ...Option) (Streamer, error) {
if len(tableSchemaGroup) == 0 {
return nil, eris.Wrap(ErrInvalidParams, "")
}
cfg := &config{credentialMap: map[string]string{}}
for _, opt := range defaultOptions {
opt.apply(cfg)
}
for _, opt := range opts {
opt.apply(cfg)
}
st := &streamer{cfg: cfg, clientMap: map[string]*bigquery.Client{},
tableSchemaMap: map[string][]*TableSchema{},
loopDone: make(chan struct{}),
}
for _, group := range tableSchemaGroup {
projectId := group.projectId
credential := group.credential
if projectId == "" || credential == "" {
return nil, eris.Wrap(ErrInvalidParams, "")
}
tableSchemaMap := make([]*TableSchema, 0, len(group.tableSchemaList))
for _, schema := range group.tableSchemaList {
if !schema.valid() {
return nil, eris.Wrap(ErrInvalidParams, "")
}
tableSchemaMap = append(tableSchemaMap, schema)
}
// get google jwt obj from jwt bytes.
jwtConfig, err := google.JWTConfigFromJSON([]byte(credential), bigquery.Scope)
if err != nil {
return nil, eris.Wrap(err, "")
}
// create client for BigQuery.
client, err := bigquery.NewClient(context.Background(), projectId,
option.WithTokenSource(jwtConfig.TokenSource(context.Background())))
if err != nil {
return nil, eris.Wrap(err, "")
}
st.cfg.credentialMap[projectId] = credential
st.clientMap[projectId] = client
st.tableSchemaMap[projectId] = tableSchemaMap
}
// create worker dispatcher.
dispatcher, err := newWorkerDispatcher(st.cfg)
if err != nil {
return nil, eris.Wrap(err, "")
}
st.dispatcher = dispatcher
return st, nil
}
// putRowsToBigQuery puts rows to bigquery.
// it must not wrap eris error.
func putRowsToBigQuery(ctx context.Context, client *bigquery.Client, datasetId, tableId string, rows []*bqRow) error {
if ctx == nil || client == nil || datasetId == "" || tableId == "" {
return ErrInvalidParams
}
if len(rows) == 0 {
return nil
}
inserter := client.Dataset(datasetId).Table(tableId).Inserter()
inserter.SkipInvalidRows = true
inserter.IgnoreUnknownValues = true
return inserter.Put(ctx, rows)
}