espipe 0.3.0

A minimalist command-line utility to pipe documents from a file or I/O stream into an Elasticsearch cluster.
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
mod bulk_response;

use super::{BulkAction, Sender};
use crate::output::OutputPreflightConfig;
use bulk_response::BulkResponse;
use elasticsearch::{
    Elasticsearch,
    http::{Method, StatusCode, headers::HeaderMap, headers::HeaderValue},
};
use eyre::{OptionExt, Result, eyre};
use futures::{StreamExt, stream::FuturesUnordered};
use serde_json::{Value, json, value::RawValue};
use std::{
    fs,
    path::{Path, PathBuf},
    sync::Arc,
    time::Duration,
};
use tokio::{sync::mpsc, task::JoinHandle, time::sleep};
use url::Url;

const DEFAULT_BATCH_SIZE: usize = 5_000;
const DEFAULT_MAX_INFLIGHT_REQUESTS: usize = 16;

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct ElasticsearchOutputConfig {
    batch_size: usize,
    max_inflight_requests: usize,
}

#[derive(Clone, Debug)]
pub struct TemplateConfig {
    path: PathBuf,
    name: Option<String>,
    overwrite: bool,
}

impl TemplateConfig {
    pub fn try_new(
        path: Option<PathBuf>,
        name: Option<String>,
        overwrite: Option<bool>,
    ) -> Result<Option<Self>> {
        if path.is_none() {
            if name.is_some() {
                return Err(eyre!("--template-name requires --template"));
            }
            if overwrite.is_some() {
                return Err(eyre!("--template-overwrite requires --template"));
            }
            return Ok(None);
        }

        Ok(Some(Self {
            path: path.expect("checked above"),
            name,
            overwrite: overwrite.unwrap_or(true),
        }))
    }
}

impl ElasticsearchOutputConfig {
    pub const DEFAULT_BATCH_SIZE: usize = DEFAULT_BATCH_SIZE;
    pub const DEFAULT_MAX_INFLIGHT_REQUESTS: usize = DEFAULT_MAX_INFLIGHT_REQUESTS;

    pub fn try_new(batch_size: usize, max_inflight_requests: usize) -> Result<Self> {
        if batch_size == 0 {
            return Err(eyre!("batch size must be greater than zero"));
        }
        if max_inflight_requests == 0 {
            return Err(eyre!("max requests must be greater than zero"));
        }

        Ok(Self {
            batch_size,
            max_inflight_requests,
        })
    }

    fn channel_capacity(self) -> usize {
        self.batch_size
    }
}

impl Default for ElasticsearchOutputConfig {
    fn default() -> Self {
        Self {
            batch_size: DEFAULT_BATCH_SIZE,
            max_inflight_requests: DEFAULT_MAX_INFLIGHT_REQUESTS,
        }
    }
}

#[derive(Debug)]
pub struct ElasticsearchOutput {
    hostname: String,
    index: String,
    sender: Option<mpsc::Sender<Box<RawValue>>>,
    worker: JoinHandle<Result<usize>>,
}

impl ElasticsearchOutput {
    pub async fn try_new(
        client: Elasticsearch,
        url: Url,
        action: BulkAction,
        config: ElasticsearchOutputConfig,
        preflight: OutputPreflightConfig,
    ) -> Result<Self> {
        let hostname = url
            .host_str()
            .ok_or_eyre("Url missing host_str")?
            .to_string();
        let index = url.path().trim_start_matches('/').to_string();
        log::debug!("Elasticsearch output to {hostname}/{index}");

        let preflight = PreparedPreflight::try_from(preflight)?;
        preflight.run(&client, &index).await?;

        let client = Arc::new(client);
        let (sender, receiver) = mpsc::channel(config.channel_capacity());
        let worker = tokio::spawn(run_bulk_worker(
            Arc::clone(&client),
            hostname.clone(),
            index.clone(),
            action,
            config,
            preflight.bulk_pipeline,
            receiver,
        ));

        Ok(Self {
            hostname,
            index,
            sender: Some(sender),
            worker,
        })
    }
}

#[derive(Debug)]
struct ParsedTemplate {
    name: String,
    overwrite: bool,
    body: Value,
}

async fn install_template(
    client: &Elasticsearch,
    target_index: &str,
    parsed: &ParsedTemplate,
) -> Result<()> {
    warn_for_index_patterns(&parsed.body, target_index);

    let mut headers = HeaderMap::new();
    headers.insert("content-type", HeaderValue::from_static("application/json"));
    let path = format!("/_index_template/{}", parsed.name);
    let method = if parsed.overwrite {
        Method::Put
    } else {
        Method::Post
    };
    let params = if parsed.overwrite {
        None
    } else {
        Some(&[("create", "true")][..])
    };
    let body = serde_json::to_vec(&parsed.body)?;
    let response = client
        .send(method, &path, headers, params, Some(body), None)
        .await
        .map_err(|err| eyre!("failed to install index template '{}': {err}", parsed.name))?;
    let status = response.status_code();
    if !status.is_success() {
        let details = response
            .text()
            .await
            .unwrap_or_else(|err| format!("failed to read error body: {err}"));
        return Err(eyre!(
            "failed to install index template '{}': status {status}: {details}",
            parsed.name
        ));
    }

    Ok(())
}

fn parse_template(config: TemplateConfig) -> Result<ParsedTemplate> {
    let body = std::fs::read_to_string(&config.path)
        .map_err(|err| eyre!("failed to read template '{}': {err}", config.path.display()))?;
    let value = match config.path.extension().and_then(|ext| ext.to_str()) {
        Some("jsonc" | "json5") => serde_json5::from_str::<Value>(&body).map_err(|err| {
            eyre!(
                "failed to parse template '{}': {err}",
                config.path.display()
            )
        })?,
        _ => serde_json::from_str::<Value>(&body).map_err(|err| {
            eyre!(
                "failed to parse template '{}': {err}",
                config.path.display()
            )
        })?,
    };
    let name = match config.name {
        Some(name) => name,
        None => derive_template_name(&config.path)?,
    };
    if name.is_empty() {
        return Err(eyre!("template name must be non-empty"));
    }

    Ok(ParsedTemplate {
        name,
        overwrite: config.overwrite,
        body: value,
    })
}

fn derive_template_name(path: &Path) -> Result<String> {
    let name = path
        .file_stem()
        .and_then(|name| name.to_str())
        .ok_or_else(|| eyre!("template name must be non-empty"))?;
    if name.is_empty() {
        return Err(eyre!("template name must be non-empty"));
    }
    Ok(name.to_string())
}

fn warn_for_index_patterns(template: &Value, target_index: &str) {
    match index_patterns_match(template, target_index) {
        Ok(true) => {}
        Ok(false) => {
            eprintln!("warning: template index_patterns do not match target index '{target_index}'")
        }
        Err(reason) => eprintln!(
            "warning: could not verify template index_patterns for target index '{target_index}': {reason}"
        ),
    }
}

fn index_patterns_match(template: &Value, target_index: &str) -> Result<bool> {
    let patterns = template
        .get("index_patterns")
        .ok_or_else(|| eyre!("index_patterns is missing"))?;
    let expressions = match patterns {
        Value::String(pattern) => vec![pattern.as_str()],
        Value::Array(patterns) => {
            let mut values = Vec::with_capacity(patterns.len());
            for pattern in patterns {
                values.push(
                    pattern
                        .as_str()
                        .ok_or_else(|| eyre!("index_patterns must contain only strings"))?,
                );
            }
            values
        }
        _ => return Err(eyre!("index_patterns must be a string or string array")),
    };

    let mut matched = false;
    for expression in expressions {
        for part in expression.split(',') {
            let part = part.trim();
            if part.is_empty() {
                continue;
            }
            let (exclude, pattern) = match part.strip_prefix('-') {
                Some("") => return Err(eyre!("invalid lone '-' index pattern")),
                Some(pattern) => (true, pattern),
                None => (false, part),
            };
            if wildcard_match(pattern, target_index) {
                matched = !exclude;
            }
        }
    }
    Ok(matched)
}

fn wildcard_match(pattern: &str, value: &str) -> bool {
    let pattern = pattern.as_bytes();
    let value = value.as_bytes();
    let (mut pattern_index, mut value_index) = (0usize, 0usize);
    let mut star_index = None;
    let mut star_value_index = 0usize;

    while value_index < value.len() {
        if pattern_index < pattern.len() && pattern[pattern_index] == value[value_index] {
            pattern_index += 1;
            value_index += 1;
        } else if pattern_index < pattern.len() && pattern[pattern_index] == b'*' {
            star_index = Some(pattern_index);
            star_value_index = value_index;
            pattern_index += 1;
        } else if let Some(star) = star_index {
            pattern_index = star + 1;
            star_value_index += 1;
            value_index = star_value_index;
        } else {
            return false;
        }
    }

    while pattern_index < pattern.len() && pattern[pattern_index] == b'*' {
        pattern_index += 1;
    }

    pattern_index == pattern.len()
}

impl Sender for ElasticsearchOutput {
    async fn send(&mut self, value: Box<RawValue>) -> Result<usize> {
        let sender = self
            .sender
            .as_ref()
            .ok_or_eyre("Elasticsearch output already closed")?;
        sender
            .send(value)
            .await
            .map_err(|_| eyre!("Elasticsearch output worker closed unexpectedly"))?;
        Ok(0)
    }

    async fn close(mut self) -> Result<usize> {
        self.sender.take();
        self.worker.await.map_err(eyre::Report::new)?
    }
}

impl std::fmt::Display for ElasticsearchOutput {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        write!(f, "{}:{}", self.hostname, self.index)
    }
}

async fn run_bulk_worker(
    client: Arc<Elasticsearch>,
    hostname: String,
    index: String,
    action: BulkAction,
    config: ElasticsearchOutputConfig,
    bulk_pipeline: Option<String>,
    mut receiver: mpsc::Receiver<Box<RawValue>>,
) -> Result<usize> {
    let mut batch = Vec::with_capacity(config.batch_size);
    let mut docs_sent = 0usize;
    let mut inflight = FuturesUnordered::<JoinHandle<Result<usize>>>::new();

    while let Some(doc) = receiver.recv().await {
        batch.push(doc);
        if batch.len() >= config.batch_size {
            spawn_flush(
                &mut inflight,
                &client,
                &hostname,
                &index,
                action,
                config,
                bulk_pipeline.as_deref(),
                &mut batch,
            )?;
            docs_sent +=
                reap_inflight_if_needed(&mut inflight, config.max_inflight_requests).await?;
        }
    }

    if !batch.is_empty() {
        spawn_flush(
            &mut inflight,
            &client,
            &hostname,
            &index,
            action,
            config,
            bulk_pipeline.as_deref(),
            &mut batch,
        )?;
    }

    while let Some(result) = inflight.next().await {
        docs_sent += result.map_err(eyre::Report::new)??;
    }

    Ok(docs_sent)
}

fn spawn_flush(
    inflight: &mut FuturesUnordered<JoinHandle<Result<usize>>>,
    client: &Arc<Elasticsearch>,
    hostname: &str,
    index: &str,
    action: BulkAction,
    config: ElasticsearchOutputConfig,
    bulk_pipeline: Option<&str>,
    batch: &mut Vec<Box<RawValue>>,
) -> Result<()> {
    let docs = std::mem::replace(batch, Vec::with_capacity(config.batch_size));
    let body = build_bulk_body(action, &docs)?;
    log::debug!("Bulk sending {} docs to {hostname}/{index}", docs.len());
    let client = Arc::clone(client);
    let index = index.to_string();
    let bulk_pipeline = bulk_pipeline.map(str::to_string);

    inflight.push(tokio::spawn(async move {
        let mut headers = HeaderMap::new();
        headers.insert("content-type", HeaderValue::from_static("application/x-ndjson"));
        let query = bulk_pipeline.as_ref().map(|pipeline| [("pipeline", pipeline.as_str())]);

        let mut attempt = 0u64;
        let mut backoff = Duration::from_secs(1);
        let max_backoff = Duration::from_secs(30);

        loop {
            attempt += 1;
            let response = client
                .send(
                    Method::Post,
                    &format!("/{index}/_bulk"),
                    headers.clone(),
                    query.as_ref(),
                    Some(body.clone()),
                    None,
                )
                .await?;

            let status_code = response.status_code();
            let bulk_response = response.json::<BulkResponse>().await?;
            match status_code {
                StatusCode::BAD_REQUEST => {
                    log::error!(
                        "Bulk response: 400 - Bad request ({})",
                        bulk_response.error_cause()
                    );
                    return Ok(0);
                }
                StatusCode::TOO_MANY_REQUESTS => {
                    log::warn!(
                        "Bulk response: 429 - Too many requests (attempt {attempt}, backoff {:?}): {}",
                        backoff,
                        bulk_response.error_cause()
                    );
                    sleep(backoff).await;
                    if backoff < max_backoff {
                        backoff = std::cmp::min(backoff * 2, max_backoff);
                    }
                }
                _ => {
                    log::debug!("Bulk response status: {status_code}");
                    if bulk_response.has_errors() {
                        log::warn!(
                            "Bulk response contained errors: {}",
                            bulk_response.error_counts()
                        );
                    }
                    return Ok(bulk_response.success_count());
                }
            }
        }
    }));

    Ok(())
}

#[derive(Debug)]
struct PreparedPreflight {
    pipeline: Option<NamedJson>,
    template: Option<ParsedTemplate>,
    bulk_pipeline: Option<String>,
    template_pipeline: Option<String>,
}

#[derive(Debug)]
struct NamedJson {
    name: String,
    body: Value,
}

impl PreparedPreflight {
    fn try_from(config: OutputPreflightConfig) -> Result<Self> {
        let pipeline = match config.pipeline {
            Some(path) => Some(load_pipeline_json(
                "pipeline",
                &path,
                config.pipeline_name.as_deref(),
            )?),
            None => {
                if let Some(name) = config.pipeline_name.as_deref() {
                    if name == "_none" {
                        None
                    } else {
                        return Err(eyre!(
                            "--pipeline-name requires --pipeline unless the name is _none"
                        ));
                    }
                } else {
                    None
                }
            }
        };

        if pipeline
            .as_ref()
            .is_some_and(|pipeline| pipeline.name == "_none")
        {
            return Err(eyre!(
                "_none is reserved for the bulk pipeline target and cannot be installed as an ingest pipeline"
            ));
        }

        let template_config = TemplateConfig::try_new(
            config.template,
            config.template_name,
            config.template_overwrite,
        )?;
        let template = template_config.map(parse_template).transpose()?;

        let template_pipeline = template
            .as_ref()
            .and_then(|template| extract_default_pipeline(&template.body).map(str::to_string));

        if let (Some(template), Some(pipeline)) = (&template, &pipeline) {
            match template_pipeline.as_deref() {
                Some(name) if name == pipeline.name => {}
                Some(name) => {
                    return Err(eyre!(
                        "template references ingest pipeline '{name}', but --pipeline selects '{}'",
                        pipeline.name
                    ));
                }
                None => {
                    return Err(eyre!(
                        "template '{}' does not reference the provided pipeline '{}'",
                        template.name,
                        pipeline.name
                    ));
                }
            }
        }

        let bulk_pipeline = if template.is_none() {
            match (&pipeline, config.pipeline_name.as_deref()) {
                (Some(pipeline), _) => Some(pipeline.name.clone()),
                (None, Some("_none")) => Some("_none".to_string()),
                _ => None,
            }
        } else {
            None
        };

        Ok(Self {
            pipeline,
            template,
            bulk_pipeline,
            template_pipeline,
        })
    }

    async fn run(&self, client: &Elasticsearch, target_index: &str) -> Result<()> {
        if let Some(pipeline) = &self.pipeline {
            put_json(
                client,
                &format!("/_ingest/pipeline/{}", pipeline.name),
                &pipeline.body,
            )
            .await?;
        }

        if let (None, Some(pipeline_name)) = (&self.pipeline, &self.template_pipeline) {
            ensure_pipeline_exists(client, pipeline_name).await?;
        }

        if let Some(template) = &self.template {
            install_template(client, target_index, template).await?;
        }

        Ok(())
    }
}

fn load_pipeline_json(kind: &str, path: &Path, name_override: Option<&str>) -> Result<NamedJson> {
    if path.extension().and_then(|extension| extension.to_str()) != Some("json") {
        return Err(eyre!(
            "{kind} file {} must use the .json extension",
            path.display()
        ));
    }
    let contents = fs::read_to_string(path)
        .map_err(|err| eyre!("failed to read {kind} file {}: {err}", path.display()))?;
    let body: Value = serde_json::from_str(&contents).map_err(|err| {
        eyre!(
            "failed to parse {kind} file {} as JSON: {err}",
            path.display()
        )
    })?;
    let name = match name_override {
        Some(name) => name.to_string(),
        None => path
            .file_stem()
            .and_then(|stem| stem.to_str())
            .unwrap_or_default()
            .to_string(),
    };
    if name.is_empty() {
        return Err(eyre!("{kind} name must be non-empty"));
    }
    Ok(NamedJson { name, body })
}

async fn put_json(client: &Elasticsearch, path: &str, body: &Value) -> Result<()> {
    let mut headers = HeaderMap::new();
    headers.insert("content-type", HeaderValue::from_static("application/json"));
    let body = serde_json::to_vec(body)?;
    let response = client
        .send(
            Method::Put,
            path,
            headers,
            Option::<&()>::None,
            Some(body),
            None,
        )
        .await?;
    ensure_success(response.status_code(), response.text().await?, path)
}

async fn ensure_pipeline_exists(client: &Elasticsearch, name: &str) -> Result<()> {
    let response = client
        .send(
            Method::Get,
            &format!("/_ingest/pipeline/{name}"),
            HeaderMap::new(),
            Option::<&()>::None,
            Option::<Vec<u8>>::None,
            None,
        )
        .await?;
    ensure_success(
        response.status_code(),
        response.text().await?,
        &format!("/_ingest/pipeline/{name}"),
    )
    .map_err(|err| {
        eyre!("template references missing or unavailable ingest pipeline '{name}': {err}")
    })
}

fn ensure_success(status: StatusCode, body: String, path: &str) -> Result<()> {
    if status.is_success() {
        Ok(())
    } else {
        Err(eyre!(
            "Elasticsearch request to {path} failed with status {status}: {body}"
        ))
    }
}

fn extract_default_pipeline(template: &Value) -> Option<&str> {
    let settings = template.get("template")?.get("settings")?;
    settings
        .get("index.default_pipeline")
        .and_then(Value::as_str)
        .or_else(|| {
            settings
                .get("index")
                .and_then(|index| index.get("default_pipeline"))
                .and_then(Value::as_str)
        })
}

async fn reap_inflight_if_needed(
    inflight: &mut FuturesUnordered<JoinHandle<Result<usize>>>,
    max_inflight_requests: usize,
) -> Result<usize> {
    let mut docs_sent = 0usize;
    while inflight.len() >= max_inflight_requests {
        if let Some(result) = inflight.next().await {
            docs_sent += result.map_err(eyre::Report::new)??;
        }
    }
    Ok(docs_sent)
}

fn build_bulk_body(action: BulkAction, batch: &[Box<RawValue>]) -> Result<Vec<u8>> {
    let mut body = Vec::with_capacity(batch.len() * 64);
    for doc in batch {
        match action {
            BulkAction::Create => {
                body.extend_from_slice(b"{\"create\":{}}\n");
                body.extend_from_slice(doc.get().as_bytes());
                body.push(b'\n');
            }
            BulkAction::Index => {
                body.extend_from_slice(b"{\"index\":{}}\n");
                body.extend_from_slice(doc.get().as_bytes());
                body.push(b'\n');
            }
            BulkAction::Update => append_update_operation(&mut body, doc)?,
        }
    }
    Ok(body)
}

fn append_update_operation(body: &mut Vec<u8>, doc: &RawValue) -> Result<()> {
    let (id, doc) = extract_update_id(doc)?;
    body.extend_from_slice(b"{\"update\":{\"_id\":");
    serde_json::to_writer(&mut *body, &id)?;
    body.extend_from_slice(b"}}\n");
    serde_json::to_writer(&mut *body, &json!({ "doc": doc }))?;
    body.push(b'\n');
    Ok(())
}

fn extract_update_id(doc: &RawValue) -> Result<(String, Value)> {
    match serde_json::from_str::<Value>(doc.get())? {
        Value::Object(mut map) => {
            let id_value = map
                .remove("_id")
                .ok_or_eyre("Update action requires an _id field on each document")?;
            let id = id_value
                .as_str()
                .ok_or_eyre("Update action requires _id to be a string")?
                .to_string();
            Ok((id, Value::Object(map)))
        }
        _ => Err(eyre!(
            "Update action requires each document to be a JSON object"
        )),
    }
}

#[cfg(test)]
mod tests {
    use super::{
        DEFAULT_BATCH_SIZE, DEFAULT_MAX_INFLIGHT_REQUESTS, ElasticsearchOutputConfig,
        OutputPreflightConfig, PreparedPreflight, TemplateConfig, build_bulk_body,
        extract_default_pipeline, extract_update_id, index_patterns_match, parse_template,
        wildcard_match,
    };
    use crate::output::BulkAction;
    use serde_json::{Value, json, value::RawValue};
    use std::{fs, path::PathBuf};

    fn temp_json_path(name: &str) -> PathBuf {
        let dir = std::env::temp_dir().join(format!(
            "espipe-pipeline-test-{}-{}-{}",
            std::process::id(),
            std::time::SystemTime::now()
                .duration_since(std::time::UNIX_EPOCH)
                .unwrap()
                .as_nanos(),
            name
        ));
        fs::create_dir_all(&dir).unwrap();
        let path = dir.join(format!("{name}.json"));
        let _ = fs::remove_file(&path);
        path
    }

    #[test]
    fn build_bulk_body_uses_create_ndjson() {
        let docs = vec![
            RawValue::from_string("{\"a\":1}".to_string()).unwrap(),
            RawValue::from_string("{\"b\":2}".to_string()).unwrap(),
        ];

        let body = build_bulk_body(BulkAction::Create, &docs).unwrap();
        assert_eq!(
            String::from_utf8(body).unwrap(),
            "{\"create\":{}}\n{\"a\":1}\n{\"create\":{}}\n{\"b\":2}\n"
        );
    }

    #[test]
    fn build_bulk_body_uses_index_ndjson() {
        let docs = vec![RawValue::from_string("{\"a\":1}".to_string()).unwrap()];
        let body = build_bulk_body(BulkAction::Index, &docs).unwrap();
        assert_eq!(
            String::from_utf8(body).unwrap(),
            "{\"index\":{}}\n{\"a\":1}\n"
        );
    }

    #[test]
    fn build_bulk_body_wraps_update_docs() {
        let docs = vec![RawValue::from_string("{\"_id\":\"1\",\"a\":1}".to_string()).unwrap()];
        let body = build_bulk_body(BulkAction::Update, &docs).unwrap();
        let lines: Vec<Value> = String::from_utf8(body)
            .unwrap()
            .lines()
            .map(|line| serde_json::from_str(line).unwrap())
            .collect();
        assert_eq!(lines[0]["update"]["_id"], "1");
        assert_eq!(lines[1], json!({ "doc": { "a": 1 } }));
    }

    #[test]
    fn extract_update_id_requires_id() {
        let doc = RawValue::from_string("{\"message\":\"hello\"}".to_string()).unwrap();
        let err = extract_update_id(&doc).err().expect("expected error");
        assert!(err.to_string().contains("_id"));
    }

    #[test]
    fn default_worker_limits_are_bounded() {
        let config = ElasticsearchOutputConfig::default();
        assert_eq!(config.batch_size, DEFAULT_BATCH_SIZE);
        assert_eq!(config.channel_capacity(), DEFAULT_BATCH_SIZE);
        assert_eq!(config.max_inflight_requests, DEFAULT_MAX_INFLIGHT_REQUESTS);
    }

    #[test]
    fn config_rejects_zero_limits() {
        let batch_err = ElasticsearchOutputConfig::try_new(0, 1).unwrap_err();
        assert!(batch_err.to_string().contains("batch size"));

        let requests_err = ElasticsearchOutputConfig::try_new(1, 0).unwrap_err();
        assert!(requests_err.to_string().contains("max requests"));
    }

    #[test]
    fn template_name_defaults_to_file_stem() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("logs-docs.json");
        std::fs::write(&path, r#"{"index_patterns":["logs-*"]}"#).unwrap();

        let parsed = parse_template(TemplateConfig {
            path,
            name: None,
            overwrite: true,
        })
        .unwrap();

        assert_eq!(parsed.name, "logs-docs");
        assert!(parsed.overwrite);
    }

    #[test]
    fn template_name_override_is_used() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("logs-docs.json");
        std::fs::write(&path, r#"{"index_patterns":["logs-*"]}"#).unwrap();

        let parsed = parse_template(TemplateConfig {
            path,
            name: Some("custom-template".to_string()),
            overwrite: false,
        })
        .unwrap();

        assert_eq!(parsed.name, "custom-template");
        assert!(!parsed.overwrite);
    }

    #[test]
    fn template_name_rejects_empty_override() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("logs-docs.json");
        std::fs::write(&path, r#"{"index_patterns":["logs-*"]}"#).unwrap();

        let err = parse_template(TemplateConfig {
            path,
            name: Some(String::new()),
            overwrite: true,
        })
        .unwrap_err();

        assert!(err.to_string().contains("template name must be non-empty"));
    }

    #[test]
    fn strict_json_template_rejects_comments() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("template.json");
        std::fs::write(&path, r#"{"index_patterns":["logs-*"] /* no */}"#).unwrap();

        let err = parse_template(TemplateConfig {
            path: path.clone(),
            name: None,
            overwrite: true,
        })
        .unwrap_err();

        assert!(err.to_string().contains(&path.display().to_string()));
    }

    #[test]
    fn jsonc_and_json5_templates_are_normalized() {
        let dir = tempfile::tempdir().unwrap();
        let jsonc_path = dir.path().join("template.jsonc");
        std::fs::write(
            &jsonc_path,
            r#"{"index_patterns":["logs-*"], /* comment */ "priority": 1}"#,
        )
        .unwrap();
        let json5_path = dir.path().join("template.json5");
        std::fs::write(
            &json5_path,
            r#"{index_patterns:["logs-*"], template: { settings: { number_of_shards: 1 } }}"#,
        )
        .unwrap();

        let jsonc = parse_template(TemplateConfig {
            path: jsonc_path,
            name: None,
            overwrite: true,
        })
        .unwrap();
        let json5 = parse_template(TemplateConfig {
            path: json5_path,
            name: None,
            overwrite: true,
        })
        .unwrap();

        assert_eq!(jsonc.body["priority"], 1);
        assert_eq!(json5.body["template"]["settings"]["number_of_shards"], 1);
    }

    #[test]
    fn index_patterns_follow_multi_target_ordering() {
        assert!(index_patterns_match(&json!({"index_patterns":"test*"}), "test3").unwrap());
        assert!(!index_patterns_match(&json!({"index_patterns":"test*,-test3"}), "test3").unwrap());
        assert!(
            index_patterns_match(&json!({"index_patterns":"test3*,-test3,test*"}), "test3")
                .unwrap()
        );
        assert!(index_patterns_match(&json!({"index_patterns":["logs-*"]}), "logs-docs").unwrap());
        assert!(
            !index_patterns_match(&json!({"index_patterns":["metrics-*"]}), "logs-docs").unwrap()
        );
        assert!(index_patterns_match(&json!({"index_patterns":"*"}), "logs-docs").unwrap());
    }

    #[test]
    fn index_patterns_report_unverifiable_shapes() {
        assert!(index_patterns_match(&json!({}), "logs-docs").is_err());
        assert!(index_patterns_match(&json!({"index_patterns": 1}), "logs-docs").is_err());
        assert!(index_patterns_match(&json!({"index_patterns": "-"}), "logs-docs").is_err());
    }

    #[test]
    fn wildcard_matching_supports_zero_or_more_chars() {
        assert!(wildcard_match("logs-*", "logs-docs"));
        assert!(wildcard_match("logs*", "logs"));
        assert!(wildcard_match("*docs", "logs-docs"));
        assert!(!wildcard_match("metrics-*", "logs-docs"));
    }

    #[test]
    fn prepared_preflight_derives_pipeline_name_and_bulk_target() {
        let path = temp_json_path("geoip");
        fs::write(&path, r#"{"processors":[]}"#).unwrap();

        let preflight = PreparedPreflight::try_from(OutputPreflightConfig {
            pipeline: Some(path.clone()),
            ..OutputPreflightConfig::default()
        })
        .unwrap();

        assert_eq!(preflight.pipeline.as_ref().unwrap().name, "geoip");
        assert_eq!(preflight.bulk_pipeline.as_deref(), Some("geoip"));

        let _ = fs::remove_file(path);
    }

    #[test]
    fn prepared_preflight_applies_pipeline_name_override() {
        let path = temp_json_path("derived");
        fs::write(&path, r#"{"processors":[]}"#).unwrap();

        let preflight = PreparedPreflight::try_from(OutputPreflightConfig {
            pipeline: Some(path.clone()),
            pipeline_name: Some("normalized".to_string()),
            ..OutputPreflightConfig::default()
        })
        .unwrap();

        assert_eq!(preflight.pipeline.as_ref().unwrap().name, "normalized");
        assert_eq!(preflight.bulk_pipeline.as_deref(), Some("normalized"));

        let _ = fs::remove_file(path);
    }

    #[test]
    fn prepared_preflight_allows_none_without_pipeline_file() {
        let preflight = PreparedPreflight::try_from(OutputPreflightConfig {
            pipeline_name: Some("_none".to_string()),
            ..OutputPreflightConfig::default()
        })
        .unwrap();

        assert!(preflight.pipeline.is_none());
        assert_eq!(preflight.bulk_pipeline.as_deref(), Some("_none"));
    }

    #[test]
    fn prepared_preflight_rejects_pipeline_name_without_pipeline_file() {
        let err = PreparedPreflight::try_from(OutputPreflightConfig {
            pipeline_name: Some("geoip".to_string()),
            ..OutputPreflightConfig::default()
        })
        .unwrap_err();

        assert!(
            err.to_string()
                .contains("--pipeline-name requires --pipeline")
        );
    }

    #[test]
    fn prepared_preflight_rejects_invalid_pipeline_json() {
        let path = temp_json_path("invalid");
        fs::write(&path, "{").unwrap();

        let err = PreparedPreflight::try_from(OutputPreflightConfig {
            pipeline: Some(path.clone()),
            ..OutputPreflightConfig::default()
        })
        .unwrap_err();

        assert!(err.to_string().contains("failed to parse pipeline file"));

        let _ = fs::remove_file(path);
    }

    #[test]
    fn prepared_preflight_rejects_non_json_pipeline_extension() {
        let path = std::env::temp_dir().join(format!(
            "espipe-pipeline-test-{}-pipeline.jsonc",
            std::process::id()
        ));
        fs::write(&path, r#"{"processors":[]}"#).unwrap();

        let err = PreparedPreflight::try_from(OutputPreflightConfig {
            pipeline: Some(path.clone()),
            ..OutputPreflightConfig::default()
        })
        .unwrap_err();

        assert!(err.to_string().contains(".json extension"));

        let _ = fs::remove_file(path);
    }

    #[test]
    fn extract_default_pipeline_supports_nested_and_flattened_settings() {
        let nested = json!({
            "template": {
                "settings": {
                    "index": {
                        "default_pipeline": "geoip"
                    }
                }
            }
        });
        assert_eq!(extract_default_pipeline(&nested), Some("geoip"));

        let flattened = json!({
            "template": {
                "settings": {
                    "index.default_pipeline": "normalized"
                }
            }
        });
        assert_eq!(extract_default_pipeline(&flattened), Some("normalized"));
    }

    #[test]
    fn prepared_preflight_rejects_template_pipeline_mismatch_before_requests() {
        let pipeline_path = temp_json_path("geoip");
        let template_path = temp_json_path("template");
        fs::write(&pipeline_path, r#"{"processors":[]}"#).unwrap();
        fs::write(
            &template_path,
            r#"{"template":{"settings":{"index.default_pipeline":"other"}}}"#,
        )
        .unwrap();

        let err = PreparedPreflight::try_from(OutputPreflightConfig {
            pipeline: Some(pipeline_path.clone()),
            template: Some(template_path.clone()),
            ..OutputPreflightConfig::default()
        })
        .unwrap_err();

        assert!(err.to_string().contains("other"));
        assert!(err.to_string().contains("geoip"));

        let _ = fs::remove_file(pipeline_path);
        let _ = fs::remove_file(template_path);
    }

    #[test]
    fn prepared_preflight_template_with_pipeline_omits_bulk_pipeline_target() {
        let pipeline_path = temp_json_path("geoip");
        let template_path = temp_json_path("template-geoip");
        fs::write(&pipeline_path, r#"{"processors":[]}"#).unwrap();
        fs::write(
            &template_path,
            r#"{"template":{"settings":{"index.default_pipeline":"geoip"}}}"#,
        )
        .unwrap();

        let preflight = PreparedPreflight::try_from(OutputPreflightConfig {
            pipeline: Some(pipeline_path.clone()),
            template: Some(template_path.clone()),
            ..OutputPreflightConfig::default()
        })
        .unwrap();

        assert_eq!(preflight.pipeline.as_ref().unwrap().name, "geoip");
        assert_eq!(preflight.template_pipeline.as_deref(), Some("geoip"));
        assert!(preflight.bulk_pipeline.is_none());

        let _ = fs::remove_file(pipeline_path);
        let _ = fs::remove_file(template_path);
    }
}