picodata_plugin/error_code.rs
1use tarantool::error::TarantoolErrorCode;
2use tarantool::static_assert;
3
4tarantool::define_enum_with_introspection! {
5 /// Error codes used with [`BoxError`] which are only generated by picodata.
6 ///
7 /// [`BoxError`]: tarantool::error::BoxError
8 pub enum ErrorCode {
9 // NOTE: At the moment of writing this the maximum tarantool error code
10 // on master branch is 286, but we still leave a huge window between
11 // that and our first error code just to be absolutely safe.
12 //
13 // We also don't want to go too big because of the msgpack encoding size
14 // considerations (65535 is the largest number which serializes into 3 bytes).
15
16 /// This is the first picodata error code. Use this for errors which
17 /// are hard to categorize and for which there's no specific way of handling.
18 ///
19 /// Also please use a **unique error message** for these kinds of errors,
20 /// so it's easy to find where it was generated in code.
21 ///
22 /// If your error instead should be handled in a specific way please
23 /// add a new one if the existing ones don't satisfy your needs.
24 Other = 10000,
25
26 /// Requested instance is not a leader.
27 NotALeader = 10001,
28
29 /// Contents of a builtin table has invalid format.
30 StorageCorrupted = 10002,
31
32 /// Operation request from different term.
33 TermMismatch = 10003,
34
35 /// Raft log is temporarily unavailable.
36 RaftLogUnavailable = 10004,
37
38 /// Can't check the predicate because raft log is compacted.
39 RaftLogCompacted = 10005,
40
41 /// Nearly impossible error indicating invalid request.
42 CasNoSuchRaftIndex = 10006,
43
44 /// Checking the predicate revealed a collision.
45 CasConflictFound = 10007,
46
47 /// Request expected raft entry to have a different term.
48 CasEntryTermMismatch = 10008,
49
50 /// SpaceNotAllowed: space {space} is prohibited for use in a predicate
51 CasTableNotAllowed = 10009,
52
53 /// Unexpected traft operation kind.
54 CasInvalidOpKind = 10010,
55
56 NoSuchService = 10011,
57 ServiceNotStarted = 10012,
58 ServicePoisoned = 10013,
59 ServiceNotAvailable = 10014,
60 WrongPluginVersion = 10015,
61
62 NoSuchInstance = 10016,
63 NoSuchReplicaset = 10017,
64
65 LeaderUnknown = 10018,
66
67 // Error in plugin system.
68 PluginError = 10019,
69
70 // Instance in question was expelled from the cluster.
71 InstanceExpelled = 10020,
72
73 // Replicaset in question was expelled from the cluster.
74 ReplicasetExpelled = 10021,
75
76 // Instance unavailiable due to it's target state is Offline
77 InstanceUnavaliable = 10022,
78
79 /// TableNotOperable: table {table} is prohibited for use in a predicate
80 CasTableNotOperable = 10023,
81
82 /// Picodata machinery is not yet initialized on the instance.
83 Uninitialized = 10024,
84
85 /// Instance is not allowed to be expelled in the given situation for
86 /// some reason. The most often solution is to use `picodata expel --force`.
87 ExpelNotAllowed = 10025,
88
89 /// TableNotOperable: table {table} is prohibited for use in a predicate
90 CasConfigNotAllowed = 10026,
91
92 /// Raft proposal was dropped by the leader.
93 RaftProposalDropped = 10027,
94
95 /// Generic sbroad error
96 SbroadError = 10028,
97
98 /// Not an actual error code, just designates the start of the range.
99 UserDefinedErrorCodesStart = 20000,
100 // Plugin writers should use error codes in this range
101 }
102}
103
104impl ErrorCode {
105 /// These types of errors signify different kinds of conflicts which can
106 /// occur during a [`compare_and_swap`] RPC request. If such an error
107 /// happens it's safe to retry the request under the following conditions:
108 /// - The raft read index operation is performed before each retry
109 /// - The preconditions of the request are checked before each retry
110 /// - The request is generated before each retry with an up to date raft index
111 ///
112 /// [`compare_and_swap`]: crate::internal::cas::compare_and_swap
113 #[inline]
114 pub fn is_retriable_for_cas(&self) -> bool {
115 match *self {
116 // Raft leader is in the middle of being changed.
117 // The client should synchronize and retry the request.
118 ErrorCode::LeaderUnknown
119 // Raft leader has changed since the CaS request was generated.
120 // The client should synchronize and retry the request.
121 | ErrorCode::NotALeader
122 // Raft term has changed since the CaS request was generated.
123 // The client should synchronize and retry the request.
124 | ErrorCode::TermMismatch
125 // Raft log was compacted on the leader, so the predicate cannot be checked.
126 // The client should synchronize and retry the request.
127 | ErrorCode::RaftLogCompacted
128 // Some raft log entries have disappeared on the leader, so the predicate cannot be checked.
129 // XXX Not sure how this would happen.
130 // The client should synchronize and retry the request.
131 | ErrorCode::RaftLogUnavailable
132 // Entry at requested index has a mismatched term in the leader's log.
133 // The client should synchronize ( raft log will likely be truncated) and retry the request.
134 | ErrorCode::CasEntryTermMismatch
135 // Leader checked the predicate and found a conflict.
136 // The client should synchronize and check the preconditions.
137 | ErrorCode::CasConflictFound
138 // Raft proposal was dropped by the leader.
139 // The client should synchronize and retry the request.
140 | ErrorCode::RaftProposalDropped
141 => true,
142 _ => false,
143 }
144 }
145}
146
147impl From<ErrorCode> for u32 {
148 #[inline(always)]
149 fn from(code: ErrorCode) -> u32 {
150 code as _
151 }
152}
153
154static_assert!(
155 ErrorCode::MIN as u32 > TarantoolErrorCode::MAX as u32,
156 "picodata error code range must not intersect tarantool's ones"
157);
158static_assert!(
159 ErrorCode::MAX as u32 <= ErrorCode::UserDefinedErrorCodesStart as u32,
160 "picodata builtin error code range must not intersect with user defined ones"
161);