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

Code before:
    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 been 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 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));

    pd_client.must_none_peer(region_id, new_peer(2, 2));
}


Review comment (suggestion):
```suggestion
// has applied a conf change but the leader has not yet applied.
```

Code after:

    // 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));

    pd_client.must_none_peer(region_id, new_peer(2, 2));
}

// This test verifies that a leader transfer is allowed when the transferee