|
| 1 | +// (c) Cartesi and individual authors (see AUTHORS) |
| 2 | +// SPDX-License-Identifier: Apache-2.0 (see LICENSE) |
| 3 | + |
| 4 | +package manager |
| 5 | + |
| 6 | +import ( |
| 7 | + "errors" |
| 8 | + "fmt" |
| 9 | +) |
| 10 | + |
| 11 | +var ErrApplicationFailureNotDurable = errors.New("application failure status is not durably confirmed") |
| 12 | + |
| 13 | +// ApplicationFailurePersistenceError means local machine work is fenced but |
| 14 | +// the repository has not confirmed the corresponding FAILED (or a stronger |
| 15 | +// terminal/deleted) application state. |
| 16 | +type ApplicationFailurePersistenceError struct { |
| 17 | + ApplicationID int64 |
| 18 | + WriteErr error |
| 19 | + ReadErr error |
| 20 | +} |
| 21 | + |
| 22 | +func (e *ApplicationFailurePersistenceError) Error() string { |
| 23 | + if e.ReadErr != nil { |
| 24 | + return fmt.Sprintf( |
| 25 | + "%v: application=%d write_error=%v read_error=%v", |
| 26 | + ErrApplicationFailureNotDurable, e.ApplicationID, e.WriteErr, e.ReadErr, |
| 27 | + ) |
| 28 | + } |
| 29 | + return fmt.Sprintf( |
| 30 | + "%v: application=%d write_error=%v durable status remains unconfirmed", |
| 31 | + ErrApplicationFailureNotDurable, e.ApplicationID, e.WriteErr, |
| 32 | + ) |
| 33 | +} |
| 34 | + |
| 35 | +func (e *ApplicationFailurePersistenceError) Unwrap() []error { |
| 36 | + errList := []error{ErrApplicationFailureNotDurable} |
| 37 | + if e.WriteErr != nil { |
| 38 | + errList = append(errList, e.WriteErr) |
| 39 | + } |
| 40 | + if e.ReadErr != nil { |
| 41 | + errList = append(errList, e.ReadErr) |
| 42 | + } |
| 43 | + return errList |
| 44 | +} |
| 45 | + |
| 46 | +// IsOnlyApplicationFailurePersistenceErrors reports whether err is one or |
| 47 | +// more application-local durability failures and contains no global failure. |
| 48 | +// It deliberately examines joined top-level errors without descending into a |
| 49 | +// persistence error's write/read causes. |
| 50 | +func IsOnlyApplicationFailurePersistenceErrors(err error) bool { |
| 51 | + if err == nil { |
| 52 | + return false |
| 53 | + } |
| 54 | + if _, ok := err.(*ApplicationFailurePersistenceError); ok { |
| 55 | + return true |
| 56 | + } |
| 57 | + |
| 58 | + switch wrapped := err.(type) { |
| 59 | + case interface{ Unwrap() []error }: |
| 60 | + children := wrapped.Unwrap() |
| 61 | + if len(children) == 0 { |
| 62 | + return false |
| 63 | + } |
| 64 | + for _, child := range children { |
| 65 | + if !IsOnlyApplicationFailurePersistenceErrors(child) { |
| 66 | + return false |
| 67 | + } |
| 68 | + } |
| 69 | + return true |
| 70 | + case interface{ Unwrap() error }: |
| 71 | + return IsOnlyApplicationFailurePersistenceErrors(wrapped.Unwrap()) |
| 72 | + default: |
| 73 | + return false |
| 74 | + } |
| 75 | +} |
0 commit comments