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
//! ノードローカルなログ関連の構成要素群.
use std::ops::{Add, AddAssign, Sub, SubAssign};

pub use self::history::{HistoryRecord, LogHistory};

use cluster::ClusterConfig;
use election::Term;
use {ErrorKind, Result};

mod history;

/// ローカルログ.
#[derive(Debug)]
pub enum Log {
    /// ログの前半部分 (i.e., スナップショット).
    Prefix(LogPrefix),

    /// ログの後半部分>
    Suffix(LogSuffix),
}
impl From<LogPrefix> for Log {
    fn from(f: LogPrefix) -> Self {
        Log::Prefix(f)
    }
}
impl From<LogSuffix> for Log {
    fn from(f: LogSuffix) -> Self {
        Log::Suffix(f)
    }
}

/// ログの前半部分 (i.e., スナップショット).
#[derive(Debug, Clone)]
pub struct LogPrefix {
    /// 前半部分の終端位置.
    ///
    /// "終端位置" = "前半部分に含まれない最初の位置".
    pub tail: LogPosition,

    /// 前半部分に含まれる中で、最新の構成情報.
    pub config: ClusterConfig,

    /// 前半部分に含まれるコマンド群の適用後の状態機械のスナップショット.
    pub snapshot: Vec<u8>,
}

/// ログの後半部分.
///
/// 厳密には、常に"後半部分"、つまり「ある地点より後ろの全てのエントリ」を
/// 含んでいる訳ではない.
///
/// ただし、このデータ構造自体は、常に追記的なアクセスのために利用され、
/// "ログの途中の一部だけを更新する"といった操作は発生しないので、
/// "常にログの末尾に対して適用される"的な意味合いで`Suffix`と付けている.
#[derive(Debug, Clone)]
pub struct LogSuffix {
    /// ログの開始位置.
    ///
    /// `entries`のサイズが1以上の場合には、
    /// その最初のエントリの位置となる.
    pub head: LogPosition,

    /// 後半部分に属するエントリ群.
    pub entries: Vec<LogEntry>,
}
impl LogSuffix {
    /// ログの終端位置を返す.
    ///
    /// "終端位置" = "entriesに含まれない最初のエントリの位置".
    ///
    /// `entries`の最後の要素が、ログ全体の最後の要素と一致している場合には、
    /// "終端位置"は「次にログに追加される位置(= ログの末端)」となる.
    pub fn tail(&self) -> LogPosition {
        let prev_term = self
            .entries
            .last()
            .map_or(self.head.prev_term, LogEntry::term);
        let index = self.head.index + self.entries.len();
        LogPosition { prev_term, index }
    }

    /// 後半部分に含まれるエントリの位置を走査するためのイテレータを返す.
    pub fn positions(&self) -> LogPositions {
        LogPositions {
            suffix: self,
            offset: 0,
        }
    }

    /// `new_head`のまでスキップする.
    ///
    /// 現在の先頭から`new_head`までのエントリは破棄され、`new_head`が新しい先頭になる.
    ///
    /// # Errors
    ///
    /// 以下のいずれかの場合には`ErrorKind::InvalidInput`が返される:
    ///
    /// - `new_head < self.head.index`
    /// - `self.tail().index < new_head`
    pub fn skip_to(&mut self, new_head: LogIndex) -> Result<()> {
        track_assert!(self.head.index <= new_head, ErrorKind::InvalidInput);
        track_assert!(new_head <= self.tail().index, ErrorKind::InvalidInput);
        let count = new_head - self.head.index;
        if count == 0 {
            return Ok(());
        }
        let prev_term = self
            .entries
            .drain(0..count)
            .last()
            .expect("Never fails")
            .term();
        self.head.prev_term = prev_term;
        self.head.index += count;
        Ok(())
    }

    /// 終端を`new_tail`の位置まで切り詰める.
    ///
    /// # Errors
    ///
    /// `new_tail`が`LogSuffix`が保持する範囲の外の場合には、
    /// `ErrorKind::InvalidInput`を理由としたエラーが返される.
    pub fn truncate(&mut self, new_tail: LogIndex) -> Result<()> {
        track_assert!(self.head.index <= new_tail, ErrorKind::InvalidInput);
        track_assert!(new_tail <= self.tail().index, ErrorKind::InvalidInput);
        let delta = self.tail().index - new_tail;
        let new_len = self.entries.len() - delta;
        self.entries.truncate(new_len);
        Ok(())
    }

    /// 指定された範囲のログ領域を切り出して返す.
    ///
    /// # Errors
    ///
    /// `self`が指定範囲を包含していない場合には、
    /// `ErrorKind::InvalidInput`を理由としてエラーが返される.
    pub fn slice(&self, start: LogIndex, end: LogIndex) -> Result<Self> {
        track_assert!(self.head.index <= start, ErrorKind::InvalidInput);
        track_assert!(start <= end, ErrorKind::InvalidInput);
        track_assert!(end <= self.tail().index, ErrorKind::InvalidInput);
        let slice_start = start - self.head.index;
        let slice_end = end - self.head.index;
        let slice_head = if start == self.head.index {
            self.head
        } else {
            let prev_term = self.entries[slice_start - 1].term();
            LogPosition {
                prev_term,
                index: start,
            }
        };
        let slice_entries = self.entries[slice_start..slice_end].into();
        Ok(LogSuffix {
            head: slice_head,
            entries: slice_entries,
        })
    }
}
impl Default for LogSuffix {
    fn default() -> Self {
        LogSuffix {
            head: LogPosition::default(),
            entries: Vec::new(),
        }
    }
}

/// `LogSuffix`に含まれるログの位置を走査するための`Iterator`実装.
#[derive(Debug)]
pub struct LogPositions<'a> {
    suffix: &'a LogSuffix,
    offset: usize,
}
impl<'a> Iterator for LogPositions<'a> {
    type Item = LogPosition;
    fn next(&mut self) -> Option<Self::Item> {
        if self.suffix.entries.len() < self.offset {
            None
        } else {
            let id = if self.offset == 0 {
                self.suffix.head
            } else {
                let i = self.offset - 1;
                let prev_term = self.suffix.entries[i].term();
                let index = self.suffix.head.index + self.offset;
                LogPosition { prev_term, index }
            };
            self.offset += 1;
            Some(id)
        }
    }
}

/// ログに格納されるエントリ.
#[derive(Debug, Clone, PartialEq, Eq)]
#[allow(missing_docs)]
pub enum LogEntry {
    /// 特に内容を持たないエントリ.
    ///
    /// リーダ選出時には、最初にこのエントリがログに追加され、
    /// `Term`が変わったことを記録する.
    Noop { term: Term },

    /// クラスタ構成の変更を共有するためのエントリ.
    Config { term: Term, config: ClusterConfig },

    /// 状態機械の入力となるコマンドを格納したエントリ.
    Command { term: Term, command: Vec<u8> },
}
impl LogEntry {
    /// このエントリが発行された`Term`を返す.
    pub fn term(&self) -> Term {
        match *self {
            LogEntry::Noop { term } => term,
            LogEntry::Config { term, .. } => term,
            LogEntry::Command { term, .. } => term,
        }
    }
}

/// 提案ID.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct ProposalId {
    /// 提案が発行された時の`Term`.
    pub term: Term,

    /// 提案を保持するエントリのログ内でのインデックス.
    pub index: LogIndex,
}

/// ログの特定位置を識別するためのデータ構造.
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Hash)]
pub struct LogPosition {
    /// 一つ前のインデックスのエントリの`Term`.
    pub prev_term: Term,

    /// この位置のインデックス.
    pub index: LogIndex,
}
impl LogPosition {
    /// `self`がログ上で、`other`と等しい、あるいは、より後方に位置している場合に`true`が返る.
    ///
    /// なお`self`と`other`が、それぞれ分岐したログ上に位置しており、
    /// 前後関係が判断できない場合には`false`が返される.
    ///
    /// # Examples
    ///
    /// ```
    /// use raftlog::log::LogPosition;
    ///
    /// // `a`の方がインデックスが大きい
    /// let a = LogPosition { prev_term: 10.into(), index: 5.into() };
    /// let b = LogPosition { prev_term: 10.into(), index: 3.into() };
    /// assert!(a.is_newer_or_equal_than(b));
    /// assert!(!b.is_newer_or_equal_than(a));
    ///
    /// // `a`の方が`Term`が大きい
    /// let a = LogPosition { prev_term: 20.into(), index: 3.into() };
    /// let b = LogPosition { prev_term: 10.into(), index: 3.into() };
    /// assert!(a.is_newer_or_equal_than(b));
    /// assert!(!b.is_newer_or_equal_than(a));
    ///
    /// // `a`の方がインデックスは大きいが、`b`の方が`Term`は大きい
    /// // => 順序が確定できない
    /// let a = LogPosition { prev_term: 5.into(), index: 10.into() };
    /// let b = LogPosition { prev_term: 10.into(), index: 3.into() };
    /// assert!(!a.is_newer_or_equal_than(b));
    /// assert!(!b.is_newer_or_equal_than(a));
    /// ```
    pub fn is_newer_or_equal_than(&self, other: LogPosition) -> bool {
        self.prev_term >= other.prev_term && self.index >= other.index
    }
}

/// あるログエントリのインデックス.
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct LogIndex(u64);
impl LogIndex {
    /// 新しい`LogIndex`インスタンスを生成する.
    pub fn new(index: u64) -> Self {
        LogIndex(index)
    }

    /// インデックスの値を返す.
    pub fn as_u64(self) -> u64 {
        self.0
    }
}
impl From<u64> for LogIndex {
    fn from(f: u64) -> Self {
        LogIndex::new(f)
    }
}
impl Add<usize> for LogIndex {
    type Output = Self;
    fn add(self, rhs: usize) -> Self::Output {
        LogIndex(self.0 + rhs as u64)
    }
}
impl AddAssign<usize> for LogIndex {
    fn add_assign(&mut self, rhs: usize) {
        self.0 += rhs as u64;
    }
}
impl Sub for LogIndex {
    type Output = usize;
    fn sub(self, rhs: Self) -> Self::Output {
        (self.0 - rhs.0) as usize
    }
}
impl Sub<usize> for LogIndex {
    type Output = Self;
    fn sub(self, rhs: usize) -> Self::Output {
        LogIndex(self.0 - rhs as u64)
    }
}
impl SubAssign<usize> for LogIndex {
    fn sub_assign(&mut self, rhs: usize) {
        self.0 -= rhs as u64;
    }
}

#[cfg(test)]
mod test {
    use super::*;

    fn id(prev_term: u64, index: u64) -> LogPosition {
        LogPosition {
            prev_term: prev_term.into(),
            index: index.into(),
        }
    }
    fn noop(term: u64) -> LogEntry {
        LogEntry::Noop { term: term.into() }
    }

    #[test]
    fn log_suffix_end() {
        let suffix = LogSuffix::default();
        assert_eq!(suffix.tail().index.as_u64(), 0);

        let suffix = LogSuffix {
            head: LogPosition::default(),
            entries: vec![noop(0), noop(1)],
        };
        assert_eq!(suffix.tail().index.as_u64(), 2);
    }
    #[test]
    fn log_suffix_positions() {
        let suffix = LogSuffix::default();
        assert_eq!(suffix.positions().collect::<Vec<_>>(), [id(0, 0)]);

        let suffix = LogSuffix {
            head: LogPosition {
                prev_term: 0.into(),
                index: 30.into(),
            },
            entries: vec![noop(0), noop(2), noop(2)],
        };
        assert_eq!(
            suffix.positions().collect::<Vec<_>>(),
            [id(0, 30), id(0, 31), id(2, 32), id(2, 33)]
        );
    }
    #[test]
    fn log_suffix_skip_to() {
        let mut suffix = LogSuffix {
            head: LogPosition {
                prev_term: 0.into(),
                index: 30.into(),
            },
            entries: vec![noop(0), noop(2), noop(2)],
        };
        assert_eq!(
            suffix.positions().collect::<Vec<_>>(),
            [id(0, 30), id(0, 31), id(2, 32), id(2, 33)]
        );
        assert_eq!(suffix.entries.len(), 3);

        suffix.skip_to(31.into()).unwrap();
        assert_eq!(
            suffix.positions().collect::<Vec<_>>(),
            [id(0, 31), id(2, 32), id(2, 33)]
        );
        assert_eq!(suffix.entries.len(), 2);

        suffix.skip_to(33.into()).unwrap();
        assert_eq!(suffix.positions().collect::<Vec<_>>(), [id(2, 33)]);
        assert_eq!(suffix.entries.len(), 0);

        suffix.skip_to(33.into()).unwrap();
        assert_eq!(suffix.positions().collect::<Vec<_>>(), [id(2, 33)]);
        assert_eq!(suffix.entries.len(), 0);
    }
    #[test]
    fn log_suffix_truncate() {
        let mut suffix = LogSuffix {
            head: LogPosition {
                prev_term: 0.into(),
                index: 30.into(),
            },
            entries: vec![noop(0), noop(2), noop(2)],
        };
        assert_eq!(
            suffix.positions().collect::<Vec<_>>(),
            [id(0, 30), id(0, 31), id(2, 32), id(2, 33)]
        );
        assert_eq!(suffix.entries.len(), 3);

        suffix.truncate(31.into()).unwrap();
        assert_eq!(
            suffix.positions().collect::<Vec<_>>(),
            [id(0, 30), id(0, 31)]
        );
        assert_eq!(suffix.entries.len(), 1);
    }
    #[test]
    fn log_suffix_slice() {
        let suffix = LogSuffix {
            head: LogPosition {
                prev_term: 0.into(),
                index: 30.into(),
            },
            entries: vec![noop(0), noop(2), noop(2)],
        };
        assert_eq!(
            suffix.positions().collect::<Vec<_>>(),
            [id(0, 30), id(0, 31), id(2, 32), id(2, 33)]
        );
        assert_eq!(suffix.entries.len(), 3);

        let slice = suffix.slice(31.into(), 33.into()).unwrap();
        assert_eq!(
            slice.positions().collect::<Vec<_>>(),
            [id(0, 31), id(2, 32), id(2, 33)]
        );
        assert_eq!(slice.entries.len(), 2);
    }
}