rustfs-ecstore 1.0.1

Erasure coding storage backend for RustFS, providing efficient data storage and retrieval with redundancy.
Documentation
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
// Copyright 2024 RustFS Team
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use crate::disk::error::Error;

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct WriteQuorumFailureSummary {
    pub required: usize,
    pub achieved: usize,
    pub failed: usize,
    pub total: usize,
    pub offline_disks: usize,
    pub ignored_failures: usize,
    pub retryable_failures: usize,
    pub dominant_error: Option<Error>,
    pub dominant_error_label: &'static str,
}

pub static OBJECT_OP_IGNORED_ERRS: &[Error] = &[
    Error::DiskNotFound,
    Error::FaultyDisk,
    Error::FaultyRemoteDisk,
    Error::DiskAccessDenied,
    Error::DiskOngoingReq,
    Error::UnformattedDisk,
];

pub static BUCKET_OP_IGNORED_ERRS: &[Error] = &[
    Error::DiskNotFound,
    Error::FaultyDisk,
    Error::FaultyRemoteDisk,
    Error::DiskAccessDenied,
    Error::UnformattedDisk,
];

pub static BASE_IGNORED_ERRS: &[Error] = &[Error::DiskNotFound, Error::FaultyDisk, Error::FaultyRemoteDisk];

pub fn reduce_write_quorum_errs(errors: &[Option<Error>], ignored_errs: &[Error], quorun: usize) -> Option<Error> {
    reduce_quorum_errs(errors, ignored_errs, quorun, Error::ErasureWriteQuorum)
}

pub fn reduce_read_quorum_errs(errors: &[Option<Error>], ignored_errs: &[Error], quorun: usize) -> Option<Error> {
    reduce_quorum_errs(errors, ignored_errs, quorun, Error::ErasureReadQuorum)
}

pub fn reduce_quorum_errs(errors: &[Option<Error>], ignored_errs: &[Error], quorun: usize, quorun_err: Error) -> Option<Error> {
    let (max_count, err) = reduce_errs(errors, ignored_errs);
    if max_count >= quorun { err } else { Some(quorun_err) }
}

pub fn reduce_errs(errors: &[Option<Error>], ignored_errs: &[Error]) -> (usize, Option<Error>) {
    let nil_error = Error::other("nil".to_string());

    // First count the number of None values (treated as nil errors)
    let nil_count = errors.iter().filter(|e| e.is_none()).count();

    let err_counts = errors
        .iter()
        .filter_map(|e| e.as_ref()) // Only process errors stored in Some
        .fold(std::collections::HashMap::new(), |mut acc, e| {
            if is_ignored_err(ignored_errs, e) {
                return acc;
            }
            *acc.entry(e.clone()).or_insert(0) += 1;
            acc
        });

    // Find the most frequent non-nil error
    let (best_err, best_count) = err_counts
        .into_iter()
        .max_by(|(_, c1), (_, c2)| c1.cmp(c2))
        .unwrap_or((nil_error, 0));

    // Guard against the empty/all-ignored case: when there is no real error and
    // no nil value, the seeded placeholder `nil_error` must not leak out as the
    // dominant error. Return the `None` sentinel so callers (e.g. write-quorum
    // failure summaries and their metric labels) treat it as "no dominant error".
    if best_count == 0 && nil_count == 0 {
        return (0, None);
    }

    // Compare nil errors with the top non-nil error and prefer the nil error
    if nil_count > best_count || (nil_count == best_count && nil_count > 0) {
        (nil_count, None)
    } else {
        (best_count, Some(best_err))
    }
}

pub fn build_write_quorum_failure_summary(
    errors: &[Option<Error>],
    ignored_errs: &[Error],
    quorum: usize,
) -> WriteQuorumFailureSummary {
    let total = errors.len();
    let achieved = errors.iter().filter(|err| err.is_none()).count();
    let failed = total.saturating_sub(achieved);
    let offline_disks = count_errs(errors, &Error::DiskNotFound);
    let ignored_failures = errors
        .iter()
        .filter_map(|err| err.as_ref())
        .filter(|err| is_ignored_err(ignored_errs, err))
        .count();
    let retryable_failures = count_retryable_failures(errors);
    let (_, dominant_error) = reduce_errs(errors, ignored_errs);
    let dominant_error_label = dominant_error_label(errors, ignored_errs, dominant_error.as_ref());

    WriteQuorumFailureSummary {
        required: quorum,
        achieved,
        failed,
        total,
        offline_disks,
        ignored_failures,
        retryable_failures,
        dominant_error,
        dominant_error_label,
    }
}

fn dominant_error_label(errors: &[Option<Error>], ignored_errs: &[Error], dominant_error: Option<&Error>) -> &'static str {
    let Some(dominant_error) = dominant_error else {
        return "nil_dominated";
    };

    if dominant_error == &Error::DiskNotFound {
        return "disk_not_found";
    }
    if dominant_error == &Error::ShortWrite {
        return "short_write";
    }

    errors
        .iter()
        .filter_map(|err| err.as_ref())
        .find(|err| !is_ignored_err(ignored_errs, err) && *err == dominant_error)
        .and_then(Error::internode_http_error_kind)
        .map(|kind| kind.metric_label())
        .unwrap_or("other_error")
}

pub fn is_ignored_err(ignored_errs: &[Error], err: &Error) -> bool {
    ignored_errs.iter().any(|e| e == err)
}

pub fn count_errs(errors: &[Option<Error>], err: &Error) -> usize {
    errors.iter().filter(|&e| e.as_ref() == Some(err)).count()
}

pub fn count_retryable_failures(errors: &[Option<Error>]) -> usize {
    errors
        .iter()
        .filter_map(|err| err.as_ref())
        .filter(|err| err.is_retryable_internode_write_failure())
        .count()
}

pub fn is_all_buckets_not_found(errs: &[Option<Error>]) -> bool {
    for err in errs.iter() {
        if let Some(err) = err {
            if err == &Error::DiskNotFound || err == &Error::VolumeNotFound {
                continue;
            }

            return false;
        }
        return false;
    }

    !errs.is_empty()
}

#[cfg(test)]
mod tests {
    use super::*;

    fn err_io(msg: &str) -> Error {
        Error::Io(std::io::Error::other(msg))
    }

    #[test]
    fn test_reduce_errs_basic() {
        let e1 = err_io("a");
        let e2 = err_io("b");
        let errors = vec![Some(e1.clone()), Some(e1.clone()), Some(e2), None];
        let ignored = vec![];
        let (count, err) = reduce_errs(&errors, &ignored);
        assert_eq!(count, 2);
        assert_eq!(err, Some(e1));
    }

    #[test]
    fn test_reduce_errs_ignored() {
        let e1 = err_io("a");
        let e2 = err_io("b");
        let errors = vec![Some(e1.clone()), Some(e2.clone()), Some(e1.clone()), Some(e2.clone()), None];
        let ignored = vec![e2];
        let (count, err) = reduce_errs(&errors, &ignored);
        assert_eq!(count, 2);
        assert_eq!(err, Some(e1));
    }

    #[test]
    fn test_reduce_quorum_errs() {
        let e1 = err_io("a");
        let e2 = err_io("b");
        let errors = vec![Some(e1.clone()), Some(e1.clone()), Some(e2), None];
        let ignored = vec![];
        let quorum_err = Error::FaultyDisk;
        // quorum = 2, should return e1
        let res = reduce_quorum_errs(&errors, &ignored, 2, quorum_err.clone());
        assert_eq!(res, Some(e1));
        // quorum = 3, should return quorum error
        let res = reduce_quorum_errs(&errors, &ignored, 3, quorum_err.clone());
        assert_eq!(res, Some(quorum_err));
    }

    #[test]
    fn test_write_quorum_reduction_preserves_internode_http_identity() {
        use http::StatusCode;
        use rustfs_rio::InternodeHttpErrorKind::{ConnectionRefused, HttpStatus, Unknown};

        for (kind, retryable) in [
            (ConnectionRefused, true),
            (HttpStatus(StatusCode::SERVICE_UNAVAILABLE), true),
            (HttpStatus(StatusCode::CONFLICT), true),
            (Unknown, false),
            (HttpStatus(StatusCode::BAD_REQUEST), false),
        ] {
            // Construct both producer errors independently: the reducer owns the first clone.
            let first = Error::from(rustfs_rio::new_test_internode_http_io_error(kind));
            let second = Error::from(rustfs_rio::new_test_internode_http_io_error(kind));
            assert_eq!(first.internode_http_error_kind(), Some(kind));
            assert_eq!(second.internode_http_error_kind(), Some(kind));
            assert_eq!(first.is_retryable_internode_write_failure(), retryable);
            let errors = [Some(first), Some(second), None];
            let reduced = reduce_write_quorum_errs(&errors, OBJECT_OP_IGNORED_ERRS, 2)
                .expect("two equal producer errors must dominate one successful write");

            assert_eq!(Some(&reduced), errors[0].as_ref());
            assert_eq!(
                reduced.is_retryable_internode_write_failure(),
                retryable,
                "quorum reduction changed retryability for {kind:?}"
            );
            assert_eq!(reduced.internode_http_error_kind(), Some(kind));
            if let HttpStatus(status) = kind {
                assert!(reduced.is_internode_http_status(status.as_u16()));
            }
            let Error::Io(io_error) = &reduced else {
                panic!("the dominant error must remain Io: {reduced:?}");
            };
            let source = io_error
                .get_ref()
                .and_then(|source| source.downcast_ref::<rustfs_rio::InternodeHttpError>())
                .expect("quorum reduction must retain the structured internode error");
            assert_eq!(source.context().method(), "PUT");
            assert_eq!(source.context().target(), "/rustfs/rpc/put_file_stream");
            assert_eq!(
                source.context().operation(),
                Some(rustfs_io_metrics::internode_metrics::INTERNODE_OPERATION_PUT_FILE_STREAM)
            );
        }
    }

    #[test]
    fn test_clone_and_write_quorum_do_not_promote_non_retryable_errors() {
        use http::StatusCode;
        use rustfs_rio::InternodeHttpErrorKind::{HttpStatus, Unknown};

        for original in [
            Error::from(rustfs_rio::new_test_internode_http_io_error(Unknown)),
            Error::from(rustfs_rio::new_test_internode_http_io_error(HttpStatus(StatusCode::BAD_REQUEST))),
            Error::from(rustfs_rio::new_test_internode_http_io_error(HttpStatus(StatusCode::FORBIDDEN))),
            Error::from(rustfs_rio::new_test_internode_http_io_error(HttpStatus(StatusCode::NOT_FOUND))),
            Error::from(rustfs_rio::new_test_internode_http_io_error(HttpStatus(
                StatusCode::INTERNAL_SERVER_ERROR,
            ))),
            err_io("internode connection reset: PUT /rustfs/rpc/put_file_stream"),
        ] {
            assert!(!original.is_retryable_internode_write_failure());
            let cloned = original.clone();
            let reduced =
                reduce_write_quorum_errs(&[Some(original)], &[], 1).expect("a non-retryable error must remain an error");
            assert!(!cloned.is_retryable_internode_write_failure());
            assert!(!reduced.is_retryable_internode_write_failure());
        }
    }

    #[test]
    fn test_count_errs() {
        let e1 = err_io("a");
        let e2 = err_io("b");
        let errors = vec![Some(e1.clone()), Some(e2.clone()), Some(e1.clone()), None];
        assert_eq!(count_errs(&errors, &e1), 2);
        assert_eq!(count_errs(&errors, &e2), 1);
    }

    #[test]
    fn test_is_ignored_err() {
        let e1 = err_io("a");
        let e2 = err_io("b");
        let ignored = vec![e1.clone()];
        assert!(is_ignored_err(&ignored, &e1));
        assert!(!is_ignored_err(&ignored, &e2));
    }

    #[test]
    fn test_build_write_quorum_failure_summary() {
        let retryable = Error::from(rustfs_rio::new_test_internode_http_io_error(
            rustfs_rio::InternodeHttpErrorKind::ConnectionReset,
        ));
        let non_retryable = err_io("other");
        let errors = vec![
            None,
            None,
            None,
            None,
            None,
            Some(retryable),
            Some(non_retryable),
            Some(Error::DiskNotFound),
        ];

        let summary = build_write_quorum_failure_summary(&errors, OBJECT_OP_IGNORED_ERRS, 6);
        assert_eq!(summary.required, 6);
        assert_eq!(summary.achieved, 5);
        assert_eq!(summary.failed, 3);
        assert_eq!(summary.total, 8);
        assert_eq!(summary.offline_disks, 1);
        assert_eq!(summary.ignored_failures, 1);
        assert_eq!(summary.retryable_failures, 1);
        assert_eq!(summary.dominant_error, None);
        assert_eq!(summary.dominant_error_label, "nil_dominated");
    }

    #[test]
    fn test_build_write_quorum_failure_summary_preserves_internode_label() {
        let retryable_a = Error::from(rustfs_rio::new_test_internode_http_io_error(
            rustfs_rio::InternodeHttpErrorKind::ConnectionReset,
        ));
        let retryable_b = Error::from(rustfs_rio::new_test_internode_http_io_error(
            rustfs_rio::InternodeHttpErrorKind::ConnectionReset,
        ));
        let errors = vec![Some(retryable_a), Some(retryable_b)];

        let summary = build_write_quorum_failure_summary(&errors, OBJECT_OP_IGNORED_ERRS, 2);

        assert_eq!(summary.retryable_failures, 2);
        assert_eq!(summary.dominant_error_label, "connection_reset");
    }

    #[test]
    fn test_reduce_errs_all_ignored_returns_none() {
        // All disks return an ignored error (e.g. every disk offline): err_counts
        // ends up empty and nil_count == 0. The synthetic "nil" placeholder must
        // not leak; the function must return the (0, None) sentinel.
        let errors = vec![
            Some(Error::DiskNotFound),
            Some(Error::DiskNotFound),
            Some(Error::DiskNotFound),
        ];
        let (count, err) = reduce_errs(&errors, OBJECT_OP_IGNORED_ERRS);
        assert_eq!(count, 0);
        assert_eq!(err, None);
    }

    #[test]
    fn test_reduce_errs_empty_slice_returns_none() {
        // Empty slice: no errors and no nil values -> (0, None), not the placeholder.
        let errors: Vec<Option<Error>> = vec![];
        let ignored: Vec<Error> = vec![];
        let (count, err) = reduce_errs(&errors, &ignored);
        assert_eq!(count, 0);
        assert_eq!(err, None);
    }

    #[test]
    fn test_build_write_quorum_failure_summary_all_ignored_is_nil_dominated() {
        // All disks offline (all DiskNotFound). The dominant error must be None and
        // the label must be "nil_dominated" rather than falling back to "other_error".
        let errors = vec![
            Some(Error::DiskNotFound),
            Some(Error::DiskNotFound),
            Some(Error::DiskNotFound),
        ];
        let summary = build_write_quorum_failure_summary(&errors, OBJECT_OP_IGNORED_ERRS, 2);
        assert_eq!(summary.dominant_error, None);
        assert_eq!(summary.dominant_error_label, "nil_dominated");
    }

    #[test]
    fn test_reduce_errs_nil_tiebreak() {
        // Error::Nil and another error have the same count, should prefer Nil
        let e1 = err_io("a");
        let errors = vec![Some(e1.clone()), None, Some(e1), None]; // e1:2, Nil:2
        let ignored = vec![];
        let (count, err) = reduce_errs(&errors, &ignored);
        assert_eq!(count, 2);
        assert_eq!(err, None); // None means Error::Nil is preferred
    }
}