Skip to content

Commit

Permalink
mononoke: add CommitSyncDataProvider
Browse files Browse the repository at this point in the history
Summary:
As described in D23845720 (5de500b) we are doing a pretty significant change in the
CommitSyncer. Previously it stored static Movers and BookmarkRenamers, but it
needs to change and they would need to fetch config from LiveCommitSyncConfig.

Unfortunately we already have a bunch of tests that create weird movers that
would be very hard to model via LiveCommitSyncConfig. So we have 2 options:

1) delete or rewrite all these tests
2) Create a wrapper that would return a mover/bookmark renamers.

Option #2 is preferable, and that's what this diff does. In production it would
use LiveCommitSyncConfig, but it also let's tests specify whatever weird movers
they need.

Reviewed By: ikostia

Differential Revision: D23909432

fbshipit-source-id: 83fb627812f625e07f7e40044e2f69274cd2d768
  • Loading branch information
StanislavGlebik authored and facebook-github-bot committed Sep 25, 2020
1 parent 5d6dfac commit f2a7850
Show file tree
Hide file tree
Showing 2 changed files with 204 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,196 @@
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This software may be used and distributed according to the terms of the
* GNU General Public License version 2.
*/

use anyhow::{anyhow, Error};
use bookmark_renaming::{get_bookmark_renamers, BookmarkRenamer, BookmarkRenamers};
use live_commit_sync_config::LiveCommitSyncConfig;
use metaconfig_types::{CommitSyncConfig, CommitSyncConfigVersion, CommitSyncDirection};
use mononoke_types::RepositoryId;
use movers::{get_movers, Mover, Movers};
use std::collections::HashMap;
use std::sync::Arc;

#[derive(Clone)]
pub enum CommitSyncDataProvider {
Live(Arc<dyn LiveCommitSyncConfig>),
Test(HashMap<CommitSyncConfigVersion, SyncData>),
}

#[derive(Clone)]
pub struct SyncData {
pub mover: Mover,
pub reverse_mover: Mover,
pub bookmark_renamer: BookmarkRenamer,
pub reverse_bookmark_renamer: BookmarkRenamer,
}

impl CommitSyncDataProvider {
pub fn get_mover(
&self,
version: &CommitSyncConfigVersion,
source_repo_id: RepositoryId,
target_repo_id: RepositoryId,
) -> Result<Mover, Error> {
use CommitSyncDataProvider::*;

match self {
Live(live_commit_sync_config) => {
let commit_sync_config = live_commit_sync_config
.get_commit_sync_config_by_version(source_repo_id, version)?;

let Movers { mover, .. } =
get_movers_from_config(&commit_sync_config, source_repo_id, target_repo_id)?;
Ok(mover)
}
Test(map) => {
let sync_data = map
.get(version)
.ok_or_else(|| anyhow!("sync data not found for {}", version))?;
Ok(sync_data.mover.clone())
}
}
}

pub fn get_reverse_mover(
&self,
version: &CommitSyncConfigVersion,
source_repo_id: RepositoryId,
target_repo_id: RepositoryId,
) -> Result<Mover, Error> {
use CommitSyncDataProvider::*;

match self {
Live(live_commit_sync_config) => {
let commit_sync_config = live_commit_sync_config
.get_commit_sync_config_by_version(source_repo_id, version)?;

let Movers { reverse_mover, .. } =
get_movers_from_config(&commit_sync_config, source_repo_id, target_repo_id)?;
Ok(reverse_mover)
}
Test(map) => {
let sync_data = map
.get(version)
.ok_or_else(|| anyhow!("sync data not found for {}", version))?;
Ok(sync_data.reverse_mover.clone())
}
}
}

pub fn get_bookmark_renamer(
&self,
version: &CommitSyncConfigVersion,
source_repo_id: RepositoryId,
target_repo_id: RepositoryId,
) -> Result<BookmarkRenamer, Error> {
use CommitSyncDataProvider::*;

match self {
Live(live_commit_sync_config) => {
let commit_sync_config = live_commit_sync_config
.get_commit_sync_config_by_version(source_repo_id, version)?;

let BookmarkRenamers {
bookmark_renamer, ..
} = get_bookmark_renamers_from_config(
&commit_sync_config,
source_repo_id,
target_repo_id,
)?;
Ok(bookmark_renamer)
}
Test(map) => {
let sync_data = map
.get(version)
.ok_or_else(|| anyhow!("sync data not found for {}", version))?;
Ok(sync_data.bookmark_renamer.clone())
}
}
}

pub fn get_reverse_bookmark_renamer(
&self,
version: &CommitSyncConfigVersion,
source_repo_id: RepositoryId,
target_repo_id: RepositoryId,
) -> Result<BookmarkRenamer, Error> {
use CommitSyncDataProvider::*;

match self {
Live(live_commit_sync_config) => {
let commit_sync_config = live_commit_sync_config
.get_commit_sync_config_by_version(source_repo_id, version)?;

let BookmarkRenamers {
reverse_bookmark_renamer,
..
} = get_bookmark_renamers_from_config(
&commit_sync_config,
source_repo_id,
target_repo_id,
)?;
Ok(reverse_bookmark_renamer)
}
Test(map) => {
let sync_data = map
.get(version)
.ok_or_else(|| anyhow!("sync data not found for {}", version))?;
Ok(sync_data.reverse_bookmark_renamer.clone())
}
}
}
}

fn get_movers_from_config(
commit_sync_config: &CommitSyncConfig,
source_repo_id: RepositoryId,
target_repo_id: RepositoryId,
) -> Result<Movers, Error> {
let (direction, small_repo_id) =
get_direction_and_small_repo_id(commit_sync_config, source_repo_id, target_repo_id)?;
get_movers(&commit_sync_config, small_repo_id, direction)
}

fn get_bookmark_renamers_from_config(
commit_sync_config: &CommitSyncConfig,
source_repo_id: RepositoryId,
target_repo_id: RepositoryId,
) -> Result<BookmarkRenamers, Error> {
let (direction, small_repo_id) =
get_direction_and_small_repo_id(commit_sync_config, source_repo_id, target_repo_id)?;
get_bookmark_renamers(commit_sync_config, small_repo_id, direction)
}

fn get_direction_and_small_repo_id(
commit_sync_config: &CommitSyncConfig,
source_repo_id: RepositoryId,
target_repo_id: RepositoryId,
) -> Result<(CommitSyncDirection, RepositoryId), Error> {
let small_repo_id = if commit_sync_config.large_repo_id == source_repo_id
&& commit_sync_config.small_repos.contains_key(&target_repo_id)
{
target_repo_id
} else if commit_sync_config.large_repo_id == target_repo_id
&& commit_sync_config.small_repos.contains_key(&source_repo_id)
{
source_repo_id
} else {
return Err(anyhow!(
"CommitSyncMapping incompatible with source repo {:?} and target repo {:?}",
source_repo_id,
target_repo_id,
));
};

let direction = if source_repo_id == small_repo_id {
CommitSyncDirection::SmallToLarge
} else {
CommitSyncDirection::LargeToSmall
};

Ok((direction, small_repo_id))
}
12 changes: 8 additions & 4 deletions eden/mononoke/commit_rewriting/cross_repo_sync/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ pub use commit_syncer_args::CommitSyncerArgs;
use pushrebase_hook::CrossRepoSyncPushrebaseHook;
use types::{Source, Target};

mod commit_sync_data_provider;
pub mod commit_sync_outcome;
mod commit_syncer_args;
mod pushrebase_hook;
Expand All @@ -66,6 +67,7 @@ pub use crate::commit_sync_outcome::{
get_commit_sync_outcome, get_commit_sync_outcome_with_hint, get_plural_commit_sync_outcome,
CandidateSelectionHint, CommitSyncOutcome, PluralCommitSyncOutcome,
};
use commit_sync_data_provider::CommitSyncDataProvider;

#[derive(Debug, Error)]
pub enum ErrorKind {
Expand Down Expand Up @@ -490,7 +492,7 @@ pub struct CommitSyncer<M> {
// TODO: Finish refactor and remove pub
pub mapping: M,
pub repos: CommitSyncRepos,
live_commit_sync_config: Arc<dyn LiveCommitSyncConfig>,
pub commit_sync_data_provider: CommitSyncDataProvider,
}

impl<M> fmt::Debug for CommitSyncer<M>
Expand All @@ -513,10 +515,11 @@ where
repos: CommitSyncRepos,
live_commit_sync_config: Arc<dyn LiveCommitSyncConfig>,
) -> Self {
let commit_sync_data_provider = CommitSyncDataProvider::Live(live_commit_sync_config);
Self {
mapping,
repos,
live_commit_sync_config,
commit_sync_data_provider,
}
}

Expand Down Expand Up @@ -1556,15 +1559,16 @@ where
version_name: commit_sync_config.version_name.clone(),
};

let commit_sync_data_provider = CommitSyncDataProvider::Live(live_commit_sync_config);
let large_to_small_commit_syncer = CommitSyncer {
mapping: mapping.clone(),
repos: large_to_small_commit_sync_repos,
live_commit_sync_config: live_commit_sync_config.clone(),
commit_sync_data_provider: commit_sync_data_provider.clone(),
};
let small_to_large_commit_syncer = CommitSyncer {
mapping,
repos: small_to_large_commit_sync_repos,
live_commit_sync_config: live_commit_sync_config.clone(),
commit_sync_data_provider,
};

Ok(Syncers {
Expand Down

0 comments on commit f2a7850

Please sign in to comment.