forked from viamrobotics/rdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcollector.go
292 lines (263 loc) · 8 KB
/
collector.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
// Package data contains the code involved with Viam's Data Management Platform for automatically collecting component
// readings from robots.
package data
import (
"context"
"fmt"
"sync"
"time"
"github.com/benbjohnson/clock"
"github.com/edaniels/golog"
"github.com/pkg/errors"
"go.opencensus.io/trace"
v1 "go.viam.com/api/app/datasync/v1"
"go.viam.com/utils"
"go.viam.com/utils/protoutils"
"google.golang.org/protobuf/types/known/anypb"
"google.golang.org/protobuf/types/known/timestamppb"
"go.viam.com/rdk/resource"
"go.viam.com/rdk/services/datamanager/datacapture"
)
// The cutoff at which if interval < cutoff, a sleep based capture func is used instead of a ticker.
var sleepCaptureCutoff = 2 * time.Millisecond
// CaptureFunc allows the creation of simple Capturers with anonymous functions.
type CaptureFunc func(ctx context.Context, params map[string]*anypb.Any) (interface{}, error)
// Collector collects data to some target.
type Collector interface {
Close()
Collect()
Flush()
}
type collector struct {
clock clock.Clock
captureResults chan *v1.SensorData
captureErrors chan error
interval time.Duration
params map[string]*anypb.Any
lock sync.Mutex
logger golog.Logger
captureWorkers sync.WaitGroup
logRoutine sync.WaitGroup
cancelCtx context.Context
cancel context.CancelFunc
captureFunc CaptureFunc
closed bool
target datacapture.BufferedWriter
}
// Close closes the channels backing the Collector. It should always be called before disposing of a Collector to avoid
// leaking goroutines.
func (c *collector) Close() {
if c.closed {
return
}
c.closed = true
c.cancel()
c.captureWorkers.Wait()
c.lock.Lock()
defer c.lock.Unlock()
if err := c.target.Flush(); err != nil {
c.logger.Errorw("failed to flush capture data", "error", err)
}
close(c.captureErrors)
c.logRoutine.Wait()
//nolint:errcheck
_ = c.logger.Sync()
}
func (c *collector) Flush() {
c.lock.Lock()
defer c.lock.Unlock()
if err := c.target.Flush(); err != nil {
c.logger.Errorw("failed to flush collector", "error", err)
}
}
// Collect starts the Collector, causing it to run c.capturer.Capture every c.interval, and write the results to
// c.target. It blocks until the underlying capture goroutine starts.
func (c *collector) Collect() {
_, span := trace.StartSpan(c.cancelCtx, "data::collector::Collect")
defer span.End()
started := make(chan struct{})
c.captureWorkers.Add(1)
utils.PanicCapturingGo(func() {
defer c.captureWorkers.Done()
c.capture(started)
})
c.captureWorkers.Add(1)
utils.PanicCapturingGo(func() {
defer c.captureWorkers.Done()
if err := c.writeCaptureResults(); err != nil {
c.captureErrors <- errors.Wrap(err, fmt.Sprintf("failed to write to collector %s", c.target.Path()))
}
})
c.logRoutine.Add(1)
utils.PanicCapturingGo(func() {
defer c.logRoutine.Done()
c.logCaptureErrs()
})
<-started
}
// Go's time.Ticker has inconsistent performance with durations of below 1ms [0], so we use a time.Sleep based approach
// when the configured capture interval is below 2ms. A Ticker based approach is kept for longer capture intervals to
// avoid wasting CPU on a thread that's idling for the vast majority of the time.
// [0]: https://www.mail-archive.com/[email protected]/msg46002.html
func (c *collector) capture(started chan struct{}) {
if c.interval < sleepCaptureCutoff {
c.sleepBasedCapture(started)
} else {
c.tickerBasedCapture(started)
}
}
func (c *collector) sleepBasedCapture(started chan struct{}) {
next := c.clock.Now().Add(c.interval)
until := c.clock.Until(next)
var captureWorkers sync.WaitGroup
close(started)
for {
if err := c.cancelCtx.Err(); err != nil {
c.captureErrors <- errors.Wrap(err, "error in context")
captureWorkers.Wait()
close(c.captureResults)
return
}
c.clock.Sleep(until)
select {
case <-c.cancelCtx.Done():
captureWorkers.Wait()
close(c.captureResults)
return
default:
captureWorkers.Add(1)
utils.PanicCapturingGo(func() {
defer captureWorkers.Done()
c.getAndPushNextReading()
})
}
next = next.Add(c.interval)
until = c.clock.Until(next)
}
}
func (c *collector) tickerBasedCapture(started chan struct{}) {
ticker := c.clock.Ticker(c.interval)
defer ticker.Stop()
var captureWorkers sync.WaitGroup
close(started)
for {
if err := c.cancelCtx.Err(); err != nil {
c.captureErrors <- errors.Wrap(err, "error in context")
captureWorkers.Wait()
close(c.captureResults)
return
}
select {
case <-c.cancelCtx.Done():
captureWorkers.Wait()
close(c.captureResults)
return
case <-ticker.C:
captureWorkers.Add(1)
utils.PanicCapturingGo(func() {
defer captureWorkers.Done()
c.getAndPushNextReading()
})
}
}
}
func (c *collector) getAndPushNextReading() {
timeRequested := timestamppb.New(c.clock.Now().UTC())
reading, err := c.captureFunc(c.cancelCtx, c.params)
timeReceived := timestamppb.New(c.clock.Now().UTC())
if err != nil {
c.captureErrors <- errors.Wrap(err, "error while capturing data")
return
}
var msg v1.SensorData
switch v := reading.(type) {
case []byte:
msg = v1.SensorData{
Metadata: &v1.SensorMetadata{
TimeRequested: timeRequested,
TimeReceived: timeReceived,
},
Data: &v1.SensorData_Binary{
Binary: v,
},
}
default:
// If it's not bytes, it's a struct.
pbReading, err := protoutils.StructToStructPb(reading)
if err != nil {
c.captureErrors <- errors.Wrap(err, "error while converting reading to structpb.Struct")
return
}
msg = v1.SensorData{
Metadata: &v1.SensorMetadata{
TimeRequested: timeRequested,
TimeReceived: timeReceived,
},
Data: &v1.SensorData_Struct{
Struct: pbReading,
},
}
}
select {
// If c.captureResults is full, c.captureResults <- a can block indefinitely. This additional select block allows cancel to
// still work when this happens.
case <-c.cancelCtx.Done():
case c.captureResults <- &msg:
}
}
// NewCollector returns a new Collector with the passed capturer and configuration options. It calls capturer at the
// specified Interval, and appends the resulting reading to target.
func NewCollector(captureFunc CaptureFunc, params CollectorParams) (Collector, error) {
if err := params.Validate(); err != nil {
return nil, errors.Wrap(err, fmt.Sprintf("failed to construct collector for %s", params.ComponentName))
}
cancelCtx, cancelFunc := context.WithCancel(context.Background())
var c clock.Clock
if params.Clock == nil {
c = clock.New()
} else {
c = params.Clock
}
return &collector{
captureResults: make(chan *v1.SensorData, params.QueueSize),
captureErrors: make(chan error, params.QueueSize),
interval: params.Interval,
params: params.MethodParams,
logger: params.Logger,
cancelCtx: cancelCtx,
cancel: cancelFunc,
captureFunc: captureFunc,
target: params.Target,
clock: c,
closed: false,
}, nil
}
func (c *collector) writeCaptureResults() error {
for msg := range c.captureResults {
if err := c.target.Write(msg); err != nil {
return err
}
}
return nil
}
func (c *collector) logCaptureErrs() {
for err := range c.captureErrors {
if c.closed {
// Don't log context cancellation errors if the collector has already been closed. This means the collector
// cancelled the context, and the context cancellation error is expected.
if errors.Is(err, context.Canceled) {
continue
}
}
c.logger.Error(err)
}
}
// InvalidInterfaceErr is the error describing when an interface not conforming to the expected resource.API was
// passed into a CollectorConstructor.
func InvalidInterfaceErr(api resource.API) error {
return errors.Errorf("passed interface does not conform to expected resource type %s", api)
}
// FailedToReadErr is the error describing when a Capturer was unable to get the reading of a method.
func FailedToReadErr(component, method string, err error) error {
return errors.Errorf("failed to get reading of method %s of component %s: %v", method, component, err)
}