Repository: tikv/tikv
File: tests/failpoints/cases/test_stale_read.rs (Rust)

Code before:

    let destroy_peer_fp = "destroy_peer";
    fail::cfg(destroy_peer_fp, "pause").unwrap();
    pd_client.must_remove_peer(r1, new_peer(1, 1));
    sleep_ms(300);

    // Try writing k2 to peer3
    let mut request = new_request(
        r1,
        cluster.pd_client.get_region_epoch(r1),
        vec![new_get_cmd(b"k1")],
        false,
    );
    request.mut_header().set_peer(new_peer(1, 1));
    let (cb, mut rx) = make_cb_rocks(&request);
    cluster
        .sim
        .rl()
        .async_command_on_node(1, request, cb)
        .unwrap();
    // Wait for raftstore receives the read request.
    sleep_ms(200);
    fail::remove(destroy_peer_fp);

    // Validate the async destroy progress.
    let check_state_on_raft_gc_log_tick = "check_state_on_raft_gc_log_tick";
    let (gc_tx, gc_rx) = mpsc::sync_channel(1);
    fail::cfg_callback(destroy_peer_fp, move || {
        gc_tx.send(check_state_on_raft_gc_log_tick).unwrap();
    })
    .unwrap();
    assert_eq!(
        gc_rx.recv_timeout(Duration::from_secs(5)).unwrap(),
        check_state_on_raft_gc_log_tick
    );
    fail::remove(check_state_on_raft_gc_log_tick);

    let resp = rx.recv_timeout(Duration::from_millis(200)).unwrap();
    assert!(
        resp.get_header().get_error().has_region_not_found(),
        "{:?}",
        resp
    );
}

/// In previous implementation, we suspect the leader lease at the position of
/// `leader_commit_prepare_merge` failpoint when `PrepareMerge` log is
/// committed, which is too late to prevent stale read.
#[test]
fn test_stale_read_during_merging_2() {
    let mut cluster = new_node_cluster(0, 3);

Review comment (bug):
It doesn't look like the `check_state_on_raft_gc_log_tick` failpoint is used anywhere, right? I don't see any `fail::cfg` call to it. 

Code after:

    let destroy_peer_fp = "destroy_peer";
    fail::cfg(destroy_peer_fp, "pause").unwrap();
    pd_client.must_remove_peer(r1, new_peer(1, 1));
    sleep_ms(300);

    // Try writing k2 to peer3
    let mut request = new_request(
        r1,
        cluster.pd_client.get_region_epoch(r1),
        vec![new_get_cmd(b"k1")],
        false,
    );
    request.mut_header().set_peer(new_peer(1, 1));
    let (cb, mut rx) = make_cb_rocks(&request);
    cluster
        .sim
        .rl()
        .async_command_on_node(1, request, cb)
        .unwrap();
    // Wait for raftstore receives the read request.
    sleep_ms(200);
    fail::remove(destroy_peer_fp);

    // Validate the async destroy progress.
    let check_state_on_raft_gc_log_tick = "check_state_on_raft_gc_log_tick";
    let (gc_tx, gc_rx) = mpsc::sync_channel(1);
    fail::cfg_callback(check_state_on_raft_gc_log_tick, move || {
        gc_tx.send(check_state_on_raft_gc_log_tick).unwrap();
    })
    .unwrap();
    assert_eq!(
        gc_rx.recv_timeout(Duration::from_secs(5)).unwrap(),
        check_state_on_raft_gc_log_tick
    );
    fail::remove(check_state_on_raft_gc_log_tick);

    let resp = rx.recv_timeout(Duration::from_millis(200)).unwrap();
    assert!(
        resp.get_header().get_error().has_region_not_found(),
        "{:?}",
        resp
    );
}

/// In previous implementation, we suspect the leader lease at the position of
/// `leader_commit_prepare_merge` failpoint when `PrepareMerge` log is
/// committed, which is too late to prevent stale read.
#[test]
fn test_stale_read_during_merging_2() {
    let mut cluster = new_node_cluster(0, 3);