-
Notifications
You must be signed in to change notification settings - Fork 240
Expand file tree
/
Copy pathdefault_ha_service.rs
More file actions
324 lines (288 loc) · 11.9 KB
/
default_ha_service.rs
File metadata and controls
324 lines (288 loc) · 11.9 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
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
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.
*/
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 std::net::SocketAddr;
use std::sync::atomic::AtomicI32;
use std::sync::atomic::AtomicI64;
use std::sync::atomic::AtomicU64;
use std::sync::Arc;
use std::time::Duration;
use rocketmq_common::common::broker::broker_role::BrokerRole;
use rocketmq_remoting::protocol::body::ha_runtime_info::HARuntimeInfo;
use rocketmq_rust::ArcMut;
use tokio::net::TcpListener;
use tokio::select;
use tokio::sync::Mutex;
use tokio::sync::Notify;
use tokio::time::sleep;
use tracing::error;
use tracing::info;
use crate::config::message_store_config::MessageStoreConfig;
use crate::ha::default_ha_client::DefaultHAClient;
use crate::ha::default_ha_connection::DefaultHAConnection;
use crate::ha::general_ha_client::GeneralHAClient;
use crate::ha::general_ha_connection::GeneralHAConnection;
use crate::ha::general_ha_service::GeneralHAService;
use crate::ha::group_transfer_service::GroupTransferService;
use crate::ha::ha_client::HAClient;
use crate::ha::ha_connection::HAConnection;
use crate::ha::ha_connection_state_notification_request::HAConnectionStateNotificationRequest;
use crate::ha::ha_connection_state_notification_service::HAConnectionStateNotificationService;
use crate::ha::ha_service::HAService;
use crate::ha::wait_notify_object::WaitNotifyObject;
use crate::log_file::flush_manager_impl::group_commit_request::GroupCommitRequest;
use crate::message_store::local_file_message_store::LocalFileMessageStore;
use crate::store_error::HAError;
use crate::store_error::HAResult;
pub struct DefaultHAService {
connection_count: Arc<AtomicU64>,
connection_list: Arc<Mutex<Vec<GeneralHAConnection>>>,
accept_socket_service: Option<AcceptSocketService>,
default_message_store: ArcMut<LocalFileMessageStore>,
wait_notify_object: Arc<WaitNotifyObject>,
push2_slave_max_offset: Arc<AtomicU64>,
group_transfer_service: Option<GroupTransferService>,
ha_client: GeneralHAClient,
ha_connection_state_notification_service: Option<HAConnectionStateNotificationService>,
}
impl DefaultHAService {
pub fn new(message_store: ArcMut<LocalFileMessageStore>) -> Self {
DefaultHAService {
connection_count: Arc::new(AtomicU64::new(0)),
connection_list: Arc::new(Mutex::new(Vec::new())),
accept_socket_service: None,
default_message_store: message_store,
wait_notify_object: Arc::new(WaitNotifyObject),
push2_slave_max_offset: Arc::new(AtomicU64::new(0)),
group_transfer_service: None,
ha_client: GeneralHAClient::new(),
ha_connection_state_notification_service: None,
}
}
// Add any necessary fields here
pub fn get_default_message_store(&self) -> &LocalFileMessageStore {
self.default_message_store.as_ref()
}
pub async fn notify_transfer_some(&self, _offset: i64) {
// This method is a placeholder for notifying transfer operations.
// The actual implementation would depend on the specific requirements of the HA service.
unimplemented!(" notify_transfer_some method is not implemented");
}
pub(crate) fn init(&mut self, this: ArcMut<Self>) -> HAResult<()> {
// Initialize the DefaultHAService with the provided message store.
let config = self.default_message_store.get_message_store_config();
let service = GeneralHAService::new_with_default_ha_service(this.clone());
let group_transfer_service = GroupTransferService::new(config.clone(), service.clone());
self.group_transfer_service = Some(group_transfer_service);
if config.broker_role == BrokerRole::Slave {
let default_message_store = self.default_message_store.clone();
let client = DefaultHAClient::new(default_message_store)
.map_err(|e| HAError::Service(format!("Failed to create DefaultHAClient: {e}")))?;
self.ha_client.set_default_ha_service(client)
}
let state_notification_service =
HAConnectionStateNotificationService::new(service, self.default_message_store.clone());
self.ha_connection_state_notification_service = Some(state_notification_service);
self.accept_socket_service = Some(AcceptSocketService::new(
self.default_message_store
.get_message_store_config()
.clone(),
this,
false,
));
Ok(())
}
pub async fn add_connection(&self, connection: GeneralHAConnection) {
// Add a new connection to the service
let mut vec = self.connection_list.lock().await;
vec.push(connection);
}
pub fn get_connection_count(&self) -> &AtomicU64 {
&self.connection_count
}
}
impl HAService for DefaultHAService {
async fn start(&mut self) -> HAResult<()> {
self.accept_socket_service
.as_mut()
.expect("AcceptSocketService not initialized")
.start()
.await?;
self.group_transfer_service
.as_mut()
.expect("GroupTransferService not initialized")
.start()
.await?;
self.ha_connection_state_notification_service
.as_mut()
.expect("HAConnectionStateNotificationService not initialized")
.start()
.await?;
self.ha_client.start().await;
Ok(())
}
fn shutdown(&self) {
todo!()
}
async fn change_to_master(&self, master_epoch: i32) -> HAResult<bool> {
todo!()
}
async fn change_to_master_when_last_role_is_master(&self, master_epoch: i32) -> HAResult<bool> {
todo!()
}
async fn change_to_slave(
&self,
new_master_addr: &str,
new_master_epoch: i32,
slave_id: Option<i64>,
) -> HAResult<bool> {
todo!()
}
async fn change_to_slave_when_master_not_change(
&self,
new_master_addr: &str,
new_master_epoch: i32,
) -> HAResult<bool> {
todo!()
}
fn update_master_address(&self, new_addr: &str) {
self.ha_client.update_master_address(new_addr);
}
fn update_ha_master_address(&self, new_addr: &str) {
self.ha_client.update_ha_master_address(new_addr);
}
fn in_sync_replicas_nums(&self, master_put_where: i64) -> i32 {
todo!()
}
fn get_connection_count(&self) -> &AtomicI32 {
todo!()
}
fn put_request(&self, request: GroupCommitRequest) {
todo!()
}
fn put_group_connection_state_request(&self, request: HAConnectionStateNotificationRequest) {
todo!()
}
fn get_connection_list<CN: HAConnection>(&self) -> Vec<Arc<CN>> {
todo!()
}
fn get_ha_client<CL: HAClient>(&self) -> Arc<CL> {
todo!()
}
fn get_push_to_slave_max_offset(&self) -> &AtomicI64 {
todo!()
}
fn get_runtime_info(&self, master_put_where: i64) -> HARuntimeInfo {
todo!()
}
fn get_wait_notify_object(&self) -> Arc<WaitNotifyObject> {
todo!()
}
fn is_slave_ok(&self, master_put_where: i64) -> bool {
todo!()
}
}
struct AcceptSocketService {
socket_address_listen: SocketAddr,
message_store_config: Arc<MessageStoreConfig>,
is_auto_switch: bool,
shutdown_notify: Arc<Notify>,
default_ha_service: ArcMut<DefaultHAService>,
}
impl AcceptSocketService {
pub fn new(
message_store_config: Arc<MessageStoreConfig>,
default_ha_service: ArcMut<DefaultHAService>,
is_auto_switch: bool,
) -> Self {
let ha_listen_port = message_store_config.ha_listen_port;
let socket_address_listen = SocketAddr::from(([0u8, 0u8, 0u8, 0u8], ha_listen_port as u16));
AcceptSocketService {
socket_address_listen,
message_store_config,
is_auto_switch,
shutdown_notify: Arc::new(Notify::new()),
default_ha_service,
}
}
pub async fn start(&mut self) -> HAResult<()> {
let listener = TcpListener::bind(self.socket_address_listen)
.await
.map_err(HAError::Io)?;
let shutdown_notify = self.shutdown_notify.clone();
let is_auto_switch = self.is_auto_switch;
let message_store_config = self.message_store_config.clone();
let default_ha_service = self.default_ha_service.clone();
tokio::spawn(async move {
let message_store_config = message_store_config;
let default_ha_service = default_ha_service;
loop {
select! {
_ = shutdown_notify.notified() => {
info!("AcceptSocketService is shutting down");
break;
}
// Accept new connections
accept_result = listener.accept() => {
match accept_result {
Ok((stream, addr)) => {
info!("HAService receive new connection, {}", addr);
if is_auto_switch {
unimplemented!("Auto-switching is not implemented yet");
}else{
let default_conn = DefaultHAConnection::new(default_ha_service.clone(), stream,message_store_config.clone()).await.expect("Error creating HAConnection");
let mut general_conn = GeneralHAConnection::new_with_default_ha_connection(default_conn);
if let Err(e) = general_conn.start().await {
error!("Error starting HAService: {}", e);
}else {
info!("HAService accept new connection, {}", addr);
default_ha_service.add_connection(general_conn).await;
}
};
}
Err(e) => {
error!("Failed to accept connection: {}", e);
// Add a small delay to prevent busy-waiting on persistent errors
sleep(Duration::from_millis(100)).await;
}
}
}
}
}
});
Ok(())
}
pub fn shutdown(&self) {
info!("Shutting down AcceptSocketService");
self.shutdown_notify.notify_waiters();
}
}