-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
migrate individual template data stage 1 (#198)
* add memory manager and update pre_upgrade hook * remove send_metrices from post_upgrade; update recharge amout in user_index * fix test
- Loading branch information
Showing
8 changed files
with
44 additions
and
13 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 14 additions & 9 deletions
23
src/canister/individual_user_template/src/api/canister_lifecycle/pre_upgrade.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,22 @@ | ||
use shared_utils::common::utils::stable_memory_serializer_deserializer; | ||
use ciborium::ser; | ||
use ic_stable_structures::writer::Writer; | ||
|
||
use crate::data_model::memory; | ||
use crate::CANISTER_DATA; | ||
|
||
pub const BUFFER_SIZE_BYTES: usize = 2 * 1024 * 1024; // 2 MiB | ||
|
||
#[ic_cdk::pre_upgrade] | ||
fn pre_upgrade() { | ||
let mut state_bytes = vec![]; | ||
CANISTER_DATA.with(|canister_data_ref_cell| { | ||
let canister_data = canister_data_ref_cell.take(); | ||
stable_memory_serializer_deserializer::serialize_to_stable_memory( | ||
canister_data, | ||
BUFFER_SIZE_BYTES, | ||
) | ||
.expect("Failed to serialize canister data"); | ||
}); | ||
} | ||
ser::into_writer(&*canister_data_ref_cell.borrow(), &mut state_bytes) | ||
}) | ||
.expect("failed to encode state"); | ||
|
||
let len = state_bytes.len() as u32; | ||
let mut memory = memory::get_upgrades_memory(); | ||
let mut writer = Writer::new(&mut memory, 0); | ||
writer.write(&len.to_le_bytes()).unwrap(); | ||
writer.write(&state_bytes).unwrap(); | ||
} |
22 changes: 22 additions & 0 deletions
22
src/canister/individual_user_template/src/data_model/memory.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
use ic_stable_structures::{DefaultMemoryImpl, memory_manager::{MemoryId, VirtualMemory, MemoryManager}}; | ||
use std::cell::RefCell; | ||
|
||
// A memory for upgrades, where data from the heap can be serialized/deserialized. | ||
const UPGRADES: MemoryId = MemoryId::new(0); | ||
|
||
// A memory for the StableBTreeMap we're using. A new memory should be created for | ||
// every additional stable structure. | ||
|
||
|
||
pub type Memory = VirtualMemory<DefaultMemoryImpl>; | ||
|
||
thread_local! { | ||
// The memory manager is used for simulating multiple memories. Given a `MemoryId` it can | ||
// return a memory that can be used by stable structures. | ||
static MEMORY_MANAGER: RefCell<MemoryManager<DefaultMemoryImpl>> = | ||
RefCell::new(MemoryManager::init(DefaultMemoryImpl::default())); | ||
} | ||
|
||
pub fn get_upgrades_memory() -> Memory { | ||
MEMORY_MANAGER.with(|m| m.borrow_mut().get(UPGRADES)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters