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
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
use crate::entity::*;
use crate::error::*;
use crate::raft::Raft;
use async_std::sync::RwLock;
use log::{error, info, warn};
use std::fs;
use std::io;
use std::io::prelude::*;
use std::path::{Path, PathBuf};
use std::sync::Arc;

static FILE_START: &str = "raft_";
static FILE_END: &str = ".log";
static BUF_SIZE: usize = 1024 * 1024;

pub struct RaftLog {
    id: u64,
    conf: Arc<Config>,
    pub log_mem: RwLock<LogMem>,
    log_file: RwLock<LogFile>,
}

pub struct RaftLogIter {
    ids: Option<Vec<u64>>,
    dir: Option<PathBuf>,
    file_index: usize,
    file: Option<fs::File>,
    file_offset: u64,
    current_index: u64,
}

impl RaftLogIter {
    fn file_len(&self) -> u64 {
        self.file.as_ref().unwrap().metadata().unwrap().len()
    }

    pub async fn next(&mut self, raft_log: &RaftLog) -> RaftResult<Option<Vec<u8>>> {
        if self.file.is_some() {
            self.iter_file(raft_log).await
        } else {
            self.iter_mem(raft_log).await
        }
    }

    async fn iter_mem(&mut self, raft_log: &RaftLog) -> RaftResult<Option<Vec<u8>>> {
        let mem = raft_log.log_mem.read().await;
        if self.current_index <= mem.offset {
            return Err(RaftError::OutMemIndex(self.current_index));
        }

        if self.current_index > raft_log.log_mem.read().await.applied {
            return Ok(None);
        }

        let v = mem.get(self.current_index)?.encode();
        self.current_index += 1;
        return Ok(Some(v));
    }

    async fn iter_file(&mut self, raft_log: &RaftLog) -> RaftResult<Option<Vec<u8>>> {
        if self.file_len() - self.file_offset <= 4 {
            if self.current_index > raft_log.log_mem.read().await.offset {
                self.file = None;
                return self.iter_mem(raft_log).await;
            }
            return Err(RaftError::LogFileInvalid(self.current_index));
        }
        let dl = read_u32(self.file.as_mut().unwrap())? as u64;
        self.file_offset += 4;

        if self.file_offset + dl > self.file_len() {
            if self.current_index > raft_log.log_mem.read().await.offset {
                self.file = None;
                return self.iter_mem(raft_log).await;
            }
            let index = self.file_index;
            return Err(RaftError::LogFileInvalid(self.ids()[index]));
        }
        let mut buf = vec![0; dl as usize];
        convert(self.file().read_exact(&mut buf))?;
        self.current_index += 1;
        self.file_offset += dl;

        if self.file_offset == self.file_len() {
            self.file_index += 1;
            if self.file_index >= self.ids().len() {
                match fs::OpenOptions::new().read(true).open(
                    self.dir
                        .as_ref()
                        .unwrap()
                        .clone()
                        .join(format!("{}{}{}", FILE_START, self.current_index, FILE_END)),
                ) {
                    Ok(f) => {
                        self.file = Some(f);
                        self.file_offset = 0;
                    }
                    Err(_) => {
                        self.file = {
                            info!("not found next index:{} so use memory", self.current_index);
                            None
                        }
                    }
                };
            } else {
                let file = convert(fs::OpenOptions::new().read(true).open(
                    self.dir.as_ref().unwrap().clone().join(format!(
                        "{}{}{}",
                        FILE_START,
                        self.ids.as_ref().unwrap()[self.file_index],
                        FILE_END
                    )),
                ))?;
                self.file = Some(file);
                self.file_offset = 0;
            }
        }

        return Ok(Some(buf));
    }

    fn file(&mut self) -> &mut fs::File {
        self.file.as_mut().unwrap()
    }

    fn ids(&mut self) -> &Vec<u64> {
        self.ids.as_ref().unwrap()
    }
}

impl RaftLog {
    //give a dir to found file index and max index id for Log file
    //file id start from 1
    pub fn new(id: u64, conf: Arc<Config>) -> RaftResult<Self> {
        let dir = Path::new(&conf.log_path).join(format!("{}", id));
        if !dir.exists() {
            convert(fs::create_dir_all(&dir))?;
        }

        let file_ids = RaftLog::file_ids(&dir)?;

        let log_file: LogFile;
        let log_mem: LogMem;

        if file_ids.len() == 0 {
            //mean's new file
            log_file = LogFile::new(dir.clone(), 0, vec![1])?;
            log_mem = LogMem::new(conf.log_max_num, 0, 0);
        } else {
            let last_index = file_ids.len() - 1;

            let (offset, entry) =
                LogFile::read_last_entry(dir.clone(), file_ids[last_index], false)?;
            let (term, index) = entry.info();
            if index > 0 {
                log_file = LogFile::new(dir.clone(), offset, file_ids)?;
                log_mem = LogMem::new(conf.log_max_num, term, index);
            } else if file_ids[last_index] == 1 {
                log_file = LogFile::new(dir.clone(), 0, file_ids)?;
                log_mem = LogMem::new(conf.log_max_num, term, index);
            } else {
                warn!("first log file is invalidate so use the 2th log file");

                let (_, entry) =
                    LogFile::read_last_entry(dir.clone(), file_ids[last_index - 2], true)?;
                let (term, index) = entry.info();
                log_file = LogFile::new(dir.clone(), 0, file_ids)?;
                log_mem = LogMem::new(conf.log_max_num, term, index);
            }
        }

        Ok(RaftLog {
            id: id,
            conf: conf,
            log_mem: RwLock::new(log_mem),
            log_file: RwLock::new(log_file),
        })
    }

    /// search all filed start id , and sorted
    fn file_ids(dir: &PathBuf) -> RaftResult<Vec<u64>> {
        let mut file_ids = vec![];
        for file in convert(fs::read_dir(&dir))? {
            let meta = file.as_ref().unwrap().metadata().unwrap();
            if !meta.is_file() {
                continue;
            }

            if let Some(name) = file.as_ref().unwrap().file_name().to_str() {
                if name.starts_with(FILE_START) && name.ends_with(FILE_END) {
                    file_ids.push(name[5..name.len() - 4].parse::<u64>().unwrap());
                }
            };
        }

        file_ids.sort_by(|a, b| a.cmp(b));
        Ok(file_ids)
    }

    pub async fn validate_log_file(&self, mut start_index: u64) -> RaftResult<()> {
        if start_index == 0 {
            start_index = 1;
        }
        let mut iter = self.iter(start_index).await?;
        let mut pre_index = start_index - 1;
        let mut pre_term = 0;
        while let Some(buf) = iter.next(&self).await? {
            let e = Entry::decode(&buf)?;
            pre_index += 1;
            let (pt, term, index) = e.commit_info();
            if pre_index != index {
                return Err(RaftError::Error(format!(
                    "id is not continuous pre:{} now:{}",
                    pre_index, index
                )));
            }

            if pre_term != 0 && pre_term != pt {
                return Err(RaftError::Error(format!(
                    "term continuous pre:{} now:{}",
                    pre_term, pt
                )));
            }
            pre_term = term;
        }
        Ok(())
    }

    //it to instance a intertor , use it first lock_truncation, my be will panic if not lock lock_truncation;
    pub async fn iter(&self, index: u64) -> RaftResult<RaftLogIter> {
        if self.log_mem.read().await.offset >= index {
            let ids = self.log_file.read().await.file_ids.clone();
            let dir = self.log_file.read().await.dir.clone();
            let start_index = match ids.binary_search(&index) {
                Ok(i) => i,
                Err(i) => {
                    if i == 0 {
                        return Err(RaftError::LogFileNotFound(index));
                    } else {
                        i - 1
                    }
                }
            };
            let id = ids[start_index];
            let mut file = convert(
                fs::OpenOptions::new()
                    .read(true)
                    .open(dir.join(format!("{}{}{}", FILE_START, id, FILE_END))),
            )?;
            let file_len = file.metadata().unwrap().len();
            let mut offset = 0;
            if id == index {
                return Ok(RaftLogIter {
                    ids: Some(ids),
                    dir: Some(dir),
                    file_index: start_index,
                    file: Some(file),
                    file_offset: offset,
                    current_index: index,
                });
            }

            for _i in 0..index - id {
                let dl = read_u32(&mut file)? as u64;
                offset += 4;
                if offset + dl > file_len {
                    return Err(RaftError::LogFileInvalid(id));
                }
                convert(file.seek(io::SeekFrom::Current(dl as i64)))?;
                offset += dl;
                if offset + 4 > file_len {
                    return Err(RaftError::LogFileInvalid(id));
                }
            }

            return Ok(RaftLogIter {
                ids: Some(ids),
                dir: Some(dir),
                file_index: start_index,
                file: Some(file),
                file_offset: offset,
                current_index: index,
            });
        }

        Ok(RaftLogIter {
            ids: None,
            dir: None,
            file_index: 0,
            file: None,
            file_offset: 0,
            current_index: index,
        })
    }

    pub async fn info(&self) -> (u64, u64, u64) {
        let mem = self.log_mem.read().await;
        (mem.term, mem.committed, mem.applied)
    }

    pub async fn last_index(&self) -> u64 {
        self.log_mem.read().await.committed
    }

    pub async fn last_applied(&self) -> u64 {
        self.log_mem.read().await.applied
    }

    pub async fn last_term(&self) -> u64 {
        self.log_mem.read().await.term
    }

    pub fn load_start_index_or_def(&self, start_index: u64) -> u64 {
        let path = Path::new(&self.conf.log_path)
            .join(format!("{}", self.id))
            .join("start_index");
        if let Ok(v) = fs::read(&path) {
            if v.len() == 8 {
                let mut dst = [0u8; 8];
                dst.clone_from_slice(&v);
                return u64::from_be_bytes(dst);
            }
        }
        fs::write(&path, u64::to_be_bytes(start_index)).expect("write start index has err");
        start_index
    }

    // pre_term: u64,
    // term: u64,
    // mut index: u64,
    // cmd: Vec<u8>,
    //this method to store entry to mem  by vec .
    //if vec length gather conf max log num  , it will truncation to min log num , but less than apllied index
    pub async fn commit(&self, mut entry: Entry) -> RaftResult<u64> {
        let mut mem = self.log_mem.write().await;
        let (pre_term, term, mut new_index) = entry.commit_info();

        if mem.term > term {
            return Err(RaftError::TermLess);
        }

        if new_index == 0 {
            new_index = mem.committed + 1;
            match entry {
                Entry::Commit { ref mut index, .. }
                | Entry::MemberChange { ref mut index, .. }
                | Entry::LeaderChange { ref mut index, .. } => {
                    *index = new_index;
                }
                _ => panic!("not support this type:{:?}", entry),
            }
        } else if new_index > mem.committed + 1 {
            return Err(RaftError::IndexLess(mem.committed, new_index));
        } else if new_index < mem.committed + 1 {
            if new_index == mem.committed && term == mem.term {
                return Ok(new_index);
            }
            //Indicates that log conflicts need to be rolled back
            if new_index < mem.offset {
                return Err(RaftError::IndexLess(mem.offset, new_index));
            }

            let new_len = (new_index - mem.offset) as usize;
            unsafe { mem.logs.set_len(new_len) };
        }
        if mem.term != pre_term {
            //if not same pre term , means last entry is invalided . rolled bak
            let new_len = mem.committed as usize - 1;
            unsafe { mem.logs.set_len(new_len) };
            let (term, index) = mem.get_uncheck(mem.committed - 1).info();
            mem.committed = index;
            mem.term = term;
            return Err(RaftError::IndexLess(index, new_index));
        }
        mem.logs.push(entry);

        mem.committed = new_index;
        mem.term = term;

        if mem.logs.len() >= self.conf.log_max_num {
            let keep_num = usize::max(self.conf.log_min_num, (new_index - mem.applied) as usize);
            if mem.logs.len() - keep_num > 10 {
                if let Err(e) = self.log_file.write().await.writer.flush() {
                    error!("flush buffer has err:{:?}", e);
                }
                let off = mem.logs.len() - keep_num;
                mem.logs = mem.logs.split_off(off as usize);
                mem.offset = mem.offset + off as u64;
            }
        }
        Ok(new_index)
    }

    pub async fn rollback(&self) {
        let mut mem = self.log_mem.write().await;
        mem.committed = mem.pre_committed;
        mem.term = mem.pre_term;
        let last = mem.logs.len() - 1;
        mem.logs.remove(last);
    }

    //if this function has err ,Means that raft may not work anymore
    // If an IO error, such as insufficient disk space, the data will be unclean. Or an unexpected error occurred
    pub async fn save_to_log(&self, target_applied: u64, raft: &Arc<Raft>) -> RaftResult<()> {
        let index = {
            let mem = self.log_mem.read().await;
            if mem.applied >= target_applied || mem.committed < target_applied {
                return Ok(());
            }
            let entry = mem.get_uncheck(mem.applied + 1);
            let bs = entry.encode();
            let (_, index) = entry.info();
            let mut file = self.log_file.write().await;
            if let Err(err) = file.writer.write(&u32::to_be_bytes(bs.len() as u32)) {
                return Err(RaftError::IOError(err.to_string()));
            }
            if let Err(err) = file.writer.write(&bs) {
                return Err(RaftError::IOError(err.to_string()));
            }

            file.offset = file.offset + bs.len() as u64;
            if file.offset >= self.conf.log_file_size_mb * 1024 * 1024 {
                file.log_rolling(index + 1)?;
            }

            if let Err(e) = raft.apply(entry).await {
                error!("apply index:{} has err:{}", index, e);
            }

            index
        };

        self.log_mem.write().await.applied = index;
        Ok(())
    }
}
// term , committed stands last entry info.
// applied stands last store file log.
pub struct LogMem {
    pub offset: u64,
    term: u64,
    pre_term: u64,
    committed: u64,
    pre_committed: u64,
    applied: u64,
    logs: Vec<Entry>,
}

impl LogMem {
    fn new(capacity: usize, term: u64, index: u64) -> LogMem {
        return LogMem {
            logs: Vec::with_capacity(capacity),
            offset: index,
            term: term,
            committed: index,
            pre_term: term,
            pre_committed: index,
            applied: index,
        };
    }

    // if use this function make sure, the log index is exists, if not it will panic.
    pub fn get_uncheck(&self, index: u64) -> &Entry {
        return self.logs.get((index - self.offset - 1) as usize).unwrap();
    }

    pub fn get(&self, index: u64) -> RaftResult<&Entry> {
        if index <= self.offset {
            return Err(RaftError::OutMemIndex(index));
        }
        match self.logs.get((index - self.offset - 1) as usize) {
            Some(e) => Ok(e),
            None => Err(RaftError::OutMemIndex(index)),
        }
    }
}

struct LogFile {
    offset: u64,
    dir: PathBuf,
    file_ids: Vec<u64>,
    writer: io::BufWriter<fs::File>,
}

impl LogFile {
    //new logfile by ids, the ids is file list
    fn new(dir: PathBuf, offset: u64, ids: Vec<u64>) -> RaftResult<LogFile> {
        let file_path = dir.join(format!("{}{}{}", FILE_START, ids[ids.len() - 1], FILE_END));
        let mut writer = io::BufWriter::with_capacity(
            BUF_SIZE,
            convert(
                fs::OpenOptions::new()
                    .create(true)
                    .append(true)
                    .read(true)
                    .write(true)
                    .open(file_path),
            )?,
        );

        if offset > 0 {
            convert(writer.seek(io::SeekFrom::Start(offset)))?;
        }

        Ok(LogFile {
            offset: offset,
            dir: dir,
            file_ids: ids,
            writer: writer,
        })
    }

    // read last entry by file , it return value Option<entry start offeset , entry>
    // if read dir is empty , it allways return zero entry
    // if file_id is not exists , it return LogFileNotFound err value is file_id
    //validate means the file complete, the last log file may be not complete,  but the penultimate must complete
    fn read_last_entry(dir: PathBuf, file_id: u64, validate: bool) -> RaftResult<(u64, Entry)> {
        let file_path = dir.join(format!("{}{}{}", FILE_START, file_id, FILE_END));
        if !file_path.exists() {
            if file_id == 0 {
                return Ok((
                    0,
                    Entry::Commit {
                        pre_term: 0,
                        term: 0,
                        index: 0,
                        commond: Vec::default(),
                    },
                ));
            } else {
                return Err(RaftError::LogFileNotFound(file_id));
            }
        }
        let mut file = convert(fs::OpenOptions::new().read(true).open(&file_path))?;

        let mut offset: u64 = 0;
        let mut len = file.metadata().unwrap().len();
        let mut pre_offset: u64 = 0;

        loop {
            if len == 0 {
                return Ok((
                    0,
                    Entry::Commit {
                        pre_term: 0,
                        term: 0,
                        index: 0,
                        commond: Vec::default(),
                    },
                ));
            }

            if len < offset || len - offset <= 4 {
                len = offset - 4;
                offset = pre_offset;
                convert(file.seek(io::SeekFrom::Start(offset)))?;
                // if file has  dirty data, change to valid length
                convert(
                    fs::OpenOptions::new()
                        .write(true)
                        .open(&file_path)
                        .unwrap()
                        .set_len(len),
                )?;
                continue;
            }

            let dl = read_u32(&mut file)? as u64;
            offset += 4;

            if len == offset + dl {
                let mut buf = vec![0; dl as usize];
                file.read_exact(&mut buf).unwrap();
                return Ok((offset, Entry::decode(&buf)?));
            } else if len < offset + dl {
                if validate {
                    return Err(RaftError::LogFileInvalid(file_id));
                }
                len = offset - 4;
                offset = pre_offset;
                convert(file.seek(io::SeekFrom::Start(offset)))?;
                // if file has  dirty data, change to valid length
                convert(
                    fs::OpenOptions::new()
                        .write(true)
                        .open(&file_path)
                        .unwrap()
                        .set_len(len),
                )?;
            } else {
                convert(file.seek(io::SeekFrom::Current(dl as i64)))?;
                pre_offset = offset - 4;
                offset += dl;
            }
        }
    }

    fn log_rolling(&mut self, new_id: u64) -> RaftResult<()> {
        convert(self.writer.flush())?;

        let file = convert(
            fs::OpenOptions::new()
                .create(true)
                .read(true)
                .write(true)
                .open(
                    self.dir
                        .join(format!("{}{}{}", FILE_START, new_id, FILE_END)),
                ),
        )?;
        self.writer = io::BufWriter::with_capacity(BUF_SIZE, file);
        self.file_ids.push(new_id);
        self.offset = 0;

        Ok(())
    }
}

fn read_u32(file: &mut fs::File) -> RaftResult<u32> {
    let mut output = [0u8; 4];
    convert(file.read_exact(&mut output[..]))?;
    Ok(u32::from_be_bytes(output))
}

#[test]
fn test_validate_log_file() {
    let raft_id = 1;
    let file_id = 1;
    let path = Path::new("example/data/raft1")
        .join(format!("{}", raft_id))
        .join(format!("{}{}{}", FILE_START, file_id, FILE_END));
}