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

Code before:
            }
        }
        data_file > 0
    };
    let mut cluster = new_node_cluster(0, 3);
    cluster.cfg.raft_store.snap_mgr_gc_tick_interval = ReadableDuration::millis(100);
    cluster.cfg.raft_store.pd_heartbeat_tick_interval = ReadableDuration::millis(100);
    // Disable raft log gc in this test case.
    cluster.cfg.raft_store.raft_log_gc_tick_interval = ReadableDuration::secs(60);

    let pd_client = Arc::clone(&cluster.pd_client);
    // Disable default max peer count check.
    pd_client.disable_default_operator();

    let region_id = cluster.run_conf_change();
    fail::cfg("manually_set_store_offline", "return").unwrap();
    pd_client.must_add_peer(region_id, new_peer(2, 2));
    pd_client.must_add_peer(region_id, new_peer(3, 3));

    (0..10).for_each(|_| cluster.must_put(b"k1", b"v1"));
    // Send heartbeats to pd to ensure that heartbeat responses have been received
    // by node 3.
    cluster.must_send_store_heartbeat(3);
    must_get_equal(&cluster.get_engine(3), b"k1", b"v1");
    pd_client.must_remove_peer(region_id, new_peer(3, 3));
    // SInce the node 3 is marked with NodeState::Removing, its `offlined == true`
    // in SnapManager, and no need to clear the corresponding data.
    sleep_ms(500);
    let engine = cluster.get_engine(3);
    let engine_path = Path::new(engine.get_engine_path());
    assert!(validate_data_files(engine_path));
    fail::remove("manually_set_store_offline");
}

#[test_case(test_raftstore::new_node_cluster)]
#[test_case(test_raftstore_v2::new_node_cluster)]
fn test_node_update_localreader_after_removed() {
    let mut cluster = new_node_cluster(0, 6);
    let pd_client = cluster.pd_client.clone();
    // Disable default max peer number check.
    pd_client.disable_default_operator();
    let r1 = cluster.run_conf_change();

    // Add 4 peers.
    for i in 2..6 {
        pd_client.must_add_peer(r1, new_peer(i, i));
    }

    // Make sure peer 1 leads the region.
    cluster.must_transfer_leader(r1, new_peer(1, 1));
    let (key, value) = (b"k1", b"v1");

Review comment (suggestion):
```suggestion
    // Since the node 3 is marked with NodeState::Removing, its `offlined == true`
```

Code after:
            }
        }
        data_file > 0
    };
    let mut cluster = new_node_cluster(0, 3);
    cluster.cfg.raft_store.snap_mgr_gc_tick_interval = ReadableDuration::millis(100);
    cluster.cfg.raft_store.pd_heartbeat_tick_interval = ReadableDuration::millis(100);
    // Disable raft log gc in this test case.
    cluster.cfg.raft_store.raft_log_gc_tick_interval = ReadableDuration::secs(60);

    let pd_client = Arc::clone(&cluster.pd_client);
    // Disable default max peer count check.
    pd_client.disable_default_operator();

    let region_id = cluster.run_conf_change();

    // Manually set store 3 is offlined.
    fail::cfg("manually_set_store_offline", "return").unwrap();
    pd_client.must_add_peer(region_id, new_peer(2, 2));
    pd_client.must_add_peer(region_id, new_peer(3, 3));

    (0..10).for_each(|_| cluster.must_put(b"k1", b"v1"));
    // Send heartbeats to pd to ensure that heartbeat responses have been received
    // by node 3.
    cluster.must_send_store_heartbeat(3);
    must_get_equal(&cluster.get_engine(3), b"k1", b"v1");
    pd_client.must_remove_peer(region_id, new_peer(3, 3));
    // Since the node 3 is marked with NodeState::Removing, its `offlined == true`
    // in SnapManager, and no need to clear the corresponding data.
    sleep_ms(500);
    let engine = cluster.get_engine(3);
    let engine_path = Path::new(engine.get_engine_path());
    assert!(validate_data_files(engine_path));
    fail::remove("manually_set_store_offline");
    // After the state is cleared, the store should be marked with
    // NodeState::Serving. And after adding a new peer to store 3,
    // all previous data should be synced to it.
    cluster.must_send_store_heartbeat(3);
    cluster.must_put(b"k10", b"v10");
    let engine = cluster.get_engine(3);
    let (key, value) = (b"k10", b"v10");
    must_get_none(&engine, key);
    pd_client.must_add_peer(region_id, new_peer(3, 4));
    must_get_equal(&engine, key, value);
    must_get_equal(&cluster.get_engine(3), b"k1", b"v1");
    // Remove the peer 4.
    pd_client.must_remove_peer(region_id, new_peer(3, 4));
    sleep_ms(500);
    must_get_none(&engine, key);
}