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
use http::StatusCode;
use serde::Deserialize;

use crate::{
    http::HttpError,
    sinks::{
        elasticsearch::service::ElasticsearchResponse,
        util::retries::{RetryAction, RetryLogic},
    },
};

#[derive(Deserialize, Debug)]
struct EsResultResponse {
    items: Vec<EsResultItem>,
}

impl EsResultResponse {
    fn parse(body: &str) -> Result<Self, String> {
        serde_json::from_str::<EsResultResponse>(body).map_err(|json_error| {
            format!(
                "some messages failed, could not parse response, error: {}",
                json_error
            )
        })
    }

    /// Returns iterator over status codes for items and optional error details.
    fn iter_status(&self) -> impl Iterator<Item = (StatusCode, Option<&EsErrorDetails>)> {
        self.items.iter().filter_map(|item| {
            item.result()
                .status
                .and_then(|status| StatusCode::from_u16(status).ok())
                .map(|status| (status, item.result().error.as_ref()))
        })
    }

    /// Selects the first error since logging all errors would be quite verbose and many are duplicates.
    /// If partial retry is enabled and we don't retry, this is because there is no retriable error in the
    /// response, thus all errors are equally interesting so logging the first is sufficient.
    /// When partial retry is disabled, we don't retry on any error.
    fn get_error_reason(&self, body: &str) -> String {
        match self
            .items
            .iter()
            .find_map(|item| item.result().error.as_ref())
        {
            Some(error) => format!("error type: {}, reason: {}", error.err_type, error.reason),
            None => format!("error response: {}", body),
        }
    }
}

#[derive(Deserialize, Debug)]
enum EsResultItem {
    #[serde(rename = "index")]
    Index(EsIndexResult),
    #[serde(rename = "create")]
    Create(EsIndexResult),
    #[serde(rename = "update")]
    Update(EsIndexResult),
}

impl EsResultItem {
    #[allow(clippy::missing_const_for_fn)] // const cannot run destructor
    fn result(&self) -> &EsIndexResult {
        match self {
            EsResultItem::Index(r) => r,
            EsResultItem::Create(r) => r,
            EsResultItem::Update(r) => r,
        }
    }
}

#[derive(Deserialize, Debug)]
struct EsIndexResult {
    status: Option<u16>,
    error: Option<EsErrorDetails>,
}

#[derive(Deserialize, Debug)]
struct EsErrorDetails {
    reason: String,
    #[serde(rename = "type")]
    err_type: String,
}

#[derive(Clone)]
pub struct ElasticsearchRetryLogic {
    pub retry_partial: bool,
}

impl RetryLogic for ElasticsearchRetryLogic {
    type Error = HttpError;
    type Response = ElasticsearchResponse;

    fn is_retriable_error(&self, _error: &Self::Error) -> bool {
        true
    }

    fn should_retry_response(&self, response: &ElasticsearchResponse) -> RetryAction {
        let status = response.http_response.status();

        match status {
            StatusCode::TOO_MANY_REQUESTS => RetryAction::Retry("too many requests".into()),
            StatusCode::NOT_IMPLEMENTED => {
                RetryAction::DontRetry("endpoint not implemented".into())
            }
            _ if status.is_server_error() => RetryAction::Retry(
                format!(
                    "{}: {}",
                    status,
                    String::from_utf8_lossy(response.http_response.body())
                )
                .into(),
            ),
            _ if status.is_client_error() => {
                let body = String::from_utf8_lossy(response.http_response.body());
                RetryAction::DontRetry(format!("client-side error, {}: {}", status, body).into())
            }
            _ if status.is_success() => {
                let body = String::from_utf8_lossy(response.http_response.body());

                if body.contains("\"errors\":true") {
                    match EsResultResponse::parse(&body) {
                        Ok(resp) => {
                            if self.retry_partial {
                                // We will retry if there exists at least one item that
                                // failed with a retriable error.
                                // Those are backpressure and server errors.
                                if let Some((status, error)) =
                                    resp.iter_status().find(|(status, _)| {
                                        *status == StatusCode::TOO_MANY_REQUESTS
                                            || status.is_server_error()
                                    })
                                {
                                    let msg = if let Some(error) = error {
                                        format!(
                                            "partial error, status: {}, error type: {}, reason: {}",
                                            status, error.err_type, error.reason
                                        )
                                    } else {
                                        format!("partial error, status: {}", status)
                                    };
                                    return RetryAction::Retry(msg.into());
                                }
                            }

                            RetryAction::DontRetry(resp.get_error_reason(&body).into())
                        }
                        Err(msg) => RetryAction::DontRetry(msg.into()),
                    }
                } else {
                    RetryAction::Successful
                }
            }
            _ => RetryAction::DontRetry(format!("response status: {}", status).into()),
        }
    }
}

#[cfg(test)]
mod tests {
    use bytes::Bytes;
    use http::Response;
    use similar_asserts::assert_eq;
    use vector_lib::{internal_event::CountByteSize, json_size::JsonSize};

    use super::*;
    use crate::event::EventStatus;

    #[test]
    fn handles_error_response() {
        let json = "{\"took\":185,\"errors\":true,\"items\":[{\"index\":{\"_index\":\"test-hgw28jv10u\",\"_type\":\"log_lines\",\"_id\":\"3GhQLXEBE62DvOOUKdFH\",\"status\":400,\"error\":{\"type\":\"illegal_argument_exception\",\"reason\":\"mapper [message] of different type, current_type [long], merged_type [text]\"}}}]}";
        let response = Response::builder()
            .status(StatusCode::OK)
            .body(Bytes::from(json))
            .unwrap();
        let logic = ElasticsearchRetryLogic {
            retry_partial: false,
        };
        assert!(matches!(
            logic.should_retry_response(&ElasticsearchResponse {
                http_response: response,
                event_status: EventStatus::Rejected,
                events_byte_size: CountByteSize(1, JsonSize::new(1)).into(),
            }),
            RetryAction::DontRetry(_)
        ));
    }

    #[test]
    fn handles_partial_error_response() {
        let json = "{\"took\":34,\"errors\":true,\"items\":[{\"index\":{\"_index\":\"test-asjkf1234\",\"_type\":\"log_lines\",\"_id\":\"4Z3QLYEBT52RtoOEKz2H\",\"status\":429}}]}";
        let response = Response::builder()
            .status(StatusCode::OK)
            .body(Bytes::from(json))
            .unwrap();
        let logic = ElasticsearchRetryLogic {
            retry_partial: true,
        };
        assert!(matches!(
            logic.should_retry_response(&ElasticsearchResponse {
                http_response: response,
                event_status: EventStatus::Errored,
                events_byte_size: CountByteSize(1, JsonSize::new(1)).into(),
            }),
            RetryAction::Retry(_)
        ));
    }

    #[test]
    fn get_index_error_reason() {
        let json = "{\"took\":185,\"errors\":true,\"items\":[{\"index\":{\"_index\":\"test-hgw28jv10u\",\"_type\":\"log_lines\",\"_id\":\"3GhQLXEBE62DvOOUKdFH\",\"status\":400,\"error\":{\"type\":\"illegal_argument_exception\",\"reason\":\"mapper [message] of different type, current_type [long], merged_type [text]\"}}}]}";
        let reason = match EsResultResponse::parse(json) {
            Ok(resp) => resp.get_error_reason(json),
            Err(msg) => msg,
        };
        assert_eq!(reason, "error type: illegal_argument_exception, reason: mapper [message] of different type, current_type [long], merged_type [text]");
    }

    #[test]
    fn get_create_error_reason() {
        let json = "{\"took\":3,\"errors\":true,\"items\":[{\"create\":{\"_index\":\"test-hgw28jv10u\",\"_type\":\"_doc\",\"_id\":\"aBLq1HcBWD7eBWkW2nj4\",\"status\":400,\"error\":{\"type\":\"mapper_parsing_exception\",\"reason\":\"object mapping for [host] tried to parse field [host] as object, but found a concrete value\"}}}]}";
        let reason = match EsResultResponse::parse(json) {
            Ok(resp) => resp.get_error_reason(json),
            Err(msg) => msg,
        };
        assert_eq!(reason, "error type: mapper_parsing_exception, reason: object mapping for [host] tried to parse field [host] as object, but found a concrete value");
    }
}