ratchjob 0.2.1

一个rust实现的分布式任务调度平台服务。计划完全兼容xxl-job协议,然后再增强一些任务调度平台能力。
Documentation
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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
#![allow(clippy::suspicious_open_options)]
use std::{path::Path, sync::Arc};

use actix::prelude::*;
use bean_factory::{bean, Inject};
use quick_protobuf::{BytesReader, Writer};
use tokio::{
    fs::OpenOptions,
    io::{AsyncReadExt, AsyncSeekExt, AsyncWriteExt},
};

use crate::common::protobuf_utils::MessageBufReader;

use super::{
    log::{LogSnapshotItem, SnapshotHeader, SnapshotRange},
    model::{SnapshotHeaderDto, SnapshotRecordDto},
    raftindex::{RaftIndexManager, RaftIndexRequest, RaftIndexResponse},
};

#[derive(Debug)]
pub struct SnapshotWriter {
    file: tokio::fs::File,
}

impl SnapshotWriter {
    pub async fn init(path: &str, header: SnapshotHeaderDto) -> anyhow::Result<Self> {
        let mut file = OpenOptions::new()
            .read(true)
            .write(true)
            //.append(true)
            //.create_new(true)
            .create(true)
            .open(path)
            .await?;
        let mut buf = Vec::new();
        let mut writer = Writer::new(&mut buf);
        let record = header.to_record_do();
        writer.write_message(&record)?;
        file.write_all(&buf).await?;
        Ok(Self { file })
    }

    pub async fn write(&mut self, buf: &[u8]) -> anyhow::Result<()> {
        self.file.write_all(buf).await?;
        Ok(())
    }

    pub async fn write_record(&mut self, record: &SnapshotRecordDto) -> anyhow::Result<()> {
        let mut buf = Vec::new();
        let mut writer = Writer::new(&mut buf);
        writer.write_message(&record.to_record_do())?;
        self.file.write_all(&buf).await?;
        Ok(())
    }

    pub async fn flush(&mut self) -> anyhow::Result<()> {
        self.file.flush().await?;
        Ok(())
    }
}

///
/// 每次打包镜像创建一个WriterActor
pub struct SnapshotWriterActor {
    path: Arc<String>,
    header: Option<SnapshotHeaderDto>,
    inner_writer: Option<SnapshotWriter>,
}

impl SnapshotWriterActor {
    pub fn new(path: Arc<String>, header: SnapshotHeaderDto) -> Self {
        Self {
            path,
            header: Some(header),
            inner_writer: None,
        }
    }

    fn init(&mut self, ctx: &mut Context<Self>) {
        let path = self.path.clone();
        let header = self.header.take().unwrap();
        async move {
            let writer = SnapshotWriter::init(&path, header).await?;
            Ok(writer)
        }
        .into_actor(self)
        .map(|v: anyhow::Result<SnapshotWriter>, act, ctx| {
            if let Ok(v) = v {
                act.inner_writer = Some(v);
            } else {
                ctx.stop();
            };
        })
        .wait(ctx);
    }

    fn write(&mut self, ctx: &mut Context<Self>, record: SnapshotRecordDto) {
        let mut writer = self.inner_writer.take().unwrap();
        async move {
            writer.write_record(&record).await?;
            Ok(writer)
        }
        .into_actor(self)
        .map(|v: anyhow::Result<SnapshotWriter>, act, ctx| {
            if let Ok(v) = v {
                act.inner_writer = Some(v);
            } else {
                ctx.stop()
            }
        })
        .wait(ctx);
    }

    fn flush(&mut self, ctx: &mut Context<Self>) {
        let mut writer = self.inner_writer.take().unwrap();
        async move {
            writer.flush().await?;
            Ok(writer)
        }
        .into_actor(self)
        .map(|v: anyhow::Result<SnapshotWriter>, act, ctx| {
            if let Ok(v) = v {
                act.inner_writer = Some(v);
            } else {
                ctx.stop()
            }
        })
        .wait(ctx);
    }
}

impl Actor for SnapshotWriterActor {
    type Context = Context<Self>;

    fn started(&mut self, ctx: &mut Self::Context) {
        self.init(ctx);
    }
}

#[derive(Message, Debug)]
#[rtype(result = "anyhow::Result<SnapshotWriterResponse>")]
pub enum SnapshotWriterRequest {
    Record(SnapshotRecordDto),
    Flush,
}

pub enum SnapshotWriterResponse {
    Path(Arc<String>),
    None,
}

impl Handler<SnapshotWriterRequest> for SnapshotWriterActor {
    type Result = anyhow::Result<SnapshotWriterResponse>;

    fn handle(&mut self, msg: SnapshotWriterRequest, ctx: &mut Self::Context) -> Self::Result {
        match msg {
            SnapshotWriterRequest::Record(record) => {
                self.write(ctx, record);
                Ok(SnapshotWriterResponse::None)
            }
            SnapshotWriterRequest::Flush => {
                self.flush(ctx);
                Ok(SnapshotWriterResponse::Path(self.path.clone()))
            }
        }
    }
}

pub struct SnapshotReader {
    file: Box<tokio::fs::File>,
    header: SnapshotHeaderDto,
    message_reader: MessageBufReader,
    is_end: bool,
}

impl SnapshotReader {
    pub async fn init_by_file(mut file: Box<tokio::fs::File>) -> anyhow::Result<Self> {
        let mut message_reader = MessageBufReader::new();
        let mut buf = vec![0u8; 1024];
        file.seek(std::io::SeekFrom::Start(0)).await?;
        let read_len = file.read(&mut buf).await?;
        message_reader.append_next_buf(&buf[..read_len]);
        if let Some(v) = message_reader.next_message_vec() {
            let mut reader = BytesReader::from_bytes(v);
            let header: SnapshotHeader = reader.read_message(v)?;
            Ok(Self {
                file,
                header: header.into(),
                message_reader,
                is_end: false,
            })
        } else {
            Err(anyhow::anyhow!("read snapshot head error"))
        }
    }

    pub async fn init(path: &str) -> anyhow::Result<Self> {
        let mut file = Box::new(OpenOptions::new().read(true).open(path).await?);
        let mut message_reader = MessageBufReader::new();
        let mut buf = vec![0u8; 1024];
        let read_len = file.read(&mut buf).await?;
        message_reader.append_next_buf(&buf[..read_len]);
        if let Some(v) = message_reader.next_message_vec() {
            let mut reader = BytesReader::from_bytes(v);
            let header: SnapshotHeader = reader.read_message(v)?;
            Ok(Self {
                file,
                header: header.into(),
                message_reader,
                is_end: false,
            })
        } else {
            Err(anyhow::anyhow!("read snapshot head error"))
        }
    }

    pub fn get_header(&self) -> &SnapshotHeaderDto {
        &self.header
    }

    pub async fn read_record(&mut self) -> anyhow::Result<Option<SnapshotRecordDto>> {
        if self.is_end {
            return Ok(None);
        }
        loop {
            if let Some(v) = self.message_reader.next_message_vec() {
                let mut reader = BytesReader::from_bytes(v);
                let item: LogSnapshotItem = reader.read_message(v)?;
                let dto = item.into();
                return Ok(Some(dto));
            }
            let mut buf = vec![0u8; 1024];
            let read_len = self.file.read(&mut buf).await?;
            if read_len == 0 {
                self.is_end = true;
                return Ok(None);
            }
            self.message_reader.append_next_buf(&buf[..read_len]);
        }
    }
}

#[bean(inject)]
#[derive(Default)]
pub struct RaftSnapshotManager {
    base_path: Arc<String>,
    snapshots: Vec<SnapshotRange>,
    last_header: Option<SnapshotHeaderDto>,
    building: Option<SnapshotRange>,
    index_manager: Option<Addr<RaftIndexManager>>,
    is_init: bool,
}

impl RaftSnapshotManager {
    pub fn new(base_path: Arc<String>, index_manager: Option<Addr<RaftIndexManager>>) -> Self {
        Self {
            base_path,
            snapshots: Vec::default(),
            last_header: None,
            building: None,
            index_manager,
            is_init: false,
        }
    }

    fn init(&mut self, ctx: &mut Context<Self>) {
        self.load_index_info(ctx);
    }

    fn load_index_info(&mut self, ctx: &mut Context<Self>) {
        //加载索引文件、构建raft日志
        let index_manager = self.index_manager.clone();
        async move {
            if let Some(index_manager) = &index_manager {
                index_manager
                    .send(super::raftindex::RaftIndexRequest::LoadIndexInfo)
                    .await?
            } else {
                Err(anyhow::anyhow!(
                    "load_index_info is error, index_manager is node"
                ))
            }
        }
        .into_actor(self)
        .map(|v, act, ctx| {
            if act.is_init {
                return;
            }
            if let Ok(RaftIndexResponse::RaftIndexInfo {
                raft_index,
                last_applied_log: _last_applied_log,
            }) = v
            {
                act.is_init = true;
                act.snapshots = raft_index.snapshots;
                if let Some(last) = act.snapshots.last() {
                    act.load_snapshot_header(ctx, last.id);
                }
            }
        })
        .wait(ctx);
    }

    fn load_snapshot_header(&mut self, ctx: &mut Context<Self>, snapshot_id: u64) {
        let path = Self::get_snapshot_path(&self.base_path, snapshot_id);
        async move {
            let reader = SnapshotReader::init(&path).await?;
            Ok(reader.header)
        }
        .into_actor(self)
        .map(|v: anyhow::Result<SnapshotHeaderDto>, act, _ctx| {
            if let Ok(v) = v {
                act.last_header = Some(v);
            }
        })
        .wait(ctx);
    }

    fn get_next_id(&mut self) -> anyhow::Result<u64> {
        if self.building.is_some() {
            return Err(anyhow::anyhow!("An snapshots is being packaged"));
        }
        let next_id = if let Some(last) = self.snapshots.last() {
            last.id + 1
        } else {
            1
        };
        Ok(next_id)
    }

    fn get_snapshot_path(base_path: &str, id: u64) -> String {
        Path::new(base_path)
            .join(format!("snapshot_{}", id))
            .to_string_lossy()
            .into_owned()
    }

    fn new_writer(
        &mut self,
        _ctx: &mut Context<Self>,
        header: SnapshotHeaderDto,
        path: Arc<String>,
    ) -> Addr<SnapshotWriterActor> {
        SnapshotWriterActor::new(path, header).start()
    }

    fn complete_snapshot(
        &mut self,
        ctx: &mut Context<Self>,
        snapshot_range: SnapshotRange,
    ) -> anyhow::Result<()> {
        self.building.take();
        //1. 删除历史镜像
        let old_snapshot_len = self.snapshots.len();
        let split_index = if old_snapshot_len > 1 {
            old_snapshot_len - 1
        } else {
            self.snapshots.push(snapshot_range);
            //更新snapshot到index中
            return self.save_snapshot_to_index(ctx);
        };
        for item in &self.snapshots[0..split_index] {
            let path = Self::get_snapshot_path(&self.base_path, item.id);
            std::fs::remove_file(path).ok();
        }

        //2. 更新index中的数据
        let mut new_snapshots = Vec::with_capacity(2);
        if let Some(last_range) = self.snapshots.last() {
            new_snapshots.push(last_range.clone());
        }
        new_snapshots.push(snapshot_range);
        self.snapshots = new_snapshots;
        self.save_snapshot_to_index(ctx)
    }

    fn save_snapshot_to_index(&mut self, ctx: &mut Context<Self>) -> anyhow::Result<()> {
        let index_request = RaftIndexRequest::SaveSnapshots(self.snapshots.clone());
        self.index_manager.as_ref().unwrap().do_send(index_request);
        if let Some(last) = self.snapshots.last() {
            self.load_snapshot_header(ctx, last.id);
        }
        Ok(())
    }

    fn install_snapshot(
        &mut self,
        ctx: &mut Context<Self>,
        snapshot_id: u64,
        end_index: u64,
    ) -> anyhow::Result<()> {
        let snapshot_range = SnapshotRange {
            id: snapshot_id,
            end_index,
        };
        self.complete_snapshot(ctx, snapshot_range)?;
        //更新 member信息到index中
        if let (Some(header), Some(index_manager)) = (&self.last_header, &self.index_manager) {
            let member_after_consensus = if header.member_after_consensus.is_empty() {
                None
            } else {
                Some(header.member_after_consensus.clone())
            };
            let req = RaftIndexRequest::SaveMember {
                member: header.member.clone(),
                member_after_consensus,
                node_addr: Some(header.node_addrs.clone()),
            };
            index_manager.do_send(req);
        }
        Ok(())
    }
}

impl Actor for RaftSnapshotManager {
    type Context = Context<Self>;

    fn started(&mut self, ctx: &mut Self::Context) {
        if self.index_manager.is_some() {
            self.init(ctx);
        }
        log::info!("RaftSnapshotActor started");
    }
}

impl Inject for RaftSnapshotManager {
    type Context = Context<Self>;

    fn inject(
        &mut self,
        factory_data: bean_factory::FactoryData,
        _factory: bean_factory::BeanFactory,
        ctx: &mut Self::Context,
    ) {
        if self.index_manager.is_none() {
            self.index_manager = factory_data.get_actor();
            self.init(ctx);
        }
    }
}

#[derive(Message, Debug)]
#[rtype(result = "anyhow::Result<RaftSnapshotResponse>")]
pub enum RaftSnapshotRequest {
    GetLastSnapshot,
    NewSnapshot(SnapshotHeaderDto),
    NewSnapshotForLoad,
    CompleteSnapshot(SnapshotRange),
    InstallSnapshot { end_index: u64, snapshot_id: u64 },
}

pub enum RaftSnapshotResponse {
    LastSnapshot(Option<String>, Option<SnapshotHeaderDto>),
    NewSnapshot(Addr<SnapshotWriterActor>, u64, Arc<String>),
    NewSnapshotForLoad(String, u64),
    None,
}

impl Handler<RaftSnapshotRequest> for RaftSnapshotManager {
    type Result = anyhow::Result<RaftSnapshotResponse>;

    fn handle(&mut self, msg: RaftSnapshotRequest, ctx: &mut Self::Context) -> Self::Result {
        match msg {
            RaftSnapshotRequest::GetLastSnapshot => {
                let path = self
                    .snapshots
                    .last()
                    .map(|e| Self::get_snapshot_path(&self.base_path, e.id));
                Ok(RaftSnapshotResponse::LastSnapshot(
                    path,
                    self.last_header.clone(),
                ))
            }
            RaftSnapshotRequest::NewSnapshot(header) => {
                let next_id = self.get_next_id()?;
                let path = Arc::new(Self::get_snapshot_path(&self.base_path, next_id));
                let writer = self.new_writer(ctx, header, path.clone());
                Ok(RaftSnapshotResponse::NewSnapshot(writer, next_id, path))
            }
            RaftSnapshotRequest::NewSnapshotForLoad => {
                let next_id = self.get_next_id()?;
                let path = Self::get_snapshot_path(&self.base_path, next_id);
                Ok(RaftSnapshotResponse::NewSnapshotForLoad(path, next_id))
            }
            RaftSnapshotRequest::CompleteSnapshot(snapshot_range) => {
                self.complete_snapshot(ctx, snapshot_range).ok();
                Ok(RaftSnapshotResponse::None)
            }
            RaftSnapshotRequest::InstallSnapshot {
                end_index,
                snapshot_id,
            } => {
                self.install_snapshot(ctx, snapshot_id, end_index).ok();
                Ok(RaftSnapshotResponse::None)
            }
        }
    }
}