-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathmemorybuffer.go
More file actions
190 lines (155 loc) · 5.49 KB
/
memorybuffer.go
File metadata and controls
190 lines (155 loc) · 5.49 KB
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
package logging
import (
"fmt"
"github.com/apache/arrow/go/v17/arrow"
"github.com/apache/arrow/go/v17/arrow/array"
"github.com/apache/arrow/go/v17/arrow/memory"
"github.com/feast-dev/feast/go/protos/feast/types"
gotypes "github.com/feast-dev/feast/go/types"
)
type MemoryBuffer struct {
logs []*Log
schema *FeatureServiceSchema
arrowSchema *arrow.Schema
records []arrow.Record
}
const (
LOG_TIMESTAMP_FIELD = "__log_timestamp"
LOG_DATE_FIELD = "__log_date"
LOG_REQUEST_ID_FIELD = "__request_id"
RECORD_SIZE = 1000
)
func NewMemoryBuffer(schema *FeatureServiceSchema) (*MemoryBuffer, error) {
arrowSchema, err := getArrowSchema(schema)
if err != nil {
return nil, err
}
return &MemoryBuffer{
logs: make([]*Log, 0),
records: make([]arrow.Record, 0),
schema: schema,
arrowSchema: arrowSchema,
}, nil
}
// Acquires the logging schema from the feature service, converts the memory buffer array of rows of logs and flushes
// them to the offline storage.
func (b *MemoryBuffer) writeBatch(sink LogSink) error {
if len(b.logs) > 0 {
err := b.Compact()
if err != nil {
return err
}
}
if len(b.records) == 0 {
return nil
}
err := sink.Write(b.records)
if err != nil {
return err
}
b.records = b.records[:0]
return nil
}
func (b *MemoryBuffer) Append(log *Log) error {
b.logs = append(b.logs, log)
if len(b.logs) == RECORD_SIZE {
return b.Compact()
}
return nil
}
func (b *MemoryBuffer) Compact() error {
rec, err := b.convertToArrowRecord()
if err != nil {
return err
}
b.records = append(b.records, rec)
b.logs = b.logs[:0]
return nil
}
func getArrowSchema(schema *FeatureServiceSchema) (*arrow.Schema, error) {
fields := make([]arrow.Field, 0)
for _, joinKey := range schema.JoinKeys {
arrowType, err := gotypes.ValueTypeEnumToArrowType(schema.JoinKeysTypes[joinKey])
if err != nil {
return nil, err
}
fields = append(fields, arrow.Field{Name: joinKey, Type: arrowType})
}
for _, requestParam := range schema.RequestData {
arrowType, err := gotypes.ValueTypeEnumToArrowType(schema.RequestDataTypes[requestParam])
if err != nil {
return nil, err
}
fields = append(fields, arrow.Field{Name: requestParam, Type: arrowType})
}
for _, featureName := range schema.Features {
arrowType, err := gotypes.ValueTypeEnumToArrowType(schema.FeaturesTypes[featureName])
if err != nil {
return nil, err
}
fields = append(fields, arrow.Field{Name: featureName, Type: arrowType})
fields = append(fields, arrow.Field{
Name: fmt.Sprintf("%s__timestamp", featureName),
Type: arrow.FixedWidthTypes.Timestamp_s})
fields = append(fields, arrow.Field{
Name: fmt.Sprintf("%s__status", featureName),
Type: arrow.PrimitiveTypes.Int32})
}
fields = append(fields, arrow.Field{Name: LOG_TIMESTAMP_FIELD, Type: arrow.FixedWidthTypes.Timestamp_us})
fields = append(fields, arrow.Field{Name: LOG_DATE_FIELD, Type: arrow.FixedWidthTypes.Date32})
fields = append(fields, arrow.Field{Name: LOG_REQUEST_ID_FIELD, Type: arrow.BinaryTypes.String})
return arrow.NewSchema(fields, nil), nil
}
// convertToArrowRecord Takes memory buffer of logs in array row and converts them to columnar with generated fcoschema generated by GetFcoSchema
// and writes them to arrow table.
// Returns arrow table that contains all of the logs in columnar format.
func (b *MemoryBuffer) convertToArrowRecord() (arrow.Record, error) {
arrowMemory := memory.NewGoAllocator()
numRows := len(b.logs)
columns := make(map[string][]*types.Value)
fieldNameToIdx := make(map[string]int)
for idx, field := range b.arrowSchema.Fields() {
fieldNameToIdx[field.Name] = idx
}
builder := array.NewRecordBuilder(arrowMemory, b.arrowSchema)
defer builder.Release()
builder.Reserve(numRows)
for rowIdx, logRow := range b.logs {
for colIdx, joinKey := range b.schema.JoinKeys {
if _, ok := columns[joinKey]; !ok {
columns[joinKey] = make([]*types.Value, numRows)
}
columns[joinKey][rowIdx] = logRow.EntityValue[colIdx]
}
for colIdx, requestParam := range b.schema.RequestData {
if _, ok := columns[requestParam]; !ok {
columns[requestParam] = make([]*types.Value, numRows)
}
columns[requestParam][rowIdx] = logRow.RequestData[colIdx]
}
for colIdx, featureName := range b.schema.Features {
if _, ok := columns[featureName]; !ok {
columns[featureName] = make([]*types.Value, numRows)
}
columns[featureName][rowIdx] = logRow.FeatureValues[colIdx]
timestamp := arrow.Timestamp(logRow.EventTimestamps[colIdx].GetSeconds())
timestampFieldIdx := fieldNameToIdx[fmt.Sprintf("%s__timestamp", featureName)]
statusFieldIdx := fieldNameToIdx[fmt.Sprintf("%s__status", featureName)]
builder.Field(timestampFieldIdx).(*array.TimestampBuilder).UnsafeAppend(timestamp)
builder.Field(statusFieldIdx).(*array.Int32Builder).UnsafeAppend(int32(logRow.FeatureStatuses[colIdx]))
}
logTimestamp := arrow.Timestamp(logRow.LogTimestamp.UnixMicro())
logDate := arrow.Date32FromTime(logRow.LogTimestamp)
builder.Field(fieldNameToIdx[LOG_TIMESTAMP_FIELD]).(*array.TimestampBuilder).UnsafeAppend(logTimestamp)
builder.Field(fieldNameToIdx[LOG_DATE_FIELD]).(*array.Date32Builder).UnsafeAppend(logDate)
builder.Field(fieldNameToIdx[LOG_REQUEST_ID_FIELD]).(*array.StringBuilder).Append(logRow.RequestId)
}
for columnName, protoArray := range columns {
fieldIdx := fieldNameToIdx[columnName]
err := gotypes.CopyProtoValuesToArrowArray(builder.Field(fieldIdx), protoArray)
if err != nil {
return nil, err
}
}
return builder.NewRecord(), nil
}