Debian Codegraph @

service/internal/graph/lifecycle_test.go

raw
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 package graph
5
6 import (
7 "context"
8 "errors"
9 "testing"
10
11 "github.com/stretchr/testify/assert"
12 "github.com/stretchr/testify/require"
13 "gonum.org/v1/gonum/graph/simple"
14
15 "go.opentelemetry.io/collector/component"
16 "go.opentelemetry.io/collector/component/componentstatus"
17 "go.opentelemetry.io/collector/component/componenttest"
18 "go.opentelemetry.io/collector/connector"
19 "go.opentelemetry.io/collector/connector/connectortest"
20 "go.opentelemetry.io/collector/exporter"
21 "go.opentelemetry.io/collector/exporter/exportertest"
22 "go.opentelemetry.io/collector/pipeline"
23 "go.opentelemetry.io/collector/processor"
24 "go.opentelemetry.io/collector/processor/processortest"
25 "go.opentelemetry.io/collector/receiver"
26 "go.opentelemetry.io/collector/receiver/receivertest"
27 "go.opentelemetry.io/collector/service/internal/builders"
28 "go.opentelemetry.io/collector/service/internal/status"
29 "go.opentelemetry.io/collector/service/pipelines"
30 )
31
32 func TestGraphStartStop(t *testing.T) {
33 t.Run("with_internal_telemetry", func(t *testing.T) {
34 setObsConsumerGateForTest(t, true)
35 testGraphStartStop(t)
36 })
37 t.Run("without_internal_telemetry", func(t *testing.T) {
38 setObsConsumerGateForTest(t, false)
39 testGraphStartStop(t)
40 })
41 }
42
43 func testGraphStartStop(t *testing.T) {
44 testCases := []struct {
45 name string
46 edges [][2]component.ID
47 }{
48 {
49 name: "single",
50 edges: [][2]component.ID{
51 {component.MustNewIDWithName("r", "1"), component.MustNewIDWithName("p", "1")},
52 {component.MustNewIDWithName("r", "2"), component.MustNewIDWithName("p", "1")},
53 {component.MustNewIDWithName("p", "1"), component.MustNewIDWithName("p", "2")},
54 {component.MustNewIDWithName("p", "2"), component.MustNewIDWithName("e", "1")},
55 {component.MustNewIDWithName("p", "1"), component.MustNewIDWithName("e", "2")},
56 },
57 },
58 {
59 name: "multi",
60 edges: [][2]component.ID{
61 // Pipeline 1
62 {component.MustNewIDWithName("r", "1"), component.MustNewIDWithName("p", "1")},
63 {component.MustNewIDWithName("r", "2"), component.MustNewIDWithName("p", "1")},
64 {component.MustNewIDWithName("p", "1"), component.MustNewIDWithName("p", "2")},
65 {component.MustNewIDWithName("p", "2"), component.MustNewIDWithName("e", "1")},
66 {component.MustNewIDWithName("p", "1"), component.MustNewIDWithName("e", "2")},
67
68 // Pipeline 2, shares r1 and e2
69 {component.MustNewIDWithName("r", "1"), component.MustNewIDWithName("p", "3")},
70 {component.MustNewIDWithName("p", "3"), component.MustNewIDWithName("e", "2")},
71 },
72 },
73 {
74 name: "connected",
75 edges: [][2]component.ID{
76 // Pipeline 1
77 {component.MustNewIDWithName("r", "1"), component.MustNewIDWithName("p", "1")},
78 {component.MustNewIDWithName("r", "2"), component.MustNewIDWithName("p", "1")},
79 {component.MustNewIDWithName("p", "1"), component.MustNewIDWithName("p", "2")},
80 {component.MustNewIDWithName("p", "2"), component.MustNewIDWithName("e", "1")},
81 {component.MustNewIDWithName("p", "1"), component.MustNewIDWithName("c", "1")},
82
83 // Pipeline 2, shares r1 and c1
84 {component.MustNewIDWithName("r", "1"), component.MustNewIDWithName("p", "3")},
85 {component.MustNewIDWithName("p", "3"), component.MustNewIDWithName("c", "1")},
86
87 // Pipeline 3, emits to e2 and c2
88 {component.MustNewIDWithName("c", "1"), component.MustNewIDWithName("e", "2")},
89 {component.MustNewIDWithName("c", "1"), component.MustNewIDWithName("c", "2")},
90
91 // Pipeline 4, also emits to e2
92 {component.MustNewIDWithName("c", "2"), component.MustNewIDWithName("e", "2")},
93 },
94 },
95 }
96
97 for _, tt := range testCases {
98 t.Run(tt.name, func(t *testing.T) {
99 ctx := &contextWithOrder{
100 Context: context.Background(),
101 order: map[component.ID]int{},
102 }
103
104 pg := &Graph{componentGraph: simple.NewDirectedGraph()}
105 pg.telemetry = componenttest.NewNopTelemetrySettings()
106 pg.instanceIDs = make(map[int64]*componentstatus.InstanceID)
107
108 for _, edge := range tt.edges {
109 f, t := &testNode{id: edge[0]}, &testNode{id: edge[1]}
110 pg.instanceIDs[f.ID()] = &componentstatus.InstanceID{}
111 pg.instanceIDs[t.ID()] = &componentstatus.InstanceID{}
112 pg.componentGraph.SetEdge(simple.Edge{F: f, T: t})
113 }
114
115 require.NoError(t, pg.StartAll(ctx, &Host{Reporter: status.NewReporter(func(*componentstatus.InstanceID, *componentstatus.Event) {}, func(error) {})}))
116 for _, edge := range tt.edges {
117 assert.Greater(t, ctx.order[edge[0]], ctx.order[edge[1]])
118 }
119
120 ctx.order = map[component.ID]int{}
121 require.NoError(t, pg.ShutdownAll(ctx, status.NewNopStatusReporter()))
122 for _, edge := range tt.edges {
123 assert.Less(t, ctx.order[edge[0]], ctx.order[edge[1]])
124 }
125 })
126 }
127 }
128
129 func TestGraphStartStopCycle(t *testing.T) {
130 t.Run("with_internal_telemetry", func(t *testing.T) {
131 setObsConsumerGateForTest(t, true)
132 testGraphStartStopCycle(t)
133 })
134 t.Run("without_internal_telemetry", func(t *testing.T) {
135 setObsConsumerGateForTest(t, false)
136 testGraphStartStopCycle(t)
137 })
138 }
139
140 func testGraphStartStopCycle(t *testing.T) {
141 pg := &Graph{componentGraph: simple.NewDirectedGraph()}
142
143 r1 := &testNode{id: component.MustNewIDWithName("r", "1")}
144 p1 := &testNode{id: component.MustNewIDWithName("p", "1")}
145 c1 := &testNode{id: component.MustNewIDWithName("c", "1")}
146 e1 := &testNode{id: component.MustNewIDWithName("e", "1")}
147
148 pg.instanceIDs = map[int64]*componentstatus.InstanceID{
149 r1.ID(): {},
150 p1.ID(): {},
151 c1.ID(): {},
152 e1.ID(): {},
153 }
154
155 pg.componentGraph.SetEdge(simple.Edge{F: r1, T: p1})
156 pg.componentGraph.SetEdge(simple.Edge{F: p1, T: c1})
157 pg.componentGraph.SetEdge(simple.Edge{F: c1, T: e1})
158 pg.componentGraph.SetEdge(simple.Edge{F: c1, T: p1}) // loop back
159
160 err := pg.StartAll(context.Background(), &Host{Reporter: status.NewReporter(func(*componentstatus.InstanceID, *componentstatus.Event) {}, func(error) {})})
161 require.ErrorContains(t, err, `topo: no topological ordering: cyclic components`)
162
163 err = pg.ShutdownAll(context.Background(), status.NewNopStatusReporter())
164 assert.ErrorContains(t, err, `topo: no topological ordering: cyclic components`)
165 }
166
167 func TestGraphStartStopComponentError(t *testing.T) {
168 t.Run("with_internal_telemetry", func(t *testing.T) {
169 setObsConsumerGateForTest(t, true)
170 testGraphStartStopComponentError(t)
171 })
172 t.Run("without_internal_telemetry", func(t *testing.T) {
173 setObsConsumerGateForTest(t, false)
174 testGraphStartStopComponentError(t)
175 })
176 }
177
178 func testGraphStartStopComponentError(t *testing.T) {
179 pg := &Graph{componentGraph: simple.NewDirectedGraph()}
180 pg.telemetry = componenttest.NewNopTelemetrySettings()
181 r1 := &testNode{
182 id: component.MustNewIDWithName("r", "1"),
183 startErr: errors.New("foo"),
184 }
185 e1 := &testNode{
186 id: component.MustNewIDWithName("e", "1"),
187 shutdownErr: errors.New("bar"),
188 }
189 pg.instanceIDs = map[int64]*componentstatus.InstanceID{
190 r1.ID(): {},
191 e1.ID(): {},
192 }
193 pg.componentGraph.SetEdge(simple.Edge{
194 F: r1,
195 T: e1,
196 })
197 require.ErrorIs(t, pg.StartAll(context.Background(), &Host{Reporter: status.NewReporter(func(*componentstatus.InstanceID, *componentstatus.Event) {}, func(error) {})}), r1.startErr)
198 assert.EqualError(t, pg.ShutdownAll(context.Background(), status.NewNopStatusReporter()), "bar")
199 }
200
201 // This includes all tests from the previous implementation, plus a new one
202 // relevant only to the new graph-based implementation.
203 func TestGraphFailToStartAndShutdown(t *testing.T) {
204 t.Run("with_internal_telemetry", func(t *testing.T) {
205 setObsConsumerGateForTest(t, true)
206 testGraphFailToStartAndShutdown(t)
207 })
208 t.Run("without_internal_telemetry", func(t *testing.T) {
209 setObsConsumerGateForTest(t, false)
210 testGraphFailToStartAndShutdown(t)
211 })
212 }
213
214 func testGraphFailToStartAndShutdown(t *testing.T) {
215 errReceiverFactory := newErrReceiverFactory()
216 errProcessorFactory := newErrProcessorFactory()
217 errExporterFactory := newErrExporterFactory()
218 errConnectorFactory := newErrConnectorFactory()
219 nopReceiverFactory := receivertest.NewNopFactory()
220 nopProcessorFactory := processortest.NewNopFactory()
221 nopExporterFactory := exportertest.NewNopFactory()
222 nopConnectorFactory := connectortest.NewNopFactory()
223
224 set := Settings{
225 Telemetry: componenttest.NewNopTelemetrySettings(),
226 BuildInfo: component.NewDefaultBuildInfo(),
227 ReceiverBuilder: builders.NewReceiver(
228 map[component.ID]component.Config{
229 component.NewID(nopReceiverFactory.Type()): nopReceiverFactory.CreateDefaultConfig(),
230 component.NewID(errReceiverFactory.Type()): errReceiverFactory.CreateDefaultConfig(),
231 },
232 map[component.Type]receiver.Factory{
233 nopReceiverFactory.Type(): nopReceiverFactory,
234 errReceiverFactory.Type(): errReceiverFactory,
235 }),
236 ProcessorBuilder: builders.NewProcessor(
237 map[component.ID]component.Config{
238 component.NewID(nopProcessorFactory.Type()): nopProcessorFactory.CreateDefaultConfig(),
239 component.NewID(errProcessorFactory.Type()): errProcessorFactory.CreateDefaultConfig(),
240 },
241 map[component.Type]processor.Factory{
242 nopProcessorFactory.Type(): nopProcessorFactory,
243 errProcessorFactory.Type(): errProcessorFactory,
244 }),
245 ExporterBuilder: builders.NewExporter(
246 map[component.ID]component.Config{
247 component.NewID(nopExporterFactory.Type()): nopExporterFactory.CreateDefaultConfig(),
248 component.NewID(errExporterFactory.Type()): errExporterFactory.CreateDefaultConfig(),
249 },
250 map[component.Type]exporter.Factory{
251 nopExporterFactory.Type(): nopExporterFactory,
252 errExporterFactory.Type(): errExporterFactory,
253 }),
254 ConnectorBuilder: builders.NewConnector(
255 map[component.ID]component.Config{
256 component.NewIDWithName(nopConnectorFactory.Type(), "conn"): nopConnectorFactory.CreateDefaultConfig(),
257 component.NewIDWithName(errConnectorFactory.Type(), "conn"): errConnectorFactory.CreateDefaultConfig(),
258 },
259 map[component.Type]connector.Factory{
260 nopConnectorFactory.Type(): nopConnectorFactory,
261 errConnectorFactory.Type(): errConnectorFactory,
262 }),
263 }
264
265 dataTypes := []pipeline.Signal{pipeline.SignalTraces, pipeline.SignalMetrics, pipeline.SignalLogs}
266 for _, dt := range dataTypes {
267 t.Run(dt.String()+"/receiver", func(t *testing.T) {
268 set.PipelineConfigs = pipelines.Config{
269 pipeline.NewID(dt): {
270 Receivers: []component.ID{component.MustNewID("nop"), component.MustNewID("err")},
271 Processors: []component.ID{component.MustNewID("nop")},
272 Exporters: []component.ID{component.MustNewID("nop")},
273 },
274 }
275 pipelines, err := Build(context.Background(), set)
276 require.NoError(t, err)
277 require.Error(t, pipelines.StartAll(context.Background(), &Host{Reporter: status.NewReporter(func(*componentstatus.InstanceID, *componentstatus.Event) {}, func(error) {})}))
278 assert.Error(t, pipelines.ShutdownAll(context.Background(), status.NewNopStatusReporter()))
279 })
280
281 t.Run(dt.String()+"/processor", func(t *testing.T) {
282 set.PipelineConfigs = pipelines.Config{
283 pipeline.NewID(dt): {
284 Receivers: []component.ID{component.MustNewID("nop")},
285 Processors: []component.ID{component.MustNewID("nop"), component.MustNewID("err")},
286 Exporters: []component.ID{component.MustNewID("nop")},
287 },
288 }
289 pipelines, err := Build(context.Background(), set)
290 require.NoError(t, err)
291 require.Error(t, pipelines.StartAll(context.Background(), &Host{Reporter: status.NewReporter(func(*componentstatus.InstanceID, *componentstatus.Event) {}, func(error) {})}))
292 assert.Error(t, pipelines.ShutdownAll(context.Background(), status.NewNopStatusReporter()))
293 })
294
295 t.Run(dt.String()+"/exporter", func(t *testing.T) {
296 set.PipelineConfigs = pipelines.Config{
297 pipeline.NewID(dt): {
298 Receivers: []component.ID{component.MustNewID("nop")},
299 Processors: []component.ID{component.MustNewID("nop")},
300 Exporters: []component.ID{component.MustNewID("nop"), component.MustNewID("err")},
301 },
302 }
303 pipelines, err := Build(context.Background(), set)
304 require.NoError(t, err)
305 require.Error(t, pipelines.StartAll(context.Background(), &Host{Reporter: status.NewReporter(func(*componentstatus.InstanceID, *componentstatus.Event) {}, func(error) {})}))
306 assert.Error(t, pipelines.ShutdownAll(context.Background(), status.NewNopStatusReporter()))
307 })
308
309 for _, dt2 := range dataTypes {
310 t.Run(dt.String()+"/"+dt2.String()+"/connector", func(t *testing.T) {
311 set.PipelineConfigs = pipelines.Config{
312 pipeline.NewIDWithName(dt, "in"): {
313 Receivers: []component.ID{component.MustNewID("nop")},
314 Processors: []component.ID{component.MustNewID("nop")},
315 Exporters: []component.ID{component.MustNewID("nop"), component.MustNewIDWithName("err", "conn")},
316 },
317 pipeline.NewIDWithName(dt2, "out"): {
318 Receivers: []component.ID{component.MustNewID("nop"), component.MustNewIDWithName("err", "conn")},
319 Processors: []component.ID{component.MustNewID("nop")},
320 Exporters: []component.ID{component.MustNewID("nop")},
321 },
322 }
323 pipelines, err := Build(context.Background(), set)
324 require.NoError(t, err)
325 require.Error(t, pipelines.StartAll(context.Background(), &Host{Reporter: status.NewReporter(func(*componentstatus.InstanceID, *componentstatus.Event) {}, func(error) {})}))
326 assert.Error(t, pipelines.ShutdownAll(context.Background(), status.NewNopStatusReporter()))
327 })
328 }
329 }
330 }
331
332 func TestStatusReportedOnStartupShutdown(t *testing.T) {
333 t.Run("with_internal_telemetry", func(t *testing.T) {
334 setObsConsumerGateForTest(t, true)
335 testStatusReportedOnStartupShutdown(t)
336 })
337 t.Run("without_internal_telemetry", func(t *testing.T) {
338 setObsConsumerGateForTest(t, false)
339 testStatusReportedOnStartupShutdown(t)
340 })
341 }
342
343 func testStatusReportedOnStartupShutdown(t *testing.T) {
344 rNoErr := &testNode{id: component.MustNewIDWithName("r_no_err", "1")}
345 rStErr := &testNode{id: component.MustNewIDWithName("r_st_err", "1"), startErr: assert.AnError}
346 rSdErr := &testNode{id: component.MustNewIDWithName("r_sd_err", "1"), shutdownErr: assert.AnError}
347
348 eNoErr := &testNode{id: component.MustNewIDWithName("e_no_err", "1")}
349 eStErr := &testNode{id: component.MustNewIDWithName("e_st_err", "1"), startErr: assert.AnError}
350 eSdErr := &testNode{id: component.MustNewIDWithName("e_sd_err", "1"), shutdownErr: assert.AnError}
351
352 instanceIDs := map[*testNode]*componentstatus.InstanceID{
353 rNoErr: componentstatus.NewInstanceID(rNoErr.id, component.KindReceiver),
354 rStErr: componentstatus.NewInstanceID(rStErr.id, component.KindReceiver),
355 rSdErr: componentstatus.NewInstanceID(rSdErr.id, component.KindReceiver),
356 eNoErr: componentstatus.NewInstanceID(eNoErr.id, component.KindExporter),
357 eStErr: componentstatus.NewInstanceID(eStErr.id, component.KindExporter),
358 eSdErr: componentstatus.NewInstanceID(eSdErr.id, component.KindExporter),
359 }
360
361 // compare two maps of status events ignoring timestamp
362 assertEqualStatuses := func(t *testing.T, evMap1, evMap2 map[*componentstatus.InstanceID][]*componentstatus.Event) {
363 assert.Len(t, evMap2, len(evMap1))
364 for id, evts1 := range evMap1 {
365 evts2 := evMap2[id]
366 assert.Len(t, evts2, len(evts1))
367 for i := range evts1 {
368 ev1 := evts1[i]
369 ev2 := evts2[i]
370 assert.Equal(t, ev1.Status(), ev2.Status())
371 assert.Equal(t, ev1.Err(), ev2.Err())
372 }
373 }
374 }
375
376 for _, tt := range []struct {
377 name string
378 edge [2]*testNode
379 expectedStatuses map[*componentstatus.InstanceID][]*componentstatus.Event
380 startupErr error
381 shutdownErr error
382 }{
383 {
384 name: "successful startup/shutdown",
385 edge: [2]*testNode{rNoErr, eNoErr},
386 expectedStatuses: map[*componentstatus.InstanceID][]*componentstatus.Event{
387 instanceIDs[rNoErr]: {
388 componentstatus.NewEvent(componentstatus.StatusStarting),
389 componentstatus.NewEvent(componentstatus.StatusOK),
390 componentstatus.NewEvent(componentstatus.StatusStopping),
391 componentstatus.NewEvent(componentstatus.StatusStopped),
392 },
393 instanceIDs[eNoErr]: {
394 componentstatus.NewEvent(componentstatus.StatusStarting),
395 componentstatus.NewEvent(componentstatus.StatusOK),
396 componentstatus.NewEvent(componentstatus.StatusStopping),
397 componentstatus.NewEvent(componentstatus.StatusStopped),
398 },
399 },
400 },
401 {
402 name: "early startup error",
403 edge: [2]*testNode{rNoErr, eStErr},
404 expectedStatuses: map[*componentstatus.InstanceID][]*componentstatus.Event{
405 instanceIDs[eStErr]: {
406 componentstatus.NewEvent(componentstatus.StatusStarting),
407 componentstatus.NewPermanentErrorEvent(assert.AnError),
408 componentstatus.NewEvent(componentstatus.StatusStopping),
409 componentstatus.NewEvent(componentstatus.StatusStopped),
410 },
411 },
412 startupErr: assert.AnError,
413 },
414 {
415 name: "late startup error",
416 edge: [2]*testNode{rStErr, eNoErr},
417 expectedStatuses: map[*componentstatus.InstanceID][]*componentstatus.Event{
418 instanceIDs[rStErr]: {
419 componentstatus.NewEvent(componentstatus.StatusStarting),
420 componentstatus.NewPermanentErrorEvent(assert.AnError),
421 componentstatus.NewEvent(componentstatus.StatusStopping),
422 componentstatus.NewEvent(componentstatus.StatusStopped),
423 },
424 instanceIDs[eNoErr]: {
425 componentstatus.NewEvent(componentstatus.StatusStarting),
426 componentstatus.NewEvent(componentstatus.StatusOK),
427 componentstatus.NewEvent(componentstatus.StatusStopping),
428 componentstatus.NewEvent(componentstatus.StatusStopped),
429 },
430 },
431 startupErr: assert.AnError,
432 },
433 {
434 name: "early shutdown error",
435 edge: [2]*testNode{rSdErr, eNoErr},
436 expectedStatuses: map[*componentstatus.InstanceID][]*componentstatus.Event{
437 instanceIDs[rSdErr]: {
438 componentstatus.NewEvent(componentstatus.StatusStarting),
439 componentstatus.NewEvent(componentstatus.StatusOK),
440 componentstatus.NewEvent(componentstatus.StatusStopping),
441 componentstatus.NewPermanentErrorEvent(assert.AnError),
442 },
443 instanceIDs[eNoErr]: {
444 componentstatus.NewEvent(componentstatus.StatusStarting),
445 componentstatus.NewEvent(componentstatus.StatusOK),
446 componentstatus.NewEvent(componentstatus.StatusStopping),
447 componentstatus.NewEvent(componentstatus.StatusStopped),
448 },
449 },
450 shutdownErr: assert.AnError,
451 },
452 {
453 name: "late shutdown error",
454 edge: [2]*testNode{rNoErr, eSdErr},
455 expectedStatuses: map[*componentstatus.InstanceID][]*componentstatus.Event{
456 instanceIDs[rNoErr]: {
457 componentstatus.NewEvent(componentstatus.StatusStarting),
458 componentstatus.NewEvent(componentstatus.StatusOK),
459 componentstatus.NewEvent(componentstatus.StatusStopping),
460 componentstatus.NewEvent(componentstatus.StatusStopped),
461 },
462 instanceIDs[eSdErr]: {
463 componentstatus.NewEvent(componentstatus.StatusStarting),
464 componentstatus.NewEvent(componentstatus.StatusOK),
465 componentstatus.NewEvent(componentstatus.StatusStopping),
466 componentstatus.NewPermanentErrorEvent(assert.AnError),
467 },
468 },
469 shutdownErr: assert.AnError,
470 },
471 } {
472 t.Run(tt.name, func(t *testing.T) {
473 pg := &Graph{componentGraph: simple.NewDirectedGraph()}
474 pg.telemetry = componenttest.NewNopTelemetrySettings()
475
476 actualStatuses := make(map[*componentstatus.InstanceID][]*componentstatus.Event)
477 rep := status.NewReporter(func(id *componentstatus.InstanceID, ev *componentstatus.Event) {
478 actualStatuses[id] = append(actualStatuses[id], ev)
479 }, func(error) {
480 })
481
482 e0, e1 := tt.edge[0], tt.edge[1]
483 pg.instanceIDs = map[int64]*componentstatus.InstanceID{
484 e0.ID(): instanceIDs[e0],
485 e1.ID(): instanceIDs[e1],
486 }
487 pg.componentGraph.SetEdge(simple.Edge{F: e0, T: e1})
488
489 require.ErrorIs(t, pg.StartAll(context.Background(), &Host{Reporter: rep}), tt.startupErr)
490 assert.Equal(t, tt.shutdownErr, pg.ShutdownAll(context.Background(), rep))
491 assertEqualStatuses(t, tt.expectedStatuses, actualStatuses)
492 })
493 }
494 }