Skip to content

Commit 0871f67

Browse files
committed
feat: simplistic version of box environment for demonstration.
1 parent 2529fb5 commit 0871f67

File tree

18 files changed

+443
-201
lines changed

18 files changed

+443
-201
lines changed

Cargo.lock

Lines changed: 18 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ members = [
3131
"util/movement/*",
3232
"util/movement-aptos/*",
3333
"util/bcs-ext",
34-
"util/types"
34+
"util/types",
35+
"util/util"
3536

3637
]
3738

@@ -215,6 +216,7 @@ movement-aptos = { path = "util/movement-aptos/movement-aptos" }
215216
movement-aptos-core = { path = "util/movement-aptos/core" }
216217
movement-aptos-core-util = { path = "util/movement-aptos/core-util" }
217218
mtma-types = { path = "util/types" }
219+
mtma-util = { path = "util/util" }
218220

219221
## Environments
220222
### util

checks/migrator/checks/pre-l1-merge/src/pre_l1_merge.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ pub mod test {
3131
// Create a Movement instance with the config
3232
let movement = Movement::new(
3333
movement_config.clone(),
34-
movement_core::MovementWorkspace::try_temp()?,
34+
movement_core::MovementWorkspace::try_temp()?.into(),
3535
Overlays::default(),
3636
true,
3737
true,

migration/core/migrator/mtma-null/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ walkdir = { workspace = true }
2828
uuid = { workspace = true }
2929
tracing = { workspace = true }
3030
mtma-types = { workspace = true }
31+
mtma-util = { workspace = true }
3132

3233
[lints]
3334
workspace = true

migration/core/migrator/mtma-null/src/migrate/migrate.rs

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,28 +8,9 @@ use mtma_types::movement_aptos::aptos_db::AptosDB;
88
use mtma_types::movement_aptos::aptos_storage_interface::DbReaderWriter;
99

1010
use anyhow::Context;
11-
use std::fs;
11+
use mtma_util::file::copy_dir_recursive;
1212
use std::path::Path;
1313
use tracing::info;
14-
use walkdir::WalkDir;
15-
16-
/// Copies a directory recursively.
17-
///
18-
/// todo: move this out of the migration module
19-
fn copy_dir_recursive(src: &Path, dst: &Path) -> std::io::Result<()> {
20-
for entry in WalkDir::new(src) {
21-
let entry = entry?;
22-
let rel_path = entry.path().strip_prefix(src).unwrap();
23-
let dest_path = dst.join(rel_path);
24-
25-
if entry.file_type().is_dir() {
26-
fs::create_dir_all(&dest_path)?;
27-
} else {
28-
fs::copy(entry.path(), &dest_path)?;
29-
}
30-
}
31-
Ok(())
32-
}
3314

3415
/// Errors thrown during the migration.
3516
#[derive(Debug, thiserror::Error)]

migration/core/node/mtma-null/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,7 @@ walkdir = { workspace = true }
2828
uuid = { workspace = true }
2929
tracing = { workspace = true }
3030
mtma-types = { workspace = true }
31+
mtma-util = { workspace = true }
32+
3133
[lints]
3234
workspace = true

migration/core/node/mtma-null/src/migrate/migrate.rs

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,28 +7,9 @@ use mtma_types::movement_aptos::aptos_db::AptosDB;
77
use mtma_types::movement_aptos::aptos_storage_interface::DbReaderWriter;
88

99
use anyhow::Context;
10-
use std::fs;
10+
use mtma_util::file::copy_dir_recursive;
1111
use std::path::Path;
1212
use tracing::info;
13-
use walkdir::WalkDir;
14-
15-
/// Copies a directory recursively.
16-
///
17-
/// todo: move this out of the migration module
18-
fn copy_dir_recursive(src: &Path, dst: &Path) -> std::io::Result<()> {
19-
for entry in WalkDir::new(src) {
20-
let entry = entry?;
21-
let rel_path = entry.path().strip_prefix(src).unwrap();
22-
let dest_path = dst.join(rel_path);
23-
24-
if entry.file_type().is_dir() {
25-
fs::create_dir_all(&dest_path)?;
26-
} else {
27-
fs::copy(entry.path(), &dest_path)?;
28-
}
29-
}
30-
Ok(())
31-
}
3213

3314
/// Errors thrown during the migration.
3415
#[derive(Debug, thiserror::Error)]

migration/util/migrator-types/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ movement-client = { workspace = true }
2828
aptos-rest-client = { workspace = true }
2929
aptos-config = { workspace = true }
3030
mtma-types = { workspace = true }
31+
reqwest = { workspace = true }
32+
mtma-util = { workspace = true }
3133

3234
[lints]
3335
workspace = true

migration/util/migrator-types/src/migrator/movement_migrator.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ pub use movement_core::{Overlay, Overlays};
55
use mtma_node_types::executor::MovementNode;
66
pub use mtma_types::movement::aptos_types::{chain_id::ChainId, state_store::TStateView};
77
use mtma_types::movement::movement_client::rest_client::Client as MovementRestClient;
8+
pub mod live;
9+
pub use live::LiveMigrator;
10+
use mtma_util::file::copy_dir_recursive;
11+
use std::path::PathBuf;
12+
use tracing::warn;
813

914
/// An enum supporting different types of runners.
1015
///
@@ -14,6 +19,8 @@ use mtma_types::movement::movement_client::rest_client::Client as MovementRestCl
1419
pub enum Runner {
1520
/// [Movement] runner.
1621
Movement(Movement),
22+
/// [LiveMigrator] runner.
23+
Live(LiveMigrator),
1724
}
1825

1926
/// The Movement migration struct as would be presented in the criterion.
@@ -35,6 +42,7 @@ impl MovementMigrator {
3542
pub async fn run(&self) -> Result<(), anyhow::Error> {
3643
match &self.runner {
3744
Runner::Movement(movement) => Ok(movement.run().await?),
45+
Runner::Live(live) => Ok(live.run().await?),
3846
}
3947
}
4048

@@ -71,6 +79,7 @@ impl MovementMigrator {
7179
.context("failed to wait for Movement rest api")?;
7280
Ok(rest_api.listen_url().to_string())
7381
}
82+
Runner::Live(live) => live.wait_for_live_rest_api_url().await,
7483
}
7584
}
7685

@@ -94,13 +103,41 @@ impl MovementMigrator {
94103
Runner::Movement(movement) => {
95104
MovementNode::try_from_dir(movement.workspace_path().to_path_buf()).await
96105
}
106+
Runner::Live(live) => MovementNode::try_from_dir(live.dir.clone()).await,
97107
}
98108
}
99109

100110
/// Sets the overlays for the runner.
101111
pub fn set_overlays(&mut self, overlays: Overlays) {
102112
match &mut self.runner {
103113
Runner::Movement(movement) => movement.set_overlays(overlays),
114+
Runner::Live(_live) => {
115+
warn!("Setting overlays for live migrator is not supported as the runner is already live. If you want to set overlays consider snapshotting the live migrator and using that as a movement runner.");
116+
}
117+
}
118+
}
119+
120+
/// Gets the dir for the runner
121+
pub fn dir(&self) -> PathBuf {
122+
match &self.runner {
123+
Runner::Movement(movement) => movement.workspace_path().to_path_buf(),
124+
Runner::Live(live) => live.dir.clone(),
104125
}
105126
}
127+
128+
/// Recursively copies the dir for the runner to the given path
129+
pub async fn copy_dir(&self, path: PathBuf) -> Result<(), anyhow::Error> {
130+
let dir = self.dir();
131+
copy_dir_recursive(&dir, &path).context("failed to copy dir for MovementMigrator")?;
132+
Ok(())
133+
}
134+
135+
/// Snapshots the migrator
136+
///
137+
/// NOTE: this can be used to, for example, transition from a live migrator to a movement runner.
138+
pub async fn snapshot(&self, path: PathBuf) -> Result<Self, anyhow::Error> {
139+
self.copy_dir(path.clone()).await?;
140+
141+
Ok(Self::new(Runner::Movement(Movement::try_from_dot_movement_dir(path)?)))
142+
}
106143
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
use std::path::PathBuf;
2+
use tracing::warn;
3+
4+
#[derive(Debug, Clone)]
5+
pub struct LiveMigrator {
6+
pub rest_api_url: String,
7+
pub dir: PathBuf,
8+
}
9+
10+
impl LiveMigrator {
11+
pub fn new(rest_api_url: String, dir: PathBuf) -> Self {
12+
Self { rest_api_url, dir }
13+
}
14+
}
15+
16+
impl LiveMigrator {
17+
pub async fn run(&self) -> Result<(), anyhow::Error> {
18+
// checks for liveness by polling the rest api url
19+
// once it becomes read the firs time, it should not later return an error
20+
let mut ready = false;
21+
loop {
22+
match reqwest::get(self.rest_api_url.clone()).await {
23+
Ok(response) => {
24+
if response.status().is_success() {
25+
ready = true;
26+
continue;
27+
} else if response.status().is_server_error() && ready {
28+
return Err(anyhow::anyhow!("live migrator failed health check"));
29+
} else {
30+
warn!("live migrator not yet ready: response: {:?}", response);
31+
}
32+
}
33+
Err(e) => {
34+
warn!("failed to poll live migrator: {}", e);
35+
if ready {
36+
return Err(anyhow::anyhow!("live migrator failed health check"));
37+
}
38+
}
39+
}
40+
}
41+
}
42+
43+
pub async fn wait_for_live_rest_api_url(&self) -> Result<String, anyhow::Error> {
44+
// us request to poll the rest API url until is ready
45+
loop {
46+
match reqwest::get(self.rest_api_url.clone()).await {
47+
Ok(response) => {
48+
if response.status().is_success() {
49+
return Ok(self.rest_api_url.clone());
50+
}
51+
warn!("rest api url is not ready yet: response: {:?}", response);
52+
}
53+
Err(e) => {
54+
warn!("failed to poll rest api url: {}", e);
55+
}
56+
}
57+
}
58+
}
59+
}

0 commit comments

Comments
 (0)