Skip to content

Commit

Permalink
modern sync: separate concerns
Browse files Browse the repository at this point in the history
Summary: I was putting everything in `sync-loop` cause that's the main command to be used, but if we want to reuse this in, e.g `sync-once` that would be nested user a bunch of unnecessary stuff, so pulling it out (also, keeping files small for my own sanity)

Reviewed By: andreacampi

Differential Revision: D65541266

fbshipit-source-id: 7a23294a98358265a3cb1165adf8c57c780a3067
  • Loading branch information
lmvasquezg authored and facebook-github-bot committed Nov 7, 2024
1 parent 3582448 commit 740bd93
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 55 deletions.
60 changes: 5 additions & 55 deletions eden/mononoke/modern_sync/src/commands/sync_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,35 +8,26 @@
use std::sync::atomic::AtomicBool;
use std::sync::Arc;

use anyhow::format_err;
use anyhow::Result;
use async_trait::async_trait;
use bookmarks::BookmarkUpdateLogRef;
use clap::Parser;
use clientinfo::ClientEntryPoint;
use clientinfo::ClientInfo;
use context::CoreContext;
use executor_lib::RepoShardedProcess;
use executor_lib::RepoShardedProcessExecutor;
use mononoke_app::args::AsRepoArg;
use mononoke_app::args::RepoArg;
use mononoke_app::MononokeApp;
use mutable_counters::MutableCountersRef;
use repo_identity::RepoIdentityRef;
use sharding_ext::RepoShard;
use slog::info;

use crate::ModernSyncArgs;
use crate::Repo;

const SM_CLEANUP_TIMEOUT_SECS: u64 = 120;
const MODERN_SYNC_COUNTER_NAME: &str = "modern_sync";

/// Replays bookmark's moves
#[derive(Parser)]
pub struct CommandArgs {
#[clap(long = "start-id", help = "Start id for the sync [default: 0]")]
start_id: Option<u64>,
start_id: Option<i64>,
}

pub struct ModernSyncProcess {
Expand Down Expand Up @@ -70,9 +61,9 @@ pub struct ModernSyncProcessExecutor {
#[async_trait]
impl RepoShardedProcessExecutor for ModernSyncProcessExecutor {
async fn execute(&self) -> Result<()> {
sync(
crate::sync::sync(
self.app.clone(),
self.sync_args.clone(),
self.sync_args.start_id,
self.repo_arg.clone(),
)
.await?;
Expand All @@ -84,47 +75,6 @@ impl RepoShardedProcessExecutor for ModernSyncProcessExecutor {
}
}

pub async fn sync(
app: Arc<MononokeApp>,
sync_args: Arc<CommandArgs>,
repo_arg: RepoArg,
) -> Result<()> {
let repo: Repo = app.open_repo(&repo_arg).await?;
let _repo_id = repo.repo_identity().id();
let repo_name = repo.repo_identity().name().to_string();

let ctx = CoreContext::new_with_logger_and_client_info(
app.fb,
app.logger().clone(),
ClientInfo::default_with_entry_point(ClientEntryPoint::ModernSync),
)
.clone_with_repo_name(&repo_name);

let _bookmark_update_log = repo.bookmark_update_log();
let start_id;

if let Some(id) = sync_args.start_id {
start_id = id
} else {
start_id = repo
.mutable_counters()
.get_counter(&ctx, MODERN_SYNC_COUNTER_NAME)
.await?
.map(|val| val.try_into())
.transpose()?
.ok_or_else(|| {
format_err!(
"No start-id or mutable counter {} provided",
MODERN_SYNC_COUNTER_NAME
)
})?;
};

info!(app.logger(), "Starting with value {}", start_id);

Ok(())
}

pub async fn run(app: MononokeApp, args: CommandArgs) -> Result<()> {
let app_args = &app.args::<ModernSyncArgs>()?;

Expand All @@ -145,9 +95,9 @@ pub async fn run(app: MononokeApp, args: CommandArgs) -> Result<()> {
.await?;
} else {
info!(logger, "Running unsharded sync loop");
sync(
crate::sync::sync(
process.app.clone(),
process.sync_args.clone(),
process.sync_args.start_id.clone(),
app_args.repo.as_repo_arg().clone(),
)
.await?;
Expand Down
1 change: 1 addition & 0 deletions eden/mononoke/modern_sync/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use mononoke_app::MononokeAppBuilder;
use mutable_counters::MutableCounters;
use repo_identity::RepoIdentity;
mod commands;
mod sync;

#[derive(Parser)]
struct ModernSyncArgs {
Expand Down
55 changes: 55 additions & 0 deletions eden/mononoke/modern_sync/src/sync.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This software may be used and distributed according to the terms of the
* GNU General Public License version 2.
*/

use std::sync::Arc;

use anyhow::format_err;
use anyhow::Result;
use clientinfo::ClientEntryPoint;
use clientinfo::ClientInfo;
use context::CoreContext;
use mononoke_app::args::RepoArg;
use mononoke_app::MononokeApp;
use mutable_counters::MutableCountersRef;
use repo_identity::RepoIdentityRef;
use slog::info;

use crate::Repo;
const MODERN_SYNC_COUNTER_NAME: &str = "modern_sync";

pub async fn sync(
app: Arc<MononokeApp>,
start_id_arg: Option<i64>,
repo_arg: RepoArg,
) -> Result<()> {
let repo: Repo = app.open_repo(&repo_arg).await?;
let _repo_id = repo.repo_identity().id();
let repo_name = repo.repo_identity().name().to_string();

let ctx = CoreContext::new_with_logger_and_client_info(
app.fb,
app.logger().clone(),
ClientInfo::default_with_entry_point(ClientEntryPoint::ModernSync),
)
.clone_with_repo_name(&repo_name);

let start_id = start_id_arg.unwrap_or(
repo.mutable_counters()
.get_counter(&ctx, MODERN_SYNC_COUNTER_NAME)
.await?
.ok_or_else(|| {
format_err!(
"No start-id or mutable counter {} provided",
MODERN_SYNC_COUNTER_NAME
)
})?,
);

info!(app.logger(), "Starting with value {}", start_id);

Ok(())
}

0 comments on commit 740bd93

Please sign in to comment.