-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathlogger.go
More file actions
289 lines (240 loc) · 7.55 KB
/
logger.go
File metadata and controls
289 lines (240 loc) · 7.55 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
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
package logging
import (
"fmt"
"log"
"math/rand"
"strings"
"sync"
"time"
"github.com/apache/arrow/go/v8/arrow"
"github.com/pkg/errors"
"google.golang.org/protobuf/types/known/timestamppb"
"github.com/feast-dev/feast/go/protos/feast/serving"
"github.com/feast-dev/feast/go/protos/feast/types"
)
type Log struct {
// Example: val{int64_val: 5017}, val{int64_val: 1003}
EntityValue []*types.Value
RequestData []*types.Value
FeatureValues []*types.Value
FeatureStatuses []serving.FieldStatus
EventTimestamps []*timestamppb.Timestamp
RequestId string
LogTimestamp time.Time
}
type LogSink interface {
// Write is used to unload logs from memory buffer.
// Logs are not guaranteed to be flushed to sink on this point.
// The data can just be written to local disk (depending on implementation).
Write(data []arrow.Record) error
// Flush actually send data to a sink.
// We want to control amount to interaction with sink, since it could be a costly operation.
// Also, some sinks like BigQuery might have quotes and physically limit amount of write requests per day.
Flush(featureServiceName string) error
}
type Logger interface {
Log(joinKeyToEntityValues map[string]*types.RepeatedValue, featureVectors []*serving.GetOnlineFeaturesResponse_FeatureVector, featureNames []string, requestData map[string]*types.RepeatedValue, requestId string) error
}
type LoggerImpl struct {
featureServiceName string
buffer *MemoryBuffer
schema *FeatureServiceSchema
logCh chan *Log
signalCh chan interface{}
sink LogSink
config LoggerConfig
isStopped bool
cond *sync.Cond
}
type LoggerConfig struct {
LoggingOptions
SampleRate float32
}
func NewLoggerConfig(sampleRate float32, opts LoggingOptions) LoggerConfig {
return LoggerConfig{
LoggingOptions: opts,
SampleRate: sampleRate,
}
}
func NewLogger(schema *FeatureServiceSchema, featureServiceName string, sink LogSink, config LoggerConfig) (*LoggerImpl, error) {
buffer, err := NewMemoryBuffer(schema)
if err != nil {
return nil, err
}
logger := &LoggerImpl{
featureServiceName: featureServiceName,
logCh: make(chan *Log, config.ChannelCapacity),
signalCh: make(chan interface{}, 2),
sink: sink,
buffer: buffer,
schema: schema,
config: config,
isStopped: false,
cond: sync.NewCond(&sync.Mutex{}),
}
logger.startLoggerLoop()
return logger, nil
}
func (l *LoggerImpl) EmitLog(log *Log) error {
select {
case l.logCh <- log:
return nil
case <-time.After(l.config.EmitTimeout):
return fmt.Errorf("could not add to log channel with capacity %d. Operation timed out. Current log channel length is %d", cap(l.logCh), len(l.logCh))
}
}
func (l *LoggerImpl) startLoggerLoop() {
go func() {
for {
if err := l.loggerLoop(); err != nil {
log.Printf("LoggerImpl[%s] recovered from panic: %+v", l.featureServiceName, err)
// Sleep for a couple of milliseconds to avoid CPU load from a potential infinite panic-recovery loop
time.Sleep(5 * time.Millisecond)
continue // try again
}
// graceful stop
return
}
}()
}
// Select that either ingests new logs that are added to the logging channel, one at a time to add
// to the in-memory buffer or flushes all of them synchronously to the OfflineStorage on a time interval.
func (l *LoggerImpl) loggerLoop() (lErr error) {
defer func() {
// Recover from panic in the logger loop, so that it doesn't bring down the entire feature server
if r := recover(); r != nil {
rErr, ok := r.(error)
if !ok {
rErr = fmt.Errorf("%v", r)
}
lErr = errors.WithStack(rErr)
}
}()
writeTicker := time.NewTicker(l.config.WriteInterval)
flushTicker := time.NewTicker(l.config.FlushInterval)
for {
shouldStop := false
select {
case <-l.signalCh:
err := l.buffer.writeBatch(l.sink)
if err != nil {
log.Printf("Log write failed: %+v", err)
}
err = l.sink.Flush(l.featureServiceName)
if err != nil {
log.Printf("Log flush failed: %+v", err)
}
shouldStop = true
case <-writeTicker.C:
err := l.buffer.writeBatch(l.sink)
if err != nil {
log.Printf("Log write failed: %+v", err)
}
case <-flushTicker.C:
err := l.sink.Flush(l.featureServiceName)
if err != nil {
log.Printf("Log flush failed: %+v", err)
}
case logItem := <-l.logCh:
err := l.buffer.Append(logItem)
if err != nil {
log.Printf("Append log failed: %+v", err)
}
}
if shouldStop {
break
}
}
writeTicker.Stop()
flushTicker.Stop()
// Notify all waiters for graceful stop
l.cond.L.Lock()
l.isStopped = true
l.cond.Broadcast()
l.cond.L.Unlock()
return nil
}
// Stop the loop goroutine gracefully
func (l *LoggerImpl) Stop() {
select {
case l.signalCh <- nil:
default:
}
}
func (l *LoggerImpl) WaitUntilStopped() {
l.cond.L.Lock()
defer l.cond.L.Unlock()
for !l.isStopped {
l.cond.Wait()
}
}
func getFullFeatureName(featureViewName string, featureName string) string {
return fmt.Sprintf("%s__%s", featureViewName, featureName)
}
func (l *LoggerImpl) Log(joinKeyToEntityValues map[string]*types.RepeatedValue, featureVectors []*serving.GetOnlineFeaturesResponse_FeatureVector, featureNames []string, requestData map[string]*types.RepeatedValue, requestId string) error {
if len(featureVectors) == 0 {
return nil
}
if rand.Float32() > l.config.SampleRate {
return nil
}
numFeatures := len(l.schema.Features)
// Should be equivalent to how many entities there are(each feature row has (entity) number of features)
numRows := len(featureVectors[0].Values)
featureNameToVectorIdx := make(map[string]int)
for idx, name := range featureNames {
featureNameToVectorIdx[name] = idx
}
for rowIdx := 0; rowIdx < numRows; rowIdx++ {
featureValues := make([]*types.Value, numFeatures)
featureStatuses := make([]serving.FieldStatus, numFeatures)
eventTimestamps := make([]*timestamppb.Timestamp, numFeatures)
for idx, featureName := range l.schema.Features {
featureIdx, ok := featureNameToVectorIdx[featureName]
if !ok {
featureNameParts := strings.Split(featureName, "__")
featureIdx, ok = featureNameToVectorIdx[featureNameParts[1]]
if !ok {
return errors.Errorf("Missing feature %s in log data", featureName)
}
}
featureValues[idx] = featureVectors[featureIdx].Values[rowIdx]
featureStatuses[idx] = featureVectors[featureIdx].Statuses[rowIdx]
eventTimestamps[idx] = featureVectors[featureIdx].EventTimestamps[rowIdx]
}
entityValues := make([]*types.Value, len(l.schema.JoinKeys))
for idx, joinKey := range l.schema.JoinKeys {
rows, ok := joinKeyToEntityValues[joinKey]
if !ok {
return errors.Errorf("Missing join key %s in log data", joinKey)
}
entityValues[idx] = rows.Val[rowIdx]
}
requestDataValues := make([]*types.Value, len(l.schema.RequestData))
for idx, requestParam := range l.schema.RequestData {
rows, ok := requestData[requestParam]
if !ok {
return errors.Errorf("Missing request parameter %s in log data", requestParam)
}
requestDataValues[idx] = rows.Val[rowIdx]
}
newLog := Log{
EntityValue: entityValues,
RequestData: requestDataValues,
FeatureValues: featureValues,
FeatureStatuses: featureStatuses,
EventTimestamps: eventTimestamps,
RequestId: requestId,
LogTimestamp: time.Now().UTC(),
}
err := l.EmitLog(&newLog)
if err != nil {
return err
}
}
return nil
}
type DummyLoggerImpl struct{}
func (l *DummyLoggerImpl) Log(joinKeyToEntityValues map[string]*types.RepeatedValue, featureVectors []*serving.GetOnlineFeaturesResponse_FeatureVector, featureNames []string, requestData map[string]*types.RepeatedValue, requestId string) error {
return nil
}