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
use std::marker::PhantomData;
use engine_traits::{KvEngine, Snapshot, CF_RAFT};
use kvproto::metapb::Region;
use crate::coprocessor::{ConsistencyCheckMethod, Coprocessor};
use crate::Result;
pub trait ConsistencyCheckObserver<E: KvEngine>: Coprocessor {
fn update_context(&self, context: &mut Vec<u8>) -> bool;
fn compute_hash(
&self,
region: &Region,
context: &mut &[u8],
snap: &E::Snapshot,
) -> Result<Option<u32>>;
}
#[derive(Clone)]
pub struct Raw<E: KvEngine>(PhantomData<E>);
impl<E: KvEngine> Coprocessor for Raw<E> {}
impl<E: KvEngine> Default for Raw<E> {
fn default() -> Raw<E> {
Raw(Default::default())
}
}
impl<E: KvEngine> ConsistencyCheckObserver<E> for Raw<E> {
fn update_context(&self, context: &mut Vec<u8>) -> bool {
context.push(ConsistencyCheckMethod::Raw as u8);
true
}
fn compute_hash(
&self,
region: &kvproto::metapb::Region,
context: &mut &[u8],
snap: &E::Snapshot,
) -> Result<Option<u32>> {
if context.is_empty() {
return Ok(None);
}
assert_eq!(context[0], ConsistencyCheckMethod::Raw as u8);
*context = &context[1..];
compute_hash_on_raw(region, snap).map(Some)
}
}
fn compute_hash_on_raw<S: Snapshot>(region: &Region, snap: &S) -> Result<u32> {
let region_id = region.get_id();
let mut digest = crc32fast::Hasher::new();
let mut cf_names = snap.cf_names();
cf_names.sort_unstable();
let start_key = keys::enc_start_key(region);
let end_key = keys::enc_end_key(region);
for cf in cf_names {
snap.scan_cf(cf, &start_key, &end_key, false, |k, v| {
digest.update(k);
digest.update(v);
Ok(true)
})?;
}
let region_state_key = keys::region_state_key(region_id);
digest.update(®ion_state_key);
match snap.get_value_cf(CF_RAFT, ®ion_state_key) {
Err(e) => return Err(e.into()),
Ok(Some(v)) => digest.update(&v),
Ok(None) => {}
}
Ok(digest.finalize())
}
#[cfg(test)]
mod tests {
use super::*;
use engine_test::kv::KvTestEngine;
#[test]
fn test_update_context() {
let mut context = Vec::new();
let observer = Raw::<KvTestEngine>::default();
assert!(observer.update_context(&mut context));
assert_eq!(context.len(), 1);
assert_eq!(context[0], ConsistencyCheckMethod::Raw as u8);
}
}