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 /// Not an actual error code, just designates the start of the range.
93 UserDefinedErrorCodesStart = 20000,
94 // Plugin writers should use error codes in this range
95 }
96}
97
98impl ErrorCode {
99 /// These types of errors signify different kinds of conflicts which can
100 /// occur during a [`compare_and_swap`] RPC request. If such an error
101 /// happens it's safe to retry the request under the following conditions:
102 /// - The raft read index operation is performed before each retry
103 /// - The preconditions of the request are checked before each retry
104 /// - The request is generated before each retry with an up to date raft index
105 ///
106 /// [`compare_and_swap`]: crate::internal::cas::compare_and_swap
107 #[inline]
108 pub fn is_retriable_for_cas(&self) -> bool {
109 match *self {
110 // Raft leader is in the middle of being changed.
111 // The client should synchronize and retry the request.
112 ErrorCode::LeaderUnknown
113 // Raft leader has changed since the CaS request was generated.
114 // The client should synchronize and retry the request.
115 | ErrorCode::NotALeader
116 // Raft term has changed since the CaS request was generated.
117 // The client should synchronize and retry the request.
118 | ErrorCode::TermMismatch
119 // Raft log was compacted on the leader, so the predicate cannot be checked.
120 // The client should synchronize and retry the request.
121 | ErrorCode::RaftLogCompacted
122 // Some raft log entries have disappeared on the leader, so the predicate cannot be checked.
123 // XXX Not sure how this would happen.
124 // The client should synchronize and retry the request.
125 | ErrorCode::RaftLogUnavailable
126 // Entry at requested index has a mismatched term in the leader's log.
127 // The client should synchronize ( raft log will likely be truncated) and retry the request.
128 | ErrorCode::CasEntryTermMismatch
129 // Leader checked the predicate and found a conflict.
130 // The client should synchronize and check the preconditions.
131 | ErrorCode::CasConflictFound
132 => true,
133 _ => false,
134 }
135 }
136}
137
138impl From<ErrorCode> for u32 {
139 #[inline(always)]
140 fn from(code: ErrorCode) -> u32 {
141 code as _
142 }
143}
144
145static_assert!(
146 ErrorCode::MIN as u32 > TarantoolErrorCode::MAX as u32,
147 "picodata error code range must not intersect tarantool's ones"
148);
149static_assert!(
150 ErrorCode::MAX as u32 <= ErrorCode::UserDefinedErrorCodesStart as u32,
151 "picodata builtin error code range must not intersect with user defined ones"
152);