mononoke/segmented_changelog: fix leader fallbacks being the wrong way around

Summary:
We have fallback logic to go to the leader if the data we want is missing in
the replica, but right now it's backwards so we go to the leader to find data
we actually *did* find in the replica (and we don't go to the leader for
missing data).

Reviewed By: sfilipco

Differential Revision: D26103898

fbshipit-source-id: 535abab2a3093165f1d55359d102a7a7cb542a9c
This commit is contained in:
Thomas Orozco 2021-01-27 12:27:04 -08:00 committed by Facebook GitHub Bot
parent 1076f2dc58
commit 6c6f698e99

View File

@ -294,7 +294,7 @@ impl IdMap for SqlIdMap {
let mut cs_ids = select_vertexes(&self.connections.read_connection, &to_query).await?; let mut cs_ids = select_vertexes(&self.connections.read_connection, &to_query).await?;
let not_found_in_replica: Vec<_> = vertexes let not_found_in_replica: Vec<_> = vertexes
.iter() .iter()
.filter(|x| cs_ids.contains_key(x)) .filter(|x| !cs_ids.contains_key(x))
.map(|v| v.0) .map(|v| v.0)
.collect(); .collect();
if !not_found_in_replica.is_empty() { if !not_found_in_replica.is_empty() {
@ -355,6 +355,7 @@ mod tests {
use super::*; use super::*;
use maplit::hashmap; use maplit::hashmap;
use sql::{rusqlite::Connection as SqliteConnection, Connection};
use fbinit::FacebookInit; use fbinit::FacebookInit;
@ -491,6 +492,41 @@ mod tests {
Ok(()) Ok(())
} }
#[fbinit::compat_test]
async fn test_find_many_changeset_ids_leader_fallback(fb: FacebookInit) -> Result<()> {
fn conn() -> Result<Connection> {
let con = SqliteConnection::open_in_memory()?;
con.execute_batch(SegmentedChangelogBuilder::CREATION_QUERY)?;
Ok(Connection::with_sqlite(con))
}
let ctx = CoreContext::test_mock(fb);
let leader = conn()?;
let replica = conn()?;
let conns = SqlConnections {
write_connection: leader.clone(),
read_connection: replica,
read_master_connection: leader,
};
let idmap = SegmentedChangelogBuilder::new()
.with_sql_connections(conns)
.with_repo_id(RepositoryId::new(0))
.build_sql_idmap()?;
idmap.insert(&ctx, Vertex(0), ONES_CSID).await?;
let res = idmap.get_changeset_id(&ctx, Vertex(0)).await?;
assert_eq!(res, ONES_CSID);
let res = idmap.find_many_changeset_ids(&ctx, vec![Vertex(0)]).await?;
assert_eq!(res, hashmap![Vertex(0) => ONES_CSID]);
Ok(())
}
#[fbinit::compat_test] #[fbinit::compat_test]
async fn test_many_repo_id_many_versions(fb: FacebookInit) -> Result<()> { async fn test_many_repo_id_many_versions(fb: FacebookInit) -> Result<()> {
let ctx = CoreContext::test_mock(fb); let ctx = CoreContext::test_mock(fb);