This repository has been archived by the owner on Nov 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathstream.go
352 lines (310 loc) · 11 KB
/
stream.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
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package arrow // import "go.opentelemetry.io/collector/exporter/otlpexporter/internal/arrow"
import (
"context"
"errors"
"fmt"
"io"
"sync"
arrowpb "github.com/f5/otel-arrow-adapter/api/collector/arrow/v1"
arrowRecord "github.com/f5/otel-arrow-adapter/pkg/otel/arrow_record"
"go.uber.org/multierr"
"go.uber.org/zap"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/consumer/consumererror"
"go.opentelemetry.io/collector/pdata/plog"
"go.opentelemetry.io/collector/pdata/pmetric"
"go.opentelemetry.io/collector/pdata/ptrace"
)
// Stream is 1:1 with gRPC stream.
type Stream struct {
// producer is exclusive to the holder of the stream.
producer arrowRecord.ProducerAPI
// prioritizer has a reference to the stream, this allows it to be severed.
prioritizer *streamPrioritizer
// telemetry are a copy of the exporter's telemetry settings
telemetry component.TelemetrySettings
// client uses the exporter's grpc.ClientConn. this is
// initially nil only set when ArrowStream() calls meaning the
// endpoint recognizes OTLP+Arrow.
client arrowpb.ArrowStreamService_ArrowStreamClient
// toWrite is passes a batch from the sender to the stream writer, which
// includes a dedicated channel for the response.
toWrite chan writeItem
// lock protects waiters.
lock sync.Mutex
// waiters is the response channel for each active batch.
waiters map[string]chan error
}
// writeItem is passed from the sender (a pipeline consumer) to the
// stream writer, which is not bound by the sender's context.
type writeItem struct {
// records is a ptrace.Traces, plog.Logs, or pmetric.Metrics
records interface{}
// errCh is used by the stream reader to unblock the sender
errCh chan error
}
// newStream constructs a stream
func newStream(
producer arrowRecord.ProducerAPI,
prioritizer *streamPrioritizer,
telemetry component.TelemetrySettings,
) *Stream {
return &Stream{
producer: producer,
prioritizer: prioritizer,
telemetry: telemetry,
toWrite: make(chan writeItem, 1),
waiters: map[string]chan error{},
}
}
// setBatchChannel places a waiting consumer's batchID into the waiters map, where
// the stream reader may find it.
func (s *Stream) setBatchChannel(batchID string, errCh chan error) {
s.lock.Lock()
defer s.lock.Unlock()
s.waiters[batchID] = errCh
}
// run blocks the calling goroutine while executing stream logic. run
// will return when the reader and writer are finished. errors will be logged.
func (s *Stream) run(bgctx context.Context, client arrowpb.ArrowStreamServiceClient, grpcOptions []grpc.CallOption) {
ctx, cancel := context.WithCancel(bgctx)
defer cancel()
sc, err := client.ArrowStream(ctx, grpcOptions...)
if err != nil {
// Returning with stream.client == nil signals the
// lack of an Arrow stream endpoint. When all the
// streams return with .client == nil, the ready
// channel will be closed.
//
// Note: These are gRPC server internal errors and
// will cause downgrade to standard OTLP. These
// cannot be simulated by connecting to a gRPC server
// that does not support the ArrowStream service, with
// or without the WaitForReady flag set. In a real
// gRPC server the first Unimplemented code is
// generally delivered to the Recv() call below, so
// this code path is not taken for an ordinary downgrade.
//
// TODO: a more graceful recovery strategy?
s.telemetry.Logger.Error("cannot start arrow stream", zap.Error(err))
return
}
// Setting .client != nil indicates that the endpoint was valid,
// streaming may start. When this stream finishes, it will be
// restarted.
s.client = sc
// ww is used to wait for the writer. Since we wait for the writer,
// the writer's goroutine is not added to exporter waitgroup (e.wg).
var ww sync.WaitGroup
ww.Add(1)
go func() {
defer ww.Done()
defer cancel()
s.write(ctx)
}()
// the result from read() is processed after cancel and wait,
// so we can set s.client = nil in case of a delayed Unimplemented.
err = s.read(ctx)
// Wait for the writer to ensure that all waiters are known.
cancel()
ww.Wait()
if err != nil {
// This branch is reached with an unimplemented status
// with or without the WaitForReady flag.
if status, ok := status.FromError(err); ok && status.Code() == codes.Unimplemented {
// This (client == nil) signals the controller
// to downgrade when all streams have returned
// in that status.
//
// TODO: Note there are partial failure modes
// that will continue to function in a
// degraded mode, such as when half of the
// streams are successful and half of streams
// take this return path. Design a graceful
// recovery mechanism?
s.client = nil
s.telemetry.Logger.Info("arrow is not supported", zap.Error(err))
} else if !errors.Is(err, io.EOF) && !errors.Is(err, context.Canceled) {
// TODO: Should we add debug-level logs for EOF and Canceled?
s.telemetry.Logger.Error("arrow recv", zap.Error(err))
}
}
// The reader and writer have both finished; respond to any
// outstanding waiters.
for _, ch := range s.waiters {
// Note: the top-level OTLP exporter will retry.
ch <- ErrStreamRestarting
}
}
// write repeatedly places this stream into the next-available queue, then
// performs a blocking send(). This returns when the data is in the write buffer,
// the caller waiting on its error channel.
func (s *Stream) write(ctx context.Context) {
for {
// Note: this can't block b/c stream has capacity &
// individual streams shut down synchronously.
s.prioritizer.setReady(s)
// this can block, and if the context is canceled we
// wait for the reader to find this stream.
var wri writeItem
select {
case wri = <-s.toWrite:
case <-ctx.Done():
// Because we did not <-stream.toWrite, there
// is a potential sender race since the stream
// is currently in the ready set.
s.prioritizer.removeReady(s)
return
}
// Note: For the two return statements below there is no potential
// sender race because the stream is not available, as indicated by
// the successful <-stream.toWrite.
batch, err := s.encode(wri.records)
if err != nil {
// TODO: Is this not permanent? Another
// sequence of data might not produce it.
//
// This is some kind of internal error.
wri.errCh <- consumererror.NewPermanent(err)
s.telemetry.Logger.Error("arrow encode", zap.Error(err))
return
}
// Let the receiver knows what to look for.
s.setBatchChannel(batch.BatchId, wri.errCh)
if err := s.client.Send(batch); err != nil {
// The error will be sent to errCh during cleanup for this stream.
// TODO: Should we add debug-level logs for EOF and Canceled?
if !errors.Is(err, io.EOF) && !errors.Is(err, context.Canceled) {
s.telemetry.Logger.Error("arrow send", zap.Error(err))
}
return
}
}
}
// read repeatedly reads a batch status and releases the consumers waiting for
// a response.
func (s *Stream) read(_ context.Context) error {
// Note we do not use the context, the stream context might
// cancel a call to Recv() but the call to processBatchStatus
// is non-blocking.
for {
resp, err := s.client.Recv()
if err != nil {
return err
}
if err = s.processBatchStatus(resp.Statuses); err != nil {
return err
}
}
}
// getSenderChannels takes the stream lock and removes the
// corresonding sender channel for each BatchId. They are returned
// with the same index as the original status, for correlation. Nil
// channels will be returned when there are errors locating the
// sender channel.
func (s *Stream) getSenderChannels(statuses []*arrowpb.StatusMessage) ([]chan error, error) {
var err error
fin := make([]chan error, len(statuses))
s.lock.Lock()
defer s.lock.Unlock()
for idx, status := range statuses {
ch, ok := s.waiters[status.BatchId]
if !ok {
// Will break the stream.
err = multierr.Append(err, fmt.Errorf("unrecognized batch ID: %s", status.BatchId))
continue
}
delete(s.waiters, status.BatchId)
fin[idx] = ch
}
return fin, err
}
// processBatchStatus processes a single response from the server and unblocks the
// associated senders.
func (s *Stream) processBatchStatus(statuses []*arrowpb.StatusMessage) error {
fin, ret := s.getSenderChannels(statuses)
for idx, ch := range fin {
if ch == nil {
// In case getSenderChannels encounters a problem, the
// channel is nil.
continue
}
status := statuses[idx]
if status.StatusCode == arrowpb.StatusCode_OK {
ch <- nil
continue
}
var err error
switch status.ErrorCode {
case arrowpb.ErrorCode_UNAVAILABLE:
// TODO: translate retry information into the form
// exporterhelper recognizes.
err = fmt.Errorf("destination unavailable: %s: %s", status.BatchId, status.ErrorMessage)
case arrowpb.ErrorCode_INVALID_ARGUMENT:
err = consumererror.NewPermanent(
fmt.Errorf("invalid argument: %s: %s", status.BatchId, status.ErrorMessage))
default:
base := fmt.Errorf("unexpected stream response: %s: %s", status.BatchId, status.ErrorMessage)
err = consumererror.NewPermanent(base)
// Will break the stream.
ret = multierr.Append(ret, base)
}
ch <- err
}
return ret
}
// SendAndWait submits a batch of records to be encoded and sent. Meanwhile, this
// goroutine waits on the incoming context or for the asynchronous response to be
// received by the stream reader.
func (s *Stream) SendAndWait(ctx context.Context, records interface{}) error {
errCh := make(chan error, 1)
s.toWrite <- writeItem{
records: records,
errCh: errCh,
}
// Note this ensures the caller's timeout is respected.
select {
case <-ctx.Done():
return ctx.Err()
case err := <-errCh:
return err
}
}
// encode produces the next batch of Arrow records.
func (s *Stream) encode(records interface{}) (_ *arrowpb.BatchArrowRecords, retErr error) {
// Defensively, protect against panics in the Arrow producer function.
defer func() {
if err := recover(); err != nil {
retErr = fmt.Errorf("panic in otel-arrow-adapter: %v", err)
}
}()
var batch *arrowpb.BatchArrowRecords
var err error
switch data := records.(type) {
case ptrace.Traces:
batch, err = s.producer.BatchArrowRecordsFromTraces(data)
case plog.Logs:
batch, err = s.producer.BatchArrowRecordsFromLogs(data)
case pmetric.Metrics:
batch, err = s.producer.BatchArrowRecordsFromMetrics(data)
default:
return nil, fmt.Errorf("unsupported OTLP type: %T", records)
}
return batch, err
}