forked from dfuse-io/dkafka
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.go
371 lines (329 loc) · 9.89 KB
/
app.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
package dkafka
import (
"context"
"crypto/tls"
"encoding/json"
"fmt"
"io"
"strings"
"time"
"github.com/confluentinc/confluent-kafka-go/kafka"
"github.com/dfuse-io/bstream/forkable"
"github.com/dfuse-io/dfuse-eosio/filtering"
pbcodec "github.com/dfuse-io/dfuse-eosio/pb/dfuse/eosio/codec/v1"
pbbstream "github.com/dfuse-io/pbgo/dfuse/bstream/v1"
pbhealth "github.com/dfuse-io/pbgo/grpc/health/v1"
"github.com/golang/protobuf/ptypes"
"go.uber.org/zap"
"golang.org/x/oauth2"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/credentials/oauth"
"github.com/dfuse-io/shutter"
)
type Config struct {
DfuseGRPCEndpoint string
DfuseToken string
DryRun bool // do not connect to Kafka, just print to stdout
BatchMode bool
StartBlockNum int64
StopBlockNum uint64
StateFile string
KafkaEndpoints string
KafkaSSLEnable bool
KafkaSSLCAFile string
KafkaSSLAuth bool
KafkaSSLClientCertFile string
KafkaSSLClientKeyFile string
KafkaCursorConsumerGroupID string
KafkaTransactionID string
CommitMinDelay time.Duration
IncludeFilterExpr string
KafkaTopic string
KafkaCursorTopic string
KafkaCursorPartition int32
EventSource string
EventKeysExpr string
EventTypeExpr string
EventExtensions map[string]string
}
type App struct {
*shutter.Shutter
config *Config
readinessProbe pbhealth.HealthClient
}
func New(config *Config) *App {
return &App{
Shutter: shutter.New(),
config: config,
}
}
func (a *App) Run() error {
// get and setup the dfuse fetcher that gets a stream of blocks, includes the filter, will include the auth token resolver/refresher
addr := a.config.DfuseGRPCEndpoint
plaintext := strings.Contains(addr, "*")
addr = strings.Replace(addr, "*", "", -1)
var dialOptions []grpc.DialOption
if plaintext {
dialOptions = append(dialOptions, grpc.WithInsecure())
} else {
transportCreds := credentials.NewTLS(&tls.Config{
InsecureSkipVerify: true,
})
dialOptions = append(dialOptions, grpc.WithTransportCredentials(transportCreds))
credential := oauth.NewOauthAccess(&oauth2.Token{AccessToken: a.config.DfuseToken, TokenType: "Bearer"})
dialOptions = append(dialOptions, grpc.WithPerRPCCredentials(credential))
}
conn, err := grpc.Dial(addr,
dialOptions...,
)
if err != nil {
return fmt.Errorf("connecting to grpc address %s: %w", addr, err)
}
client := pbbstream.NewBlockStreamV2Client(conn)
req := &pbbstream.BlocksRequestV2{
IncludeFilterExpr: a.config.IncludeFilterExpr,
StartBlockNum: a.config.StartBlockNum,
StopBlockNum: a.config.StopBlockNum,
}
conf := createKafkaConfig(a.config)
var producer *kafka.Producer
if !a.config.BatchMode || !a.config.DryRun {
producer, err = getKafkaProducer(conf, a.config.KafkaTransactionID)
if err != nil {
return fmt.Errorf("getting kafka producer: %w", err)
}
}
var cp checkpointer
if a.config.BatchMode {
zlog.Info("running in batch mode, ignoring cursors")
cp = &nilCheckpointer{}
} else {
cp = newKafkaCheckpointer(conf, a.config.KafkaCursorTopic, a.config.KafkaCursorPartition, a.config.KafkaTopic, a.config.KafkaCursorConsumerGroupID, producer)
cursor, err := cp.Load()
switch err {
case NoCursorErr:
zlog.Info("running in live mode, no cursor found: starting from beginning", zap.Int64("start_block_num", a.config.StartBlockNum))
case nil:
c, err := forkable.CursorFromOpaque(cursor)
if err != nil {
zlog.Error("cannot decode cursor", zap.Error(err))
return err
}
zlog.Info("running in live mode, found cursor",
zap.String("cursor", cursor),
zap.Stringer("plain_cursor", c),
zap.Stringer("cursor_block", c.Block),
zap.Stringer("cursor_head_block", c.HeadBlock),
zap.Stringer("cursor_LIB", c.LIB),
)
req.StartCursor = cursor
default:
return fmt.Errorf("error loading cursor: %w", err)
}
}
if irreversibleOnly {
req.ForkSteps = []pbbstream.ForkStep{pbbstream.ForkStep_STEP_IRREVERSIBLE}
}
var s sender
if a.config.DryRun {
s = &dryRunSender{}
} else {
s, err = getKafkaSender(producer, cp, a.config.KafkaTransactionID != "")
if err != nil {
return err
}
}
ctx, cancel := context.WithCancel(context.Background())
a.OnTerminating(func(_ error) {
cancel()
})
executor, err := client.Blocks(ctx, req)
if err != nil {
return fmt.Errorf("requesting blocks from dfuse firehose: %w", err)
}
// setup the transformer, that will transform incoming blocks
eventTypeProg, err := exprToCelProgram(a.config.EventTypeExpr)
if err != nil {
return fmt.Errorf("cannot parse event-type-expr: %w", err)
}
eventKeyProg, err := exprToCelProgram(a.config.EventKeysExpr)
if err != nil {
return fmt.Errorf("cannot parse event-keys-expr: %w", err)
}
var extensions []*extension
for k, v := range a.config.EventExtensions {
prog, err := exprToCelProgram(v)
if err != nil {
return fmt.Errorf("cannot parse event-extension: %w", err)
}
extensions = append(extensions, &extension{
name: k,
expr: v,
prog: prog,
})
}
sourceHeader := kafka.Header{
Key: "ce_source",
Value: []byte(a.config.EventSource),
}
specHeader := kafka.Header{
Key: "ce_specversion",
Value: []byte("1.0"),
}
contentTypeHeader := kafka.Header{
Key: "content-type",
Value: []byte("application/json"),
}
dataContentTypeHeader := kafka.Header{
Key: "ce_datacontenttype",
Value: []byte("application/json"),
}
// loop: receive block, transform block, send message...
for {
msg, err := executor.Recv()
if err != nil {
if err == io.EOF {
return nil
}
return fmt.Errorf("error on receive: %w", err)
}
blk := &pbcodec.Block{}
if err := ptypes.UnmarshalAny(msg.Block, blk); err != nil {
return fmt.Errorf("decoding any of type %q: %w", msg.Block.TypeUrl, err)
}
step := sanitizeStep(msg.Step.String())
if blk.Number%100 == 0 {
zlog.Info("incoming block 1/100", zap.Uint32("blk_number", blk.Number), zap.String("step", step), zap.Int("length_filtered_trx_traces", len(blk.FilteredTransactionTraces)))
}
if blk.Number%10 == 0 {
zlog.Debug("incoming block 1/10", zap.Uint32("blk_number", blk.Number), zap.String("step", step), zap.Int("length_filtered_trx_traces", len(blk.FilteredTransactionTraces)))
}
for _, trx := range blk.TransactionTraces() {
status := sanitizeStatus(trx.Receipt.Status.String())
memoizableTrxTrace := &filtering.MemoizableTrxTrace{TrxTrace: trx}
for _, act := range trx.ActionTraces {
if !act.FilteringMatched {
continue
}
var jsonData json.RawMessage
if act.Action.JsonData != "" {
jsonData = json.RawMessage(act.Action.JsonData)
}
activation := filtering.NewActionTraceActivation(
act,
memoizableTrxTrace,
msg.Step.String(),
)
var auths []string
for _, auth := range act.Action.Authorization {
auths = append(auths, auth.Authorization())
}
var globalSeq uint64
if act.Receipt != nil {
globalSeq = act.Receipt.GlobalSequence
}
eosioAction := event{
BlockNum: blk.Number,
BlockID: blk.Id,
Status: status,
Executed: !trx.HasBeenReverted(),
Step: step,
TransactionID: trx.Id,
ActionInfo: ActionInfo{
Account: act.Account(),
Receiver: act.Receiver,
Action: act.Name(),
JSONData: &jsonData,
DBOps: trx.DBOpsForAction(act.ExecutionIndex),
Authorization: auths,
GlobalSequence: globalSeq,
},
}
eventType, err := evalString(eventTypeProg, activation)
if err != nil {
return fmt.Errorf("error eventtype eval: %w", err)
}
extensionsKV := make(map[string]string)
for _, ext := range extensions {
val, err := evalString(ext.prog, activation)
if err != nil {
return fmt.Errorf("program: %w", err)
}
extensionsKV[ext.name] = val
}
eventKeys, err := evalStringArray(eventKeyProg, activation)
if err != nil {
return fmt.Errorf("event keyeval: %w", err)
}
dedupeMap := make(map[string]bool)
for _, eventKey := range eventKeys {
if dedupeMap[eventKey] {
continue
}
dedupeMap[eventKey] = true
headers := []kafka.Header{
kafka.Header{
Key: "ce_id",
Value: hashString(fmt.Sprintf("%s%s%d%s%s", blk.Id, trx.Id, act.ExecutionIndex, msg.Step.String(), eventKey)),
},
sourceHeader,
specHeader,
kafka.Header{
Key: "ce_type",
Value: []byte(eventType),
},
contentTypeHeader,
kafka.Header{
Key: "ce_time",
Value: []byte(blk.MustTime().Format("2006-01-02T15:04:05.9Z")),
},
dataContentTypeHeader,
{
Key: "ce_blkstep",
Value: []byte(step),
},
}
for k, v := range extensionsKV {
headers = append(headers, kafka.Header{
Key: k,
Value: []byte(v),
})
}
msg := kafka.Message{
Key: []byte(eventKey),
Headers: headers,
Value: eosioAction.JSON(),
TopicPartition: kafka.TopicPartition{
Topic: &a.config.KafkaTopic,
},
}
if err := s.Send(&msg); err != nil {
return fmt.Errorf("sending message: %w", err)
}
}
}
}
if a.IsTerminating() {
return s.Commit(context.Background(), msg.Cursor)
}
if err := s.CommitIfAfter(context.Background(), msg.Cursor, a.config.CommitMinDelay); err != nil {
return fmt.Errorf("committing message: %w", err)
}
}
}
func createKafkaConfig(appConf *Config) kafka.ConfigMap {
conf := kafka.ConfigMap{
"bootstrap.servers": appConf.KafkaEndpoints,
}
if appConf.KafkaSSLEnable {
conf["security.protocol"] = "ssl"
conf["ssl.ca.location"] = appConf.KafkaSSLCAFile
}
if appConf.KafkaSSLAuth {
conf["ssl.certificate.location"] = appConf.KafkaSSLClientCertFile
conf["ssl.key.location"] = appConf.KafkaSSLClientKeyFile
//conf["ssl.key.password"] = "keypass"
}
return conf
}