-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
mod.rs
1436 lines (1265 loc) · 46.8 KB
/
mod.rs
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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//! Topology contains all topology based types.
//!
//! Topology is broken up into two main sections. The first
//! section contains all the main topology types include `Topology`
//! and the ability to start, stop and reload a config. The second
//! part contains config related items including config traits for
//! each type of component.
pub mod builder;
pub mod fanout;
mod task;
use crate::{
buffers::{self, EventStream},
config::{Config, ConfigDiff, HealthcheckOptions, Resource},
event::Event,
shutdown::SourceShutdownCoordinator,
topology::{
builder::Pieces,
task::{Task, TaskOutput},
},
trigger::DisabledTrigger,
};
use futures::{future, Future, FutureExt, SinkExt};
use std::{
collections::{HashMap, HashSet},
panic::AssertUnwindSafe,
pin::Pin,
sync::{Arc, Mutex},
};
use tokio::{
sync::{mpsc, watch},
time::{interval, sleep_until, Duration, Instant},
};
use tracing_futures::Instrument;
type TaskHandle = tokio::task::JoinHandle<Result<TaskOutput, ()>>;
type BuiltBuffer = (
buffers::BufferInputCloner<Event>,
Arc<Mutex<Option<Pin<EventStream>>>>,
buffers::Acker,
);
type Outputs = HashMap<String, fanout::ControlChannel>;
// Watcher types for topology changes. These are currently specific to receiving
// `Outputs`. This could be expanded in the future to send an enum of types if, for example,
// this included a new 'Inputs' type.
type WatchTx = watch::Sender<Outputs>;
pub type WatchRx = watch::Receiver<Outputs>;
#[allow(dead_code)]
pub struct RunningTopology {
inputs: HashMap<String, buffers::BufferInputCloner<Event>>,
outputs: HashMap<String, fanout::ControlChannel>,
source_tasks: HashMap<String, TaskHandle>,
tasks: HashMap<String, TaskHandle>,
shutdown_coordinator: SourceShutdownCoordinator,
detach_triggers: HashMap<String, DisabledTrigger>,
config: Config,
abort_tx: mpsc::UnboundedSender<()>,
watch: (WatchTx, WatchRx),
}
pub async fn start_validated(
config: Config,
diff: ConfigDiff,
mut pieces: Pieces,
) -> Option<(RunningTopology, mpsc::UnboundedReceiver<()>)> {
let (abort_tx, abort_rx) = mpsc::unbounded_channel();
let mut running_topology = RunningTopology {
inputs: HashMap::new(),
outputs: HashMap::new(),
config,
shutdown_coordinator: SourceShutdownCoordinator::default(),
detach_triggers: HashMap::new(),
source_tasks: HashMap::new(),
tasks: HashMap::new(),
abort_tx,
watch: watch::channel(HashMap::new()),
};
if !running_topology
.run_healthchecks(&diff, &mut pieces, running_topology.config.healthchecks)
.await
{
return None;
}
running_topology.connect_diff(&diff, &mut pieces).await;
running_topology.spawn_diff(&diff, pieces);
Some((running_topology, abort_rx))
}
pub async fn build_or_log_errors(
config: &Config,
diff: &ConfigDiff,
buffers: HashMap<String, BuiltBuffer>,
) -> Option<Pieces> {
match builder::build_pieces(config, diff, buffers).await {
Err(errors) => {
for error in errors {
error!(message = "Configuration error.", %error);
}
None
}
Ok(new_pieces) => Some(new_pieces),
}
}
pub fn take_healthchecks(diff: &ConfigDiff, pieces: &mut Pieces) -> Vec<(String, Task)> {
(&diff.sinks.to_change | &diff.sinks.to_add)
.into_iter()
.filter_map(|name| {
pieces
.healthchecks
.remove(&name)
.map(move |task| (name, task))
})
.collect()
}
impl RunningTopology {
/// Returned future will finish once all current sources have finished.
pub fn sources_finished(&self) -> future::BoxFuture<'static, ()> {
self.shutdown_coordinator.shutdown_tripwire()
}
/// Sends the shutdown signal to all sources and returns a future that resolves
/// once all components (sources, transforms, and sinks) have finished shutting down.
/// Transforms and sinks should shut down automatically once their input tasks finish.
/// Note that this takes ownership of `self`, so once this function returns everything in the
/// RunningTopology instance has been dropped except for the `tasks` map, which gets moved
/// into the returned future and is used to poll for when the tasks have completed. Once the
/// returned future is dropped then everything from this RunningTopology instance is fully
/// dropped.
pub fn stop(self) -> impl Future<Output = ()> {
// Create handy handles collections of all tasks for the subsequent operations.
let mut wait_handles = Vec::new();
// We need a Vec here since source components have two tasks. One for pump in self.tasks,
// and the other for source in self.source_tasks.
let mut check_handles = HashMap::<String, Vec<_>>::new();
// We need to give some time to the sources to gracefully shutdown, so we will merge
// them with other tasks.
for (name, task) in self.tasks.into_iter().chain(self.source_tasks.into_iter()) {
let task = task.map(|_result| ()).shared();
wait_handles.push(task.clone());
check_handles.entry(name).or_default().push(task);
}
// If we reach this, we will forcefully shutdown the sources.
let deadline = Instant::now() + Duration::from_secs(60);
// If we reach the deadline, this future will print out which components won't
// gracefully shutdown since we will start to forcefully shutdown the sources.
let mut check_handles2 = check_handles.clone();
let timeout = async move {
sleep_until(deadline).await;
// Remove all tasks that have shutdown.
check_handles2.retain(|_name, handles| {
retain(handles, |handle| handle.peek().is_none());
!handles.is_empty()
});
let remaining_components = check_handles2.keys().cloned().collect::<Vec<_>>();
error!(
message = "Failed to gracefully shut down in time. Killing components.",
components = ?remaining_components.join(", ")
);
};
// Reports in intervals which components are still running.
let mut interval = interval(Duration::from_secs(5));
let reporter = async move {
loop {
interval.tick().await;
// Remove all tasks that have shutdown.
check_handles.retain(|_name, handles| {
retain(handles, |handle| handle.peek().is_none());
!handles.is_empty()
});
let remaining_components = check_handles.keys().cloned().collect::<Vec<_>>();
// TODO: replace with checked_duration_since once it's stable
let time_remaining = if deadline > Instant::now() {
format!("{} seconds left", (deadline - Instant::now()).as_secs())
} else {
"overdue".to_string()
};
info!(
message = "Shutting down... Waiting on running components.", remaining_components = ?remaining_components.join(", "), time_remaining = ?time_remaining
);
}
};
// Finishes once all tasks have shutdown.
let success = futures::future::join_all(wait_handles).map(|_| ());
// Aggregate future that ends once anything detects that all tasks have shutdown.
let shutdown_complete_future = future::select_all(vec![
Box::pin(timeout) as future::BoxFuture<'static, ()>,
Box::pin(reporter) as future::BoxFuture<'static, ()>,
Box::pin(success) as future::BoxFuture<'static, ()>,
]);
// Now kick off the shutdown process by shutting down the sources.
let source_shutdown_complete = self.shutdown_coordinator.shutdown_all(deadline);
futures::future::join(source_shutdown_complete, shutdown_complete_future).map(|_| ())
}
/// On Error, topology is in invalid state.
/// May change componenets even if reload fails.
pub async fn reload_config_and_respawn(&mut self, new_config: Config) -> Result<bool, ()> {
if self.config.global != new_config.global {
error!(
message =
"Global options can't be changed while reloading config file; reload aborted. Please restart vector to reload the configuration file."
);
return Ok(false);
}
let diff = ConfigDiff::new(&self.config, &new_config);
// Checks passed so let's shutdown the difference.
let buffers = self.shutdown_diff(&diff, &new_config).await;
// Gives windows some time to make available any port
// released by shutdown componenets.
// Issue: https://github.com/timberio/vector/issues/3035
if cfg!(windows) {
// This value is guess work.
tokio::time::sleep(Duration::from_millis(200)).await;
}
// Now let's actually build the new pieces.
if let Some(mut new_pieces) = build_or_log_errors(&new_config, &diff, buffers.clone()).await
{
if self
.run_healthchecks(&diff, &mut new_pieces, new_config.healthchecks)
.await
{
self.connect_diff(&diff, &mut new_pieces).await;
self.spawn_diff(&diff, new_pieces);
self.config = new_config;
// We have successfully changed to new config.
return Ok(true);
}
}
// We need to rebuild the removed.
info!("Rebuilding old configuration.");
let diff = diff.flip();
if let Some(mut new_pieces) = build_or_log_errors(&self.config, &diff, buffers).await {
if self
.run_healthchecks(&diff, &mut new_pieces, self.config.healthchecks)
.await
{
self.connect_diff(&diff, &mut new_pieces).await;
self.spawn_diff(&diff, new_pieces);
// We have successfully returned to old config.
return Ok(false);
}
}
// We failed in rebuilding the old state.
error!("Failed in rebuilding the old configuration.");
Err(())
}
async fn run_healthchecks(
&mut self,
diff: &ConfigDiff,
pieces: &mut Pieces,
options: HealthcheckOptions,
) -> bool {
if options.enabled {
let healthchecks = take_healthchecks(diff, pieces)
.into_iter()
.map(|(_, task)| task);
let healthchecks = future::try_join_all(healthchecks);
info!("Running healthchecks.");
if options.require_healthy {
let success = healthchecks.await;
if success.is_ok() {
info!("All healthchecks passed.");
true
} else {
error!("Sinks unhealthy.");
false
}
} else {
tokio::spawn(healthchecks);
true
}
} else {
true
}
}
/// Shutdowns removed and replaced pieces of topology.
/// Returns buffers to be reused.
async fn shutdown_diff(
&mut self,
diff: &ConfigDiff,
new_config: &Config,
) -> HashMap<String, BuiltBuffer> {
// Sources
let timeout = Duration::from_secs(30); //sec
// First pass to tell the sources to shut down.
let mut source_shutdown_complete_futures = Vec::new();
// Only log that we are waiting for shutdown if we are actually removing
// sources.
if !diff.sources.to_remove.is_empty() {
info!(
message = "Waiting for sources to finish shutting down.", timeout = ?timeout.as_secs()
);
}
let deadline = Instant::now() + timeout;
for name in &diff.sources.to_remove {
info!(message = "Removing source.", name = ?name);
let previous = self.tasks.remove(name).unwrap();
drop(previous); // detach and forget
self.remove_outputs(name);
source_shutdown_complete_futures
.push(self.shutdown_coordinator.shutdown_source(name, deadline));
}
for name in &diff.sources.to_change {
self.remove_outputs(name);
source_shutdown_complete_futures
.push(self.shutdown_coordinator.shutdown_source(name, deadline));
}
// Wait for the shutdowns to complete
// Only log message if there are actual futures to check.
if !source_shutdown_complete_futures.is_empty() {
info!(
"Waiting for up to {} seconds for sources to finish shutting down.",
timeout.as_secs()
);
}
futures::future::join_all(source_shutdown_complete_futures).await;
// Second pass now that all sources have shut down for final cleanup.
for name in diff.sources.removed_and_changed() {
if let Some(task) = self.source_tasks.remove(name) {
task.await.unwrap().unwrap();
}
}
// Transforms
for name in &diff.transforms.to_remove {
info!(message = "Removing transform.", name = ?name);
let previous = self.tasks.remove(name).unwrap();
drop(previous); // detach and forget
self.remove_inputs(name).await;
self.remove_outputs(name);
}
// Sinks
// Resource conflicts
// At this point both the old and the new config don't have
// conflicts in their resource usage. So if we combine their
// resources, all found conflicts are between
// to be removed and to be added components.
let remove_sink = diff
.sinks
.removed_and_changed()
.map(|name| (name, self.config.sinks[name].resources(name)));
let add_source = diff
.sources
.changed_and_added()
.map(|name| (name, new_config.sources[name].inner.resources()));
let add_sink = diff
.sinks
.changed_and_added()
.map(|name| (name, new_config.sinks[name].resources(name)));
let conflicts = Resource::conflicts(
remove_sink.map(|(key, value)| ((true, key), value)).chain(
add_sink
.chain(add_source)
.map(|(key, value)| ((false, key), value)),
),
)
.into_iter()
.flat_map(|(_, components)| components)
.collect::<HashSet<_>>();
// Existing conflicting sinks
let conflicting_sinks = conflicts
.into_iter()
.filter(|&(existing_sink, _)| existing_sink)
.map(|(_, name)| name.clone());
// Buffer reuse
// We can reuse buffers whose configuration wasn't changed.
let reuse_buffers = diff
.sinks
.to_change
.iter()
.filter(|&name| self.config.sinks[name].buffer == new_config.sinks[name].buffer)
.cloned()
.collect::<HashSet<_>>();
let wait_for_sinks = conflicting_sinks
.chain(reuse_buffers.iter().cloned())
.collect::<HashSet<_>>();
// First pass
// Detach removed sinks
for name in &diff.sinks.to_remove {
info!(message = "Removing sink.", name = ?name);
self.remove_inputs(name).await;
}
// Detach changed sinks
for name in &diff.sinks.to_change {
if reuse_buffers.contains(name) {
self.detach_triggers
.remove(name)
.unwrap()
.into_inner()
.cancel();
} else if wait_for_sinks.contains(name) {
self.detach_inputs(name).await;
}
}
// Second pass for final cleanup
// Cleanup removed
for name in &diff.sinks.to_remove {
let previous = self.tasks.remove(name).unwrap();
if wait_for_sinks.contains(name) {
debug!(message = "Waiting for sink to shutdown.", %name);
previous.await.unwrap().unwrap();
} else {
drop(previous); // detach and forget
}
}
// Cleanup changed and collect buffers to be reused
let mut buffers = HashMap::new();
for name in &diff.sinks.to_change {
if wait_for_sinks.contains(name) {
let previous = self.tasks.remove(name).unwrap();
debug!(message = "Waiting for sink to shutdown.", %name);
let buffer = previous.await.unwrap().unwrap();
if reuse_buffers.contains(name) {
let tx = self.inputs.remove(name).unwrap();
let (rx, acker) = match buffer {
TaskOutput::Sink(rx, acker) => (rx, acker),
_ => unreachable!(),
};
buffers.insert(name.clone(), (tx, Arc::new(Mutex::new(Some(rx))), acker));
}
}
}
buffers
}
/// Rewires topology
async fn connect_diff(&mut self, diff: &ConfigDiff, new_pieces: &mut Pieces) {
// Sources
for name in diff.sources.changed_and_added() {
self.setup_outputs(name, new_pieces).await;
}
// Transforms
// Make sure all transform outputs are set up before another transform might try use
// it as an input
for name in diff.transforms.changed_and_added() {
self.setup_outputs(name, new_pieces).await;
}
for name in &diff.transforms.to_change {
self.replace_inputs(name, new_pieces).await;
}
for name in &diff.transforms.to_add {
self.setup_inputs(name, new_pieces).await;
}
// Sinks
for name in &diff.sinks.to_change {
self.replace_inputs(name, new_pieces).await;
}
for name in &diff.sinks.to_add {
self.setup_inputs(name, new_pieces).await;
}
// Broadcast changes to subscribers.
if !self.watch.0.is_closed() {
self.watch
.0
.send(self.outputs.clone())
.expect("Couldn't broadcast config changes.");
}
}
/// Starts new and changed pieces of topology.
fn spawn_diff(&mut self, diff: &ConfigDiff, mut new_pieces: Pieces) {
// Sources
for name in &diff.sources.to_change {
info!(message = "Rebuilding source.", name = ?name);
self.spawn_source(name, &mut new_pieces);
}
for name in &diff.sources.to_add {
info!(message = "Starting source.", name = ?name);
self.spawn_source(name, &mut new_pieces);
}
// Transforms
for name in &diff.transforms.to_change {
info!(message = "Rebuilding transform.", name = ?name);
self.spawn_transform(name, &mut new_pieces);
}
for name in &diff.transforms.to_add {
info!(message = "Starting transform.", name = ?name);
self.spawn_transform(name, &mut new_pieces);
}
// Sinks
for name in &diff.sinks.to_change {
info!(message = "Rebuilding sink.", name = ?name);
self.spawn_sink(name, &mut new_pieces);
}
for name in &diff.sinks.to_add {
info!(message = "Starting sink.", name = ?name);
self.spawn_sink(name, &mut new_pieces);
}
}
fn spawn_sink(&mut self, name: &str, new_pieces: &mut builder::Pieces) {
let task = new_pieces.tasks.remove(name).unwrap();
let span = error_span!(
"sink",
component_kind = "sink",
component_name = %task.name(),
component_type = %task.typetag(),
);
let task = handle_errors(task, self.abort_tx.clone()).instrument(span);
let spawned = tokio::spawn(task);
if let Some(previous) = self.tasks.insert(name.to_string(), spawned) {
drop(previous); // detach and forget
}
}
fn spawn_transform(&mut self, name: &str, new_pieces: &mut builder::Pieces) {
let task = new_pieces.tasks.remove(name).unwrap();
let span = error_span!(
"transform",
component_kind = "transform",
component_name = %task.name(),
component_type = %task.typetag(),
);
let task = handle_errors(task, self.abort_tx.clone()).instrument(span);
let spawned = tokio::spawn(task);
if let Some(previous) = self.tasks.insert(name.to_string(), spawned) {
drop(previous); // detach and forget
}
}
fn spawn_source(&mut self, name: &str, new_pieces: &mut builder::Pieces) {
let task = new_pieces.tasks.remove(name).unwrap();
let span = error_span!(
"source",
component_kind = "source",
component_name = %task.name(),
component_type = %task.typetag(),
);
let task = handle_errors(task, self.abort_tx.clone()).instrument(span.clone());
let spawned = tokio::spawn(task);
if let Some(previous) = self.tasks.insert(name.to_string(), spawned) {
drop(previous); // detach and forget
}
self.shutdown_coordinator
.takeover_source(name, &mut new_pieces.shutdown_coordinator);
let source_task = new_pieces.source_tasks.remove(name).unwrap();
let source_task = handle_errors(source_task, self.abort_tx.clone()).instrument(span);
self.source_tasks
.insert(name.to_string(), tokio::spawn(source_task));
}
fn remove_outputs(&mut self, name: &str) {
self.outputs.remove(name);
}
async fn remove_inputs(&mut self, name: &str) {
self.inputs.remove(name);
self.detach_triggers.remove(name);
let sink_inputs = self.config.sinks.get(name).map(|s| &s.inputs);
let trans_inputs = self.config.transforms.get(name).map(|t| &t.inputs);
let inputs = sink_inputs.or(trans_inputs);
if let Some(inputs) = inputs {
for input in inputs {
if let Some(output) = self.outputs.get_mut(input) {
// This can only fail if we are disconnected, which is a valid situation.
let _ = output
.send(fanout::ControlMessage::Remove(name.to_string()))
.await;
}
}
}
}
async fn setup_outputs(&mut self, name: &str, new_pieces: &mut builder::Pieces) {
let mut output = new_pieces.outputs.remove(name).unwrap();
for (sink_name, sink) in &self.config.sinks {
if sink.inputs.iter().any(|i| i == name) {
// Sink may have been removed with the new config so it may not be present.
if let Some(input) = self.inputs.get(sink_name) {
let _ = output
.send(fanout::ControlMessage::Add(sink_name.clone(), input.get()))
.await;
}
}
}
for (transform_name, transform) in &self.config.transforms {
if transform.inputs.iter().any(|i| i == name) {
// Transform may have been removed with the new config so it may not be present.
if let Some(input) = self.inputs.get(transform_name) {
let _ = output
.send(fanout::ControlMessage::Add(
transform_name.clone(),
input.get(),
))
.await;
}
}
}
self.outputs.insert(name.to_string(), output);
}
async fn setup_inputs(&mut self, name: &str, new_pieces: &mut builder::Pieces) {
let (tx, inputs) = new_pieces.inputs.remove(name).unwrap();
for input in inputs {
// This can only fail if we are disconnected, which is a valid situation.
let _ = self
.outputs
.get_mut(&input)
.unwrap()
.send(fanout::ControlMessage::Add(name.to_string(), tx.get()))
.await;
}
self.inputs.insert(name.to_string(), tx);
new_pieces.detach_triggers.remove(name).map(|trigger| {
self.detach_triggers
.insert(name.to_string(), trigger.into())
});
}
async fn replace_inputs(&mut self, name: &str, new_pieces: &mut builder::Pieces) {
let (tx, inputs) = new_pieces.inputs.remove(name).unwrap();
let sink_inputs = self.config.sinks.get(name).map(|s| &s.inputs);
let trans_inputs = self.config.transforms.get(name).map(|t| &t.inputs);
let old_inputs = sink_inputs
.or(trans_inputs)
.unwrap()
.iter()
.collect::<HashSet<_>>();
let new_inputs = inputs.iter().collect::<HashSet<_>>();
let inputs_to_remove = &old_inputs - &new_inputs;
let inputs_to_add = &new_inputs - &old_inputs;
let inputs_to_replace = old_inputs.intersection(&new_inputs);
for input in inputs_to_remove {
if let Some(output) = self.outputs.get_mut(input) {
// This can only fail if we are disconnected, which is a valid situation.
let _ = output
.send(fanout::ControlMessage::Remove(name.to_string()))
.await;
}
}
for input in inputs_to_add {
// This can only fail if we are disconnected, which is a valid situation.
let _ = self
.outputs
.get_mut(input)
.unwrap()
.send(fanout::ControlMessage::Add(name.to_string(), tx.get()))
.await;
}
for &input in inputs_to_replace {
// This can only fail if we are disconnected, which is a valid situation.
let _ = self
.outputs
.get_mut(input)
.unwrap()
.send(fanout::ControlMessage::Replace(
name.to_string(),
Some(tx.get()),
))
.await;
}
self.inputs.insert(name.to_string(), tx);
new_pieces.detach_triggers.remove(name).map(|trigger| {
self.detach_triggers
.insert(name.to_string(), trigger.into())
});
}
async fn detach_inputs(&mut self, name: &str) {
self.inputs.remove(name);
self.detach_triggers.remove(name);
let sink_inputs = self.config.sinks.get(name).map(|s| &s.inputs);
let trans_inputs = self.config.transforms.get(name).map(|t| &t.inputs);
let old_inputs = sink_inputs.or(trans_inputs).unwrap();
for input in old_inputs {
// This can only fail if we are disconnected, which is a valid situation.
let _ = self
.outputs
.get_mut(input)
.unwrap()
.send(fanout::ControlMessage::Replace(name.to_string(), None))
.await;
}
}
/// Borrows the Config
pub fn config(&self) -> &Config {
&self.config
}
/// Subscribe to topology changes. This will receive an `Outputs` currently, but may be
/// expanded in the future to accommodate `Inputs`. This is used by the 'tap' API to observe
/// config changes, and re-wire tap sinks.
pub fn watch(&self) -> watch::Receiver<Outputs> {
self.watch.1.clone()
}
}
async fn handle_errors(
task: impl Future<Output = Result<TaskOutput, ()>>,
abort_tx: mpsc::UnboundedSender<()>,
) -> Result<TaskOutput, ()> {
AssertUnwindSafe(task)
.catch_unwind()
.await
.map_err(|_| ())
.and_then(|res| res)
.map_err(|_| {
error!("An error occurred that vector couldn't handle.");
let _ = abort_tx.send(());
})
}
/// If the closure returns false, then the element is removed
fn retain<T>(vec: &mut Vec<T>, mut retain_filter: impl FnMut(&mut T) -> bool) {
let mut i = 0;
while let Some(data) = vec.get_mut(i) {
if retain_filter(data) {
i += 1;
} else {
let _ = vec.remove(i);
}
}
}
#[cfg(all(test, feature = "sinks-console", feature = "sources-socket"))]
mod tests {
use crate::{
config::Config,
sinks::console::{ConsoleSinkConfig, Encoding, Target},
sources::socket::SocketConfig,
test_util::{next_addr, start_topology},
};
use std::path::Path;
#[tokio::test]
async fn topology_doesnt_reload_new_data_dir() {
let mut old_config = Config::builder();
old_config.add_source("in", SocketConfig::make_basic_tcp_config(next_addr()));
old_config.add_sink(
"out",
&["in"],
ConsoleSinkConfig {
target: Target::Stdout,
encoding: Encoding::Text.into(),
},
);
old_config.global.data_dir = Some(Path::new("/asdf").to_path_buf());
let mut new_config = old_config.clone();
let (mut topology, _crash) = start_topology(old_config.build().unwrap(), false).await;
new_config.global.data_dir = Some(Path::new("/qwerty").to_path_buf());
topology
.reload_config_and_respawn(new_config.build().unwrap())
.await
.unwrap();
assert_eq!(
topology.config.global.data_dir,
Some(Path::new("/asdf").to_path_buf())
);
}
}
#[cfg(all(
test,
feature = "sinks-console",
feature = "sources-splunk_hec",
feature = "sources-generator",
feature = "sinks-prometheus",
feature = "transforms-log_to_metric",
feature = "sinks-socket",
feature = "leveldb"
))]
mod reload_tests {
use crate::buffers::{BufferConfig, WhenFull};
use crate::config::Config;
use crate::sinks::console::{ConsoleSinkConfig, Encoding, Target};
use crate::sinks::prometheus::exporter::PrometheusExporterConfig;
use crate::sources::generator::GeneratorConfig;
use crate::sources::splunk_hec::SplunkConfig;
use crate::test_util::{next_addr, start_topology, temp_dir, wait_for_tcp};
use crate::transforms::log_to_metric::{GaugeConfig, LogToMetricConfig, MetricConfig};
use futures::StreamExt;
use std::net::{SocketAddr, TcpListener};
use std::time::Duration;
use tokio::time::sleep;
use tokio_stream::wrappers::UnboundedReceiverStream;
#[tokio::test]
async fn topology_reuse_old_port() {
let address = next_addr();
let mut old_config = Config::builder();
old_config.add_source("in1", SplunkConfig::on(address));
old_config.add_sink(
"out",
&[&"in1"],
ConsoleSinkConfig {
target: Target::Stdout,
encoding: Encoding::Text.into(),
},
);
let mut new_config = Config::builder();
new_config.add_source("in2", SplunkConfig::on(address));
new_config.add_sink(
"out",
&[&"in2"],
ConsoleSinkConfig {
target: Target::Stdout,
encoding: Encoding::Text.into(),
},
);
let (mut topology, _crash) = start_topology(old_config.build().unwrap(), false).await;
assert!(topology
.reload_config_and_respawn(new_config.build().unwrap())
.await
.unwrap());
}
#[tokio::test]
async fn topology_rebuild_old() {
let address_0 = next_addr();
let address_1 = next_addr();
let mut old_config = Config::builder();
old_config.add_source("in1", SplunkConfig::on(address_0));
old_config.add_sink(
"out",
&[&"in1"],
ConsoleSinkConfig {
target: Target::Stdout,
encoding: Encoding::Text.into(),
},
);
let mut new_config = Config::builder();
new_config.add_source("in1", SplunkConfig::on(address_1));
new_config.add_sink(
"out",
&[&"in1"],
ConsoleSinkConfig {
target: Target::Stdout,
encoding: Encoding::Text.into(),
},
);
// Will cause the new_config to fail on build
let _bind = TcpListener::bind(address_1).unwrap();
let (mut topology, _crash) = start_topology(old_config.build().unwrap(), false).await;
assert!(!topology
.reload_config_and_respawn(new_config.build().unwrap())
.await
.unwrap());
}
#[tokio::test]
async fn topology_old() {
let address = next_addr();
let mut old_config = Config::builder();
old_config.add_source("in1", SplunkConfig::on(address));
old_config.add_sink(
"out",
&[&"in1"],
ConsoleSinkConfig {
target: Target::Stdout,
encoding: Encoding::Text.into(),
},
);
let (mut topology, _crash) =
start_topology(old_config.clone().build().unwrap(), false).await;
assert!(topology
.reload_config_and_respawn(old_config.build().unwrap())
.await
.unwrap());
}
#[tokio::test]
async fn topology_reuse_old_port_sink() {
let address = next_addr();
let source = GeneratorConfig::repeat(vec!["msg".to_string()], usize::MAX, Some(0.001));
let transform = LogToMetricConfig {
metrics: vec![MetricConfig::Gauge(GaugeConfig {
field: "message".to_string(),
name: None,
namespace: None,
tags: None,
})],
};
let mut old_config = Config::builder();
old_config.add_source("in", source.clone());
old_config.add_transform("trans", &[&"in"], transform.clone());
old_config.add_sink(
"out1",
&[&"trans"],
PrometheusExporterConfig {
address,
flush_period_secs: 1,
..PrometheusExporterConfig::default()
},
);
let mut new_config = Config::builder();
new_config.add_source("in", source.clone());
new_config.add_transform("trans", &[&"in"], transform.clone());
new_config.add_sink(
"out1",
&[&"trans"],
PrometheusExporterConfig {
address,
flush_period_secs: 2,
..PrometheusExporterConfig::default()
},
);