Skip to content

Commit

Permalink
Remove code to migrate members and events to stable memory (#7024)
Browse files Browse the repository at this point in the history
  • Loading branch information
hpeebles authored Dec 9, 2024
1 parent b2d640c commit 616fb1f
Show file tree
Hide file tree
Showing 6 changed files with 10 additions and 170 deletions.
4 changes: 2 additions & 2 deletions backend/canisters/community/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
- Ensure bot has permission to execute given action ([#7014](https://github.com/open-chat-labs/open-chat/pull/7014))
- Allow bots to send a subset of message types ([#7016](https://github.com/open-chat-labs/open-chat/pull/7016))

### Fixed
### Removed

- Temporarily reinstate `MigrateMembersToStableMemory` job to fix upgrade ([#7007](https://github.com/open-chat-labs/open-chat/pull/7007))
- Remove code to migrate members and events to stable memory ([#7024](https://github.com/open-chat-labs/open-chat/pull/7024))

## [[2.0.1500](https://github.com/open-chat-labs/open-chat/releases/tag/v2.0.1500-community)] - 2024-12-06

Expand Down
4 changes: 0 additions & 4 deletions backend/canisters/community/impl/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,6 @@ impl RuntimeState {
instruction_counts: self.data.instruction_counts_log.iter().collect(),
event_store_client_info: self.data.event_store_client.info(),
timer_jobs: self.data.timer_jobs.len() as u32,
members_migrated_to_stable_memory: self.data.members_migrated_to_stable_memory,
stable_memory_sizes: memory::memory_sizes(),
canister_ids: CanisterIds {
user_index: self.data.user_index_canister_id,
Expand Down Expand Up @@ -377,7 +376,6 @@ struct Data {
user_cache: UserCache,
user_event_sync_queue: GroupedTimerJobQueue<UserEventBatch>,
stable_memory_keys_to_garbage_collect: Vec<BaseKeyPrefix>,
members_migrated_to_stable_memory: bool,
#[serde(default)]
bots: GroupBots,
}
Expand Down Expand Up @@ -482,7 +480,6 @@ impl Data {
user_cache: UserCache::default(),
user_event_sync_queue: GroupedTimerJobQueue::new(5, true),
stable_memory_keys_to_garbage_collect: Vec::new(),
members_migrated_to_stable_memory: true,
bots: GroupBots::default(),
}
}
Expand Down Expand Up @@ -897,7 +894,6 @@ pub struct Metrics {
pub instruction_counts: Vec<InstructionCountEntry>,
pub event_store_client_info: EventStoreClientInfo,
pub timer_jobs: u32,
pub members_migrated_to_stable_memory: bool,
pub stable_memory_sizes: BTreeMap<u8, u64>,
pub canister_ids: CanisterIds,
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,11 @@ fn post_upgrade(args: Args) {
let memory = get_upgrades_memory();
let reader = get_reader(&memory);

let (mut data, errors, logs, traces): (Data, Vec<LogEntry>, Vec<LogEntry>, Vec<LogEntry>) =
let (data, errors, logs, traces): (Data, Vec<LogEntry>, Vec<LogEntry>, Vec<LogEntry>) =
msgpack::deserialize(reader).unwrap();

assert!(data.members_migrated_to_stable_memory);

canister_logger::init_with_logs(data.test_mode, errors, logs, traces);

data.events.migrate_to_stable_memory();

let env = init_env(data.rng_seed);
init_state(env, data, args.wasm_version);

Expand Down
151 changes: 6 additions & 145 deletions backend/canisters/community/impl/src/model/events.rs
Original file line number Diff line number Diff line change
@@ -1,82 +1,22 @@
use crate::model::events::stable_memory::EventsStableStorage;
use chat_events::GroupGateUpdatedInternal;
use serde::{Deserialize, Serialize};
use std::collections::BTreeMap;
use tracing::info;
use types::{
AvatarChanged, BannerChanged, BotAdded, BotRemoved, BotUpdated, ChannelDeleted, ChannelId, ChatId,
CommunityMemberLeftInternal, CommunityMembersRemoved, CommunityPermissionsChanged, CommunityRoleChanged,
CommunityUsersBlocked, CommunityVisibilityChanged, DefaultChannelsChanged, EventIndex, EventWrapperInternal, GroupCreated,
GroupDescriptionChanged, GroupFrozen, GroupInviteCodeChanged, GroupNameChanged, GroupRulesChanged, GroupUnfrozen,
MemberJoinedInternal, PrimaryLanguageChanged, TimestampMillis, UserId, UsersInvited, UsersUnblocked,
AvatarChanged, BannerChanged, BotAdded, BotRemoved, BotUpdated, ChannelDeleted, ChannelId, ChatId, CommunityMembersRemoved,
CommunityPermissionsChanged, CommunityRoleChanged, CommunityUsersBlocked, CommunityVisibilityChanged, EventIndex,
EventWrapperInternal, GroupCreated, GroupDescriptionChanged, GroupFrozen, GroupInviteCodeChanged, GroupNameChanged,
GroupRulesChanged, GroupUnfrozen, PrimaryLanguageChanged, TimestampMillis, UserId, UsersInvited, UsersUnblocked,
};

mod stable_memory;

#[derive(Serialize, Deserialize)]
#[serde(from = "CommunityEventsPrevious")]
pub struct CommunityEvents {
events_map: BTreeMap<EventIndex, EventWrapperInternal<CommunityEventInternal>>,
stable_events_map: EventsStableStorage,
latest_event_index: EventIndex,
latest_event_timestamp: TimestampMillis,
}

#[derive(Serialize, Deserialize)]
pub struct CommunityEventsPrevious {
events_map: BTreeMap<EventIndex, EventWrapperInternal<CommunityEventInternalOld>>,
}

#[derive(Serialize, Deserialize, Clone, Debug)]
pub enum CommunityEventInternalOld {
#[serde(rename = "cr", alias = "Created")]
Created(Box<GroupCreated>),
#[serde(rename = "nc", alias = "NameChanged")]
NameChanged(Box<GroupNameChanged>),
#[serde(rename = "dc", alias = "DescriptionChanged")]
DescriptionChanged(Box<GroupDescriptionChanged>),
#[serde(rename = "rc", alias = "RulesChanged")]
RulesChanged(Box<GroupRulesChanged>),
#[serde(rename = "ac", alias = "AvatarChanged")]
AvatarChanged(Box<AvatarChanged>),
#[serde(rename = "bc", alias = "BannerChanged")]
BannerChanged(Box<BannerChanged>),
#[serde(rename = "ui", alias = "UsersInvited")]
UsersInvited(Box<UsersInvited>),
#[serde(rename = "mj", alias = "MemberJoined")]
MemberJoined(Box<MemberJoinedInternal>),
#[serde(rename = "mr", alias = "MembersRemoved")]
MembersRemoved(Box<CommunityMembersRemoved>),
#[serde(rename = "ml", alias = "MemberLeft")]
MemberLeft(Box<CommunityMemberLeftInternal>),
#[serde(rename = "rc", alias = "RoleChanged")]
RoleChanged(Box<CommunityRoleChanged>),
#[serde(rename = "ub", alias = "UsersBlocked")]
UsersBlocked(Box<CommunityUsersBlocked>),
#[serde(rename = "uu", alias = "UsersUnblocked")]
UsersUnblocked(Box<UsersUnblocked>),
#[serde(rename = "pc", alias = "PermissionsChanged")]
PermissionsChanged(Box<CommunityPermissionsChanged>),
#[serde(rename = "vc", alias = "VisibilityChanged")]
VisibilityChanged(Box<CommunityVisibilityChanged>),
#[serde(rename = "ic", alias = "InviteCodeChanged")]
InviteCodeChanged(Box<GroupInviteCodeChanged>),
#[serde(rename = "fr", alias = "Frozen")]
Frozen(Box<GroupFrozen>),
#[serde(rename = "uf", alias = "Unfrozen")]
Unfrozen(Box<GroupUnfrozen>),
#[serde(rename = "gu", alias = "GateUpdated")]
GateUpdated(Box<GroupGateUpdatedInternal>),
#[serde(rename = "cd", alias = "ChannelDeleted")]
ChannelDeleted(Box<ChannelDeleted>),
#[serde(rename = "dcc", alias = "DefaultChannelsChanged")]
DefaultChannelsChanged(Box<DefaultChannelsChanged>),
#[serde(rename = "pl", alias = "PrimaryLanguageChanged")]
PrimaryLanguageChanged(Box<PrimaryLanguageChanged>),
#[serde(rename = "gi", alias = "GroupImported")]
GroupImported(Box<GroupImportedInternal>),
}

#[derive(Serialize, Deserialize, Clone, Debug)]
pub enum CommunityEventInternal {
#[serde(rename = "cr", alias = "Created")]
Expand Down Expand Up @@ -128,14 +68,6 @@ pub enum CommunityEventInternal {
}

impl CommunityEvents {
pub fn migrate_to_stable_memory(&mut self) {
for event in self.events_map.values() {
self.stable_events_map.insert(event.clone());
}
let count = self.events_map.len();
info!(count, "Community events migrated to stable memory");
}

pub fn new(name: String, description: String, created_by: UserId, now: TimestampMillis) -> CommunityEvents {
let event_index = EventIndex::default();
let event = EventWrapperInternal {
Expand All @@ -150,13 +82,10 @@ impl CommunityEvents {
})),
};

let mut events_map = BTreeMap::new();
events_map.insert(event_index, event.clone());
let mut stable_events_map = EventsStableStorage::default();
stable_events_map.insert(event);

CommunityEvents {
events_map,
stable_events_map,
latest_event_index: event_index,
latest_event_timestamp: now,
Expand All @@ -165,16 +94,13 @@ impl CommunityEvents {

pub(crate) fn push_event(&mut self, event: CommunityEventInternal, now: TimestampMillis) -> EventIndex {
let event_index = self.next_event_index();
let event = EventWrapperInternal {
self.stable_events_map.insert(EventWrapperInternal {
index: event_index,
timestamp: now,
correlation_id: 0,
expires_at: None,
event,
};

self.events_map.insert(event_index, event.clone());
self.stable_events_map.insert(event);
});

self.latest_event_index = event_index;
self.latest_event_timestamp = now;
Expand All @@ -201,68 +127,3 @@ pub struct GroupImportedInternal {
pub channel_id: ChannelId,
pub members_added: Vec<UserId>,
}

impl TryFrom<CommunityEventInternalOld> for CommunityEventInternal {
type Error = ();

fn try_from(value: CommunityEventInternalOld) -> Result<Self, Self::Error> {
match value {
CommunityEventInternalOld::Created(e) => Ok(CommunityEventInternal::Created(e)),
CommunityEventInternalOld::NameChanged(e) => Ok(CommunityEventInternal::NameChanged(e)),
CommunityEventInternalOld::DescriptionChanged(e) => Ok(CommunityEventInternal::DescriptionChanged(e)),
CommunityEventInternalOld::RulesChanged(e) => Ok(CommunityEventInternal::RulesChanged(e)),
CommunityEventInternalOld::AvatarChanged(e) => Ok(CommunityEventInternal::AvatarChanged(e)),
CommunityEventInternalOld::BannerChanged(e) => Ok(CommunityEventInternal::BannerChanged(e)),
CommunityEventInternalOld::UsersInvited(e) => Ok(CommunityEventInternal::UsersInvited(e)),
CommunityEventInternalOld::MembersRemoved(e) => Ok(CommunityEventInternal::MembersRemoved(e)),
CommunityEventInternalOld::RoleChanged(e) => Ok(CommunityEventInternal::RoleChanged(e)),
CommunityEventInternalOld::UsersBlocked(e) => Ok(CommunityEventInternal::UsersBlocked(e)),
CommunityEventInternalOld::UsersUnblocked(e) => Ok(CommunityEventInternal::UsersUnblocked(e)),
CommunityEventInternalOld::PermissionsChanged(e) => Ok(CommunityEventInternal::PermissionsChanged(e)),
CommunityEventInternalOld::VisibilityChanged(e) => Ok(CommunityEventInternal::VisibilityChanged(e)),
CommunityEventInternalOld::InviteCodeChanged(e) => Ok(CommunityEventInternal::InviteCodeChanged(e)),
CommunityEventInternalOld::Frozen(e) => Ok(CommunityEventInternal::Frozen(e)),
CommunityEventInternalOld::Unfrozen(e) => Ok(CommunityEventInternal::Unfrozen(e)),
CommunityEventInternalOld::GateUpdated(e) => Ok(CommunityEventInternal::GateUpdated(e)),
CommunityEventInternalOld::ChannelDeleted(e) => Ok(CommunityEventInternal::ChannelDeleted(e)),
CommunityEventInternalOld::PrimaryLanguageChanged(e) => Ok(CommunityEventInternal::PrimaryLanguageChanged(e)),
CommunityEventInternalOld::GroupImported(e) => Ok(CommunityEventInternal::GroupImported(e)),
CommunityEventInternalOld::MemberJoined(_)
| CommunityEventInternalOld::MemberLeft(_)
| CommunityEventInternalOld::DefaultChannelsChanged(_) => Err(()),
}
}
}

impl From<CommunityEventsPrevious> for CommunityEvents {
fn from(value: CommunityEventsPrevious) -> Self {
let mut events_map = BTreeMap::new();
let mut index = EventIndex::default();
for old_event in value.events_map.into_values() {
if let Ok(new_event) = CommunityEventInternal::try_from(old_event.event) {
events_map.insert(
index,
EventWrapperInternal {
index,
timestamp: old_event.timestamp,
correlation_id: 0,
expires_at: None,
event: new_event,
},
);
index = index.incr();
}
}

let last = events_map.values().last().unwrap();
let latest_event_index = last.index;
let latest_event_timestamp = last.timestamp;

CommunityEvents {
events_map,
stable_events_map: EventsStableStorage::default(),
latest_event_index,
latest_event_timestamp,
}
}
}
2 changes: 0 additions & 2 deletions backend/canisters/community/impl/src/model/members.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ const MAX_MEMBERS_PER_COMMUNITY: u32 = 100_000;

#[derive(Serialize, Deserialize)]
pub struct CommunityMembers {
#[serde(alias = "stable_memory_members_map")]
members_map: MembersStableStorage,
member_channel_links: BTreeSet<(UserId, ChannelId)>,
member_channel_links_removed: BTreeMap<(UserId, ChannelId), TimestampMillis>,
Expand All @@ -36,7 +35,6 @@ pub struct CommunityMembers {
members_with_display_names: BTreeSet<UserId>,
members_with_referrals: BTreeSet<UserId>,
updates: BTreeSet<(TimestampMillis, UserId, MemberUpdate)>,
#[serde(default)]
latest_update_removed: TimestampMillis,
}

Expand Down
13 changes: 1 addition & 12 deletions backend/canisters/community/impl/src/timer_job_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use chat_events::MessageContentInternal;
use constants::{DAY_IN_MS, MINUTE_IN_MS, NANOS_PER_MILLISECOND, SECOND_IN_MS};
use ledger_utils::process_transaction;
use serde::{Deserialize, Serialize};
use tracing::{error, info};
use tracing::error;
use types::{
BlobReference, CanisterId, ChannelId, ChatId, MessageId, MessageIndex, P2PSwapStatus, PendingCryptoTransaction, UserId,
};
Expand All @@ -27,7 +27,6 @@ pub enum TimerJob {
CancelP2PSwapInEscrowCanister(CancelP2PSwapInEscrowCanisterJob),
MarkP2PSwapExpired(MarkP2PSwapExpiredJob),
MarkVideoCallEnded(MarkVideoCallEndedJob),
MigrateMembersToStableMemory(MigrateMembersToStableMemoryJob),
}

#[derive(Serialize, Deserialize, Clone)]
Expand Down Expand Up @@ -138,9 +137,6 @@ pub struct MarkP2PSwapExpiredJob {
#[derive(Serialize, Deserialize, Clone)]
pub struct MarkVideoCallEndedJob(pub community_canister::end_video_call::Args);

#[derive(Serialize, Deserialize, Clone)]
pub struct MigrateMembersToStableMemoryJob;

impl Job for TimerJob {
fn execute(self) {
if can_borrow_state() {
Expand All @@ -161,7 +157,6 @@ impl Job for TimerJob {
TimerJob::CancelP2PSwapInEscrowCanister(job) => job.execute(),
TimerJob::MarkP2PSwapExpired(job) => job.execute(),
TimerJob::MarkVideoCallEnded(job) => job.execute(),
TimerJob::MigrateMembersToStableMemory(job) => job.execute(),
}
}
}
Expand Down Expand Up @@ -455,9 +450,3 @@ impl Job for MarkVideoCallEndedJob {
mutate_state(|state| end_video_call_impl(self.0, state));
}
}

impl Job for MigrateMembersToStableMemoryJob {
fn execute(self) {
info!("MigrateMembersToStableMemoryJob executed")
}
}

0 comments on commit 616fb1f

Please sign in to comment.