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

Code before:
    fail::cfg("propose_locks_before_transfer_leader", "return").unwrap();

    pd_client.remove_peer(region_id, new_peer(2, 2));
    sleep_ms(300);
    // Peer 2 is still exists since the leader hasn't applied the ConfChange
    // yet.
    pd_client.must_have_peer(region_id, new_peer(2, 2));

    pd_client.transfer_leader(region_id, new_peer(3, 3), vec![]);
    pd_client.region_leader_must_be(region_id, new_peer(3, 3));
}

// This test verifies that a leader transfer is allowed when the transferee
// has been applied a region split but the leader has not yet applied.
#[test]
fn test_when_applied_region_split_on_transferee_pessimistic_lock() {
    let mut cluster = new_server_cluster(0, 3);
    let pd_client = cluster.pd_client.clone();
    pd_client.disable_default_operator();
    let region_id = cluster.run_conf_change();
    pd_client.must_add_peer(region_id, new_peer(2, 2));
    pd_client.must_add_peer(region_id, new_peer(3, 3));
    // Use peer_id 4 as the leader since we want to reuse the failpoint
    // apply_before_split_1_3.
    pd_client.transfer_leader(region_id, new_peer(3, 3), vec![]);
    pd_client.region_leader_must_be(region_id, new_peer(3, 3));

    fail::cfg("apply_before_split_1_1", "pause").unwrap();
    fail::cfg("apply_before_split_1_3", "pause").unwrap();
    fail::cfg("propose_locks_before_transfer_leader", "return").unwrap();

    let region = pd_client.get_region(b"x").unwrap();
    pd_client.split_region(region, pdpb::CheckPolicy::Usekey, vec![b"x1".to_vec()]);
    sleep_ms(300);

    pd_client.transfer_leader(region_id, new_peer(2, 2), vec![]);
    sleep_ms(300);
    pd_client.region_leader_must_be(region_id, new_peer(2, 2));
}

// This test verifies that a leader transfer is:
// - Not allowed when the transferee has been applied a region commit-merge but
//   the leader has not yet applied.
// - Allowed when the transferee has been applied a region prepare-merge but the
//   leader has not yet applied.
#[test]
fn test_when_applied_region_merge_on_transferee_pessimistic_lock() {
    let mut cluster = new_server_cluster(0, 3);
    let pd_client = cluster.pd_client.clone();
    pd_client.disable_default_operator();
    let region_id = cluster.run_conf_change();

Review comment (bug):
```suggestion
    fail::remove("apply_on_conf_change_1_1");
    fail::remove("apply_on_conf_change_3_1");
}
```

Make sure that all failpoints are removed safely.

Code after:
    pd_client.must_add_peer(region_id, new_peer(3, 3));
    pd_client.region_leader_must_be(region_id, new_peer(1, 1));

    fail::cfg("apply_on_conf_change_1_1", "pause").unwrap();

    // Demote peer-2 to be a learner.
    pd_client.joint_confchange(
        region_id,
        vec![(ConfChangeType::AddLearnerNode, new_learner_peer(2, 2))],
    );
    sleep_ms(300);

    pd_client.transfer_leader(region_id, new_peer(2, 2), vec![]);
    sleep_ms(300);
    assert_eq!(
        pd_client.check_region_leader(region_id, new_peer(2, 2)),
        false
    );

    pd_client.transfer_leader(region_id, new_peer(3, 3), vec![]);
    pd_client.region_leader_must_be(region_id, new_peer(3, 3));
    let region = block_on(pd_client.get_region_by_id(region_id))
        .unwrap()
        .unwrap();
    assert_eq!(region.get_peers()[1].get_role(), PeerRole::Learner);
}

// This test verifies that a leader transfer is allowed when the transferee
// has applied a conf change but the leader has not yet applied.
#[test]
fn test_when_applied_conf_change_on_transferee_pessimistic_lock() {
    let mut cluster = new_server_cluster(0, 4);
    let pd_client = cluster.pd_client.clone();
    pd_client.disable_default_operator();
    let region_id = cluster.run_conf_change();
    pd_client.must_add_peer(region_id, new_peer(2, 2));
    pd_client.must_add_peer(region_id, new_peer(3, 3));
    pd_client.region_leader_must_be(region_id, new_peer(1, 1));

    fail::cfg("apply_on_conf_change_1_1", "pause").unwrap();
    fail::cfg("propose_locks_before_transfer_leader", "return").unwrap();

    pd_client.remove_peer(region_id, new_peer(2, 2));
    sleep_ms(300);
    // Peer 2 still exists since the leader hasn't applied the ConfChange
    // yet.
    pd_client.must_have_peer(region_id, new_peer(2, 2));

    pd_client.transfer_leader(region_id, new_peer(3, 3), vec![]);
    pd_client.region_leader_must_be(region_id, new_peer(3, 3));