/
async_parser.go
136 lines (122 loc) · 4.02 KB
/
async_parser.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
package airbyte
import (
"bufio"
"encoding/json"
"fmt"
"github.com/jitsucom/jitsu/server/drivers/base"
"github.com/jitsucom/jitsu/server/logging"
"github.com/jitsucom/jitsu/server/schema"
"github.com/jitsucom/jitsu/server/timestamp"
"io"
"math"
"time"
)
const (
airbyteSystem = "Airbyte"
)
// asynchronousParser is an Airbyte read command result parser
type asynchronousParser struct {
dataConsumer base.CLIDataConsumer
streamsRepresentation map[string]*base.StreamRepresentation
logger logging.TaskLogger
}
// Parse reads from stdout and:
//
// parses airbyte output
// applies input schemas
// passes data as batches to dataConsumer
func (ap *asynchronousParser) parse(stdout io.Reader) error {
startTime := timestamp.Now()
timeInDestinations := time.Duration(0)
totalCount := 0
ap.logger.INFO("Airbyte sync will store data as batches >= [%d] elements size", Instance.batchSize)
output := base.NewCLIOutputRepresentation()
for streamName, representation := range ap.streamsRepresentation {
output.AddStream(streamName, &base.StreamRepresentation{
StreamName: streamName,
BatchHeader: &schema.BatchHeader{TableName: representation.BatchHeader.TableName, Fields: representation.BatchHeader.Fields.Clone()},
KeyFields: representation.KeyFields,
Objects: []map[string]interface{}{},
NeedClean: representation.NeedClean,
})
}
scanner := bufio.NewScanner(stdout)
buf := make([]byte, 0, 64*1024)
scanner.Buffer(buf, math.MaxInt/2)
records := 0
for scanner.Scan() {
lineBytes := scanner.Bytes()
row := &Row{}
err := json.Unmarshal(lineBytes, row)
if err != nil {
ap.logger.LOG(string(lineBytes), airbyteSystem, logging.DEBUG)
continue
}
switch row.Type {
case LogType:
if row.Log == nil {
return fmt.Errorf("Error parsing airbyte log line %s: 'log' doesn't exist", string(lineBytes))
}
switch row.Log.Level {
case "ERROR":
ap.logger.LOG(row.Log.Message, airbyteSystem, logging.ERROR)
case "DEBUG":
ap.logger.LOG(row.Log.Message, airbyteSystem, logging.DEBUG)
case "INFO":
ap.logger.LOG(row.Log.Message, airbyteSystem, logging.INFO)
case "WARN":
ap.logger.LOG(row.Log.Message, airbyteSystem, logging.WARN)
default:
ap.logger.LOG(row.Log.Message, airbyteSystem, logging.DEBUG)
}
case StateType:
if row.State == nil || row.State.Data == nil {
return fmt.Errorf("Error parsing airbyte state line %s: malformed state line 'data' doesn't exist", string(lineBytes))
}
output.State = row.State.Data
case RecordType:
records++
if row.Record == nil || row.Record.Data == nil {
return fmt.Errorf("Error parsing airbyte record line %s: %v", string(lineBytes), err)
}
s, _ := output.GetStream(row.Record.Stream)
s.Objects = append(s.Objects, row.Record.Data)
logging.Debugf("[%s] row#%d: %+v", row.Record.Stream, totalCount+records, row.Record.Data)
default:
ap.logger.LOG(string(lineBytes), airbyteSystem, logging.DEBUG)
}
//persist batch and recreate variables
if records >= Instance.batchSize {
startDestinationTime := timestamp.Now()
err := ap.dataConsumer.Consume(output)
if err != nil {
return err
}
timeInDestinations += timestamp.Now().Sub(startDestinationTime)
totalCount += records
//remove already persisted objects
for _, stream := range output.GetStreams() {
stream.Objects = []map[string]interface{}{}
}
output.State = nil
records = 0
}
}
//persist last batch
if records > 0 || output.State != nil {
startDestinationTime := timestamp.Now()
err := ap.dataConsumer.Consume(output)
if err != nil {
return err
}
timeInDestinations += timestamp.Now().Sub(startDestinationTime)
totalCount += records
}
err := scanner.Err()
if err != nil {
return err
}
totalTime := timestamp.Now().Sub(startTime)
ap.logger.INFO("Sync finished in %s (storage time: %s), %d records processed, avg speed: %.2f records per sec", totalTime.Round(time.Second), timeInDestinations.Round(time.Second), totalCount, float64(totalCount)/totalTime.Seconds())
return nil
}