-
Notifications
You must be signed in to change notification settings - Fork 273
Expand file tree
/
Copy pathremoting_command_defaults.rs
More file actions
289 lines (249 loc) Β· 10.1 KB
/
Copy pathremoting_command_defaults.rs
File metadata and controls
289 lines (249 loc) Β· 10.1 KB
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
// Copyright 2026 The RocketMQ Rust Authors
//
// 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.
//! Immutable defaults and factories for business `RemotingCommand` values.
use std::fmt;
use std::sync::OnceLock;
use bytes::Bytes;
use cheetah_string::CheetahString;
use crate::code::response_code::RemotingSysResponseCode;
use crate::protocol::command_custom_header::CommandCustomHeader;
use crate::protocol::remoting_command::RemotingCommand;
use crate::protocol::SerializeType;
const JAVA_DEFAULT_RESPONSE_REMARK: &str = "not set any response code";
/// Wire defaults shared by all business command factories in a process.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct RemotingCommandDefaults {
version: i32,
serialize_type: SerializeType,
}
impl RemotingCommandDefaults {
pub const fn new(version: i32, serialize_type: SerializeType) -> Self {
Self {
version,
serialize_type,
}
}
pub const fn version(self) -> i32 {
self.version
}
pub const fn serialize_type(self) -> SerializeType {
self.serialize_type
}
}
impl Default for RemotingCommandDefaults {
fn default() -> Self {
Self::new(crate::version::CURRENT_VERSION as i32, SerializeType::JSON)
}
}
/// Builds business commands from one immutable set of wire defaults.
///
/// A factory does not read process configuration. Application owners may keep
/// the process-default compatibility path or inject explicit factories when
/// multiple remoting configurations must coexist in one process.
///
/// # Examples
///
/// ```
/// use rocketmq_protocol::protocol::remoting_command_defaults::{
/// RemotingCommandDefaults, RemotingCommandFactory,
/// };
/// use rocketmq_protocol::protocol::SerializeType;
///
/// let json = RemotingCommandFactory::new(RemotingCommandDefaults::new(101, SerializeType::JSON));
/// let binary = RemotingCommandFactory::new(RemotingCommandDefaults::new(202, SerializeType::ROCKETMQ));
///
/// assert_eq!(json.create_remoting_command(10).version(), 101);
/// assert_eq!(binary.create_remoting_command(10).version(), 202);
/// ```
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct RemotingCommandFactory {
defaults: RemotingCommandDefaults,
}
impl RemotingCommandFactory {
/// Creates a factory with explicit version and serialization defaults.
pub const fn new(defaults: RemotingCommandDefaults) -> Self {
Self { defaults }
}
/// Returns the immutable defaults owned by this factory.
pub const fn defaults(&self) -> RemotingCommandDefaults {
self.defaults
}
/// Creates a base command with an explicit code.
pub fn create_remoting_command(&self, code: impl Into<i32>) -> RemotingCommand {
RemotingCommand::with_resolved_defaults(self.defaults.version(), self.defaults.serialize_type()).set_code(code)
}
/// Creates a request with an external body.
pub fn create_request(&self, code: impl Into<i32>, body: impl Into<Bytes>) -> RemotingCommand {
self.create_remoting_command(code).set_body(body)
}
/// Creates a request with a typed custom header.
pub fn create_request_command<T>(&self, code: impl Into<i32>, header: T) -> RemotingCommand
where
T: CommandCustomHeader + Sync + Send + 'static,
{
self.create_remoting_command(code).set_command_custom_header(header)
}
/// Creates a response with an explicit response code.
pub fn create_response_command_with_code(&self, code: impl Into<i32>) -> RemotingCommand {
self.create_remoting_command(code).mark_response_type()
}
/// Creates a response with an explicit code and typed custom header.
pub fn create_response_command_with_code_and_header(
&self,
code: impl Into<i32>,
header: impl CommandCustomHeader + Sync + Send + 'static,
) -> RemotingCommand {
self.create_response_command_with_code(code)
.set_command_custom_header(header)
}
/// Creates a response with an explicit code and remark.
pub fn create_response_command_with_code_remark(
&self,
code: impl Into<i32>,
remark: impl Into<CheetahString>,
) -> RemotingCommand {
self.create_remoting_command(code)
.set_remark_option(Some(remark.into()))
.mark_response_type()
}
/// Creates an explicitly successful response.
pub fn create_success_response_command(&self) -> RemotingCommand {
self.create_response_command_with_code(RemotingSysResponseCode::Success)
}
/// Creates an explicitly successful response with a typed custom header.
pub fn create_success_response_command_with_header(
&self,
header: impl CommandCustomHeader + Sync + Send + 'static,
) -> RemotingCommand {
self.create_response_command_with_code_and_header(RemotingSysResponseCode::Success, header)
}
/// Creates Java's unset error response.
pub fn create_java_default_error_response_command(&self) -> RemotingCommand {
self.create_response_command_with_code_remark(
RemotingSysResponseCode::SystemError,
JAVA_DEFAULT_RESPONSE_REMARK,
)
}
/// Creates Java's unset error response with a typed custom header.
pub fn create_java_default_error_response_command_with_header(
&self,
header: impl CommandCustomHeader + Sync + Send + 'static,
) -> RemotingCommand {
self.create_java_default_error_response_command()
.set_command_custom_header(header)
}
}
/// Returned when a process attempts to replace initialized command defaults.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct RemotingCommandDefaultsConflict {
initialized: RemotingCommandDefaults,
requested: RemotingCommandDefaults,
}
impl RemotingCommandDefaultsConflict {
pub const fn initialized(self) -> RemotingCommandDefaults {
self.initialized
}
pub const fn requested(self) -> RemotingCommandDefaults {
self.requested
}
}
impl fmt::Display for RemotingCommandDefaultsConflict {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
formatter,
"remoting command defaults are already initialized to {:?}, requested {:?}",
self.initialized, self.requested
)
}
}
impl std::error::Error for RemotingCommandDefaultsConflict {}
static APPLICATION_DEFAULTS: OnceLock<RemotingCommandDefaults> = OnceLock::new();
fn initialize_defaults(
cell: &OnceLock<RemotingCommandDefaults>,
defaults: RemotingCommandDefaults,
) -> Result<(), RemotingCommandDefaultsConflict> {
match cell.set(defaults) {
Ok(()) => Ok(()),
Err(requested) => {
// OnceLock returns Err only after retaining the previously initialized value.
let initialized = *cell
.get()
.expect("failed OnceLock::set always retains an initialized value");
if initialized == requested {
Ok(())
} else {
Err(RemotingCommandDefaultsConflict { initialized, requested })
}
}
}
}
/// Initializes the immutable defaults used by business command factories.
///
/// Repeating initialization with the same value is idempotent. A different
/// value is rejected so wire defaults cannot change after command creation.
///
/// # Errors
///
/// Returns [`RemotingCommandDefaultsConflict`] when different defaults were
/// initialized earlier in the process.
pub fn initialize_remoting_command_defaults(
defaults: RemotingCommandDefaults,
) -> Result<(), RemotingCommandDefaultsConflict> {
initialize_defaults(&APPLICATION_DEFAULTS, defaults)
}
pub(crate) fn application_remoting_command_defaults() -> RemotingCommandDefaults {
*APPLICATION_DEFAULTS.get_or_init(RemotingCommandDefaults::default)
}
/// Returns the immutable compatibility factory selected by the application.
///
/// Instance owners should capture this value once at their composition
/// boundary and pass it to request/response producers. The returned factory
/// does not read environment variables.
pub fn application_remoting_command_factory() -> RemotingCommandFactory {
RemotingCommandFactory::new(application_remoting_command_defaults())
}
#[cfg(test)]
mod tests {
use rocketmq_error::CanonicalCondition;
use super::*;
#[test]
fn initialization_is_idempotent_and_rejects_conflicts() {
let cell = OnceLock::new();
let first = RemotingCommandDefaults::new(1, SerializeType::JSON);
let second = RemotingCommandDefaults::new(2, SerializeType::ROCKETMQ);
assert_eq!(initialize_defaults(&cell, first), Ok(()));
assert_eq!(initialize_defaults(&cell, first), Ok(()));
assert_eq!(
initialize_defaults(&cell, second),
Err(RemotingCommandDefaultsConflict {
initialized: first,
requested: second,
})
);
}
#[test]
fn initialized_defaults_conflict_is_a_failed_precondition() {
let cell = OnceLock::new();
let initialized = RemotingCommandDefaults::new(1, SerializeType::JSON);
let requested = RemotingCommandDefaults::new(2, SerializeType::ROCKETMQ);
initialize_defaults(&cell, initialized).expect("first defaults initialize");
let source = initialize_defaults(&cell, requested).expect_err("different defaults conflict");
let error = crate::ProtocolContractViolation::from(source);
assert_eq!(error.condition(), CanonicalCondition::FailedPrecondition);
assert_eq!(
error.to_string(),
"remoting command defaults conflict with the initialized value"
);
}
}